nftables iptables nat co-existence patches
Subsystems (2)
Florian Westphal
maintainercover nftables iptables nat co-existence patches
... another round of changes we can discuss at nfws. This allows to use both iptables and nft nat at the same time, first match wins.
Florian Westphal
maintainerRFC PATCH 1/2 netfilter: add nf_nat_netns_get infrasstructure
This adds nf_nat_netns_get and put helpers, and uses them in the nft expressions and xtables targets that deal with nat setup.
The new helpers are just aliases for nf_ct_netns_get/put, this is done to keep the actual change -- per netns register of nat rewrite hooks -- separate.
--- include/net/netfilter/nf_nat.h | 4 ++++ net/ipv4/netfilter/ipt_MASQUERADE.c | 4 ++-- net/ipv4/netfilter/nft_masq_ipv4.c | 2 +- net/ipv4/netfilter/nft_redir_ipv4.c | 2 +- net/ipv6/netfilter/nft_masq_ipv6.c | 2 +- net/ipv6/netfilter/nft_redir_ipv6.c | 2 +- net/netfilter/nf_nat_core.c | 24 ++++++++++++++++++++++++ net/netfilter/nft_masq.c | 2 +- net/netfilter/nft_nat.c | 4 ++-- net/netfilter/nft_redir.c | 2 +- net/netfilter/xt_NETMAP.c | 6 +++--- net/netfilter/xt_REDIRECT.c | 6 +++--- net/netfilter/xt_nat.c | 6 +++--- 13 files changed, 47 insertions(+), 19 deletions(-)
include/net/netfilter/nf_nat.h +4 -0
@@ -47,6 +47,10 @@ extern unsigned int nf_nat_alloc_null_binding(struct nf_conn *ct,struct nf_conn_nat *nf_ct_nat_ext_add (struct nf_conn *ct); +/* net namespace needs NAT hooks */ +int nf_nat_netns_get (struct net *net,u8 nfproto); +void nf_nat_netns_put (struct net *net,u8 nfproto); +/* Is this tuple already taken? (not by us)*/ int nf_nat_used_tuple (const struct nf_conntrack_tuple *tuple,const struct nf_conn *ignored_conntrack);
net/ipv4/netfilter/ipt_MASQUERADE.c +2 -2
@@ -41,7 +41,7 @@ static int masquerade_tg_check(const struct xt_tgchk_param *par)pr_debug ("bad rangesize %u\n" , mr->rangesize);return -EINVAL ; } -return nf_ct_netns_get (par->net, par->family); +return nf_nat_netns_get (par->net, par->family); } static unsigned int
@@ -61,7 +61,7 @@ masquerade_tg(struct sk_buff *skb, const struct xt_action_param *par)static void masquerade_tg_destroy (const struct xt_tgdtor_param *par) { -nf_ct_netns_put (par->net, par->family); +nf_nat_netns_put (par->net, par->family); }static struct xt_target masquerade_tg_reg__read_mostly = {
net/ipv4/netfilter/nft_masq_ipv4.c +1 -1
@@ -38,7 +38,7 @@ static void nft_masq_ipv4_eval(const struct nft_expr *expr,static void nft_masq_ipv4_destroy (const struct nft_ctx *ctx,const struct nft_expr *expr) { -nf_ct_netns_put (ctx->net,NFPROTO_IPV4 ); +nf_nat_netns_put (ctx->net,NFPROTO_IPV4 ); }static struct nft_expr_type nft_masq_ipv4_type;
net/ipv4/netfilter/nft_redir_ipv4.c +1 -1
@@ -41,7 +41,7 @@ static void nft_redir_ipv4_eval(const struct nft_expr *expr,static void nft_redir_ipv4_destroy (const struct nft_ctx *ctx,const struct nft_expr *expr) { -nf_ct_netns_put (ctx->net,NFPROTO_IPV4 ); +nf_nat_netns_put (ctx->net,NFPROTO_IPV4 ); }static struct nft_expr_type nft_redir_ipv4_type;
net/ipv6/netfilter/nft_masq_ipv6.c +1 -1
@@ -39,7 +39,7 @@ static void nft_masq_ipv6_eval(const struct nft_expr *expr,static void nft_masq_ipv6_destroy (const struct nft_ctx *ctx,const struct nft_expr *expr) { -nf_ct_netns_put (ctx->net,NFPROTO_IPV6 ); +nf_nat_netns_put (ctx->net,NFPROTO_IPV6 ); }static struct nft_expr_type nft_masq_ipv6_type;
net/ipv6/netfilter/nft_redir_ipv6.c +1 -1
@@ -42,7 +42,7 @@ static void nft_redir_ipv6_eval(const struct nft_expr *expr,static void nft_redir_ipv6_destroy (const struct nft_ctx *ctx,const struct nft_expr *expr) { -nf_ct_netns_put (ctx->net,NFPROTO_IPV6 ); +nf_nat_netns_put (ctx->net,NFPROTO_IPV6 ); }static struct nft_expr_type nft_redir_ipv6_type;
net/netfilter/nf_nat_core.c +24 -0
@@ -818,6 +818,30 @@ static struct nf_ct_helper_expectfn follow_master_nat = { .expectfn = nf_nat_follow_master, }; +int nf_nat_netns_get (struct net *net,u8 nfproto) +{ +int ret; + +if (WARN_ON (nfproto >=ARRAY_SIZE (nat_net->users))) +return -EINVAL ; + + ret =nf_ct_netns_get (net, nfproto); +if (ret <0 ) +return ret; + +return ret; +} +EXPORT_SYMBOL_GPL (nf_nat_netns_get); + +void nf_nat_netns_put (struct net *net,u8 nfproto) +{ +if (WARN_ON (nfproto >=ARRAY_SIZE (nat_net->users))) + goto out; +out: +nf_ct_netns_put (net, nfproto); +} +EXPORT_SYMBOL_GPL (nf_nat_netns_put); +static int __init nf_nat_init (void ) {int ret;
net/netfilter/nft_masq.c +1 -1
@@ -73,7 +73,7 @@ int nft_masq_init(const struct nft_ctx *ctx, } } -return nf_ct_netns_get (ctx->net, ctx->afi->family); +return nf_nat_netns_get (ctx->net, ctx->afi->family); }EXPORT_SYMBOL_GPL (nft_masq_init);
net/netfilter/nft_nat.c +2 -2
@@ -205,7 +205,7 @@ static int nft_nat_init(const struct nft_ctx *ctx, const struct nft_expr *expr,return -EINVAL ; } -return nf_ct_netns_get (ctx->net, family); +return nf_nat_netns_get (ctx->net, family); }static int nft_nat_dump (struct sk_buff *skb,const struct nft_expr *expr)
@@ -258,7 +258,7 @@ nft_nat_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr) {const struct nft_nat *priv =nft_expr_priv (expr); -nf_ct_netns_put (ctx->net, priv->family); +nf_nat_netns_put (ctx->net, priv->family); }static struct nft_expr_type nft_nat_type;
net/netfilter/nft_redir.c +1 -1
@@ -75,7 +75,7 @@ int nft_redir_init(const struct nft_ctx *ctx,return -EINVAL ; } -return nf_ct_netns_get (ctx->net, ctx->afi->family); +return nf_nat_netns_get (ctx->net, ctx->afi->family); }EXPORT_SYMBOL_GPL (nft_redir_init);
net/netfilter/xt_NETMAP.c +3 -3
@@ -60,12 +60,12 @@ static int netmap_tg6_checkentry(const struct xt_tgchk_param *par)if (!(range->flags &NF_NAT_RANGE_MAP_IPS ))return -EINVAL ; -return nf_ct_netns_get (par->net, par->family); +return nf_nat_netns_get (par->net, par->family); }static void netmap_tg_destroy (const struct xt_tgdtor_param *par) { -nf_ct_netns_put (par->net, par->family); +nf_nat_netns_put (par->net, par->family); } static unsigned int
@@ -116,7 +116,7 @@ static int netmap_tg4_check(const struct xt_tgchk_param *par)pr_debug ("bad rangesize %u.\n" , mr->rangesize);return -EINVAL ; } -return nf_ct_netns_get (par->net, par->family); +return nf_nat_netns_get (par->net, par->family); }static struct xt_target netmap_tg_reg[]__read_mostly = {
net/netfilter/xt_REDIRECT.c +3 -3
@@ -41,12 +41,12 @@ static int redirect_tg6_checkentry(const struct xt_tgchk_param *par)if (range->flags &NF_NAT_RANGE_MAP_IPS )return -EINVAL ; -return nf_ct_netns_get (par->net, par->family); +return nf_nat_netns_get (par->net, par->family); }static void redirect_tg_destroy (const struct xt_tgdtor_param *par) { -nf_ct_netns_put (par->net, par->family); +nf_nat_netns_put (par->net, par->family); }/* FIXME: Take multiple ranges --RR */
@@ -62,7 +62,7 @@ static int redirect_tg4_check(const struct xt_tgchk_param *par)pr_debug ("bad rangesize %u.\n" , mr->rangesize);return -EINVAL ; } -return nf_ct_netns_get (par->net, par->family); +return nf_nat_netns_get (par->net, par->family); } static unsigned int
net/netfilter/xt_nat.c +3 -3
@@ -23,17 +23,17 @@ static int xt_nat_checkentry_v0(const struct xt_tgchk_param *par) par->target->name);return -EINVAL ; } -return nf_ct_netns_get (par->net, par->family); +return nf_nat_netns_get (par->net, par->family); }static int xt_nat_checkentry (const struct xt_tgchk_param *par) { -return nf_ct_netns_get (par->net, par->family); +return nf_nat_netns_get (par->net, par->family); }static void xt_nat_destroy (const struct xt_tgdtor_param *par) { -nf_ct_netns_put (par->net, par->family); +nf_nat_netns_put (par->net, par->family); } static void xt_nat_convert_range(struct nf_nat_range *dst,
Signature · 1 line
2.13.0
Florian Westphal
maintainerRFC PATCH 2/2 nefilter: nf_nat: split nat rewriting from do_chain logic
In reply to Florian WestphalCurrently the packet rewrite and instantiation of nat NULL bindings happens from the protocol specific nat backend.
Problem is that this means invocation occurs either via ip(6)table_nat or the nf_tables nat chain type.
This is a problem for two reasons:
1. Can't use iptables nat and nf_tables nat at the same time, as the first user adds a nat binding (we add a NULL binding if no nat rule matched so we can detect post-nat tuple collisions).
2. If you use e.g. nft_masq, snat, redir, etc. you need to register an empty base chain so that the nat core does the reverse translation.
After this change, the nat core deals with null bindings and reverse translation. If both iptables and nftables nat exists, the first matching one is used.
The rewrite/null addition hooks get added/removed once a net namespace installs the first nat rule in either nftables or xtables nat tables.
The downside of this change is that we need one more hook function in all the ip stack hook points (except forward),
--- net/ipv4/netfilter/nf_nat_l3proto_ipv4.c | 32 +----- net/ipv6/netfilter/nf_nat_l3proto_ipv6.c | 29 +----- net/netfilter/nf_nat_core.c | 174 +++++++++++++++++++++++++++++++ 3 files changed, 179 insertions(+), 56 deletions(-)
net/ipv4/netfilter/nf_nat_l3proto_ipv4.c +3 -29
@@ -251,7 +251,6 @@ nf_nat_ipv4_fn(void *priv, struct sk_buff *skb, {struct nf_conn *ct;enum ip_conntrack_info ctinfo; -struct nf_conn_nat *nat;/* maniptype == SRC for postrouting. */ enum nf_nat_manip_type maniptype =HOOK2MANIP (state->hook);
@@ -264,8 +263,6 @@ nf_nat_ipv4_fn(void *priv, struct sk_buff *skb,if (!ct)return NF_ACCEPT ; - nat =nfct_nat (ct); -switch (ctinfo) {case IP_CT_RELATED :case IP_CT_RELATED_REPLY :
@@ -287,36 +284,13 @@ nf_nat_ipv4_fn(void *priv, struct sk_buff *skb, ret =do_chain (priv, skb, state, ct);if (ret !=NF_ACCEPT )return ret; - -if (nf_nat_initialized (ct,HOOK2MANIP (state->hook))) -break ; - - ret =nf_nat_alloc_null_binding (ct, state->hook); -if (ret !=NF_ACCEPT ) -return ret; - }else { -pr_debug ("Already setup manip %s for ct %p\n" , - maniptype ==NF_NAT_MANIP_SRC ?"SRC" :"DST" , - ct); -if (nf_nat_oif_changed (state->hook, ctinfo, nat, - state->out)) - goto oif_changed; }break ; -default : -/* ESTABLISHED */ -NF_CT_ASSERT (ctinfo ==IP_CT_ESTABLISHED || - ctinfo ==IP_CT_ESTABLISHED_REPLY ); -if (nf_nat_oif_changed (state->hook, ctinfo, nat, state->out)) - goto oif_changed; +break ; } -return nf_nat_packet (ct, ctinfo, state->hook, skb); - -oif_changed: -nf_ct_kill_acct (ct, ctinfo, skb); -return NF_DROP ; +return NF_ACCEPT ; }EXPORT_SYMBOL_GPL (nf_nat_ipv4_fn);
@@ -436,8 +410,8 @@ static int __init nf_nat_l3proto_ipv4_init(void) err =nf_nat_l3proto_register (&nf_nat_l3proto_ipv4);if (err <0 ) goto err2; -return err; +return err; err2:nf_nat_l4proto_unregister (NFPROTO_IPV4 , &nf_nat_l4proto_icmp); err1:
net/ipv6/netfilter/nf_nat_l3proto_ipv6.c +2 -27
@@ -258,7 +258,6 @@ nf_nat_ipv6_fn(void *priv, struct sk_buff *skb, {struct nf_conn *ct;enum ip_conntrack_info ctinfo; -struct nf_conn_nat *nat;enum nf_nat_manip_type maniptype =HOOK2MANIP (state->hook);__be16 frag_off;int hdrlen;
@@ -273,8 +272,6 @@ nf_nat_ipv6_fn(void *priv, struct sk_buff *skb,if (!ct)return NF_ACCEPT ; - nat =nfct_nat (ct); -switch (ctinfo) {case IP_CT_RELATED :case IP_CT_RELATED_REPLY :
@@ -301,35 +298,13 @@ nf_nat_ipv6_fn(void *priv, struct sk_buff *skb, ret =do_chain (priv, skb, state, ct);if (ret !=NF_ACCEPT )return ret; - -if (nf_nat_initialized (ct,HOOK2MANIP (state->hook))) -break ; - - ret =nf_nat_alloc_null_binding (ct, state->hook); -if (ret !=NF_ACCEPT ) -return ret; - }else { -pr_debug ("Already setup manip %s for ct %p\n" , - maniptype ==NF_NAT_MANIP_SRC ?"SRC" :"DST" , - ct); -if (nf_nat_oif_changed (state->hook, ctinfo, nat, state->out)) - goto oif_changed; }break ; -default : -/* ESTABLISHED */ -NF_CT_ASSERT (ctinfo ==IP_CT_ESTABLISHED || - ctinfo ==IP_CT_ESTABLISHED_REPLY ); -if (nf_nat_oif_changed (state->hook, ctinfo, nat, state->out)) - goto oif_changed; +break ; } -return nf_nat_packet (ct, ctinfo, state->hook, skb); - -oif_changed: -nf_ct_kill_acct (ct, ctinfo, skb); -return NF_DROP ; +return NF_ACCEPT ; }EXPORT_SYMBOL_GPL (nf_nat_ipv6_fn);
net/netfilter/nf_nat_core.c +174 -0
231 lines not on this page · raw message
Signature · 1 line
2.13.0