From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=103.168.172.153 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795989482; b=dxbWNTiWH37+fLyCwyRel+/xKgLTAgu63ubLwhq754+Y6xCYqzg5NK47bslozo4GeD7P 6kgrh2xn/zPV2qNfETdjqbeVcsLCXwWpym8IJhZpuuLpL5zWMa9roHQcqs1ZHhLCr3iw3 aiBLCtBGNTPChM7mTcppkm1t7JHWDulSjENuNPq668mPHF+OPea/0kz5K+L1j/+Kg+VvM jM/SRLVyquGRhMvHQgh2sllUD9kPYzxoMLgztfWPQP3/p1yQL3lG8m06Pc2U5hcrNX86J blNZIkw2ZvqioSMmLI+EbEKvw2kq7n7bVWD9hOi3m7D4VBPf4g+0axnjhas+ezFNLLA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795989482; h=DMARC-Filter:Received:Received:DKIM-Signature:DKIM-Signature: X-DKIM2-Info:DKIM2-Signature:X-DKIM2-Info:Message-Instance: X-ME-Sender:X-ME-Received:X-ME-Proxy-Cause:X-ME-Proxy:Feedback-ID: Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Type:Content-Transfer-Encoding; bh=SAG6gjZCyKtUDSGIgkYe06G1M+7x2Xnrjh7eVan/w2s=; b=P7OUDN6yJYgYdtVnx/WMsijS6yev8QIhyPp2QYPYyRCoKUBTpPNHlJJaZrFricmxMngB WlTBiVTUoiPhNdxjXmnZ1KZ2Tn+PKCuUBK+FLNgcW/5qco+iKXne0Jp1B0fRtJr/ZFyBJ TrnIuFwHinLKl1URdRRbwlRHnymFKkJ2h99YbqTZvnD+VAyW7K2gfIch2mIaYpHqcYGxp 4rskxbP92aHcDa4TBwMaymgAmuCu26inHeN8+TbUGt+sPqwRf7wAYYXxUd37/x/GXNdVG 8qWu8mqQFtycLbRLytd/0Mb9821U4vEsrXb26bxYf40kgvtjyrSIflRHdHJTuXywaiA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=cbannister.uk; spf=pass smtp.mailfrom=cbannister.uk; dkim=pass header.d=cbannister.uk header.i=@cbannister.uk header.a=rsa-sha256 header.s=fm3 header.b=Vg4hPJ1W; dkim=pass header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=wCaxM9TW; arc=none smtp.remote-ip=103.168.172.153 Received: from fhigh-a2-smtp.messagingengine.com (fhigh-a2-smtp.messagingengine.com [103.168.172.153]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9C995606A327 for ; Sun, 29 Nov 2026 21:58:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9C995606A327 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cbannister.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cbannister.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=cbannister.uk header.i=@cbannister.uk header.a=rsa-sha256 header.s=fm3 header.b=Vg4hPJ1W; dkim=pass (2048-bit key, unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=wCaxM9TW Received: from phl-compute-02.internal (phl-compute-02.internal [10.202.2.42]) by mailfhigh.phl.internal (Postfix) with ESMTP id CCBCE140025A for ; Sun, 29 Nov 2026 16:58:00 -0500 (EST) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-02.internal (MEProxy); Sun, 29 Nov 2026 16:58:00 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cbannister.uk; h=cc:cc:content-transfer-encoding:content-type:content-type :date:date:from:from:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to; s=fm3; t=1795989480; x= 1796075880; bh=SAG6gjZCyKtUDSGIgkYe06G1M+7x2Xnrjh7eVan/w2s=; b=V g4hPJ1WUPNz024ghXkYH6IKZpwa4aSKgJAuTGfkay1qjvz7Webtrm8x1jQjZZuPC LVUl2nGxbpKltjxxljNsiDEq+6+8yxIqPJHCQ07BnMoBLU/5EbOxuKffp0gt/HYf Uh0XbMPCMj54ec/Xl3rnxvtwqZjLm+1byoBcLPu9ykAfwjA189R5lPcJAO6785pJ Y31oUm5c5Dm+/4dyhoSMPJf7Z/cav7qHgjhmm3WEv5adxefKR1+xckyhMWllWJvO 8RyNG/qYMXCAXyAtFM7R4Ve/Yiu5rg/SOl+L7wygubTa6erjiGJaLKbBbobb+AgB rN6CDykJD+Rh7kaVThLZg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:message-id:mime-version:reply-to:subject :subject:to:to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1795989480; x=1796075880; bh=SAG6gjZCyKtUDSGIgkYe06G1M+7x 2Xnrjh7eVan/w2s=; b=wCaxM9TW6VHQ2InKvOA7QybYZu8kFj9e2EBK9maX20/+ qHQ1cBsyPgrw1ZhPAjGp6vh3eR867MaxjN/q0xCXwZZbDDqCxSkzDjn/DEtvVmFx Sw/Sgwr8l7AJuhs4C8j3aVvmaTQB8dgtXUx4OXf0BgfbM96MLP6bSSAVNdmbncVj HDkionwHUOKL6tSAWWpVrMvXpgbQmkWZBisTZj+w4xSQtPjgWMxvRFMrVS4bN088 tODA0mwrSXFA0RXGXUeC4Iknrtvvt48J8GBkABMDlw5NqkH9I1u0K0x5v1mI6kIw PURq24TrNcg/H/rbij7xCTr6reDWlJlkxNNK7XSFiQ== X-DKIM2-Info: draft=ietf-dkim-dkim2-spec-09; repo=github.com/dkim2wg/interop; date=2026-11-03; sw=lmtpprox; action=sign d=cbannister.uk a=rsa-sha256; DKIM2-Signature: i=1; m=1; t=1795989480; d=cbannister.uk; mf=PGtlcm5lbEBjYmFubmlzdGVyLnVrPg==; rt=PGxpbnV4LWtlcm5lbEB2Z2VyLmtlcm5lbC5vcmc+; s=fm3:rsa-sha256:Vt6jldD2cvYbF+DSaCrAmioGHWlayz0cjwXOlpLedrba+XE nfCcRErwG6fMeFfbz8xTwRvxMphghfHk2jTUxUHnIMK1zcZR4hKav9NHQiof7Nfb LPjsu2OZiySr7l3+cLgn+ZsCp0wxezpfXsamwHZgF7MG1QBVjnxYPHHLMguhwA9d S3gkacFGzXqNZjeRIosh/vIX1JqeNE8MtMw+uWnBHmMfUF03pzuL3NYdh49QgM5I +O0VHHdri3YsVcWX0wSfluFZmu4/qC/aIgRHaBqUT0GAqg+ZgexJpiT3jVE9MLMo FZZ+u0sNAjRvFBfie3DI/DiuDa5BXn2lz04Wyig==; X-DKIM2-Info: draft=ietf-dkim-dkim2-spec-09; repo=github.com/dkim2wg/interop; date=2026-11-03; sw=lmtpprox; action=mi-m=1; hc=10; hn=cc,content-transfer-encoding,content-type,date,feedback-id, from,message-id,mime-version,subject,to; Message-Instance: m=1; h=sha256:5fOaeeK2qIP2ZsaNMmaJDoERQ5oV5TfWfRgsT6sQHfc=:xnpT029p9yqjxi8IfkSExZwn3dbrn6LjSCc7PL2Wifg=; X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTFEVdy6YH5C6EtKbeRAblpKYyA9A9Euw06RF6HmaOUyOqp/Pf7GH7xG6hk93yYmeD jLXa2ifb1xaVnTmm+N0NvtFfbwrE7qUsTBxnX0vnpN2ISKraUkKuFoFQOTfGCXmm6z9NuN h3+JSdOUGP2YtyPgQQrcAYprabFh57l8vhZBi83FU2LULDGMKvEZzAJmG3LMBChpWgT+Da UF0lO8zOOTokVv9AWgHgjuzd/nWGEVzsodThqj6NmX8j9o410OvaPv+rwQoX5K4ytem+bP CE0PnEetiuUI8hmF9h7lu0P4mtSyfAkRWwqy/o4mqul1XKpFL63B3Rldnpo+GeZ8cKHKSD /blGuqwBrU/W0hjW0Mam5IVeob5OrUy1BMBt6jZmjSsQJxAnIGz1+60393rbImCDpoxJ2M pk+fSrqkSvtsE61TgD8wUGVap6FRByVSAEqvkjNBqALLIlJuNrUj2hLxhDMO/5lnc1nIv9 yIAuOniIQXbvOFEiabVDCBJMh8dnMAN58ie4F8o55tYpX1HMGZXf66T24Og25Oofrq0ZG5 5VtcgbDzE8A49oFOsxR77Ryv/9x3yl1DtwV5wYhEEirzoR4WzZramdqbucGKMw9Zv7G1wg wCihcIEuPYkklXPzw1yCMYDP+qfvJdHRZM1hB1BJdYMKHOmzJB2i+s4eKmzw X-ME-Proxy: Feedback-ID: ic4a04a9c:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 29 Nov 2026 16:57:59 -0500 (EST) From: Chris Bannister To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , "David S. Miller" , netdev@vger.kernel.org Cc: regressions@lists.linux.dev, linux-kernel@vger.kernel.org, Simon Horman , Kuniyuki Iwashima Subject: [REGRESSION] rtnetlink: a full RTM_GETLINK racing a netns move can return the device's data from its new netns since v7.2 Date: Sun, 29 Nov 2026 21:57:51 +0000 Message-ID: <20261129215752.251291-1-kernel@cbannister.uk> X-Mailer: git-send-email 2.56.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 8bit Hi, A review of the series merged as aa064a614efc ("rtnetlink: RTNL avoidance in rtnl_getlink() and rtnl_dump_ifinfo()") found a second regression from the same commit as the reply-ordering report [1]. A full RTM_GETLINK that races a netns move of the device can be answered with the device's data from its new netns. It bisects to e896e5c0734b ("rtnetlink: do not acquire RTNL in rtnl_getlink() with RTEXT_FILTER_NAME_ONLY") [2]: clean at its parent (00888feb6014), reproduces at that commit. v7.3.6 carries the change, and it is unfixed in mainline as of v7.4-rc4. #regzbot introduced: e896e5c0734b The reproducers, kernel configs, run records and the fix patches are in a bundle [3], and paths below are relative to it. [1] https://lore.kernel.org/all/20261129214529.4059732-1-kernel@cbannister.uk/ [2] https://lore.kernel.org/all/20260525083542.1565964-4-edumazet@google.com/ [3] https://github.com/zariel/linux-rtnetlink-getlink-regressions The sections below were drafted by AI tools from the investigation records. I have reviewed them, and the numbers are from the run records in the bundle. Cause ----- A full RTM_GETLINK is one without RTEXT_FILTER_NAME_ONLY. Since e896e5c0734b, rtnl_getlink() looks the device up in the target netns under RCU, and only then takes RTNL to fill the reply. Once it holds RTNL it checks only netif_is_alive(). A netns move runs under RTNL and leaves reg_state at NETREG_REGISTERED, so that check passes, and nothing rechecks dev_net(dev) == tgt_net. A request that found the device and then waited on RTNL while the device moved fills its reply from the device in its new netns. The reply carries, for example, the ifindex the device has there. Before e896e5c0734b the lookup and the fill shared one RTNL hold, so the answer was either the device in the target netns or -ENODEV. Evidence -------- The evidence comes from vanilla kernel.org builds with taint 0. Every row below, with its host configuration and run IDs, is in /reports/results.md. The test environment is the one described in the ordering report. The Go stress reproducer, /nlrace/go-netns/ (-mode netns), floods full RTM_GETLINK by name in one netns while a dummy device moves between two, with a different ifindex in each. It counts replies that carry the device's ifindex in the other netns. Such replies out of iterations. Pass 1 used a 12 vCPU host with mitigations on, and pass 2 a 20 vCPU host with mitigations off. Both ran five concurrent guests per kernel, with no build running: parent 00888feb6014 (pass 1): 0 / 5,000 e896e5c0734b (pass 1): 6 / 5,000 v7.3.6 (pass 2): 6 / 5,000 v7.4-rc4 (pass 2): 3 / 5,000 v6.18.65 LTS (pass 2): 0 / 5,000 In one guest alone, with the same host, config and binaries: v7.4-rc4: 47 / 20,000 v7.4-rc4 plus the fix: 0 / 20,000 A v7.4-rc4 kernel with the fix, lockdep and KASAN ran the same reproducers with no lock-order, KASAN, RCU or might-sleep report, and taint 0. Reproducer ---------- nlnetns.c is a single C file that shows the same failure. It is appended at the end of this mail and is also at /nlrace/c/nlnetns.c: cc -O2 -Wall -Wextra -pthread -o nlnetns nlnetns.c unshare -Urn ./nlnetns -t 30 -f 4 It exits 1 on a violation, 0 on none and 125 on a setup error. A violation is a reply that carries the ifindex from the other netns. Violations in five such runs per vanilla kernel (taint 0, 20 vCPU host, mitigations off; the first four kernels as concurrent guests, the fix in a guest alone): 00888feb6014: 0 in every run e896e5c0734b: 5,773 to 6,845 per run v7.3.6: 6,174 to 6,833 per run v7.4-rc4: 6,014 to 6,851 per run v7.4-rc4 plus the fix: 0 in every run As supporting evidence, a bare-metal distribution 7.3.5 kernel gave 1,844 in one run. Selftest -------- A deterministic selftest is patch 3 of the fix series, under /kernels/patches/upstream/series/. On vanilla kernels with taint 0 it fails in 20 of 20 runs on e896e5c0734b and on v7.4-rc4, and passes in 20 of 20 on 00888feb6014 and on v7.4-rc4 plus the fix, with no skips. Architecture and compiler: x86_64, gcc 16.2.0 (from the "Linux version" lines under /evidence/). Kernel configs are under /kernels/configs/. Questions --------- A fix series follows these two reports. It has three patches: a fix for this bug, a fix for the ordering bug, and the selftest. The patches are also in the bundle. - Is the extra RTNL acquisition for full requests that name a missing device acceptable, or is an RCU pre-check preferred? - Should the name-only fill use a single RCU section, with dev->ifindex written with WRITE_ONCE() in __dev_change_net_namespace()? - The selftest holds RTNL by making the copy_to_user() in SIOCGIFCONF fault on a userfaultfd page. It is deterministic, but it rests on dev_ifconf() copying out under RTNL, and I cannot judge whether a test that depends on that detail is acceptable. Is it, or is there a better way to hold RTNL from user space? I am happy to test other approaches. Disclosure: this investigation was carried out by AI agents under my direction. Claude Opus 5.5 coordinated it, designed the experiments, minimised the reproducer and wrote its review fixes and reader comments; Claude Fable 5.2 found this bug in its review of the series, designed the offered fixes and tests, wrote nlnetns.c and reviewed the package; Claude Sonnet 5.5 ran the test host, built the kernels, wrote the first nlrace.c and drafted this mail; OpenAI gpt-6.2-sol and gpt-6.2-astra wrote the first harness and Go reproducer, analysed the source and reviewed the fixes. The reproducers and the offered fix were run on the kernels listed above. My own part: I read rtnl_getlink() and __dev_change_net_namespace(), reviewed nlnetns.c and reproduced this bug with it on my development machine. I reviewed the fix patches, and read the selftest without running it. I searched lore for related bugs, reports and patches, and I reviewed this mail before sending it. Per-agent detail: /reports/provenance.md. Thanks, Chris Bannister ---- 8< ---- nlnetns.c ---- 8< ---- // SPDX-License-Identifier: GPL-2.0 /* * nlnetns: can a full RTM_GETLINK for an ifindex in one network namespace be * answered with the device as it is in another namespace? * * Since e896e5c0734b ("rtnetlink: do not acquire RTNL in rtnl_getlink() with * RTEXT_FILTER_NAME_ONLY", v7.2), rtnl_getlink() looks the device up in the * target netns under RCU, and only then takes RTNL to fill a full reply. Once * it holds RTNL it checks only that the device is still registered. A move to * another netns (__dev_change_net_namespace()) runs under RTNL and leaves * reg_state at NETREG_REGISTERED, so a request that found the device and then * slept on RTNL for the length of a move fills its reply from the device in * its new netns. Before that commit the lookup and the fill shared one RTNL * hold. * * The program runs in netns A and creates a second netns B. Threads: * - main, the mover: moves the dummy link nlnetns0 from A to B and back, in * a loop. Each move passes IFLA_NEW_IFINDEX, so the device has ifindex X * whenever it is in A and X + 1000 whenever it is in B. * - flooders (-f): each has its own socket in A, sends a full RTM_GETLINK * for ifindex X, and reads the answer before it sends the next request. * * Violation: an RTM_NEWLINK reply whose ifi_index is not X. Every request * asks netns A for ifindex X, and the device carries another ifindex only * while it is in B, so such a reply was filled from the device in B. * * Why a correct kernel cannot produce this: listing a device in a netns, * unlisting it and changing its ifindex all happen under RTNL. When the * lookup and the fill share one RTNL hold, a device found in A under * ifindex X is still in A with ifindex X when the reply is filled, and a * request that arrives while the device is in B finds nothing and gets * -ENODEV. The private namespaces mean nothing else creates or moves links. * -ENODEV answers are expected on every kernel and are only counted. * * Build: cc -O2 -Wall -Wextra -pthread -o nlnetns nlnetns.c * Run: unshare -Urn ./nlnetns [-t SECONDS] [-f FLOODERS] * Exit: 0 no violations, 1 violations, 125 the run gave no verdict (usage, * setup or netlink failure, or an answer that did not arrive in * time), so "git bisect run" skips that commit. */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define DEV "nlnetns0" #define PEER_OFFSET 1000 /* ifindex in B minus ifindex in A */ #define SKIP 125 /* exit status: no verdict; "git bisect run" skips the commit */ static int ifindex; /* X: the device's ifindex while it is in A */ static atomic_int stop; static atomic_ulong requests, enodev, foreign; static void die(const char *what) { fprintf(stderr, "nlnetns: %s: %s\n", what, strerror(errno)); exit(SKIP); } /* A NETLINK_ROUTE socket in the calling thread's current netns. */ static int nlsock(void) { int fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE); struct timeval tv = { 10, 0 }; struct sockaddr_nl sa = { .nl_family = AF_NETLINK }; if (fd < 0) die("socket"); setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); if (bind(fd, (void *)&sa, sizeof(sa))) die("bind"); return fd; } static int netns_fd(void) { int fd = open("/proc/thread-self/ns/net", O_RDONLY | O_CLOEXEC); if (fd < 0) die("open /proc/thread-self/ns/net"); return fd; } /* Send one rtnetlink request: ifinfomsg for ifindex, then optional attributes. */ static void request(int fd, int type, int flags, unsigned seq, int idx, const void *attrs, int alen) { struct { struct nlmsghdr h; struct ifinfomsg i; char a[64]; } m = {0}; m.h.nlmsg_type = type; m.h.nlmsg_flags = NLM_F_REQUEST | flags; m.h.nlmsg_seq = seq; m.i.ifi_index = idx; if (alen) memcpy(m.a, attrs, alen); m.h.nlmsg_len = NLMSG_LENGTH(sizeof(m.i)) + alen; if (send(fd, &m, m.h.nlmsg_len, 0) < 0) die("send"); } /* Wait for the ack of a request on fd. */ static void wait_ack(int fd, const char *what) { char buf[4096]; struct nlmsghdr *h = (void *)buf; if (recv(fd, buf, sizeof(buf), 0) < 0) die(what); if (h->nlmsg_type != NLMSG_ERROR || ((struct nlmsgerr *)NLMSG_DATA(h))->error) { errno = h->nlmsg_type == NLMSG_ERROR ? -((struct nlmsgerr *)NLMSG_DATA(h))->error : EPROTO; die(what); } } /* * Move the device, known as ifindex idx in the netns of socket fd, to the * netns behind ns, where it gets ifindex new_idx. The ack arrives after the * move is complete. */ static void move(int fd, int idx, int ns, int new_idx) { struct { struct rtattr n; int ns; struct rtattr i; int idx; } a = { { RTA_LENGTH(sizeof(int)), IFLA_NET_NS_FD }, ns, { RTA_LENGTH(sizeof(int)), IFLA_NEW_IFINDEX }, new_idx }; request(fd, RTM_SETLINK, NLM_F_ACK, 0, idx, &a, sizeof(a)); wait_ack(fd, "move"); } /* * Requester: asks netns A for ifindex X with a full RTM_GETLINK (no * IFLA_EXT_MASK, so not RTEXT_FILTER_NAME_ONLY) and applies the oracle from * the header comment to the answer. The socket has no group membership and * one request outstanding, so the next message on it is that answer. */ static void *flooder(void *arg) { char buf[1 << 15]; struct nlmsghdr *h = (void *)buf; unsigned seq = 0; int fd = nlsock(); (void)arg; while (!atomic_load(&stop)) { int n; request(fd, RTM_GETLINK, 0, ++seq, ifindex, NULL, 0); n = recv(fd, buf, sizeof(buf), 0); if (n < 0) die("recv GETLINK answer"); if (!NLMSG_OK(h, n) || h->nlmsg_seq != seq) { errno = EPROTO; die("GETLINK answer"); } atomic_fetch_add(&requests, 1); if (h->nlmsg_type == NLMSG_ERROR) { errno = -((struct nlmsgerr *)NLMSG_DATA(h))->error; if (errno != ENODEV) die("GETLINK"); atomic_fetch_add(&enodev, 1); } else if (h->nlmsg_type == RTM_NEWLINK) { int got = ((struct ifinfomsg *)NLMSG_DATA(h))->ifi_index; if (got != ifindex && atomic_fetch_add(&foreign, 1) < 5) printf("GETLINK for ifindex %d answered with ifindex %d (the device in the other netns)\n", ifindex, got); } else { errno = EPROTO; die("GETLINK answer type"); } } close(fd); return NULL; } static double since(struct timespec *t) { struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); return (now.tv_sec - t->tv_sec) * 1e3 + (now.tv_nsec - t->tv_nsec) / 1e6; } int main(int argc, char **argv) { int opt, nflood = 4, seconds = 30, moves = 0; int ns_a, ns_b, ctl_a, ctl_b; struct timespec start; struct utsname u; pthread_t *fl; while ((opt = getopt(argc, argv, "t:f:")) != -1) { if (opt == 't') seconds = atoi(optarg); else if (opt == 'f') nflood = atoi(optarg); else { fprintf(stderr, "usage: %s [-t SECONDS] [-f FLOODERS]\n", argv[0]); return SKIP; } } if (nflood < 1) nflood = 1; /* * Create netns B and return to A. A socket stays in the netns it was * created in, so ctl_b is opened while this thread is in B. This runs * before any thread exists, so every thread starts in A. */ ns_a = netns_fd(); if (unshare(CLONE_NEWNET)) die("unshare(CLONE_NEWNET)"); ns_b = netns_fd(); ctl_b = nlsock(); if (setns(ns_a, CLONE_NEWNET)) die("setns back to the first netns"); ctl_a = nlsock(); /* Create the dummy link in A and find its index there. */ { struct { struct rtattr n; char name[12]; struct rtattr li; struct rtattr k; char kind[8]; } a = { { RTA_LENGTH(sizeof(DEV)), IFLA_IFNAME }, DEV, { RTA_LENGTH(RTA_LENGTH(6)), IFLA_LINKINFO | NLA_F_NESTED }, { RTA_LENGTH(6), IFLA_INFO_KIND }, "dummy" }; request(ctl_a, RTM_NEWLINK, NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL, 0, 0, &a, sizeof(a)); wait_ack(ctl_a, "create " DEV); } if (!(ifindex = if_nametoindex(DEV))) die("if_nametoindex"); if (!(fl = calloc(nflood, sizeof(*fl)))) die("calloc"); for (int i = 0; i < nflood; i++) if ((errno = pthread_create(&fl[i], NULL, flooder, NULL))) die("pthread_create"); clock_gettime(CLOCK_MONOTONIC, &start); while (since(&start) < seconds * 1e3) { move(ctl_a, ifindex, ns_b, ifindex + PEER_OFFSET); move(ctl_b, ifindex + PEER_OFFSET, ns_a, ifindex); moves += 2; } /* The device is back in A. Every flooder request gets an answer, so the joins end. */ atomic_store(&stop, 1); for (int i = 0; i < nflood; i++) pthread_join(fl[i], NULL); request(ctl_a, RTM_DELLINK, NLM_F_ACK, 0, ifindex, NULL, 0); wait_ack(ctl_a, "delete " DEV); free(fl); uname(&u); printf("%s: %d moves, %lu requests (%lu ENODEV), %lu violations\n", u.release, moves, atomic_load(&requests), atomic_load(&enodev), atomic_load(&foreign)); return atomic_load(&foreign) ? 1 : 0; }