Netfilter zone directions
Subsystems (4)
v4 cover Netfilter zone directions
This is a rework of the originally named flextuples [1] patch set, but after discussions from NFWS completely reworked towards integration into the existing zones infrastructure.
Please see individual patches for details.
Thanks!
[1] http://thread.gmane.org/gmane.comp.security.firewalls.netfilter.devel/57412/
v3 -> v4:
- Rebased & retested everything onto latest nf-next
- Added nested CTA_TUPLE_ZONE attribute with direction meta data
- Renamed CTA_DIR; sysctl was already in it's own function
v2 -> v3:
- Have a global default zone object, use it directly
- Do not touch uapi-exposed ct->status bits, but integrate the marking flag into the zones structure
- Rebased onto latest nf-next, rerun all stress tests
v1 -> v2:
- Reworked entire set, integration into zones
- Rebased onto latest nf-next
Daniel Borkmann (3):
netfilter: nf_conntrack: push zone object into functions
netfilter: nf_conntrack: add direction support for zones
netfilter: nf_conntrack: add efficient mark to zone mapping
include/net/netfilter/nf_conntrack.h | 10 +- include/net/netfilter/nf_conntrack_core.h | 3 +- include/net/netfilter/nf_conntrack_expect.h | 11 +- include/net/netfilter/nf_conntrack_zones.h | 99 ++++++++++- include/uapi/linux/netfilter/nfnetlink_conntrack.h | 16 ++ include/uapi/linux/netfilter/xt_CT.h | 8 +- net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c | 2 +- net/ipv4/netfilter/nf_conntrack_proto_icmp.c | 4 +- net/ipv4/netfilter/nf_defrag_ipv4.c | 17 +- net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c | 2 +- net/ipv6/netfilter/nf_conntrack_proto_icmpv6.c | 5 +- net/ipv6/netfilter/nf_defrag_ipv6_hooks.c | 18 +- net/netfilter/ipvs/ip_vs_nfct.c | 2 +- net/netfilter/nf_conntrack_core.c | 134 +++++++------- net/netfilter/nf_conntrack_expect.c | 21 ++- net/netfilter/nf_conntrack_netlink.c | 196 ++++++++++++++++----- net/netfilter/nf_conntrack_pptp.c | 3 +- net/netfilter/nf_conntrack_standalone.c | 30 +++- net/netfilter/nf_nat_core.c | 24 +-- net/netfilter/nf_synproxy_core.c | 4 +- net/netfilter/xt_CT.c | 26 ++- net/netfilter/xt_connlimit.c | 9 +- net/sched/act_connmark.c | 6 +- 23 files changed, 468 insertions(+), 182 deletions(-)
Signature · 1 line
1.9.3
PATCH v4 1/3 netfilter: nf_conntrack: push zone object into functions
This patch replaces the zone id which is pushed down into functions with the actual zone object. It's a bigger one-time change, but needed for later on extending zones with a direction parameter, and thus decoupling this additional information from all call-sites.
No functional changes in this patch.
The default zone becomes a global const object, namely nf_ct_zone_dflt and will be returned directly in various cases, one being, when there's f.e. no zoning support.
--- include/net/netfilter/nf_conntrack.h | 10 ++- include/net/netfilter/nf_conntrack_core.h | 3 +- include/net/netfilter/nf_conntrack_expect.h | 11 +++- include/net/netfilter/nf_conntrack_zones.h | 33 +++++++--- net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c | 2 +- net/ipv4/netfilter/nf_conntrack_proto_icmp.c | 3 +- net/ipv4/netfilter/nf_defrag_ipv4.c | 11 ++-- net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c | 2 +- net/ipv6/netfilter/nf_conntrack_proto_icmpv6.c | 3 +- net/ipv6/netfilter/nf_defrag_ipv6_hooks.c | 12 ++-- net/netfilter/ipvs/ip_vs_nfct.c | 2 +- net/netfilter/nf_conntrack_core.c | 75 ++++++++++++++--------- net/netfilter/nf_conntrack_expect.c | 21 ++++--- net/netfilter/nf_conntrack_netlink.c | 84 ++++++++++++++------------ net/netfilter/nf_conntrack_pptp.c | 3 +- net/netfilter/nf_conntrack_standalone.c | 17 ++++-- net/netfilter/nf_nat_core.c | 19 +++--- net/netfilter/nf_synproxy_core.c | 4 +- net/netfilter/xt_CT.c | 6 +- net/netfilter/xt_connlimit.c | 9 +-- net/sched/act_connmark.c | 5 +- 21 files changed, 203 insertions(+), 132 deletions(-)
include/net/netfilter/nf_conntrack.h +8 -2
@@ -250,8 +250,12 @@ void nf_ct_untracked_status_or(unsigned long bits);void nf_ct_iterate_cleanup (struct net *net,int (*iter)(struct nf_conn *i,void *data),void *data,u32 portid,int report); + +struct nf_conntrack_zone ; +void nf_conntrack_free (struct nf_conn *ct); -struct nf_conn *nf_conntrack_alloc (struct net *net,u16 zone, +struct nf_conn *nf_conntrack_alloc (struct net *net, +const struct nf_conntrack_zone *zone,const struct nf_conntrack_tuple *orig,const struct nf_conntrack_tuple *repl,gfp_t gfp);
@@ -291,7 +295,9 @@ extern unsigned int nf_conntrack_max;extern unsigned int nf_conntrack_hash_rnd;void init_nf_conntrack_hash_rnd (void ); -struct nf_conn *nf_ct_tmpl_alloc (struct net *net,u16 zone,gfp_t flags); +struct nf_conn *nf_ct_tmpl_alloc (struct net *net, +const struct nf_conntrack_zone *zone, +gfp_t flags);#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)
include/net/netfilter/nf_conntrack_core.h +2 -1
@@ -52,7 +52,8 @@ bool nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,/* Find a connection corresponding to a tuple. */ struct nf_conntrack_tuple_hash * -nf_conntrack_find_get (struct net *net,u16 zone, +nf_conntrack_find_get (struct net *net, +const struct nf_conntrack_zone *zone,const struct nf_conntrack_tuple *tuple);int __nf_conntrack_confirm (struct sk_buff *skb);
include/net/netfilter/nf_conntrack_expect.h +8 -3
@@ -4,7 +4,9 @@#ifndef _NF_CONNTRACK_EXPECT_H#define _NF_CONNTRACK_EXPECT_H +#include <net/netfilter/nf_conntrack.h> +#include <net/netfilter/nf_conntrack_zones.h> extern unsigned int nf_ct_expect_hsize;extern unsigned int nf_ct_expect_max;
@@ -76,15 +78,18 @@ int nf_conntrack_expect_init(void);void nf_conntrack_expect_fini (void );struct nf_conntrack_expect * -__nf_ct_expect_find (struct net *net,u16 zone, +__nf_ct_expect_find (struct net *net, +const struct nf_conntrack_zone *zone,const struct nf_conntrack_tuple *tuple);struct nf_conntrack_expect * -nf_ct_expect_find_get (struct net *net,u16 zone, +nf_ct_expect_find_get (struct net *net, +const struct nf_conntrack_zone *zone,const struct nf_conntrack_tuple *tuple);struct nf_conntrack_expect * -nf_ct_find_expectation (struct net *net,u16 zone, +nf_ct_find_expectation (struct net *net, +const struct nf_conntrack_zone *zone,const struct nf_conntrack_tuple *tuple); void nf_ct_unlink_expect_report(struct nf_conntrack_expect *exp,
include/net/netfilter/nf_conntrack_zones.h +23 -10
@@ -1,25 +1,38 @@#ifndef _NF_CONNTRACK_ZONES_H#define _NF_CONNTRACK_ZONES_H -#define NF_CT_DEFAULT_ZONE 0 - -#if defined(CONFIG_NF_CONNTRACK ) || defined(CONFIG_NF_CONNTRACK_MODULE ) -#include <net/netfilter/nf_conntrack_extend.h> +#define NF_CT_DEFAULT_ZONE_ID 0struct nf_conntrack_zone {u16 id; }; -static inline u16 nf_ct_zone (const struct nf_conn *ct) +extern const struct nf_conntrack_zone nf_ct_zone_dflt; + +#if IS_ENABLED (CONFIG_NF_CONNTRACK ) +#include <net/netfilter/nf_conntrack_extend.h> + +static inline const struct nf_conntrack_zone * +nf_ct_zone (const struct nf_conn *ct) { +const struct nf_conntrack_zone *nf_ct_zone =NULL ; +#ifdef CONFIG_NF_CONNTRACK_ZONES -struct nf_conntrack_zone *nf_ct_zone; nf_ct_zone =nf_ct_ext_find (ct,NF_CT_EXT_ZONE ); -if (nf_ct_zone) -return nf_ct_zone->id;#endif -return NF_CT_DEFAULT_ZONE ; +return nf_ct_zone ? nf_ct_zone : &nf_ct_zone_dflt; } -#endif /* CONFIG_NF_CONNTRACK || CONFIG_NF_CONNTRACK_MODULE */ +static inline const struct nf_conntrack_zone * +nf_ct_zone_tmpl (const struct nf_conn *tmpl) +{ +return tmpl ?nf_ct_zone (tmpl) : &nf_ct_zone_dflt; +} + +static inline bool nf_ct_zone_equal (const struct nf_conn *a, +const struct nf_conntrack_zone *b) +{ +return nf_ct_zone (a)->id == b->id; +} +#endif /* IS_ENABLED(CONFIG_NF_CONNTRACK) */ #endif /* _NF_CONNTRACK_ZONES_H */
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c +1 -1
@@ -280,7 +280,7 @@ getorigdst(struct sock *sk, int optval, void __user *user, int *len)return -EINVAL ; } - h =nf_conntrack_find_get (sock_net (sk),NF_CT_DEFAULT_ZONE , &tuple); + h =nf_conntrack_find_get (sock_net (sk), &nf_ct_zone_dflt, &tuple);if (h) {struct sockaddr_in sin;struct nf_conn *ct =nf_ct_tuplehash_to_ctrack (h);
net/ipv4/netfilter/nf_conntrack_proto_icmp.c +2 -1
@@ -134,9 +134,10 @@ icmp_error_message(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,struct nf_conntrack_tuple innertuple, origtuple;const struct nf_conntrack_l4proto *innerproto;const struct nf_conntrack_tuple_hash *h; -u16 zone = tmpl ?nf_ct_zone (tmpl) :NF_CT_DEFAULT_ZONE ; +const struct nf_conntrack_zone *zone;NF_CT_ASSERT (skb->nfct ==NULL ); + zone =nf_ct_zone_tmpl (tmpl);/* Are they talking about one of our connections? */ if (!nf_ct_get_tuplepr(skb,
net/ipv4/netfilter/nf_defrag_ipv4.c +5 -6
@@ -43,19 +43,18 @@ static int nf_ct_ipv4_gather_frags(struct sk_buff *skb, u_int32_t user)static enum ip_defrag_users nf_ct_defrag_user (unsigned int hooknum,struct sk_buff *skb) { -u16 zone =NF_CT_DEFAULT_ZONE ; - +u16 zone_id =NF_CT_DEFAULT_ZONE_ID ;#if IS_ENABLED (CONFIG_NF_CONNTRACK )if (skb->nfct) - zone =nf_ct_zone ((struct nf_conn *)skb->nfct); + zone_id =nf_ct_zone ((struct nf_conn *)skb->nfct)->id;#endif if (nf_bridge_in_prerouting (skb)) -return IP_DEFRAG_CONNTRACK_BRIDGE_IN + zone; +return IP_DEFRAG_CONNTRACK_BRIDGE_IN + zone_id;if (hooknum ==NF_INET_PRE_ROUTING ) -return IP_DEFRAG_CONNTRACK_IN + zone; +return IP_DEFRAG_CONNTRACK_IN + zone_id;else -return IP_DEFRAG_CONNTRACK_OUT + zone; +return IP_DEFRAG_CONNTRACK_OUT + zone_id; } static unsigned int ipv4_conntrack_defrag(const struct nf_hook_ops *ops,
net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c +1 -1
@@ -251,7 +251,7 @@ ipv6_getorigdst(struct sock *sk, int optval, void __user *user, int *len)if (*len <0 || (unsigned int ) *len <sizeof (sin6))return -EINVAL ; - h =nf_conntrack_find_get (sock_net (sk),NF_CT_DEFAULT_ZONE , &tuple); + h =nf_conntrack_find_get (sock_net (sk), &nf_ct_zone_dflt, &tuple);if (!h) { pr_debug("IP6T_SO_ORIGINAL_DST: Can't find %pI6c/%u-%pI6c/%u.\n", &tuple.src.u3.ip6, ntohs(tuple.src.u.tcp.port),
net/ipv6/netfilter/nf_conntrack_proto_icmpv6.c +1 -2
@@ -150,7 +150,6 @@ icmpv6_error_message(struct net *net, struct nf_conn *tmpl,struct nf_conntrack_tuple intuple, origtuple;const struct nf_conntrack_tuple_hash *h;const struct nf_conntrack_l4proto *inproto; -u16 zone = tmpl ?nf_ct_zone (tmpl) :NF_CT_DEFAULT_ZONE ;NF_CT_ASSERT (skb->nfct ==NULL );
@@ -177,7 +176,7 @@ icmpv6_error_message(struct net *net, struct nf_conn *tmpl, *ctinfo =IP_CT_RELATED ; - h =nf_conntrack_find_get (net, zone, &intuple); + h =nf_conntrack_find_get (net,nf_ct_zone_tmpl (tmpl), &intuple);if (!h) {pr_debug ("icmpv6_error: no match\n" );return -NF_ACCEPT ;
net/ipv6/netfilter/nf_defrag_ipv6_hooks.c +5 -7
@@ -33,20 +33,18 @@static enum ip6_defrag_users nf_ct6_defrag_user (unsigned int hooknum,struct sk_buff *skb) { -u16 zone =NF_CT_DEFAULT_ZONE ; - +u16 zone_id =NF_CT_DEFAULT_ZONE_ID ;#if IS_ENABLED (CONFIG_NF_CONNTRACK )if (skb->nfct) - zone =nf_ct_zone ((struct nf_conn *)skb->nfct); + zone_id =nf_ct_zone ((struct nf_conn *)skb->nfct)->id;#endif if (nf_bridge_in_prerouting (skb)) -return IP6_DEFRAG_CONNTRACK_BRIDGE_IN + zone; +return IP6_DEFRAG_CONNTRACK_BRIDGE_IN + zone_id;if (hooknum ==NF_INET_PRE_ROUTING ) -return IP6_DEFRAG_CONNTRACK_IN + zone; +return IP6_DEFRAG_CONNTRACK_IN + zone_id;else -return IP6_DEFRAG_CONNTRACK_OUT + zone; - +return IP6_DEFRAG_CONNTRACK_OUT + zone_id; } static unsigned int ipv6_defrag(const struct nf_hook_ops *ops,
net/netfilter/ipvs/ip_vs_nfct.c +1 -1
@@ -274,7 +274,7 @@ void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp)" for conn " FMT_CONN "\n" , __func__, ARG_TUPLE(&tuple), ARG_CONN(cp)); - h =nf_conntrack_find_get (ip_vs_conn_net (cp),NF_CT_DEFAULT_ZONE , + h =nf_conntrack_find_get (ip_vs_conn_net (cp), &nf_ct_zone_dflt, &tuple);if (h) { ct =nf_ct_tuplehash_to_ctrack (h);
net/netfilter/nf_conntrack_core.c +47 -28
257 lines not on this page · raw message
net/netfilter/nf_conntrack_expect.c +12 -9
@@ -88,7 +88,8 @@ static unsigned int nf_ct_expect_dst_hash(const struct nf_conntrack_tuple *tuple }struct nf_conntrack_expect * -__nf_ct_expect_find (struct net *net,u16 zone, +__nf_ct_expect_find (struct net *net, +const struct nf_conntrack_zone *zone,const struct nf_conntrack_tuple *tuple) {struct nf_conntrack_expect *i;
@@ -100,7 +101,7 @@ __nf_ct_expect_find(struct net *net, u16 zone, h =nf_ct_expect_dst_hash (tuple);hlist_for_each_entry_rcu (i, &net->ct.expect_hash[h], hnode) {if (nf_ct_tuple_mask_cmp (tuple, &i->tuple, &i->mask) && -nf_ct_zone (i->master) == zone) +nf_ct_zone_equal (i->master, zone))return i; }return NULL ;
@@ -109,7 +110,8 @@ EXPORT_SYMBOL_GPL(__nf_ct_expect_find);/* Just find a expectation corresponding to a tuple. */ struct nf_conntrack_expect * -nf_ct_expect_find_get (struct net *net,u16 zone, +nf_ct_expect_find_get (struct net *net, +const struct nf_conntrack_zone *zone,const struct nf_conntrack_tuple *tuple) {struct nf_conntrack_expect *i;
@@ -127,7 +129,8 @@ EXPORT_SYMBOL_GPL(nf_ct_expect_find_get);/* If an expectation for this connection is found, it gets delete from * global list then returned. */ struct nf_conntrack_expect * -nf_ct_find_expectation (struct net *net,u16 zone, +nf_ct_find_expectation (struct net *net, +const struct nf_conntrack_zone *zone,const struct nf_conntrack_tuple *tuple) {struct nf_conntrack_expect *i, *exp =NULL ;
@@ -140,7 +143,7 @@ nf_ct_find_expectation(struct net *net, u16 zone,hlist_for_each_entry (i, &net->ct.expect_hash[h], hnode) {if (!(i->flags &NF_CT_EXPECT_INACTIVE ) &&nf_ct_tuple_mask_cmp (tuple, &i->tuple, &i->mask) && -nf_ct_zone (i->master) == zone) { +nf_ct_zone_equal (i->master, zone)) { exp = i;break ; }
@@ -220,16 +223,16 @@ static inline int expect_clash(const struct nf_conntrack_expect *a, }return nf_ct_tuple_mask_cmp (&a->tuple, &b->tuple, &intersect_mask) && -nf_ct_zone (a->master) ==nf_ct_zone (b->master); +nf_ct_zone_equal (a->master,nf_ct_zone (b->master)); }static inline int expect_matches (const struct nf_conntrack_expect *a,const struct nf_conntrack_expect *b) {return a->master == b->master && a->class == b->class && -nf_ct_tuple_equal (&a->tuple, &b->tuple) && -nf_ct_tuple_mask_equal (&a->mask, &b->mask) && -nf_ct_zone (a->master) ==nf_ct_zone (b->master); +nf_ct_tuple_equal (&a->tuple, &b->tuple) && +nf_ct_tuple_mask_equal (&a->mask, &b->mask) && +nf_ct_zone_equal (a->master,nf_ct_zone (b->master)); }/* Generally a bad idea to call this: could have matched already. */
net/netfilter/nf_conntrack_netlink.c +44 -40
240 lines not on this page · raw message
net/netfilter/nf_conntrack_pptp.c +2 -1
@@ -143,13 +143,14 @@ static int destroy_sibling_or_exp(struct net *net, struct nf_conn *ct, const struct nf_conntrack_tuple *t) {const struct nf_conntrack_tuple_hash *h; +const struct nf_conntrack_zone *zone;struct nf_conntrack_expect *exp;struct nf_conn *sibling; -u16 zone =nf_ct_zone (ct);pr_debug ("trying to timeout ct or exp for tuple " );nf_ct_dump_tuple (t); + zone =nf_ct_zone (ct); h =nf_conntrack_find_get (net, zone, t);if (h) { sibling =nf_ct_tuplehash_to_ctrack (h);
net/netfilter/nf_conntrack_standalone.c +12 -5
@@ -140,6 +140,17 @@ static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct) }#endif +#ifdef CONFIG_NF_CONNTRACK_ZONES +static void ct_show_zone (struct seq_file *s,const struct nf_conn *ct) +{ +seq_printf (s,"zone=%u " ,nf_ct_zone (ct)->id); +} +#else +static inline void ct_show_zone (struct seq_file *s,const struct nf_conn *ct) +{ +} +#endif +#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP static void ct_show_delta_time (struct seq_file *s,const struct nf_conn *ct) {
@@ -228,11 +239,7 @@ static int ct_seq_show(struct seq_file *s, void *v)#endif ct_show_secctx (s, ct); - -#ifdef CONFIG_NF_CONNTRACK_ZONES -seq_printf (s,"zone=%u " ,nf_ct_zone (ct)); -#endif - +ct_show_zone (s, ct);ct_show_delta_time (s, ct);seq_printf (s,"use=%u\n" ,atomic_read (&ct->ct_general.use));
net/netfilter/nf_nat_core.c +12 -7
@@ -118,14 +118,15 @@ EXPORT_SYMBOL(nf_xfrm_me_harder);/* We keep an extra hash for each conntrack, for fast searching. */ static inline unsigned int -hash_by_src (const struct net *net,u16 zone, +hash_by_src (const struct net *net, +const struct nf_conntrack_zone *zone,const struct nf_conntrack_tuple *tuple) {unsigned int hash;/* Original src, to ensure we map it consistently if poss. */ hash =jhash2 ((u32 *)&tuple->src,sizeof (tuple->src) /sizeof (u32), - tuple->dst.protonum ^ zone ^ nf_conntrack_hash_rnd); + tuple->dst.protonum ^ zone->id ^ nf_conntrack_hash_rnd);return reciprocal_scale (hash, net->ct.nat_htable_size); }
@@ -185,7 +186,8 @@ same_src(const struct nf_conn *ct,/* Only called for SRC manip */ static int -find_appropriate_src(struct net *net, u16 zone, +find_appropriate_src(struct net *net, + const struct nf_conntrack_zone *zone, const struct nf_nat_l3proto *l3proto, const struct nf_nat_l4proto *l4proto, const struct nf_conntrack_tuple *tuple,
@@ -198,7 +200,7 @@ find_appropriate_src(struct net *net, u16 zone,hlist_for_each_entry_rcu (nat, &net->ct.nat_bysource[h], bysource) { ct = nat->ct; -if (same_src (ct, tuple) &&nf_ct_zone (ct) == zone) { +if (same_src (ct, tuple) &&nf_ct_zone_equal (ct, zone)) {/* Copy source part from reply tuple. */ nf_ct_invert_tuplepr (result, &ct->tuplehash[IP_CT_DIR_REPLY ].tuple);
@@ -218,7 +220,8 @@ find_appropriate_src(struct net *net, u16 zone,* the ip with the lowest src-ip/dst-ip/proto usage. */ static void -find_best_ips_proto (u16 zone,struct nf_conntrack_tuple *tuple, +find_best_ips_proto (const struct nf_conntrack_zone *zone, +struct nf_conntrack_tuple *tuple,const struct nf_nat_range *range,const struct nf_conn *ct,enum nf_nat_manip_type maniptype)
@@ -258,7 +261,7 @@ find_best_ips_proto(u16 zone, struct nf_conntrack_tuple *tuple,*/ j =jhash2 ((u32 *)&tuple->src.u3,sizeof (tuple->src.u3) /sizeof (u32), range->flags &NF_NAT_RANGE_PERSISTENT ? -0 : (__force u32 )tuple->dst.u3.all[max] ^ zone); +0 : (__force u32 )tuple->dst.u3.all[max] ^ zone->id); full_range = false;for (i =0 ; i <= max; i++) {
@@ -297,10 +300,12 @@ get_unique_tuple(struct nf_conntrack_tuple *tuple,struct nf_conn *ct, enum nf_nat_manip_type maniptype) { + const struct nf_conntrack_zone *zone;const struct nf_nat_l3proto *l3proto;const struct nf_nat_l4proto *l4proto;struct net *net =nf_ct_net (ct); -u16 zone =nf_ct_zone (ct); + + zone =nf_ct_zone (ct);rcu_read_lock (); l3proto =__nf_nat_l3proto_find (orig_tuple->src.l3num);
net/netfilter/nf_synproxy_core.c +3 -1
@@ -17,10 +17,12 @@#include <linux/netfilter/x_tables.h> #include <linux/netfilter/xt_tcpudp.h> #include <linux/netfilter/xt_SYNPROXY.h> +#include <net/netfilter/nf_conntrack.h> #include <net/netfilter/nf_conntrack_extend.h> #include <net/netfilter/nf_conntrack_seqadj.h> #include <net/netfilter/nf_conntrack_synproxy.h> +#include <net/netfilter/nf_conntrack_zones.h> int synproxy_net_id;EXPORT_SYMBOL_GPL (synproxy_net_id);
@@ -352,7 +354,7 @@ static int __net_init synproxy_net_init(struct net *net)struct nf_conn *ct;int err = -ENOMEM ; - ct =nf_ct_tmpl_alloc (net,0 ,GFP_KERNEL ); + ct =nf_ct_tmpl_alloc (net, &nf_ct_zone_dflt,GFP_KERNEL );if (IS_ERR (ct)) { err =PTR_ERR (ct); goto err1;
net/netfilter/xt_CT.c +5 -1
@@ -184,6 +184,7 @@ out:static int xt_ct_tg_check (const struct xt_tgchk_param *par,struct xt_ct_target_info_v1 *info) { +struct nf_conntrack_zone zone;struct nf_conn *ct;int ret = -EOPNOTSUPP ;
@@ -201,7 +202,10 @@ static int xt_ct_tg_check(const struct xt_tgchk_param *par,if (ret <0 ) goto err1; - ct =nf_ct_tmpl_alloc (par->net, info->zone,GFP_KERNEL ); +memset (&zone,0 ,sizeof (zone)); + zone.id = info->zone; + + ct =nf_ct_tmpl_alloc (par->net, &zone,GFP_KERNEL ); ret =PTR_ERR (ct);if (IS_ERR (ct)) goto err2;
net/netfilter/xt_connlimit.c +5 -4
@@ -134,7 +134,7 @@ static bool add_hlist(struct hlist_head *head,static unsigned int check_hlist (struct net *net,struct hlist_head *head,const struct nf_conntrack_tuple *tuple, -u16 zone, +const struct nf_conntrack_zone *zone,bool *addit) {const struct nf_conntrack_tuple_hash *found;
@@ -201,7 +201,7 @@ static unsigned int count_tree(struct net *net, struct rb_root *root, const struct nf_conntrack_tuple *tuple, const union nf_inet_addr *addr, const union nf_inet_addr *mask, - u8 family, u16 zone) + u8 family, const struct nf_conntrack_zone *zone) { struct xt_connlimit_rb *gc_nodes[CONNLIMIT_GC_MAX_NODES];struct rb_node **rbnode, *parent;
@@ -290,7 +290,8 @@ static int count_them(struct net *net,const struct nf_conntrack_tuple *tuple, const union nf_inet_addr *addr, const union nf_inet_addr *mask, - u_int8_t family, u16 zone) + u_int8_t family, + const struct nf_conntrack_zone *zone) { struct rb_root *root;int count;
@@ -321,10 +322,10 @@ connlimit_mt(const struct sk_buff *skb, struct xt_action_param *par)union nf_inet_addr addr;struct nf_conntrack_tuple tuple;const struct nf_conntrack_tuple *tuple_ptr = &tuple; +const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;enum ip_conntrack_info ctinfo;const struct nf_conn *ct;unsigned int connections; -u16 zone =NF_CT_DEFAULT_ZONE ; ct =nf_ct_get (skb, &ctinfo);if (ct !=NULL ) {
net/sched/act_connmark.c +4 -1
@@ -37,6 +37,7 @@ static int tcf_connmark(struct sk_buff *skb, const struct tc_action *a,struct nf_conntrack_tuple tuple;enum ip_conntrack_info ctinfo;struct tcf_connmark_info *ca = a->priv; +struct nf_conntrack_zone zone;struct nf_conn *c;int proto;
@@ -70,7 +71,9 @@ static int tcf_connmark(struct sk_buff *skb, const struct tc_action *a, proto, &tuple)) goto out; - thash =nf_conntrack_find_get (dev_net (skb->dev), ca->zone, &tuple); + zone.id = ca->zone; + + thash =nf_conntrack_find_get (dev_net (skb->dev), &zone, &tuple);if (!thash) goto out;
Signature · 1 line
1.9.3
Re: [PATCH v4 1/3] netfilter: nf_conntrack: push zone object into functions
Quoted 10 lines · Daniel Borkmann
This patch replaces the zone id which is pushed down into functions with the actual zone object. It's a bigger one-time change, but needed for later on extending zones with a direction parameter, and thus decoupling this additional information from all call-sites.
No functional changes in this patch.
The default zone becomes a global const object, namely
nf_ct_zone_dfltand will be returned directly in various cases, one being, when there's f.e. no zoning support.
Applied, thanks Daniel.
PATCH v4 2/3 netfilter: nf_conntrack: add direction support for zones
In reply to Daniel BorkmannThis work adds a direction parameter to netfilter zones, so identity separation can be performed only in original/reply or both directions (default). This basically opens up the possibility of doing NAT with conflicting IP address/port tuples from multiple, isolated tenants on a host (e.g. from a netns) without requiring each tenant to NAT twice resp. to use its own dedicated IP address to SNAT to, meaning overlapping tuples can be made unique with the zone identifier in original direction, where the NAT engine will then allocate a unique tuple in the commonly shared default zone for the reply direction. In some restricted, local DNAT cases, also port redirection could be used for making the reply traffic unique w/o requiring SNAT.
The consensus we've reached and discussed at NFWS and since the initial implementation [1] was to directly integrate the direction meta data into the existing zones infrastructure, as opposed to the ct->mark approach we proposed initially.
As we pass the nf_conntrack_zone object directly around, we don't have to touch all call-sites, but only those, that contain equality checks of zones. Thus, based on the current direction (original or reply), we either return the actual id, or the default NF_CT_DEFAULT_ZONE_ID. CT expectations are direction-agnostic entities when expectations are being compared among themselves, so we can only use the identifier in this case.
Note that zone identifiers can not be included into the hash mix anymore as they don't contain a "stable" value that would be equal for both directions at all times, f.e. if only zone->id would unconditionally be xor'ed into the table slot hash, then replies won't find the corresponding conntracking entry anymore.
If no particular direction is specified when configuring zones, the behaviour is exactly as we expect currently (both directions).
Support has been added for the CT netlink interface as well as the x_tables raw CT target, which both already offer existing interfaces to user space for the configuration of zones.
Below a minimal, simplified collision example (script in [2]) with netperf sessions:
+--- tenant-1 ---+ mark := 1
| netperf |--+
+----------------+ | CT zone := mark [ORIGINAL]
[ip,sport] := X +--------------+ +--- gateway ---+
| mark routing |--| SNAT |-- ... +
+--------------+ +---------------+ |
+--- tenant-2 ---+ | ~~~|~~~
| netperf |--+ +-----------+ |
+----------------+ mark := 2 | netserver |------ ... +
[ip,sport] := X +-----------+
[ip,port] := YOn the gateway netns, example:
iptables -t raw -A PREROUTING -j CT --zone mark --zone-dir ORIGINAL iptables -t nat -A POSTROUTING -o <dev> -j SNAT --to-source <ip> --random-fully iptables -t mangle -A PREROUTING -m conntrack --ctdir ORIGINAL -j CONNMARK --save-mark iptables -t mangle -A POSTROUTING -m conntrack --ctdir REPLY -j CONNMARK --restore-mark
conntrack -L from gateway netns:
netperf -H 10.1.1.2 -t TCP_STREAM -l60 -p12865,5555 from each tenant netns
tcp 6 431995 ESTABLISHED src=40.1.1.1 dst=10.1.1.2 sport=5555 dport=12865
src=10.1.1.2 dst=10.1.1.1 sport=12865 dport=1024
[ASSURED] mark=1 secctx=system_u:object_r:unlabeled_t:s0
zone=1 use=1 zone-dir=original
tcp 6 431994 ESTABLISHED src=40.1.1.1 dst=10.1.1.2 sport=5555 dport=12865
src=10.1.1.2 dst=10.1.1.1 sport=12865 dport=5555
[ASSURED] mark=2 secctx=system_u:object_r:unlabeled_t:s0
zone=2 use=1 zone-dir=original
tcp 6 299 ESTABLISHED src=40.1.1.1 dst=10.1.1.2 sport=39438 dport=33768
src=10.1.1.2 dst=10.1.1.1 sport=33768 dport=39438
[ASSURED] mark=1 secctx=system_u:object_r:unlabeled_t:s0
zone=1 use=1 zone-dir=original
tcp 6 300 ESTABLISHED src=40.1.1.1 dst=10.1.1.2 sport=32889 dport=40206
src=10.1.1.2 dst=10.1.1.1 sport=40206 dport=32889
[ASSURED] mark=2 secctx=system_u:object_r:unlabeled_t:s0
zone=2 use=2 zone-dir=original
Taking this further, test script in [2] creates 200 tenants and runs original-tuple colliding netperf sessions each. A conntrack -L dump in the gateway netns also confirms 200 overlapping entries, all in ESTABLISHED state as expected.
I also did run various other tests with some permutations of the script, to mention some: SNAT in random/random-fully/persistent mode, no zones (no overlaps), static zones (original, reply, both directions), etc.
[1] http://thread.gmane.org/gmane.comp.security.firewalls.netfilter.devel/57412/
[2] https://paste.fedoraproject.org/242835/65657871/
--- include/net/netfilter/nf_conntrack_zones.h | 31 +++++- include/uapi/linux/netfilter/nfnetlink_conntrack.h | 16 +++ include/uapi/linux/netfilter/xt_CT.h | 6 +- net/ipv4/netfilter/nf_defrag_ipv4.c | 8 +- net/ipv6/netfilter/nf_defrag_ipv6_hooks.c | 8 +- net/netfilter/nf_conntrack_core.c | 53 ++++----- net/netfilter/nf_conntrack_expect.c | 8 +- net/netfilter/nf_conntrack_netlink.c | 124 +++++++++++++++++++-- net/netfilter/nf_conntrack_standalone.c | 15 ++- net/netfilter/nf_nat_core.c | 13 +-- net/netfilter/xt_CT.c | 17 ++- net/sched/act_connmark.c | 1 + 12 files changed, 243 insertions(+), 57 deletions(-)
include/net/netfilter/nf_conntrack_zones.h +30 -1
@@ -1,10 +1,18 @@#ifndef _NF_CONNTRACK_ZONES_H#define _NF_CONNTRACK_ZONES_H +#include <linux/netfilter/nf_conntrack_tuple_common.h> +#define NF_CT_DEFAULT_ZONE_ID 0 +#define NF_CT_ZONE_DIR_ORIG (1 << IP_CT_DIR_ORIGINAL) +#define NF_CT_ZONE_DIR_REPL (1 << IP_CT_DIR_REPLY) + +#define NF_CT_DEFAULT_ZONE_DIR (NF_CT_ZONE_DIR_ORIG | NF_CT_ZONE_DIR_REPL) +struct nf_conntrack_zone {u16 id; +u16 dir; };extern const struct nf_conntrack_zone nf_ct_zone_dflt;
@@ -29,8 +37,29 @@ nf_ct_zone_tmpl(const struct nf_conn *tmpl)return tmpl ?nf_ct_zone (tmpl) : &nf_ct_zone_dflt; } +static inline bool nf_ct_zone_matches_dir (const struct nf_conntrack_zone *zone, +enum ip_conntrack_dir dir) +{ +return zone->dir & (1 << dir); +} + +static inline u16 nf_ct_zone_id (const struct nf_conntrack_zone *zone, +enum ip_conntrack_dir dir) +{ +return nf_ct_zone_matches_dir (zone, dir) ? + zone->id :NF_CT_DEFAULT_ZONE_ID ; +} +static inline bool nf_ct_zone_equal (const struct nf_conn *a, -const struct nf_conntrack_zone *b) +const struct nf_conntrack_zone *b, +enum ip_conntrack_dir dir) +{ +return nf_ct_zone_id (nf_ct_zone (a), dir) == +nf_ct_zone_id (b, dir); +} + +static inline bool nf_ct_zone_equal_any (const struct nf_conn *a, +const struct nf_conntrack_zone *b) {return nf_ct_zone (a)->id == b->id; }
include/uapi/linux/netfilter/nfnetlink_conntrack.h +16 -0
@@ -53,6 +53,7 @@ enum ctattr_type {CTA_MARK_MASK ,CTA_LABELS ,CTA_LABELS_MASK , +CTA_TUPLE_ZONE , __CTA_MAX };#define CTA_MAX (__CTA_MAX - 1)
@@ -260,4 +261,19 @@ enum ctattr_expect_stats { };#define CTA_STATS_EXP_MAX (__CTA_STATS_EXP_MAX - 1) +enum ctattr_zone { +CTA_ZONE_UNSPEC , +CTA_ZONE_DIR , + __CTA_ZONE_MAX, +}; +#define CTA_ZONE_MAX (__CTA_ZONE_MAX - 1) + +enum ctattr_zone_dir { +CTA_ZONE_DIR_UNSPEC , +CTA_ZONE_DIR_ORIG , +CTA_ZONE_DIR_REPL , + __CTA_ZONE_DIR_MAX +}; +#define CTA_ZONE_DIR_MAX (__CTA_ZONE_DIR_MAX - 1) +#endif /* _IPCONNTRACK_NETLINK_H */
include/uapi/linux/netfilter/xt_CT.h +5 -1
@@ -6,7 +6,11 @@enum {XT_CT_NOTRACK =1 <<0 ,XT_CT_NOTRACK_ALIAS =1 <<1 , -XT_CT_MASK =XT_CT_NOTRACK |XT_CT_NOTRACK_ALIAS , +XT_CT_ZONE_DIR_ORIG =1 <<2 , +XT_CT_ZONE_DIR_REPL =1 <<3 , + +XT_CT_MASK =XT_CT_NOTRACK |XT_CT_NOTRACK_ALIAS | +XT_CT_ZONE_DIR_ORIG |XT_CT_ZONE_DIR_REPL , };struct xt_ct_target_info {
net/ipv4/netfilter/nf_defrag_ipv4.c +6 -2
@@ -45,8 +45,12 @@ 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) - zone_id =nf_ct_zone ((struct nf_conn *)skb->nfct)->id; +if (skb->nfct) { +enum ip_conntrack_info ctinfo; +const struct nf_conn *ct =nf_ct_get (skb, &ctinfo); + + zone_id =nf_ct_zone_id (nf_ct_zone (ct),CTINFO2DIR (ctinfo)); + }#endif if (nf_bridge_in_prerouting (skb))return IP_DEFRAG_CONNTRACK_BRIDGE_IN + zone_id;
net/ipv6/netfilter/nf_defrag_ipv6_hooks.c +6 -2
@@ -35,8 +35,12 @@ 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) - zone_id =nf_ct_zone ((struct nf_conn *)skb->nfct)->id; +if (skb->nfct) { +enum ip_conntrack_info ctinfo; +const struct nf_conn *ct =nf_ct_get (skb, &ctinfo); + + zone_id =nf_ct_zone_id (nf_ct_zone (ct),CTINFO2DIR (ctinfo)); + }#endif if (nf_bridge_in_prerouting (skb))return IP6_DEFRAG_CONNTRACK_BRIDGE_IN + zone_id;
net/netfilter/nf_conntrack_core.c +27 -26
187 lines not on this page · raw message
net/netfilter/nf_conntrack_expect.c +4 -4
@@ -101,7 +101,7 @@ __nf_ct_expect_find(struct net *net, h =nf_ct_expect_dst_hash (tuple);hlist_for_each_entry_rcu (i, &net->ct.expect_hash[h], hnode) {if (nf_ct_tuple_mask_cmp (tuple, &i->tuple, &i->mask) && -nf_ct_zone_equal (i->master, zone)) +nf_ct_zone_equal_any (i->master, zone))return i; }return NULL ;
@@ -143,7 +143,7 @@ nf_ct_find_expectation(struct net *net,hlist_for_each_entry (i, &net->ct.expect_hash[h], hnode) {if (!(i->flags &NF_CT_EXPECT_INACTIVE ) &&nf_ct_tuple_mask_cmp (tuple, &i->tuple, &i->mask) && -nf_ct_zone_equal (i->master, zone)) { +nf_ct_zone_equal_any (i->master, zone)) { exp = i;break ; }
@@ -223,7 +223,7 @@ static inline int expect_clash(const struct nf_conntrack_expect *a, }return nf_ct_tuple_mask_cmp (&a->tuple, &b->tuple, &intersect_mask) && -nf_ct_zone_equal (a->master,nf_ct_zone (b->master)); +nf_ct_zone_equal_any (a->master,nf_ct_zone (b->master)); } static inline int expect_matches(const struct nf_conntrack_expect *a,
@@ -232,7 +232,7 @@ static inline int expect_matches(const struct nf_conntrack_expect *a,return a->master == b->master && a->class == b->class &&nf_ct_tuple_equal (&a->tuple, &b->tuple) &&nf_ct_tuple_mask_equal (&a->mask, &b->mask) && -nf_ct_zone_equal (a->master,nf_ct_zone (b->master)); +nf_ct_zone_equal_any (a->master,nf_ct_zone (b->master)); }/* Generally a bad idea to call this: could have matched already. */
net/netfilter/nf_conntrack_netlink.c +112 -12
226 lines not on this page · raw message
net/netfilter/nf_conntrack_standalone.c +14 -1
@@ -143,7 +143,20 @@ static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)#ifdef CONFIG_NF_CONNTRACK_ZONES static void ct_show_zone (struct seq_file *s,const struct nf_conn *ct) { -seq_printf (s,"zone=%u " ,nf_ct_zone (ct)->id); +const struct nf_conntrack_zone *zone =nf_ct_zone (ct); + +seq_printf (s,"zone=%u " , zone->id); + +switch (zone->dir) { +case NF_CT_ZONE_DIR_ORIG : +seq_puts (s,"zone-dir=ORIGINAL " ); +break ; +case NF_CT_ZONE_DIR_REPL : +seq_puts (s,"zone-dir=REPLY " ); +break ; +default : +break ; + } }#else static inline void ct_show_zone (struct seq_file *s,const struct nf_conn *ct)
net/netfilter/nf_nat_core.c +6 -7
@@ -118,15 +118,13 @@ EXPORT_SYMBOL(nf_xfrm_me_harder);/* We keep an extra hash for each conntrack, for fast searching. */ static inline unsigned int -hash_by_src (const struct net *net, -const struct nf_conntrack_zone *zone, -const struct nf_conntrack_tuple *tuple) +hash_by_src (const struct net *net,const struct nf_conntrack_tuple *tuple) {unsigned int hash;/* Original src, to ensure we map it consistently if poss. */ hash =jhash2 ((u32 *)&tuple->src,sizeof (tuple->src) /sizeof (u32), - tuple->dst.protonum ^ zone->id ^ nf_conntrack_hash_rnd); + tuple->dst.protonum ^ nf_conntrack_hash_rnd);return reciprocal_scale (hash, net->ct.nat_htable_size); }
@@ -194,13 +192,14 @@ find_appropriate_src(struct net *net,struct nf_conntrack_tuple *result, const struct nf_nat_range *range) { - unsigned int h = hash_by_src(net, zone, tuple); + unsigned int h = hash_by_src(net, tuple);const struct nf_conn_nat *nat;const struct nf_conn *ct;hlist_for_each_entry_rcu (nat, &net->ct.nat_bysource[h], bysource) { ct = nat->ct; -if (same_src (ct, tuple) &&nf_ct_zone_equal (ct, zone)) { +if (same_src (ct, tuple) && +nf_ct_zone_equal (ct, zone,IP_CT_DIR_ORIGINAL )) {/* Copy source part from reply tuple. */ nf_ct_invert_tuplepr (result, &ct->tuplehash[IP_CT_DIR_REPLY ].tuple);
@@ -425,7 +424,7 @@ nf_nat_setup_info(struct nf_conn *ct,if (maniptype ==NF_NAT_MANIP_SRC ) {unsigned int srchash; - srchash =hash_by_src (net,nf_ct_zone (ct), + srchash =hash_by_src (net, &ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple);spin_lock_bh (&nf_nat_lock);/* nf_conntrack_alter_reply might re-allocate extension aera */
net/netfilter/xt_CT.c +16 -1
@@ -181,6 +181,19 @@ out:#endif } +static u16 xt_ct_flags_to_dir (const struct xt_ct_target_info_v1 *info) +{ +switch (info->flags & (XT_CT_ZONE_DIR_ORIG | +XT_CT_ZONE_DIR_REPL )) { +case XT_CT_ZONE_DIR_ORIG : +return NF_CT_ZONE_DIR_ORIG ; +case XT_CT_ZONE_DIR_REPL : +return NF_CT_ZONE_DIR_REPL ; +default : +return NF_CT_DEFAULT_ZONE_DIR ; + } +} +static int xt_ct_tg_check (const struct xt_tgchk_param *par,struct xt_ct_target_info_v1 *info) {
@@ -194,7 +207,8 @@ static int xt_ct_tg_check(const struct xt_tgchk_param *par, }#ifndef CONFIG_NF_CONNTRACK_ZONES -if (info->zone) +if (info->zone || info->flags & (XT_CT_ZONE_DIR_ORIG | +XT_CT_ZONE_DIR_REPL )) goto err1;#endif
@@ -204,6 +218,7 @@ static int xt_ct_tg_check(const struct xt_tgchk_param *par,memset (&zone,0 ,sizeof (zone)); zone.id = info->zone; + zone.dir =xt_ct_flags_to_dir (info); ct =nf_ct_tmpl_alloc (par->net, &zone,GFP_KERNEL ); ret =PTR_ERR (ct);
net/sched/act_connmark.c +1 -0
@@ -72,6 +72,7 @@ static int tcf_connmark(struct sk_buff *skb, const struct tc_action *a, goto out; zone.id = ca->zone; + zone.dir =NF_CT_DEFAULT_ZONE_DIR ; thash =nf_conntrack_find_get (dev_net (skb->dev), &zone, &tuple);if (!thash)
Signature · 1 line
1.9.3
Pablo Neira Ayuso
maintainerRe: [PATCH v4 2/3] netfilter: nf_conntrack: add direction support for zones
Hi Daniel,
I have applied 1/3 so you don't need to resend, but I still need one more change in this patch, see below.
include/uapi/linux/netfilter/nfnetlink_conntrack.h enum ctattr_type { · line 56
Quoted 9 lines · Daniel Borkmann
include/uapi/linux/netfilter/nfnetlink_conntrack.h +1
-0@@ -53,6 +53,7 @@ enum ctattr_type {CTA_MARK_MASK ,CTA_LABELS ,CTA_LABELS_MASK , + CTA_TUPLE_ZONE,
I remember to have suggested to place this in ctattr_tuple:
http://www.spinics.net/lists/netfilter-devel/msg37593.html
The zone is part of the tuple in an optional fashion, so it should appear there. The direction is already implicit based on CTA_TUPLE_ORIG or CTA_TUPLE_REPLY.
Quoted 11 lines
include/uapi/linux/netfilter/nfnetlink_conntrack.h +4
-0__CTA_MAX };#define CTA_MAX (__CTA_MAX - 1)@@ -260,4 +261,19 @@ enum ctattr_expect_stats { };#define CTA_STATS_EXP_MAX (__CTA_STATS_EXP_MAX - 1) +enum ctattr_zone { +CTA_ZONE_UNSPEC , +CTA_ZONE_DIR , + __CTA_ZONE_MAX,
include/uapi/linux/netfilter/nfnetlink_conntrack.h enum ctattr_expect_stats { · line 277
Quoted 10 lines
include/uapi/linux/netfilter/nfnetlink_conntrack.h +10
-0+}; +#define CTA_ZONE_MAX (__CTA_ZONE_MAX - 1) + +enum ctattr_zone_dir { +CTA_ZONE_DIR_UNSPEC , +CTA_ZONE_DIR_ORIG , +CTA_ZONE_DIR_REPL , + __CTA_ZONE_DIR_MAX +}; +#define CTA_ZONE_DIR_MAX (__CTA_ZONE_DIR_MAX - 1)
With the change above we can skip this CTA_ZONE_DIR.
Quoted 2 lines
include/uapi/linux/netfilter/nfnetlink_conntrack.h +1
-0+#endif /* _IPCONNTRACK_NETLINK_H */
[...]
Quoted 10 lines
net/netfilter/nf_conntrack_standalone.c +1
-1@@ -143,7 +143,20 @@ static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)#ifdef CONFIG_NF_CONNTRACK_ZONES static void ct_show_zone (struct seq_file *s,const struct nf_conn *ct) { -seq_printf (s,"zone=%u " ,nf_ct_zone (ct)->id); +const struct nf_conntrack_zone *zone =nf_ct_zone (ct);
net/netfilter/nf_conntrack_standalone.c static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct) · line 156
Quoted 10 lines
net/netfilter/nf_conntrack_standalone.c +10
-0+ +seq_printf (s,"zone=%u " , zone->id); + +switch (zone->dir) { +case NF_CT_ZONE_DIR_ORIG : +seq_puts (s,"zone-dir=ORIGINAL " ); +break ; +case NF_CT_ZONE_DIR_REPL : +seq_puts (s,"zone-dir=REPLY " ); +break ;
I'd suggest the output shows the zone on the corresponding tuple, eg. in case it only applies to the original tuple:
udp 17 29 src=192.168.2.195 dst=192.168.2.1 sport=40446 dport=53 zone=1 \
src=192.168.2.1 dst=192.168.2.195 sport=53 dport=40446 [ASSURED] mark=0 use=1We have a more compact output IMO.
Please, don't forget that you also have to update libnetfilter_conntrack and conntrack to get this feature available from there. I'll take this patchset to the kernel so you have the time to update the userspace side later on without blocking this further.
Thanks.
Re: [PATCH v4 2/3] netfilter: nf_conntrack: add direction support for zones
Hi Pablo,
thanks a lot for applying patch 1/3!
On 08/12/2015 07:48 PM, Pablo Neira Ayuso wrote:
...
Quoted 13 lines · Daniel Borkmann
include/uapi/linux/netfilter/nfnetlink_conntrack.h enum ctattr_type { · line 56
Quoted 9 lines
include/uapi/linux/netfilter/nfnetlink_conntrack.h +1
-0@@ -53,6 +53,7 @@ enum ctattr_type {CTA_MARK_MASK ,CTA_LABELS ,CTA_LABELS_MASK , + CTA_TUPLE_ZONE,I remember to have suggested to place this in
ctattr_tuple:
Quoted 5 lines
http://www.spinics.net/lists/netfilter-devel/msg37593.html
The zone is part of the tuple in an optional fashion, so it should appear there. The direction is already implicit based on
CTA_TUPLE_ORIGorCTA_TUPLE_REPLY.
Sorry, seems like I totally misunderstood your email. :/
I thought to place a CTA_ZONE_DIR attribute into a new nested CTA_TUPLE_ZONE container, where also possible future meta data can be placed there.
Thus, we'd have CTA_ZONE as the id itself and CTA_TUPLE_ZONE with additional optional data related to the zone, but it seems this was your /initial/ suggestion (modulo the attribute name). I actually find this approach quite reasonable, probably that's why my mind stuck to it too much. ;)
But you are basically saying to add the nested CTA_TUPLE_ZONE container here, that is part of a nested CTA_TUPLE_ORIG and/or CTA_TUPLE_REPLY attribute ...
enum ctattr_tuple {
CTA_TUPLE_UNSPEC,
CTA_TUPLE_IP,
CTA_TUPLE_PROTO,
CTA_TUPLE_ZONE, <---
__CTA_TUPLE_MAX
};... where CTA_TUPLE_ZONE would be a container for further attributes, say CTA_TUPLE_ZONE_ID, which is then the actual NLA_U16 zone id, right?
So, we'd have a zone id spread in 3 possible places, and additional (future) meta data spread around in 2 possible places, hmm ... Okay, lets say, we'd add future attribute X and Y to zones. Now, if I want a zone only in ORIG dir or only in REPLY dir, that works fine from ctnetlink perspective, even with your idea that there could be two different non-default zones entirely.
But, lets say I just want to use a traditional zones config (as in: nowadays) and have my tuple for /one/ particular zone id that is the same in /both/ directions. That would mean I have to duplicate my parameters X and Y across CTA_TUPLE_ORIG and CTA_TUPLE_REPLY, right? Or, we'd add a third attribute set (as in: CTA_ZONE_INFO) only for the single zone in both directions?
So far I find the current approach a bit cleaner to be honest (I can, of course, still change the CTA_TUPLE_ZONE back into CTA_ZONE_INFO name) ... but when the time comes where someone really should need two /non-default/ zones for a single tuple, don't we need a global setting as in this patch here anyway (due to reasons above)? (I'm fine either way, I'm just asking on how we want to handle this in an ideal/clean way.)
...
Quoted 7 lines
I'd suggest the output shows the zone on the corresponding tuple, eg. in case it only applies to the original tuple:
udp 17 29 src=192.168.2.195 dst=192.168.2.1 sport=40446 dport=53 zone=1 \ src=192.168.2.1 dst=192.168.2.195 sport=53 dport=40446 [ASSURED] mark=0 use=1We have a more compact output IMO.
Okay, that's fine by me. It would mean we'd see zone=1 twice in case a direction was not specified (thus, both directions apply), but I think that should be totally okay for the stand-alone interface (and in future conntrack -L).
Quoted 4 lines
Please, don't forget that you also have to update
libnetfilter_conntrackand conntrack to get this feature available from there. I'll take this patchset to the kernel so you have the time to update the userspace side later on without blocking this further.
Thanks, yes, after Plumbers I'll add proper support for both.
For testing that the netlink interface works, I had a local hack, but will get properly ready after the kernel and iptables patches. Was planning to do this anyway.
Thanks again,
Daniel
Pablo Neira Ayuso
maintainerRe: [PATCH v4 2/3] netfilter: nf_conntrack: add direction support for zones
On Wed, Aug 12, 2015 at 10:05:11PM +0200, Daniel Borkmann wrote:
[...]
Quoted 10 lines
But you are basically saying to add the nested
CTA_TUPLE_ZONEcontainer here, that is part of a nestedCTA_TUPLE_ORIGand/orCTA_TUPLE_REPLYattribute ...enum ctattr_tuple { CTA_TUPLE_UNSPEC, CTA_TUPLE_IP, CTA_TUPLE_PROTO, CTA_TUPLE_ZONE, <--- __CTA_TUPLE_MAX };
Right.
Quoted 2 lines
... where
CTA_TUPLE_ZONEwould be a container for further attributes, sayCTA_TUPLE_ZONE_ID, which is then the actualNLA_U16zone id, right?
Question is if we really need a nested attribute or not here, we've been discussing this before but future requirements are not clear. I think it would be good to keep those in mind to enhance this the right way.
So, going back to this, I think the idea is to add new commands to ctnetlink to create zone objects with specific settings at some point, so we get three new enum cntl_msg_types.
IPCTNL_MSG_CT_NEW_ZONE
IPCTNL_MSG_CT_GET_ZONE
IPCTNL_MSG_CT_DEL_ZONE
These new messages allow us to create/delete/retrieve custom zones with specific settings, each zone can be represented by:
enum ctattr_zone {
CTA_ZONE_UNSPEC,
CTA_ZONE_ID,
CTA_ZONE_CONFIG,
__CTA_ZONE_MAX
};The CTA_ZONE_CONFIG is a nested attribute with specific configuration for this zone, eg. the maximum number of connections.
The custom zone can be used from the CT target, so we not only set a zone ID to the conntrack but we also can attach configurations.
There's a problem though: By the time -j CT --zone X is loaded, the zone ID may not exists, so we need a new --zone-template X to explicitly refer to zones that are created via ctnetlink.
Quoted 5 lines
So, we'd have a zone id spread in 3 possible places, and additional (future) meta data spread around in 2 possible places, hmm ... Okay, lets say, we'd add future attribute X and Y to zones. Now, if I want a zone only in ORIG dir or only in REPLY dir, that works fine from ctnetlink perspective, even with your idea that there could be two different non-default zones entirely.
If we ever get connection limiting per zone as Thomas suggested during NFWS, I think we may well have two different non-default zones, what it comes to my mind is a simple scenario with two uplinks, each uplink becomes a zone with different connection limits.
Quoted 5 lines
But, lets say I just want to use a traditional zones config (as in: nowadays) and have my tuple for /one/ particular zone id that is the same in /both/ directions. That would mean I have to duplicate my parameters X and Y across
CTA_TUPLE_ORIGandCTA_TUPLE_REPLY, right? Or, we'd add a third attribute set (as in:CTA_ZONE_INFO) only for the single zone in both directions?
CTA_ZONE can still be used to set a zone for both directions, we can get rid of that. The new CTA_TUPLE_ZONE allows you to set the zone at per-tuple level. We only have to be careful on how to interpret if the user sends us two of them that have overlapping semantics.
Quoted 14 lines
So far I find the current approach a bit cleaner to be honest (I can, of course, still change the
CTA_TUPLE_ZONEback intoCTA_ZONE_INFOname) ... but when the time comes where someone really should need two /non-default/ zones for a single tuple, don't we need a global setting as in this patch here anyway (due to reasons above)? (I'm fine either way, I'm just asking on how we want to handle this in an ideal/clean way.)...
Quoted 6 lines
I'd suggest the output shows the zone on the corresponding tuple, eg. in case it only applies to the original tuple:
udp 17 29 src=192.168.2.195 dst=192.168.2.1 sport=40446 dport=53 zone=1 \ src=192.168.2.1 dst=192.168.2.195 sport=53 dport=40446 [ASSURED] mark=0 use=1
Quoted 6 lines
Quoted 1 line
We have a more compact output IMO.
Okay, that's fine by me. It would mean we'd see zone=1 twice in case a direction was not specified (thus, both directions apply), but I think that should be totally okay for the stand-alone interface (and in future conntrack -L).
I think we can leave it the same way it looks now when the zone applies to two directions, but looking at this output now this may look ambiguous when the zone is in the reply tuple, so I think we have to add different tags, ie.
1) When the zone is used from the original tuple:
udp 17 29 src=192.168.2.195 dst=192.168.2.1 sport=40446 dport=53 zone-orig=1 \
src=192.168.2.1 dst=192.168.2.195 sport=53 dport=40446 [ASSURED] mark=0 use=12) When used from the reply tuple:
udp 17 29 src=192.168.2.195 dst=192.168.2.1 sport=40446 dport=53 \
src=192.168.2.1 dst=192.168.2.195 sport=53 dport=40446 zone-reply=X [ASSURED] mark=0 use=13) When used in both (existing output):
udp 17 29 src=192.168.2.195 dst=192.168.2.1 sport=40446 dport=53 \
src=192.168.2.1 dst=192.168.2.195 sport=53 dport=40446 [ASSURED] mark=0 zone=1 use=1Quoted 10 lines
Quoted 4 lines
Please, don't forget that you also have to update
libnetfilter_conntrackand conntrack to get this feature available from there. I'll take this patchset to the kernel so you have the time to update the userspace side later on without blocking this further.Thanks, yes, after Plumbers I'll add proper support for both.
For testing that the netlink interface works, I had a local hack, but will get properly ready after the kernel and iptables patches. Was planning to do this anyway.
Thanks, the userspace chunks are also good to have, many people rely on the ctnetlink interface already and the userspace utilities to interact with it.
Re: [PATCH v4 2/3] netfilter: nf_conntrack: add direction support for zones
Quoted 44 lines · Pablo Neira Ayuso
On Wed, Aug 12, 2015 at 10:05:11PM +0200, Daniel Borkmann wrote:
[...]Quoted 10 lines
But you are basically saying to add the nested
CTA_TUPLE_ZONEcontainer here, that is part of a nestedCTA_TUPLE_ORIGand/orCTA_TUPLE_REPLYattribute ...enum ctattr_tuple { CTA_TUPLE_UNSPEC, CTA_TUPLE_IP, CTA_TUPLE_PROTO, CTA_TUPLE_ZONE, <--- __CTA_TUPLE_MAX };Right.
Quoted 2 lines
... where
CTA_TUPLE_ZONEwould be a container for further attributes, sayCTA_TUPLE_ZONE_ID, which is then the actualNLA_U16zone id, right?Question is if we really need a nested attribute or not here, we've been discussing this before but future requirements are not clear. I think it would be good to keep those in mind to enhance this the right way.
So, going back to this, I think the idea is to add new commands to ctnetlink to create zone objects with specific settings at some point, so we get three new enum
cntl_msg_types.
IPCTNL_MSG_CT_NEW_ZONE
IPCTNL_MSG_CT_GET_ZONE
IPCTNL_MSG_CT_DEL_ZONEThese new messages allow us to create/delete/retrieve custom zones with specific settings, each zone can be represented by:
enum ctattr_zone { CTA_ZONE_UNSPEC, CTA_ZONE_ID, CTA_ZONE_CONFIG, __CTA_ZONE_MAX };The
CTA_ZONE_CONFIGis a nested attribute with specific configuration for this zone, eg. the maximum number of connections.
Quoted 6 lines
The custom zone can be used from the CT target, so we not only set a zone ID to the conntrack but we also can attach configurations.
There's a problem though: By the time -j CT --zone X is loaded, the zone ID may not exists, so we need a new --zone-template X to explicitly refer to zones that are created via ctnetlink.
Yes, right. So the above makes sense to me. You would create zones with a specific configuration attached over a separate interface.
And then, you could use the CTA_ZONE or CTA_TUPLE_ZONE (both NLA_U16 attrs that represent the zone id) to look up a specific zone to be used. As a result, that would mean, it might be best to have CTA_TUPLE_ZONE as NLA_U16. Seems a clean way forward to me.
Quoted 17 lines
Quoted 5 lines
So, we'd have a zone id spread in 3 possible places, and additional (future) meta data spread around in 2 possible places, hmm ... Okay, lets say, we'd add future attribute X and Y to zones. Now, if I want a zone only in ORIG dir or only in REPLY dir, that works fine from ctnetlink perspective, even with your idea that there could be two different non-default zones entirely.
If we ever get connection limiting per zone as Thomas suggested during NFWS, I think we may well have two different non-default zones, what it comes to my mind is a simple scenario with two uplinks, each uplink becomes a zone with different connection limits.
Quoted 5 lines
But, lets say I just want to use a traditional zones config (as in: nowadays) and have my tuple for /one/ particular zone id that is the same in /both/ directions. That would mean I have to duplicate my parameters X and Y across
CTA_TUPLE_ORIGandCTA_TUPLE_REPLY, right? Or, we'd add a third attribute set (as in:CTA_ZONE_INFO) only for the single zone in both directions?
Quoted 4 lines
CTA_ZONEcan still be used to set a zone for both directions, we can get rid of that. The newCTA_TUPLE_ZONEallows you to set the zone at per-tuple level. We only have to be careful on how to interpret if the user sends us two of them that have overlapping semantics.
Right, we'd need to reject conflicting configurations, of course.
Quoted 33 lines
Quoted 14 lines
So far I find the current approach a bit cleaner to be honest (I can, of course, still change the
CTA_TUPLE_ZONEback intoCTA_ZONE_INFOname) ... but when the time comes where someone really should need two /non-default/ zones for a single tuple, don't we need a global setting as in this patch here anyway (due to reasons above)? (I'm fine either way, I'm just asking on how we want to handle this in an ideal/clean way.)...
Quoted 6 lines
I'd suggest the output shows the zone on the corresponding tuple, eg. in case it only applies to the original tuple:
udp 17 29 src=192.168.2.195 dst=192.168.2.1 sport=40446 dport=53 zone=1 \ src=192.168.2.1 dst=192.168.2.195 sport=53 dport=40446 [ASSURED] mark=0 use=1Quoted 6 lines
Quoted 1 line
We have a more compact output IMO.
Okay, that's fine by me. It would mean we'd see zone=1 twice in case a direction was not specified (thus, both directions apply), but I think that should be totally okay for the stand-alone interface (and in future conntrack -L).
I think we can leave it the same way it looks now when the zone applies to two directions, but looking at this output now this may look ambiguous when the zone is in the reply tuple, so I think we have to add different tags, ie.
1) When the zone is used from the original tuple:
udp 17 29 src=192.168.2.195 dst=192.168.2.1 sport=40446 dport=53 zone-orig=1 \ src=192.168.2.1 dst=192.168.2.195 sport=53 dport=40446 [ASSURED] mark=0 use=12) When used from the reply tuple:
Quoted 7 lines
udp 17 29 src=192.168.2.195 dst=192.168.2.1 sport=40446 dport=53 \ src=192.168.2.1 dst=192.168.2.195 sport=53 dport=40446 zone-reply=X [ASSURED] mark=0 use=13) When used in both (existing output):
udp 17 29 src=192.168.2.195 dst=192.168.2.1 sport=40446 dport=53 \ src=192.168.2.1 dst=192.168.2.195 sport=53 dport=40446 [ASSURED] mark=0 zone=1 use=1
Agreed, sounds good. Will change it into this representation.
Thanks Pablo!
Best,
Daniel
PATCH v4 3/3 netfilter: nf_conntrack: add efficient mark to zone mapping
In reply to Daniel BorkmannThis work adds the possibility of deriving the zone id from the skb->mark field in a scalable manner. This allows for having only a single template serving hundreds/thousands of different zones, for example, instead of the need to have one match for each zone as an extra CT jump target.
Note that we'd need to have this information attached to the template as at the time when we're trying to lookup a possible ct object, we already need to know zone information for a possible match when going into __nf_conntrack_find_get(). This work provides a minimal implementation for a possible mapping.
In order to not add/expose an extra ct->status bit, the zone structure has been extended to carry a flag for deriving the mark.
--- include/net/netfilter/nf_conntrack_zones.h | 45 +++++++++++++++++++++-- include/uapi/linux/netfilter/xt_CT.h | 4 ++- net/ipv4/netfilter/nf_conntrack_proto_icmp.c | 3 +- net/ipv6/netfilter/nf_conntrack_proto_icmpv6.c | 4 ++- net/netfilter/nf_conntrack_core.c | 50 +++++++++----------------- net/netfilter/nf_conntrack_netlink.c | 4 +-- net/netfilter/xt_CT.c | 5 ++- 7 files changed, 72 insertions(+), 43 deletions(-)
include/net/netfilter/nf_conntrack_zones.h +42 -3
@@ -10,9 +10,12 @@#define NF_CT_DEFAULT_ZONE_DIR (NF_CT_ZONE_DIR_ORIG | NF_CT_ZONE_DIR_REPL) +#define NF_CT_FLAG_MARK 1 +struct nf_conntrack_zone {u16 id; -u16 dir; +u8 flags; +u8 dir; };extern const struct nf_conntrack_zone nf_ct_zone_dflt;
@@ -32,9 +35,45 @@ nf_ct_zone(const struct nf_conn *ct) }static inline const struct nf_conntrack_zone * -nf_ct_zone_tmpl (const struct nf_conn *tmpl) +nf_ct_zone_init (struct nf_conntrack_zone *zone,u16 id,u8 dir,u8 flags) +{ + zone->id = id; + zone->flags = flags; + zone->dir = dir; + +return zone; +} + +static inline const struct nf_conntrack_zone * +nf_ct_zone_tmpl (const struct nf_conn *tmpl,const struct sk_buff *skb, +struct nf_conntrack_zone *tmp) +{ +const struct nf_conntrack_zone *zone; + +if (!tmpl) +return &nf_ct_zone_dflt; + + zone =nf_ct_zone (tmpl); +if (zone->flags &NF_CT_FLAG_MARK ) + zone =nf_ct_zone_init (tmp, skb->mark, zone->dir,0 ); + +return zone; +} + +static inline int nf_ct_zone_add (struct nf_conn *ct,gfp_t flags, +const struct nf_conntrack_zone *info) { -return tmpl ?nf_ct_zone (tmpl) : &nf_ct_zone_dflt; +#ifdef CONFIG_NF_CONNTRACK_ZONES +struct nf_conntrack_zone *nf_ct_zone; + + nf_ct_zone =nf_ct_ext_add (ct,NF_CT_EXT_ZONE , flags); +if (!nf_ct_zone) +return -ENOMEM ; + +nf_ct_zone_init (nf_ct_zone, info->id, info->dir, + info->flags); +#endif +return 0 ; } static inline bool nf_ct_zone_matches_dir(const struct nf_conntrack_zone *zone,
include/uapi/linux/netfilter/xt_CT.h +3 -1
@@ -8,9 +8,11 @@ enum {XT_CT_NOTRACK_ALIAS =1 <<1 ,XT_CT_ZONE_DIR_ORIG =1 <<2 ,XT_CT_ZONE_DIR_REPL =1 <<3 , +XT_CT_ZONE_MARK =1 <<4 ,XT_CT_MASK =XT_CT_NOTRACK |XT_CT_NOTRACK_ALIAS | - XT_CT_ZONE_DIR_ORIG | XT_CT_ZONE_DIR_REPL, +XT_CT_ZONE_DIR_ORIG |XT_CT_ZONE_DIR_REPL | + XT_CT_ZONE_MARK, }; struct xt_ct_target_info {
net/ipv4/netfilter/nf_conntrack_proto_icmp.c +2 -1
@@ -135,9 +135,10 @@ icmp_error_message(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,const struct nf_conntrack_l4proto *innerproto;const struct nf_conntrack_tuple_hash *h;const struct nf_conntrack_zone *zone; +struct nf_conntrack_zone tmp;NF_CT_ASSERT (skb->nfct ==NULL ); - zone =nf_ct_zone_tmpl (tmpl); + zone =nf_ct_zone_tmpl (tmpl, skb, &tmp);/* Are they talking about one of our connections? */ if (!nf_ct_get_tuplepr(skb,
net/ipv6/netfilter/nf_conntrack_proto_icmpv6.c +3 -1
@@ -150,6 +150,7 @@ icmpv6_error_message(struct net *net, struct nf_conn *tmpl,struct nf_conntrack_tuple intuple, origtuple;const struct nf_conntrack_tuple_hash *h;const struct nf_conntrack_l4proto *inproto; +struct nf_conntrack_zone tmp;NF_CT_ASSERT (skb->nfct ==NULL );
@@ -176,7 +177,8 @@ icmpv6_error_message(struct net *net, struct nf_conn *tmpl, *ctinfo =IP_CT_RELATED ; - h =nf_conntrack_find_get (net,nf_ct_zone_tmpl (tmpl), &intuple); + h =nf_conntrack_find_get (net,nf_ct_zone_tmpl (tmpl, skb, &tmp), + &intuple);if (!h) {pr_debug ("icmpv6_error: no match\n" );return -NF_ACCEPT ;
net/netfilter/nf_conntrack_core.c +16 -34
106 lines not on this page · raw message
net/netfilter/nf_conntrack_netlink.c +2 -2
@@ -1050,8 +1050,8 @@ ctnetlink_parse_zone(const struct nlattr *attr_zone,const struct nlattr *attr_tuple_zone, struct nf_conntrack_zone *zone) { - zone->id = NF_CT_DEFAULT_ZONE_ID; - zone->dir =NF_CT_DEFAULT_ZONE_DIR ; +nf_ct_zone_init (zone ,NF_CT_DEFAULT_ZONE_ID , + NF_CT_DEFAULT_ZONE_DIR, 0);#ifdef CONFIG_NF_CONNTRACK_ZONES if (attr_zone)
net/netfilter/xt_CT.c +4 -1
@@ -208,7 +208,8 @@ static int xt_ct_tg_check(const struct xt_tgchk_param *par,#ifndef CONFIG_NF_CONNTRACK_ZONES if (info->zone || info->flags & (XT_CT_ZONE_DIR_ORIG | -XT_CT_ZONE_DIR_REPL )) +XT_CT_ZONE_DIR_REPL | +XT_CT_ZONE_MARK )) goto err1;#endif
@@ -219,6 +220,8 @@ static int xt_ct_tg_check(const struct xt_tgchk_param *par,memset (&zone,0 ,sizeof (zone)); zone.id = info->zone; zone.dir =xt_ct_flags_to_dir (info); +if (info->flags &XT_CT_ZONE_MARK ) + zone.flags |=NF_CT_FLAG_MARK ; ct =nf_ct_tmpl_alloc (par->net, &zone,GFP_KERNEL ); ret =PTR_ERR (ct);
Signature · 1 line
1.9.3