netfilter: replace skb_try_make_writable() by skb_ensure_writable()
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH v2 1/3 netfilter: replace skb_try_make_writable() by skb_ensure_writable()
skb_try_make_writable() only works on clones and uncloned packets might have their network header in paged fragments.
nft_fwd needs to work for the ingress and egress hooks, but the egress hook where skb->data points to the mac header, use skb_network_offset() to deal include the mac header. The flowtable path only works in the ingress hook, it does not need this same handling as nft_fwd does.
---
v2: - update ipv6 flowtable forwarding too to use skb_ensure_writable()
- add skb_network_offset() to include mac header in nft_fwd so this works for egress path too.
net/netfilter/nf_flow_table_ip.c | 4 ++-- net/netfilter/nft_fwd_netdev.c | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-)
net/netfilter/nf_flow_table_ip.c +2 -2
@@ -524,7 +524,7 @@ static int nf_flow_offload_forward(struct nf_flowtable_ctx *ctx,return 0 ; } -if (skb_try_make_writable (skb, thoff + ctx->hdrsize)) +if (skb_ensure_writable (skb, thoff + ctx->hdrsize))return -1 ;flow_offload_refresh (flow_table, flow, false);
@@ -1037,7 +1037,7 @@ static int nf_flow_offload_ipv6_forward(struct nf_flowtable_ctx *ctx,return 0 ; } -if (skb_try_make_writable (skb, thoff + ctx->hdrsize)) +if (skb_ensure_writable (skb, thoff + ctx->hdrsize))return -1 ;flow_offload_refresh (flow_table, flow, false);
net/netfilter/nft_fwd_netdev.c +3 -2
@@ -98,6 +98,7 @@ static void nft_fwd_neigh_eval(const struct nft_expr *expr,struct nft_fwd_neigh *priv =nft_expr_priv (expr);void *addr = ®s->data[priv->sreg_addr];int oif = regs->data[priv->sreg_dev]; +int nhoff =skb_network_offset (skb);unsigned int verdict =NF_STOLEN ;struct sk_buff *skb = pkt->skb;struct net_device *dev;
@@ -111,7 +112,7 @@ static void nft_fwd_neigh_eval(const struct nft_expr *expr, verdict =NFT_BREAK ; goto out; } -if (skb_try_make_writable (skb,sizeof (*iph))) { +if (skb_ensure_writable (skb, nhoff +sizeof (*iph))) { verdict =NF_DROP ; goto out; }
@@ -132,7 +133,7 @@ static void nft_fwd_neigh_eval(const struct nft_expr *expr, verdict =NFT_BREAK ; goto out; } -if (skb_try_make_writable (skb,sizeof (*ip6h))) { +if (skb_ensure_writable (skb, nhoff +sizeof (*ip6h))) { verdict =NF_DROP ; goto out; }
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v2 2/3 netfilter: nft_fwd_netdev: drop packet if no device found when forwarding via neigh
The ttl field has been decremented already and evaluate of this rule would proceed, just drop this packet instead if there is no destination device to forwards this packet. This is exactly what nf_dup already does in this case.
---
v2: no changes
net/netfilter/nft_fwd_netdev.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
net/netfilter/nft_fwd_netdev.c +4 -2
@@ -153,8 +153,10 @@ static void nft_fwd_neigh_eval(const struct nft_expr *expr, } dev =dev_get_by_index_rcu (nft_net (pkt), oif); -if (dev ==NULL ) -return ; +if (dev ==NULL ) { + verdict =NF_DROP ; + goto out; + } skb->dev = dev;skb_clear_tstamp (skb);
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v2 3/3 netfilter: nft_fwd_netdev: use recursion counter in neigh egress path
In reply to Pablo Neira AyusoFrom: Weiming Shi <[email protected]>
nft_fwd_neigh can be used in egress chains (NF_NETDEV_EGRESS). When the forwarding rule targets the same device or two devices forward to each other, neigh_xmit() triggers dev_queue_xmit() which re-enters nf_hook_egress(), causing infinite recursion and stack overflow.
Move the nf_get_nf_dup_skb_recursion() accessor and NF_RECURSION_LIMIT to the shared header nf_dup_netdev.h as a static inline, so that nft_fwd_netdev can use the recursion counter directly without exported function call overhead. Guard neigh_xmit() with the same recursion limit already used in nf_do_netdev_egress().
---
v2: no changes
include/net/netfilter/nf_dup_netdev.h | 13 +++++++++++++ net/netfilter/nf_dup_netdev.c | 16 ---------------- net/netfilter/nft_fwd_netdev.c | 7 +++++++ 3 files changed, 20 insertions(+), 16 deletions(-)
include/net/netfilter/nf_dup_netdev.h +13 -0
@@ -3,10 +3,23 @@#define _NF_DUP_NETDEV_H_#include <net/netfilter/nf_tables.h> +#include <linux/netdevice.h> +#include <linux/sched.h> void nf_dup_netdev_egress (const struct nft_pktinfo *pkt,int oif);void nf_fwd_netdev_egress (const struct nft_pktinfo *pkt,int oif); +#define NF_RECURSION_LIMIT 2 + +static inline u8 *nf_get_nf_dup_skb_recursion (void ) +{ +#ifndef CONFIG_PREEMPT_RT +return this_cpu_ptr (&softnet_data.xmit.nf_dup_skb_recursion); +#else +return ¤t->net_xmit.nf_dup_skb_recursion; +#endif +} +struct nft_offload_ctx ;struct nft_flow_rule ;
net/netfilter/nf_dup_netdev.c +0 -16
@@ -13,22 +13,6 @@#include <net/netfilter/nf_tables_offload.h> #include <net/netfilter/nf_dup_netdev.h> -#define NF_RECURSION_LIMIT 2 - -#ifndef CONFIG_PREEMPT_RT -static u8 *nf_get_nf_dup_skb_recursion (void ) -{ -return this_cpu_ptr (&softnet_data.xmit.nf_dup_skb_recursion); -} -#else - -static u8 *nf_get_nf_dup_skb_recursion (void ) -{ -return ¤t->net_xmit.nf_dup_skb_recursion; -} - -#endif -static void nf_do_netdev_egress (struct sk_buff *skb,struct net_device *dev,enum nf_dev_hooks hook) {
net/netfilter/nft_fwd_netdev.c +7 -0
@@ -152,6 +152,11 @@ static void nft_fwd_neigh_eval(const struct nft_expr *expr, goto out; } +if (*nf_get_nf_dup_skb_recursion () >NF_RECURSION_LIMIT ) { + verdict =NF_DROP ; + goto out; + } + dev =dev_get_by_index_rcu (nft_net (pkt), oif);if (dev ==NULL ) { verdict =NF_DROP ;
@@ -160,7 +165,9 @@ static void nft_fwd_neigh_eval(const struct nft_expr *expr, skb->dev = dev;skb_clear_tstamp (skb); + (*nf_get_nf_dup_skb_recursion ())++;neigh_xmit (neigh_table, dev, addr, skb); + (*nf_get_nf_dup_skb_recursion ())--; out: regs->verdict.code = verdict; }
Signature · 1 line
2.47.3
kernel test robot
build botRe: [PATCH nf,v2 3/3] netfilter: nft_fwd_netdev: use recursion counter in neigh egress path
kernel test robot noticed the following build errors:
Hi Pablo,
kernel test robot noticed the following build errors:
[auto build test ERROR on netfilter-nf/main]
[also build test ERROR on linus/master v7.1-rc2 next-20260505] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Pablo-Neira-Ayuso/netfilter-nft_fwd_netdev-drop-packet-if-no-device-found-when-forwarding-via-neigh/20260425-072424 base: https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git main patch link: https://lore.kernel.org/r/20260422094444.198178-3-pablo%40netfilter.org patch subject: [PATCH nf,v2 3/3] netfilter: nft_fwd_netdev: use recursion counter in neigh egress path config: x86_64-rhel-9.4-ltp (https://download.01.org/0day-ci/archive/20260506/[email protected]/config) compiler: gcc-14 (Debian 14.2.0-19) 14.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260506/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
net/netfilter/nft_fwd_netdev.c: In function 'nft_fwd_neigh_eval':
>> net/netfilter/nft_fwd_netdev.c:101:40: error: 'skb' undeclared (first use in this function)
101 | int nhoff = skb_network_offset(skb);
| ^~~ net/netfilter/nft_fwd_netdev.c:101:40: note: each undeclared identifier is reported only once for each function it appears in
vim +/skb +101 net/netfilter/nft_fwd_netdev.c
93
94 static void nft_fwd_neigh_eval(const struct nft_expr *expr,
95 struct nft_regs *regs,
96 const struct nft_pktinfo *pkt)
97 {
98 struct nft_fwd_neigh *priv = nft_expr_priv(expr);
99 void *addr = ®s->data[priv->sreg_addr];
100 int oif = regs->data[priv->sreg_dev];
> 101 int nhoff = skb_network_offset(skb); 102 unsigned int verdict = NF_STOLEN;
103 struct sk_buff *skb = pkt->skb;
104 struct net_device *dev;
105 int neigh_table;
106
107 switch (priv->nfproto) {
108 case NFPROTO_IPV4: {
109 struct iphdr *iph;
110
111 if (skb->protocol != htons(ETH_P_IP)) {
112 verdict = NFT_BREAK;
113 goto out;
114 }
115 if (skb_ensure_writable(skb, nhoff + sizeof(*iph))) {
116 verdict = NF_DROP;
117 goto out;
118 }
119 iph = ip_hdr(skb);
120 if (iph->ttl <= 1) {
121 verdict = NF_DROP;
122 goto out;
123 }
124
125 ip_decrease_ttl(iph);
126 neigh_table = NEIGH_ARP_TABLE;
127 break;
128 }
129 case NFPROTO_IPV6: {
130 struct ipv6hdr *ip6h;
131
132 if (skb->protocol != htons(ETH_P_IPV6)) {
133 verdict = NFT_BREAK;
134 goto out;
135 }
136 if (skb_ensure_writable(skb, nhoff + sizeof(*ip6h))) {
137 verdict = NF_DROP;
138 goto out;
139 }
140 ip6h = ipv6_hdr(skb);
141 if (ip6h->hop_limit <= 1) {
142 verdict = NF_DROP;
143 goto out;
144 }
145
146 ip6h->hop_limit--;
147 neigh_table = NEIGH_ND_TABLE;
148 break;
149 }
150 default:
151 verdict = NFT_BREAK;
152 goto out;
153 }
154
155 dev = dev_get_by_index_rcu(nft_net(pkt), oif);
156 if (dev == NULL)
157 return;
158
159 skb->dev = dev;
160 skb_clear_tstamp(skb);
161 neigh_xmit(neigh_table, dev, addr, skb);
162 out:
163 regs->verdict.code = verdict;
164 }
165
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki