summaryrefslogtreecommitdiffstats
path: root/src/conntrack.c
Commit message (Collapse)AuthorAgeFilesLines
* conntrack: don't print [USERSPACE] information in case of XML outputIgnacy Gawędzki2024-02-071-1/+1
| | | | | | | | In case XML output is requested, refrain from appending "[USERSPACE]" and details to the output. Signed-off-by: Ignacy Gawędzki <ignacy.gawedzki@green-communications.fr> Signed-off-by: Florian Westphal <fw@strlen.de>
* conntrack: ct label update requires proper rulesetPablo Neira Ayuso2023-10-131-0/+5
| | | | | | | | | | | | | | As of kernel 6.6-rc, your ruleset must use either the 'connlabel' match in iptables or the 'ct label' statement in nftables to attach labels to conntrack entries. Update documentation to describe this behaviour. This patch addresses a corner case scenario: conntrack already contains entries but ruleset that specifies connlabel did not get loaded yet. In such case, skip ENOSPC errors for conntracks that have no ct label extension. Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1622 Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack, nfct: fix some typo'sJeremy Sowden2023-08-241-1/+1
| | | | | | | Four misspellings and a missing pronoun. Signed-off-by: Jeremy Sowden <jeremy@azazel.net> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: do not set on NLM_F_ACK in IPCTNL_MSG_CT_GET requestsPablo Neira Ayuso2023-08-221-2/+2
| | | | | | | | | | | | | | | | | | | GET requests report either error via NLMSG_ERROR or the corresponding entry, therefore, there is always a reply from the kernel. The NLM_F_ACK flag results in two netlink messages as reply in case of success for GET requests, one containing the entry and another with the explicit acknowledgment. nfct_mnl_request() leaves the explicit acknowledment in the buffer, filling it up with unhandled netlink messages, leading to the following error: conntrack v1.4.7 (conntrack-tools): Operation failed: No buffer space available Fixes: b7a396b70015 ("conntrack: use libmnl for updating conntrack table") Reported-by: Tony He <huangya90@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: skip ENOENT when -U/-D finds a stale conntrack entryPablo Neira Ayuso2023-08-221-3/+11
| | | | | | | | | | | | | | -U and -D commands iterate over the netlink dump and it might try to update/delete an entry which is not in the kernel anymore. Skip ENOENT errors. The -U command uses printf and it continues on error, instead bail out with exit_error(). This problem is present in 1.4.6, this is related to the recent updates to use libmnl in 1.4.7. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: unbreak -U command, use correct familyPablo Neira Ayuso2023-08-221-2/+4
| | | | | | | | | | | | | Set the family from the conntrack object, otherwise, if -f is not specified and the kernel bails out with: # conntrack -U -p tcp -m 1 Operation failed: Not supported conntrack v1.4.7 (conntrack-tools): Operation failed: Not supported Fixes: b7a396b70015 ("conntrack: use libmnl for updating conntrack table") Reported-by: Tony He <huangya90@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: don't override mark when filtering by statusJacek Tomasiak2023-06-151-2/+1
| | | | | | | | | | | | | | | | | When creating new rules with (e.g. with `conntrack -I -m 123 -u UNSET ...`), the mark from `-m` was overriden by value from `-u`. This behavior was introduced in 1a5828f491c6a1593f30cb5f1551fe9f9cf76a8d ("conntrack: enable kernel-based status filtering with -L -u STATUS") for filtering the output of `-L` option but caused a regression in other cases. [ fw@strlen.de: likely copy&waste bug, status and mark are completely different things ] Fixes: 1a5828f491c6 ("conntrack: enable kernel-based status filtering with -L -u STATUS") Signed-off-by: Jacek Tomasiak <jtomasiak@arista.com> Signed-off-by: Jacek Tomasiak <jacek.tomasiak@gmail.com> Signed-off-by: Florian Westphal <fw@strlen.de>
* conntrack: do not silence EEXIST error, use NLM_F_EXCLPablo Neira Ayuso2023-05-181-6/+8
| | | | | | | | | | | | Instead of silencing EEXIST error with -A/--add, unset NLM_F_EXCL netlink flag. Do not ignore error from kernel for command invocation. This patch revisits e42ea65e9c93 ("conntrack: introduce new -A command"). Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: Sanitize free_tmpl_objects()Phil Sutter2022-12-211-0/+2
| | | | | | | | | | | The function unconditionally dereferenced its parameter, yet it is possible for the passed 'cur_tmpl' pointer when called from exit_error() to be still NULL: It is assigned to by alloc_tmpl_objects() at start of do_parse(), though callers of that function might call exit_error() in beforehand. Fixes: 258b4540f4512 ("conntrack: add struct ct_tmpl") Signed-off-by: Phil Sutter <phil@nwl.cc>
* conntrack: Fix for unused assignment in ct_save_snprintf()Phil Sutter2022-12-211-1/+0
| | | | | | | Setting 'ret' without calling BUFFER_SIZE() is pointless. Fixes: 1c596b9ec8f26 ("conntrack: implement save output format") Signed-off-by: Phil Sutter <phil@nwl.cc>
* conntrack: Fix for unused assignment in do_command_ct()Phil Sutter2022-12-211-1/+1
| | | | | | | | | | | The variable is overwritten immediately in the next iteration and the loop can't exit before doing that. Instead of dropping the assignment, one could add a return code check - but since event_cb() never fails, that check is pointless as well. Fixes: e0dac21ed02e3 ("conntrack: use libmnl for conntrack events") Signed-off-by: Phil Sutter <phil@nwl.cc>
* conntrack: Fix potential array out of bounds accessPhil Sutter2022-12-211-1/+1
| | | | | | | | If the link target length exceeds 'sizeof(tmp)' bytes, readlink() will return 'sizeof(tmp)'. Using this value as index is illegal. Fixes: b031cd2102d9b ("conntrack: pretty-print the portid") Signed-off-by: Phil Sutter <phil@nwl.cc>
* conntrack: slightly simplify parse_proto_num() by using strtoul()Pablo Neira Ayuso2022-07-111-3/+2
| | | | | | Use strtoul() instead and remove check for negative value. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: use IPPROTO_RAWPablo Neira Ayuso2022-07-111-1/+1
| | | | | | | | | IPPROTO_MPTCP defeats the purpose of IPPROTO_MAX to check for the maximum layer 4 protocol supported in the IP header. Use IPPROTO_RAW (255) instead. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: introduce new -A commandMikhail Sennikovsky2022-07-081-8/+17
| | | | | | | | | | | | | | The -A command works exactly the same way as -I except that it does not fail if the ct entry already exists. This command is useful for the batched ct loads to not abort if some entries being applied exist. The ct entry dump in the "save" format is now switched to use the -A command as well for the generated output. Also tests added to cover the -A command. Signed-off-by: Mikhail Sennikovsky <mikhail.sennikovskii@ionos.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: use C99 initializer syntax for option mapMikhail Sennikovsky2022-07-081-82/+20
| | | | | | | | | | | | | The old way of the commands_v_options initialization made it more difficult and error-prone to add a map for a new command, because one would have to calculate a proper "index" for the initializer and fill the gap with zeros. As a preparation step for adding the new "-A" command support, switch to C99 initializer syntax for commands_v_options. Signed-off-by: Mikhail Sennikovsky <mikhail.sennikovskii@ionos.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: generalize command parsingMikhail Sennikovsky2022-07-081-13/+10
| | | | | | | | | | | | | Currently the -U command has a special case handling in the do_parse because it does not have EXP_ counterpart. Generalizing it would simplify adding support for new commands w/o EXP_ counterpart. As a preparation step for adding the new "-A" command support, make the -U command be handled the same way as the rest. Signed-off-by: Mikhail Sennikovsky <mikhail.sennikovskii@ionos.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: fix -o save dump for unknown protocolsMikhail Sennikovsky2022-06-271-0/+9
| | | | | | | | | | | | | | Make sure the protocol (-p) option is included in the -o save ct entry dumps for L4 protocols unknown to the conntrack tool. Do not use getprotobynumber for unknown protocols to ensure "-o save" data incompatibility between hosts having different /etc/protocols contents. Include testcases covering the issue. Signed-off-by: Mikhail Sennikovsky <mikhail.sennikovskii@ionos.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: fix protocol number parsingMikhail Sennikovsky2022-06-271-2/+17
| | | | | | | | | | | | Before this commit it was possible to successfully create a ct entry passing -p 256 and -p some_nonsense. In both cases an entry with the protocol=0 would be created. Do not allow invalid protocol values to -p option. Include testcases covering the issue. Signed-off-by: Mikhail Sennikovsky <mikhail.sennikovskii@ionos.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: update CT_GET to use libmnlPablo Neira Ayuso2022-06-201-48/+3
| | | | | | | | Use nfct_mnl_request() to build and send the netlink command. Remove dump_cb() since this is a copy of the new libmnl's mnl_nfct_dump_cb() callback function. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: pass command object to nfct_mnl_request()Pablo Neira Ayuso2022-06-201-11/+14
| | | | | | | This patch comes in preparation for updating the CT_GET command to use libmnl. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: use same modifier socket for bulk opsMikhail Sennikovsky2022-06-201-6/+21
| | | | | | | | | | For bulk ct entry loads (with -R option) reusing the same mnl modifier socket for all entries results in reduction of entries creation time, which becomes especially signifficant when loading tens of thouthand of entries. Signed-off-by: Mikhail Sennikovsky <mikhail.sennikovskii@ionos.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: unbreak event modeFlorian Westphal2022-05-161-1/+1
| | | | | | | | read() occurs from the wrong socket so 'conntrack -E' hangs without reporting any events. Fixes: 5ec684be0854 ("conntrack: consolidate socket open call") Signed-off-by: Florian Westphal <fw@strlen.de>
* conntrack: remove -o userspaceFlorian Westphal2022-05-161-4/+3
| | | | | | | | | | This flag makes life a lot harder because lack of the flag hides very useful information. Remove it and always tag events triggered by userspace flush. Option is still parsed for backwards compatibility sake. Signed-off-by: Florian Westphal <fw@strlen.de>
* conntrack: consolidate socket open callPablo Neira Ayuso2022-05-021-48/+17
| | | | | | | Create netlink socket once and reuse it, rather than open + close it over and over again. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* connntrack: Fix for memleak when parsing -j argPhil Sutter2022-03-281-0/+2
| | | | | | | Have to free the strings allocated by split_address_and_port(). Fixes: 29b390a212214 ("conntrack: Support IPv6 NAT") Signed-off-by: Phil Sutter <phil@nwl.cc>
* conntrack: use libmnl for flushing conntrack tableMikhail Sennikovsky2022-03-081-5/+8
| | | | | | | | Use libmnl and libnetfilter_conntrack mnl helpers to flush the conntrack table entries. Signed-off-by: Mikhail Sennikovsky <mikhail.sennikovskii@ionos.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: use libmnl for ct entries deletionMikhail Sennikovsky2022-03-081-19/+28
| | | | | | | | Use libmnl and libnetfilter_conntrack mnl helpers to delete the conntrack table entries. Signed-off-by: Mikhail Sennikovsky <mikhail.sennikovskii@ionos.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: use libmnl for updating conntrack tableMikhail Sennikovsky2022-03-081-43/+61
| | | | | | | | Use libmnl and libnetfilter_conntrack mnl helpers to update the conntrack table entries. Signed-off-by: Mikhail Sennikovsky <mikhail.sennikovskii@ionos.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: add nfct_mnl_request()Pablo Neira Ayuso2022-03-081-28/+17
| | | | | | | Provide a helper function to build and send the netlink request, this allows to consolidate nfct_mnl_get() and nfct_mnl_create(). Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: rename nfct_mnl_recv() to __nfct_mnl_dump()Pablo Neira Ayuso2022-03-081-3/+3
| | | | | | | nfct_mnl_recv() is misleading, this helper function allows you to perform a netlink dump, rename it. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: pass sock to nfct_mnl_*() functionsMikhail Sennikovsky2021-12-241-53/+62
| | | | | | | | In preparation for using multiple instances of mnl sockets required for conntrack entries update and delete support. Signed-off-by: Mikhail Sennikovsky <mikhail.sennikovskii@ionos.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: use libmnl to create entryPablo Neira Ayuso2021-12-241-6/+26
| | | | | | | Use libmnl to create entries through the new nfct_mnl_create() helper function. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: add netlink flags to nfct_mnl_nlmsghdr_put()Pablo Neira Ayuso2021-12-241-4/+4
| | | | | | | Moreover, remove NLM_F_DUMP for IPCTNL_MSG_CT_GET_STATS since ctnetlink ignores this flag, this is simple netlink get command, not a dump. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: add nfct_mnl_talk() and nfct_mnl_recv() helper functionsPablo Neira Ayuso2021-12-241-20/+34
| | | | | | Add helper function to consolidate nfct_mnl_dump() and nfct_mnl_get(). Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: use libmnl for listing conntrack tablePablo Neira Ayuso2021-11-141-20/+16
| | | | | | | Use libmnl and libnetfilter_conntrack mnl helpers to dump the conntrack table entries. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: enhance mnl_nfct_dump_cb()Pablo Neira Ayuso2021-11-141-12/+39
| | | | | | Add missing features in dump_cb() to mnl_nfct_dump_cb(). Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: pass filter_dump object to nfct_mnl_dump()Pablo Neira Ayuso2021-11-141-5/+10
| | | | | | In preparation for kernel filtering support for nfct_mnl_dump(). Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: add support for chaintoolong stat counterFlorian Westphal2021-09-081-3/+4
| | | | | | | | | | | | | | | | | While at it, also fix up commit 5ec55c2a0a264 ("conntrack: add support for CLASH_RESOLVED counter"). The intention was to also print unknown statistic counters provided the attributes are of type u32, but mnl_attr_type_valid() needs the correct max-attr number for this to work. With this fix in place, conntrack -S shows: cpu=3 found=0 inv[..] clash_resolve=0 unknown1=8200 on a modified kernel. Signed-off-by: Florian Westphal <fw@strlen.de>
* conntrack: add shorthand mnemonic for UNREPLIEDFlorian Westphal2021-08-051-0/+12
| | | | | | | | | conntrack tool prints [UNREPLIED] if a conntrack entry lacks the SEEN_REPLY bit. Accept this as '-u' argument too. If requested, mask is set to SEEN_REPLY and value remains 0 (bit not set). Signed-off-by: Florian Westphal <fw@strlen.de>
* conntrack: enable kernel-based status filtering with -L -u STATUSFlorian Westphal2021-08-051-4/+55
| | | | | | | | | This change is backwards compatible: Old kernels do not recognize CTA_STATUS_MASK attribute and will ignore it (no filtering in kernel). Signed-off-by: Florian Westphal <fw@strlen.de>
* conntrack: accept commands from fileMikhail Sennikovsky2021-05-031-6/+219
| | | | | | | | | | | | | | | | | | | | | | | | | | This commit implements the --load-file option which allows processing conntrack commands stored in file. Most often this would be used as a counter-part for the -o save option, which outputs conntrack entries in the format of the conntrack tool options. This could be useful when one needs to add/update/delete a large set of ct entries with a single conntrack tool invocation. This patch introduces a ct_cmd_list, which represents a list of ct_cmd elements. Expected syntax is "conntrack --load-file file". If "-" is given as a file name, stdin is used. No other commands or options are allowed to be specified in conjunction with the --load-file command. It is however possible to specify multiple --load-file file pairs. Example: Copy all entries from ct zone 11 to ct zone 12: conntrack -L -w 11 -o save | sed "s/-w 11/-w 12/g" | \ conntrack --load-file - Joint work with Pablo. Signed-off-by: Mikhail Sennikovsky <mikhail.sennikovskii@ionos.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: release options after parsingPablo Neira Ayuso2021-05-031-1/+2
| | | | | | | | | | | | | | | Fix memleak in parser: ==8445== 3,808 bytes in 2 blocks are definitely lost in loss record 6 of 6 ==8445== at 0x483577F: malloc (vg_replace_malloc.c:299) ==8445== by 0x112636: merge_options (conntrack.c:1056) ==8445== by 0x112636: do_parse (conntrack.c:2903) ==8445== by 0x11343E: ct_file_parse_line (conntrack.c:3672) ==8445== by 0x11343E: ct_parse_file (conntrack.c:3693) ==8445== by 0x10D819: main (conntrack.c:3750) Fixes: 8f76d6360dbf ("conntrack: add struct ct_cmd") Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: add function to print command statsPablo Neira Ayuso2021-03-151-8/+17
| | | | | | Wrap code to display command stats in a function. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: move options flag to ct_cmd objectPablo Neira Ayuso2021-03-151-48/+59
| | | | | | Prepare for the batch support. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: pass cmd to filter nat, mark and network functionsPablo Neira Ayuso2021-03-151-10/+11
| | | | | | | Pass the command object to the nat, mark and IP address userspace filters. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: pass cmd to nfct_filter()Pablo Neira Ayuso2021-03-151-10/+9
| | | | | | Pass the command object to the userspace filter routine. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: pass ct_cmd to nfct_filter_init()Pablo Neira Ayuso2021-03-151-6/+8
| | | | | | Pass command object to initialize the userspace filter. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: pass command object to callbacksPablo Neira Ayuso2021-03-151-23/+27
| | | | | | | | | Pass the command object to prepare for batch support. Move ct_cmd structure definition right at the top of file otherwise compilation breaks. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
* conntrack: add do_command_ct()Pablo Neira Ayuso2021-01-141-20/+24
| | | | | | Wrap the code to run the command around the do_command_ct() function. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>