Skip to content

Commit 2260059

Browse files
lxinkuba-moo
authored andcommitted
ipv4: give an IPv4 dev to blackhole_netdev
After commit 8d7017f ("blackhole_netdev: use blackhole_netdev to invalidate dst entries"), blackhole_netdev was introduced to invalidate dst cache entries on the TX path whenever the cache times out or is flushed. When two UDP sockets (sk1 and sk2) send messages to the same destination simultaneously, they are using the same dst cache. If the dst cache is invalidated on one path (sk2) while the other (sk1) is still transmitting, sk1 may try to use the invalid dst entry. CPU1 CPU2 udp_sendmsg(sk1) udp_sendmsg(sk2) udp_send_skb() ip_output() <--- dst timeout or flushed dst_dev_put() ip_finish_output2() ip_neigh_for_gw() This results in a scenario where ip_neigh_for_gw() returns -EINVAL because blackhole_dev lacks an in_dev, which is needed to initialize the neigh in arp_constructor(). This error is then propagated back to userspace, breaking the UDP application. The patch fixes this issue by assigning an in_dev to blackhole_dev for IPv4, similar to what was done for IPv6 in commit e5f80fc ("ipv6: give an IPv6 dev to blackhole_netdev"). This ensures that even when the dst entry is invalidated with blackhole_dev, it will not fail to create the neigh entry. As devinet_init() is called ealier than blackhole_netdev_init() in system booting, it can not assign the in_dev to blackhole_dev in devinet_init(). As Paolo suggested, add a separate late_initcall() in devinet.c to ensure inet_blackhole_dev_init() is called after blackhole_netdev_init(). Fixes: 8d7017f ("blackhole_netdev: use blackhole_netdev to invalidate dst entries") Signed-off-by: Xin Long <lucien.xin@gmail.com> Reviewed-by: Eric Dumazet <edumazet@google.com> Link: https://patch.msgid.link/3000792d45ca44e16c785ebe2b092e610e5b3df1.1728499633.git.lucien.xin@gmail.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 1d227fc commit 2260059

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

net/ipv4/devinet.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -298,17 +298,19 @@ static struct in_device *inetdev_init(struct net_device *dev)
298298
/* Account for reference dev->ip_ptr (below) */
299299
refcount_set(&in_dev->refcnt, 1);
300300

301-
err = devinet_sysctl_register(in_dev);
302-
if (err) {
303-
in_dev->dead = 1;
304-
neigh_parms_release(&arp_tbl, in_dev->arp_parms);
305-
in_dev_put(in_dev);
306-
in_dev = NULL;
307-
goto out;
301+
if (dev != blackhole_netdev) {
302+
err = devinet_sysctl_register(in_dev);
303+
if (err) {
304+
in_dev->dead = 1;
305+
neigh_parms_release(&arp_tbl, in_dev->arp_parms);
306+
in_dev_put(in_dev);
307+
in_dev = NULL;
308+
goto out;
309+
}
310+
ip_mc_init_dev(in_dev);
311+
if (dev->flags & IFF_UP)
312+
ip_mc_up(in_dev);
308313
}
309-
ip_mc_init_dev(in_dev);
310-
if (dev->flags & IFF_UP)
311-
ip_mc_up(in_dev);
312314

313315
/* we can receive as soon as ip_ptr is set -- do this last */
314316
rcu_assign_pointer(dev->ip_ptr, in_dev);
@@ -347,6 +349,19 @@ static void inetdev_destroy(struct in_device *in_dev)
347349
in_dev_put(in_dev);
348350
}
349351

352+
static int __init inet_blackhole_dev_init(void)
353+
{
354+
int err = 0;
355+
356+
rtnl_lock();
357+
if (!inetdev_init(blackhole_netdev))
358+
err = -ENOMEM;
359+
rtnl_unlock();
360+
361+
return err;
362+
}
363+
late_initcall(inet_blackhole_dev_init);
364+
350365
int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b)
351366
{
352367
const struct in_ifaddr *ifa;

0 commit comments

Comments
 (0)