From patchwork Wed Mar 2 18:49:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 18473 X-Patchwork-Delegate: sw@simonwunderlich.de Return-Path: X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from diktynna.open-mesh.org (localhost [IPv6:::1]) by diktynna.open-mesh.org (Postfix) with ESMTP id B050784499; Wed, 2 Mar 2022 19:49:59 +0100 (CET) Received: from dvalin.narfation.org (dvalin.narfation.org [IPv6:2a00:17d8:100::8b1]) by diktynna.open-mesh.org (Postfix) with ESMTPS id 2AB9C8001C for ; Wed, 2 Mar 2022 19:49:55 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=narfation.org; s=20121; t=1646246994; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=68B4uCg0jgF4vh/DBlT6MDT+JLmCPlWhaTKMCs2/zwQ=; b=YhVk0TFwBs+2z7Np8A+7YTs/SZqnRcVIKkBKb23dXeOJiBRKkNyyXidWuisCbxcMJ21tbf hQAzeDnHIrXDXX1NU8LJHp7pFIDefLV31xXPAWiyZxP9T6KEy8vN0BYrf+uTK9XVe7cTOa 4ybMbnyyW9NK1SC8bbX2wgmSnfCUI3g= From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Cc: Sven Eckelmann Subject: [PATCH maint 1/3] batman-adv: Request iflink once in batadv-on-batadv check Date: Wed, 2 Mar 2022 19:49:44 +0100 Message-Id: <20220302184946.414934-1-sven@narfation.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=narfation.org header.s=20121 header.b=YhVk0TFw; dmarc=pass (policy=none) header.from=narfation.org; spf=pass (diktynna.open-mesh.org: domain of sven@narfation.org designates 2a00:17d8:100::8b1 as permitted sender) smtp.mailfrom=sven@narfation.org ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1646246995; a=rsa-sha256; cv=none; b=O38qfK7Jat4zt03lFZj9qi6TRuaDw9qY0xd1O8mEE3s16mjQBFh+WXb4va06R0NTAGpkOf nVhg+/7ghyYq9tiZqwBV8Ie2ZPWu2yhR2EpZhNdRs4/tOhy9HwYyDTSGIzSVAT3WyknLK0 GO6Z1Vx27pCrv5wivGfRtVy1kNtAqXo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1646246995; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:dkim-signature; bh=68B4uCg0jgF4vh/DBlT6MDT+JLmCPlWhaTKMCs2/zwQ=; b=JPQYUY91deD4CG04Iq2u0q2wT+ZsXxA8aBL1qPhS0LwisTYyS3bpgtLVQhY5YdQuf9SLMv 453SFe4AvTr/R38J0Gg22O0OXZT2oZM2IArjLIjH1HEiOb4uSpSOOldIt3zvSbmLoJT9EH gT5gVl6VXAOlIftAl89qw5tj199LMDU= Message-ID-Hash: 7S3FIFQG6MMGI64C5EYX3FN3ONVXJA3T X-Message-ID-Hash: 7S3FIFQG6MMGI64C5EYX3FN3ONVXJA3T X-MailFrom: sven@narfation.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-b.a.t.m.a.n.lists.open-mesh.org-0; header-match-b.a.t.m.a.n.lists.open-mesh.org-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: There is no need to call dev_get_iflink multiple times for the same net_device in batadv_is_on_batman_iface. And since some of the .ndo_get_iflink callbacks are dynamic (for example via RCUs like in vxcan_get_iflink), it could easily happen that the returned values are not stable. The pre-checks before __dev_get_by_index are then of course bogus. Fixes: 3d48811b27f5 ("batman-adv: prevent using any virtual device created on batman-adv as hard-interface") Signed-off-by: Sven Eckelmann --- net/batman-adv/hard-interface.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/net/batman-adv/hard-interface.c b/net/batman-adv/hard-interface.c index 59d19097..774a26c5 100644 --- a/net/batman-adv/hard-interface.c +++ b/net/batman-adv/hard-interface.c @@ -149,22 +149,23 @@ static bool batadv_is_on_batman_iface(const struct net_device *net_dev) struct net *net = dev_net(net_dev); struct net_device *parent_dev; struct net *parent_net; + int iflink; bool ret; /* check if this is a batman-adv mesh interface */ if (batadv_softif_is_valid(net_dev)) return true; + iflink = dev_get_iflink(net_dev); + /* no more parents..stop recursion */ - if (dev_get_iflink(net_dev) == 0 || - dev_get_iflink(net_dev) == net_dev->ifindex) + if (iflink == 0 || iflink == net_dev->ifindex) return false; parent_net = batadv_getlink_net(net_dev, net); /* recurse over the parent device */ - parent_dev = __dev_get_by_index((struct net *)parent_net, - dev_get_iflink(net_dev)); + parent_dev = __dev_get_by_index((struct net *)parent_net, iflink); /* if we got a NULL parent_dev there is something broken.. */ if (!parent_dev) { pr_err("Cannot find parent device\n"); From patchwork Wed Mar 2 18:49:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 18474 X-Patchwork-Delegate: sw@simonwunderlich.de Return-Path: X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from diktynna.open-mesh.org (localhost [IPv6:::1]) by diktynna.open-mesh.org (Postfix) with ESMTP id 12AC5844AE; Wed, 2 Mar 2022 19:50:01 +0100 (CET) Received: from dvalin.narfation.org (dvalin.narfation.org [IPv6:2a00:17d8:100::8b1]) by diktynna.open-mesh.org (Postfix) with ESMTPS id 5D1E880CC7 for ; Wed, 2 Mar 2022 19:49:57 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=narfation.org; s=20121; t=1646246997; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KBQwZptHVfZnzZbN2CasZMUlvQIKWSZ+4EEur87U1VY=; b=Vg70RwLqnpLRpzA1+8IYB8gd7SaOe6FbFMshVb6IPDT+dtXklE6udcdME73PJWLCVrClT+ UzWtv0ts82RGc67pb/ARhlcscpnQc2BforiTWaNWhr8Jvp7wbfwV1tmbDKsJ5ddJCueVp7 ZIpjxbziCDvciTV5H9eMuDrc8VFVX8Q= From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Cc: Sven Eckelmann Subject: [PATCH maint 2/3] batman-adv: Request iflink once in batadv_get_real_netdevice Date: Wed, 2 Mar 2022 19:49:45 +0100 Message-Id: <20220302184946.414934-2-sven@narfation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220302184946.414934-1-sven@narfation.org> References: <20220302184946.414934-1-sven@narfation.org> MIME-Version: 1.0 ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=narfation.org header.s=20121 header.b=Vg70RwLq; dmarc=pass (policy=none) header.from=narfation.org; spf=pass (diktynna.open-mesh.org: domain of sven@narfation.org designates 2a00:17d8:100::8b1 as permitted sender) smtp.mailfrom=sven@narfation.org ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1646246997; a=rsa-sha256; cv=none; b=pq3sEPFGW89wXLdpeXZySH/dGonPYY630GFyYRxYXxrz2KD63hgmryj++5NyzJehpXgflX 273JUIN1IGZvM6e85Bzsfxf55O/1vHzJU4aQlaq9dD9aBxvR6gz5ht0Z8ZAzZ2kAVGZBMw FYH+C0ZxCtA2HlwZL3kQGVhnK5s8Sws= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1646246997; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=KBQwZptHVfZnzZbN2CasZMUlvQIKWSZ+4EEur87U1VY=; b=EYYUHlG2KGmINqT14j0VETJhQiaK+G7LMMpj3CuFFxH1hXaVWRciMEAXuegMverrohU6nh QAcfkeOU3jVLYIz+0r2cnMYYCafEiyZIbvgxTcO7wnJNzKH/QoMdQzf3YG4qbrAhpjrWYw wsbHSGm8bsJ9AODCToCdixIQBf3Hiuk= Message-ID-Hash: FC2EN7W2VC2PRYM3HF3JRODLK5RF7R6E X-Message-ID-Hash: FC2EN7W2VC2PRYM3HF3JRODLK5RF7R6E X-MailFrom: sven@narfation.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-b.a.t.m.a.n.lists.open-mesh.org-0; header-match-b.a.t.m.a.n.lists.open-mesh.org-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: There is no need to call dev_get_iflink multiple times for the same net_device in batadv_get_real_netdevice. And since some of the ndo_get_iflink callbacks are dynamic (for example via RCUs like in vxcan_get_iflink), it could easily happen that the returned values are not stable. The pre-checks before __dev_get_by_index are then of course bogus. Fixes: 2b45bb6c3aad ("batman-adv: additional checks for virtual interfaces on top of WiFi") Signed-off-by: Sven Eckelmann --- net/batman-adv/hard-interface.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/net/batman-adv/hard-interface.c b/net/batman-adv/hard-interface.c index 774a26c5..8ed128bb 100644 --- a/net/batman-adv/hard-interface.c +++ b/net/batman-adv/hard-interface.c @@ -215,14 +215,16 @@ static struct net_device *batadv_get_real_netdevice(struct net_device *netdev) struct net_device *real_netdev = NULL; struct net *real_net; struct net *net; - int ifindex; + int iflink; ASSERT_RTNL(); if (!netdev) return NULL; - if (netdev->ifindex == dev_get_iflink(netdev)) { + iflink = dev_get_iflink(netdev); + + if (netdev->ifindex == iflink) { dev_hold(netdev); return netdev; } @@ -232,9 +234,8 @@ static struct net_device *batadv_get_real_netdevice(struct net_device *netdev) goto out; net = dev_net(hard_iface->soft_iface); - ifindex = dev_get_iflink(netdev); real_net = batadv_getlink_net(netdev, net); - real_netdev = dev_get_by_index(real_net, ifindex); + real_netdev = dev_get_by_index(real_net, iflink); out: batadv_hardif_put(hard_iface); From patchwork Wed Mar 2 18:49:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 18475 X-Patchwork-Delegate: sw@simonwunderlich.de Return-Path: X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from diktynna.open-mesh.org (localhost [IPv6:::1]) by diktynna.open-mesh.org (Postfix) with ESMTP id 51657844BD; Wed, 2 Mar 2022 19:50:04 +0100 (CET) Received: from dvalin.narfation.org (dvalin.narfation.org [IPv6:2a00:17d8:100::8b1]) by diktynna.open-mesh.org (Postfix) with ESMTPS id C0909844AD for ; Wed, 2 Mar 2022 19:50:00 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=narfation.org; s=20121; t=1646247000; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pC/A67JIOuNMrtCSjO81k07Gl26xRzGD87ShEJKNZG0=; b=DjGjAr/VMtj018ZCQQ7NTPlhyaP3C5a5dXeM7A24FI7m7riOf49obKpkdGTFFWyQHMgWgv Zv9k47bnIynxWE14qcXx8hfaX68hNS9h6zjQWZx9dD9qsUYLeVrHt6qmLXTQ0XHB0R93Yw cHibyNe0RKWztGVCVj8IG8mJ7R64Pg4= From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Subject: [PATCH maint 3/3] batman-adv: Don't expect inter-netns unique iflink indices Date: Wed, 2 Mar 2022 19:49:46 +0100 Message-Id: <20220302184946.414934-3-sven@narfation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220302184946.414934-1-sven@narfation.org> References: <20220302184946.414934-1-sven@narfation.org> MIME-Version: 1.0 ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=narfation.org header.s=20121 header.b="DjGjAr/V"; dmarc=pass (policy=none) header.from=narfation.org; spf=pass (diktynna.open-mesh.org: domain of sven@narfation.org designates 2a00:17d8:100::8b1 as permitted sender) smtp.mailfrom=sven@narfation.org ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1646247000; a=rsa-sha256; cv=none; b=wo50/jgyPC7VxWYQS/UX7pSX0ZVwf71TXYNYp+MS9HQ/X2P3+HOGvRKNSULXHkgyZysoRU SVKMocXsJ7cN45oDK3bZIDD5Qy8U5xOSGjOlQMHGAI+REWjLxZG+oQqcH5rYUx32S5+OQE qWnP+RpxdxyWO2JtfF0xH666Xp40sco= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1646247000; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=pC/A67JIOuNMrtCSjO81k07Gl26xRzGD87ShEJKNZG0=; b=o5NO9kTQrAwD5lSXz3pk1UUWmzUlUpjn781slMwp0Cc8+iZEHmDJk/rELiC8G33IWcnkzi DmhyaXQyoApZNB82g9mEQn8dsf4TXBfc8PQPxs+9PUdZKIGvpWTYnBsxjngraL3c+QTbBZ ysR7CSulbcCC1rN2Xx1oRPmWlsP5qQY= Message-ID-Hash: HTO6V2Q4YX77SIMAQBGYTXEDTVQYP4BY X-Message-ID-Hash: HTO6V2Q4YX77SIMAQBGYTXEDTVQYP4BY X-MailFrom: sven@narfation.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-b.a.t.m.a.n.lists.open-mesh.org-0; header-match-b.a.t.m.a.n.lists.open-mesh.org-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: Sabrina Dubroca X-Mailman-Version: 3.2.1 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: The ifindex doesn't have to be unique for multiple network namespaces on the same machine. $ ip netns add test1 $ ip -net test1 link add dummy1 type dummy $ ip netns add test2 $ ip -net test2 link add dummy2 type dummy $ ip -net test1 link show dev dummy1 6: dummy1: mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 96:81:55:1e:dd:85 brd ff:ff:ff:ff:ff:ff $ ip -net test2 link show dev dummy2 6: dummy2: mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 5a:3c:af:35:07:c3 brd ff:ff:ff:ff:ff:ff But the batman-adv code to walk through the various layers of virtual interfaces uses this assumption because dev_get_iflink handles it internally and doesn't return the actual netns of the iflink. And dev_get_iflink only documents the situation where ifindex == iflink for physical devices. But only checking for dev->netdev_ops->ndo_get_iflink is also not an option because ipoib_get_iflink implements it even when it sometimes returns an iflink != ifindex and sometimes iflink == ifindex. The caller must therefore make sure itself to check both netns and iflink + ifindex for equality. Only when they are equal, a "physical" interface was detected which should stop the traversal. On the other hand, vxcan_get_iflink can also return 0 in case there was currently no valid peer. In this case, it is still necessary to stop. Fixes: 3d48811b27f5 ("batman-adv: prevent using any virtual device created on batman-adv as hard-interface") Fixes: 2b45bb6c3aad ("batman-adv: additional checks for virtual interfaces on top of WiFi") Reported-by: Sabrina Dubroca Signed-off-by: Sven Eckelmann --- net/batman-adv/hard-interface.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/net/batman-adv/hard-interface.c b/net/batman-adv/hard-interface.c index 8ed128bb..cdb6e542 100644 --- a/net/batman-adv/hard-interface.c +++ b/net/batman-adv/hard-interface.c @@ -157,13 +157,15 @@ static bool batadv_is_on_batman_iface(const struct net_device *net_dev) return true; iflink = dev_get_iflink(net_dev); - - /* no more parents..stop recursion */ - if (iflink == 0 || iflink == net_dev->ifindex) + if (iflink == 0) return false; parent_net = batadv_getlink_net(net_dev, net); + /* iflink to itself, most likely physical device */ + if (net == parent_net && iflink == net_dev->ifindex) + return false; + /* recurse over the parent device */ parent_dev = __dev_get_by_index((struct net *)parent_net, iflink); /* if we got a NULL parent_dev there is something broken.. */ @@ -223,8 +225,7 @@ static struct net_device *batadv_get_real_netdevice(struct net_device *netdev) return NULL; iflink = dev_get_iflink(netdev); - - if (netdev->ifindex == iflink) { + if (iflink == 0) { dev_hold(netdev); return netdev; } @@ -235,6 +236,14 @@ static struct net_device *batadv_get_real_netdevice(struct net_device *netdev) net = dev_net(hard_iface->soft_iface); real_net = batadv_getlink_net(netdev, net); + + /* iflink to itself, most likely physical device */ + if (net == real_net && netdev->ifindex == iflink) { + real_netdev = netdev; + dev_hold(real_netdev); + goto out; + } + real_netdev = dev_get_by_index(real_net, iflink); out: