From patchwork Mon Dec 9 14:01:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Liang X-Patchwork-Id: 18715 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 C2D6F83DF4 for ; Mon, 9 Dec 2024 17:49:08 +0100 (CET) ARC-Seal: i=2; cv=pass; a=rsa-sha256; d=open-mesh.org; s=20121; t=1733762948; b=Pz5oZOOsgEV8BY5jL+qwxVd/P9dcVSo2r5EbGbODvfVwN/6GaEDkDWgGh5qQl1G9rxaW7 0ePetyeDTje/KRaVIBOQ/6xnAjfzR7pqIbDgwrEDlIeBrj6+uPrR5IYcJx2uRMOOVHxddNR MQISmgo5+L8ngNVYOIwZokBZPho9JSA= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1733762948; h=from : sender : reply-to : subject : date : message-id : to : cc : mime-version : content-type : content-transfer-encoding : content-id : content-description : resent-date : resent-from : resent-sender : resent-to : resent-cc : resent-message-id : in-reply-to : references : list-id : list-help : list-unsubscribe : list-subscribe : list-post : list-owner : list-archive; bh=93uG6ogDA0suRI92nCq7Qt11rTzcUQ+bpSh6BVGj6ZI=; b=Vu6NTno+W5VsporqfTQoHutq7nT5rKT5Ocb3VqvN3iRiE2bCvMLdUzqrzzq05kp/I/jBv apPVyHv79EmZIAIXUFU4PPXS55GGK3etKyAnYjEkKtHDxVege+oRzSSwESRTPM3RXppC8Bc bfCERZfVflm/Mc+ZrnSu9kseIChdCwI= ARC-Authentication-Results: i=2; open-mesh.org; dkim=pass header.d=gmail.com; arc=pass; dmarc=pass (Used From Domain Record) header.from=gmail.com policy.dmarc=quarantine Authentication-Results: open-mesh.org; dkim=pass header.d=gmail.com; arc=pass; dmarc=pass (Used From Domain Record) header.from=gmail.com policy.dmarc=quarantine Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by diktynna.open-mesh.org (Postfix) with ESMTPS id 0559B82F27 for ; Mon, 9 Dec 2024 15:02:38 +0100 (CET) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1733752959; 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=93uG6ogDA0suRI92nCq7Qt11rTzcUQ+bpSh6BVGj6ZI=; b=eVY7i8cyft3SPFjI4xfUUg274SeMvPrIruq4Do4OEUuWrBLiwzNuWLda3beN6lusLoisX+ wOGJ9jWi6LNreL+6tAXk53N2TZNYeWMrt8obxiL4DPMXCdMjP26OdHLXpPeyH+Y8Iz2aTc rXm22QeLBFVisdfMYkZ4XyTySuPvYnM= ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1733752959; a=rsa-sha256; cv=none; b=SBRmsCsCmyV2+bfbkbn4Z6WEe3E+1QF6NQi3zSJm8P0R0ABe7TGh0WClO8HiZbOr0LD0zy oYY71W8cr43OvN0xdbul4ieqMXpZuk8EQot9xzGDkaO3hKw34o4D5ofJMm719ARADe/Xsu /hAtRLyj886xhD/ohKDkO6o14O6Byhk= ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=gmail.com header.s=20230601 header.b=JR7I++ZT; spf=pass (diktynna.open-mesh.org: domain of shaw.leon@gmail.com designates 2607:f8b0:4864:20::629 as permitted sender) smtp.mailfrom=shaw.leon@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-2163bd70069so13581815ad.0 for ; Mon, 09 Dec 2024 06:02:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733752957; x=1734357757; darn=lists.open-mesh.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=93uG6ogDA0suRI92nCq7Qt11rTzcUQ+bpSh6BVGj6ZI=; b=JR7I++ZTNh2rYVaN3sTPkRFVD2vCtCOK2UgjYrXdu+dS6cxPv1EwJJoEO48DUw+MqC WsYHMC8F0CkiT1DVw9mbgrWARib6wQ/YY2EPEhR9PTa4TWYgMea6edL48m8N5/c397/H t//u3faNGFQxG8/CYfndAeT253peTSwmKcm/97JVpSxlxeTWLUJAk1aeYhiIwExC7vrt XtLuY2XmCE4HiIHEd7l7/OrCddu8kHNa+Luq7LvtKJSeQfkvAmvt9FeWkvbZjraNbfSI cVxLVq+jLkobcKz0mp6lCzoap/DNk5XuF1F3xshe1y35xcMCFK2rSwUNa5w6Zl1eAQhN uASA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733752957; x=1734357757; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=93uG6ogDA0suRI92nCq7Qt11rTzcUQ+bpSh6BVGj6ZI=; b=KM76j0WGwwJQ7V8bQzRq8y5U/mbpqQlRUApzD/oNPpBd1C+zEMaAP81ioa/zXIM5fV UfQ7qRc7CSslhoR/Z9t/8mjexYtMAWH7pP78hw9fegsf4IH6V8EuhHDI58XXVWIQKjbE FEcopAeCVaJ8ExyA+zmQBDkEDCkqbLtOcxIW/fA1bUkuPXF4+R9IDCMpYQIDDXLzAa65 PPppyUYs98HqJY2SyiaGz+6naQGc777pAovkfUlmSCVXbE4pmQoxSgsRxSOD0jgTy+a6 K7Bf1wny6Qlhx36JantJldtVjJcGj7Tl9G+LxwbYaJ0TX2Ggo6P8uFTrdUBD5o+L2J5R jjPQ== X-Forwarded-Encrypted: i=1; AJvYcCVh+qCUEO32ye7a4qvAaWmjAy/D9MqJ2ZLtDDXOwEBneEquoKe4kl19n1IezTc9qySZA6CHCHaiyDKUPg==@lists.open-mesh.org X-Gm-Message-State: AOJu0YwW3cEriYsCzTVkzjBdAvdhU2nWnq4perE3luqxiOxNc0eYyt1x qt5Zh4UBzob7WdvETKdzz1P3fO4AvaqWZlT2AXDGcDxTQWXaE8sX X-Gm-Gg: ASbGncsvNzs/yaqc5UccwrK25aU1LcEXd4Aritb8bnIgzRGSiz2ribdZPgBW+bVh7Ps MC1rJfF/1Ce7niB3zrHRKeeTsd2LIBuyvNmsvwXo/3DuA3A4XS6gbKdOJmI7w3WauW0GboW2tRB qfqtYuSgQ16qF1rfP8ZRxjDYmCcK0saZvkTWB7jW55LWrxu1ICsFX27b8up30aa1XfLRhFsYfve /3bNuDScoMrLkkI9jJXc9h0U06EWrsWzYAZ2iO+MD6FLjU= X-Google-Smtp-Source: AGHT+IEJ2QVBGY2MGcE4mBFIU/c+dzWKa28cbDvoqfUFybFT/JmX0nfLyJJqRhwpUbbXsTp1UoSovg== X-Received: by 2002:a17:902:cf08:b0:205:6a9b:7e3e with SMTP id d9443c01a7336-2166a0b59acmr8694095ad.56.1733752957447; Mon, 09 Dec 2024 06:02:37 -0800 (PST) Received: from nova-ws.. ([103.167.140.11]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-216221db645sm49605645ad.46.2024.12.09.06.02.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Dec 2024 06:02:36 -0800 (PST) From: Xiao Liang To: netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, Kuniyuki Iwashima , Jakub Kicinski , Donald Hunter Cc: "David S. Miller" , David Ahern , Eric Dumazet , Paolo Abeni , Ido Schimmel , Andrew Lunn , Simon Horman , Shuah Khan , Jiri Pirko , Hangbin Liu , linux-rdma@vger.kernel.org, linux-can@vger.kernel.org, osmocom-net-gprs@lists.osmocom.org, bpf@vger.kernel.org, linux-ppp@vger.kernel.org, wireguard@lists.zx2c4.com, linux-wireless@vger.kernel.org, b.a.t.m.a.n@lists.open-mesh.org, bridge@lists.linux.dev, linux-wpan@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next v5 1/5] net: ip_tunnel: Build flow in underlay net namespace Date: Mon, 9 Dec 2024 22:01:47 +0800 Message-ID: <20241209140151.231257-2-shaw.leon@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241209140151.231257-1-shaw.leon@gmail.com> References: <20241209140151.231257-1-shaw.leon@gmail.com> MIME-Version: 1.0 X-MailFrom: shaw.leon@gmail.com X-Mailman-Rule-Hits: nonmember-moderation 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; header-match-b.a.t.m.a.n.lists.open-mesh.org-2 Message-ID-Hash: 77L5J4PDMXETNFWNOPIVSO35I3TPDNAH X-Message-ID-Hash: 77L5J4PDMXETNFWNOPIVSO35I3TPDNAH X-Mailman-Approved-At: Mon, 09 Dec 2024 17:47:40 +0100 X-Mailman-Version: 3.3.8 Precedence: list List-Id: The list for a Better Approach To Mobile Ad-hoc Networking Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Build IPv4 flow in underlay net namespace, where encapsulated packets are routed. Signed-off-by: Xiao Liang --- net/ipv4/ip_tunnel.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/net/ipv4/ip_tunnel.c b/net/ipv4/ip_tunnel.c index 25505f9b724c..09b73acf037a 100644 --- a/net/ipv4/ip_tunnel.c +++ b/net/ipv4/ip_tunnel.c @@ -294,7 +294,7 @@ static int ip_tunnel_bind_dev(struct net_device *dev) ip_tunnel_init_flow(&fl4, iph->protocol, iph->daddr, iph->saddr, tunnel->parms.o_key, - iph->tos & INET_DSCP_MASK, dev_net(dev), + iph->tos & INET_DSCP_MASK, tunnel->net, tunnel->parms.link, tunnel->fwmark, 0, 0); rt = ip_route_output_key(tunnel->net, &fl4); @@ -611,7 +611,7 @@ void ip_md_tunnel_xmit(struct sk_buff *skb, struct net_device *dev, } ip_tunnel_init_flow(&fl4, proto, key->u.ipv4.dst, key->u.ipv4.src, tunnel_id_to_key32(key->tun_id), - tos & INET_DSCP_MASK, dev_net(dev), 0, skb->mark, + tos & INET_DSCP_MASK, tunnel->net, 0, skb->mark, skb_get_hash(skb), key->flow_flags); if (!tunnel_hlen) @@ -774,7 +774,7 @@ void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev, ip_tunnel_init_flow(&fl4, protocol, dst, tnl_params->saddr, tunnel->parms.o_key, tos & INET_DSCP_MASK, - dev_net(dev), READ_ONCE(tunnel->parms.link), + tunnel->net, READ_ONCE(tunnel->parms.link), tunnel->fwmark, skb_get_hash(skb), 0); if (ip_tunnel_encap(skb, &tunnel->encap, &protocol, &fl4) < 0) From patchwork Mon Dec 9 14:01:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Liang X-Patchwork-Id: 18716 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 2CC9083FAB for ; Mon, 9 Dec 2024 17:49:53 +0100 (CET) ARC-Seal: i=2; cv=pass; a=rsa-sha256; d=open-mesh.org; s=20121; t=1733762993; b=qITycbjzX0/ZhjDFb9Pyh48w+i7BaOYNGpsHMN65C5crsay3iqXhXoZvLWyB0lpK6MrsV Bc1xyg7mJzAWajqrycMNrc+GSWP61jilNfx4YRTvcBMH/nPnHfQJvC1xxAkV5C9gr41OHgh lo6QPSEBjcPZtypq7Q4yNOH6umcJ3hY= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1733762993; h=from : sender : reply-to : subject : date : message-id : to : cc : mime-version : content-type : content-transfer-encoding : content-id : content-description : resent-date : resent-from : resent-sender : resent-to : resent-cc : resent-message-id : in-reply-to : references : list-id : list-help : list-unsubscribe : list-subscribe : list-post : list-owner : list-archive; bh=gu2pXduYtokx49vUkdKLIncKvOPJKtJoZQ/PcO8iW+o=; b=sQuYJoNGu9llHIfCMCC9FqV3/8dQwMzTaLFFof/b7p+1vyyQjSMeNT04sWAPtfRHqxRZj P+7NaCk3dPJnPrrh31WM/JqKFTuYTO0pRmOMb5lbRT1I+7joBvkcQRRmzO/ESlgQIS31qLn HrVHw45a+mHNW3At1/y1yCqEmQgA82w= ARC-Authentication-Results: i=2; open-mesh.org; dkim=pass header.d=gmail.com; arc=pass; dmarc=pass (Used From Domain Record) header.from=gmail.com policy.dmarc=quarantine Authentication-Results: open-mesh.org; dkim=pass header.d=gmail.com; arc=pass; dmarc=pass (Used From Domain Record) header.from=gmail.com policy.dmarc=quarantine Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by diktynna.open-mesh.org (Postfix) with ESMTPS id 3239581841 for ; Mon, 9 Dec 2024 15:03:48 +0100 (CET) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1733753028; 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=gu2pXduYtokx49vUkdKLIncKvOPJKtJoZQ/PcO8iW+o=; b=unOqurJo18KmsS4kcfGHa4We+DoCaCO79eu/PI1yYZtvkEQ7hDEHvzdKEsjgHAgZrkNhYh ngFo3QtzmW5B8KPSUzzQy1gAyOWHxCEdbmBkKi2ObN0hLZ3+SUVxT9CM1iktkZflTyoveq 6bhXSuQHq1XhJgTper6JUgjCObgjtII= ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1733753028; a=rsa-sha256; cv=none; b=RbDOhjnuM6E6SWA0Suhlu78ag3grtUsGF6Hmd1pE4AX2YKG1cPFLF+g/IyGpxANYCXQXK2 cGpFGFW3vlf4nR+ilGQueclGzS8Pmo3WsOX3r0YcWRlFlsy700QyT2Q2eFHaE5HSnC8iJJ xSE/d1DRT7W/u9Cyg3PZxV/670oKY5M= ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=gmail.com header.s=20230601 header.b=OIOVefwH; spf=pass (diktynna.open-mesh.org: domain of shaw.leon@gmail.com designates 2607:f8b0:4864:20::629 as permitted sender) smtp.mailfrom=shaw.leon@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-216585cbe1eso7949785ad.3 for ; Mon, 09 Dec 2024 06:03:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733753027; x=1734357827; darn=lists.open-mesh.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gu2pXduYtokx49vUkdKLIncKvOPJKtJoZQ/PcO8iW+o=; b=OIOVefwH0Hwr+DK1p9+jzs7Oy8C4BMjV3u27FWEbnsMt+xgSJmVe4aOebpJQLtdeMM +1QyISvLyuBuSDqvT+E3pONiN47tdcfbPcp+tk7vgeGX5lLSLOBMgeWC1zQEbIVBA58f XxYlV3UPe4yk0UIWWXQne7QGr4TXQ+INR2iVDMhWxCcyDqkvoXG84E2Byrr+SqKYTjHX n6ylDwTTf6S+3SZRdSXF0DKT0FZ1/1M8RrmLajbRGZMRoKqO5l4yFK71yBdabqTvSBFd wF0GNn0BqP2OUBIp2yhTQiZNo4li5zCPkaQkKUkNLOpj2KemsmAWyvqiSjCQ9fJSeOPB Qd4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733753027; x=1734357827; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gu2pXduYtokx49vUkdKLIncKvOPJKtJoZQ/PcO8iW+o=; b=lXLo0hfHIU9hYPxpBpmGkw4veJF/wReTnzMaVBik5ZFm7TILayA4SdCI/2wlIk/X9U qLpFPZKuobnUwfuqVmUpGOQw4V9T+VSx084YldziJYmr9eZULJ1Ocde3EBKCjxvd5GKZ x+xIleQKZuBU4SXr4F3T7MZVCHdJQ+VmzoBWA6zq0Xm2pmghoOBVOejQFTWvmJFXawkK txu03naYOs4JIQUVPnSG6b8yEHkfZ+4pm57FqnFqP/fA0ue7jwQd1P51Y1dTcikEM+ez +efFi4Zs7RaFKItB9Nx4eQZg21hVyqNNbE11w9ptUoVb+TkyU2NtC+6qnavpyc0ivtK3 XxBw== X-Forwarded-Encrypted: i=1; AJvYcCWmr2RTGbCo0z6dpT1oZJHEAPuNLJ2LfL8tRo5GoxgxAZtPG418KdXPtIhXu+y8roEdWVA3zdkneVMuyQ==@lists.open-mesh.org X-Gm-Message-State: AOJu0Yyq9C2tNtKj3VbBjagLiq4Oe+l03o6A0hx+ZxNey82dlAKK3p1G EARTWDj5d4+cQLL+SWeXBskjwhofMLp1Q9sgeORHz9SMrxEhP6Ru X-Gm-Gg: ASbGncsksxCBN+Kvh0lqlgA6I/l9izXnehVsUDqTT7hv4XUFZMS+wADidrB+af+JhP5 FpJRd2D7FtUUxeATDwkfyT8X0qHtVq+ioURxaLcqO4t5t5itvytuMP57eh5JNSlGoq+RhxLYi9s vq8rqHR9mhMn1LLiSe7KxuFxSMIGuh++f+mJxU/NAORcvXCFYD0GHuKpeA3In44kM3MKnxbHQb2 JpbkcyYHOpmgqH+0VQATOPtwzotsMS93rKg1zxof6+SORA= X-Google-Smtp-Source: AGHT+IFvuzKQIZf1qkxZ4+Ci/Eeds3C/nB8ZqihF0Bnr/4C7RYL2k3LJsI5chnoI4Z2JG+UfbiRHQQ== X-Received: by 2002:a17:902:e5c5:b0:216:393b:23d4 with SMTP id d9443c01a7336-21669f993b4mr7567455ad.11.1733753026559; Mon, 09 Dec 2024 06:03:46 -0800 (PST) Received: from nova-ws.. ([103.167.140.11]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-216221db645sm49605645ad.46.2024.12.09.06.02.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Dec 2024 06:03:45 -0800 (PST) From: Xiao Liang To: netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, Kuniyuki Iwashima , Jakub Kicinski , Donald Hunter Cc: "David S. Miller" , David Ahern , Eric Dumazet , Paolo Abeni , Ido Schimmel , Andrew Lunn , Simon Horman , Shuah Khan , Jiri Pirko , Hangbin Liu , linux-rdma@vger.kernel.org, linux-can@vger.kernel.org, osmocom-net-gprs@lists.osmocom.org, bpf@vger.kernel.org, linux-ppp@vger.kernel.org, wireguard@lists.zx2c4.com, linux-wireless@vger.kernel.org, b.a.t.m.a.n@lists.open-mesh.org, bridge@lists.linux.dev, linux-wpan@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next v5 2/5] rtnetlink: Lookup device in target netns when creating link Date: Mon, 9 Dec 2024 22:01:48 +0800 Message-ID: <20241209140151.231257-3-shaw.leon@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241209140151.231257-1-shaw.leon@gmail.com> References: <20241209140151.231257-1-shaw.leon@gmail.com> MIME-Version: 1.0 X-MailFrom: shaw.leon@gmail.com X-Mailman-Rule-Hits: nonmember-moderation 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; header-match-b.a.t.m.a.n.lists.open-mesh.org-2 Message-ID-Hash: 4TEJEDMORMFIIEGT4FLDSNJBWWS2AZLE X-Message-ID-Hash: 4TEJEDMORMFIIEGT4FLDSNJBWWS2AZLE X-Mailman-Approved-At: Mon, 09 Dec 2024 17:47:40 +0100 X-Mailman-Version: 3.3.8 Precedence: list List-Id: The list for a Better Approach To Mobile Ad-hoc Networking Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: When creating link, lookup for existing device in target net namespace instead of current one. For example, two links created by: # ip link add dummy1 type dummy # ip link add netns ns1 dummy1 type dummy should have no conflict since they are in different namespaces. Signed-off-by: Xiao Liang --- net/core/rtnetlink.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index ab5f201bf0ab..7855f81c917b 100644 --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c @@ -3851,20 +3851,26 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, { struct nlattr ** const tb = tbs->tb; struct net *net = sock_net(skb->sk); + struct net *device_net; struct net_device *dev; struct ifinfomsg *ifm; bool link_specified; + /* When creating, lookup for existing device in target net namespace */ + device_net = (nlh->nlmsg_flags & NLM_F_CREATE) && + (nlh->nlmsg_flags & NLM_F_EXCL) ? + tgt_net : net; + ifm = nlmsg_data(nlh); if (ifm->ifi_index > 0) { link_specified = true; - dev = __dev_get_by_index(net, ifm->ifi_index); + dev = __dev_get_by_index(device_net, ifm->ifi_index); } else if (ifm->ifi_index < 0) { NL_SET_ERR_MSG(extack, "ifindex can't be negative"); return -EINVAL; } else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) { link_specified = true; - dev = rtnl_dev_get(net, tb); + dev = rtnl_dev_get(device_net, tb); } else { link_specified = false; dev = NULL; From patchwork Mon Dec 9 14:01:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Liang X-Patchwork-Id: 18717 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 80EA08403D for ; Mon, 9 Dec 2024 17:50:29 +0100 (CET) ARC-Seal: i=2; cv=pass; a=rsa-sha256; d=open-mesh.org; s=20121; t=1733763029; b=np5OMpOdWqx+bnjGHY4mRwECBiSorq7b6ex9PBYkfea3sXMhxbBNApLJ4/+fQooXHjgRu rhjqgZEtcg8yn35IMBXhMXYqZwB5jgl3Ibze7UNSjkaBIAWmTDSHcslJ2wWLrgRLfPKORu3 M2yiuuJSpLTmu5ccVgYpxZ2xsAMjUho= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1733763029; h=from : sender : reply-to : subject : date : message-id : to : cc : mime-version : content-type : content-transfer-encoding : content-id : content-description : resent-date : resent-from : resent-sender : resent-to : resent-cc : resent-message-id : in-reply-to : references : list-id : list-help : list-unsubscribe : list-subscribe : list-post : list-owner : list-archive; bh=qPy/Wa4sfWIv6r2JdpWfPhpUlAIM+rgHQUPWOAmdvfo=; b=bf4/DE931SDwGerIn52l9Lh4jn0MYUCsdqsdZhimCCd6MDsasWSWRI9URvQwD/wTdyZm8 HGl1Z+POr2cEqUkevM48nRYGKov/mmg5xEgDXg/ZxeP7VFtpkVK4BwgRYRhlmKxmWTZtzGr r0tu0vsJX4v5sru7ZQxiW2dePm6HbPU= ARC-Authentication-Results: i=2; open-mesh.org; dkim=pass header.d=gmail.com; arc=pass; dmarc=pass (Used From Domain Record) header.from=gmail.com policy.dmarc=quarantine Authentication-Results: open-mesh.org; dkim=pass header.d=gmail.com; arc=pass; dmarc=pass (Used From Domain Record) header.from=gmail.com policy.dmarc=quarantine Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by diktynna.open-mesh.org (Postfix) with ESMTPS id 125B183B82 for ; Mon, 9 Dec 2024 15:05:58 +0100 (CET) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1733753159; 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=qPy/Wa4sfWIv6r2JdpWfPhpUlAIM+rgHQUPWOAmdvfo=; b=lHZeJHmnFSByYgL3IZ5NoYoyvjLmSyN1O1RIcYPw9gCCurb5UguZc04S/FGAycAikA2iGm jSI8MtlziqEWHYnDhQc7ujNXZwU0o9x/Jicn276VXxA6KdisfFfESIm9sas2Ij01gWBvQd VKuNS0PxRM1Jl1YIx88Ar4QMMUAxIaE= ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1733753159; a=rsa-sha256; cv=none; b=ls9mhzpwIom1wtFrWjtI8bGJZKEs0WVuela47LwJvOsxb7DUEBOZUA46xOLHfhMaq6eONS b8VD3VEkORMyCN2u9aJWKJhQ1pSnCPzdVm9J1bllFZRKAxIhttIzyGo4uTRcM4N0RPfUo4 LYMDgd0EzsW2f3hlsNkbNjSHGCVDWYI= ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=gmail.com header.s=20230601 header.b=BvS8IZa5; spf=pass (diktynna.open-mesh.org: domain of shaw.leon@gmail.com designates 2607:f8b0:4864:20::62a as permitted sender) smtp.mailfrom=shaw.leon@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-2162c0f6a39so15519335ad.0 for ; Mon, 09 Dec 2024 06:05:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733753157; x=1734357957; darn=lists.open-mesh.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qPy/Wa4sfWIv6r2JdpWfPhpUlAIM+rgHQUPWOAmdvfo=; b=BvS8IZa5C/gzfMUvwEQBCJEmo9Ev8bGOF+UNt2HiGIdXLfKOyA86g9sH4/wh/zv3pZ waHzARsBMV0kvIhc060RVipf9JsJcWzcR9+HjcL3gVOKBcNwqgvXj08HnrjApXfwm/Vm Q1nOkf1OE4P+8EzyyscKuNmk1Q6E8HFESWU+TtZIYX2eXycn69qn6chsLeqR2CogrGMF LiMs6LdP/+wFxLQh+b1kaxZxWDcVnb5VyRwi9940OhoTAhT+iY11JgN30hh6Lx14xG/G dnBbtvl/oy7KnyxiE+l01fWGoxkExBCGcO0HCg+Zp8abqPgQmqFYnymDmn1s4kyhOY4R xFLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733753157; x=1734357957; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qPy/Wa4sfWIv6r2JdpWfPhpUlAIM+rgHQUPWOAmdvfo=; b=KIM+b6eL6/Nf/EE1a7JMYxzbW1db1z6UUdw4mxv3kNnzIqrgwMCDpD0hqaWJsnvvPu d02WiIM3UdBR7Mqa0QBmGd8bmRGuo6TBzspr10mVhWfAn7rVME2TnmCcL7QW4Q0x4oWW JqwoGKPfsZ1UqMXCoaUbmlQi5hlb/6o6b/2SFpUSJRXjSFw7Vor45vPTu7VBh2TPH90v VRSCFGgiWBAQ7WcmH1uZ6FXna8P9Tjsi5qPD256028aGTJgHJ3EIC0wL5qGDDjVfv9Oo uoLl4JFaIrn2dNBAGkTlmgD1dzmwmsVc9ygT4VbSwNcNsncVxin0nPr0kMsM7j2zGTfT tWPg== X-Forwarded-Encrypted: i=1; AJvYcCVcyf61HPcrwUBMzfJdb/SwfK/H/C5Ve+HRlprDGH+pnwsCPkjsJO46kZ3A11ZrgjaFvJQL9jO24iRoCQ==@lists.open-mesh.org X-Gm-Message-State: AOJu0Yxg0ApCqNhCQdZ3DBOpbRmw205+QSu/Iyqaje+KAtuxCHk+NwIC fYom4o+XimUmme9WZpg3JApPz+uWh4UyeMMEUnyK07hAYIOoMBYy X-Gm-Gg: ASbGncsm2jLndew+CgsfdrfUmnxtjYyy+upINBST2ntOmXKGncUnl2ZdrHl6z85Gurt iLkjPLHR3QUqb7esUd7RJAchJ2rP95/J4Ennf+Csb+7UfJK2jTBGokZO6sOPiSaK8PhnyMu3PU8 rPicPAyTRyei7QusOmAZr0RqBjVoT8PUSkScaMmWVyTFHm3wKCbZjNxFAwBr+KwHlwddAZIMlfE PacscXhNYzxD8oGZowlmJvRUwoxVs3Ix/TyIo7jCnaCzd0= X-Google-Smtp-Source: AGHT+IFhtw6aOaRBRqvTJbYhi/Fe6yXPqzisSZXdysCa+uK3mB+MiqtBQc7Dx64qWN61pTDIg+VOTg== X-Received: by 2002:a17:902:ce03:b0:212:1abb:cba1 with SMTP id d9443c01a7336-21611b1362emr181353375ad.24.1733753156954; Mon, 09 Dec 2024 06:05:56 -0800 (PST) Received: from nova-ws.. ([103.167.140.11]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-216221db645sm49605645ad.46.2024.12.09.06.05.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Dec 2024 06:05:55 -0800 (PST) From: Xiao Liang To: netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, Kuniyuki Iwashima , Jakub Kicinski , Donald Hunter Cc: "David S. Miller" , David Ahern , Eric Dumazet , Paolo Abeni , Ido Schimmel , Andrew Lunn , Simon Horman , Shuah Khan , Jiri Pirko , Hangbin Liu , linux-rdma@vger.kernel.org, linux-can@vger.kernel.org, osmocom-net-gprs@lists.osmocom.org, bpf@vger.kernel.org, linux-ppp@vger.kernel.org, wireguard@lists.zx2c4.com, linux-wireless@vger.kernel.org, b.a.t.m.a.n@lists.open-mesh.org, bridge@lists.linux.dev, linux-wpan@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next v5 3/5] rtnetlink: Decouple net namespaces in rtnl_newlink_create() Date: Mon, 9 Dec 2024 22:01:49 +0800 Message-ID: <20241209140151.231257-4-shaw.leon@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241209140151.231257-1-shaw.leon@gmail.com> References: <20241209140151.231257-1-shaw.leon@gmail.com> MIME-Version: 1.0 X-MailFrom: shaw.leon@gmail.com X-Mailman-Rule-Hits: nonmember-moderation 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; header-match-b.a.t.m.a.n.lists.open-mesh.org-2 Message-ID-Hash: ONIY4M4Y65ODRRV6VDZ3RIMG27TCQBZD X-Message-ID-Hash: ONIY4M4Y65ODRRV6VDZ3RIMG27TCQBZD X-Mailman-Approved-At: Mon, 09 Dec 2024 17:47:40 +0100 X-Mailman-Version: 3.3.8 Precedence: list List-Id: The list for a Better Approach To Mobile Ad-hoc Networking Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: There are 4 net namespaces involved when creating links: - source netns - where the netlink socket resides, - target netns - where to put the device being created, - link netns - netns associated with the device (backend), - peer netns - netns of peer device. Currently, two nets are passed to newlink() callback - "src_net" parameter and "dev_net" (implicitly in net_device). They are set as follows, depending on netlink attributes. +------------+-------------------+---------+---------+ | peer netns | IFLA_LINK_NETNSID | src_net | dev_net | +------------+-------------------+---------+---------+ | | absent | source | target | | absent +-------------------+---------+---------+ | | present | link | link | +------------+-------------------+---------+---------+ | | absent | peer | target | | present +-------------------+---------+---------+ | | present | peer | link | +------------+-------------------+---------+---------+ When IFLA_LINK_NETNSID is present, the device is created in link netns first. This has some side effects, including extra ifindex allocation, ifname validation and link notifications. There's also an extra step to move the device to target netns. These could be avoided if we create it in target netns at the beginning. On the other hand, the meaning of src_net is ambiguous. It varies depending on how parameters are passed. It is the effective link or peer netns by design, but some drivers ignore it and use dev_net instead. This patch refactors netns handling by packing newlink() parameters into a struct, and passing source, link and peer netns as is through this struct. Fallback logic is implemented in helper functions - rtnl_newlink_link_net() and rtnl_newlink_peer_net(). If is not set, peer netns falls back to link netns, and link netns falls back to source netns. rtnl_newlink_create() now creates devices in target netns directly, so dev_net is always target netns. For drivers that use dev_net as fallback of link_netns, current behavior is kept for compatibility. Signed-off-by: Xiao Liang --- There're some issues found when coverting drivers. Please check if they work as intended: - In amt_newlink() drivers/net/amt.c: amt->net = net; ... amt->stream_dev = dev_get_by_index(net, ... Uses net (src_net actually), but amt_lookup_upper_dev() only searches in dev_net. - In gtp_newlink() in drivers/net/gtp.c: gtp->net = src_net; ... gn = net_generic(dev_net(dev), gtp_net_id); list_add_rcu(>p->list, &gn->gtp_dev_list); Uses src_net, but is linked to list in dev_net. - In pfcp_newlink() in drivers/net/pfcp.c: pfcp->net = net; ... pn = net_generic(dev_net(dev), pfcp_net_id); list_add_rcu(&pfcp->list, &pn->pfcp_dev_list); Same. - In lowpan_newlink() in net/ieee802154/6lowpan/core.c: wdev = dev_get_by_index(dev_net(ldev), nla_get_u32(tb[IFLA_LINK])); Looks for IFLA_LINK in dev_net, but in theory the ifindex is defined in link netns. --- drivers/infiniband/ulp/ipoib/ipoib_netlink.c | 11 +++-- drivers/net/amt.c | 13 +++--- drivers/net/bareudp.c | 11 +++-- drivers/net/bonding/bond_netlink.c | 8 ++-- drivers/net/can/dev/netlink.c | 4 +- drivers/net/can/vxcan.c | 9 ++-- .../ethernet/qualcomm/rmnet/rmnet_config.c | 11 +++-- drivers/net/geneve.c | 11 +++-- drivers/net/gtp.c | 9 ++-- drivers/net/ipvlan/ipvlan.h | 4 +- drivers/net/ipvlan/ipvlan_main.c | 11 +++-- drivers/net/ipvlan/ipvtap.c | 7 ++- drivers/net/macsec.c | 11 +++-- drivers/net/macvlan.c | 8 ++-- drivers/net/macvtap.c | 8 ++-- drivers/net/netkit.c | 9 ++-- drivers/net/pfcp.c | 8 ++-- drivers/net/ppp/ppp_generic.c | 10 +++-- drivers/net/team/team_core.c | 7 +-- drivers/net/veth.c | 9 ++-- drivers/net/vrf.c | 7 +-- drivers/net/vxlan/vxlan_core.c | 11 +++-- drivers/net/wireguard/device.c | 8 ++-- drivers/net/wireless/virtual/virt_wifi.c | 10 +++-- drivers/net/wwan/wwan_core.c | 15 +++++-- include/net/ip_tunnels.h | 5 ++- include/net/rtnetlink.h | 44 ++++++++++++++++--- net/8021q/vlan_netlink.c | 11 +++-- net/batman-adv/soft-interface.c | 12 ++--- net/bridge/br_netlink.c | 8 ++-- net/caif/chnl_net.c | 6 +-- net/core/rtnetlink.c | 25 +++++------ net/hsr/hsr_netlink.c | 14 +++--- net/ieee802154/6lowpan/core.c | 9 ++-- net/ipv4/ip_gre.c | 27 ++++++++---- net/ipv4/ip_tunnel.c | 10 +++-- net/ipv4/ip_vti.c | 10 +++-- net/ipv4/ipip.c | 10 +++-- net/ipv6/ip6_gre.c | 28 +++++++----- net/ipv6/ip6_tunnel.c | 16 +++---- net/ipv6/ip6_vti.c | 15 +++---- net/ipv6/sit.c | 16 +++---- net/xfrm/xfrm_interface_core.c | 14 +++--- 43 files changed, 305 insertions(+), 205 deletions(-) diff --git a/drivers/infiniband/ulp/ipoib/ipoib_netlink.c b/drivers/infiniband/ulp/ipoib/ipoib_netlink.c index 9ad8d9856275..da587af85d4f 100644 --- a/drivers/infiniband/ulp/ipoib/ipoib_netlink.c +++ b/drivers/infiniband/ulp/ipoib/ipoib_netlink.c @@ -97,10 +97,13 @@ static int ipoib_changelink(struct net_device *dev, struct nlattr *tb[], return ret; } -static int ipoib_new_child_link(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int ipoib_new_child_link(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; + struct net *link_net = rtnl_newlink_link_net(params); struct net_device *pdev; struct ipoib_dev_priv *ppriv; u16 child_pkey; @@ -109,7 +112,7 @@ static int ipoib_new_child_link(struct net *src_net, struct net_device *dev, if (!tb[IFLA_LINK]) return -EINVAL; - pdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); + pdev = __dev_get_by_index(link_net, nla_get_u32(tb[IFLA_LINK])); if (!pdev || pdev->type != ARPHRD_INFINIBAND) return -ENODEV; diff --git a/drivers/net/amt.c b/drivers/net/amt.c index 98c6205ed19f..2f7bf50e05d2 100644 --- a/drivers/net/amt.c +++ b/drivers/net/amt.c @@ -3161,14 +3161,17 @@ static int amt_validate(struct nlattr *tb[], struct nlattr *data[], return 0; } -static int amt_newlink(struct net *net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int amt_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; + struct net *link_net = rtnl_newlink_link_net(params); struct amt_dev *amt = netdev_priv(dev); int err = -EINVAL; - amt->net = net; + amt->net = link_net; amt->mode = nla_get_u32(data[IFLA_AMT_MODE]); if (data[IFLA_AMT_MAX_TUNNELS] && @@ -3183,7 +3186,7 @@ static int amt_newlink(struct net *net, struct net_device *dev, amt->hash_buckets = AMT_HSIZE; amt->nr_tunnels = 0; get_random_bytes(&amt->hash_seed, sizeof(amt->hash_seed)); - amt->stream_dev = dev_get_by_index(net, + amt->stream_dev = dev_get_by_index(link_net, nla_get_u32(data[IFLA_AMT_LINK])); if (!amt->stream_dev) { NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LINK], diff --git a/drivers/net/bareudp.c b/drivers/net/bareudp.c index 70814303aab8..91e1c02ada72 100644 --- a/drivers/net/bareudp.c +++ b/drivers/net/bareudp.c @@ -698,10 +698,13 @@ static void bareudp_dellink(struct net_device *dev, struct list_head *head) unregister_netdevice_queue(dev, head); } -static int bareudp_newlink(struct net *net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int bareudp_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; + struct net *link_net = rtnl_newlink_link_net(params); struct bareudp_conf conf; int err; @@ -709,7 +712,7 @@ static int bareudp_newlink(struct net *net, struct net_device *dev, if (err) return err; - err = bareudp_configure(net, dev, &conf, extack); + err = bareudp_configure(link_net, dev, &conf, extack); if (err) return err; diff --git a/drivers/net/bonding/bond_netlink.c b/drivers/net/bonding/bond_netlink.c index 2a6a424806aa..db3062c6dbe0 100644 --- a/drivers/net/bonding/bond_netlink.c +++ b/drivers/net/bonding/bond_netlink.c @@ -564,10 +564,12 @@ static int bond_changelink(struct net_device *bond_dev, struct nlattr *tb[], return 0; } -static int bond_newlink(struct net *src_net, struct net_device *bond_dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int bond_newlink(struct rtnl_newlink_params *params) { + struct net_device *bond_dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; int err; err = bond_changelink(bond_dev, tb, data, extack); diff --git a/drivers/net/can/dev/netlink.c b/drivers/net/can/dev/netlink.c index 01aacdcda260..52dae0e94858 100644 --- a/drivers/net/can/dev/netlink.c +++ b/drivers/net/can/dev/netlink.c @@ -624,9 +624,7 @@ static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev) return -EMSGSIZE; } -static int can_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int can_newlink(struct rtnl_newlink_params *params) { return -EOPNOTSUPP; } diff --git a/drivers/net/can/vxcan.c b/drivers/net/can/vxcan.c index ca8811941085..65ae07116c91 100644 --- a/drivers/net/can/vxcan.c +++ b/drivers/net/can/vxcan.c @@ -172,10 +172,13 @@ static void vxcan_setup(struct net_device *dev) /* forward declaration for rtnl_create_link() */ static struct rtnl_link_ops vxcan_link_ops; -static int vxcan_newlink(struct net *peer_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int vxcan_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; + struct net *peer_net = rtnl_newlink_peer_net(params); struct vxcan_priv *priv; struct net_device *peer; diff --git a/drivers/net/ethernet/qualcomm/rmnet/rmnet_config.c b/drivers/net/ethernet/qualcomm/rmnet/rmnet_config.c index f3bea196a8f9..d45555d784e6 100644 --- a/drivers/net/ethernet/qualcomm/rmnet/rmnet_config.c +++ b/drivers/net/ethernet/qualcomm/rmnet/rmnet_config.c @@ -117,10 +117,13 @@ static void rmnet_unregister_bridge(struct rmnet_port *port) rmnet_unregister_real_device(bridge_dev); } -static int rmnet_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int rmnet_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; + struct net *link_net = rtnl_newlink_link_net(params); u32 data_format = RMNET_FLAGS_INGRESS_DEAGGREGATION; struct net_device *real_dev; int mode = RMNET_EPMODE_VND; @@ -134,7 +137,7 @@ static int rmnet_newlink(struct net *src_net, struct net_device *dev, return -EINVAL; } - real_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); + real_dev = __dev_get_by_index(link_net, nla_get_u32(tb[IFLA_LINK])); if (!real_dev) { NL_SET_ERR_MSG_MOD(extack, "link does not exist"); return -ENODEV; diff --git a/drivers/net/geneve.c b/drivers/net/geneve.c index 642155cb8315..77978617f509 100644 --- a/drivers/net/geneve.c +++ b/drivers/net/geneve.c @@ -1614,10 +1614,13 @@ static void geneve_link_config(struct net_device *dev, geneve_change_mtu(dev, ldev_mtu - info->options_len); } -static int geneve_newlink(struct net *net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int geneve_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; + struct net *link_net = rtnl_newlink_link_net(params); struct geneve_config cfg = { .df = GENEVE_DF_UNSET, .use_udp6_rx_checksums = false, @@ -1631,7 +1634,7 @@ static int geneve_newlink(struct net *net, struct net_device *dev, if (err) return err; - err = geneve_configure(net, dev, extack, &cfg); + err = geneve_configure(link_net, dev, extack, &cfg); if (err) return err; diff --git a/drivers/net/gtp.c b/drivers/net/gtp.c index 89a996ad8cd0..3eb1bc3ac124 100644 --- a/drivers/net/gtp.c +++ b/drivers/net/gtp.c @@ -1460,10 +1460,11 @@ static int gtp_create_sockets(struct gtp_dev *gtp, const struct nlattr *nla, #define GTP_TH_MAXLEN (sizeof(struct udphdr) + sizeof(struct gtp0_header)) #define GTP_IPV6_MAXLEN (sizeof(struct ipv6hdr) + GTP_TH_MAXLEN) -static int gtp_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int gtp_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **data = params->data; + struct net *link_net = rtnl_newlink_link_net(params); unsigned int role = GTP_ROLE_GGSN; struct gtp_dev *gtp; struct gtp_net *gn; @@ -1494,7 +1495,7 @@ static int gtp_newlink(struct net *src_net, struct net_device *dev, gtp->restart_count = nla_get_u8_default(data[IFLA_GTP_RESTART_COUNT], 0); - gtp->net = src_net; + gtp->net = link_net; err = gtp_hashtable_new(gtp, hashsize); if (err < 0) diff --git a/drivers/net/ipvlan/ipvlan.h b/drivers/net/ipvlan/ipvlan.h index 025e0c19ec25..beff25a1d6f0 100644 --- a/drivers/net/ipvlan/ipvlan.h +++ b/drivers/net/ipvlan/ipvlan.h @@ -166,9 +166,7 @@ struct ipvl_addr *ipvlan_addr_lookup(struct ipvl_port *port, void *lyr3h, void *ipvlan_get_L3_hdr(struct ipvl_port *port, struct sk_buff *skb, int *type); void ipvlan_count_rx(const struct ipvl_dev *ipvlan, unsigned int len, bool success, bool mcast); -int ipvlan_link_new(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack); +int ipvlan_link_new(struct rtnl_newlink_params *params); void ipvlan_link_delete(struct net_device *dev, struct list_head *head); void ipvlan_link_setup(struct net_device *dev); int ipvlan_link_register(struct rtnl_link_ops *ops); diff --git a/drivers/net/ipvlan/ipvlan_main.c b/drivers/net/ipvlan/ipvlan_main.c index ee2c3cf4df36..53860e9d08b1 100644 --- a/drivers/net/ipvlan/ipvlan_main.c +++ b/drivers/net/ipvlan/ipvlan_main.c @@ -532,10 +532,13 @@ static int ipvlan_nl_fillinfo(struct sk_buff *skb, return ret; } -int ipvlan_link_new(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +int ipvlan_link_new(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; + struct net *link_net = rtnl_newlink_link_net(params); struct ipvl_dev *ipvlan = netdev_priv(dev); struct ipvl_port *port; struct net_device *phy_dev; @@ -545,7 +548,7 @@ int ipvlan_link_new(struct net *src_net, struct net_device *dev, if (!tb[IFLA_LINK]) return -EINVAL; - phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); + phy_dev = __dev_get_by_index(link_net, nla_get_u32(tb[IFLA_LINK])); if (!phy_dev) return -ENODEV; diff --git a/drivers/net/ipvlan/ipvtap.c b/drivers/net/ipvlan/ipvtap.c index 1afc4c47be73..69e7456a48ca 100644 --- a/drivers/net/ipvlan/ipvtap.c +++ b/drivers/net/ipvlan/ipvtap.c @@ -73,10 +73,9 @@ static void ipvtap_update_features(struct tap_dev *tap, netdev_update_features(vlan->dev); } -static int ipvtap_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int ipvtap_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; struct ipvtap_dev *vlantap = netdev_priv(dev); int err; @@ -97,7 +96,7 @@ static int ipvtap_newlink(struct net *src_net, struct net_device *dev, /* Don't put anything that may fail after macvlan_common_newlink * because we can't undo what it does. */ - err = ipvlan_link_new(src_net, dev, tb, data, extack); + err = ipvlan_link_new(params); if (err) { netdev_rx_handler_unregister(dev); return err; diff --git a/drivers/net/macsec.c b/drivers/net/macsec.c index 1bc1e5993f56..e8b147fe4fce 100644 --- a/drivers/net/macsec.c +++ b/drivers/net/macsec.c @@ -4141,10 +4141,13 @@ static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len) static struct lock_class_key macsec_netdev_addr_lock_key; -static int macsec_newlink(struct net *net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int macsec_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; + struct net *link_net = rtnl_newlink_link_net(params); struct macsec_dev *macsec = macsec_priv(dev); rx_handler_func_t *rx_handler; u8 icv_len = MACSEC_DEFAULT_ICV_LEN; @@ -4154,7 +4157,7 @@ static int macsec_newlink(struct net *net, struct net_device *dev, if (!tb[IFLA_LINK]) return -EINVAL; - real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK])); + real_dev = __dev_get_by_index(link_net, nla_get_u32(tb[IFLA_LINK])); if (!real_dev) return -ENODEV; if (real_dev->type != ARPHRD_ETHER) diff --git a/drivers/net/macvlan.c b/drivers/net/macvlan.c index fed4fe2a4748..7050a061b2b9 100644 --- a/drivers/net/macvlan.c +++ b/drivers/net/macvlan.c @@ -1565,11 +1565,11 @@ int macvlan_common_newlink(struct net *src_net, struct net_device *dev, } EXPORT_SYMBOL_GPL(macvlan_common_newlink); -static int macvlan_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int macvlan_newlink(struct rtnl_newlink_params *params) { - return macvlan_common_newlink(src_net, dev, tb, data, extack); + return macvlan_common_newlink(rtnl_newlink_link_net(params), + params->dev, params->tb, params->data, + params->extack); } void macvlan_dellink(struct net_device *dev, struct list_head *head) diff --git a/drivers/net/macvtap.c b/drivers/net/macvtap.c index 29a5929d48e5..213a16719c5a 100644 --- a/drivers/net/macvtap.c +++ b/drivers/net/macvtap.c @@ -77,10 +77,9 @@ static void macvtap_update_features(struct tap_dev *tap, netdev_update_features(vlan->dev); } -static int macvtap_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int macvtap_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; struct macvtap_dev *vlantap = netdev_priv(dev); int err; @@ -105,7 +104,8 @@ static int macvtap_newlink(struct net *src_net, struct net_device *dev, /* Don't put anything that may fail after macvlan_common_newlink * because we can't undo what it does. */ - err = macvlan_common_newlink(src_net, dev, tb, data, extack); + err = macvlan_common_newlink(rtnl_newlink_link_net(params), dev, + params->tb, params->data, params->extack); if (err) { netdev_rx_handler_unregister(dev); return err; diff --git a/drivers/net/netkit.c b/drivers/net/netkit.c index c1d881dc6409..607d3b141f8c 100644 --- a/drivers/net/netkit.c +++ b/drivers/net/netkit.c @@ -327,10 +327,13 @@ static int netkit_validate(struct nlattr *tb[], struct nlattr *data[], static struct rtnl_link_ops netkit_link_ops; -static int netkit_new_link(struct net *peer_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int netkit_new_link(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; + struct net *peer_net = rtnl_newlink_peer_net(params); struct nlattr *peer_tb[IFLA_MAX + 1], **tbp = tb, *attr; enum netkit_action policy_prim = NETKIT_PASS; enum netkit_action policy_peer = NETKIT_PASS; diff --git a/drivers/net/pfcp.c b/drivers/net/pfcp.c index 69434fd13f96..8576d5117233 100644 --- a/drivers/net/pfcp.c +++ b/drivers/net/pfcp.c @@ -184,15 +184,15 @@ static int pfcp_add_sock(struct pfcp_dev *pfcp) return PTR_ERR_OR_ZERO(pfcp->sock); } -static int pfcp_newlink(struct net *net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int pfcp_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct net *link_net = rtnl_newlink_link_net(params); struct pfcp_dev *pfcp = netdev_priv(dev); struct pfcp_net *pn; int err; - pfcp->net = net; + pfcp->net = link_net; err = pfcp_add_sock(pfcp); if (err) { diff --git a/drivers/net/ppp/ppp_generic.c b/drivers/net/ppp/ppp_generic.c index 4583e15ad03a..a0ace8aa5b5d 100644 --- a/drivers/net/ppp/ppp_generic.c +++ b/drivers/net/ppp/ppp_generic.c @@ -1303,10 +1303,12 @@ static int ppp_nl_validate(struct nlattr *tb[], struct nlattr *data[], return 0; } -static int ppp_nl_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int ppp_nl_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct net *link_net = rtnl_newlink_link_net(params); struct ppp_config conf = { .unit = -1, .ifname_is_set = true, @@ -1343,7 +1345,7 @@ static int ppp_nl_newlink(struct net *src_net, struct net_device *dev, if (!tb[IFLA_IFNAME] || !nla_len(tb[IFLA_IFNAME]) || !*(char *)nla_data(tb[IFLA_IFNAME])) conf.ifname_is_set = false; - err = ppp_dev_configure(src_net, dev, &conf); + err = ppp_dev_configure(link_net, dev, &conf); out_unlock: mutex_unlock(&ppp_mutex); diff --git a/drivers/net/team/team_core.c b/drivers/net/team/team_core.c index a1b27b69f010..c9ee70030517 100644 --- a/drivers/net/team/team_core.c +++ b/drivers/net/team/team_core.c @@ -2206,10 +2206,11 @@ static void team_setup(struct net_device *dev) dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; } -static int team_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int team_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + if (tb[IFLA_ADDRESS] == NULL) eth_hw_addr_random(dev); diff --git a/drivers/net/veth.c b/drivers/net/veth.c index 07ebb800edf1..e9818f6b666b 100644 --- a/drivers/net/veth.c +++ b/drivers/net/veth.c @@ -1765,10 +1765,13 @@ static int veth_init_queues(struct net_device *dev, struct nlattr *tb[]) return 0; } -static int veth_newlink(struct net *peer_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int veth_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; + struct net *peer_net = rtnl_newlink_peer_net(params); int err; struct net_device *peer; struct veth_priv *priv; diff --git a/drivers/net/vrf.c b/drivers/net/vrf.c index ca81b212a246..ed1d47a473e2 100644 --- a/drivers/net/vrf.c +++ b/drivers/net/vrf.c @@ -1677,10 +1677,11 @@ static void vrf_dellink(struct net_device *dev, struct list_head *head) unregister_netdevice_queue(dev, head); } -static int vrf_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int vrf_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; struct net_vrf *vrf = netdev_priv(dev); struct netns_vrf *nn_vrf; bool *add_fib_rules; diff --git a/drivers/net/vxlan/vxlan_core.c b/drivers/net/vxlan/vxlan_core.c index b46a799bd390..2e27b19a557e 100644 --- a/drivers/net/vxlan/vxlan_core.c +++ b/drivers/net/vxlan/vxlan_core.c @@ -4351,10 +4351,13 @@ static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[], return 0; } -static int vxlan_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int vxlan_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; + struct net *link_net = rtnl_newlink_link_net(params); struct vxlan_config conf; int err; @@ -4362,7 +4365,7 @@ static int vxlan_newlink(struct net *src_net, struct net_device *dev, if (err) return err; - return __vxlan_dev_create(src_net, dev, &conf, extack); + return __vxlan_dev_create(link_net, dev, &conf, extack); } static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[], diff --git a/drivers/net/wireguard/device.c b/drivers/net/wireguard/device.c index 6cf173a008e7..a27f844cfb7f 100644 --- a/drivers/net/wireguard/device.c +++ b/drivers/net/wireguard/device.c @@ -307,14 +307,14 @@ static void wg_setup(struct net_device *dev) wg->dev = dev; } -static int wg_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int wg_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct net *link_net = rtnl_newlink_link_net(params); struct wg_device *wg = netdev_priv(dev); int ret = -ENOMEM; - rcu_assign_pointer(wg->creating_net, src_net); + rcu_assign_pointer(wg->creating_net, link_net); init_rwsem(&wg->static_identity.lock); mutex_init(&wg->socket_update_lock); mutex_init(&wg->device_update_lock); diff --git a/drivers/net/wireless/virtual/virt_wifi.c b/drivers/net/wireless/virtual/virt_wifi.c index 4ee374080466..107dc503b4f2 100644 --- a/drivers/net/wireless/virtual/virt_wifi.c +++ b/drivers/net/wireless/virtual/virt_wifi.c @@ -519,10 +519,12 @@ static rx_handler_result_t virt_wifi_rx_handler(struct sk_buff **pskb) } /* Called with rtnl lock held. */ -static int virt_wifi_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int virt_wifi_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct netlink_ext_ack *extack = params->extack; + struct net *link_net = rtnl_newlink_link_net(params); struct virt_wifi_netdev_priv *priv = netdev_priv(dev); int err; @@ -532,7 +534,7 @@ static int virt_wifi_newlink(struct net *src_net, struct net_device *dev, netif_carrier_off(dev); priv->upperdev = dev; - priv->lowerdev = __dev_get_by_index(src_net, + priv->lowerdev = __dev_get_by_index(link_net, nla_get_u32(tb[IFLA_LINK])); if (!priv->lowerdev) diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c index a51e2755991a..450cf2e253e4 100644 --- a/drivers/net/wwan/wwan_core.c +++ b/drivers/net/wwan/wwan_core.c @@ -967,10 +967,11 @@ static struct net_device *wwan_rtnl_alloc(struct nlattr *tb[], return dev; } -static int wwan_rtnl_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int wwan_rtnl_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent); u32 link_id = nla_get_u32(data[IFLA_WWAN_LINK_ID]); struct wwan_netdev_priv *priv = netdev_priv(dev); @@ -1064,6 +1065,11 @@ static void wwan_create_default_link(struct wwan_device *wwandev, struct net_device *dev; struct nlmsghdr *nlh; struct sk_buff *msg; + struct rtnl_newlink_params params = { + .src_net = &init_net, + .tb = tb, + .data = data, + }; /* Forge attributes required to create a WWAN netdev. We first * build a netlink message and then parse it. This looks @@ -1105,7 +1111,8 @@ static void wwan_create_default_link(struct wwan_device *wwandev, if (WARN_ON(IS_ERR(dev))) goto unlock; - if (WARN_ON(wwan_rtnl_newlink(&init_net, dev, tb, data, NULL))) { + params.dev = dev; + if (WARN_ON(wwan_rtnl_newlink(¶ms))) { free_netdev(dev); goto unlock; } diff --git a/include/net/ip_tunnels.h b/include/net/ip_tunnels.h index 1aa31bdb2b31..ae1f2dda4533 100644 --- a/include/net/ip_tunnels.h +++ b/include/net/ip_tunnels.h @@ -406,8 +406,9 @@ int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb, bool log_ecn_error); int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[], struct ip_tunnel_parm_kern *p, __u32 fwmark); -int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[], - struct ip_tunnel_parm_kern *p, __u32 fwmark); +int ip_tunnel_newlink(struct net *net, struct net_device *dev, + struct nlattr *tb[], struct ip_tunnel_parm_kern *p, + __u32 fwmark); void ip_tunnel_setup(struct net_device *dev, unsigned int net_id); bool ip_tunnel_netlink_encap_parms(struct nlattr *data[], diff --git a/include/net/rtnetlink.h b/include/net/rtnetlink.h index bc0069a8b6ea..04fc0e91af42 100644 --- a/include/net/rtnetlink.h +++ b/include/net/rtnetlink.h @@ -69,6 +69,44 @@ static inline int rtnl_msg_family(const struct nlmsghdr *nlh) return AF_UNSPEC; } +/** + * struct rtnl_newlink_params - parameters of rtnl_link_ops::newlink() + * + * @src_net: Source netns of rtnetlink socket + * @link_net: Link netns by IFLA_LINK_NETNSID, NULL if not specified + * @peer_net: Peer netns + * @dev: The net_device being created + * @tb: IFLA_* attributes + * @data: IFLA_INFO_DATA attributes + * @extack: Netlink extended ACK + */ +struct rtnl_newlink_params { + struct net *src_net; + struct net *link_net; + struct net *peer_net; + struct net_device *dev; + struct nlattr **tb; + struct nlattr **data; + struct netlink_ext_ack *extack; +}; + +/* Get effective link netns from newlink params. Generally, this is link_net + * and falls back to src_net. But for compatibility, a driver may * choose to + * use dev_net(dev) instead. + */ +static inline struct net *rtnl_newlink_link_net(struct rtnl_newlink_params *p) +{ + return p->link_net ? : p->src_net; +} + +/* Get peer netns from newlink params. Fallback to link netns if peer netns is + * not specified explicitly. + */ +static inline struct net *rtnl_newlink_peer_net(struct rtnl_newlink_params *p) +{ + return p->peer_net ? : rtnl_newlink_link_net(p); +} + /** * struct rtnl_link_ops - rtnetlink link operations * @@ -125,11 +163,7 @@ struct rtnl_link_ops { struct nlattr *data[], struct netlink_ext_ack *extack); - int (*newlink)(struct net *src_net, - struct net_device *dev, - struct nlattr *tb[], - struct nlattr *data[], - struct netlink_ext_ack *extack); + int (*newlink)(struct rtnl_newlink_params *params); int (*changelink)(struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], diff --git a/net/8021q/vlan_netlink.c b/net/8021q/vlan_netlink.c index 134419667d59..603b2ea1e2b4 100644 --- a/net/8021q/vlan_netlink.c +++ b/net/8021q/vlan_netlink.c @@ -135,10 +135,13 @@ static int vlan_changelink(struct net_device *dev, struct nlattr *tb[], return 0; } -static int vlan_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int vlan_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; + struct net *link_net = rtnl_newlink_link_net(params); struct vlan_dev_priv *vlan = vlan_dev_priv(dev); struct net_device *real_dev; unsigned int max_mtu; @@ -155,7 +158,7 @@ static int vlan_newlink(struct net *src_net, struct net_device *dev, return -EINVAL; } - real_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); + real_dev = __dev_get_by_index(link_net, nla_get_u32(tb[IFLA_LINK])); if (!real_dev) { NL_SET_ERR_MSG_MOD(extack, "link does not exist"); return -ENODEV; diff --git a/net/batman-adv/soft-interface.c b/net/batman-adv/soft-interface.c index 2758aba47a2f..c411b8857095 100644 --- a/net/batman-adv/soft-interface.c +++ b/net/batman-adv/soft-interface.c @@ -1063,18 +1063,14 @@ static int batadv_softif_validate(struct nlattr *tb[], struct nlattr *data[], /** * batadv_softif_newlink() - pre-initialize and register new batadv link - * @src_net: the applicable net namespace - * @dev: network device to register - * @tb: IFLA_INFO_DATA netlink attributes - * @data: enum batadv_ifla_attrs attributes - * @extack: extended ACK report struct + * @params: rtnl newlink parameters * * Return: 0 if successful or error otherwise. */ -static int batadv_softif_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int batadv_softif_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **data = params->data; struct batadv_priv *bat_priv = netdev_priv(dev); const char *algo_name; int err; diff --git a/net/bridge/br_netlink.c b/net/bridge/br_netlink.c index 3e0f47203f2a..ccce5119b28d 100644 --- a/net/bridge/br_netlink.c +++ b/net/bridge/br_netlink.c @@ -1553,10 +1553,12 @@ static int br_changelink(struct net_device *brdev, struct nlattr *tb[], return 0; } -static int br_dev_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int br_dev_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; struct net_bridge *br = netdev_priv(dev); int err; diff --git a/net/caif/chnl_net.c b/net/caif/chnl_net.c index 94ad09e36df2..748e38908709 100644 --- a/net/caif/chnl_net.c +++ b/net/caif/chnl_net.c @@ -438,10 +438,10 @@ static void caif_netlink_parms(struct nlattr *data[], } } -static int ipcaif_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int ipcaif_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **data = params->data; int ret; struct chnl_net *caifdev; ASSERT_RTNL(); diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index 7855f81c917b..3ea63722d0fd 100644 --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c @@ -3757,6 +3757,14 @@ static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm, struct net_device *dev; char ifname[IFNAMSIZ]; int err; + struct rtnl_newlink_params params = { + .src_net = net, + .link_net = link_net, + .peer_net = peer_net, + .tb = tb, + .data = data, + .extack = extack, + }; if (!ops->alloc && !ops->setup) return -EOPNOTSUPP; @@ -3768,22 +3776,18 @@ static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm, name_assign_type = NET_NAME_ENUM; } - dev = rtnl_create_link(link_net ? : tgt_net, ifname, - name_assign_type, ops, tb, extack); + dev = rtnl_create_link(tgt_net, ifname, name_assign_type, ops, tb, + extack); if (IS_ERR(dev)) { err = PTR_ERR(dev); goto out; } dev->ifindex = ifm->ifi_index; - - if (link_net) - net = link_net; - if (peer_net) - net = peer_net; + params.dev = dev; if (ops->newlink) - err = ops->newlink(net, dev, tb, data, extack); + err = ops->newlink(¶ms); else err = register_netdevice(dev); if (err < 0) { @@ -3794,11 +3798,6 @@ static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm, err = rtnl_configure_link(dev, ifm, portid, nlh); if (err < 0) goto out_unregister; - if (link_net) { - err = dev_change_net_namespace(dev, tgt_net, ifname); - if (err < 0) - goto out_unregister; - } if (tb[IFLA_MASTER]) { err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); if (err) diff --git a/net/hsr/hsr_netlink.c b/net/hsr/hsr_netlink.c index b68f2f71d0e1..694392222637 100644 --- a/net/hsr/hsr_netlink.c +++ b/net/hsr/hsr_netlink.c @@ -29,10 +29,12 @@ static const struct nla_policy hsr_policy[IFLA_HSR_MAX + 1] = { /* Here, it seems a netdevice has already been allocated for us, and the * hsr_dev_setup routine has been executed. Nice! */ -static int hsr_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int hsr_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; + struct net *link_net = rtnl_newlink_link_net(params); enum hsr_version proto_version; unsigned char multicast_spec; u8 proto = HSR_PROTOCOL_HSR; @@ -46,7 +48,7 @@ static int hsr_newlink(struct net *src_net, struct net_device *dev, NL_SET_ERR_MSG_MOD(extack, "Slave1 device not specified"); return -EINVAL; } - link[0] = __dev_get_by_index(src_net, + link[0] = __dev_get_by_index(link_net, nla_get_u32(data[IFLA_HSR_SLAVE1])); if (!link[0]) { NL_SET_ERR_MSG_MOD(extack, "Slave1 does not exist"); @@ -56,7 +58,7 @@ static int hsr_newlink(struct net *src_net, struct net_device *dev, NL_SET_ERR_MSG_MOD(extack, "Slave2 device not specified"); return -EINVAL; } - link[1] = __dev_get_by_index(src_net, + link[1] = __dev_get_by_index(link_net, nla_get_u32(data[IFLA_HSR_SLAVE2])); if (!link[1]) { NL_SET_ERR_MSG_MOD(extack, "Slave2 does not exist"); @@ -69,7 +71,7 @@ static int hsr_newlink(struct net *src_net, struct net_device *dev, } if (data[IFLA_HSR_INTERLINK]) - interlink = __dev_get_by_index(src_net, + interlink = __dev_get_by_index(link_net, nla_get_u32(data[IFLA_HSR_INTERLINK])); if (interlink && interlink == link[0]) { diff --git a/net/ieee802154/6lowpan/core.c b/net/ieee802154/6lowpan/core.c index 175efd860f7b..65a5c61cf38c 100644 --- a/net/ieee802154/6lowpan/core.c +++ b/net/ieee802154/6lowpan/core.c @@ -129,10 +129,10 @@ static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[], return 0; } -static int lowpan_newlink(struct net *src_net, struct net_device *ldev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int lowpan_newlink(struct rtnl_newlink_params *params) { + struct net_device *ldev = params->dev; + struct nlattr **tb = params->tb; struct net_device *wdev; int ret; @@ -143,7 +143,8 @@ static int lowpan_newlink(struct net *src_net, struct net_device *ldev, if (!tb[IFLA_LINK]) return -EINVAL; /* find and hold wpan device */ - wdev = dev_get_by_index(dev_net(ldev), nla_get_u32(tb[IFLA_LINK])); + wdev = dev_get_by_index(params->link_net ? : dev_net(ldev), + nla_get_u32(tb[IFLA_LINK])); if (!wdev) return -ENODEV; if (wdev->type != ARPHRD_IEEE802154) { diff --git a/net/ipv4/ip_gre.c b/net/ipv4/ip_gre.c index f1f31ebfc793..4a3f8e450ef5 100644 --- a/net/ipv4/ip_gre.c +++ b/net/ipv4/ip_gre.c @@ -1389,10 +1389,12 @@ ipgre_newlink_encap_setup(struct net_device *dev, struct nlattr *data[]) return 0; } -static int ipgre_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int ipgre_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct net *net = params->link_net ? : dev_net(dev); struct ip_tunnel_parm_kern p; __u32 fwmark = 0; int err; @@ -1404,13 +1406,15 @@ static int ipgre_newlink(struct net *src_net, struct net_device *dev, err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark); if (err < 0) return err; - return ip_tunnel_newlink(dev, tb, &p, fwmark); + return ip_tunnel_newlink(net, dev, tb, &p, fwmark); } -static int erspan_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int erspan_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct net *net = params->link_net ? : dev_net(dev); struct ip_tunnel_parm_kern p; __u32 fwmark = 0; int err; @@ -1422,7 +1426,7 @@ static int erspan_newlink(struct net *src_net, struct net_device *dev, err = erspan_netlink_parms(dev, data, tb, &p, &fwmark); if (err) return err; - return ip_tunnel_newlink(dev, tb, &p, fwmark); + return ip_tunnel_newlink(net, dev, tb, &p, fwmark); } static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[], @@ -1695,6 +1699,10 @@ struct net_device *gretap_fb_dev_create(struct net *net, const char *name, LIST_HEAD(list_kill); struct ip_tunnel *t; int err; + struct rtnl_newlink_params params = { + .src_net = net, + .tb = tb, + }; memset(&tb, 0, sizeof(tb)); @@ -1707,7 +1715,8 @@ struct net_device *gretap_fb_dev_create(struct net *net, const char *name, t = netdev_priv(dev); t->collect_md = true; - err = ipgre_newlink(net, dev, tb, NULL, NULL); + params.dev = dev; + err = ipgre_newlink(¶ms); if (err < 0) { free_netdev(dev); return ERR_PTR(err); diff --git a/net/ipv4/ip_tunnel.c b/net/ipv4/ip_tunnel.c index 09b73acf037a..618a50d5c0c2 100644 --- a/net/ipv4/ip_tunnel.c +++ b/net/ipv4/ip_tunnel.c @@ -1213,11 +1213,11 @@ void ip_tunnel_delete_nets(struct list_head *net_list, unsigned int id, } EXPORT_SYMBOL_GPL(ip_tunnel_delete_nets); -int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[], - struct ip_tunnel_parm_kern *p, __u32 fwmark) +int ip_tunnel_newlink(struct net *net, struct net_device *dev, + struct nlattr *tb[], struct ip_tunnel_parm_kern *p, + __u32 fwmark) { struct ip_tunnel *nt; - struct net *net = dev_net(dev); struct ip_tunnel_net *itn; int mtu; int err; @@ -1326,7 +1326,9 @@ int ip_tunnel_init(struct net_device *dev) } tunnel->dev = dev; - tunnel->net = dev_net(dev); + if (!tunnel->net) + tunnel->net = dev_net(dev); + strscpy(tunnel->parms.name, dev->name); iph->version = 4; iph->ihl = 5; diff --git a/net/ipv4/ip_vti.c b/net/ipv4/ip_vti.c index f0b4419cef34..b567e2375302 100644 --- a/net/ipv4/ip_vti.c +++ b/net/ipv4/ip_vti.c @@ -575,15 +575,17 @@ static void vti_netlink_parms(struct nlattr *data[], *fwmark = nla_get_u32(data[IFLA_VTI_FWMARK]); } -static int vti_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int vti_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; struct ip_tunnel_parm_kern parms; __u32 fwmark = 0; vti_netlink_parms(data, &parms, &fwmark); - return ip_tunnel_newlink(dev, tb, &parms, fwmark); + return ip_tunnel_newlink(params->link_net ? : dev_net(dev), dev, tb, + &parms, fwmark); } static int vti_changelink(struct net_device *dev, struct nlattr *tb[], diff --git a/net/ipv4/ipip.c b/net/ipv4/ipip.c index dc0db5895e0e..9dccaa0d6ba7 100644 --- a/net/ipv4/ipip.c +++ b/net/ipv4/ipip.c @@ -436,10 +436,11 @@ static void ipip_netlink_parms(struct nlattr *data[], *fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]); } -static int ipip_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int ipip_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; struct ip_tunnel *t = netdev_priv(dev); struct ip_tunnel_encap ipencap; struct ip_tunnel_parm_kern p; @@ -453,7 +454,8 @@ static int ipip_newlink(struct net *src_net, struct net_device *dev, } ipip_netlink_parms(data, &p, &t->collect_md, &fwmark); - return ip_tunnel_newlink(dev, tb, &p, fwmark); + return ip_tunnel_newlink(params->link_net ? : dev_net(dev), dev, tb, &p, + fwmark); } static int ipip_changelink(struct net_device *dev, struct nlattr *tb[], diff --git a/net/ipv6/ip6_gre.c b/net/ipv6/ip6_gre.c index 235808cfec70..7d6d3db200a1 100644 --- a/net/ipv6/ip6_gre.c +++ b/net/ipv6/ip6_gre.c @@ -1971,7 +1971,7 @@ static bool ip6gre_netlink_encap_parms(struct nlattr *data[], return ret; } -static int ip6gre_newlink_common(struct net *src_net, struct net_device *dev, +static int ip6gre_newlink_common(struct net *link_net, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -1992,7 +1992,7 @@ static int ip6gre_newlink_common(struct net *src_net, struct net_device *dev, eth_hw_addr_random(dev); nt->dev = dev; - nt->net = dev_net(dev); + nt->net = link_net; err = register_netdevice(dev); if (err) @@ -2005,12 +2005,14 @@ static int ip6gre_newlink_common(struct net *src_net, struct net_device *dev, return err; } -static int ip6gre_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int ip6gre_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; struct ip6_tnl *nt = netdev_priv(dev); - struct net *net = dev_net(dev); + struct net *net = params->link_net ? : dev_net(dev); struct ip6gre_net *ign; int err; @@ -2025,7 +2027,7 @@ static int ip6gre_newlink(struct net *src_net, struct net_device *dev, return -EEXIST; } - err = ip6gre_newlink_common(src_net, dev, tb, data, extack); + err = ip6gre_newlink_common(net, dev, tb, data, extack); if (!err) { ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]); ip6gre_tunnel_link_md(ign, nt); @@ -2241,12 +2243,14 @@ static void ip6erspan_tap_setup(struct net_device *dev) netif_keep_dst(dev); } -static int ip6erspan_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int ip6erspan_newlink(struct rtnl_newlink_params *params) { + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; struct ip6_tnl *nt = netdev_priv(dev); - struct net *net = dev_net(dev); + struct net *net = params->link_net ? : dev_net(dev); struct ip6gre_net *ign; int err; @@ -2262,7 +2266,7 @@ static int ip6erspan_newlink(struct net *src_net, struct net_device *dev, return -EEXIST; } - err = ip6gre_newlink_common(src_net, dev, tb, data, extack); + err = ip6gre_newlink_common(net, dev, tb, data, extack); if (!err) { ip6erspan_tnl_link_config(nt, !tb[IFLA_MTU]); ip6erspan_tunnel_link_md(ign, nt); diff --git a/net/ipv6/ip6_tunnel.c b/net/ipv6/ip6_tunnel.c index 48fd53b98972..33a58c3c9ebe 100644 --- a/net/ipv6/ip6_tunnel.c +++ b/net/ipv6/ip6_tunnel.c @@ -250,10 +250,9 @@ static void ip6_dev_free(struct net_device *dev) dst_cache_destroy(&t->dst_cache); } -static int ip6_tnl_create2(struct net_device *dev) +static int ip6_tnl_create2(struct net *net, struct net_device *dev) { struct ip6_tnl *t = netdev_priv(dev); - struct net *net = dev_net(dev); struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); int err; @@ -308,7 +307,7 @@ static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p) t = netdev_priv(dev); t->parms = *p; t->net = dev_net(dev); - err = ip6_tnl_create2(dev); + err = ip6_tnl_create2(net, dev); if (err < 0) goto failed_free; @@ -2002,11 +2001,12 @@ static void ip6_tnl_netlink_parms(struct nlattr *data[], parms->fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]); } -static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int ip6_tnl_newlink(struct rtnl_newlink_params *params) { - struct net *net = dev_net(dev); + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct net *net = params->link_net ? : dev_net(dev); struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); struct ip_tunnel_encap ipencap; struct ip6_tnl *nt, *t; @@ -2031,7 +2031,7 @@ static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev, return -EEXIST; } - err = ip6_tnl_create2(dev); + err = ip6_tnl_create2(net, dev); if (!err && tb[IFLA_MTU]) ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU])); diff --git a/net/ipv6/ip6_vti.c b/net/ipv6/ip6_vti.c index 590737c27537..ff9dc74819c5 100644 --- a/net/ipv6/ip6_vti.c +++ b/net/ipv6/ip6_vti.c @@ -174,10 +174,9 @@ vti6_tnl_unlink(struct vti6_net *ip6n, struct ip6_tnl *t) } } -static int vti6_tnl_create2(struct net_device *dev) +static int vti6_tnl_create2(struct net *net, struct net_device *dev) { struct ip6_tnl *t = netdev_priv(dev); - struct net *net = dev_net(dev); struct vti6_net *ip6n = net_generic(net, vti6_net_id); int err; @@ -221,7 +220,7 @@ static struct ip6_tnl *vti6_tnl_create(struct net *net, struct __ip6_tnl_parm *p t->parms = *p; t->net = dev_net(dev); - err = vti6_tnl_create2(dev); + err = vti6_tnl_create2(net, dev); if (err < 0) goto failed_free; @@ -997,11 +996,11 @@ static void vti6_netlink_parms(struct nlattr *data[], parms->fwmark = nla_get_u32(data[IFLA_VTI_FWMARK]); } -static int vti6_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int vti6_newlink(struct rtnl_newlink_params *params) { - struct net *net = dev_net(dev); + struct net_device *dev = params->dev; + struct nlattr **data = params->data; + struct net *net = params->link_net ? : dev_net(dev); struct ip6_tnl *nt; nt = netdev_priv(dev); @@ -1012,7 +1011,7 @@ static int vti6_newlink(struct net *src_net, struct net_device *dev, if (vti6_locate(net, &nt->parms, 0)) return -EEXIST; - return vti6_tnl_create2(dev); + return vti6_tnl_create2(net, dev); } static void vti6_dellink(struct net_device *dev, struct list_head *head) diff --git a/net/ipv6/sit.c b/net/ipv6/sit.c index 39bd8951bfca..cbcaccbfc3c9 100644 --- a/net/ipv6/sit.c +++ b/net/ipv6/sit.c @@ -198,10 +198,9 @@ static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn) #endif } -static int ipip6_tunnel_create(struct net_device *dev) +static int ipip6_tunnel_create(struct net *net, struct net_device *dev) { struct ip_tunnel *t = netdev_priv(dev); - struct net *net = dev_net(dev); struct sit_net *sitn = net_generic(net, sit_net_id); int err; @@ -270,7 +269,7 @@ static struct ip_tunnel *ipip6_tunnel_locate(struct net *net, nt = netdev_priv(dev); nt->parms = *parms; - if (ipip6_tunnel_create(dev) < 0) + if (ipip6_tunnel_create(net, dev) < 0) goto failed_free; if (!parms->name[0]) @@ -1550,11 +1549,12 @@ static bool ipip6_netlink_6rd_parms(struct nlattr *data[], } #endif -static int ipip6_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int ipip6_newlink(struct rtnl_newlink_params *params) { - struct net *net = dev_net(dev); + struct net_device *dev = params->dev; + struct nlattr **tb = params->tb; + struct nlattr **data = params->data; + struct net *net = params->link_net ? : dev_net(dev); struct ip_tunnel *nt; struct ip_tunnel_encap ipencap; #ifdef CONFIG_IPV6_SIT_6RD @@ -1575,7 +1575,7 @@ static int ipip6_newlink(struct net *src_net, struct net_device *dev, if (ipip6_tunnel_locate(net, &nt->parms, 0)) return -EEXIST; - err = ipip6_tunnel_create(dev); + err = ipip6_tunnel_create(net, dev); if (err < 0) return err; diff --git a/net/xfrm/xfrm_interface_core.c b/net/xfrm/xfrm_interface_core.c index 98f1e2b67c76..d1f2674a98c8 100644 --- a/net/xfrm/xfrm_interface_core.c +++ b/net/xfrm/xfrm_interface_core.c @@ -242,10 +242,9 @@ static void xfrmi_dev_free(struct net_device *dev) gro_cells_destroy(&xi->gro_cells); } -static int xfrmi_create(struct net_device *dev) +static int xfrmi_create(struct net *net, struct net_device *dev) { struct xfrm_if *xi = netdev_priv(dev); - struct net *net = dev_net(dev); struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id); int err; @@ -814,11 +813,12 @@ static void xfrmi_netlink_parms(struct nlattr *data[], parms->collect_md = true; } -static int xfrmi_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int xfrmi_newlink(struct rtnl_newlink_params *params) { - struct net *net = dev_net(dev); + struct net_device *dev = params->dev; + struct nlattr **data = params->data; + struct netlink_ext_ack *extack = params->extack; + struct net *net = params->link_net ? : dev_net(dev); struct xfrm_if_parms p = {}; struct xfrm_if *xi; int err; @@ -851,7 +851,7 @@ static int xfrmi_newlink(struct net *src_net, struct net_device *dev, xi->net = net; xi->dev = dev; - err = xfrmi_create(dev); + err = xfrmi_create(net, dev); return err; } From patchwork Mon Dec 9 14:01:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Liang X-Patchwork-Id: 18718 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 B46E783CAB for ; Mon, 9 Dec 2024 17:51:13 +0100 (CET) ARC-Seal: i=2; cv=pass; a=rsa-sha256; d=open-mesh.org; s=20121; t=1733763073; b=1jj/5UaxOc41a2VE1qes8JXWLWL9vNLuAfa7APte0FZGFO0sCRsDHOS3UaHlv6You5Vck CgPGbOOKxnhIKpoUQwCHTGyKxX9j147q7fWseUe7HVCEsHyxhdhjFo+/y0rLsfr8O+YK1jT JZoQJ+ubwAU1UL1yJkMklwxabwccSeM= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1733763073; h=from : sender : reply-to : subject : date : message-id : to : cc : mime-version : content-type : content-transfer-encoding : content-id : content-description : resent-date : resent-from : resent-sender : resent-to : resent-cc : resent-message-id : in-reply-to : references : list-id : list-help : list-unsubscribe : list-subscribe : list-post : list-owner : list-archive; bh=f/aVoxIsj2NaHZxO0wthneclpZNQFTaEi7bz2gcdad0=; b=yXLdgIt0WhJwLTspKYhvobuBoCwoKTQV0aELch+9EO02kT5YIVzE0NBSB+HTXe65eNXqI V8wg0k1kzSzHMp52ShzxF1NgkHcsMBK5uGsfVKnNqp29D26rZGWmG/x6hqjoKR+KXDEZ/VU FaewRFdvJwlfHPF2oIKK4xUKT42k3jM= ARC-Authentication-Results: i=2; open-mesh.org; dkim=pass header.d=gmail.com; arc=pass; dmarc=pass (Used From Domain Record) header.from=gmail.com policy.dmarc=quarantine Authentication-Results: open-mesh.org; dkim=pass header.d=gmail.com; arc=pass; dmarc=pass (Used From Domain Record) header.from=gmail.com policy.dmarc=quarantine Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by diktynna.open-mesh.org (Postfix) with ESMTPS id 9ADB583BDD for ; Mon, 9 Dec 2024 15:06:07 +0100 (CET) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1733753168; 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=f/aVoxIsj2NaHZxO0wthneclpZNQFTaEi7bz2gcdad0=; b=Hn2MT2gghIOaLBEHpNtEK2S1Dt0c21rroJtYJ0kB/KaKmqCyFqYM45uRy1hEh51s+rDvNG W5YMSsO98fHgzAOBfiHWszk2DVBWKi7q+Z/j/g6m5/FM2RBQ3vJ+EeHw9u4w5aJ5y6PQvT kyBwHbxH1lFdaN/caAP48zb3WeAgAXI= ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1733753168; a=rsa-sha256; cv=none; b=AAJP+Bsvx19aCAwzEcG1AM9SMxW3+dn9W6aaPGn4cSUC2H4Ss0GbpVoDEE6mz7lfI/K8FP qHh9FIkRFFlvi9DgaxyHHcj/D/7KLoAQA+w5McJSnkGpoqH2KKnTFVs+VKB1+0iEsA2fo7 1nAN623saVIplz/8M3Rq5oDsVQ/G/Io= ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=gmail.com header.s=20230601 header.b=lvjHixRr; spf=pass (diktynna.open-mesh.org: domain of shaw.leon@gmail.com designates 2607:f8b0:4864:20::632 as permitted sender) smtp.mailfrom=shaw.leon@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-21631789fcdso10136175ad.1 for ; Mon, 09 Dec 2024 06:06:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733753166; x=1734357966; darn=lists.open-mesh.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=f/aVoxIsj2NaHZxO0wthneclpZNQFTaEi7bz2gcdad0=; b=lvjHixRr83t4n0I1yB9IsWliO0R/teIEkd5RMNQA6SZR5EF9vwOOZzOd0ChVstLxAk 7lVHcPthSiBM7lzn78EXWYRQilJJKMLhd5heI7IVMiawoh/BIDRgPp+SgQsAC8aNJ4b8 hoktwckKN/zX+XZMn1nQYDWhjUWGKqnp8e8zXBjtCY2Eh/zTQK55rOq1khWxuHj4jmSs 7BPZ5zxOyRue5BgeKEgdy1Ee6qQkX5J/ukjkAz8CwHSksahet8VLIj/DSVVYSsFWyBTI xu8XyefRHAk5lDHqVaM+yZ1w35trbcjfkM9vepHIw0uodjCWwTmmAPbCvSvhmHvqF2X/ Q4+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733753166; x=1734357966; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=f/aVoxIsj2NaHZxO0wthneclpZNQFTaEi7bz2gcdad0=; b=UIRPdYZ4vEM4Mjjojo8JbdSokG4pH+EL1RPowteShtcoOG4BuZ+M3hKalQnfrjeKw5 VWgtNIp59sjr/yCBwJ+jKdCy0RT3WlSiGGlHID3d20lf8sFcMRwluqIdNT1X5yWZUmB8 iYoNL3vOXBAKDrJVUyUrWiKObAA/DRelJf+JNhGrl3YbrNFSv974F2Oc438K7t2CppIg 4OLNglDVKDnEShhxjToqOce171yJztiFWPUBEN/9BsWYX5afva9JbCZH7hEZgeA41+lu /dFErh4KJaVwaY/A0InjQXQrxt9uv4gPqJxehEP0wkkY9aB4Y1vanpllwMPsW+zM6WM/ nBkA== X-Forwarded-Encrypted: i=1; AJvYcCUAD2ZvadJztS1RK/v6r1Zs8Fw0wnzWTmJTlVmcNOOhPdn7kB/2+2ffwG7HdFCk/+Hs3x8UxUjvmIw1Dw==@lists.open-mesh.org X-Gm-Message-State: AOJu0YymyqN6eOBQxI+pmArDqRUBvqI7etgEJq56h6LXVeAQES48FL8x tAAbX/T82t6Mbi/Xk8yM/hVPcm4lKC6EEPLcLv17MQ+JyujIEajL X-Gm-Gg: ASbGncsIdONHT4DmkwRNAsyTT5HZ62y+rycORd5SRBxLe+Gdgj5GKg86/SnKD7HnQHi UP/w+0RPopxSFBcc4RSK4sICZ9DYoZ0cS4idmcOtSfnijhsVtB0pXWEWKZjV+fPmbUh8edMlY3K dcEzXnW3CqnItpKWWS8YYyKOusxummbhoxu1OPDneZWW1aKgF7XOetsu+iCa0tTeQaiLNSxYZV4 s30228J9cLI/dOMdQ/SwsEHESx21XtD4zJe+2dOz8wtLwQ= X-Google-Smtp-Source: AGHT+IEgO+eAsdpey+DR5/LF2b2LrxgYAnjIwOd9wPEert31MRK25WTtIv1pjQ5j1prur61gIiJ4iQ== X-Received: by 2002:a17:902:c949:b0:215:58be:334e with SMTP id d9443c01a7336-21610b3539amr198169675ad.10.1733753165835; Mon, 09 Dec 2024 06:06:05 -0800 (PST) Received: from nova-ws.. ([103.167.140.11]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-216221db645sm49605645ad.46.2024.12.09.06.05.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Dec 2024 06:06:05 -0800 (PST) From: Xiao Liang To: netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, Kuniyuki Iwashima , Jakub Kicinski , Donald Hunter Cc: "David S. Miller" , David Ahern , Eric Dumazet , Paolo Abeni , Ido Schimmel , Andrew Lunn , Simon Horman , Shuah Khan , Jiri Pirko , Hangbin Liu , linux-rdma@vger.kernel.org, linux-can@vger.kernel.org, osmocom-net-gprs@lists.osmocom.org, bpf@vger.kernel.org, linux-ppp@vger.kernel.org, wireguard@lists.zx2c4.com, linux-wireless@vger.kernel.org, b.a.t.m.a.n@lists.open-mesh.org, bridge@lists.linux.dev, linux-wpan@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next v5 4/5] selftests: net: Add python context manager for netns entering Date: Mon, 9 Dec 2024 22:01:50 +0800 Message-ID: <20241209140151.231257-5-shaw.leon@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241209140151.231257-1-shaw.leon@gmail.com> References: <20241209140151.231257-1-shaw.leon@gmail.com> MIME-Version: 1.0 X-MailFrom: shaw.leon@gmail.com X-Mailman-Rule-Hits: nonmember-moderation 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; header-match-b.a.t.m.a.n.lists.open-mesh.org-2 Message-ID-Hash: S3MZCAE55R6O3545M7UCLUTYYXYKWGZZ X-Message-ID-Hash: S3MZCAE55R6O3545M7UCLUTYYXYKWGZZ X-Mailman-Approved-At: Mon, 09 Dec 2024 17:47:40 +0100 X-Mailman-Version: 3.3.8 Precedence: list List-Id: The list for a Better Approach To Mobile Ad-hoc Networking Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Change netns of current thread and switch back on context exit. For example: with NetNSEnter("ns1"): ip("link add dummy0 type dummy") The command be executed in netns "ns1". Signed-off-by: Xiao Liang --- tools/testing/selftests/net/lib/py/__init__.py | 2 +- tools/testing/selftests/net/lib/py/netns.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/net/lib/py/__init__.py b/tools/testing/selftests/net/lib/py/__init__.py index 54d8f5eba810..e2d6c7b63019 100644 --- a/tools/testing/selftests/net/lib/py/__init__.py +++ b/tools/testing/selftests/net/lib/py/__init__.py @@ -2,7 +2,7 @@ from .consts import KSRC from .ksft import * -from .netns import NetNS +from .netns import NetNS, NetNSEnter from .nsim import * from .utils import * from .ynl import NlError, YnlFamily, EthtoolFamily, NetdevFamily, RtnlFamily diff --git a/tools/testing/selftests/net/lib/py/netns.py b/tools/testing/selftests/net/lib/py/netns.py index ecff85f9074f..8e9317044eef 100644 --- a/tools/testing/selftests/net/lib/py/netns.py +++ b/tools/testing/selftests/net/lib/py/netns.py @@ -1,9 +1,12 @@ # SPDX-License-Identifier: GPL-2.0 from .utils import ip +import ctypes import random import string +libc = ctypes.cdll.LoadLibrary('libc.so.6') + class NetNS: def __init__(self, name=None): @@ -29,3 +32,18 @@ class NetNS: def __repr__(self): return f"NetNS({self.name})" + + +class NetNSEnter: + def __init__(self, ns_name): + self.ns_path = f"/run/netns/{ns_name}" + + def __enter__(self): + self.saved = open("/proc/thread-self/ns/net") + with open(self.ns_path) as ns_file: + libc.setns(ns_file.fileno(), 0) + return self + + def __exit__(self, exc_type, exc_value, traceback): + libc.setns(self.saved.fileno(), 0) + self.saved.close() From patchwork Mon Dec 9 14:01:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Liang X-Patchwork-Id: 18719 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 F3D5683FDF for ; Mon, 9 Dec 2024 17:52:02 +0100 (CET) ARC-Seal: i=2; cv=pass; a=rsa-sha256; d=open-mesh.org; s=20121; t=1733763122; b=WsBTSV9P2fDN5fxWof18yJBJ8y7C+LFHPmjbWWiBqrty4EzOJp+6M8HNteE2ZtRtZOL5p fiFuzAnRP5FFbQE8KSvsh2mszaJxHtdTq313rDsBvRXrpxMRCVhnzedHHJzbLlMwPpFrBoH fFqhxCCvXEDZe68u9kZGdJNnS8y9tKE= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1733763122; h=from : sender : reply-to : subject : date : message-id : to : cc : mime-version : content-type : content-transfer-encoding : content-id : content-description : resent-date : resent-from : resent-sender : resent-to : resent-cc : resent-message-id : in-reply-to : references : list-id : list-help : list-unsubscribe : list-subscribe : list-post : list-owner : list-archive; bh=IZacjWdb8RyenSpXv6Caz5kcIhA6mwKRd5G/S3r2hOU=; b=ztmmPf/8I8/E4eM4vKjrL8mzOCz4Ny6EJOvXFft22nJi1+aIVxmmnyk3s5fXQQaspUAnO ewbmp8WxN6o161hl9vrfYxr/P5FbUOpggWFJvfOX3o1qfDeHuwDye2n8KeoDOEaTIj/FIw3 XMdy+j777Hg8mMna9n1xnfq265p5TXQ= ARC-Authentication-Results: i=2; open-mesh.org; dkim=pass header.d=gmail.com; arc=pass; dmarc=pass (Used From Domain Record) header.from=gmail.com policy.dmarc=quarantine Authentication-Results: open-mesh.org; dkim=pass header.d=gmail.com; arc=pass; dmarc=pass (Used From Domain Record) header.from=gmail.com policy.dmarc=quarantine Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by diktynna.open-mesh.org (Postfix) with ESMTPS id 0F47683C01 for ; Mon, 9 Dec 2024 15:06:15 +0100 (CET) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1733753176; 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=IZacjWdb8RyenSpXv6Caz5kcIhA6mwKRd5G/S3r2hOU=; b=rl5ujaVS1OGMehztd+jUum3h0JVXa/+XNfkmS7RIwCmiDSMwUIp2PVvvp0D+jZFvw9K0RY udCICbADQQNT9V7+hUBqnkB34MW3p0kDuSchzaBkD7tyUB655DjcqfTcEQb1wUnAmvKuuL u9hL2HxreOup+cTDbQGqfbjL10QPQLk= ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1733753176; a=rsa-sha256; cv=none; b=lX52C25ae+YrxgV4UBIO9lY63XG7r5dU2ndqfYPUN8NWFe+v1z0UAQjUnBAh5UPRbcb59H MOYav29Oln+QEbikfuwVsJOG197EjtupykQk30CJm0hTab5d2c1OXJVs4Rqqc1zvPpeyAD QZ9gwexpBnMhrQJFAogD2A8gWXmjoVU= ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=gmail.com header.s=20230601 header.b=XayYTEes; spf=pass (diktynna.open-mesh.org: domain of shaw.leon@gmail.com designates 2607:f8b0:4864:20::634 as permitted sender) smtp.mailfrom=shaw.leon@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-2166651f752so4327835ad.3 for ; Mon, 09 Dec 2024 06:06:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733753174; x=1734357974; darn=lists.open-mesh.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=IZacjWdb8RyenSpXv6Caz5kcIhA6mwKRd5G/S3r2hOU=; b=XayYTEesNnx7dMRYDMAJzyJIbacyzfeDyBUSerr94HoKo/UO3uh84fCpUf+AJvIH7M n3YhUZoh9drL0Dx6aRuhP8sgsR/u0nJdt4ahWeAb2VZ3A0gZwInboo3kysoGONAr6xxu eOfgOZE2ehqR46tF9itHAcDQuWw9FvU8cGgT6MNjqlbWHrEvXarCg/0XPPdtOlqIQ6Ix nmVGEYanGiAw9wZf3whf9h3ZdKrPscISjTBYqKn3LU0DrNUdUHw/cet2BDvtfRA4AyJj 5gCPSm4QTbWkp4z2uSLD0DpRDHvS6pcSs6uxL5PO594WZEuuvINuIRkZpVtzfVE7FGtt DAfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733753174; x=1734357974; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IZacjWdb8RyenSpXv6Caz5kcIhA6mwKRd5G/S3r2hOU=; b=KP4O0xbUPGjmGpCF4+dqUXa4OM1bxx9TmP5RTW2VFppB/0P1J0JkDxGGdfVmPLyfpG +VGYStFkjfYsiqS1QixB5fVDxowgaUGYpuJhacmDYnxu6DRkd3GmN8YAfx38Jy8KSN+V e34EJPR06xHvcNGf4rVd8jhJB5rctL7RlOPdUA0k19PkTxHCvUs2iu5cTH8elnPk8RLo uebKPLGJByJZoo7l3ninuTMGhcQECDgzNn+uLx6rm4phyFQsT4iOZc/JAbbZvartHsx8 wwP3wmYIN8my8Y8JqdoKn13SBz+RZYdSTecEzvLlC3gMjU7e6fWS3x/pLG8rLoSHl2g2 BdZQ== X-Forwarded-Encrypted: i=1; AJvYcCWBRDXFa67ahQGvDu9yGlxc70I9uFc7IzWhWMv802RR4KJpQ9Av6+Ag0e2TGFFcHsjtiBC/FascG+243Q==@lists.open-mesh.org X-Gm-Message-State: AOJu0YxQAKUd7xZdzoQQzbQjZt9dhXgoviep9anoabYQvuvnOr5X+XSz XLNQG9wHmHg4Ut4XEl2owzmG+AVK/kMnBBQux+RQUvbVy5FnjFqs X-Gm-Gg: ASbGncsjdy0llWtbPs55oL0qP54GnLM+RXlGTSxyfe+xbsctYXvTo1oGtUewvt8Tz2K yMjv/szWcPverpyL7XW3GEbc3qsVyXpwhAjQ7QIjhRr/7jNIEVeD+GIKiIpYCtsqjnJ/Q9zYYtB bISuqgtUP2PB4yEr3BpNZWfSK4ZECDYbSeT2RLjyl2VAnB/31aiTOwSfhH6CfS9QWhl3YM0JsDA CXKqc3hN1dNhAgf0gwe14PJSk0O8TiveoQcSVDio1FwhGc= X-Google-Smtp-Source: AGHT+IHmgSwYi0f3YPXZUnipKY4/LE+6y74m5MtL3Oj9+h7tdAHjZEgR2L+TFdRwx2htNJ1fIiOnow== X-Received: by 2002:a17:902:ce8d:b0:215:a2af:44ca with SMTP id d9443c01a7336-21614dd2374mr177498285ad.37.1733753174431; Mon, 09 Dec 2024 06:06:14 -0800 (PST) Received: from nova-ws.. ([103.167.140.11]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-216221db645sm49605645ad.46.2024.12.09.06.06.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Dec 2024 06:06:13 -0800 (PST) From: Xiao Liang To: netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, Kuniyuki Iwashima , Jakub Kicinski , Donald Hunter Cc: "David S. Miller" , David Ahern , Eric Dumazet , Paolo Abeni , Ido Schimmel , Andrew Lunn , Simon Horman , Shuah Khan , Jiri Pirko , Hangbin Liu , linux-rdma@vger.kernel.org, linux-can@vger.kernel.org, osmocom-net-gprs@lists.osmocom.org, bpf@vger.kernel.org, linux-ppp@vger.kernel.org, wireguard@lists.zx2c4.com, linux-wireless@vger.kernel.org, b.a.t.m.a.n@lists.open-mesh.org, bridge@lists.linux.dev, linux-wpan@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next v5 5/5] selftests: net: Add two test cases for link netns Date: Mon, 9 Dec 2024 22:01:51 +0800 Message-ID: <20241209140151.231257-6-shaw.leon@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241209140151.231257-1-shaw.leon@gmail.com> References: <20241209140151.231257-1-shaw.leon@gmail.com> MIME-Version: 1.0 X-MailFrom: shaw.leon@gmail.com X-Mailman-Rule-Hits: nonmember-moderation 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; header-match-b.a.t.m.a.n.lists.open-mesh.org-2 Message-ID-Hash: BYCVHPNKHPSWJWDCQBD673O3GQVZ2ZII X-Message-ID-Hash: BYCVHPNKHPSWJWDCQBD673O3GQVZ2ZII X-Mailman-Approved-At: Mon, 09 Dec 2024 17:47:40 +0100 X-Mailman-Version: 3.3.8 Precedence: list List-Id: The list for a Better Approach To Mobile Ad-hoc Networking Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: - Add test for creating link in another netns when a link of the same name and ifindex exists in current netns. - Add test for link netns atomicity - create link directly in target netns, and no notifications should be generated in current netns. Signed-off-by: Xiao Liang --- tools/testing/selftests/net/Makefile | 1 + tools/testing/selftests/net/netns-name.sh | 10 ++++++ tools/testing/selftests/net/netns_atomic.py | 39 +++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100755 tools/testing/selftests/net/netns_atomic.py diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile index cb2fc601de66..f9f7a765d645 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile @@ -34,6 +34,7 @@ TEST_PROGS += gre_gso.sh TEST_PROGS += cmsg_so_mark.sh TEST_PROGS += cmsg_time.sh cmsg_ipv6.sh TEST_PROGS += netns-name.sh +TEST_PROGS += netns_atomic.py TEST_PROGS += nl_netdev.py TEST_PROGS += srv6_end_dt46_l3vpn_test.sh TEST_PROGS += srv6_end_dt4_l3vpn_test.sh diff --git a/tools/testing/selftests/net/netns-name.sh b/tools/testing/selftests/net/netns-name.sh index 6974474c26f3..0be1905d1f2f 100755 --- a/tools/testing/selftests/net/netns-name.sh +++ b/tools/testing/selftests/net/netns-name.sh @@ -78,6 +78,16 @@ ip -netns $NS link show dev $ALT_NAME 2> /dev/null && fail "Can still find alt-name after move" ip -netns $test_ns link del $DEV || fail +# +# Test no conflict of the same name/ifindex in different netns +# +ip -netns $NS link add name $DEV index 100 type dummy || fail +ip -netns $NS link add netns $test_ns name $DEV index 100 type dummy || + fail "Can create in netns without moving" +ip -netns $test_ns link show dev $DEV >> /dev/null || fail "Device not found" +ip -netns $NS link del $DEV || fail +ip -netns $test_ns link del $DEV || fail + echo -ne "$(basename $0) \t\t\t\t" if [ $RET_CODE -eq 0 ]; then echo "[ OK ]" diff --git a/tools/testing/selftests/net/netns_atomic.py b/tools/testing/selftests/net/netns_atomic.py new file mode 100755 index 000000000000..d350a3fc0a91 --- /dev/null +++ b/tools/testing/selftests/net/netns_atomic.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 + +import time + +from lib.py import ksft_run, ksft_exit, ksft_true +from lib.py import ip +from lib.py import NetNS, NetNSEnter +from lib.py import RtnlFamily + + +def test_event(ns1, ns2) -> None: + with NetNSEnter(str(ns1)): + rtnl = RtnlFamily() + + rtnl.ntf_subscribe("rtnlgrp-link") + + ip(f"netns set {ns1} 0", ns=str(ns2)) + + ip(f"link add netns {ns2} link-netnsid 0 dummy1 type dummy") + ip(f"link add netns {ns2} dummy2 type dummy", ns=str(ns1)) + + ip("link del dummy1", ns=str(ns2)) + ip("link del dummy2", ns=str(ns2)) + + time.sleep(1) + rtnl.check_ntf() + ksft_true(rtnl.async_msg_queue.empty(), + "Received unexpected link notification") + + +def main() -> None: + with NetNS() as ns1, NetNS() as ns2: + ksft_run([test_event], args=(ns1, ns2)) + ksft_exit() + + +if __name__ == "__main__": + main()