netfilter updates for net-next
Subsystems (6)
Florian Westphal
maintainercover netfilter updates for net-next
Hello,
This pull request contains changes for the *net-next* tree.
1. Change IPv6 stack to keep conntrack references until ipsec policy checks are done, like ipv4, from Madhu Koriginja.
This update was missed when IPv6 NAT support was added 10 years ago.
2. get rid of old 'compat' structure layout in nf_nat_redirect core and move the conversion to the only user that needs the old layout for abi reasons. From Jeremy Sowden.
3. Compact some common code paths in nft_redir, also from Jeremy.
4. Time to remove the 'default y' knob so iptables 32bit compat interface isn't compiled in by default anymore, from myself.
5. Move ip(6)tables builtin icmp matches to the udptcp one. This has the advantage that icmp/icmpv6 match doesn't load the iptables/ip6tables modules anymore when iptables-nft is used. Also from myself.
The following changes since commit 5c5945dc695c54f2b55a934a10b6c4e220f9c140:
selftests/net: Add SHA256 computation over data sent in tcp_mmap (2023-03-22 15:34:31 +0100)
are available in the Git repository at:
ssh://[email protected]/pub/scm/linux/kernel/git/netfilter/nf-next main
for you to fetch changes up to b0e214d212030fe497d4d150bb3474e50ad5d093:
netfilter: keep conntrack reference until IPsecv6 policy checks are done (2023-03-22 21:50:23 +0100)
----------------------------------------------------------------
Florian Westphal (2):
netfilter: xtables: disable 32bit compat interface by default
xtables: move icmp/icmpv6 logic to xt_tcpudp
Jeremy Sowden (2):
netfilter: nft_redir: use struct nf_nat_range2 throughout and deduplicate eval call-backs
netfilter: nft_masq: deduplicate eval call-backs
Madhu Koriginja (1):
netfilter: keep conntrack reference until IPsecv6 policy checks are done
include/net/netfilter/nf_nat_redirect.h | 3 +- net/dccp/ipv6.c | 1 + net/ipv4/netfilter/ip_tables.c | 68 +------------------- net/ipv6/ip6_input.c | 14 ++-- net/ipv6/netfilter/ip6_tables.c | 68 +------------------- net/ipv6/raw.c | 5 +- net/ipv6/tcp_ipv6.c | 2 + net/ipv6/udp.c | 2 + net/netfilter/Kconfig | 1 - net/netfilter/nf_nat_redirect.c | 71 ++++++++++----------- net/netfilter/nft_masq.c | 75 +++++++++------------- net/netfilter/nft_redir.c | 84 +++++++++--------------- net/netfilter/xt_REDIRECT.c | 10 ++- net/netfilter/xt_tcpudp.c | 110 ++++++++++++++++++++++++++++++++ 14 files changed, 226 insertions(+), 288 deletions(-)
Florian Westphal
maintainerPATCH 1/5 netfilter: nft_redir: use `struct nf_nat_range2` throughout and deduplicate eval call-backs
From: Jeremy Sowden <[email protected]>
nf_nat_redirect_ipv4 takes a struct nf_nat_ipv4_multi_range_compat, but converts it internally to a struct nf_nat_range2. Change the function to take the latter, factor out the code now shared with nf_nat_redirect_ipv6, move the conversion to the xt_REDIRECT module, and update the ipv4 range initialization in the nft_redir module.
Replace a bare hex constant for 127.0.0.1 with a macro.
Remove WARN_ON. nf_nat_setup_info calls nf_ct_is_confirmed:
/* Can't setup nat info for confirmed ct. */ if (nf_ct_is_confirmed(ct)) return NF_ACCEPT;
This means that ct cannot be null or the kernel will crash, and implies that ctinfo is IP_CT_NEW or IP_CT_RELATED.
nft_redir has separate ipv4 and ipv6 call-backs which share much of their code, and an inet one switch containing a switch that calls one of the others based on the family of the packet. Merge the ipv4 and ipv6 ones into the inet one in order to get rid of the duplicate code.
Const-qualify the priv pointer since we don't need to write through it.
Assign priv->flags to the range instead of OR-ing it in.
Set the NF_NAT_RANGE_PROTO_SPECIFIED flag once during init, rather than on every eval.
--- include/net/netfilter/nf_nat_redirect.h | 3 +- net/netfilter/nf_nat_redirect.c | 71 ++++++++++----------- net/netfilter/nft_redir.c | 84 +++++++++---------------- net/netfilter/xt_REDIRECT.c | 10 ++- 4 files changed, 72 insertions(+), 96 deletions(-)
include/net/netfilter/nf_nat_redirect.h +1 -2
@@ -6,8 +6,7 @@#include <uapi/linux/netfilter/nf_nat.h> unsigned int -nf_nat_redirect_ipv4 (struct sk_buff *skb, -const struct nf_nat_ipv4_multi_range_compat *mr, +nf_nat_redirect_ipv4 (struct sk_buff *skb,const struct nf_nat_range2 *range,unsigned int hooknum); unsigned int nf_nat_redirect_ipv6(struct sk_buff *skb, const struct nf_nat_range2 *range,
net/netfilter/nf_nat_redirect.c +32 -39
125 lines not on this page · raw message
net/netfilter/nft_redir.c +31 -53
131 lines not on this page · raw message
net/netfilter/xt_REDIRECT.c +8 -2
@@ -46,7 +46,6 @@ static void redirect_tg_destroy(const struct xt_tgdtor_param *par)nf_ct_netns_put (par->net, par->family); } -/* FIXME: Take multiple ranges --RR */ static int redirect_tg4_check (const struct xt_tgchk_param *par) {const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo;
@@ -65,7 +64,14 @@ static int redirect_tg4_check(const struct xt_tgchk_param *par)static unsigned int redirect_tg4 (struct sk_buff *skb,const struct xt_action_param *par) { -return nf_nat_redirect_ipv4 (skb, par->targinfo,xt_hooknum (par)); +const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo; +struct nf_nat_range2 range = { + .flags = mr->range[0 ].flags, + .min_proto = mr->range[0 ].min, + .max_proto = mr->range[0 ].max, + }; + +return nf_nat_redirect_ipv4 (skb, &range,xt_hooknum (par)); }static struct xt_target redirect_tg_reg[]__read_mostly = {
Signature · 1 line
2.39.2
Re: [PATCH net-next 1/5] netfilter: nft_redir: use `struct nf_nat_range2` throughout and deduplicate eval call-backs
This series was applied to netdev/net-next.git (main) by Florian Westphal <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Florian Westphal <[email protected]>:
Quoted 9 lines · On Wed, 22 Mar 2023 22:07:58 +0100 you wrote:
From: Jeremy Sowden <[email protected]>
nf_nat_redirect_ipv4takes astruct nf_nat_ipv4_multi_range_compat, but converts it internally to astruct nf_nat_range2. Change the function to take the latter, factor out the code now shared withnf_nat_redirect_ipv6, move the conversion to thext_REDIRECTmodule, and update the ipv4 range initialization in thenft_redirmodule.[...]
Here is the summary with links:
- [net-next,1/5] netfilter: nft_redir: use struct nf_nat_range2 throughout and deduplicate eval call-backs
https://git.kernel.org/netdev/net-next/c/6f56ad1b9232 - [net-next,2/5] netfilter: nft_masq: deduplicate eval call-backs https://git.kernel.org/netdev/net-next/c/f6ca5d5ed7ec - [net-next,3/5] netfilter: xtables: disable 32bit compat interface by default https://git.kernel.org/netdev/net-next/c/bde7170a04d6 - [net-next,4/5] xtables: move icmp/icmpv6 logic to xt_tcpudp https://git.kernel.org/netdev/net-next/c/36ce9982ef2f - [net-next,5/5] netfilter: keep conntrack reference until IPsecv6 policy checks are done https://git.kernel.org/netdev/net-next/c/b0e214d21203
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Florian Westphal
maintainerPATCH 2/5 netfilter: nft_masq: deduplicate eval call-backs
In reply to Florian WestphalFrom: Jeremy Sowden <[email protected]>
nft_masq has separate ipv4 and ipv6 call-backs which share much of their code, and an inet one switch containing a switch that calls one of the others based on the family of the packet. Merge the ipv4 and ipv6 ones into the inet one in order to get rid of the duplicate code.
Const-qualify the priv pointer since we don't need to write through it.
--- net/netfilter/nft_masq.c | 75 ++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 46 deletions(-)
net/netfilter/nft_masq.c +29 -46
@@ -96,23 +96,39 @@ static int nft_masq_dump(struct sk_buff *skb,return -1 ; } -static void nft_masq_ipv4_eval (const struct nft_expr *expr, -struct nft_regs *regs, -const struct nft_pktinfo *pkt) +static void nft_masq_eval (const struct nft_expr *expr, +struct nft_regs *regs, +const struct nft_pktinfo *pkt) { -struct nft_masq *priv =nft_expr_priv (expr); +const struct nft_masq *priv =nft_expr_priv (expr);struct nf_nat_range2 range;memset (&range,0 ,sizeof (range)); range.flags = priv->flags;if (priv->sreg_proto_min) { - range.min_proto.all = (__force __be16 )nft_reg_load16 ( - ®s->data[priv->sreg_proto_min]); - range.max_proto.all = (__force __be16 )nft_reg_load16 ( - ®s->data[priv->sreg_proto_max]); + range.min_proto.all = (__force __be16 ) +nft_reg_load16 (®s->data[priv->sreg_proto_min]); + range.max_proto.all = (__force __be16 ) +nft_reg_load16 (®s->data[priv->sreg_proto_max]); + } + +switch (nft_pf (pkt)) { +case NFPROTO_IPV4 : + regs->verdict.code =nf_nat_masquerade_ipv4 (pkt->skb, +nft_hook (pkt), + &range, +nft_out (pkt)); +break ; +#ifdef CONFIG_NF_TABLES_IPV6 +case NFPROTO_IPV6 : + regs->verdict.code =nf_nat_masquerade_ipv6 (pkt->skb, &range, +nft_out (pkt)); +break ; +#endif +default : +WARN_ON_ONCE (1 ); +break ; } - regs->verdict.code =nf_nat_masquerade_ipv4 (pkt->skb,nft_hook (pkt), - &range,nft_out (pkt)); } static void
@@ -125,7 +141,7 @@ static struct nft_expr_type nft_masq_ipv4_type;static const struct nft_expr_ops nft_masq_ipv4_ops = { .type = &nft_masq_ipv4_type, .size =NFT_EXPR_SIZE (sizeof (struct nft_masq )), - .eval = nft_masq_ipv4_eval, + .eval = nft_masq_eval, .init = nft_masq_init, .destroy = nft_masq_ipv4_destroy, .dump = nft_masq_dump,
@@ -143,25 +159,6 @@ static struct nft_expr_type nft_masq_ipv4_type __read_mostly = { };#ifdef CONFIG_NF_TABLES_IPV6 -static void nft_masq_ipv6_eval (const struct nft_expr *expr, -struct nft_regs *regs, -const struct nft_pktinfo *pkt) -{ -struct nft_masq *priv =nft_expr_priv (expr); -struct nf_nat_range2 range; - -memset (&range,0 ,sizeof (range)); - range.flags = priv->flags; -if (priv->sreg_proto_min) { - range.min_proto.all = (__force __be16 )nft_reg_load16 ( - ®s->data[priv->sreg_proto_min]); - range.max_proto.all = (__force __be16 )nft_reg_load16 ( - ®s->data[priv->sreg_proto_max]); - } - regs->verdict.code =nf_nat_masquerade_ipv6 (pkt->skb, &range, -nft_out (pkt)); -} -static void nft_masq_ipv6_destroy (const struct nft_ctx *ctx,const struct nft_expr *expr) {
@@ -172,7 +169,7 @@ static struct nft_expr_type nft_masq_ipv6_type;static const struct nft_expr_ops nft_masq_ipv6_ops = { .type = &nft_masq_ipv6_type, .size =NFT_EXPR_SIZE (sizeof (struct nft_masq )), - .eval = nft_masq_ipv6_eval, + .eval = nft_masq_eval, .init = nft_masq_init, .destroy = nft_masq_ipv6_destroy, .dump = nft_masq_dump,
@@ -204,20 +201,6 @@ 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) {
@@ -228,7 +211,7 @@ 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, + .eval = nft_masq_eval, .init = nft_masq_init, .destroy = nft_masq_inet_destroy, .dump = nft_masq_dump,
Signature · 1 line
2.39.2
Florian Westphal
maintainerPATCH 3/5 netfilter: xtables: disable 32bit compat interface by default
In reply to Florian WestphalThis defaulted to 'y' because before this knob existed the 32bit compat layer was always compiled in if CONFIG_COMPAT was set.
32bit iptables on 64bit kernel isn't common anymore, so remove the default-y now.
--- net/netfilter/Kconfig | 1 - 1 file changed, 1 deletion(-)
net/netfilter/Kconfig +0 -1
@@ -753,7 +753,6 @@ if NETFILTER_XTABLES config NETFILTER_XTABLES_COMPAT bool "Netfilter Xtables 32bit support" depends on COMPAT - default y help This option provides a translation layer to run 32bit arp,ip(6),ebtables binaries on 64bit kernels.
Signature · 1 line
2.39.2
Florian Westphal
maintainerPATCH 4/5 xtables: move icmp/icmpv6 logic to xt_tcpudp
In reply to Florian Westphalicmp/icmp6 matches are baked into ip(6)_tables.ko.
This means that even if iptables-nft is used, a rule like "-p icmp --icmp-type 1" will load the ip(6)tables modules.
Move them to xt_tcpdudp.ko instead to avoid this.
This will also allow to eventually add kconfig knobs to build kernels that support iptables-nft but not iptables-legacy (old set/getsockopt interface).
--- net/ipv4/netfilter/ip_tables.c | 68 +------------------- net/ipv6/netfilter/ip6_tables.c | 68 +------------------- net/netfilter/xt_tcpudp.c | 110 ++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 134 deletions(-)
net/ipv4/netfilter/ip_tables.c +1 -67
@@ -14,7 +14,6 @@#include <linux/vmalloc.h> #include <linux/netdevice.h> #include <linux/module.h> -#include <linux/icmp.h> #include <net/ip.h> #include <net/compat.h> #include <linux/uaccess.h>
@@ -31,7 +30,6 @@MODULE_LICENSE ("GPL" );MODULE_AUTHOR ("Netfilter Core Team <[email protected]>" );MODULE_DESCRIPTION ("IPv4 packet filter" ); -MODULE_ALIAS ("ipt_icmp" );void *ipt_alloc_initial_table (const struct xt_table *info) {
@@ -1799,52 +1797,6 @@ void ipt_unregister_table_exit(struct net *net, const char *name)__ipt_unregister_table (net, table); } -/* Returns 1 if the type and code is matched by the range, 0 otherwise */ -static inline bool -icmp_type_code_match (u_int8_t test_type,u_int8_t min_code,u_int8_t max_code, -u_int8_t type,u_int8_t code, -bool invert) -{ -return ((test_type ==0xFF ) || - (type == test_type && code >= min_code && code <= max_code)) - ^ invert; -} - -static bool -icmp_match (const struct sk_buff *skb,struct xt_action_param *par) -{ -const struct icmphdr *ic; -struct icmphdr _icmph; -const struct ipt_icmp *icmpinfo = par->matchinfo; - -/* Must not be a fragment. */ -if (par->fragoff !=0 ) -return false; - - ic =skb_header_pointer (skb, par->thoff,sizeof (_icmph), &_icmph); -if (ic ==NULL ) { -/* We've been asked to examine this packet, and we -* can't. Hence, no choice but to drop. -*/ - par->hotdrop = true; -return false; - } - -return icmp_type_code_match (icmpinfo->type, - icmpinfo->code[0 ], - icmpinfo->code[1 ], - ic->type, ic->code, - !!(icmpinfo->invflags&IPT_ICMP_INV )); -} - -static int icmp_checkentry (const struct xt_mtchk_param *par) -{ -const struct ipt_icmp *icmpinfo = par->matchinfo; - -/* Must specify no unknown invflags */ -return (icmpinfo->invflags & ~IPT_ICMP_INV ) ? -EINVAL :0 ; -} -static struct xt_target ipt_builtin_tg[]__read_mostly = { { .name =XT_STANDARD_TARGET ,
@@ -1875,18 +1827,6 @@ static struct nf_sockopt_ops ipt_sockopts = { .owner = THIS_MODULE, }; -static struct xt_match ipt_builtin_mt[]__read_mostly = { - { - .name ="icmp" , - .match = icmp_match, - .matchsize =sizeof (struct ipt_icmp ), - .checkentry = icmp_checkentry, - .proto =IPPROTO_ICMP , - .family =NFPROTO_IPV4 , - .me =THIS_MODULE , - }, -}; -static int __net_init ip_tables_net_init (struct net *net) {return xt_proto_init (net,NFPROTO_IPV4 );
@@ -1914,19 +1854,14 @@ static int __init ip_tables_init(void) ret =xt_register_targets (ipt_builtin_tg,ARRAY_SIZE (ipt_builtin_tg));if (ret <0 ) goto err2; - ret =xt_register_matches (ipt_builtin_mt,ARRAY_SIZE (ipt_builtin_mt)); -if (ret <0 ) - goto err4;/* Register setsockopt */ ret =nf_register_sockopt (&ipt_sockopts);if (ret <0 ) - goto err5; + goto err4;return 0 ; -err5: -xt_unregister_matches (ipt_builtin_mt,ARRAY_SIZE (ipt_builtin_mt)); err4:xt_unregister_targets (ipt_builtin_tg,ARRAY_SIZE (ipt_builtin_tg)); err2:
@@ -1939,7 +1874,6 @@ static void __exit ip_tables_fini(void) {nf_unregister_sockopt (&ipt_sockopts); -xt_unregister_matches (ipt_builtin_mt,ARRAY_SIZE (ipt_builtin_mt));xt_unregister_targets (ipt_builtin_tg,ARRAY_SIZE (ipt_builtin_tg));unregister_pernet_subsys (&ip_tables_net_ops); }
net/ipv6/netfilter/ip6_tables.c +1 -67
111 lines not on this page · raw message
net/netfilter/xt_tcpudp.c +110 -0
134 lines not on this page · raw message
Signature · 1 line
2.39.2
Florian Westphal
maintainerPATCH 5/5 netfilter: keep conntrack reference until IPsecv6 policy checks are done
In reply to Florian WestphalFrom: Madhu Koriginja <[email protected]>
Keep the conntrack reference until policy checks have been performed for IPsec V6 NAT support, just like ipv4.
The reference needs to be dropped before a packet is queued to avoid having the conntrack module unloadable.
--- net/dccp/ipv6.c | 1 + net/ipv6/ip6_input.c | 14 ++++++-------- net/ipv6/raw.c | 5 ++--- net/ipv6/tcp_ipv6.c | 2 ++ net/ipv6/udp.c | 2 ++ 5 files changed, 13 insertions(+), 11 deletions(-)
net/dccp/ipv6.c +1 -0
@@ -784,6 +784,7 @@ static int dccp_v6_rcv(struct sk_buff *skb)if (!xfrm6_policy_check (sk,XFRM_POLICY_IN , skb)) goto discard_and_relse; +nf_reset_ct (skb);return __sk_receive_skb (sk, skb,1 , dh->dccph_doff *4 , refcounted) ?-1 :0 ;
net/ipv6/ip6_input.c +6 -8
@@ -404,10 +404,6 @@ void ip6_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int nexthdr,/* Only do this once for first final protocol */ have_final = true; -/* Free reference early: we don't need it any more, -and it may hold ip_conntrack module loaded -indefinitely. */ -nf_reset_ct (skb);skb_postpull_rcsum (skb,skb_network_header (skb),skb_network_header_len (skb));
@@ -430,10 +426,12 @@ void ip6_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int nexthdr, goto discard; } } -if (!(ipprot->flags &INET6_PROTO_NOPOLICY ) && - !xfrm6_policy_check (NULL ,XFRM_POLICY_IN , skb)) { -SKB_DR_SET (reason,XFRM_POLICY ); - goto discard; +if (!(ipprot->flags &INET6_PROTO_NOPOLICY )) { +if (!xfrm6_policy_check (NULL ,XFRM_POLICY_IN , skb)) { +SKB_DR_SET (reason,XFRM_POLICY ); + goto discard; + } +nf_reset_ct (skb); } ret = INDIRECT_CALL_2(ipprot->handler, tcp_v6_rcv, udpv6_rcv,
net/ipv6/raw.c +2 -3
@@ -194,10 +194,8 @@ static bool ipv6_raw_deliver(struct sk_buff *skb, int nexthdr)struct sk_buff *clone =skb_clone (skb,GFP_ATOMIC );/* Not releasing hash table! */ -if (clone) { -nf_reset_ct (clone); +if (clone)rawv6_rcv (sk, clone); - } } }rcu_read_unlock ();
@@ -391,6 +389,7 @@ int rawv6_rcv(struct sock *sk, struct sk_buff *skb)kfree_skb_reason (skb,SKB_DROP_REASON_XFRM_POLICY );return NET_RX_DROP ; } +nf_reset_ct (skb);if (!rp->checksum) skb->ip_summed =CHECKSUM_UNNECESSARY ;
net/ipv6/tcp_ipv6.c +2 -0
@@ -1723,6 +1723,8 @@ INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb)if (drop_reason) goto discard_and_relse; +nf_reset_ct (skb); +if (tcp_filter (sk, skb)) { drop_reason =SKB_DROP_REASON_SOCKET_FILTER ; goto discard_and_relse;
net/ipv6/udp.c +2 -0
@@ -704,6 +704,7 @@ static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb) drop_reason =SKB_DROP_REASON_XFRM_POLICY ; goto drop; } +nf_reset_ct (skb);if (static_branch_unlikely (&udpv6_encap_needed_key) && up->encap_type) {int (*encap_rcv)(struct sock *sk,struct sk_buff *skb);
@@ -1027,6 +1028,7 @@ int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,if (!xfrm6_policy_check (NULL ,XFRM_POLICY_IN , skb)) goto discard; +nf_reset_ct (skb);if (udp_lib_checksum_complete (skb)) goto csum_error;
Signature · 1 line
2.39.2