netfilter: nat: add inet family nat support
Subsystems (4)
Florian Westphal
maintainercover netfilter: nat: add inet family nat support
This series adds support for the nat chain type in the inet family.
Example:
table inet nat {
chain postrouting {
type nat hook output priority 0; policy accept;
ip6 daddr dead:1::99 snat ip6 to dead:2::99
meta oifname "ppp+" masquerade
}
}This avoids the need to create both ip and ip6 nat tables for the masquerade rule.
Florian Westphal (6):
netfilter: nat: add inet family nat support
netfilter: nf_tables: merge route type into core
netfilter: replace NF_NAT_NEEDED with IS_ENABLED(CONFIG_NF_NAT)
netfilter: nft_masq: add inet support
netfilter: nft_redir: add inet support
kselftests: extend nft_nat with inet family based nat hooks
include/linux/netfilter.h | 2 include/linux/netfilter_ipv6.h | 15 ++ include/net/netfilter/nf_conntrack_expect.h | 2 include/net/netfilter/nf_nat.h | 7 - include/net/netfilter/nf_tables.h | 2 net/ipv4/netfilter/Kconfig | 8 - net/ipv4/netfilter/Makefile | 1 net/ipv6/netfilter/Kconfig | 8 - net/ipv6/netfilter/Makefile | 1 net/netfilter/Kconfig | 5 net/netfilter/Makefile | 3 net/netfilter/nf_conntrack_expect.c | 2 net/netfilter/nf_conntrack_netlink.c | 16 +- net/netfilter/nf_conntrack_sip.c | 2 net/netfilter/nf_nat_core.c | 16 +- net/netfilter/nf_nat_proto.c | 59 +++++--- net/netfilter/nf_tables_api.c | 2 net/netfilter/nft_chain_nat.c | 36 +++++ net/netfilter/nft_chain_route.c | 169 +++++++++++++++++++++++++ net/netfilter/nft_masq.c | 64 +++++++++ net/netfilter/nft_nat.c | 58 ++++++++ net/netfilter/nft_redir.c | 61 +++++++++ net/openvswitch/conntrack.c | 18 +- tools/testing/selftests/netfilter/nft_nat.sh | 130 +++++++++++++------ net/ipv4/netfilter/nft_chain_route_ipv4.c | 89 ------------- net/ipv6/netfilter/nft_chain_route_ipv6.c | 91 ------------- 26 files changed, 572 insertions(+), 295 deletions(-)
Florian Westphal
maintainerPATCH 1/6 netfilter: nat: add inet family nat support
We need minimal support from the nat core for this, as we do not want to register additional base hooks.
When an inet hook is registered, interally register ipv4 and ipv6 hooks for them and unregister those when inet hooks are removed.
--- include/net/netfilter/nf_nat.h | 7 ++-- net/netfilter/nf_nat_core.c | 16 ++++------ net/netfilter/nf_nat_proto.c | 43 +++++++++++++++++++++---- net/netfilter/nft_chain_nat.c | 36 +++++++++++++++++++++ net/netfilter/nft_nat.c | 58 ++++++++++++++++++++++++++++++++-- 5 files changed, 141 insertions(+), 19 deletions(-)
include/net/netfilter/nf_nat.h +5 -2
@@ -69,9 +69,9 @@ static inline bool nf_nat_oif_changed(unsigned int hooknum,#endif } -int nf_nat_register_fn (struct net *net,const struct nf_hook_ops *ops, +int nf_nat_register_fn (struct net *net,u8 pf,const struct nf_hook_ops *ops,const struct nf_hook_ops *nat_ops,unsigned int ops_count); -void nf_nat_unregister_fn (struct net *net,const struct nf_hook_ops *ops, +void nf_nat_unregister_fn (struct net *net,u8 pf,const struct nf_hook_ops *ops,unsigned int ops_count); unsigned int nf_nat_packet(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
@@ -98,6 +98,9 @@ void nf_nat_ipv4_unregister_fn(struct net *net, const struct nf_hook_ops *ops);int nf_nat_ipv6_register_fn (struct net *net,const struct nf_hook_ops *ops);void nf_nat_ipv6_unregister_fn (struct net *net,const struct nf_hook_ops *ops); +int nf_nat_inet_register_fn (struct net *net,const struct nf_hook_ops *ops); +void nf_nat_inet_unregister_fn (struct net *net,const struct nf_hook_ops *ops); +unsigned int nf_nat_inet_fn (void *priv,struct sk_buff *skb,const struct nf_hook_state *state);
net/netfilter/nf_nat_core.c +7 -9
@@ -1009,7 +1009,7 @@ static struct nf_ct_helper_expectfn follow_master_nat = { .expectfn = nf_nat_follow_master, }; -int nf_nat_register_fn (struct net *net,const struct nf_hook_ops *ops, +int nf_nat_register_fn (struct net *net,u8 pf,const struct nf_hook_ops *ops,const struct nf_hook_ops *orig_nat_ops,unsigned int ops_count) {struct nat_net *nat_net =net_generic (net, nat_net_id);
@@ -1019,14 +1019,12 @@ int nf_nat_register_fn(struct net *net, const struct nf_hook_ops *ops,struct nf_hook_ops *nat_ops;int i, ret; -if (WARN_ON_ONCE (ops->pf >=ARRAY_SIZE (nat_net->nat_proto_net))) +if (WARN_ON_ONCE (pf >=ARRAY_SIZE (nat_net->nat_proto_net)))return -EINVAL ; - nat_proto_net = &nat_net->nat_proto_net[ops->pf]; + nat_proto_net = &nat_net->nat_proto_net[pf];for (i =0 ; i < ops_count; i++) { -if (WARN_ON (orig_nat_ops[i].pf != ops->pf)) -return -EINVAL ;if (orig_nat_ops[i].hooknum == hooknum) { hooknum = i;break ;
@@ -1086,8 +1084,8 @@ int nf_nat_register_fn(struct net *net, const struct nf_hook_ops *ops,return ret; } -void nf_nat_unregister_fn (struct net *net,const struct nf_hook_ops *ops, -unsigned int ops_count) +void nf_nat_unregister_fn (struct net *net,u8 pf,const struct nf_hook_ops *ops, +unsigned int ops_count) {struct nat_net *nat_net =net_generic (net, nat_net_id);struct nf_nat_hooks_net *nat_proto_net;
@@ -1096,10 +1094,10 @@ void nf_nat_unregister_fn(struct net *net, const struct nf_hook_ops *ops,int hooknum = ops->hooknum;int i; -if (ops->pf >=ARRAY_SIZE (nat_net->nat_proto_net)) +if (pf >=ARRAY_SIZE (nat_net->nat_proto_net))return ; - nat_proto_net = &nat_net->nat_proto_net[ops->pf]; + nat_proto_net = &nat_net->nat_proto_net[pf];mutex_lock (&nf_nat_proto_mutex);if (WARN_ON (nat_proto_net->users ==0 ))
net/netfilter/nf_nat_proto.c +37 -6
78 lines not on this page · raw message
net/netfilter/nft_chain_nat.c +36 -0
@@ -74,6 +74,36 @@ static const struct nft_chain_type nft_chain_nat_ipv6 = { };#endif +#ifdef CONFIG_NF_TABLES_INET +static int nft_nat_inet_reg (struct net *net,const struct nf_hook_ops *ops) +{ +return nf_nat_inet_register_fn (net, ops); +} + +static void nft_nat_inet_unreg (struct net *net,const struct nf_hook_ops *ops) +{ +nf_nat_inet_unregister_fn (net, ops); +} + +static const struct nft_chain_type nft_chain_nat_inet = { + .name ="nat" , + .type =NFT_CHAIN_T_NAT , + .family =NFPROTO_INET , + .hook_mask = (1 <<NF_INET_PRE_ROUTING ) | + (1 <<NF_INET_LOCAL_IN ) | + (1 <<NF_INET_LOCAL_OUT ) | + (1 <<NF_INET_POST_ROUTING ), + .hooks = { + [NF_INET_PRE_ROUTING ] = nft_nat_do_chain, + [NF_INET_LOCAL_IN ] = nft_nat_do_chain, + [NF_INET_LOCAL_OUT ] = nft_nat_do_chain, + [NF_INET_POST_ROUTING ] = nft_nat_do_chain, + }, + .ops_register = nft_nat_inet_reg, + .ops_unregister = nft_nat_inet_unreg, +}; +#endif +static int __init nft_chain_nat_init (void ) {#ifdef CONFIG_NF_TABLES_IPV6
@@ -82,6 +112,9 @@ static int __init nft_chain_nat_init(void)#ifdef CONFIG_NF_TABLES_IPV4 nft_register_chain_type (&nft_chain_nat_ipv4);#endif +#ifdef CONFIG_NF_TABLES_INET +nft_register_chain_type (&nft_chain_nat_inet); +#endif return 0 ; }
@@ -94,6 +127,9 @@ static void __exit nft_chain_nat_exit(void)#ifdef CONFIG_NF_TABLES_IPV6 nft_unregister_chain_type (&nft_chain_nat_ipv6);#endif +#ifdef CONFIG_NF_TABLES_INET +nft_unregister_chain_type (&nft_chain_nat_inet); +#endif }module_init (nft_chain_nat_init);
net/netfilter/nft_nat.c +56 -2
76 lines not on this page · raw message
Signature · 1 line
2.19.2
Florian Westphal
maintainerPATCH 2/6 netfilter: nf_tables: merge route type into core
In reply to Florian Westphalvery little code, so it really doesn't make sense to have extra modules or even a kconfig knob for this.
Merge them and make functionality available unconditionally. The merge makes inet family route support trivial, so add it as well here.
Before:
text data bss dec hex filename
835 832 0 1667 683 nft_chain_route_ipv4.ko
870 832 0 1702 6a6 nft_chain_route_ipv6.ko111568 2556 529 114653 1bfdd nf_tables.ko
After:
text data bss dec hex filename 113133 2556 529 116218 1c5fa nf_tables.ko
--- include/linux/netfilter_ipv6.h | 15 ++ include/net/netfilter/nf_tables.h | 2 + net/ipv4/netfilter/Kconfig | 8 - net/ipv4/netfilter/Makefile | 1 - net/ipv4/netfilter/nft_chain_route_ipv4.c | 89 ------------ net/ipv6/netfilter/Kconfig | 8 - net/ipv6/netfilter/Makefile | 1 - net/ipv6/netfilter/nft_chain_route_ipv6.c | 91 ------------ net/netfilter/Makefile | 3 +- net/netfilter/nf_nat_proto.c | 16 +- net/netfilter/nf_tables_api.c | 2 + net/netfilter/nft_chain_route.c | 169 ++++++++++++++++++++++ 12 files changed, 191 insertions(+), 214 deletions(-) delete mode 100644 net/ipv4/netfilter/nft_chain_route_ipv4.c delete mode 100644 net/ipv6/netfilter/nft_chain_route_ipv6.c create mode 100644 net/netfilter/nft_chain_route.c
include/linux/netfilter_ipv6.h +15 -0
@@ -87,6 +87,21 @@ static inline int nf_ip6_route(struct net *net, struct dst_entry **dst, }int ip6_route_me_harder (struct net *net,struct sk_buff *skb); + +static inline int nf_ip6_route_me_harder (struct net *net,struct sk_buff *skb) +{ +#if IS_MODULE (CONFIG_IPV6 ) +const struct nf_ipv6_ops *v6_ops =nf_get_ipv6_ops (); + +if (!v6_ops) +return -EHOSTUNREACH ; + +return v6_ops->route_me_harder (net, skb); +#else +return ip6_route_me_harder (net, skb); +#endif +} +__sum16 nf_ip6_checksum (struct sk_buff *skb,unsigned int hook,unsigned int dataoff,u_int8_t protocol);
include/net/netfilter/nf_tables.h +2 -0
@@ -1411,4 +1411,6 @@ struct nft_trans_flowtable {int __init nft_chain_filter_init (void );void nft_chain_filter_fini (void ); +void __init nft_chain_route_init (void ); +void nft_chain_route_fini (void );#endif /* _NET_NF_TABLES_H */
net/ipv4/netfilter/Kconfig +0 -8
@@ -27,14 +27,6 @@ config NF_TABLES_IPV4 if NF_TABLES_IPV4 -config NFT_CHAIN_ROUTE_IPV4 - tristate "IPv4 nf_tables route chain support" - help - This option enables the "route" chain for IPv4 in nf_tables. This - chain type is used to force packet re-routing after mangling header - fields such as the source, destination, type of service and - the packet mark. - config NFT_REJECT_IPV4 select NF_REJECT_IPV4 default NFT_REJECT
net/ipv4/netfilter/Makefile +0 -1
@@ -24,7 +24,6 @@ nf_nat_snmp_basic-y := nf_nat_snmp_basic.asn1.o nf_nat_snmp_basic_main.o $(obj)/nf_nat_snmp_basic_main.o: $(obj)/nf_nat_snmp_basic.asn1.h obj-$(CONFIG_NF_NAT_SNMP_BASIC) += nf_nat_snmp_basic.o -obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV4) += nft_chain_route_ipv4.o obj-$(CONFIG_NFT_REJECT_IPV4) += nft_reject_ipv4.o obj-$(CONFIG_NFT_FIB_IPV4) += nft_fib_ipv4.o obj-$(CONFIG_NFT_DUP_IPV4) += nft_dup_ipv4.o
net/ipv4/netfilter/nft_chain_route_ipv4.c deleted +0 -89
89 lines not on this page · raw message
net/ipv6/netfilter/Kconfig +0 -8
@@ -23,14 +23,6 @@ config NF_TABLES_IPV6 if NF_TABLES_IPV6 -config NFT_CHAIN_ROUTE_IPV6 - tristate "IPv6 nf_tables route chain support" - help - This option enables the "route" chain for IPv6 in nf_tables. This - chain type is used to force packet re-routing after mangling header - fields such as the source, destination, flowlabel, hop-limit and - the packet mark. - config NFT_REJECT_IPV6 select NF_REJECT_IPV6 default NFT_REJECT
net/ipv6/netfilter/Makefile +0 -1
@@ -27,7 +27,6 @@ obj-$(CONFIG_NF_REJECT_IPV6) += nf_reject_ipv6.o obj-$(CONFIG_NF_DUP_IPV6) += nf_dup_ipv6.o # nf_tables -obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV6) += nft_chain_route_ipv6.o obj-$(CONFIG_NFT_REJECT_IPV6) += nft_reject_ipv6.o obj-$(CONFIG_NFT_DUP_IPV6) += nft_dup_ipv6.o obj-$(CONFIG_NFT_FIB_IPV6) += nft_fib_ipv6.o
net/ipv6/netfilter/nft_chain_route_ipv6.c deleted +0 -91
91 lines not on this page · raw message
net/netfilter/Makefile +2 -1
@@ -77,7 +77,8 @@ obj-$(CONFIG_NF_DUP_NETDEV) += nf_dup_netdev.o nf_tables-objs := nf_tables_core.o nf_tables_api.o nft_chain_filter.o \ nf_tables_trace.o nft_immediate.o nft_cmp.o nft_range.o \ nft_bitwise.o nft_byteorder.o nft_payload.o nft_lookup.o \ - nft_dynset.o nft_meta.o nft_rt.o nft_exthdr.o + nft_dynset.o nft_meta.o nft_rt.o nft_exthdr.o \ + nft_chain_route.o nf_tables_set-objs := nf_tables_set_core.o \ nft_set_hash.o nft_set_bitmap.o nft_set_rbtree.o
net/netfilter/nf_nat_proto.c +1 -15
@@ -926,20 +926,6 @@ nf_nat_ipv6_out(void *priv, struct sk_buff *skb,return ret; } -static int nat_route_me_harder (struct net *net,struct sk_buff *skb) -{ -#ifdef CONFIG_IPV6_MODULE -const struct nf_ipv6_ops *v6_ops =nf_get_ipv6_ops (); - -if (!v6_ops) -return -EHOSTUNREACH ; - -return v6_ops->route_me_harder (net, skb); -#else -return ip6_route_me_harder (net, skb); -#endif -} -static unsigned int nf_nat_ipv6_local_fn (void *priv,struct sk_buff *skb,const struct nf_hook_state *state)
@@ -959,7 +945,7 @@ nf_nat_ipv6_local_fn(void *priv, struct sk_buff *skb,if (!nf_inet_addr_cmp (&ct->tuplehash[dir].tuple.dst.u3, &ct->tuplehash[!dir].tuple.src.u3)) { - err =nat_route_me_harder (state->net, skb); + err =nf_ip6_route_me_harder (state->net, skb);if (err <0 ) ret =NF_DROP_ERR (err); }
net/netfilter/nf_tables_api.c +2 -0
@@ -7531,6 +7531,7 @@ static int __init nf_tables_module_init(void)if (err <0 ) goto err5; +nft_chain_route_init ();return err; err5:rhltable_destroy (&nft_objname_ht);
@@ -7550,6 +7551,7 @@ static void __exit nf_tables_module_exit(void)nfnetlink_subsys_unregister (&nf_tables_subsys);unregister_netdevice_notifier (&nf_tables_flowtable_notifier);nft_chain_filter_fini (); +nft_chain_route_fini ();unregister_pernet_subsys (&nf_tables_net_ops);cancel_work_sync (&trans_destroy_work);rcu_barrier ();
net/netfilter/nft_chain_route.c new file +169 -0
169 lines not on this page · raw message
Signature · 1 line
2.19.2
Florian Westphal
maintainerPATCH 3/6 netfilter: replace NF_NAT_NEEDED with IS_ENABLED(CONFIG_NF_NAT)
In reply to Florian WestphalNF_NAT_NEEDED is true whenever nat support for either ipv4 or ipv6 is enabled. Now that the af-specific nat configuration switches have been removed, IS_ENABLED(CONFIG_NF_NAT) has the same effect.
--- include/linux/netfilter.h | 2 +- include/net/netfilter/nf_conntrack_expect.h | 2 +- net/netfilter/Kconfig | 5 ----- net/netfilter/nf_conntrack_expect.c | 2 +- net/netfilter/nf_conntrack_netlink.c | 16 ++++++++-------- net/netfilter/nf_conntrack_sip.c | 2 +- net/openvswitch/conntrack.c | 18 +++++++++--------- 7 files changed, 21 insertions(+), 26 deletions(-)
include/linux/netfilter.h +1 -1
@@ -360,7 +360,7 @@ extern struct nf_nat_hook __rcu *nf_nat_hook;static inline void nf_nat_decode_session (struct sk_buff *skb,struct flowi *fl,u_int8_t family) { -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT )struct nf_nat_hook *nat_hook;rcu_read_lock ();
include/net/netfilter/nf_conntrack_expect.h +1 -1
@@ -48,7 +48,7 @@ struct nf_conntrack_expect {/* Expectation class */ unsigned int class; -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT )union nf_inet_addr saved_addr;/* This is the original per-proto part, used to map the * expected connection the way the recipient expects. */
net/netfilter/Kconfig +0 -5
@@ -404,11 +404,6 @@ config NF_NAT forms of full Network Address Port Translation. This can be controlled by iptables, ip6tables or nft. -config NF_NAT_NEEDED - bool - depends on NF_NAT - default y - config NF_NAT_AMANDA tristate depends on NF_CONNTRACK && NF_NAT
net/netfilter/nf_conntrack_expect.c +1 -1
@@ -336,7 +336,7 @@ void nf_ct_expect_init(struct nf_conntrack_expect *exp, unsigned int class, exp->tuple.dst.u.all = *dst; -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT )memset (&exp->saved_addr,0 ,sizeof (exp->saved_addr));memset (&exp->saved_proto,0 ,sizeof (exp->saved_proto));#endif
net/netfilter/nf_conntrack_netlink.c +8 -8
@@ -45,7 +45,7 @@#include <net/netfilter/nf_conntrack_timestamp.h> #include <net/netfilter/nf_conntrack_labels.h> #include <net/netfilter/nf_conntrack_synproxy.h> -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT )#include <net/netfilter/nf_nat.h> #include <net/netfilter/nf_nat_helper.h> #endif
@@ -655,7 +655,7 @@ static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct) +nla_total_size (0 )/* CTA_HELP */ +nla_total_size (NF_CT_HELPER_NAME_LEN )/* CTA_HELP_NAME */ + ctnetlink_secctx_size(ct) -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT ) +2 *nla_total_size (0 )/* CTA_NAT_SEQ_ADJ_ORIG|REPL */ + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */#endif
@@ -1494,7 +1494,7 @@ static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl,return -EOPNOTSUPP ; } -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT ) static int ctnetlink_parse_nat_setup(struct nf_conn *ct, enum nf_nat_manip_type manip,
@@ -1586,7 +1586,7 @@ ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])static int ctnetlink_setup_nat (struct nf_conn *ct,const struct nlattr *const cda[]) { -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT )int ret;if (!cda[CTA_NAT_DST ] && !cda[CTA_NAT_SRC ])
@@ -2369,7 +2369,7 @@ ctnetlink_glue_build_size(const struct nf_conn *ct) +nla_total_size (0 )/* CTA_HELP */ +nla_total_size (NF_CT_HELPER_NAME_LEN )/* CTA_HELP_NAME */ + ctnetlink_secctx_size(ct) -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT ) +2 *nla_total_size (0 )/* CTA_NAT_SEQ_ADJ_ORIG|REPL */ + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */#endif
@@ -2699,7 +2699,7 @@ ctnetlink_exp_dump_expect(struct sk_buff *skb,struct nf_conn *master = exp->master;long timeout = ((long )exp->timeout.expires - (long )jiffies) /HZ ;struct nf_conn_help *help; -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT )struct nlattr *nest_parms;struct nf_conntrack_tuple nat_tuple = {};#endif
@@ -2717,7 +2717,7 @@ ctnetlink_exp_dump_expect(struct sk_buff *skb, CTA_EXPECT_MASTER) < 0) goto nla_put_failure; -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT )if (!nf_inet_addr_cmp (&exp->saved_addr, &any_addr) || exp->saved_proto.all) { nest_parms =nla_nest_start (skb,CTA_EXPECT_NAT |NLA_F_NESTED );
@@ -3180,7 +3180,7 @@ ctnetlink_parse_expect_nat(const struct nlattr *attr, struct nf_conntrack_expect *exp, u_int8_t u3) { -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT )struct nlattr *tb[CTA_EXPECT_NAT_MAX +1 ];struct nf_conntrack_tuple nat_tuple = {};int err;
net/netfilter/nf_conntrack_sip.c +1 -1
@@ -933,7 +933,7 @@ static int set_expected_rtp_rtcp(struct sk_buff *skb, unsigned int protoff,nfct_help (exp->master)->helper !=nfct_help (ct)->helper || exp->class != class) break; -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT ) if (!direct_rtp && (!nf_inet_addr_cmp(&exp->saved_addr, &exp->tuple.dst.u3) || exp->saved_proto.udp.port != exp->tuple.dst.u.udp.port) &&
net/openvswitch/conntrack.c +9 -9
@@ -28,7 +28,7 @@#include <net/netfilter/ipv6/nf_defrag_ipv6.h> #include <net/ipv6_frag.h> -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT )#include <net/netfilter/nf_nat.h> #endif
@@ -73,7 +73,7 @@ struct ovs_conntrack_info {u32 eventmask;/* Mask of 1 << IPCT_*. */ struct md_mark mark;struct md_labels labels; -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT )struct nf_nat_range2 range;/* Only present for SRC NAT and DST NAT. */ #endif };
@@ -719,7 +719,7 @@ static bool skb_nfct_cached(struct net *net,return ct_executed; } -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT )/* Modelled after nf_nat_ipv[46]_fn(). * range is only used for new, uninitialized NAT state. * Returns either NF_ACCEPT or NF_DROP.
@@ -901,7 +901,7 @@ static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,return err; } -#else /* !CONFIG_NF_NAT_NEEDED */ +#else /* !CONFIG_NF_NAT */ static int ovs_ct_nat(struct net *net, struct sw_flow_key *key, const struct ovs_conntrack_info *info, struct sk_buff *skb, struct nf_conn *ct,
@@ -1322,7 +1322,7 @@ static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,return 0 ; } -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT )static int parse_nat (const struct nlattr *attr,struct ovs_conntrack_info *info,bool log) {
@@ -1459,7 +1459,7 @@ static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = { .maxlen = sizeof(struct md_labels) }, [OVS_CT_ATTR_HELPER] = { .minlen = 1, .maxlen = NF_CT_HELPER_NAME_LEN }, -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT ) /* NAT length is checked when parsing the nested attributes. */ [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },#endif
@@ -1537,7 +1537,7 @@ static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,return -EINVAL ; }break ; -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT )case OVS_CT_ATTR_NAT : {int err =parse_nat (a, info, log);
@@ -1650,7 +1650,7 @@ int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,return err; } -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT )static bool ovs_ct_nat_to_attr (const struct ovs_conntrack_info *info,struct sk_buff *skb) {
@@ -1752,7 +1752,7 @@ int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info, nla_put_u32(skb, OVS_CT_ATTR_EVENTMASK, ct_info->eventmask))return -EMSGSIZE ; -#ifdef CONFIG_NF_NAT_NEEDED +#if IS_ENABLED (CONFIG_NF_NAT )if (ct_info->nat && !ovs_ct_nat_to_attr (ct_info, skb))return -EMSGSIZE ;#endif
Signature · 1 line
2.19.2
Florian Westphal
maintainerPATCH 4/6 netfilter: nft_masq: add inet support
In reply to Florian WestphalThis allows use of a single masquerade rule in nat inet family to handle both ipv4 and ipv6.
--- net/netfilter/nft_masq.c | 64 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+)
net/netfilter/nft_masq.c +64 -0
@@ -218,6 +218,61 @@ static inline int nft_masq_module_init_ipv6(void) { return 0; }static inline void nft_masq_module_exit_ipv6 (void ) {}#endif +#ifdef CONFIG_NF_TABLES_INET +static void nft_masq_inet_eval (const struct nft_expr *expr, +struct nft_regs *regs, +const struct nft_pktinfo *pkt) +{ +switch (nft_pf (pkt)) { +case NFPROTO_IPV4 : +return nft_masq_ipv4_eval (expr, regs, pkt); +case NFPROTO_IPV6 : +return nft_masq_ipv6_eval (expr, regs, pkt); + } + +WARN_ON_ONCE (1 ); +} + +static void +nft_masq_inet_destroy (const struct nft_ctx *ctx,const struct nft_expr *expr) +{ +nf_ct_netns_put (ctx->net,NFPROTO_INET ); +} + +static struct nft_expr_type nft_masq_inet_type; +static const struct nft_expr_ops nft_masq_inet_ops = { + .type = &nft_masq_inet_type, + .size =NFT_EXPR_SIZE (sizeof (struct nft_masq )), + .eval = nft_masq_inet_eval, + .init = nft_masq_init, + .destroy = nft_masq_inet_destroy, + .dump = nft_masq_dump, + .validate = nft_masq_validate, +}; + +static struct nft_expr_type nft_masq_inet_type__read_mostly = { + .family =NFPROTO_INET , + .name ="masq" , + .ops = &nft_masq_inet_ops, + .policy = nft_masq_policy, + .maxattr =NFTA_MASQ_MAX , + .owner =THIS_MODULE , +}; + +static int __init nft_masq_module_init_inet (void ) +{ +return nft_register_expr (&nft_masq_inet_type); +} + +static void nft_masq_module_exit_inet (void ) +{ +nft_unregister_expr (&nft_masq_inet_type); +} +#else +static inline int nft_masq_module_init_inet (void ) {return 0 ; } +static inline void nft_masq_module_exit_inet (void ) {} +#endif +static int __init nft_masq_module_init (void ) {int ret;
@@ -226,8 +281,15 @@ static int __init nft_masq_module_init(void)if (ret <0 )return ret; + ret =nft_masq_module_init_inet (); +if (ret <0 ) { +nft_masq_module_exit_ipv6 (); +return ret; + } + ret =nft_register_expr (&nft_masq_ipv4_type);if (ret <0 ) { +nft_masq_module_exit_inet ();nft_masq_module_exit_ipv6 ();return ret; }
@@ -235,6 +297,7 @@ static int __init nft_masq_module_init(void) ret =nf_nat_masquerade_ipv4_register_notifier ();if (ret <0 ) {nft_masq_module_exit_ipv6 (); +nft_masq_module_exit_inet ();nft_unregister_expr (&nft_masq_ipv4_type);return ret; }
@@ -245,6 +308,7 @@ static int __init nft_masq_module_init(void) static void __exit nft_masq_module_exit(void) {nft_masq_module_exit_ipv6 (); +nft_masq_module_exit_inet ();nft_unregister_expr (&nft_masq_ipv4_type);nf_nat_masquerade_ipv4_unregister_notifier (); }
Signature · 1 line
2.19.2
Florian Westphal
maintainerPATCH 5/6 netfilter: nft_redir: add inet support
In reply to Florian Westphalallows to redirect both ipv4 and ipv6 with a single rule in an inet nat table.
--- net/netfilter/nft_redir.c | 61 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)
net/netfilter/nft_redir.c +61 -0
@@ -202,6 +202,55 @@ static struct nft_expr_type nft_redir_ipv6_type __read_mostly = { };#endif +#ifdef CONFIG_NF_TABLES_INET +static void nft_redir_inet_eval (const struct nft_expr *expr, +struct nft_regs *regs, +const struct nft_pktinfo *pkt) +{ +switch (nft_pf (pkt)) { +case NFPROTO_IPV4 : +return nft_redir_ipv4_eval (expr, regs, pkt); +case NFPROTO_IPV6 : +return nft_redir_ipv6_eval (expr, regs, pkt); + } + +WARN_ON_ONCE (1 ); +} + +static void +nft_redir_inet_destroy (const struct nft_ctx *ctx,const struct nft_expr *expr) +{ +nf_ct_netns_put (ctx->net,NFPROTO_INET ); +} + +static struct nft_expr_type nft_redir_inet_type; +static const struct nft_expr_ops nft_redir_inet_ops = { + .type = &nft_redir_inet_type, + .size =NFT_EXPR_SIZE (sizeof (struct nft_redir )), + .eval = nft_redir_inet_eval, + .init = nft_redir_init, + .destroy = nft_redir_inet_destroy, + .dump = nft_redir_dump, + .validate = nft_redir_validate, +}; + +static struct nft_expr_type nft_redir_inet_type__read_mostly = { + .family =NFPROTO_INET , + .name ="redir" , + .ops = &nft_redir_inet_ops, + .policy = nft_redir_policy, + .maxattr =NFTA_MASQ_MAX , + .owner =THIS_MODULE , +}; + +static int __init nft_redir_module_init_inet (void ) +{ +return nft_register_expr (&nft_redir_inet_type); +} +#else +static inline int nft_redir_module_init_inet (void ) {return 0 ; } +#endif +static int __init nft_redir_module_init (void ) {int ret =nft_register_expr (&nft_redir_ipv4_type);
@@ -217,6 +266,15 @@ static int __init nft_redir_module_init(void) }#endif + ret =nft_redir_module_init_inet (); +if (ret <0 ) { +nft_unregister_expr (&nft_redir_ipv4_type); +#ifdef CONFIG_NF_TABLES_IPV6 +nft_unregister_expr (&nft_redir_ipv6_type); +#endif +return ret; + } +return ret; }
@@ -226,6 +284,9 @@ static void __exit nft_redir_module_exit(void)#ifdef CONFIG_NF_TABLES_IPV6 nft_unregister_expr (&nft_redir_ipv6_type);#endif +#ifdef CONFIG_NF_TABLES_INET +nft_unregister_expr (&nft_redir_inet_type); +#endif }module_init (nft_redir_module_init);
Signature · 1 line
2.19.2
Florian Westphal
maintainerPATCH 6/6 kselftests: extend nft_nat with inet family based nat hooks
In reply to Florian WestphalWith older nft versions, this will cause:
[..]
PASS: ipv6 ping to ns1 was ip6 NATted to ns2
/dev/stdin:4:30-31: Error: syntax error, unexpected to, expecting newline or semicolon
ip daddr 10.0.1.99 dnat ip to 10.0.2.99
^^SKIP: inet nat tests
PASS: ip IP masquerade for ns2
[..]
as there is currently no way to detect if nft will be able to parse the inet format.
redirect and masquerade tests need to be skipped in this case for inet too because nft userspace has overzealous family check and rejects their use in the inet family.
--- tools/testing/selftests/netfilter/nft_nat.sh | 130 ++++++++++++++----- 1 file changed, 94 insertions(+), 36 deletions(-)
tools/testing/selftests/netfilter/nft_nat.sh +94 -36
@@ -6,6 +6,7 @@ # Kselftest framework requirement - SKIP code is 4. ksft_skip=4 ret=0 +test_inet_nat=true nft --version > /dev/null 2>&1 if [ $? -ne 0 ];then
@@ -141,17 +142,24 @@ reset_counters() test_local_dnat6() { + local family=$1 local lret=0 + local IPF="" + + if [ $family = "inet" ];then + IPF="ip6" + fi + ip netns exec ns0 nft -f - <<EOF -table ip6 nat { +table $family nat { chain output { type nat hook output priority 0; policy accept; - ip6 daddr dead:1::99 dnat to dead:2::99 + ip6 daddr dead:1::99 dnat $IPF to dead:2::99 } } EOF if [ $? -ne 0 ]; then - echo "SKIP: Could not add add ip6 dnat hook" + echo "SKIP: Could not add add $family dnat hook" return $ksft_skip fi
@@ -201,7 +209,7 @@ EOF fi done - test $lret -eq 0 && echo "PASS: ipv6 ping to ns1 was NATted to ns2" + test $lret -eq 0 && echo "PASS: ipv6 ping to ns1 was $family NATted to ns2" ip netns exec ns0 nft flush chain ip6 nat output return $lret
@@ -209,15 +217,32 @@ EOF test_local_dnat() { + local family=$1 local lret=0 -ip netns exec ns0 nft -f - <<EOF -table ip nat { + local IPF="" + + if [ $family = "inet" ];then + IPF="ip" + fi + +ip netns exec ns0 nft -f - <<EOF 2>/dev/null +table $family nat { chain output { type nat hook output priority 0; policy accept; - ip daddr 10.0.1.99 dnat to 10.0.2.99 + ip daddr 10.0.1.99 dnat $IPF to 10.0.2.99 } } EOF + if [ $? -ne 0 ]; then + if [ $family = "inet" ];then + echo "SKIP: inet nat tests" + test_inet_nat=false + return $ksft_skip + fi + echo "SKIP: Could not add add $family dnat hook" + return $ksft_skip + fi + # ping netns1, expect rewrite to netns2 ip netns exec ns0 ping -q -c 1 10.0.1.99 > /dev/null if [ $? -ne 0 ]; then
@@ -264,9 +289,9 @@ EOF fi done - test $lret -eq 0 && echo "PASS: ping to ns1 was NATted to ns2" + test $lret -eq 0 && echo "PASS: ping to ns1 was $family NATted to ns2" - ip netns exec ns0 nft flush chain ip nat output + ip netns exec ns0 nft flush chain $family nat output reset_counters ip netns exec ns0 ping -q -c 1 10.0.1.99 > /dev/null
@@ -313,7 +338,7 @@ EOF fi done - test $lret -eq 0 && echo "PASS: ping to ns1 OK after nat output chain flush" + test $lret -eq 0 && echo "PASS: ping to ns1 OK after $family nat output chain flush" return $lret }
@@ -321,6 +346,7 @@ EOF test_masquerade6() { + local family=$1 local lret=0 ip netns exec ns0 sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
@@ -351,16 +377,21 @@ test_masquerade6() # add masquerading rule ip netns exec ns0 nft -f - <<EOF -table ip6 nat { +table $family nat { chain postrouting { type nat hook postrouting priority 0; policy accept; meta oif veth0 masquerade } } EOF + if [ $? -ne 0 ]; then + echo "SKIP: Could not add add $family masquerade hook" + return $ksft_skip + fi + ip netns exec ns2 ping -q -c 1 dead:1::99 > /dev/null # ping ns2->ns1 if [ $? -ne 0 ] ; then - echo "ERROR: cannot ping ns1 from ns2 with active ipv6 masquerading" + echo "ERROR: cannot ping ns1 from ns2 with active $family masquerading" lret=1 fi
@@ -397,19 +428,20 @@ EOF fi done - ip netns exec ns0 nft flush chain ip6 nat postrouting + ip netns exec ns0 nft flush chain $family nat postrouting if [ $? -ne 0 ]; then - echo "ERROR: Could not flush ip6 nat postrouting" 1>&2 + echo "ERROR: Could not flush $family nat postrouting" 1>&2 lret=1 fi - test $lret -eq 0 && echo "PASS: IPv6 masquerade for ns2" + test $lret -eq 0 && echo "PASS: $family IPv6 masquerade for ns2" return $lret } test_masquerade() { + local family=$1 local lret=0 ip netns exec ns0 sysctl net.ipv4.conf.veth0.forwarding=1 > /dev/null
@@ -440,16 +472,21 @@ test_masquerade() # add masquerading rule ip netns exec ns0 nft -f - <<EOF -table ip nat { +table $family nat { chain postrouting { type nat hook postrouting priority 0; policy accept; meta oif veth0 masquerade } } EOF + if [ $? -ne 0 ]; then + echo "SKIP: Could not add add $family masquerade hook" + return $ksft_skip + fi + ip netns exec ns2 ping -q -c 1 10.0.1.99 > /dev/null # ping ns2->ns1 if [ $? -ne 0 ] ; then - echo "ERROR: cannot ping ns1 from ns2 with active ip masquerading" + echo "ERROR: cannot ping ns1 from ns2 with active $family masquerading" lret=1 fi
@@ -485,19 +522,20 @@ EOF fi done - ip netns exec ns0 nft flush chain ip nat postrouting + ip netns exec ns0 nft flush chain $family nat postrouting if [ $? -ne 0 ]; then - echo "ERROR: Could not flush nat postrouting" 1>&2 + echo "ERROR: Could not flush $family nat postrouting" 1>&2 lret=1 fi - test $lret -eq 0 && echo "PASS: IP masquerade for ns2" + test $lret -eq 0 && echo "PASS: $family IP masquerade for ns2" return $lret } test_redirect6() { + local family=$1 local lret=0 ip netns exec ns0 sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
@@ -527,16 +565,21 @@ test_redirect6() # add redirect rule ip netns exec ns0 nft -f - <<EOF -table ip6 nat { +table $family nat { chain prerouting { type nat hook prerouting priority 0; policy accept; meta iif veth1 meta l4proto icmpv6 ip6 saddr dead:2::99 ip6 daddr dead:1::99 redirect } } EOF + if [ $? -ne 0 ]; then + echo "SKIP: Could not add add $family redirect hook" + return $ksft_skip + fi + ip netns exec ns2 ping -q -c 1 dead:1::99 > /dev/null # ping ns2->ns1 if [ $? -ne 0 ] ; then - echo "ERROR: cannot ping ns1 from ns2 with active ip6 redirect" + echo "ERROR: cannot ping ns1 from ns2 via ipv6 with active $family redirect" lret=1 fi
@@ -560,19 +603,20 @@ EOF fi done - ip netns exec ns0 nft delete table ip6 nat + ip netns exec ns0 nft delete table $family nat if [ $? -ne 0 ]; then - echo "ERROR: Could not delete ip6 nat table" 1>&2 + echo "ERROR: Could not delete $family nat table" 1>&2 lret=1 fi - test $lret -eq 0 && echo "PASS: IPv6 redirection for ns2" + test $lret -eq 0 && echo "PASS: $family IPv6 redirection for ns2" return $lret } test_redirect() { + local family=$1 local lret=0 ip netns exec ns0 sysctl net.ipv4.conf.veth0.forwarding=1 > /dev/null
@@ -603,16 +647,21 @@ test_redirect() # add redirect rule ip netns exec ns0 nft -f - <<EOF -table ip nat { +table $family nat { chain prerouting { type nat hook prerouting priority 0; policy accept; meta iif veth1 ip protocol icmp ip saddr 10.0.2.99 ip daddr 10.0.1.99 redirect } } EOF + if [ $? -ne 0 ]; then + echo "SKIP: Could not add add $family redirect hook" + return $ksft_skip + fi + ip netns exec ns2 ping -q -c 1 10.0.1.99 > /dev/null # ping ns2->ns1 if [ $? -ne 0 ] ; then - echo "ERROR: cannot ping ns1 from ns2 with active ip redirect" + echo "ERROR: cannot ping ns1 from ns2 with active $family ip redirect" lret=1 fi
@@ -637,13 +686,13 @@ EOF fi done - ip netns exec ns0 nft delete table ip nat + ip netns exec ns0 nft delete table $family nat if [ $? -ne 0 ]; then - echo "ERROR: Could not delete nat table" 1>&2 + echo "ERROR: Could not delete $family nat table" 1>&2 lret=1 fi - test $lret -eq 0 && echo "PASS: IP redirection for ns2" + test $lret -eq 0 && echo "PASS: $family IP redirection for ns2" return $lret }
@@ -746,16 +795,25 @@ if [ $ret -eq 0 ];then fi reset_counters -test_local_dnat -test_local_dnat6 +test_local_dnat ip +test_local_dnat6 ip6 +reset_counters +$test_inet_nat && test_local_dnat inet +$test_inet_nat && test_local_dnat6 inet reset_counters -test_masquerade -test_masquerade6 +test_masquerade ip +test_masquerade6 ip6 +reset_counters +$test_inet_nat && test_masquerade inet +$test_inet_nat && test_masquerade6 inet reset_counters -test_redirect -test_redirect6 +test_redirect ip +test_redirect6 ip6 +reset_counters +$test_inet_nat && test_redirect inet +$test_inet_nat && test_redirect6 inet for i in 0 1 2; do ip netns del ns$i;done
Signature · 1 line
2.19.2
Pablo Neira Ayuso
maintainerQuoted 12 lines · Florian Westphal
This series adds support for the nat chain type in the inet family.
Example:
table inet nat { chain postrouting { type nat hook output priority 0; policy accept; ip6 daddr dead:1::99 snat ip6 to dead:2::99 meta oifname "ppp+" masquerade } }
Quoted 2 lines
This avoids the need to create both ip and ip6 nat tables for the masquerade rule.
Series applied, thanks Florian.