netfilter: nf_nat: Support fullcone NAT
Subsystems (2)
PATCH 1/1 netfilter: nf_nat: Support fullcone NAT
From: Kiran Kella <[email protected]>
Changes done in the kernel to ensure 3-tuple uniqueness of the conntrack entries for the fullcone nat functionality.
* Hashlist is maintained for the 3-tuple unique keys (Protocol/Source
IP/Port) for all the conntrack entries.
* When NAT table rules are created with the fullcone option, the
SNAT/POSTROUTING stage ensures the ports from the pool are picked up in such a way that the 3-tuple is uniquely assigned.
* In the DNAT/POSTROUTING stage, the fullcone behavior is ensured by checking
and reusing the 3-tuple for the Source IP/Port in the original direction.
* When the pool is exhausted of the 3-tuple assignments, the packets are
dropped, else, they will be going out of the router they being 5-tuple unique (which is not intended).
* Passing fullcone option using iptables is part of another PR (in
sonic-buildimage repo).
The kernel changes mentioned above are done to counter the challenges explained in the section *3.4.2.1 Handling NAT model mismatch between the ASIC and the Kernel* in the NAT HLD [1].
[1]: https://github.com/kirankella/SONiC/blob/nat_doc_changes/doc/nat/nat_design_spec.md
---
Dear Linux folks,
This is taken from switch network operating system (NOS) SONiC’s Linux repository, where the support was added in September 2019 [1], and forwarded ported to Linux 4.19 by Akhilesh in June 2020 [2].
I am sending it upstream as a request for comments, before effort is put into forward porting it to Linux master.
Kind regards,
Paul
[1]: https://github.com/Azure/sonic-linux-kernel/pull/100
[2]: https://github.com/Azure/sonic-linux-kernel/pull/147
include/net/netfilter/nf_conntrack.h | 3 + include/net/netfilter/nf_nat.h | 6 + include/net/netfilter/nf_nat_l4proto.h | 12 +- include/uapi/linux/netfilter/nf_nat.h | 1 + net/ipv4/netfilter/nf_nat_proto_gre.c | 8 +- net/ipv4/netfilter/nf_nat_proto_icmp.c | 6 +- net/ipv6/netfilter/nf_nat_proto_icmpv6.c | 5 +- net/netfilter/nf_nat_core.c | 173 ++++++++++++++++++++--- net/netfilter/nf_nat_proto_common.c | 32 +++-- net/netfilter/nf_nat_proto_dccp.c | 6 +- net/netfilter/nf_nat_proto_sctp.c | 6 +- net/netfilter/nf_nat_proto_tcp.c | 6 +- net/netfilter/nf_nat_proto_udp.c | 12 +- net/netfilter/nf_nat_proto_unknown.c | 4 +- 14 files changed, 220 insertions(+), 60 deletions(-)
include/net/netfilter/nf_conntrack.h +3 -0
@@ -84,6 +84,9 @@ struct nf_conn {#if IS_ENABLED (CONFIG_NF_NAT )struct hlist_node nat_bysource;#endif +/* To optionally ensure 3-tuple uniqueness on the translated source */ +struct hlist_node nat_by_manip_src; +/* all members below initialized via memset */ u8 __nfct_init_offset[0 ];
include/net/netfilter/nf_nat.h +6 -0
@@ -51,6 +51,12 @@ struct nf_conn_nat *nf_ct_nat_ext_add(struct nf_conn *ct);int nf_nat_used_tuple (const struct nf_conntrack_tuple *tuple,const struct nf_conn *ignored_conntrack); +/* Is this 3-tuple already taken? (not by us)*/ +int +nf_nat_used_3_tuple (const struct nf_conntrack_tuple *tuple, +const struct nf_conn *ignored_conntrack, +enum nf_nat_manip_type maniptype); +static inline struct nf_conn_nat *nfct_nat (const struct nf_conn *ct) {#if defined(CONFIG_NF_NAT ) || defined(CONFIG_NF_NAT_MODULE )
include/net/netfilter/nf_nat_l4proto.h +6 -6
@@ -32,7 +32,7 @@ struct nf_nat_l4proto {* possible. Per-protocol part of tuple is initialized to the * incoming packet. */ - void (*unique_tuple)(const struct nf_nat_l3proto *l3proto, + int (*unique_tuple)(const struct nf_nat_l3proto *l3proto, struct nf_conntrack_tuple *tuple, const struct nf_nat_range2 *range, enum nf_nat_manip_type maniptype,
@@ -70,11 +70,11 @@ bool nf_nat_l4proto_in_range(const struct nf_conntrack_tuple *tuple,const union nf_conntrack_man_proto *min, const union nf_conntrack_man_proto *max); -void nf_nat_l4proto_unique_tuple (const struct nf_nat_l3proto *l3proto, -struct nf_conntrack_tuple *tuple, -const struct nf_nat_range2 *range, -enum nf_nat_manip_type maniptype, -const struct nf_conn *ct,u16 *rover); +int nf_nat_l4proto_unique_tuple (const struct nf_nat_l3proto *l3proto, +struct nf_conntrack_tuple *tuple, +const struct nf_nat_range2 *range, +enum nf_nat_manip_type maniptype, +const struct nf_conn *ct,u16 *rover);int nf_nat_l4proto_nlattr_to_range (struct nlattr *tb[],struct nf_nat_range2 *range);
include/uapi/linux/netfilter/nf_nat.h +1 -0
@@ -11,6 +11,7 @@#define NF_NAT_RANGE_PERSISTENT (1 << 3)#define NF_NAT_RANGE_PROTO_RANDOM_FULLY (1 << 4)#define NF_NAT_RANGE_PROTO_OFFSET (1 << 5) +#define NF_NAT_RANGE_FULLCONE (1 << 6)#define NF_NAT_RANGE_PROTO_RANDOM_ALL \ (NF_NAT_RANGE_PROTO_RANDOM | NF_NAT_RANGE_PROTO_RANDOM_FULLY)
net/ipv4/netfilter/nf_nat_proto_gre.c +4 -4
@@ -38,7 +38,7 @@ MODULE_AUTHOR("Harald Welte <[email protected]>");MODULE_DESCRIPTION ("Netfilter NAT protocol helper module for GRE" );/* generate unique tuple ... */ -static void +static int gre_unique_tuple(const struct nf_nat_l3proto *l3proto, struct nf_conntrack_tuple *tuple, const struct nf_nat_range2 *range,
@@ -52,7 +52,7 @@ gre_unique_tuple(const struct nf_nat_l3proto *l3proto,/* If there is no master conntrack we are not PPTP, do not change tuples */ if (!ct->master) -return ; +return 0 ;if (maniptype ==NF_NAT_MANIP_SRC ) keyptr = &tuple->src.u.gre.key;
@@ -73,11 +73,11 @@ gre_unique_tuple(const struct nf_nat_l3proto *l3proto,for (i =0 ; ; ++key) { *keyptr =htons (min + key % range_size);if (++i == range_size || !nf_nat_used_tuple (tuple, ct)) -return ; +return 1 ; }pr_debug ("%p: no NAT mapping\n" , ct); -return ; +return 0 ; }/* manipulate a GRE packet according to maniptype */
net/ipv4/netfilter/nf_nat_proto_icmp.c +3 -3
@@ -27,7 +27,7 @@ icmp_in_range(const struct nf_conntrack_tuple *tuple,ntohs (tuple->src.u.icmp.id) <=ntohs (max->icmp.id); } -static void +static int icmp_unique_tuple(const struct nf_nat_l3proto *l3proto, struct nf_conntrack_tuple *tuple, const struct nf_nat_range2 *range,
@@ -48,9 +48,9 @@ icmp_unique_tuple(const struct nf_nat_l3proto *l3proto, tuple->src.u.icmp.id =htons (ntohs (range->min_proto.icmp.id) + (id % range_size));if (++i == range_size || !nf_nat_used_tuple (tuple, ct)) -return ; +return 1 ; } -return ; +return 0 ; } static bool
net/ipv6/netfilter/nf_nat_proto_icmpv6.c +3 -2
@@ -29,7 +29,7 @@ icmpv6_in_range(const struct nf_conntrack_tuple *tuple,ntohs (tuple->src.u.icmp.id) <=ntohs (max->icmp.id); } -static void +static int icmpv6_unique_tuple(const struct nf_nat_l3proto *l3proto, struct nf_conntrack_tuple *tuple, const struct nf_nat_range2 *range,
@@ -50,8 +50,9 @@ icmpv6_unique_tuple(const struct nf_nat_l3proto *l3proto, tuple->src.u.icmp.id =htons (ntohs (range->min_proto.icmp.id) + (id % range_size));if (++i == range_size || !nf_nat_used_tuple (tuple, ct)) -return ; +return 1 ; } +return 0 ; } static bool
net/netfilter/nf_nat_core.c +156 -17
287 lines not on this page · raw message
net/netfilter/nf_nat_proto_common.c +21 -11
@@ -34,12 +34,12 @@ bool nf_nat_l4proto_in_range(const struct nf_conntrack_tuple *tuple, }EXPORT_SYMBOL_GPL (nf_nat_l4proto_in_range); -void nf_nat_l4proto_unique_tuple (const struct nf_nat_l3proto *l3proto, -struct nf_conntrack_tuple *tuple, -const struct nf_nat_range2 *range, -enum nf_nat_manip_type maniptype, -const struct nf_conn *ct, -u16 *rover) +int nf_nat_l4proto_unique_tuple (const struct nf_nat_l3proto *l3proto, +struct nf_conntrack_tuple *tuple, +const struct nf_nat_range2 *range, +enum nf_nat_manip_type maniptype, +const struct nf_conn *ct, +u16 *rover) {unsigned int range_size, min, max, i;__be16 *portptr;
@@ -54,7 +54,7 @@ void nf_nat_l4proto_unique_tuple(const struct nf_nat_l3proto *l3proto,if (!(range->flags &NF_NAT_RANGE_PROTO_SPECIFIED )) {/* If it's dst rewrite, can't change port */ if (maniptype ==NF_NAT_MANIP_DST ) -return ; +return 0 ;if (ntohs (*portptr) <1024 ) {/* Loose convention: >> 512 is credential passing */
@@ -87,17 +87,27 @@ void nf_nat_l4proto_unique_tuple(const struct nf_nat_l3proto *l3proto, off = (ntohs (*portptr) -ntohs (range->base_proto.all)); }else { off = *rover; +if ((range->flags &NF_NAT_RANGE_FULLCONE ) && (maniptype ==NF_NAT_MANIP_SRC )) { +/* Try from the next L4 port in the range */ + off++; + } } -for (i =0 ; ; ++off) { +for (i =0 ; (i != range_size); ++i, ++off) { *portptr =htons (min + off % range_size); -if (++i != range_size &&nf_nat_used_tuple (tuple, ct)) -continue ; +if ((range->flags &NF_NAT_RANGE_FULLCONE ) && (maniptype ==NF_NAT_MANIP_SRC )) { +if (nf_nat_used_3_tuple (tuple, ct, maniptype)) +continue ; + }else { +if (nf_nat_used_tuple (tuple, ct)) +continue ; + }if (!(range->flags & (NF_NAT_RANGE_PROTO_RANDOM_ALL |NF_NAT_RANGE_PROTO_OFFSET ))) *rover = off; -return ; +return 1 ; } +return 0 ; }EXPORT_SYMBOL_GPL (nf_nat_l4proto_unique_tuple);
net/netfilter/nf_nat_proto_dccp.c +3 -3
@@ -20,15 +20,15 @@static u_int16_t dccp_port_rover; -static void +static int dccp_unique_tuple (const struct nf_nat_l3proto *l3proto,struct nf_conntrack_tuple *tuple,const struct nf_nat_range2 *range,enum nf_nat_manip_type maniptype,const struct nf_conn *ct) { -nf_nat_l4proto_unique_tuple (l3proto, tuple, range, maniptype, ct, - &dccp_port_rover); +return nf_nat_l4proto_unique_tuple (l3proto, tuple, range, maniptype, ct, + &dccp_port_rover); } static bool
net/netfilter/nf_nat_proto_sctp.c +3 -3
@@ -14,15 +14,15 @@static u_int16_t nf_sctp_port_rover; -static void +static int sctp_unique_tuple (const struct nf_nat_l3proto *l3proto,struct nf_conntrack_tuple *tuple,const struct nf_nat_range2 *range,enum nf_nat_manip_type maniptype,const struct nf_conn *ct) { -nf_nat_l4proto_unique_tuple (l3proto, tuple, range, maniptype, ct, - &nf_sctp_port_rover); +return nf_nat_l4proto_unique_tuple (l3proto, tuple, range, maniptype, ct, + &nf_sctp_port_rover); } static bool
net/netfilter/nf_nat_proto_tcp.c +3 -3
@@ -20,15 +20,15 @@static u16 tcp_port_rover; -static void +static int tcp_unique_tuple (const struct nf_nat_l3proto *l3proto,struct nf_conntrack_tuple *tuple,const struct nf_nat_range2 *range,enum nf_nat_manip_type maniptype,const struct nf_conn *ct) { -nf_nat_l4proto_unique_tuple (l3proto, tuple, range, maniptype, ct, - &tcp_port_rover); +return nf_nat_l4proto_unique_tuple (l3proto, tuple, range, maniptype, ct, + &tcp_port_rover); } static bool
net/netfilter/nf_nat_proto_udp.c +6 -6
@@ -19,15 +19,15 @@static u16 udp_port_rover; -static void +static int udp_unique_tuple (const struct nf_nat_l3proto *l3proto,struct nf_conntrack_tuple *tuple,const struct nf_nat_range2 *range,enum nf_nat_manip_type maniptype,const struct nf_conn *ct) { -nf_nat_l4proto_unique_tuple (l3proto, tuple, range, maniptype, ct, - &udp_port_rover); +return nf_nat_l4proto_unique_tuple (l3proto, tuple, range, maniptype, ct, + &udp_port_rover); } static void
@@ -97,15 +97,15 @@ static bool udplite_manip_pkt(struct sk_buff *skb,return true; } -static void +static int udplite_unique_tuple (const struct nf_nat_l3proto *l3proto,struct nf_conntrack_tuple *tuple,const struct nf_nat_range2 *range,enum nf_nat_manip_type maniptype,const struct nf_conn *ct) { -nf_nat_l4proto_unique_tuple (l3proto, tuple, range, maniptype, ct, - &udplite_port_rover); +return nf_nat_l4proto_unique_tuple (l3proto, tuple, range, maniptype, ct, + &udplite_port_rover); }const struct nf_nat_l4proto nf_nat_l4proto_udplite = {
net/netfilter/nf_nat_proto_unknown.c +2 -2
@@ -25,7 +25,7 @@ static bool unknown_in_range(const struct nf_conntrack_tuple *tuple,return true; } -static void unknown_unique_tuple(const struct nf_nat_l3proto *l3proto, +static int unknown_unique_tuple(const struct nf_nat_l3proto *l3proto, struct nf_conntrack_tuple *tuple, const struct nf_nat_range2 *range, enum nf_nat_manip_type maniptype,
@@ -34,7 +34,7 @@ static void unknown_unique_tuple(const struct nf_nat_l3proto *l3proto,/* Sorry: we can't help you; if it's not unique, we can't frob * anything. */ -return ; +return 0 ; } static bool
Florian Westphal
maintainerQuoted 15 lines · Paul Menzel
From: Kiran Kella <[email protected]>
Changes done in the kernel to ensure 3-tuple uniqueness of the conntrack entries for the fullcone nat functionality.
* Hashlist is maintained for the 3-tuple unique keys (Protocol/Source
IP/Port) for all the conntrack entries.* When NAT table rules are created with the fullcone option, the
SNAT/POSTROUTING stage ensures the ports from the pool are picked up in such a way that the 3-tuple is uniquely assigned.* In the DNAT/POSTROUTING stage, the fullcone behavior is ensured by checking
and reusing the 3-tuple for the Source IP/Port in the original direction.
Quoted 6 lines
* When the pool is exhausted of the 3-tuple assignments, the packets are
dropped, else, they will be going out of the router they being 5-tuple unique (which is not intended).* Passing fullcone option using iptables is part of another PR (in
sonic-buildimage repo).
These are way too many changes for a single patch. Please consider splitting this ino multiple chunks, e.g. at least separate functional fullcone from the boilerplate changes.
Quoted 3 lines
The kernel changes mentioned above are done to counter the challenges explained in the section *3.4.2.1 Handling NAT model mismatch between the ASIC and the Kernel* in the NAT HLD [1].
And please add the relevant explanations from this
Quoted 1 line
[1]: https://github.com/kirankella/SONiC/blob/nat_doc_changes/doc/nat/nat_design_spec.md
... to the commit message.
Quoted 6 lines
This is taken from switch network operating system (NOS) SONiC’s Linux repository, where the support was added in September 2019 [1], and forwarded ported to Linux 4.19 by Akhilesh in June 2020 [2].
I am sending it upstream as a request for comments, before effort is put into forward porting it to Linux master.
I don't see any huge problems from a technical pov. But I don't see why this functionality is needed from a pure SW point of view.
AFAICS SONiC uses a propritary (or at least, custom) offload mechanism to place nat entries into HW.
Netfilter already has a forwarding offload mechanism, described in Documentation/networking/nf_flowtable.rst , so I'm not sure it makes sense to accept this without patches to support the needed offload support as well.
AFAIU passing fullcone makes no sense unless using offload HW that doesn't support the current nat port allocation scheme.
And current kernel doesn't support any such HW.
Nevertheless, some comments below.
include/net/netfilter/nf_nat.h struct nf_conn_nat *nf_ct_nat_ext_add(struct nf_conn *ct);
Quoted 12 lines
include/net/netfilter/nf_nat.h +7
-1+/* Is this 3-tuple already taken? (not by us)*/ +int +nf_nat_used_3_tuple (const struct nf_conntrack_tuple *tuple, +const struct nf_conn *ignored_conntrack, +enum nf_nat_manip_type maniptype); + */ - void (*unique_tuple)(const struct nf_nat_l3proto *l3proto, + int (*unique_tuple)(const struct nf_nat_l3proto *l3proto, struct nf_conntrack_tuple *tuple, const struct nf_nat_range2 *range, enum nf_nat_manip_type maniptype,
The above change should be done in a separate patch, so its in a isolated change. This will ease review of the fullcone part.
Quoted 16 lines
/* generate unique tuple ... */
net/ipv4/netfilter/nf_nat_proto_gre.c +2
-2-static void +static int gre_unique_tuple(const struct nf_nat_l3proto *l3proto, struct nf_conntrack_tuple *tuple, const struct nf_nat_range2 *range,@@ -52,7 +52,7 @@ gre_unique_tuple(const struct nf_nat_l3proto *l3proto,/* If there is no master conntrack we are not PPTP, do not change tuples */ if (!ct->master) -return ; +return 0 ;if (maniptype ==NF_NAT_MANIP_SRC ) keyptr = &tuple->src.u.gre.key;
net/ipv4/netfilter/nf_nat_proto_gre.c gre_unique_tuple(const struct nf_nat_l3proto *l3proto, · line 77
Quoted 6 lines
net/ipv4/netfilter/nf_nat_proto_gre.c +1
-1@@ -73,11 +73,11 @@ gre_unique_tuple(const struct nf_nat_l3proto *l3proto,for (i =0 ; ; ++key) { *keyptr =htons (min + key % range_size);if (++i == range_size || !nf_nat_used_tuple (tuple, ct)) -return ; +return 1 ; }
I suggest you use 'bool' type for this rather than int, unless you plan to use errno codes here in some future change.
net/netfilter/nf_nat_core.c hash_by_src(const struct net *n, const struct nf_conntrack_tuple *tuple) · line 160
Quoted 6 lines
net/netfilter/nf_nat_core.c +2
-0@@ -155,6 +156,31 @@ hash_by_src(const struct net *n, const struct nf_conntrack_tuple *tuple)return reciprocal_scale (hash, nf_nat_htable_size); } +static inline unsigned int +hash_by_dst (const struct net *n,const struct nf_conntrack_tuple *tuple)
please avoid inline keyword in .c files for new submissions.
net/netfilter/nf_nat_core.c hash_by_src(const struct net *n, const struct nf_conntrack_tuple *tuple) · line 164
Quoted 4 lines
net/netfilter/nf_nat_core.c +4
-0+{ +unsigned int hash; + +get_random_once (&nf_nat_hash_rnd,sizeof (nf_nat_hash_rnd));
get_random_once can't be called from multiple places for the same random value.
[ I did not check if thats the case since patch doesn't apply to current nf ].
net/netfilter/nf_nat_core.c hash_by_src(const struct net *n, const struct nf_conntrack_tuple *tuple) · line 182
Quoted 11 lines
net/netfilter/nf_nat_core.c +11
-0+static inline int +same_reply_dst (const struct nf_conn *ct, +const struct nf_conntrack_tuple *tuple) +{ +const struct nf_conntrack_tuple *t; + + t = &ct->tuplehash[IP_CT_DIR_REPLY ].tuple; +return (t->dst.protonum == tuple->dst.protonum && +nf_inet_addr_cmp (&t->dst.u3, &tuple->dst.u3) && + t->dst.u.all == tuple->dst.u.all); +}
Please run patches through scripts/checkpatch.pl before submission, we try to avoid '()' in return (...);
net/netfilter/nf_nat_core.c find_appropriate_src(struct net *net, · line 310
Quoted 12 lines
net/netfilter/nf_nat_core.c +12
-0+/* Only called for DST manip */ +static int +find_appropriate_dst (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, +struct nf_conntrack_tuple *result) +{ +struct nf_conntrack_tuple reply; +unsigned int h; +const struct nf_conn *ct;
Silly, but some maintainers prefer reverse-xmas-tree, i.e.
struct nf_conntrack_tuple reply;
const struct nf_conn *ct;
unsigned int h;
net/netfilter/nf_nat_core.c get_unique_tuple(struct nf_conntrack_tuple *tuple, · line 427
Quoted 8 lines
net/netfilter/nf_nat_core.c +2
-0@@ -327,8 +421,11 @@ get_unique_tuple(struct nf_conntrack_tuple *tuple,const struct nf_conntrack_zone *zone;const struct nf_nat_l3proto *l3proto;const struct nf_nat_l4proto *l4proto; +struct nf_nat_range2 nat_range;struct net *net =nf_ct_net (ct); +memcpy (&nat_range, range,sizeof (struct nf_nat_range2 ));
nat_range = *range ?
net/netfilter/nf_nat_core.c static int __init nf_nat_init(void) · line 1193
Quoted 7 lines
net/netfilter/nf_nat_core.c +3
-0@@ -1055,9 +1188,14 @@ static int __init nf_nat_init(void)if (!nf_nat_bysource)return -ENOMEM ; + nf_nat_by_manip_src =nf_ct_alloc_hashtable (&nf_nat_htable_size,0 ); +if (!nf_nat_by_manip_src) +return -ENOMEM ;
This lacks error unwind for nf_nat_bysource.