blob: 2c40fc835b2781497802c0b38eada3c980ca3e7c [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001From: Sven Eckelmann <sven@narfation.org>
2Date: Wed, 2 Mar 2022 19:49:46 +0100
3Subject: batman-adv: Don't expect inter-netns unique iflink indices
4
5The ifindex doesn't have to be unique for multiple network namespaces on
6the same machine.
7
8 $ ip netns add test1
9 $ ip -net test1 link add dummy1 type dummy
10 $ ip netns add test2
11 $ ip -net test2 link add dummy2 type dummy
12
13 $ ip -net test1 link show dev dummy1
14 6: dummy1: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
15 link/ether 96:81:55:1e:dd:85 brd ff:ff:ff:ff:ff:ff
16 $ ip -net test2 link show dev dummy2
17 6: dummy2: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
18 link/ether 5a:3c:af:35:07:c3 brd ff:ff:ff:ff:ff:ff
19
20But the batman-adv code to walk through the various layers of virtual
21interfaces uses this assumption because dev_get_iflink handles it
22internally and doesn't return the actual netns of the iflink. And
23dev_get_iflink only documents the situation where ifindex == iflink for
24physical devices.
25
26But only checking for dev->netdev_ops->ndo_get_iflink is also not an option
27because ipoib_get_iflink implements it even when it sometimes returns an
28iflink != ifindex and sometimes iflink == ifindex. The caller must
29therefore make sure itself to check both netns and iflink + ifindex for
30equality. Only when they are equal, a "physical" interface was detected
31which should stop the traversal. On the other hand, vxcan_get_iflink can
32also return 0 in case there was currently no valid peer. In this case, it
33is still necessary to stop.
34
35Fixes: 3d48811b27f5 ("batman-adv: prevent using any virtual device created on batman-adv as hard-interface")
36Fixes: 2b45bb6c3aad ("batman-adv: additional checks for virtual interfaces on top of WiFi")
37Reported-by: Sabrina Dubroca <sd@queasysnail.net>
38Signed-off-by: Sven Eckelmann <sven@narfation.org>
39Origin: upstream, https://git.open-mesh.org/batman-adv.git/commit/0aac7a9fbbbeec25f2f54a9e6d53ea91217ba720
40
41--- a/net/batman-adv/hard-interface.c
42+++ b/net/batman-adv/hard-interface.c
43@@ -157,13 +157,15 @@ static bool batadv_is_on_batman_iface(co
44 return true;
45
46 iflink = dev_get_iflink(net_dev);
47-
48- /* no more parents..stop recursion */
49- if (iflink == 0 || iflink == net_dev->ifindex)
50+ if (iflink == 0)
51 return false;
52
53 parent_net = batadv_getlink_net(net_dev, net);
54
55+ /* iflink to itself, most likely physical device */
56+ if (net == parent_net && iflink == net_dev->ifindex)
57+ return false;
58+
59 /* recurse over the parent device */
60 parent_dev = __dev_get_by_index((struct net *)parent_net, iflink);
61 /* if we got a NULL parent_dev there is something broken.. */
62@@ -223,8 +225,7 @@ static struct net_device *batadv_get_rea
63 return NULL;
64
65 iflink = dev_get_iflink(netdev);
66-
67- if (netdev->ifindex == iflink) {
68+ if (iflink == 0) {
69 dev_hold(netdev);
70 return netdev;
71 }
72@@ -235,6 +236,14 @@ static struct net_device *batadv_get_rea
73
74 net = dev_net(hard_iface->soft_iface);
75 real_net = batadv_getlink_net(netdev, net);
76+
77+ /* iflink to itself, most likely physical device */
78+ if (net == real_net && netdev->ifindex == iflink) {
79+ real_netdev = netdev;
80+ dev_hold(real_netdev);
81+ goto out;
82+ }
83+
84 real_netdev = dev_get_by_index(real_net, iflink);
85
86 out: