Netfilter updates for net-next
Subsystems (9)
Pablo Neira Ayuso
maintainercover Netfilter updates for net-next
Hi David,
The following patchset contains Netfilter updates for your net-next tree, they are:
1) Stash ctinfo 3-bit field into pointer to nf_conntrack object from sk_buff so we only access one single cacheline in the conntrack hotpath. Patchset from Florian Westphal.
2) Don't leak pointer to internal structures when exporting x_tables ruleset back to userspace, from Willem DeBruijn. This includes new helper functions to copy data to userspace such as xt_data_to_user() as well as conversions of our ip_tables, ip6_tables and arp_tables clients to use it. Not surprinsingly, ebtables requires an ad-hoc update. There is also a new field in x_tables extensions to indicate the amount of bytes that we copy to userspace.
3) Add nf_log_all_netns sysctl: This new knob allows you to enable logging via nf_log infrastructure for all existing netnamespaces. Given the effort to provide pernet syslog has been discontinued, let's provide a way to restore logging using netfilter kernel logging facilities in trusted environments. Patch from Michal Kubecek.
4) Validate SCTP checksum from conntrack helper, from Davide Caratti.
5) Merge UDPlite conntrack and NAT helpers into UDP, this was mostly a copy&paste from the original helper, from Florian Westphal.
6) Reset netfilter state when duplicating packets, also from Florian.
7) Remove unnecessary check for broadcast in IPv6 in pkttype match and nft_meta, from Liping Zhang.
8) Add missing code to deal with loopback packets from nft_meta when used by the netdev family, also from Liping.
9) Several cleanups on nf_tables, one to remove unnecessary check from the netlink control plane path to add table, set and stateful objects and code consolidation when unregister chain hooks, from Gao Feng.
10) Fix harmless reference counter underflow in IPVS that, however, results in problems with the introduction of the new refcount_t type, from David Windsor.
11) Enable LIBCRC32C from nf_ct_sctp instead of nf_nat_sctp, from Davide Caratti.
12) Missing documentation on nf_tables uapi header, from Liping Zhang.
13) Use rb_entry() helper in xt_connlimit, from Geliang Tang.
You can pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git
Thanks!
----------------------------------------------------------------
The following changes since commit 0a0a8d6b0e88d947d7ab3198b325e31f677bebc2:
net: fealnx: use new api ethtool_{get|set}_link_ksettings (2017-01-02 16:59:10 -0500)
are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git HEAD
for you to fetch changes up to 2851940ffee313e0ff12540a8e11a8c54dea9c65:
netfilter: allow logging from non-init namespaces (2017-02-02 14:31:58 +0100)
----------------------------------------------------------------
David Windsor (1):
ipvs: free ip_vs_dest structs when refcnt=0
Davide Caratti (2):
netfilter: select LIBCRC32C together with SCTP conntrack
netfilter: conntrack: validate SCTP crc32c in PREROUTING
Feng (1):
netfilter: nf_tables: Eliminate duplicated code in nf_tables_table_enable()
Florian Westphal (9):
netfilter: merge udp and udplite conntrack helpers
netfilter: nat: merge udp and udplite helpers
netfilter: conntrack: no need to pass ctinfo to error handler
netfilter: reset netfilter state when duplicating packet
netfilter: reduce direct skb->nfct usage
skbuff: add and use skb_nfct helper
netfilter: add and use nf_ct_set helper
netfilter: guarantee 8 byte minalign for template addresses
netfilter: merge ctinfo into nfct pointer storage area
Gao Feng (1):
netfilter: nf_tables: eliminate useless condition checks
Geliang Tang (1):
netfilter: xt_connlimit: use rb_entry()
Liping Zhang (4):
netfilter: nf_tables: add missing descriptions in nft_ct_keys
netfilter: nft_ct: add average bytes per packet support
netfilter: pkttype: unnecessary to check ipv6 multicast address
netfilter: nft_meta: deal with PACKET_LOOPBACK in netdev family
Michal Kubeček (1):
netfilter: allow logging from non-init namespaces
Willem de Bruijn (7):
xtables: add xt_match, xt_target and data copy_to_user functions
iptables: use match, target and data copy_to_user helpers
ip6tables: use match, target and data copy_to_user helpers
arptables: use match, target and data copy_to_user helpers
ebtables: use match, target and data copy_to_user helpers
xtables: use match, target and data copy_to_user helpers in compat
xtables: extend matches and targets with .usersize
Documentation/networking/netfilter-sysctl.txt | 10 + include/linux/netfilter/x_tables.h | 9 + include/linux/skbuff.h | 32 +-- include/net/ip_vs.h | 12 +- include/net/netfilter/ipv4/nf_conntrack_ipv4.h | 1 + include/net/netfilter/ipv6/nf_conntrack_ipv6.h | 1 + include/net/netfilter/nf_conntrack.h | 17 +- include/net/netfilter/nf_conntrack_core.h | 2 +- include/net/netfilter/nf_conntrack_l4proto.h | 2 +- include/net/netfilter/nf_log.h | 3 + include/net/netns/conntrack.h | 16 -- include/uapi/linux/netfilter/nf_tables.h | 5 + net/bridge/netfilter/ebt_limit.c | 1 + net/bridge/netfilter/ebt_log.c | 2 +- net/bridge/netfilter/ebtables.c | 78 +++--- net/core/skbuff.c | 2 +- net/ipv4/netfilter/arp_tables.c | 15 +- net/ipv4/netfilter/ip_tables.c | 21 +- net/ipv4/netfilter/ipt_CLUSTERIP.c | 1 + net/ipv4/netfilter/ipt_SYNPROXY.c | 11 +- net/ipv4/netfilter/nf_conntrack_proto_icmp.c | 15 +- net/ipv4/netfilter/nf_defrag_ipv4.c | 4 +- net/ipv4/netfilter/nf_dup_ipv4.c | 7 +- net/ipv4/netfilter/nf_log_arp.c | 2 +- net/ipv4/netfilter/nf_log_ipv4.c | 2 +- net/ipv6/netfilter/ip6_tables.c | 21 +- net/ipv6/netfilter/ip6t_NPT.c | 2 + net/ipv6/netfilter/ip6t_SYNPROXY.c | 11 +- net/ipv6/netfilter/nf_conntrack_proto_icmpv6.c | 20 +- net/ipv6/netfilter/nf_defrag_ipv6_hooks.c | 4 +- net/ipv6/netfilter/nf_dup_ipv6.c | 7 +- net/ipv6/netfilter/nf_log_ipv6.c | 2 +- net/netfilter/Kconfig | 2 +- net/netfilter/Makefile | 2 - net/netfilter/core.c | 2 +- net/netfilter/ipvs/ip_vs_ctl.c | 8 +- net/netfilter/nf_conntrack_core.c | 73 +++--- net/netfilter/nf_conntrack_proto_dccp.c | 1 - net/netfilter/nf_conntrack_proto_sctp.c | 32 +++ net/netfilter/nf_conntrack_proto_tcp.c | 1 - net/netfilter/nf_conntrack_proto_udp.c | 124 +++++++++- net/netfilter/nf_conntrack_proto_udplite.c | 324 ------------------------- net/netfilter/nf_conntrack_standalone.c | 3 + net/netfilter/nf_log.c | 24 ++ net/netfilter/nf_nat_helper.c | 2 +- net/netfilter/nf_nat_proto_udp.c | 78 +++++- net/netfilter/nf_nat_proto_udplite.c | 73 ------ net/netfilter/nf_tables_api.c | 63 +++-- net/netfilter/nft_ct.c | 25 +- net/netfilter/nft_meta.c | 31 ++- net/netfilter/x_tables.c | 68 +++++- net/netfilter/xt_CT.c | 15 +- net/netfilter/xt_RATEEST.c | 1 + net/netfilter/xt_TEE.c | 2 + net/netfilter/xt_bpf.c | 2 + net/netfilter/xt_cgroup.c | 1 + net/netfilter/xt_connlimit.c | 5 +- net/netfilter/xt_hashlimit.c | 4 + net/netfilter/xt_limit.c | 2 + net/netfilter/xt_pkttype.c | 3 +- net/netfilter/xt_quota.c | 1 + net/netfilter/xt_rateest.c | 1 + net/netfilter/xt_string.c | 1 + net/openvswitch/conntrack.c | 12 +- net/sched/cls_flow.c | 2 +- 65 files changed, 646 insertions(+), 680 deletions(-) create mode 100644 Documentation/networking/netfilter-sysctl.txt delete mode 100644 net/netfilter/nf_conntrack_proto_udplite.c delete mode 100644 net/netfilter/nf_nat_proto_udplite.c
Pablo Neira Ayuso
maintainerPATCH 1/27 netfilter: merge udp and udplite conntrack helpers
From: Florian Westphal <[email protected]>
udplite was copied from udp, they are virtually 100% identical.
This adds udplite tracker to udp instead, removes udplite module, and then makes the udplite tracker builtin.
udplite will then simply re-use udp timeout settings. It makes little sense to add separate sysctls, nowadays we have fine-grained timeout policy support via the CT target.
old:
text data bss dec hex filename 1633 672 0 2305 901 nf_conntrack_proto_udp.o 1756 672 0 2428 97c nf_conntrack_proto_udplite.o
69526 17937 268 87731 156b3 nf_conntrack.ko
new:
text data bss dec hex filename 2442 1184 0 3626 e2a nf_conntrack_proto_udp.o
68565 17721 268 86554 1521a nf_conntrack.ko
--- include/net/netfilter/ipv4/nf_conntrack_ipv4.h | 1 + include/net/netfilter/ipv6/nf_conntrack_ipv6.h | 1 + include/net/netns/conntrack.h | 16 -- net/netfilter/Makefile | 1 - net/netfilter/nf_conntrack_proto_udp.c | 123 ++++++++++ net/netfilter/nf_conntrack_proto_udplite.c | 324 ------------------------- 6 files changed, 125 insertions(+), 341 deletions(-) delete mode 100644 net/netfilter/nf_conntrack_proto_udplite.c
include/net/netfilter/ipv4/nf_conntrack_ipv4.h +1 -0
@@ -14,6 +14,7 @@ extern struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4;extern struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp4;extern struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4; +extern struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4;extern struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp;#ifdef CONFIG_NF_CT_PROTO_DCCP extern struct nf_conntrack_l4proto nf_conntrack_l4proto_dccp4;
include/net/netfilter/ipv6/nf_conntrack_ipv6.h +1 -0
@@ -5,6 +5,7 @@ extern struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6;extern struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp6;extern struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6; +extern struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6;extern struct nf_conntrack_l4proto nf_conntrack_l4proto_icmpv6;#ifdef CONFIG_NF_CT_PROTO_DCCP extern struct nf_conntrack_l4proto nf_conntrack_l4proto_dccp6;
include/net/netns/conntrack.h +0 -16
@@ -69,19 +69,6 @@ struct nf_sctp_net { };#endif -#ifdef CONFIG_NF_CT_PROTO_UDPLITE -enum udplite_conntrack { -UDPLITE_CT_UNREPLIED , -UDPLITE_CT_REPLIED , -UDPLITE_CT_MAX -}; - -struct nf_udplite_net { -struct nf_proto_net pn; -unsigned int timeouts[UDPLITE_CT_MAX ]; -}; -#endif -struct nf_ip_net {struct nf_generic_net generic;struct nf_tcp_net tcp;
@@ -94,9 +81,6 @@ struct nf_ip_net {#ifdef CONFIG_NF_CT_PROTO_SCTP struct nf_sctp_net sctp;#endif -#ifdef CONFIG_NF_CT_PROTO_UDPLITE -struct nf_udplite_net udplite; -#endif };struct ct_pcpu {
net/netfilter/Makefile +0 -1
@@ -7,7 +7,6 @@ nf_conntrack-$(CONFIG_NF_CONNTRACK_EVENTS) += nf_conntrack_ecache.o nf_conntrack-$(CONFIG_NF_CONNTRACK_LABELS) += nf_conntrack_labels.o nf_conntrack-$(CONFIG_NF_CT_PROTO_DCCP) += nf_conntrack_proto_dccp.o nf_conntrack-$(CONFIG_NF_CT_PROTO_SCTP) += nf_conntrack_proto_sctp.o -nf_conntrack-$(CONFIG_NF_CT_PROTO_UDPLITE) += nf_conntrack_proto_udplite.o obj-$(CONFIG_NETFILTER) = netfilter.o
net/netfilter/nf_conntrack_proto_udp.c +123 -0
138 lines not on this page · raw message
net/netfilter/nf_conntrack_proto_udplite.c deleted +0 -324
324 lines not on this page · raw message
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 2/27 netfilter: nat: merge udp and udplite helpers
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
udplite nat was copied from udp nat, they are virtually 100% identical. Not really surprising given udplite is just udp with partial csum coverage.
old:
text data bss dec hex filename
11606 1457 210 13273 33d9 nf_nat.ko
330 0 2 332 14c nf_nat_proto_udp.o
276 0 2 278 116 nf_nat_proto_udplite.o
new:
text data bss dec hex filename
11598 1457 210 13265 33d1 nf_nat.ko
640 0 4 644 284 nf_nat_proto_udp.o--- net/netfilter/Makefile | 1 - net/netfilter/nf_nat_proto_udp.c | 78 ++++++++++++++++++++++++++++++------ net/netfilter/nf_nat_proto_udplite.c | 73 --------------------------------- 3 files changed, 66 insertions(+), 86 deletions(-) delete mode 100644 net/netfilter/nf_nat_proto_udplite.c
net/netfilter/Makefile +0 -1
@@ -46,7 +46,6 @@ nf_nat-y := nf_nat_core.o nf_nat_proto_unknown.o nf_nat_proto_common.o \ # NAT protocols (nf_nat) nf_nat-$(CONFIG_NF_NAT_PROTO_DCCP) += nf_nat_proto_dccp.o nf_nat-$(CONFIG_NF_NAT_PROTO_SCTP) += nf_nat_proto_sctp.o -nf_nat-$(CONFIG_NF_NAT_PROTO_UDPLITE) += nf_nat_proto_udplite.o # generic transport layer logging obj-$(CONFIG_NF_LOG_COMMON) += nf_log_common.o
net/netfilter/nf_nat_proto_udp.c +66 -12
102 lines not on this page · raw message
net/netfilter/nf_nat_proto_udplite.c deleted +0 -73
73 lines not on this page · raw message
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 3/27 netfilter: nf_tables: add missing descriptions in nft_ct_keys
In reply to Pablo Neira AyusoFrom: Liping Zhang <[email protected]>
We missed to add descriptions about NFT_CT_LABELS, NFT_CT_PKTS and NFT_CT_BYTES, now add it.
--- include/uapi/linux/netfilter/nf_tables.h | 3 +++ 1 file changed, 3 insertions(+)
include/uapi/linux/netfilter/nf_tables.h +3 -0
@@ -860,6 +860,9 @@ enum nft_rt_attributes {* @NFT_CT_PROTOCOL: conntrack layer 4 protocol * @NFT_CT_PROTO_SRC: conntrack layer 4 protocol source * @NFT_CT_PROTO_DST: conntrack layer 4 protocol destination +* @NFT_CT_LABELS: conntrack labels +* @NFT_CT_PKTS: conntrack packets +* @NFT_CT_BYTES: conntrack bytes */ enum nft_ct_keys {NFT_CT_STATE ,
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 4/27 netfilter: nft_ct: add average bytes per packet support
In reply to Pablo Neira AyusoFrom: Liping Zhang <[email protected]>
Similar to xt_connbytes, user can match how many average bytes per packet a connection has transferred so far.
--- include/uapi/linux/netfilter/nf_tables.h | 2 ++ net/netfilter/nft_ct.c | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-)
include/uapi/linux/netfilter/nf_tables.h +2 -0
@@ -863,6 +863,7 @@ enum nft_rt_attributes {* @NFT_CT_LABELS: conntrack labels * @NFT_CT_PKTS: conntrack packets * @NFT_CT_BYTES: conntrack bytes +* @NFT_CT_AVGPKT: conntrack average bytes per packet */ enum nft_ct_keys {NFT_CT_STATE ,
@@ -881,6 +882,7 @@ enum nft_ct_keys {NFT_CT_LABELS ,NFT_CT_PKTS ,NFT_CT_BYTES , + NFT_CT_AVGPKT, };/**
net/netfilter/nft_ct.c +21 -1
@@ -129,6 +129,22 @@ static void nft_ct_get_eval(const struct nft_expr *expr,memcpy (dest, &count,sizeof (count));return ; } +case NFT_CT_AVGPKT : { +const struct nf_conn_acct *acct =nf_conn_acct_find (ct); +u64 avgcnt =0 , bcnt =0 , pcnt =0 ; + +if (acct) { + pcnt =nft_ct_get_eval_counter (acct->counter, +NFT_CT_PKTS , priv->dir); + bcnt =nft_ct_get_eval_counter (acct->counter, +NFT_CT_BYTES , priv->dir); +if (pcnt !=0 ) + avgcnt =div64_u64 (bcnt, pcnt); + } + +memcpy (dest, &avgcnt,sizeof (avgcnt)); +return ; + }case NFT_CT_L3PROTOCOL : *dest =nf_ct_l3num (ct);return ;
@@ -316,6 +332,7 @@ static int nft_ct_get_init(const struct nft_ctx *ctx,break ;case NFT_CT_BYTES :case NFT_CT_PKTS : +case NFT_CT_AVGPKT :/* no direction? return sum of original + reply */ if (tb[NFTA_CT_DIRECTION ] ==NULL ) priv->dir =IP_CT_DIR_MAX ;
@@ -346,7 +363,9 @@ static int nft_ct_get_init(const struct nft_ctx *ctx,if (err <0 )return err; -if (priv->key ==NFT_CT_BYTES || priv->key ==NFT_CT_PKTS ) +if (priv->key ==NFT_CT_BYTES || + priv->key ==NFT_CT_PKTS || + priv->key ==NFT_CT_AVGPKT )nf_ct_set_acct (ctx->net, true);return 0 ;
@@ -445,6 +464,7 @@ static int nft_ct_get_dump(struct sk_buff *skb, const struct nft_expr *expr)break ;case NFT_CT_BYTES :case NFT_CT_PKTS : +case NFT_CT_AVGPKT :if (priv->dir <IP_CT_DIR_MAX &&nla_put_u8 (skb,NFTA_CT_DIRECTION , priv->dir)) goto nla_put_failure;
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 5/27 netfilter: select LIBCRC32C together with SCTP conntrack
In reply to Pablo Neira AyusoFrom: Davide Caratti <[email protected]>
nf_conntrack needs to compute crc32c when dealing with SCTP packets. Moreover, NF_NAT_PROTO_SCTP (currently selecting LIBCRC32C) can be enabled only if conntrack support for SCTP is enabled. Therefore, move enabling of kernel support for crc32c so that it is selected when NF_CT_PROTO_SCTP=y.
--- net/netfilter/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/Kconfig +1 -1
@@ -162,6 +162,7 @@ config NF_CT_PROTO_SCTP bool 'SCTP protocol connection tracking support' depends on NETFILTER_ADVANCED default y + select LIBCRC32C help With this option enabled, the layer 3 independent connection tracking code will be able to do state tracking on SCTP connections.
@@ -397,7 +398,6 @@ config NF_NAT_PROTO_SCTP bool default NF_NAT && NF_CT_PROTO_SCTP depends on NF_NAT && NF_CT_PROTO_SCTP - select LIBCRC32C config NF_NAT_AMANDA tristate
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 6/27 netfilter: conntrack: validate SCTP crc32c in PREROUTING
In reply to Pablo Neira AyusoFrom: Davide Caratti <[email protected]>
implement sctp_error to let nf_conntrack_in validate crc32c on the packet transport header. Assign skb->ip_summed to CHECKSUM_UNNECESSARY and return NF_ACCEPT in case of successful validation; otherwise, return -NF_ACCEPT to let netfilter skip connection tracking, like other protocols do.
Besides preventing corrupted packets from matching conntrack entries, this fixes functionality of REJECT target: it was not generating any ICMP upon reception of SCTP packets, because it was computing RFC 1624 checksum on the packet and systematically mismatching crc32c in the SCTP header.
--- net/netfilter/nf_conntrack_proto_sctp.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
net/netfilter/nf_conntrack_proto_sctp.c +32 -0
@@ -22,7 +22,9 @@#include <linux/seq_file.h> #include <linux/spinlock.h> #include <linux/interrupt.h> +#include <net/sctp/checksum.h> +#include <net/netfilter/nf_log.h> #include <net/netfilter/nf_conntrack.h> #include <net/netfilter/nf_conntrack_l4proto.h> #include <net/netfilter/nf_conntrack_ecache.h>
@@ -505,6 +507,34 @@ static bool sctp_new(struct nf_conn *ct, const struct sk_buff *skb,return true; } +static int sctp_error (struct net *net,struct nf_conn *tpl,struct sk_buff *skb, +unsigned int dataoff,enum ip_conntrack_info *ctinfo, +u8 pf,unsigned int hooknum) +{ +const struct sctphdr *sh; +struct sctphdr _sctph; +const char *logmsg; + + sh =skb_header_pointer (skb, dataoff,sizeof (_sctph), &_sctph); +if (!sh) { + logmsg ="nf_ct_sctp: short packet " ; + goto out_invalid; + } +if (net->ct.sysctl_checksum && hooknum ==NF_INET_PRE_ROUTING && + skb->ip_summed ==CHECKSUM_NONE ) { +if (sh->checksum !=sctp_compute_cksum (skb, dataoff)) { + logmsg ="nf_ct_sctp: bad CRC " ; + goto out_invalid; + } + skb->ip_summed =CHECKSUM_UNNECESSARY ; + } +return NF_ACCEPT ; +out_invalid: +if (LOG_INVALID (net,IPPROTO_SCTP )) +nf_log_packet (net, pf,0 , skb,NULL ,NULL ,NULL ,"%s" , logmsg); +return -NF_ACCEPT ; +} +#if IS_ENABLED (CONFIG_NF_CT_NETLINK )#include <linux/netfilter/nfnetlink.h>
@@ -752,6 +782,7 @@ struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp4 __read_mostly = { .packet = sctp_packet, .get_timeouts = sctp_get_timeouts, .new = sctp_new, + .error = sctp_error, .me =THIS_MODULE , #if IS_ENABLED(CONFIG_NF_CT_NETLINK) .to_nlattr = sctp_to_nlattr,
@@ -786,6 +817,7 @@ struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp6 __read_mostly = { .packet = sctp_packet, .get_timeouts = sctp_get_timeouts, .new = sctp_new, + .error = sctp_error, .me =THIS_MODULE , #if IS_ENABLED(CONFIG_NF_CT_NETLINK) .to_nlattr = sctp_to_nlattr,
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 7/27 netfilter: xt_connlimit: use rb_entry()
In reply to Pablo Neira AyusoFrom: Geliang Tang <[email protected]>
To make the code clearer, use rb_entry() instead of container_of() to deal with rbtree.
--- net/netfilter/xt_connlimit.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/xt_connlimit.c +2 -2
@@ -218,7 +218,7 @@ count_tree(struct net *net, struct rb_root *root,int diff;bool addit; - rbconn = container_of(*rbnode, struct xt_connlimit_rb, node); + rbconn = rb_entry(*rbnode, struct xt_connlimit_rb, node); parent = *rbnode; diff =same_source_net (addr, mask, &rbconn->addr, family);
@@ -398,7 +398,7 @@ static void destroy_tree(struct rb_root *r)struct rb_node *node;while ((node =rb_first (r)) !=NULL ) { - rbconn = container_of(node, struct xt_connlimit_rb, node); + rbconn = rb_entry(node, struct xt_connlimit_rb, node);rb_erase (node, r);
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 8/27 xtables: add xt_match, xt_target and data copy_to_user functions
In reply to Pablo Neira AyusoFrom: Willem de Bruijn <[email protected]>
xt_entry_target, xt_entry_match and their private data may contain kernel data.
Introduce helper functions xt_match_to_user, xt_target_to_user and xt_data_to_user that copy only the expected fields. These replace existing logic that calls copy_to_user on entire structs, then overwrites select fields.
Private data is defined in xt_match and xt_target. All matches and targets that maintain kernel data store this at the tail of their private structure. Extend xt_match and xt_target with .usersize to limit how many bytes of data are copied. The remainder is cleared.
If compatsize is specified, usersize can only safely be used if all fields up to usersize use platform-independent types. Otherwise, the compat_to_user callback must be defined.
This patch does not yet enable the support logic.
--- include/linux/netfilter/x_tables.h | 9 +++++++ net/netfilter/x_tables.c | 54 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+)
include/linux/netfilter/x_tables.h +9 -0
@@ -167,6 +167,7 @@ struct xt_match {const char *table;unsigned int matchsize; +unsigned int usersize;#ifdef CONFIG_COMPAT unsigned int compatsize;#endif
@@ -207,6 +208,7 @@ struct xt_target {const char *table;unsigned int targetsize; +unsigned int usersize;#ifdef CONFIG_COMPAT unsigned int compatsize;#endif
@@ -287,6 +289,13 @@ int xt_check_match(struct xt_mtchk_param *, unsigned int size, u_int8_t proto,int xt_check_target (struct xt_tgchk_param *,unsigned int size,u_int8_t proto,bool inv_proto); +int xt_match_to_user (const struct xt_entry_match *m, +struct xt_entry_match __user *u); +int xt_target_to_user (const struct xt_entry_target *t, +struct xt_entry_target __user *u); +int xt_data_to_user (void __user *dst,const void *src, +int usersize,int size); +void *xt_copy_counters_from_user (const void __user *user,unsigned int len,struct xt_counters_info *info,bool compat);
net/netfilter/x_tables.c +54 -0
60 lines not on this page · raw message
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 9/27 iptables: use match, target and data copy_to_user helpers
In reply to Pablo Neira AyusoFrom: Willem de Bruijn <[email protected]>
Convert iptables to copying entries, matches and targets one by one, using the xt_match_to_user and xt_target_to_user helper functions.
--- net/ipv4/netfilter/ip_tables.c | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-)
net/ipv4/netfilter/ip_tables.c +6 -15
@@ -826,10 +826,6 @@ copy_entries_to_user(unsigned int total_size,return PTR_ERR (counters); loc_cpu_entry = private->entries; -if (copy_to_user (userptr, loc_cpu_entry, total_size) !=0 ) { - ret = -EFAULT ; - goto free_counters; - }/* FIXME: use iterator macros --RR */ /* ... then go back and fix counters and names */
@@ -839,6 +835,10 @@ copy_entries_to_user(unsigned int total_size,const struct xt_entry_target *t; e = (struct ipt_entry *)(loc_cpu_entry + off); +if (copy_to_user (userptr + off, e,sizeof (*e))) { + ret = -EFAULT ; + goto free_counters; + } if (copy_to_user(userptr + off + offsetof(struct ipt_entry, counters), &counters[num],
@@ -852,23 +852,14 @@ copy_entries_to_user(unsigned int total_size, i += m->u.match_size) { m = (void *)e + i; -if (copy_to_user (userptr + off + i - + offsetof(struct xt_entry_match , - u.user.name), - m->u.kernel.match->name, -strlen (m->u.kernel.match->name)+1 ) - != 0) { +if (xt_match_to_user (m, userptr + off + i)) { ret = -EFAULT ; goto free_counters; } } t =ipt_get_target_c (e); -if (copy_to_user (userptr + off + e->target_offset - + offsetof(struct xt_entry_target , - u.user.name), - t->u.kernel.target->name, - strlen(t->u.kernel.target->name)+1) != 0) { +if (xt_target_to_user (t, userptr + off + e->target_offset)) { ret = -EFAULT ; goto free_counters; }
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 10/27 ip6tables: use match, target and data copy_to_user helpers
In reply to Pablo Neira AyusoFrom: Willem de Bruijn <[email protected]>
Convert ip6tables to copying entries, matches and targets one by one, using the xt_match_to_user and xt_target_to_user helper functions.
--- net/ipv6/netfilter/ip6_tables.c | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-)
net/ipv6/netfilter/ip6_tables.c +6 -15
@@ -855,10 +855,6 @@ copy_entries_to_user(unsigned int total_size,return PTR_ERR (counters); loc_cpu_entry = private->entries; -if (copy_to_user (userptr, loc_cpu_entry, total_size) !=0 ) { - ret = -EFAULT ; - goto free_counters; - }/* FIXME: use iterator macros --RR */ /* ... then go back and fix counters and names */
@@ -868,6 +864,10 @@ copy_entries_to_user(unsigned int total_size,const struct xt_entry_target *t; e = (struct ip6t_entry *)(loc_cpu_entry + off); +if (copy_to_user (userptr + off, e,sizeof (*e))) { + ret = -EFAULT ; + goto free_counters; + } if (copy_to_user(userptr + off + offsetof(struct ip6t_entry, counters), &counters[num],
@@ -881,23 +881,14 @@ copy_entries_to_user(unsigned int total_size, i += m->u.match_size) { m = (void *)e + i; -if (copy_to_user (userptr + off + i - + offsetof(struct xt_entry_match , - u.user.name), - m->u.kernel.match->name, -strlen (m->u.kernel.match->name)+1 ) - != 0) { +if (xt_match_to_user (m, userptr + off + i)) { ret = -EFAULT ; goto free_counters; } } t =ip6t_get_target_c (e); -if (copy_to_user (userptr + off + e->target_offset - + offsetof(struct xt_entry_target , - u.user.name), - t->u.kernel.target->name, - strlen(t->u.kernel.target->name)+1) != 0) { +if (xt_target_to_user (t, userptr + off + e->target_offset)) { ret = -EFAULT ; goto free_counters; }
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 11/27 arptables: use match, target and data copy_to_user helpers
In reply to Pablo Neira AyusoFrom: Willem de Bruijn <[email protected]>
Convert arptables to copying entries, matches and targets one by one, using the xt_match_to_user and xt_target_to_user helper functions.
--- net/ipv4/netfilter/arp_tables.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-)
net/ipv4/netfilter/arp_tables.c +5 -10
@@ -677,11 +677,6 @@ static int copy_entries_to_user(unsigned int total_size,return PTR_ERR (counters); loc_cpu_entry = private->entries; -/* ... then copy entire thing ... */ -if (copy_to_user (userptr, loc_cpu_entry, total_size) !=0 ) { - ret = -EFAULT ; - goto free_counters; - }/* FIXME: use iterator macros --RR */ /* ... then go back and fix counters and names */
@@ -689,6 +684,10 @@ static int copy_entries_to_user(unsigned int total_size,const struct xt_entry_target *t; e = (struct arpt_entry *)(loc_cpu_entry + off); +if (copy_to_user (userptr + off, e,sizeof (*e))) { + ret = -EFAULT ; + goto free_counters; + } if (copy_to_user(userptr + off + offsetof(struct arpt_entry, counters), &counters[num],
@@ -698,11 +697,7 @@ static int copy_entries_to_user(unsigned int total_size, } t =arpt_get_target_c (e); -if (copy_to_user (userptr + off + e->target_offset - + offsetof(struct xt_entry_target , - u.user.name), - t->u.kernel.target->name, - strlen(t->u.kernel.target->name)+1) != 0) { +if (xt_target_to_user (t, userptr + off + e->target_offset)) { ret = -EFAULT ; goto free_counters; }
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 12/27 ebtables: use match, target and data copy_to_user helpers
In reply to Pablo Neira AyusoFrom: Willem de Bruijn <[email protected]>
Convert ebtables to copying entries, matches and targets one by one.
The solution is analogous to that of generic xt_(match|target)_to_user helpers, but is applied to different structs.
Convert existing helpers ebt_make_XXXname helpers that overwrite fields of an already copy_to_user'd struct with ebt_XXX_to_user helpers that copy all relevant fields of the struct from scratch.
--- net/bridge/netfilter/ebtables.c | 78 +++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 31 deletions(-)
net/bridge/netfilter/ebtables.c +47 -31
@@ -1346,56 +1346,72 @@ static int update_counters(struct net *net, const void __user *user, hlp.num_counters, user, len); } -static inline int ebt_make_matchname (const struct ebt_entry_match *m, -const char *base,char __user *ubase) +static inline int ebt_obj_to_user (char __user *um,const char *_name, +const char *data,int entrysize, +int usersize,int datasize) { -char __user *hlp = ubase + ((char *)m - base); -char name[EBT_FUNCTION_MAXNAMELEN ] = {}; +char name[EBT_FUNCTION_MAXNAMELEN ] = {0 };/* ebtables expects 32 bytes long names but xt_match names are 29 bytes * long. Copy 29 bytes and fill remaining bytes with zeroes. */ -strlcpy (name, m->u.match->name,sizeof (name)); -if (copy_to_user (hlp, name,EBT_FUNCTION_MAXNAMELEN )) +strlcpy (name, _name,sizeof (name)); +if (copy_to_user (um, name,EBT_FUNCTION_MAXNAMELEN ) || +put_user (datasize, (int __user *)(um +EBT_FUNCTION_MAXNAMELEN )) || +xt_data_to_user (um + entrysize, data, usersize, datasize))return -EFAULT ; +return 0 ; } -static inline int ebt_make_watchername (const struct ebt_entry_watcher *w, -const char *base,char __user *ubase) +static inline int ebt_match_to_user (const struct ebt_entry_match *m, +const char *base,char __user *ubase) { -char __user *hlp = ubase + ((char *)w - base); -char name[EBT_FUNCTION_MAXNAMELEN ] = {}; +return ebt_obj_to_user (ubase + ((char *)m - base), + m->u.match->name, m->data,sizeof (*m), + m->u.match->usersize, m->match_size); +} -strlcpy (name, w->u.watcher->name,sizeof (name)); -if (copy_to_user (hlp, name,EBT_FUNCTION_MAXNAMELEN )) -return -EFAULT ; -return 0 ; +static inline int ebt_watcher_to_user (const struct ebt_entry_watcher *w, +const char *base,char __user *ubase) +{ +return ebt_obj_to_user (ubase + ((char *)w - base), + w->u.watcher->name, w->data,sizeof (*w), + w->u.watcher->usersize, w->watcher_size); } -static inline int ebt_make_names (struct ebt_entry *e,const char *base, -char __user *ubase) +static inline int ebt_entry_to_user (struct ebt_entry *e,const char *base, +char __user *ubase) {int ret;char __user *hlp;const struct ebt_entry_target *t; -char name[EBT_FUNCTION_MAXNAMELEN ] = {}; -if (e->bitmask ==0 ) +if (e->bitmask ==0 ) { +/* special case !EBT_ENTRY_OR_ENTRIES */ +if (copy_to_user (ubase + ((char *)e - base), e, +sizeof (struct ebt_entries ))) +return -EFAULT ;return 0 ; + } + +if (copy_to_user (ubase + ((char *)e - base), e,sizeof (*e))) +return -EFAULT ; hlp = ubase + (((char *)e + e->target_offset) - base); t = (struct ebt_entry_target *)(((char *)e) + e->target_offset); - ret =EBT_MATCH_ITERATE (e, ebt_make_matchname, base, ubase); + ret =EBT_MATCH_ITERATE (e, ebt_match_to_user, base, ubase);if (ret !=0 )return ret; - ret =EBT_WATCHER_ITERATE (e, ebt_make_watchername, base, ubase); + ret =EBT_WATCHER_ITERATE (e, ebt_watcher_to_user, base, ubase);if (ret !=0 )return ret; -strlcpy (name, t->u.target->name,sizeof (name)); -if (copy_to_user (hlp, name,EBT_FUNCTION_MAXNAMELEN )) -return -EFAULT ; + ret =ebt_obj_to_user (hlp, t->u.target->name, t->data,sizeof (*t), + t->u.target->usersize, t->target_size); +if (ret !=0 ) +return ret; +return 0 ; }
@@ -1475,13 +1491,9 @@ static int copy_everything_to_user(struct ebt_table *t, void __user *user,if (ret)return ret; -if (copy_to_user (tmp.entries, entries, entries_size)) { -BUGPRINT ("Couldn't copy entries to userspace\n" ); -return -EFAULT ; - }/* set the match/watcher/target names right */ return EBT_ENTRY_ITERATE (entries, entries_size, - ebt_make_names, entries, tmp.entries); + ebt_entry_to_user, entries, tmp.entries); } static int do_ebt_set_ctl(struct sock *sk,
@@ -1630,8 +1642,10 @@ static int compat_match_to_user(struct ebt_entry_match *m, void __user **dstptr,if (match->compat_to_user) {if (match->compat_to_user (cm->data, m->data))return -EFAULT ; - }else if (copy_to_user (cm->data, m->data, msize)) + }else { +if (xt_data_to_user (cm->data, m->data, match->usersize, msize))return -EFAULT ; + } *size -=ebt_compat_entry_padsize () + off; *dstptr = cm->data;
@@ -1657,8 +1671,10 @@ static int compat_target_to_user(struct ebt_entry_target *t,if (target->compat_to_user) {if (target->compat_to_user (cm->data, t->data))return -EFAULT ; - }else if (copy_to_user (cm->data, t->data, tsize)) -return -EFAULT ; + }else { +if (xt_data_to_user (cm->data, t->data, target->usersize, tsize)) +return -EFAULT ; + } *size -=ebt_compat_entry_padsize () + off; *dstptr = cm->data;
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 13/27 xtables: use match, target and data copy_to_user helpers in compat
In reply to Pablo Neira AyusoFrom: Willem de Bruijn <[email protected]>
Convert compat to copying entries, matches and targets one by one, using the xt_match_to_user and xt_target_to_user helper functions.
--- net/netfilter/x_tables.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-)
net/netfilter/x_tables.c +4 -10
@@ -619,17 +619,14 @@ int xt_compat_match_to_user(const struct xt_entry_match *m,int off =xt_compat_match_offset (match);u_int16_t msize = m->u.user.match_size - off; -if (copy_to_user (cm, m,sizeof (*cm)) || -put_user (msize, &cm->u.user.match_size) || -copy_to_user (cm->u.user.name, m->u.kernel.match->name, -strlen (m->u.kernel.match->name) +1 )) +if (XT_OBJ_TO_USER (cm, m, match, msize))return -EFAULT ;if (match->compat_to_user) {if (match->compat_to_user ((void __user *)cm->data, m->data))return -EFAULT ; }else { -if (copy_to_user (cm->data, m->data, msize -sizeof (*cm))) +if (XT_DATA_TO_USER (cm, m, match, msize -sizeof (*cm)))return -EFAULT ; }
@@ -977,17 +974,14 @@ int xt_compat_target_to_user(const struct xt_entry_target *t,int off =xt_compat_target_offset (target);u_int16_t tsize = t->u.user.target_size - off; -if (copy_to_user (ct, t,sizeof (*ct)) || -put_user (tsize, &ct->u.user.target_size) || -copy_to_user (ct->u.user.name, t->u.kernel.target->name, -strlen (t->u.kernel.target->name) +1 )) +if (XT_OBJ_TO_USER (ct, t, target, tsize))return -EFAULT ;if (target->compat_to_user) {if (target->compat_to_user ((void __user *)ct->data, t->data))return -EFAULT ; }else { -if (copy_to_user (ct->data, t->data, tsize -sizeof (*ct))) +if (XT_DATA_TO_USER (ct, t, target, tsize -sizeof (*ct)))return -EFAULT ; }
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 14/27 xtables: extend matches and targets with .usersize
In reply to Pablo Neira AyusoFrom: Willem de Bruijn <[email protected]>
In matches and targets that define a kernel-only tail to their xt_match and xt_target data structs, add a field .usersize that specifies up to where data is to be shared with userspace.
Performed a search for comment "Used internally by the kernel" to find relevant matches and targets. Manually inspected the structs to derive a valid offsetof.
--- net/bridge/netfilter/ebt_limit.c | 1 + net/ipv4/netfilter/ipt_CLUSTERIP.c | 1 + net/ipv6/netfilter/ip6t_NPT.c | 2 ++ net/netfilter/xt_CT.c | 3 +++ net/netfilter/xt_RATEEST.c | 1 + net/netfilter/xt_TEE.c | 2 ++ net/netfilter/xt_bpf.c | 2 ++ net/netfilter/xt_cgroup.c | 1 + net/netfilter/xt_connlimit.c | 1 + net/netfilter/xt_hashlimit.c | 4 ++++ net/netfilter/xt_limit.c | 2 ++ net/netfilter/xt_quota.c | 1 + net/netfilter/xt_rateest.c | 1 + net/netfilter/xt_string.c | 1 + 14 files changed, 23 insertions(+)
net/bridge/netfilter/ebt_limit.c +1 -0
@@ -105,6 +105,7 @@ static struct xt_match ebt_limit_mt_reg __read_mostly = { .match = ebt_limit_mt, .checkentry = ebt_limit_mt_check, .matchsize = sizeof(struct ebt_limit_info), + .usersize = offsetof(struct ebt_limit_info, prev),#ifdef CONFIG_COMPAT .compatsize = sizeof(struct ebt_compat_limit_info),#endif
net/ipv4/netfilter/ipt_CLUSTERIP.c +1 -0
@@ -468,6 +468,7 @@ static struct xt_target clusterip_tg_reg __read_mostly = { .checkentry = clusterip_tg_check, .destroy = clusterip_tg_destroy, .targetsize = sizeof(struct ipt_clusterip_tgt_info), + .usersize = offsetof(struct ipt_clusterip_tgt_info, config),#ifdef CONFIG_COMPAT .compatsize = sizeof(struct compat_ipt_clusterip_tgt_info),#endif /* CONFIG_COMPAT */
net/ipv6/netfilter/ip6t_NPT.c +2 -0
@@ -112,6 +112,7 @@ static struct xt_target ip6t_npt_target_reg[] __read_mostly = { .table = "mangle", .target = ip6t_snpt_tg, .targetsize = sizeof(struct ip6t_npt_tginfo), + .usersize = offsetof(struct ip6t_npt_tginfo, adjustment), .checkentry = ip6t_npt_checkentry, .family = NFPROTO_IPV6, .hooks = (1 << NF_INET_LOCAL_IN) |
@@ -123,6 +124,7 @@ static struct xt_target ip6t_npt_target_reg[] __read_mostly = { .table = "mangle", .target = ip6t_dnpt_tg, .targetsize = sizeof(struct ip6t_npt_tginfo), + .usersize = offsetof(struct ip6t_npt_tginfo, adjustment), .checkentry = ip6t_npt_checkentry, .family = NFPROTO_IPV6, .hooks = (1 << NF_INET_PRE_ROUTING) |
net/netfilter/xt_CT.c +3 -0
@@ -373,6 +373,7 @@ static struct xt_target xt_ct_tg_reg[] __read_mostly = { .name = "CT", .family = NFPROTO_UNSPEC, .targetsize = sizeof(struct xt_ct_target_info), + .usersize = offsetof(struct xt_ct_target_info, ct), .checkentry = xt_ct_tg_check_v0, .destroy = xt_ct_tg_destroy_v0, .target = xt_ct_target_v0,
@@ -384,6 +385,7 @@ static struct xt_target xt_ct_tg_reg[] __read_mostly = { .family = NFPROTO_UNSPEC, .revision = 1, .targetsize = sizeof(struct xt_ct_target_info_v1), + .usersize = offsetof(struct xt_ct_target_info, ct), .checkentry = xt_ct_tg_check_v1, .destroy = xt_ct_tg_destroy_v1, .target = xt_ct_target_v1,
@@ -395,6 +397,7 @@ static struct xt_target xt_ct_tg_reg[] __read_mostly = { .family = NFPROTO_UNSPEC, .revision = 2, .targetsize = sizeof(struct xt_ct_target_info_v1), + .usersize = offsetof(struct xt_ct_target_info, ct), .checkentry = xt_ct_tg_check_v2, .destroy = xt_ct_tg_destroy_v1, .target = xt_ct_target_v1,
net/netfilter/xt_RATEEST.c +1 -0
@@ -162,6 +162,7 @@ static struct xt_target xt_rateest_tg_reg __read_mostly = { .checkentry = xt_rateest_tg_checkentry, .destroy = xt_rateest_tg_destroy, .targetsize = sizeof(struct xt_rateest_target_info), + .usersize = offsetof(struct xt_rateest_target_info, est), .me = THIS_MODULE, };
net/netfilter/xt_TEE.c +2 -0
@@ -133,6 +133,7 @@ static struct xt_target tee_tg_reg[] __read_mostly = { .family = NFPROTO_IPV4, .target = tee_tg4, .targetsize = sizeof(struct xt_tee_tginfo), + .usersize = offsetof(struct xt_tee_tginfo, priv), .checkentry = tee_tg_check, .destroy = tee_tg_destroy, .me = THIS_MODULE,
@@ -144,6 +145,7 @@ static struct xt_target tee_tg_reg[] __read_mostly = { .family = NFPROTO_IPV6, .target = tee_tg6, .targetsize = sizeof(struct xt_tee_tginfo), + .usersize = offsetof(struct xt_tee_tginfo, priv), .checkentry = tee_tg_check, .destroy = tee_tg_destroy, .me = THIS_MODULE,
net/netfilter/xt_bpf.c +2 -0
@@ -110,6 +110,7 @@ static struct xt_match bpf_mt_reg[] __read_mostly = { .match = bpf_mt, .destroy = bpf_mt_destroy, .matchsize = sizeof(struct xt_bpf_info), + .usersize = offsetof(struct xt_bpf_info, filter), .me = THIS_MODULE, }, {
@@ -120,6 +121,7 @@ static struct xt_match bpf_mt_reg[] __read_mostly = { .match = bpf_mt_v1, .destroy = bpf_mt_destroy_v1, .matchsize = sizeof(struct xt_bpf_info_v1), + .usersize = offsetof(struct xt_bpf_info_v1, filter), .me = THIS_MODULE, }, };
net/netfilter/xt_cgroup.c +1 -0
@@ -122,6 +122,7 @@ static struct xt_match cgroup_mt_reg[] __read_mostly = { .checkentry = cgroup_mt_check_v1, .match = cgroup_mt_v1, .matchsize = sizeof(struct xt_cgroup_info_v1), + .usersize = offsetof(struct xt_cgroup_info_v1, priv), .destroy = cgroup_mt_destroy_v1, .me = THIS_MODULE, .hooks = (1 << NF_INET_LOCAL_OUT) |
net/netfilter/xt_connlimit.c +1 -0
@@ -431,6 +431,7 @@ static struct xt_match connlimit_mt_reg __read_mostly = { .checkentry = connlimit_mt_check, .match = connlimit_mt, .matchsize = sizeof(struct xt_connlimit_info), + .usersize = offsetof(struct xt_connlimit_info, data), .destroy = connlimit_mt_destroy, .me = THIS_MODULE, };
net/netfilter/xt_hashlimit.c +4 -0
@@ -838,6 +838,7 @@ static struct xt_match hashlimit_mt_reg[] __read_mostly = { .family = NFPROTO_IPV4, .match = hashlimit_mt_v1, .matchsize = sizeof(struct xt_hashlimit_mtinfo1), + .usersize = offsetof(struct xt_hashlimit_mtinfo1, hinfo), .checkentry = hashlimit_mt_check_v1, .destroy = hashlimit_mt_destroy_v1, .me = THIS_MODULE,
@@ -848,6 +849,7 @@ static struct xt_match hashlimit_mt_reg[] __read_mostly = { .family = NFPROTO_IPV4, .match = hashlimit_mt, .matchsize = sizeof(struct xt_hashlimit_mtinfo2), + .usersize = offsetof(struct xt_hashlimit_mtinfo2, hinfo), .checkentry = hashlimit_mt_check, .destroy = hashlimit_mt_destroy, .me = THIS_MODULE,
@@ -859,6 +861,7 @@ static struct xt_match hashlimit_mt_reg[] __read_mostly = { .family = NFPROTO_IPV6, .match = hashlimit_mt_v1, .matchsize = sizeof(struct xt_hashlimit_mtinfo1), + .usersize = offsetof(struct xt_hashlimit_mtinfo1, hinfo), .checkentry = hashlimit_mt_check_v1, .destroy = hashlimit_mt_destroy_v1, .me = THIS_MODULE,
@@ -869,6 +872,7 @@ static struct xt_match hashlimit_mt_reg[] __read_mostly = { .family = NFPROTO_IPV6, .match = hashlimit_mt, .matchsize = sizeof(struct xt_hashlimit_mtinfo2), + .usersize = offsetof(struct xt_hashlimit_mtinfo2, hinfo), .checkentry = hashlimit_mt_check, .destroy = hashlimit_mt_destroy, .me = THIS_MODULE,
net/netfilter/xt_limit.c +2 -0
@@ -192,6 +192,8 @@ static struct xt_match limit_mt_reg __read_mostly = { .compatsize = sizeof(struct compat_xt_rateinfo), .compat_from_user = limit_mt_compat_from_user, .compat_to_user = limit_mt_compat_to_user, +#else + .usersize = offsetof(struct xt_rateinfo, prev), #endif .me = THIS_MODULE, };
net/netfilter/xt_quota.c +1 -0
@@ -73,6 +73,7 @@ static struct xt_match quota_mt_reg __read_mostly = { .checkentry = quota_mt_check, .destroy = quota_mt_destroy, .matchsize = sizeof(struct xt_quota_info), + .usersize = offsetof(struct xt_quota_info, master), .me = THIS_MODULE, };
net/netfilter/xt_rateest.c +1 -0
@@ -133,6 +133,7 @@ static struct xt_match xt_rateest_mt_reg __read_mostly = { .checkentry = xt_rateest_mt_checkentry, .destroy = xt_rateest_mt_destroy, .matchsize = sizeof(struct xt_rateest_match_info), + .usersize = offsetof(struct xt_rateest_match_info, est1), .me = THIS_MODULE, };
net/netfilter/xt_string.c +1 -0
@@ -77,6 +77,7 @@ static struct xt_match xt_string_mt_reg __read_mostly = { .match = string_mt, .destroy = string_mt_destroy, .matchsize = sizeof(struct xt_string_info), + .usersize = offsetof(struct xt_string_info, config), .me = THIS_MODULE, };
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 15/27 netfilter: pkttype: unnecessary to check ipv6 multicast address
In reply to Pablo Neira AyusoFrom: Liping Zhang <[email protected]>
Since there's no broadcast address in IPV6, so in ipv6 family, the PACKET_LOOPBACK must be multicast packets, there's no need to check it again.
--- net/netfilter/nft_meta.c | 5 +---- net/netfilter/xt_pkttype.c | 3 +-- 2 files changed, 2 insertions(+), 6 deletions(-)
net/netfilter/nft_meta.c +1 -4
@@ -154,10 +154,7 @@ void nft_meta_get_eval(const struct nft_expr *expr, *dest =PACKET_BROADCAST ;break ;case NFPROTO_IPV6 : -if (ipv6_hdr (skb)->daddr.s6_addr[0 ] ==0xFF ) - *dest =PACKET_MULTICAST ; -else - *dest =PACKET_BROADCAST ; + *dest =PACKET_MULTICAST ;break ;default :WARN_ON (1 );
net/netfilter/xt_pkttype.c +1 -2
@@ -33,8 +33,7 @@ pkttype_mt(const struct sk_buff *skb, struct xt_action_param *par)else if (xt_family (par) ==NFPROTO_IPV4 &&ipv4_is_multicast (ip_hdr (skb)->daddr)) type =PACKET_MULTICAST ; -else if (xt_family (par) ==NFPROTO_IPV6 && -ipv6_hdr (skb)->daddr.s6_addr[0 ] ==0xFF ) +else if (xt_family (par) ==NFPROTO_IPV6 ) type =PACKET_MULTICAST ;else type =PACKET_BROADCAST ;
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 16/27 netfilter: nft_meta: deal with PACKET_LOOPBACK in netdev family
In reply to Pablo Neira AyusoFrom: Liping Zhang <[email protected]>
After adding the following nft rule, then ping 224.0.0.1:
# nft add rule netdev t c pkttype host counter
The warning complain message will be printed out again and again:
WARNING: CPU: 0 PID: 10182 at net/netfilter/nft_meta.c:163 \
nft_meta_get_eval+0x3fe/0x460 [nft_meta]
[...]
Call Trace:
<IRQ>
dump_stack+0x85/0xc2
__warn+0xcb/0xf0
warn_slowpath_null+0x1d/0x20
nft_meta_get_eval+0x3fe/0x460 [nft_meta]
nft_do_chain+0xff/0x5e0 [nf_tables]So we should deal with PACKET_LOOPBACK in netdev family too. For ipv4, convert it to PACKET_BROADCAST/MULTICAST according to the destination address's type; For ipv6, convert it to PACKET_MULTICAST directly.
--- net/netfilter/nft_meta.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-)
net/netfilter/nft_meta.c +27 -1
@@ -156,8 +156,34 @@ void nft_meta_get_eval(const struct nft_expr *expr,case NFPROTO_IPV6 : *dest =PACKET_MULTICAST ;break ; +case NFPROTO_NETDEV : +switch (skb->protocol) { +case htons (ETH_P_IP ): { +int noff =skb_network_offset (skb); +struct iphdr *iph, _iph; + + iph =skb_header_pointer (skb, noff, +sizeof (_iph), &_iph); +if (!iph) + goto err; + +if (ipv4_is_multicast (iph->daddr)) + *dest =PACKET_MULTICAST ; +else + *dest =PACKET_BROADCAST ; + +break ; + } +case htons (ETH_P_IPV6 ): + *dest =PACKET_MULTICAST ; +break ; +default : +WARN_ON_ONCE (1 ); + goto err; + } +break ;default : -WARN_ON (1 ); +WARN_ON_ONCE (1 ); goto err; }break ;
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 17/27 netfilter: nf_tables: eliminate useless condition checks
In reply to Pablo Neira AyusoFrom: Gao Feng <[email protected]>
The return value of nf_tables_table_lookup() is valid pointer or one pointer error. There are two cases:
1) IS_ERR(table) is true, it would return the error or reset the table as NULL, it is unnecessary to perform the latter check "table != NULL".
2) IS_ERR(obj) is false, the table is one valid pointer. It is also unnecessary to perform that check.
The nf_tables_newset() and nf_tables_newobj() have same logic codes.
In summary, we could move the block of condition check "table != NULL" in the else block to eliminate the original condition checks.
--- net/netfilter/nf_tables_api.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-)
net/netfilter/nf_tables_api.c +3 -12
@@ -696,10 +696,7 @@ static int nf_tables_newtable(struct net *net, struct sock *nlsk,if (IS_ERR (table)) {if (PTR_ERR (table) != -ENOENT )return PTR_ERR (table); - table =NULL ; - } - -if (table !=NULL ) { + }else {if (nlh->nlmsg_flags &NLM_F_EXCL )return -EEXIST ;if (nlh->nlmsg_flags &NLM_F_REPLACE )
@@ -2963,10 +2960,7 @@ static int nf_tables_newset(struct net *net, struct sock *nlsk,if (IS_ERR (set)) {if (PTR_ERR (set) != -ENOENT )return PTR_ERR (set); - set =NULL ; - } - -if (set !=NULL ) { + }else {if (nlh->nlmsg_flags &NLM_F_EXCL )return -EEXIST ;if (nlh->nlmsg_flags &NLM_F_REPLACE )
@@ -4153,10 +4147,7 @@ static int nf_tables_newobj(struct net *net, struct sock *nlsk,if (err != -ENOENT )return err; - obj =NULL ; - } - -if (obj !=NULL ) { + }else {if (nlh->nlmsg_flags &NLM_F_EXCL )return -EEXIST ;
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 18/27 netfilter: nf_tables: Eliminate duplicated code in nf_tables_table_enable()
In reply to Pablo Neira AyusoFrom: Feng <[email protected]>
If something fails in nf_tables_table_enable(), it unregisters the chains. But the rollback code is the same as nf_tables_table_disable() almostly, except there is one counter check. Now create one wrapper function to eliminate the duplicated codes.
--- net/netfilter/nf_tables_api.c | 48 ++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 23 deletions(-)
net/netfilter/nf_tables_api.c +25 -23
@@ -576,6 +576,28 @@ static int nf_tables_gettable(struct net *net, struct sock *nlsk,return err; } +static void _nf_tables_table_disable (struct net *net, +const struct nft_af_info *afi, +struct nft_table *table, +u32 cnt) +{ +struct nft_chain *chain; +u32 i =0 ; + +list_for_each_entry (chain, &table->chains, list) { +if (!nft_is_active_next (net, chain)) +continue ; +if (!(chain->flags &NFT_BASE_CHAIN )) +continue ; + +if (cnt && i++ == cnt) +break ; + +nf_unregister_net_hooks (net,nft_base_chain (chain)->ops, + afi->nops); + } +} +static int nf_tables_table_enable (struct net *net,const struct nft_af_info *afi,struct nft_table *table)
@@ -598,18 +620,8 @@ static int nf_tables_table_enable(struct net *net, }return 0 ; err: -list_for_each_entry (chain, &table->chains, list) { -if (!nft_is_active_next (net, chain)) -continue ; -if (!(chain->flags &NFT_BASE_CHAIN )) -continue ; - -if (i-- <=0 ) -break ; - -nf_unregister_net_hooks (net,nft_base_chain (chain)->ops, - afi->nops); - } +if (i) +_nf_tables_table_disable (net, afi, table, i);return err; }
@@ -617,17 +629,7 @@ static void nf_tables_table_disable(struct net *net,const struct nft_af_info *afi, struct nft_table *table) { - struct nft_chain *chain; - -list_for_each_entry (chain, &table->chains, list) { -if (!nft_is_active_next (net, chain)) -continue ; -if (!(chain->flags &NFT_BASE_CHAIN )) -continue ; - -nf_unregister_net_hooks (net,nft_base_chain (chain)->ops, - afi->nops); - } + _nf_tables_table_disable(net, afi, table, 0); }static int nf_tables_updtable (struct nft_ctx *ctx)
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 19/27 netfilter: conntrack: no need to pass ctinfo to error handler
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
It is never accessed for reading and the only places that write to it are the icmp(6) handlers, which also set skb->nfct (and skb->nfctinfo).
The conntrack core specifically checks for attached skb->nfct after ->error() invocation and returns early in this case.
--- include/net/netfilter/nf_conntrack_l4proto.h | 2 +- net/ipv4/netfilter/nf_conntrack_proto_icmp.c | 12 ++++++------ net/ipv6/netfilter/nf_conntrack_proto_icmpv6.c | 12 ++++++------ net/netfilter/nf_conntrack_core.c | 3 +-- net/netfilter/nf_conntrack_proto_dccp.c | 1 - net/netfilter/nf_conntrack_proto_sctp.c | 2 +- net/netfilter/nf_conntrack_proto_tcp.c | 1 - net/netfilter/nf_conntrack_proto_udp.c | 3 +-- 8 files changed, 16 insertions(+), 20 deletions(-)
include/net/netfilter/nf_conntrack_l4proto.h +1 -1
@@ -55,7 +55,7 @@ struct nf_conntrack_l4proto {void (*destroy)(struct nf_conn *ct);int (*error)(struct net *net,struct nf_conn *tmpl,struct sk_buff *skb, -unsigned int dataoff,enum ip_conntrack_info *ctinfo, +unsigned int dataoff,u_int8_t pf,unsigned int hooknum);/* Print out the per-protocol part of the tuple. Return like seq_* */
net/ipv4/netfilter/nf_conntrack_proto_icmp.c +6 -6
@@ -128,13 +128,13 @@ static bool icmp_new(struct nf_conn *ct, const struct sk_buff *skb,/* Returns conntrack if it dealt with ICMP, and filled in skb fields */ static int icmp_error_message (struct net *net,struct nf_conn *tmpl,struct sk_buff *skb, -enum ip_conntrack_info *ctinfo,unsigned int hooknum) {struct nf_conntrack_tuple innertuple, origtuple;const struct nf_conntrack_l4proto *innerproto;const struct nf_conntrack_tuple_hash *h;const struct nf_conntrack_zone *zone; +enum ip_conntrack_info ctinfo;struct nf_conntrack_zone tmp;NF_CT_ASSERT (skb->nfct ==NULL );
@@ -160,7 +160,7 @@ icmp_error_message(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,return -NF_ACCEPT ; } - *ctinfo =IP_CT_RELATED ; + ctinfo =IP_CT_RELATED ; h =nf_conntrack_find_get (net, zone, &innertuple);if (!h) {
@@ -169,11 +169,11 @@ icmp_error_message(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb, }if (NF_CT_DIRECTION (h) ==IP_CT_DIR_REPLY ) - *ctinfo +=IP_CT_IS_REPLY ; + ctinfo +=IP_CT_IS_REPLY ;/* Update skb to refer to this connection */ skb->nfct = &nf_ct_tuplehash_to_ctrack (h)->ct_general; - skb->nfctinfo = *ctinfo; + skb->nfctinfo = ctinfo;return NF_ACCEPT ; }
@@ -181,7 +181,7 @@ icmp_error_message(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,static int icmp_error (struct net *net,struct nf_conn *tmpl,struct sk_buff *skb,unsigned int dataoff, -enum ip_conntrack_info *ctinfo,u_int8_t pf,unsigned int hooknum) +u8 pf,unsigned int hooknum) {const struct icmphdr *icmph;struct icmphdr _ih;
@@ -225,7 +225,7 @@ icmp_error(struct net *net, struct nf_conn *tmpl, icmph->type != ICMP_REDIRECT)return NF_ACCEPT ; -return icmp_error_message (net, tmpl, skb, ctinfo, hooknum); +return icmp_error_message (net, tmpl, skb, hooknum); }#if IS_ENABLED (CONFIG_NF_CT_NETLINK )
net/ipv6/netfilter/nf_conntrack_proto_icmpv6.c +6 -6
@@ -145,12 +145,12 @@ static int icmpv6_error_message(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb, unsigned int icmp6off, - enum ip_conntrack_info *ctinfo, unsigned int hooknum) { struct nf_conntrack_tuple intuple, origtuple;const struct nf_conntrack_tuple_hash *h;const struct nf_conntrack_l4proto *inproto; +enum ip_conntrack_info ctinfo;struct nf_conntrack_zone tmp;NF_CT_ASSERT (skb->nfct ==NULL );
@@ -176,7 +176,7 @@ icmpv6_error_message(struct net *net, struct nf_conn *tmpl,return -NF_ACCEPT ; } - *ctinfo =IP_CT_RELATED ; + ctinfo =IP_CT_RELATED ; h =nf_conntrack_find_get (net,nf_ct_zone_tmpl (tmpl, skb, &tmp), &intuple);
@@ -185,19 +185,19 @@ icmpv6_error_message(struct net *net, struct nf_conn *tmpl,return -NF_ACCEPT ; }else {if (NF_CT_DIRECTION (h) ==IP_CT_DIR_REPLY ) - *ctinfo +=IP_CT_IS_REPLY ; + ctinfo +=IP_CT_IS_REPLY ; }/* Update skb to refer to this connection */ skb->nfct = &nf_ct_tuplehash_to_ctrack (h)->ct_general; - skb->nfctinfo = *ctinfo; + skb->nfctinfo = ctinfo;return NF_ACCEPT ; }static int icmpv6_error (struct net *net,struct nf_conn *tmpl,struct sk_buff *skb,unsigned int dataoff, -enum ip_conntrack_info *ctinfo,u_int8_t pf,unsigned int hooknum) +u8 pf,unsigned int hooknum) {const struct icmp6hdr *icmp6h;struct icmp6hdr _ih;
@@ -232,7 +232,7 @@ icmpv6_error(struct net *net, struct nf_conn *tmpl,if (icmp6h->icmp6_type >=128 )return NF_ACCEPT ; -return icmpv6_error_message (net, tmpl, skb, dataoff, ctinfo, hooknum); +return icmpv6_error_message (net, tmpl, skb, dataoff, hooknum); }#if IS_ENABLED (CONFIG_NF_CT_NETLINK )
net/netfilter/nf_conntrack_core.c +1 -2
@@ -1326,8 +1326,7 @@ nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum,* inverse of the return code tells to the netfilter * core what to do with the packet. */ if (l4proto->error !=NULL ) { - ret = l4proto->error (net, tmpl, skb, dataoff, &ctinfo, - pf, hooknum); + ret = l4proto->error (net, tmpl, skb, dataoff, pf, hooknum);if (ret <=0 ) {NF_CT_STAT_INC_ATOMIC (net, error);NF_CT_STAT_INC_ATOMIC (net, invalid);
net/netfilter/nf_conntrack_proto_dccp.c +0 -1
@@ -561,7 +561,6 @@ static int dccp_packet(struct nf_conn *ct, const struct sk_buff *skb,static int dccp_error (struct net *net,struct nf_conn *tmpl,struct sk_buff *skb,unsigned int dataoff, -enum ip_conntrack_info *ctinfo,u_int8_t pf,unsigned int hooknum) {struct dccp_hdr _dh, *dh;
net/netfilter/nf_conntrack_proto_sctp.c +1 -1
@@ -508,7 +508,7 @@ static bool sctp_new(struct nf_conn *ct, const struct sk_buff *skb, }static int sctp_error (struct net *net,struct nf_conn *tpl,struct sk_buff *skb, -unsigned int dataoff,enum ip_conntrack_info *ctinfo, +unsigned int dataoff,u8 pf,unsigned int hooknum) {const struct sctphdr *sh;
net/netfilter/nf_conntrack_proto_tcp.c +0 -1
@@ -750,7 +750,6 @@ static const u8 tcp_valid_flags[(TCPHDR_FIN|TCPHDR_SYN|TCPHDR_RST|TCPHDR_ACK|static int tcp_error (struct net *net,struct nf_conn *tmpl,struct sk_buff *skb,unsigned int dataoff, -enum ip_conntrack_info *ctinfo,u_int8_t pf,unsigned int hooknum) {
net/netfilter/nf_conntrack_proto_udp.c +1 -2
@@ -112,7 +112,6 @@ static bool udp_new(struct nf_conn *ct, const struct sk_buff *skb,static int udplite_error (struct net *net,struct nf_conn *tmpl,struct sk_buff *skb,unsigned int dataoff, -enum ip_conntrack_info *ctinfo,u8 pf,unsigned int hooknum) {unsigned int udplen = skb->len - dataoff;
@@ -162,7 +161,7 @@ static int udplite_error(struct net *net, struct nf_conn *tmpl,#endif static int udp_error (struct net *net,struct nf_conn *tmpl,struct sk_buff *skb, -unsigned int dataoff,enum ip_conntrack_info *ctinfo, +unsigned int dataoff,u_int8_t pf,unsigned int hooknum) {
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 20/27 netfilter: reset netfilter state when duplicating packet
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
We should also toss nf_bridge_info, if any -- packet is leaving via ip_local_out, also, this skb isn't bridged -- it is a locally generated copy. Also this avoids the need to touch this later when skb->nfct is replaced with 'unsigned long _nfct' in followup patch.
--- net/ipv4/netfilter/nf_dup_ipv4.c | 2 +- net/ipv6/netfilter/nf_dup_ipv6.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
net/ipv4/netfilter/nf_dup_ipv4.c +1 -1
@@ -68,7 +68,7 @@ void nf_dup_ipv4(struct net *net, struct sk_buff *skb, unsigned int hooknum,#if IS_ENABLED (CONFIG_NF_CONNTRACK )/* Avoid counting cloned packets towards the original connection. */ -nf_conntrack_put (skb->nfct); +nf_reset (skb); skb->nfct = &nf_ct_untracked_get ()->ct_general; skb->nfctinfo =IP_CT_NEW ;nf_conntrack_get (skb->nfct);
net/ipv6/netfilter/nf_dup_ipv6.c +1 -1
@@ -57,7 +57,7 @@ void nf_dup_ipv6(struct net *net, struct sk_buff *skb, unsigned int hooknum,return ;#if IS_ENABLED (CONFIG_NF_CONNTRACK ) -nf_conntrack_put (skb->nfct); +nf_reset (skb); skb->nfct = &nf_ct_untracked_get ()->ct_general; skb->nfctinfo =IP_CT_NEW ;nf_conntrack_get (skb->nfct);
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 21/27 netfilter: reduce direct skb->nfct usage
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Next patch makes direct skb->nfct access illegal, reduce noise in next patch by using accessors we already have.
--- include/net/ip_vs.h | 9 ++++++--- net/netfilter/nf_conntrack_core.c | 15 +++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-)
include/net/ip_vs.h +6 -3
@@ -1554,10 +1554,13 @@ static inline void ip_vs_notrack(struct sk_buff *skb)struct nf_conn *ct =nf_ct_get (skb, &ctinfo);if (!ct || !nf_ct_is_untracked (ct)) { -nf_conntrack_put (skb->nfct); - skb->nfct = &nf_ct_untracked_get ()->ct_general; +struct nf_conn *untracked; + +nf_conntrack_put (&ct->ct_general); + untracked =nf_ct_untracked_get (); +nf_conntrack_get (&untracked->ct_general); + skb->nfct = &untracked->ct_general; skb->nfctinfo =IP_CT_NEW ; -nf_conntrack_get (skb->nfct); }#endif }
net/netfilter/nf_conntrack_core.c +9 -6
@@ -686,8 +686,11 @@ static int nf_ct_resolve_clash(struct net *net, struct sk_buff *skb, !nfct_nat(ct) && !nf_ct_is_dying(ct) && atomic_inc_not_zero(&ct->ct_general.use)) { -nf_ct_acct_merge (ct, ctinfo, (struct nf_conn *)skb->nfct); -nf_conntrack_put (skb->nfct); +enum ip_conntrack_info oldinfo; +struct nf_conn *loser_ct =nf_ct_get (skb, &oldinfo); + +nf_ct_acct_merge (ct, ctinfo, loser_ct); +nf_conntrack_put (&loser_ct->ct_general);/* Assign conntrack already in hashes to this skbuff. Don't * modify skb->nfctinfo to ensure consistent stateful filtering. */
@@ -1288,7 +1291,7 @@ unsigned int nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum, struct sk_buff *skb) { - struct nf_conn *ct, *tmpl = NULL; + struct nf_conn *ct, *tmpl;enum ip_conntrack_info ctinfo;struct nf_conntrack_l3proto *l3proto;struct nf_conntrack_l4proto *l4proto;
@@ -1298,9 +1301,9 @@ nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum,int set_reply =0 ;int ret; -if (skb->nfct) { + tmpl =nf_ct_get (skb, &ctinfo); +if (tmpl) {/* Previously seen (loopback or untracked)? Ignore. */ - tmpl = (struct nf_conn *)skb->nfct;if (!nf_ct_is_template (tmpl)) {NF_CT_STAT_INC_ATOMIC (net, ignore);return NF_ACCEPT ;
@@ -1364,7 +1367,7 @@ nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum,/* Invalid: inverse of the return code tells * the netfilter core what to do */ pr_debug ("nf_conntrack_in: Can't track with proto module\n" ); -nf_conntrack_put (skb->nfct); +nf_conntrack_put (&ct->ct_general); skb->nfct =NULL ;NF_CT_STAT_INC_ATOMIC (net, invalid);if (ret == -NF_DROP )
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 22/27 skbuff: add and use skb_nfct helper
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Followup patch renames skb->nfct and changes its type so add a helper to avoid intrusive rename change later.
--- include/linux/skbuff.h | 13 ++++++++++--- include/net/netfilter/nf_conntrack_core.h | 2 +- net/core/skbuff.c | 2 +- net/ipv4/netfilter/ipt_SYNPROXY.c | 8 ++++---- net/ipv4/netfilter/nf_conntrack_proto_icmp.c | 2 +- net/ipv4/netfilter/nf_defrag_ipv4.c | 4 ++-- net/ipv4/netfilter/nf_dup_ipv4.c | 2 +- net/ipv6/netfilter/ip6t_SYNPROXY.c | 8 ++++---- net/ipv6/netfilter/nf_conntrack_proto_icmpv6.c | 4 ++-- net/ipv6/netfilter/nf_defrag_ipv6_hooks.c | 4 ++-- net/netfilter/nf_conntrack_core.c | 4 ++-- net/netfilter/nf_nat_helper.c | 2 +- net/netfilter/xt_CT.c | 2 +- net/openvswitch/conntrack.c | 6 +++--- net/sched/cls_flow.c | 2 +- 15 files changed, 36 insertions(+), 29 deletions(-)
include/linux/skbuff.h +10 -3
@@ -3553,6 +3553,15 @@ static inline void skb_remcsum_process(struct sk_buff *skb, void *ptr, skb->csum =csum_add (skb->csum, delta); } +static inline struct nf_conntrack *skb_nfct (const struct sk_buff *skb) +{ +#if IS_ENABLED (CONFIG_NF_CONNTRACK ) +return skb->nfct; +#else +return NULL ; +#endif +} +#if defined(CONFIG_NF_CONNTRACK ) || defined(CONFIG_NF_CONNTRACK_MODULE )void nf_conntrack_destroy (struct nf_conntrack *nfct);static inline void nf_conntrack_put (struct nf_conntrack *nfct)
@@ -3652,9 +3661,7 @@ static inline bool skb_irq_freeable(const struct sk_buff *skb)#if IS_ENABLED (CONFIG_XFRM ) !skb->sp && #endif -#if IS_ENABLED (CONFIG_NF_CONNTRACK ) - !skb->nfct && -#endif + !skb_nfct (skb) && !skb->_skb_refdst && !skb_has_frag_list(skb); }
include/net/netfilter/nf_conntrack_core.h +1 -1
@@ -62,7 +62,7 @@ int __nf_conntrack_confirm(struct sk_buff *skb);/* Confirm a connection: returns NF_DROP if packet must be dropped. */ static inline int nf_conntrack_confirm (struct sk_buff *skb) { -struct nf_conn *ct = (struct nf_conn *)skb->nfct; +struct nf_conn *ct = (struct nf_conn *)skb_nfct (skb);int ret =NF_ACCEPT ;if (ct && !nf_ct_is_untracked (ct)) {
net/core/skbuff.c +1 -1
@@ -655,7 +655,7 @@ static void skb_release_head_state(struct sk_buff *skb) skb->destructor (skb); }#if IS_ENABLED (CONFIG_NF_CONNTRACK ) -nf_conntrack_put (skb->nfct); +nf_conntrack_put (skb_nfct (skb));#endif #if IS_ENABLED (CONFIG_BRIDGE_NETFILTER )nf_bridge_put (skb->nf_bridge);
net/ipv4/netfilter/ipt_SYNPROXY.c +4 -4
@@ -107,8 +107,8 @@ synproxy_send_client_synack(struct net *net,synproxy_build_options (nth, opts); -synproxy_send_tcp (net, skb, nskb, skb->nfct,IP_CT_ESTABLISHED_REPLY , - niph, nth, tcp_hdr_size); +synproxy_send_tcp (net, skb, nskb,skb_nfct (skb), +IP_CT_ESTABLISHED_REPLY , niph, nth, tcp_hdr_size); } static void
@@ -230,8 +230,8 @@ synproxy_send_client_ack(struct net *net,synproxy_build_options (nth, opts); -synproxy_send_tcp (net, skb, nskb, skb->nfct,IP_CT_ESTABLISHED_REPLY , - niph, nth, tcp_hdr_size); +synproxy_send_tcp (net, skb, nskb,skb_nfct (skb), +IP_CT_ESTABLISHED_REPLY , niph, nth, tcp_hdr_size); } static bool
net/ipv4/netfilter/nf_conntrack_proto_icmp.c +1 -1
@@ -137,7 +137,7 @@ icmp_error_message(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,enum ip_conntrack_info ctinfo;struct nf_conntrack_zone tmp; -NF_CT_ASSERT (skb->nfct ==NULL ); +NF_CT_ASSERT (!skb_nfct (skb)); zone =nf_ct_zone_tmpl (tmpl, skb, &tmp);/* Are they talking about one of our connections? */
net/ipv4/netfilter/nf_defrag_ipv4.c +2 -2
@@ -45,7 +45,7 @@ static enum ip_defrag_users nf_ct_defrag_user(unsigned int hooknum, {u16 zone_id =NF_CT_DEFAULT_ZONE_ID ;#if IS_ENABLED (CONFIG_NF_CONNTRACK ) -if (skb->nfct) { +if (skb_nfct (skb)) {enum ip_conntrack_info ctinfo;const struct nf_conn *ct =nf_ct_get (skb, &ctinfo);
@@ -75,7 +75,7 @@ static unsigned int ipv4_conntrack_defrag(void *priv,#if !IS_ENABLED (CONFIG_NF_NAT )/* Previously seen (loopback)? Ignore. Do this before fragment check. */ -if (skb->nfct && !nf_ct_is_template ((struct nf_conn *)skb->nfct)) +if (skb_nfct (skb) && !nf_ct_is_template ((struct nf_conn *)skb_nfct (skb)))return NF_ACCEPT ;#endif #endif
net/ipv4/netfilter/nf_dup_ipv4.c +1 -1
@@ -71,7 +71,7 @@ void nf_dup_ipv4(struct net *net, struct sk_buff *skb, unsigned int hooknum,nf_reset (skb); skb->nfct = &nf_ct_untracked_get ()->ct_general; skb->nfctinfo =IP_CT_NEW ; -nf_conntrack_get (skb->nfct); +nf_conntrack_get (skb_nfct (skb));#endif /* * If we are in PREROUTING/INPUT, decrease the TTL to mitigate potential
net/ipv6/netfilter/ip6t_SYNPROXY.c +4 -4
@@ -121,8 +121,8 @@ synproxy_send_client_synack(struct net *net,synproxy_build_options (nth, opts); -synproxy_send_tcp (net, skb, nskb, skb->nfct,IP_CT_ESTABLISHED_REPLY , - niph, nth, tcp_hdr_size); +synproxy_send_tcp (net, skb, nskb,skb_nfct (skb), +IP_CT_ESTABLISHED_REPLY , niph, nth, tcp_hdr_size); } static void
@@ -244,8 +244,8 @@ synproxy_send_client_ack(struct net *net,synproxy_build_options (nth, opts); -synproxy_send_tcp (net, skb, nskb, skb->nfct,IP_CT_ESTABLISHED_REPLY , - niph, nth, tcp_hdr_size); +synproxy_send_tcp (net, skb, nskb,skb_nfct (skb), +IP_CT_ESTABLISHED_REPLY , niph, nth, tcp_hdr_size); } static bool
net/ipv6/netfilter/nf_conntrack_proto_icmpv6.c +2 -2
@@ -153,7 +153,7 @@ icmpv6_error_message(struct net *net, struct nf_conn *tmpl,enum ip_conntrack_info ctinfo;struct nf_conntrack_zone tmp; -NF_CT_ASSERT (skb->nfct ==NULL ); +NF_CT_ASSERT (!skb_nfct (skb));/* Are they talking about one of our connections? */ if (!nf_ct_get_tuplepr(skb,
@@ -224,7 +224,7 @@ icmpv6_error(struct net *net, struct nf_conn *tmpl, noct_valid_new[type]) { skb->nfct = &nf_ct_untracked_get ()->ct_general; skb->nfctinfo =IP_CT_NEW ; -nf_conntrack_get (skb->nfct); +nf_conntrack_get (skb_nfct (skb));return NF_ACCEPT ; }
net/ipv6/netfilter/nf_defrag_ipv6_hooks.c +2 -2
@@ -37,7 +37,7 @@ static enum ip6_defrag_users nf_ct6_defrag_user(unsigned int hooknum, {u16 zone_id =NF_CT_DEFAULT_ZONE_ID ;#if IS_ENABLED (CONFIG_NF_CONNTRACK ) -if (skb->nfct) { +if (skb_nfct (skb)) {enum ip_conntrack_info ctinfo;const struct nf_conn *ct =nf_ct_get (skb, &ctinfo);
@@ -61,7 +61,7 @@ static unsigned int ipv6_defrag(void *priv,#if IS_ENABLED (CONFIG_NF_CONNTRACK )/* Previously seen (loopback)? */ -if (skb->nfct && !nf_ct_is_template ((struct nf_conn *)skb->nfct)) +if (skb_nfct (skb) && !nf_ct_is_template ((struct nf_conn *)skb_nfct (skb)))return NF_ACCEPT ;#endif
net/netfilter/nf_conntrack_core.c +2 -2
@@ -1357,7 +1357,7 @@ nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum, goto out; } -NF_CT_ASSERT (skb->nfct); +NF_CT_ASSERT (skb_nfct (skb));/* Decide what timeout policy we want to apply to this flow. */ timeouts =nf_ct_timeout_lookup (net, ct, l4proto);
@@ -1528,7 +1528,7 @@ static void nf_conntrack_attach(struct sk_buff *nskb, const struct sk_buff *skb)/* Attach to new skbuff, and increment count */ nskb->nfct = &ct->ct_general; nskb->nfctinfo = ctinfo; -nf_conntrack_get (nskb->nfct); +nf_conntrack_get (skb_nfct (nskb)); }/* Bring out ya dead! */
net/netfilter/nf_nat_helper.c +1 -1
@@ -60,7 +60,7 @@ static void mangle_contents(struct sk_buff *skb,__skb_trim (skb, skb->len + rep_len - match_len); } -if (nf_ct_l3num ((struct nf_conn *)skb->nfct) ==NFPROTO_IPV4 ) { +if (nf_ct_l3num ((struct nf_conn *)skb_nfct (skb)) ==NFPROTO_IPV4 ) {/* fix IP hdr checksum information */ ip_hdr (skb)->tot_len =htons (skb->len);ip_send_check (ip_hdr (skb));
net/netfilter/xt_CT.c +1 -1
@@ -415,7 +415,7 @@ notrack_tg(struct sk_buff *skb, const struct xt_action_param *par) skb->nfct = &nf_ct_untracked_get ()->ct_general; skb->nfctinfo =IP_CT_NEW ; -nf_conntrack_get (skb->nfct); +nf_conntrack_get (skb_nfct (skb));return XT_CONTINUE ; }
net/openvswitch/conntrack.c +3 -3
@@ -721,8 +721,8 @@ static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,/* Associate skb with specified zone. */ if (tmpl) { -if (skb->nfct) -nf_conntrack_put (skb->nfct); +if (skb_nfct (skb)) +nf_conntrack_put (skb_nfct (skb));nf_conntrack_get (&tmpl->ct_general); skb->nfct = &tmpl->ct_general; skb->nfctinfo =IP_CT_NEW ;
@@ -819,7 +819,7 @@ static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,if (err)return err; - ct = (struct nf_conn *)skb->nfct; + ct = (struct nf_conn *)skb_nfct (skb);if (ct)nf_ct_deliver_cached_events (ct); }
net/sched/cls_flow.c +1 -1
@@ -129,7 +129,7 @@ static u32 flow_get_mark(const struct sk_buff *skb)static u32 flow_get_nfct (const struct sk_buff *skb) {#if IS_ENABLED (CONFIG_NF_CONNTRACK ) -return addr_fold (skb->nfct); +return addr_fold (skb_nfct (skb));#else return 0 ;#endif
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 23/27 netfilter: add and use nf_ct_set helper
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Add a helper to assign a nf_conn entry and the ctinfo bits to an sk_buff. This avoids changing code in followup patch that merges skb->nfct and skb->nfctinfo into skb->_nfct.
--- include/net/ip_vs.h | 3 +-- include/net/netfilter/nf_conntrack.h | 8 ++++++++ net/ipv4/netfilter/ipt_SYNPROXY.c | 3 +-- net/ipv4/netfilter/nf_conntrack_proto_icmp.c | 3 +-- net/ipv4/netfilter/nf_dup_ipv4.c | 3 +-- net/ipv6/netfilter/ip6t_SYNPROXY.c | 3 +-- net/ipv6/netfilter/nf_conntrack_proto_icmpv6.c | 6 ++---- net/ipv6/netfilter/nf_dup_ipv6.c | 3 +-- net/netfilter/nf_conntrack_core.c | 11 +++-------- net/netfilter/nft_ct.c | 3 +-- net/netfilter/xt_CT.c | 6 ++---- net/openvswitch/conntrack.c | 6 ++---- 12 files changed, 24 insertions(+), 34 deletions(-)
include/net/ip_vs.h +1 -2
@@ -1559,8 +1559,7 @@ static inline void ip_vs_notrack(struct sk_buff *skb)nf_conntrack_put (&ct->ct_general); untracked =nf_ct_untracked_get ();nf_conntrack_get (&untracked->ct_general); - skb->nfct = &untracked->ct_general; - skb->nfctinfo = IP_CT_NEW; + nf_ct_set(skb, untracked, IP_CT_NEW); }#endif }
include/net/netfilter/nf_conntrack.h +8 -0
@@ -34,6 +34,7 @@ union nf_conntrack_proto {struct ip_ct_sctp sctp;struct ip_ct_tcp tcp;struct nf_ct_gre gre; +unsigned int tmpl_padto; };union nf_conntrack_expect_proto {
@@ -341,6 +342,13 @@ struct nf_conn *nf_ct_tmpl_alloc(struct net *net, gfp_t flags);void nf_ct_tmpl_free (struct nf_conn *tmpl); +static inline void +nf_ct_set (struct sk_buff *skb,struct nf_conn *ct,enum ip_conntrack_info info) +{ + skb->nfct = &ct->ct_general; + skb->nfctinfo = info; +} +#define NF_CT_STAT_INC (net, count) __this_cpu_inc((net)->ct.stat->count)#define NF_CT_STAT_INC_ATOMIC (net, count) this_cpu_inc((net)->ct.stat->count)#define NF_CT_STAT_ADD_ATOMIC (net, count, v) this_cpu_add((net)->ct.stat->count, (v))
net/ipv4/netfilter/ipt_SYNPROXY.c +1 -2
@@ -57,8 +57,7 @@ synproxy_send_tcp(struct net *net, goto free_nskb;if (nfct) { - nskb->nfct = nfct; - nskb->nfctinfo = ctinfo; +nf_ct_set (nskb, (struct nf_conn *)nfct, ctinfo);nf_conntrack_get (nfct); }
net/ipv4/netfilter/nf_conntrack_proto_icmp.c +1 -2
@@ -172,8 +172,7 @@ icmp_error_message(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb, ctinfo +=IP_CT_IS_REPLY ;/* Update skb to refer to this connection */ - skb->nfct = &nf_ct_tuplehash_to_ctrack (h)->ct_general; - skb->nfctinfo = ctinfo; +nf_ct_set (skb,nf_ct_tuplehash_to_ctrack (h), ctinfo);return NF_ACCEPT ; }
net/ipv4/netfilter/nf_dup_ipv4.c +1 -2
@@ -69,8 +69,7 @@ void nf_dup_ipv4(struct net *net, struct sk_buff *skb, unsigned int hooknum,#if IS_ENABLED (CONFIG_NF_CONNTRACK )/* Avoid counting cloned packets towards the original connection. */ nf_reset (skb); - skb->nfct = &nf_ct_untracked_get ()->ct_general; - skb->nfctinfo =IP_CT_NEW ; +nf_ct_set (skb,nf_ct_untracked_get (),IP_CT_NEW );nf_conntrack_get (skb_nfct (skb));#endif /*
net/ipv6/netfilter/ip6t_SYNPROXY.c +1 -2
@@ -71,8 +71,7 @@ synproxy_send_tcp(struct net *net,skb_dst_set (nskb, dst);if (nfct) { - nskb->nfct = nfct; - nskb->nfctinfo = ctinfo; +nf_ct_set (nskb, (struct nf_conn *)nfct, ctinfo);nf_conntrack_get (nfct); }
net/ipv6/netfilter/nf_conntrack_proto_icmpv6.c +2 -4
@@ -189,8 +189,7 @@ icmpv6_error_message(struct net *net, struct nf_conn *tmpl, }/* Update skb to refer to this connection */ - skb->nfct = &nf_ct_tuplehash_to_ctrack (h)->ct_general; - skb->nfctinfo = ctinfo; +nf_ct_set (skb,nf_ct_tuplehash_to_ctrack (h), ctinfo);return NF_ACCEPT ; }
@@ -222,8 +221,7 @@ icmpv6_error(struct net *net, struct nf_conn *tmpl, type = icmp6h->icmp6_type -130 ;if (type >=0 && type <sizeof (noct_valid_new) && noct_valid_new[type]) { - skb->nfct = &nf_ct_untracked_get ()->ct_general; - skb->nfctinfo =IP_CT_NEW ; +nf_ct_set (skb,nf_ct_untracked_get (),IP_CT_NEW );nf_conntrack_get (skb_nfct (skb));return NF_ACCEPT ; }
net/ipv6/netfilter/nf_dup_ipv6.c +1 -2
@@ -58,8 +58,7 @@ void nf_dup_ipv6(struct net *net, struct sk_buff *skb, unsigned int hooknum,#if IS_ENABLED (CONFIG_NF_CONNTRACK )nf_reset (skb); - skb->nfct = &nf_ct_untracked_get ()->ct_general; - skb->nfctinfo =IP_CT_NEW ; +nf_ct_set (skb,nf_ct_untracked_get (),IP_CT_NEW );nf_conntrack_get (skb->nfct);#endif if (hooknum == NF_INET_PRE_ROUTING ||
net/netfilter/nf_conntrack_core.c +3 -8
@@ -691,10 +691,7 @@ static int nf_ct_resolve_clash(struct net *net, struct sk_buff *skb,nf_ct_acct_merge (ct, ctinfo, loser_ct);nf_conntrack_put (&loser_ct->ct_general); -/* Assign conntrack already in hashes to this skbuff. Don't -* modify skb->nfctinfo to ensure consistent stateful filtering. -*/ - skb->nfct = &ct->ct_general; +nf_ct_set (skb, ct, oldinfo);return NF_ACCEPT ; }NF_CT_STAT_INC (net, drop);
@@ -1282,8 +1279,7 @@ resolve_normal_ct(struct net *net, struct nf_conn *tmpl, } *set_reply =0 ; } - skb->nfct = &ct->ct_general; - skb->nfctinfo = *ctinfo; +nf_ct_set (skb, ct, *ctinfo);return ct; }
@@ -1526,8 +1522,7 @@ static void nf_conntrack_attach(struct sk_buff *nskb, const struct sk_buff *skb) ctinfo =IP_CT_RELATED ;/* Attach to new skbuff, and increment count */ - nskb->nfct = &ct->ct_general; - nskb->nfctinfo = ctinfo; +nf_ct_set (nskb, ct, ctinfo);nf_conntrack_get (skb_nfct (nskb)); }
net/netfilter/nft_ct.c +1 -2
@@ -554,8 +554,7 @@ static void nft_notrack_eval(const struct nft_expr *expr, ct =nf_ct_untracked_get ();atomic_inc (&ct->ct_general.use); - skb->nfct = &ct->ct_general; - skb->nfctinfo =IP_CT_NEW ; +nf_ct_set (skb, ct,IP_CT_NEW ); }static struct nft_expr_type nft_notrack_type;
net/netfilter/xt_CT.c +2 -4
@@ -30,8 +30,7 @@ static inline int xt_ct_target(struct sk_buff *skb, struct nf_conn *ct)if (!ct) ct =nf_ct_untracked_get ();atomic_inc (&ct->ct_general.use); - skb->nfct = &ct->ct_general; - skb->nfctinfo =IP_CT_NEW ; +nf_ct_set (skb, ct,IP_CT_NEW );return XT_CONTINUE ; }
@@ -413,8 +412,7 @@ notrack_tg(struct sk_buff *skb, const struct xt_action_param *par)if (skb->nfct !=NULL )return XT_CONTINUE ; - skb->nfct = &nf_ct_untracked_get ()->ct_general; - skb->nfctinfo =IP_CT_NEW ; +nf_ct_set (skb,nf_ct_untracked_get (),IP_CT_NEW );nf_conntrack_get (skb_nfct (skb));return XT_CONTINUE ;
net/openvswitch/conntrack.c +2 -4
@@ -460,8 +460,7 @@ ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone, ct =nf_ct_tuplehash_to_ctrack (h); - skb->nfct = &ct->ct_general; - skb->nfctinfo =ovs_ct_get_info (h); +nf_ct_set (skb, ct,ovs_ct_get_info (h));return ct; }
@@ -724,8 +723,7 @@ static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,if (skb_nfct (skb))nf_conntrack_put (skb_nfct (skb));nf_conntrack_get (&tmpl->ct_general); - skb->nfct = &tmpl->ct_general; - skb->nfctinfo =IP_CT_NEW ; +nf_ct_set (skb, tmpl,IP_CT_NEW ); } err = nf_conntrack_in(net, info->family,
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 24/27 netfilter: guarantee 8 byte minalign for template addresses
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
The next change will merge skb->nfct pointer and skb->nfctinfo status bits into single skb->_nfct (unsigned long) area.
For this to work nf_conn addresses must always be aligned at least on an 8 byte boundary since we will need the lower 3bits to store nfctinfo.
Conntrack templates are allocated via kmalloc.
kbuild test robot reportedBUILD_BUG_ON failed: NFCT_INFOMASK >= ARCH_KMALLOC_MINALIGN on v1 of this patchset, so not all platforms meet this requirement.
Do manual alignment if needed, the alignment offset is stored in the nf_conn entry protocol area. This works because templates are not handed off to L4 protocol trackers.
--- include/net/netfilter/nf_conntrack.h | 2 ++ net/netfilter/nf_conntrack_core.c | 29 ++++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-)
include/net/netfilter/nf_conntrack.h +2 -0
@@ -163,6 +163,8 @@ void nf_conntrack_alter_reply(struct nf_conn *ct,int nf_conntrack_tuple_taken (const struct nf_conntrack_tuple *tuple,const struct nf_conn *ignored_conntrack); +#define NFCT_INFOMASK 7UL +/* Return conntrack_info and tuple hash for given skb. */ static inline struct nf_conn *nf_ct_get (const struct sk_buff *skb,enum ip_conntrack_info *ctinfo)
net/netfilter/nf_conntrack_core.c +24 -5
@@ -350,16 +350,31 @@ static void nf_ct_del_from_dying_or_unconfirmed_list(struct nf_conn *ct)spin_unlock (&pcpu->lock); } +#define NFCT_ALIGN (len) (((len) + NFCT_INFOMASK) & ~NFCT_INFOMASK) +/* Released via destroy_conntrack() */ struct nf_conn *nf_ct_tmpl_alloc (struct net *net,const struct nf_conntrack_zone *zone,gfp_t flags) { -struct nf_conn *tmpl; +struct nf_conn *tmpl, *p; - tmpl =kzalloc (sizeof (*tmpl), flags); -if (tmpl ==NULL ) -return NULL ; +if (ARCH_KMALLOC_MINALIGN <=NFCT_INFOMASK ) { + tmpl =kzalloc (sizeof (*tmpl) +NFCT_INFOMASK , flags); +if (!tmpl) +return NULL ; + + p = tmpl; + tmpl = (struct nf_conn *)NFCT_ALIGN ((unsigned long )p); +if (tmpl != p) { + tmpl = (struct nf_conn *)NFCT_ALIGN ((unsigned long )p); + tmpl->proto.tmpl_padto = (char *)tmpl - (char *)p; + } + }else { + tmpl =kzalloc (sizeof (*tmpl), flags); +if (!tmpl) +return NULL ; + } tmpl->status =IPS_TEMPLATE ;write_pnet (&tmpl->ct_net, net);
@@ -374,7 +389,11 @@ void nf_ct_tmpl_free(struct nf_conn *tmpl) {nf_ct_ext_destroy (tmpl);nf_ct_ext_free (tmpl); -kfree (tmpl); + +if (ARCH_KMALLOC_MINALIGN <=NFCT_INFOMASK ) +kfree ((char *)tmpl - tmpl->proto.tmpl_padto); +else +kfree (tmpl); }EXPORT_SYMBOL_GPL (nf_ct_tmpl_free);
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 25/27 netfilter: merge ctinfo into nfct pointer storage area
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
After this change conntrack operations (lookup, creation, matching from ruleset) only access one instead of two sk_buff cache lines.
This works for normal conntracks because those are allocated from a slab that guarantees hw cacheline or 8byte alignment (whatever is larger) so the 3 bits needed for ctinfo won't overlap with nf_conn addresses.
Template allocation now does manual address alignment (see previous change) on arches that don't have sufficent kmalloc min alignment.
Some spots intentionally use skb->_nfct instead of skb_nfct() helpers, this is to avoid undoing the skb_nfct() use when we remove untracked conntrack object in the future.
--- include/linux/skbuff.h | 21 +++++++++------------ include/net/netfilter/nf_conntrack.h | 11 ++++++----- net/ipv6/netfilter/nf_dup_ipv6.c | 2 +- net/netfilter/core.c | 2 +- net/netfilter/nf_conntrack_core.c | 11 ++++++----- net/netfilter/nf_conntrack_standalone.c | 3 +++ net/netfilter/xt_CT.c | 4 ++-- 7 files changed, 28 insertions(+), 26 deletions(-)
include/linux/skbuff.h +9 -12
@@ -585,7 +585,6 @@ static inline bool skb_mstamp_after(const struct skb_mstamp *t1,* @cloned: Head may be cloned (check refcnt to be sure) * @ip_summed: Driver fed us an IP checksum * @nohdr: Payload reference only, must not modify header -* @nfctinfo: Relationship of this skb to the connection * @pkt_type: Packet class * @fclone: skbuff clone status * @ipvs_property: skbuff is owned by ipvs
@@ -594,7 +593,7 @@ static inline bool skb_mstamp_after(const struct skb_mstamp *t1,* @nf_trace: netfilter packet trace flag * @protocol: Packet protocol from driver * @destructor: Destruct function -* @nfct: Associated connection, if any +* @_nfct: Associated connection, if any (with nfctinfo bits) * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c * @skb_iif: ifindex of device we arrived on * @tc_index: Traffic control index
@@ -668,7 +667,7 @@ struct sk_buff {struct sec_path *sp;#endif #if defined(CONFIG_NF_CONNTRACK ) || defined(CONFIG_NF_CONNTRACK_MODULE ) -struct nf_conntrack *nfct; +unsigned long _nfct;#endif #if IS_ENABLED (CONFIG_BRIDGE_NETFILTER )struct nf_bridge_info *nf_bridge;
@@ -721,7 +720,6 @@ struct sk_buff { __u8 pkt_type:3; __u8 pfmemalloc:1; __u8 ignore_df:1; - __u8 nfctinfo:3; __u8 nf_trace:1; __u8 ip_summed:2;
@@ -836,6 +834,7 @@ static inline bool skb_pfmemalloc(const struct sk_buff *skb)#define SKB_DST_NOREF 1UL#define SKB_DST_PTRMASK ~(SKB_DST_NOREF) +#define SKB_NFCT_PTRMASK ~(7UL)/** * skb_dst - returns skb dst_entry * @skb: buffer
@@ -3556,7 +3555,7 @@ static inline void skb_remcsum_process(struct sk_buff *skb, void *ptr,static inline struct nf_conntrack *skb_nfct (const struct sk_buff *skb) {#if IS_ENABLED (CONFIG_NF_CONNTRACK ) -return skb->nfct; +return (void *)(skb->_nfct &SKB_NFCT_PTRMASK );#else return NULL ;#endif
@@ -3590,8 +3589,8 @@ static inline void nf_bridge_get(struct nf_bridge_info *nf_bridge)static inline void nf_reset (struct sk_buff *skb) {#if defined(CONFIG_NF_CONNTRACK ) || defined(CONFIG_NF_CONNTRACK_MODULE ) -nf_conntrack_put (skb->nfct); - skb->nfct =NULL ; +nf_conntrack_put (skb_nfct (skb)); + skb->_nfct =0 ;#endif #if IS_ENABLED (CONFIG_BRIDGE_NETFILTER )nf_bridge_put (skb->nf_bridge);
@@ -3611,10 +3610,8 @@ static inline void __nf_copy(struct sk_buff *dst, const struct sk_buff *src, bool copy) {#if defined(CONFIG_NF_CONNTRACK ) || defined(CONFIG_NF_CONNTRACK_MODULE ) - dst->nfct = src->nfct; -nf_conntrack_get (src->nfct); -if (copy) - dst->nfctinfo = src->nfctinfo; + dst->_nfct = src->_nfct; +nf_conntrack_get (skb_nfct (src));#endif #if IS_ENABLED (CONFIG_BRIDGE_NETFILTER ) dst->nf_bridge = src->nf_bridge;
@@ -3629,7 +3626,7 @@ static inline void __nf_copy(struct sk_buff *dst, const struct sk_buff *src,static inline void nf_copy (struct sk_buff *dst,const struct sk_buff *src) {#if defined(CONFIG_NF_CONNTRACK ) || defined(CONFIG_NF_CONNTRACK_MODULE ) -nf_conntrack_put (dst->nfct); +nf_conntrack_put (skb_nfct (dst));#endif #if IS_ENABLED (CONFIG_BRIDGE_NETFILTER )nf_bridge_put (dst->nf_bridge);
include/net/netfilter/nf_conntrack.h +6 -5
@@ -76,7 +76,7 @@ struct nf_conn {/* Usage count in here is 1 for hash table, 1 per skb, * plus 1 for any connection(s) we are `master' for * -* Hint, SKB address this struct and refcnt via skb->nfct and +* Hint, SKB address this struct and refcnt via skb->_nfct and * helpers nf_conntrack_get() and nf_conntrack_put(). * Helper nf_ct_put() equals nf_conntrack_put() by dec refcnt, * beware nf_ct_get() is different and don't inc refcnt.
@@ -164,13 +164,15 @@ int nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple, const struct nf_conn *ignored_conntrack);#define NFCT_INFOMASK 7UL +#define NFCT_PTRMASK ~(NFCT_INFOMASK)/* Return conntrack_info and tuple hash for given skb. */ static inline struct nf_conn *nf_ct_get (const struct sk_buff *skb,enum ip_conntrack_info *ctinfo) { - *ctinfo = skb->nfctinfo; -return (struct nf_conn *)skb->nfct; + *ctinfo = skb->_nfct &NFCT_INFOMASK ; + +return (struct nf_conn *)(skb->_nfct &NFCT_PTRMASK ); }/* decrement reference count on a conntrack */
@@ -347,8 +349,7 @@ void nf_ct_tmpl_free(struct nf_conn *tmpl);static inline void nf_ct_set (struct sk_buff *skb,struct nf_conn *ct,enum ip_conntrack_info info) { - skb->nfct = &ct->ct_general; - skb->nfctinfo = info; + skb->_nfct = (unsigned long )ct | info; }#define NF_CT_STAT_INC (net, count) __this_cpu_inc((net)->ct.stat->count)
net/ipv6/netfilter/nf_dup_ipv6.c +1 -1
@@ -59,7 +59,7 @@ void nf_dup_ipv6(struct net *net, struct sk_buff *skb, unsigned int hooknum,#if IS_ENABLED (CONFIG_NF_CONNTRACK )nf_reset (skb);nf_ct_set (skb,nf_ct_untracked_get (),IP_CT_NEW ); -nf_conntrack_get (skb->nfct); +nf_conntrack_get (skb_nfct (skb));#endif if (hooknum ==NF_INET_PRE_ROUTING || hooknum ==NF_INET_LOCAL_IN ) {
net/netfilter/core.c +1 -1
@@ -375,7 +375,7 @@ void nf_ct_attach(struct sk_buff *new, const struct sk_buff *skb) {void (*attach)(struct sk_buff *,const struct sk_buff *); -if (skb->nfct) { +if (skb->_nfct) {rcu_read_lock (); attach =rcu_dereference (ip_ct_attach);if (attach)
net/netfilter/nf_conntrack_core.c +6 -5
@@ -1239,7 +1239,7 @@ init_conntrack(struct net *net, struct nf_conn *tmpl,return &ct->tuplehash[IP_CT_DIR_ORIGINAL ]; } -/* On success, returns conntrack ptr, sets skb->nfct and ctinfo */ +/* On success, returns conntrack ptr, sets skb->_nfct | ctinfo */ static inline struct nf_conn * resolve_normal_ct(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,
@@ -1323,7 +1323,7 @@ nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum,NF_CT_STAT_INC_ATOMIC (net, ignore);return NF_ACCEPT ; } - skb->nfct =NULL ; + skb->_nfct =0 ; }/* rcu_read_lock()ed by nf_hook_thresh */
@@ -1352,7 +1352,7 @@ nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum, goto out; }/* ICMP[v6] protocol trackers may assign one conntrack. */ -if (skb->nfct) +if (skb->_nfct) goto out; } repeat:
@@ -1383,7 +1383,7 @@ nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum,* the netfilter core what to do */ pr_debug ("nf_conntrack_in: Can't track with proto module\n" );nf_conntrack_put (&ct->ct_general); - skb->nfct =NULL ; + skb->_nfct =0 ;NF_CT_STAT_INC_ATOMIC (net, invalid);if (ret == -NF_DROP )NF_CT_STAT_INC_ATOMIC (net, drop);
@@ -1878,7 +1878,8 @@ int nf_conntrack_init_start(void) nf_conntrack_max = max_factor * nf_conntrack_htable_size; nf_conntrack_cachep =kmem_cache_create ("nf_conntrack" , -sizeof (struct nf_conn ),0 , +sizeof (struct nf_conn ), +NFCT_INFOMASK +1 ,SLAB_DESTROY_BY_RCU |SLAB_HWCACHE_ALIGN ,NULL );if (!nf_conntrack_cachep) goto err_cachep;
net/netfilter/nf_conntrack_standalone.c +3 -0
@@ -642,6 +642,9 @@ static int __init nf_conntrack_standalone_init(void)if (ret <0 ) goto out_start; +BUILD_BUG_ON (SKB_NFCT_PTRMASK !=NFCT_PTRMASK ); +BUILD_BUG_ON (NFCT_INFOMASK <=IP_CT_NUMBER ); +#ifdef CONFIG_SYSCTL nf_ct_netfilter_header =register_net_sysctl (&init_net,"net" , nf_ct_netfilter_table);
net/netfilter/xt_CT.c +2 -2
@@ -23,7 +23,7 @@static inline int xt_ct_target (struct sk_buff *skb,struct nf_conn *ct) {/* Previously seen (loopback)? Ignore. */ -if (skb->nfct !=NULL ) +if (skb->_nfct !=0 )return XT_CONTINUE ;/* special case the untracked ct : we want the percpu object */
@@ -409,7 +409,7 @@ static unsigned int notrack_tg(struct sk_buff *skb, const struct xt_action_param *par) {/* Previously seen (loopback)? Ignore. */ -if (skb->nfct !=NULL ) +if (skb->_nfct !=0 )return XT_CONTINUE ;nf_ct_set (skb,nf_ct_untracked_get (),IP_CT_NEW );
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 26/27 ipvs: free ip_vs_dest structs when refcnt=0
In reply to Pablo Neira AyusoFrom: David Windsor <[email protected]>
Currently, the ip_vs_dest cache frees ip_vs_dest objects when their reference count becomes < 0. Aside from not being semantically sound, this is problematic for the new type refcount_t, which will be introduced shortly in a separate patch. refcount_t is the new kernel type for holding reference counts, and provides overflow protection and a constrained interface relative to atomic_t (the type currently being used for kernel reference counts).
Per Julian Anastasov: "The problem is that dest_trash currently holds deleted dests (unlinked from RCU lists) with refcnt=0." Changing dest_trash to hold dest with refcnt=1 will allow us to free ip_vs_dest structs when their refcnt=0, in ip_vs_dest_put_and_free().
--- include/net/ip_vs.h | 2 +- net/netfilter/ipvs/ip_vs_ctl.c | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-)
include/net/ip_vs.h +1 -1
@@ -1421,7 +1421,7 @@ static inline void ip_vs_dest_put(struct ip_vs_dest *dest)static inline void ip_vs_dest_put_and_free (struct ip_vs_dest *dest) { -if (atomic_dec_return (&dest->refcnt) <0 ) +if (atomic_dec_and_test (&dest->refcnt))kfree (dest); }
net/netfilter/ipvs/ip_vs_ctl.c +3 -5
@@ -711,7 +711,6 @@ ip_vs_trash_get_dest(struct ip_vs_service *svc, int dest_af, dest->vport == svc->port))) {/* HIT */ list_del (&dest->t_list); -ip_vs_dest_hold (dest); goto out; } }
@@ -741,7 +740,7 @@ static void ip_vs_dest_free(struct ip_vs_dest *dest)* When the ip_vs_control_clearup is activated by ipvs module exit, * the service tables must have been flushed and all the connections * are expired, and the refcnt of each destination in the trash must -* be 0, so we simply release them here. +* be 1, so we simply release them here. */ static void ip_vs_trash_cleanup (struct netns_ipvs *ipvs) {
@@ -1080,11 +1079,10 @@ static void __ip_vs_del_dest(struct netns_ipvs *ipvs, struct ip_vs_dest *dest,if (list_empty (&ipvs->dest_trash) && !cleanup)mod_timer (&ipvs->dest_trash_timer, jiffies + (IP_VS_DEST_TRASH_PERIOD >>1 )); -/* dest lives in trash without reference */ +/* dest lives in trash with reference */ list_add (&dest->t_list, &ipvs->dest_trash); dest->idle_start =0 ;spin_unlock_bh (&ipvs->dest_trash_lock); -ip_vs_dest_put (dest); }
@@ -1160,7 +1158,7 @@ static void ip_vs_dest_trash_expire(unsigned long data)spin_lock (&ipvs->dest_trash_lock);list_for_each_entry_safe (dest, next, &ipvs->dest_trash, t_list) { -if (atomic_read (&dest->refcnt) >0 ) +if (atomic_read (&dest->refcnt) >1 )continue ;if (dest->idle_start) { if (time_before(now, dest->idle_start +
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 27/27 netfilter: allow logging from non-init namespaces
In reply to Pablo Neira AyusoFrom: Michal Kubeček <[email protected]>
Commit 69b34fb996b2 ("netfilter: xt_LOG: add net namespace support for xt_LOG") disabled logging packets using the LOG target from non-init namespaces. The motivation was to prevent containers from flooding kernel log of the host. The plan was to keep it that way until syslog namespace implementation allows containers to log in a safe way.
However, the work on syslog namespace seems to have hit a dead end somewhere in 2013 and there are users who want to use xt_LOG in all network namespaces. This patch allows to do so by setting
/proc/sys/net/netfilter/nf_log_all_netns
to a nonzero value. This sysctl is only accessible from init_net so that one cannot switch the behaviour from inside a container.
--- Documentation/networking/netfilter-sysctl.txt | 10 ++++++++++ include/net/netfilter/nf_log.h | 3 +++ net/bridge/netfilter/ebt_log.c | 2 +- net/ipv4/netfilter/nf_log_arp.c | 2 +- net/ipv4/netfilter/nf_log_ipv4.c | 2 +- net/ipv6/netfilter/nf_log_ipv6.c | 2 +- net/netfilter/nf_log.c | 24 ++++++++++++++++++++++++ 7 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 Documentation/networking/netfilter-sysctl.txt
Documentation/networking/netfilter-sysctl.txt new file +10 -0
@@ -0,0 +1,10 @@ +/proc/sys/net/netfilter/* Variables: + +nf_log_all_netns - BOOLEAN + 0 - disabled (default) + not 0 - enabled + + By default, only init_net namespace can log packets into kernel log + with LOG target; this aims to prevent containers from flooding host + kernel log. If enabled, this target also works in other network + namespaces. This variable is only accessible from init_net.
include/net/netfilter/nf_log.h +3 -0
@@ -51,6 +51,9 @@ struct nf_logger {struct module *me; }; +/* sysctl_nf_log_all_netns - allow LOG target in all network namespaces */ +extern int sysctl_nf_log_all_netns; +/* Function to register/unregister log function. */ int nf_log_register (u_int8_t pf,struct nf_logger *logger);void nf_log_unregister (struct nf_logger *logger);
net/bridge/netfilter/ebt_log.c +1 -1
@@ -78,7 +78,7 @@ ebt_log_packet(struct net *net, u_int8_t pf, unsigned int hooknum,unsigned int bitmask;/* FIXME: Disabled from containers until syslog ns is supported */ -if (!net_eq (net, &init_net)) +if (!net_eq (net, &init_net) && !sysctl_nf_log_all_netns)return ;spin_lock_bh (&ebt_log_lock);
net/ipv4/netfilter/nf_log_arp.c +1 -1
@@ -87,7 +87,7 @@ static void nf_log_arp_packet(struct net *net, u_int8_t pf,struct nf_log_buf *m;/* FIXME: Disabled from containers until syslog ns is supported */ -if (!net_eq (net, &init_net)) +if (!net_eq (net, &init_net) && !sysctl_nf_log_all_netns)return ; m =nf_log_buf_open ();
net/ipv4/netfilter/nf_log_ipv4.c +1 -1
@@ -319,7 +319,7 @@ static void nf_log_ip_packet(struct net *net, u_int8_t pf,struct nf_log_buf *m;/* FIXME: Disabled from containers until syslog ns is supported */ -if (!net_eq (net, &init_net)) +if (!net_eq (net, &init_net) && !sysctl_nf_log_all_netns)return ; m =nf_log_buf_open ();
net/ipv6/netfilter/nf_log_ipv6.c +1 -1
@@ -351,7 +351,7 @@ static void nf_log_ip6_packet(struct net *net, u_int8_t pf,struct nf_log_buf *m;/* FIXME: Disabled from containers until syslog ns is supported */ -if (!net_eq (net, &init_net)) +if (!net_eq (net, &init_net) && !sysctl_nf_log_all_netns)return ; m =nf_log_buf_open ();
net/netfilter/nf_log.c +24 -0
@@ -16,6 +16,9 @@#define NF_LOG_PREFIXLEN 128#define NFLOGGER_NAME_LEN 64 +int sysctl_nf_log_all_netns__read_mostly ; +EXPORT_SYMBOL (sysctl_nf_log_all_netns); +static struct nf_logger __rcu *loggers[NFPROTO_NUMPROTO ][NF_LOG_TYPE_MAX ]__read_mostly ;static DEFINE_MUTEX (nf_log_mutex);
@@ -414,6 +417,18 @@ static const struct file_operations nflog_file_ops = {#ifdef CONFIG_SYSCTL static char nf_log_sysctl_fnames[NFPROTO_NUMPROTO -NFPROTO_UNSPEC ][3 ];static struct ctl_table nf_log_sysctl_table[NFPROTO_NUMPROTO +1 ]; +static struct ctl_table_header *nf_log_sysctl_fhdr; + +static struct ctl_table nf_log_sysctl_ftable[] = { + { + .procname ="nf_log_all_netns" , + .data = &sysctl_nf_log_all_netns, + .maxlen =sizeof (sysctl_nf_log_all_netns), + .mode =0644 , + .proc_handler = proc_dointvec, + }, + { } +};static int nf_log_proc_dostring (struct ctl_table *table,int write,void __user *buffer,size_t *lenp,loff_t *ppos)
@@ -483,6 +498,10 @@ static int netfilter_log_sysctl_init(struct net *net) nf_log_sysctl_table[i].extra1 = (void *)(unsigned long ) i; } + nf_log_sysctl_fhdr =register_net_sysctl (net,"net/netfilter" , + nf_log_sysctl_ftable); +if (!nf_log_sysctl_fhdr) + goto err_freg; }for (i =NFPROTO_UNSPEC ; i <NFPROTO_NUMPROTO ; i++)
@@ -499,6 +518,9 @@ static int netfilter_log_sysctl_init(struct net *net) err_reg:if (!net_eq (net, &init_net))kfree (table); +else +unregister_net_sysctl_table (nf_log_sysctl_fhdr); +err_freg: err_alloc:return -ENOMEM ; }
@@ -511,6 +533,8 @@ static void netfilter_log_sysctl_exit(struct net *net)unregister_net_sysctl_table (net->nf.nf_log_dir_header);if (!net_eq (net, &init_net))kfree (table); +else + unregister_net_sysctl_table(nf_log_sysctl_fhdr); }#else static int netfilter_log_sysctl_init (struct net *net)
Signature · 1 line
2.1.4
David Miller
maintainerFrom: Pablo Neira Ayuso <[email protected]>
Date: Fri, 3 Feb 2017 13:25:11 +0100
Quoted 2 lines
The following patchset contains Netfilter updates for your net-next tree, they are:
...
Quoted 3 lines
You can pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git
Pulled, thanks a lot!
RE: [PATCH 24/27] netfilter: guarantee 8 byte minalign for template addresses
In reply to Pablo Neira AyusoFrom: Pablo Neira Ayuso
Quoted 6 lines
Sent: 03 February 2017 12:26
The next change will mergeskb->nfctpointer andskb->nfctinfostatus bits into singleskb->_nfct(unsigned long) area.For this to work
nf_connaddresses must always be aligned at least on an 8 byte boundary since we will need the lower 3bits to store nfctinfo.
Don't do it then.
You are using a bit somewhere else to allow you to use a 'hack' that stores 3 status bits into the bottom of an address where there are only 2 'available' bits.
That sounds like it just adds code everywhere.
I suspect you'll regret doing it later on as well.
David