NAT updates for nf_tables.
Subsystems (2)
cover NAT updates for nf_tables.
The following series implements some updates for NAT in nf_tables.
First of all, I add a new flag attribute to allow clients of nft_nat to specify additional config flags. This enables implementing port randomization and persistence to be set from nft.
Two patches split the masquerade code from ip[6]t_MASQUERADE.c to generic modules, so we can use this NAT type from nft_nat.
Then, the nft_nat code is splitted in AF specific parts, so we avoid potential dependencies regarding AF specific symbols in the last patch.
The last patch finally implements masquerade for nft_nat.
Please, note that this series applies on top of a patch by Pablo:
netfilter: nft_nat: don't dump port information if unset
(http://patchwork.ozlabs.org/patch/361400/)
Comments are welcomed.
---
Arturo Borrero Gonzalez (5):
netfilter: nft_nat: include a flag attribute
netfilter: nf_nat_masquerade_ipv4: code factorization
netfilter: nf_nat_masquerade_ipv6: code factorization
netfilter: nft_nat: split code in AF parts
netfilter: nft_nat: add masquerade support
.../net/netfilter/ipv4/nf_nat_masquerade_ipv4.h | 14 ++ .../net/netfilter/ipv6/nf_nat_masquerade_ipv6.h | 10 + include/net/netfilter/nft_nat.h | 20 ++ include/uapi/linux/netfilter/nf_tables.h | 7 + net/ipv4/netfilter/Kconfig | 14 ++ net/ipv4/netfilter/Makefile | 2 net/ipv4/netfilter/ipt_MASQUERADE.c | 108 +------------ net/ipv4/netfilter/nf_nat_masquerade_ipv4.c | 155 +++++++++++++++++++ net/ipv4/netfilter/nft_nat_ipv4.c | 164 ++++++++++++++++++++ net/ipv6/netfilter/Kconfig | 14 ++ net/ipv6/netfilter/Makefile | 2 net/ipv6/netfilter/ip6t_MASQUERADE.c | 76 +-------- net/ipv6/netfilter/nf_nat_masquerade_ipv6.c | 121 ++++++++++++++ net/ipv6/netfilter/nft_nat_ipv6.c | 163 ++++++++++++++++++++ net/netfilter/nft_nat.c | 167 +++----------------- 15 files changed, 725 insertions(+), 312 deletions(-) create mode 100644 include/net/netfilter/ipv4/nf_nat_masquerade_ipv4.h create mode 100644 include/net/netfilter/ipv6/nf_nat_masquerade_ipv6.h create mode 100644 include/net/netfilter/nft_nat.h create mode 100644 net/ipv4/netfilter/nf_nat_masquerade_ipv4.c create mode 100644 net/ipv4/netfilter/nft_nat_ipv4.c create mode 100644 net/ipv6/netfilter/nf_nat_masquerade_ipv6.c create mode 100644 net/ipv6/netfilter/nft_nat_ipv6.c
Signature · 1 line
Arturo Borrero Gonzalez
PATCH 1/5 netfilter: nft_nat: include a flag attribute
Both SNAT and DNAT (and the upcoming masquerade) can have additional configuration parameters, such as port randomization or NAT addressing persistence.
We can cover these scenarios by simply adding a flag attribute for userspace to fill when needed.
The flags to use are defined in include/uapi/linux/netfilter/nf_nat.h,
NF_NAT_RANGE_MAP_IPS
NF_NAT_RANGE_PROTO_SPECIFIED
NF_NAT_RANGE_PROTO_RANDOM
NF_NAT_RANGE_PERSISTENT
NF_NAT_RANGE_PROTO_RANDOM_FULLY NF_NAT_RANGE_PROTO_RANDOM_ALL
The caller must take care of not messing up with the flags, as they are added unconditionally to the final resulting nf_nat_range.
--- include/uapi/linux/netfilter/nf_tables.h | 2 ++ net/netfilter/nft_nat.c | 13 +++++++++++++ 2 files changed, 15 insertions(+)
include/uapi/linux/netfilter/nf_tables.h +2 -0
@@ -773,6 +773,7 @@ enum nft_nat_types {* @NFTA_NAT_REG_ADDR_MAX: source register of address range end (NLA_U32: nft_registers) * @NFTA_NAT_REG_PROTO_MIN: source register of proto range start (NLA_U32: nft_registers) * @NFTA_NAT_REG_PROTO_MAX: source register of proto range end (NLA_U32: nft_registers) +* @NFTA_NAT_FLAGS: additional NAT configuration (NF_NAT_RANGE_*) (NLA_U32) */ enum nft_nat_attributes {NFTA_NAT_UNSPEC ,
@@ -782,6 +783,7 @@ enum nft_nat_attributes {NFTA_NAT_REG_ADDR_MAX ,NFTA_NAT_REG_PROTO_MIN ,NFTA_NAT_REG_PROTO_MAX , +NFTA_NAT_FLAGS , __NFTA_NAT_MAX };#define NFTA_NAT_MAX (__NFTA_NAT_MAX - 1)
net/netfilter/nft_nat.c +13 -0
@@ -33,6 +33,7 @@ struct nft_nat { enum nft_registers sreg_proto_max:8; enum nf_nat_manip_type type:8;u8 family; +u32 flags; }; static void nft_nat_eval(const struct nft_expr *expr,
@@ -71,6 +72,8 @@ static void nft_nat_eval(const struct nft_expr *expr, range.flags |=NF_NAT_RANGE_PROTO_SPECIFIED ; } + range.flags |= priv->flags; + data[NFT_REG_VERDICT ].verdict =nf_nat_setup_info (ct, &range, priv->type); }
@@ -82,6 +85,7 @@ static const struct nla_policy nft_nat_policy[NFTA_NAT_MAX + 1] = { [NFTA_NAT_REG_ADDR_MAX] = { .type = NLA_U32 }, [NFTA_NAT_REG_PROTO_MIN] = { .type = NLA_U32 }, [NFTA_NAT_REG_PROTO_MAX] = { .type = NLA_U32 }, + [NFTA_NAT_FLAGS] = { .type = NLA_U32 }, }; static int nft_nat_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
@@ -149,6 +153,9 @@ static int nft_nat_init(const struct nft_ctx *ctx, const struct nft_expr *expr, }else priv->sreg_proto_max = priv->sreg_proto_min; +if (tb[NFTA_NAT_FLAGS ]) + priv->flags =ntohl (nla_get_be32 (tb[NFTA_NAT_FLAGS ])); +return 0 ; }
@@ -183,6 +190,12 @@ static int nft_nat_dump(struct sk_buff *skb, const struct nft_expr *expr) htonl(priv->sreg_proto_max))) goto nla_put_failure; } + +if (priv->flags !=0 ) { +if (nla_put_be32 (skb,NFT_NAT_FLAGS ,htonl (priv->flags))) + goto nla_put_failure); + } +return 0 ; nla_put_failure:
Florian Westphal
maintainerRe: [linux PATCH 1/5] netfilter: nft_nat: include a flag attribute
Quoted 14 lines · Arturo Borrero Gonzalez
Both SNAT and DNAT (and the upcoming masquerade) can have additional configuration parameters, such as port randomization or NAT addressing persistence.
We can cover these scenarios by simply adding a flag attribute for userspace to fill when needed.The flags to use are defined in
include/uapi/linux/netfilter/nf_nat.h,
NF_NAT_RANGE_MAP_IPS
NF_NAT_RANGE_PROTO_SPECIFIED
NF_NAT_RANGE_PROTO_RANDOM
NF_NAT_RANGE_PERSISTENT
NF_NAT_RANGE_PROTO_RANDOM_FULLYNF_NAT_RANGE_PROTO_RANDOM_ALL
Quoted 2 lines
The caller must take care of not messing up with the flags, as they are added unconditionally to the final resulting
nf_nat_range.
I dont think that this is desirable.
When userspace knows about NF_NAT_RANGE_FOO but the kernel does not userspace should be ablre to detect that kernel is too old.
IOW,
net/netfilter/nft_nat.c static int nft_nat_init(const struct nft_ctx *ctx, const struct nft_expr *expr, · line 160
Quoted 10 lines
static int nft_nat_init(const struct
nft_ctx*ctx, const structnft_expr*expr,net/netfilter/nft_nat.c +3
-0@@ -149,6 +153,9 @@ static int nft_nat_init(const struct nft_ctx *ctx, const struct nft_expr *expr, }else priv->sreg_proto_max = priv->sreg_proto_min; +if (tb[NFTA_NAT_FLAGS ]) + priv->flags =ntohl (nla_get_be32 (tb[NFTA_NAT_FLAGS ])); +return 0 ; }
I think that NFTA_NAT_FLAGS desevers further validation that all flags/bits are known.
PATCH 2/5 netfilter: nf_nat_masquerade_ipv4: code factorization
In reply to Arturo Borrero GonzalezLet's refactor the code so we can reach the masquerade functionality from outside the xt context (ie, nftables).
The patch includes adding an atomic counter to the masquerade notifier: the stuff to be done by the notifier is the same in any case, and agnostic about who called it. Only one notification handler is needed.
This factorization only involves IPv4; a similar patch will follow to handle IPv6.
--- .../net/netfilter/ipv4/nf_nat_masquerade_ipv4.h | 14 ++ net/ipv4/netfilter/Kconfig | 7 + net/ipv4/netfilter/Makefile | 1 net/ipv4/netfilter/ipt_MASQUERADE.c | 108 +------------- net/ipv4/netfilter/nf_nat_masquerade_ipv4.c | 155 ++++++++++++++++++++ 5 files changed, 186 insertions(+), 99 deletions(-) create mode 100644 include/net/netfilter/ipv4/nf_nat_masquerade_ipv4.h create mode 100644 net/ipv4/netfilter/nf_nat_masquerade_ipv4.c
include/net/netfilter/ipv4/nf_nat_masquerade_ipv4.h new file +14 -0
@@ -0,0 +1,14 @@ +#ifndef _NF_NAT_MASQUERADE_IPV4_H_ +#define _NF_NAT_MASQUERADE_IPV4_H_ + +#include <net/netfilter/nf_nat.h> + +unsigned int +nf_nat_masquerade_ipv4 (struct sk_buff *skb,unsigned int hooknum, +const struct nf_nat_range *range, +const struct net_device *out); + +void nf_nat_masquerade_ipv4_register_notifier (void ); +void nf_nat_masquerade_ipv4_unregister_notifier (void ); + +#endif /*_NF_NAT_MASQUERADE_IPV4_H_ */
net/ipv4/netfilter/Kconfig +7 -0
@@ -193,8 +193,15 @@ config NF_NAT_IPV4 if NF_NAT_IPV4 +config NF_NAT_MASQUERADE_IPV4 + tristate "IPv4 masquerade support" + help + This is the kernel functionality to provide NAT in the masquerade + flavour (automatic source address selection). + config IP_NF_TARGET_MASQUERADE tristate "MASQUERADE target support" + select NF_NAT_MASQUERADE_IPV4 default m if NETFILTER_ADVANCED=n help Masquerading is a special case of NAT: all outgoing connections are
net/ipv4/netfilter/Makefile +1 -0
@@ -23,6 +23,7 @@ obj-$(CONFIG_NF_DEFRAG_IPV4) += nf_defrag_ipv4.o obj-$(CONFIG_NF_NAT_H323) += nf_nat_h323.o obj-$(CONFIG_NF_NAT_PPTP) += nf_nat_pptp.o obj-$(CONFIG_NF_NAT_SNMP_BASIC) += nf_nat_snmp_basic.o +obj-$(CONFIG_NF_NAT_MASQUERADE_IPV4) += nf_nat_masquerade_ipv4.o # NAT protocols (nf_nat) obj-$(CONFIG_NF_NAT_PROTO_GRE) += nf_nat_proto_gre.o
net/ipv4/netfilter/ipt_MASQUERADE.c +9 -99
138 lines not on this page · raw message
net/ipv4/netfilter/nf_nat_masquerade_ipv4.c new file +155 -0
155 lines not on this page · raw message
PATCH 3/5 netfilter: nf_nat_masquerade_ipv6: code factorization
In reply to Arturo Borrero GonzalezLet's refactor the code so we can reach the masquerade functionality from outside the xt context (ie, nftables).
The patch includes adding an atomic counter to the masquerade notifier: the stuff to be done by the notifier is the same in any case, and agnostic about who called it. Only one notification handler is needed.
This factorization only involves IPv6; a similar patch exists to handle IPv4.
--- .../net/netfilter/ipv6/nf_nat_masquerade_ipv6.h | 10 ++ net/ipv6/netfilter/Kconfig | 7 + net/ipv6/netfilter/Makefile | 1 net/ipv6/netfilter/ip6t_MASQUERADE.c | 76 +------------ net/ipv6/netfilter/nf_nat_masquerade_ipv6.c | 121 ++++++++++++++++++++ 5 files changed, 144 insertions(+), 71 deletions(-) create mode 100644 include/net/netfilter/ipv6/nf_nat_masquerade_ipv6.h create mode 100644 net/ipv6/netfilter/nf_nat_masquerade_ipv6.c
include/net/netfilter/ipv6/nf_nat_masquerade_ipv6.h new file +10 -0
@@ -0,0 +1,10 @@ +#ifndef _NF_NAT_MASQUERADE_IPV6_H_ +#define _NF_NAT_MASQUERADE_IPV6_H_ + +unsigned int +nf_nat_masquerade_ipv6 (struct sk_buff *skb,const struct nf_nat_range *range, +const struct net_device *out); +void nf_nat_masquerade_ipv6_register_notifier (void ); +void nf_nat_masquerade_ipv6_unregister_notifier (void ); + +#endif /* _NF_NAT_MASQUERADE_IPV6_H_ */
net/ipv6/netfilter/Kconfig +7 -0
@@ -241,8 +241,15 @@ config NF_NAT_IPV6 if NF_NAT_IPV6 +config NF_NAT_MASQUERADE_IPV6 + tristate "IPv6 masquerade support" + help + This is the kernel functionality to provide NAT in the masquerade + flavour (automatic source address selection) for IPv6. + config IP6_NF_TARGET_MASQUERADE tristate "MASQUERADE target support" + select NF_NAT_MASQUERADE_IPV6 help Masquerading is a special case of NAT: all outgoing connections are changed to seem to come from a particular interface's address, and
net/ipv6/netfilter/Makefile +1 -0
@@ -18,6 +18,7 @@ obj-$(CONFIG_NF_CONNTRACK_IPV6) += nf_conntrack_ipv6.o nf_nat_ipv6-y := nf_nat_l3proto_ipv6.o nf_nat_proto_icmpv6.o obj-$(CONFIG_NF_NAT_IPV6) += nf_nat_ipv6.o +obj-$(CONFIG_NF_NAT_MASQUERADE_IPV6) += nf_nat_masquerade_ipv6.o # defrag nf_defrag_ipv6-y := nf_defrag_ipv6_hooks.o nf_conntrack_reasm.o
net/ipv6/netfilter/ip6t_MASQUERADE.c +5 -71
103 lines not on this page · raw message
net/ipv6/netfilter/nf_nat_masquerade_ipv6.c new file +121 -0
121 lines not on this page · raw message
PATCH 4/5 netfilter: nft_nat: split code in AF parts
In reply to Arturo Borrero GonzalezThis patch refactorices the nft_nat code into AF specific parts, allowing further work in the AF specific zones, like adding masquerade support.
While at it, code style is fixed in several places.
--- include/net/netfilter/nft_nat.h | 20 ++++ net/ipv4/netfilter/Kconfig | 7 ++ net/ipv4/netfilter/Makefile | 1 net/ipv4/netfilter/nft_nat_ipv4.c | 140 +++++++++++++++++++++++++++++++ net/ipv6/netfilter/Kconfig | 7 ++ net/ipv6/netfilter/Makefile | 1 net/ipv6/netfilter/nft_nat_ipv6.c | 139 +++++++++++++++++++++++++++++++ net/netfilter/nft_nat.c | 166 ++++--------------------------------- 8 files changed, 335 insertions(+), 146 deletions(-) create mode 100644 include/net/netfilter/nft_nat.h create mode 100644 net/ipv4/netfilter/nft_nat_ipv4.c create mode 100644 net/ipv6/netfilter/nft_nat_ipv6.c
include/net/netfilter/nft_nat.h new file +20 -0
@@ -0,0 +1,20 @@ +#ifndef _NFT_NAT_H_ +#define _NFT_NAT_H_ + +struct nft_nat { +enum nft_registers sreg_addr_min:8 ; +enum nft_registers sreg_addr_max:8 ; +enum nft_registers sreg_proto_min:8 ; +enum nft_registers sreg_proto_max:8 ; +enum nf_nat_manip_type type:8 ; +u8 family; +u32 flags; +}; + +extern const struct nla_policy nft_nat_policy[]; + +int nft_nat_init (const struct nft_ctx *ctx, +const struct nft_expr *expr, +const struct nlattr *const tb[]); + +#endif /* _NFT_NAT_H_ */
net/ipv4/netfilter/Kconfig +7 -0
@@ -199,6 +199,13 @@ config NF_NAT_MASQUERADE_IPV4 This is the kernel functionality to provide NAT in the masquerade flavour (automatic source address selection). +config NFT_NAT_IPV4 + tristate "nft_nat IPv4 support" + depends on NFT_NAT + select NF_NAT_MASQUERADE_IPV4 + help + This is the nftables expression that handles NAT in IPv4. + config IP_NF_TARGET_MASQUERADE tristate "MASQUERADE target support" select NF_NAT_MASQUERADE_IPV4
net/ipv4/netfilter/Makefile +1 -0
@@ -32,6 +32,7 @@ obj-$(CONFIG_NF_TABLES_IPV4) += nf_tables_ipv4.o obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV4) += nft_chain_route_ipv4.o obj-$(CONFIG_NFT_CHAIN_NAT_IPV4) += nft_chain_nat_ipv4.o obj-$(CONFIG_NFT_REJECT_IPV4) += nft_reject_ipv4.o +obj-$(CONFIG_NFT_NAT_IPV4) += nft_nat_ipv4.o obj-$(CONFIG_NF_TABLES_ARP) += nf_tables_arp.o # generic IP tables
net/ipv4/netfilter/nft_nat_ipv4.c new file +140 -0
140 lines not on this page · raw message
net/ipv6/netfilter/Kconfig +7 -0
@@ -247,6 +247,13 @@ config NF_NAT_MASQUERADE_IPV6 This is the kernel functionality to provide NAT in the masquerade flavour (automatic source address selection) for IPv6. +config NFT_NAT_IPV6 + tristate "nft_nat IPv6 support" + depends on NFT_NAT + select NF_NAT_MASQUERADE_IPV6 + help + This is the nftables expression that handles NAT in IPv6. + config IP6_NF_TARGET_MASQUERADE tristate "MASQUERADE target support" select NF_NAT_MASQUERADE_IPV6
net/ipv6/netfilter/Makefile +1 -0
@@ -29,6 +29,7 @@ obj-$(CONFIG_NF_TABLES_IPV6) += nf_tables_ipv6.o obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV6) += nft_chain_route_ipv6.o obj-$(CONFIG_NFT_CHAIN_NAT_IPV6) += nft_chain_nat_ipv6.o obj-$(CONFIG_NFT_REJECT_IPV6) += nft_reject_ipv6.o +obj-$(CONFIG_NFT_NAT_IPV6) += nft_nat_ipv6.o # matches obj-$(CONFIG_IP6_NF_MATCH_AH) += ip6t_ah.o
net/ipv6/netfilter/nft_nat_ipv6.c new file +139 -0
139 lines not on this page · raw message
net/netfilter/nft_nat.c +20 -146
218 lines not on this page · raw message
PATCH 5/5 netfilter: nft_nat: add masquerade support
In reply to Arturo Borrero GonzalezThis patch adds masquerade support to nft_nat.
Note that enum nf_nat_manip_type is replaced by enum nft_nat_types in order to support masquerade.
--- include/net/netfilter/nft_nat.h | 2 + include/uapi/linux/netfilter/nf_tables.h | 5 ++- net/ipv4/netfilter/nft_nat_ipv4.c | 52 ++++++++++++++++++++++-------- net/ipv6/netfilter/nft_nat_ipv6.c | 52 ++++++++++++++++++++++-------- net/netfilter/nft_nat.c | 8 ++--- 5 files changed, 84 insertions(+), 35 deletions(-)
include/net/netfilter/nft_nat.h +1 -1
@@ -6,7 +6,7 @@ struct nft_nat { enum nft_registers sreg_addr_max:8; enum nft_registers sreg_proto_min:8; enum nft_registers sreg_proto_max:8; - enum nf_nat_manip_type type:8; + enum nft_nat_types type:8;u8 family;u32 flags; };
include/uapi/linux/netfilter/nf_tables.h +4 -1
@@ -754,14 +754,17 @@ enum nft_reject_attributes {#define NFTA_REJECT_MAX (__NFTA_REJECT_MAX - 1)/** -* enum nft_nat_types - nf_tables nat expression NAT types +* enum nft_nat_types - nf_tables nat expression NAT types. +* must match enum nf_nat_manip_type. * * @NFT_NAT_SNAT: source NAT * @NFT_NAT_DNAT: destination NAT +* @NFT_NAT_MASQUERADE: masquerade NAT */ enum nft_nat_types {NFT_NAT_SNAT ,NFT_NAT_DNAT , +NFT_NAT_MASQUERADE , };/**
net/ipv4/netfilter/nft_nat_ipv4.c +38 -14
81 lines not on this page · raw message
net/ipv6/netfilter/nft_nat_ipv6.c +38 -14
87 lines not on this page · raw message
net/netfilter/nft_nat.c +3 -5
@@ -43,18 +43,16 @@ int nft_nat_init(const struct nft_ctx *ctx, const struct nft_expr *expr, const struct nlattr * const tb[]) {struct nft_nat *priv =nft_expr_priv (expr); -u32 family;int err;if (tb[NFTA_NAT_TYPE ] ==NULL )return -EINVAL ; -switch (ntohl (nla_get_be32 (tb[NFTA_NAT_TYPE ]))) { + priv->type =ntohl (nla_get_be32 (tb[NFTA_NAT_TYPE ])); +switch (priv->type) {case NFT_NAT_SNAT : - priv->type =NF_NAT_MANIP_SRC ; -break ;case NFT_NAT_DNAT : - priv->type =NF_NAT_MANIP_DST ; +case NFT_NAT_MASQUERADE :break ;default :return -EINVAL ;