[PATCH net-next v1 2/6] ipv4: avoid a stack canary in ip_finish_output2()

From: Xuanqiang Luo

Date: Tue Nov 24 2026 - 08:07:51 EST


From: Xuanqiang Luo <luoxuanqiang@xxxxxxxxxx>

ip_neigh_gw4() passes the address of its by-value @daddr argument to
__neigh_create() when the lockless lookup finds no neighbour.

Once inlined, this forces a stack canary (CONFIG_STACKPROTECTOR_STRONG=y)
into its callers, ip_finish_output2() being the one in the transmit path
of every IPv4 packet, although creating a neighbour is rare compared to
finding one.

Change ip_neigh_gw4() to take a pointer to the address, like
ip_neigh_gw6(). All callers have the address in the route, in the packet
or in another object that is not on their stack.

Call it only once in ip_neigh_for_gw() and ipv4_dst_neigh_lookup(), so
that the lookup and the creation are no longer inlined twice there.

Change the type of ipv4_nh in struct bpf_nh_params to __be32, since its
address is now given to ip_neigh_gw4().

Note that skb_do_redirect() and nf_flow_offload_ip_hook() still need a
canary for other reasons.

$ scripts/bloat-o-meter -t vmlinux.old vmlinux.new
add/remove: 0/0 grow/shrink: 1/5 up/down: 3/-846 (-843)
Function old new delta
nf_flow_offload_ip_hook 2065 2068 +3
ip_do_fragment 1441 1425 -16
ipv4_dst_neigh_lookup 684 587 -97
skb_do_redirect 4382 4222 -160
vrf_finish_output 1614 1345 -269
ip_finish_output2 1775 1471 -304
Total: Before=24068916, After=24068073, chg -0.00%

Signed-off-by: Xuanqiang Luo <luoxuanqiang@xxxxxxxxxx>
---
include/linux/filter.h | 2 +-
include/net/route.h | 16 ++++++++--------
net/core/filter.c | 2 +-
net/ipv4/route.c | 15 +++++++--------
net/netfilter/nf_flow_table_ip.c | 4 +++-
5 files changed, 20 insertions(+), 19 deletions(-)

diff --git a/include/linux/filter.h b/include/linux/filter.h
index 4f0cd67caccb..e258ba4741af 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -871,7 +871,7 @@ struct bpf_skb_data_end {
struct bpf_nh_params {
u32 nh_family;
union {
- u32 ipv4_nh;
+ __be32 ipv4_nh;
struct in6_addr ipv6_nh;
};
};
diff --git a/include/net/route.h b/include/net/route.h
index 3fccd31eb74d..1571e1c6082c 100644
--- a/include/net/route.h
+++ b/include/net/route.h
@@ -406,15 +406,15 @@ static inline int ip4_dst_hoplimit(const struct dst_entry *dst)
}

static inline struct neighbour *ip_neigh_gw4(struct net_device *dev,
- __be32 daddr)
+ const __be32 *daddr)
{
struct neighbour *neigh;

- neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)daddr);
+ neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)*daddr);
if (unlikely(!neigh)) {
struct neigh_table *tbl = arp_table(dev_net(dev));

- neigh = __neigh_create(tbl, &daddr, dev, false);
+ neigh = __neigh_create(tbl, daddr, dev, false);
}

return neigh;
@@ -425,17 +425,17 @@ static inline struct neighbour *ip_neigh_for_gw(struct rtable *rt,
bool *is_v6gw)
{
struct net_device *dev = rt->dst.dev;
- struct neighbour *neigh;
+ const __be32 *daddr;

if (likely(rt->rt_gw_family == AF_INET)) {
- neigh = ip_neigh_gw4(dev, rt->rt_gw4);
+ daddr = &rt->rt_gw4;
} else if (rt->rt_gw_family == AF_INET6) {
- neigh = ip_neigh_gw6(dev, &rt->rt_gw6);
*is_v6gw = true;
+ return ip_neigh_gw6(dev, &rt->rt_gw6);
} else {
- neigh = ip_neigh_gw4(dev, ip_hdr(skb)->daddr);
+ daddr = &ip_hdr(skb)->daddr;
}
- return neigh;
+ return ip_neigh_gw4(dev, daddr);
}

#endif /* _ROUTE_H */
diff --git a/net/core/filter.c b/net/core/filter.c
index f42ecfb5841f..8ee023c87c24 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -2376,7 +2376,7 @@ static int bpf_out_neigh_v4(struct net *net, struct sk_buff *skb,
neigh = ip_neigh_gw6(dev, &nh->ipv6_nh);
is_v6gw = true;
} else if (nh->nh_family == AF_INET) {
- neigh = ip_neigh_gw4(dev, nh->ipv4_nh);
+ neigh = ip_neigh_gw4(dev, &nh->ipv4_nh);
} else {
rcu_read_unlock();
goto out_drop;
diff --git a/net/ipv4/route.c b/net/ipv4/route.c
index d0930ae4c5ae..585c10743e1b 100644
--- a/net/ipv4/route.c
+++ b/net/ipv4/route.c
@@ -419,15 +419,14 @@ static struct neighbour *ipv4_dst_neigh_lookup(const struct dst_entry *dst,

rcu_read_lock();
dev = dst_dev_rcu(dst);
- if (likely(rt->rt_gw_family == AF_INET)) {
- n = ip_neigh_gw4(dev, rt->rt_gw4);
- } else if (rt->rt_gw_family == AF_INET6) {
+ if (unlikely(rt->rt_gw_family == AF_INET6)) {
n = ip_neigh_gw6(dev, &rt->rt_gw6);
- } else {
- __be32 pkey;
-
- pkey = skb ? ip_hdr(skb)->daddr : *((__be32 *) daddr);
- n = ip_neigh_gw4(dev, pkey);
+ } else {
+ if (likely(rt->rt_gw_family == AF_INET))
+ daddr = &rt->rt_gw4;
+ else if (skb)
+ daddr = &ip_hdr(skb)->daddr;
+ n = ip_neigh_gw4(dev, daddr);
}

if (!IS_ERR(n) && !refcount_inc_not_zero(&n->refcnt))
diff --git a/net/netfilter/nf_flow_table_ip.c b/net/netfilter/nf_flow_table_ip.c
index 78bba416dd5b..19f62005b517 100644
--- a/net/netfilter/nf_flow_table_ip.c
+++ b/net/netfilter/nf_flow_table_ip.c
@@ -872,7 +872,9 @@ static int nf_flow_queue_xmit4(struct sk_buff *skb,
flow_offload_teardown(flow);
return NF_DROP;
}
- neigh = ip_neigh_gw4(rt->dst.dev, rt_nexthop(rt, ip_addr->ip));
+ neigh = ip_neigh_gw4(rt->dst.dev,
+ rt->rt_gw_family == AF_INET ?
+ &rt->rt_gw4 : &ip_addr->ip);
if (IS_ERR(neigh)) {
flow_offload_teardown(flow);
return NF_DROP;
--
2.43.0