From nobody Fri Oct 3 13:34:14 2025 Received: from bregans-1.gladserv.net (bregans-1.gladserv.net [185.128.211.58]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1EDF942065; Sun, 31 Aug 2025 10:29:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.128.211.58 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756636196; cv=none; b=K7Lac2rwOGo3mjvYSTEixmor88imB7lPOmiVFw/haENVUVnIVvhaEqeuCsORhu5AL8ThgDGosXWu/FGVHkwgei+Hg6AymJ3qaHN+URQxRzB4Lhu0fFhQBZd/1NoQJep21B3RIh94koSbxubbFFHXVwn9RUC6D3XOPWYbWYxB76w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756636196; c=relaxed/simple; bh=2EsMqCtVHrXT7IjY5w8NhPLpj46DcUqkEIp0+bOlwXk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UamtXrf0l6IbofjPvaXn3ePpLZByXYOwVIwFNZu3GkwWLKSHaBQSBhF6p8OjQoKvzTwbclpkehErTmM34h34XC1HGN7JxbPRw9MvO4BE/r3v2W35CyyZs/+kAaW/wYwXrdKbDc0EN13PNUE2LF7LbDAPSZDiLcNh7qml/qQ51rA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=librecast.net; spf=pass smtp.mailfrom=librecast.net; arc=none smtp.client-ip=185.128.211.58 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=librecast.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=librecast.net From: Brett A C Sheffield To: willemdebruijn.kernel@gmail.com Cc: bacs@librecast.net, davem@davemloft.net, edumazet@google.com, gregkh@linuxfoundation.org, horms@kernel.org, kuba@kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, netdev@vger.kernel.org, pabeni@redhat.com, shuah@kernel.org, willemb@google.com Subject: [PATCH net-next v2] selftests: net: add test for ipv6 fragmentation Date: Sun, 31 Aug 2025 10:18:34 +0000 Message-ID: <20250831102908.14655-1-bacs@librecast.net> X-Mailer: git-send-email 2.49.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add selftest for the IPv6 fragmentation regression which affected several stable kernels. Commit a18dfa9925b9 ("ipv6: save dontfrag in cork") was backported to stable without some prerequisite commits. This caused a regression when sending IPv6 UDP packets by preventing fragmentation and instead returning -1 (EMSGSIZE). Add selftest to check for this issue by attempting to send a packet larger than the interface MTU. The packet will be fragmented on a working kernel, with sendmsg(2) correctly returning the expected number of bytes sent. When the regression is present, sendmsg returns -1 and sets errno to EMSGSIZE. Link: https://lore.kernel.org/stable/aElivdUXqd1OqgMY@karahi.gladserv.com Signed-off-by: Brett A C Sheffield --- Willem: Thanks for the suggestion to check /sys/class/net/$DEV/operstate I did try this but, if I call unshare() and THEN create a TAP interface in = the new namespace, operstate no longer seems to be visible to the process? The process can still read operstate for interfaces in the shared namespace= , but not ones created in the unshare()d namespace. I'm sure I'm doing something wrong there, but after trying a few different things including reading operstate from netlink it suddenly occurred to me = that a simpler and more reliable way to check whether an interface is ready to s= end is to, er, send, and then handle the error for the (unlikely) failure case = and retry. I've incorporated your other review suggestions in this v2. Many thanks. v2 changes: - remove superfluous namespace calls - unshare(2) suffices - remove usleep(). Don't wait for the interface to be ready, just send, and handle the (less likely) error case by retrying. - set destination address only once - document our use of the IPv6 link-local source address - send to port 9 (DISCARD) instead of 4242 (DONT PANIC) - ensure sockets are closed on failure paths - use KSFT exit codes for clarity v1: https://lore.kernel.org/netdev/20250825092548.4436-3-bacs@librecast.net tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/Makefile | 1 + .../selftests/net/ipv6_fragmentation.c | 189 ++++++++++++++++++ 3 files changed, 191 insertions(+) create mode 100644 tools/testing/selftests/net/ipv6_fragmentation.c diff --git a/tools/testing/selftests/net/.gitignore b/tools/testing/selftes= ts/net/.gitignore index 47c293c2962f..3d4b4a53dfda 100644 --- a/tools/testing/selftests/net/.gitignore +++ b/tools/testing/selftests/net/.gitignore @@ -16,6 +16,7 @@ ip_local_port_range ipsec ipv6_flowlabel ipv6_flowlabel_mgr +ipv6_fragmentation log.txt msg_oob msg_zerocopy diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests= /net/Makefile index eef0b8f8a7b0..276e0481d996 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile @@ -117,6 +117,7 @@ TEST_GEN_FILES +=3D tfo TEST_PROGS +=3D tfo_passive.sh TEST_PROGS +=3D broadcast_pmtu.sh TEST_PROGS +=3D ipv6_force_forwarding.sh +TEST_GEN_PROGS +=3D ipv6_fragmentation TEST_PROGS +=3D route_hint.sh =20 # YNL files, must be before "include ..lib.mk" diff --git a/tools/testing/selftests/net/ipv6_fragmentation.c b/tools/testi= ng/selftests/net/ipv6_fragmentation.c new file mode 100644 index 000000000000..4ba16bf56a32 --- /dev/null +++ b/tools/testing/selftests/net/ipv6_fragmentation.c @@ -0,0 +1,189 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Author: Brett A C Sheffield + * + * Kernel selftest for the IPv6 fragmentation regression which affected st= able + * kernels: + * + * https://lore.kernel.org/stable/aElivdUXqd1OqgMY@karahi.gladserv.com + * + * Commit: a18dfa9925b9 ("ipv6: save dontfrag in cork") was backported to = stable + * without some prerequisite commits. + * + * This caused a regression when sending IPv6 UDP packets by preventing + * fragmentation and instead returning -1 (EMSGSIZE). + * + * This selftest demonstrates the issue by sending an IPv6 UDP packet from + * the autoconfigured link-local address to an arbritrary multicast group. + * + * sendmsg(2) returns bytes sent correctly on a working kernel, and return= s -1 + * (EMSGSIZE) when the regression is present. + * + * The regression was not present in the mainline kernel, but add this tes= t to + * catch similar breakage in future. + */ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../kselftest.h" + +#define MTU 1500 +#define LARGER_THAN_MTU 8192 + +/* ensure MTU is smaller than what we plan to send */ +static int set_mtu(int ctl, struct ifreq *ifr) +{ + ifr->ifr_mtu =3D MTU; + return ioctl(ctl, SIOCSIFMTU, ifr); +} + +/* bring up interface */ +static int interface_up(int ctl, struct ifreq *ifr) +{ + if (ioctl(ctl, SIOCGIFFLAGS, ifr) =3D=3D -1) { + perror("ioctl SIOCGIFFLAGS"); + return -1; + } + ifr->ifr_flags =3D ifr->ifr_flags | IFF_UP; + return ioctl(ctl, SIOCSIFFLAGS, ifr); +} + +/* no need to wait for DAD in our namespace */ +static int disable_dad(char *ifname) +{ + char sysvar[] =3D "/proc/sys/net/ipv6/conf/%s/accept_dad"; + char fname[IFNAMSIZ + sizeof(sysvar)]; + int fd; + + snprintf(fname, sizeof(fname), sysvar, ifname); + fd =3D open(fname, O_WRONLY); + if (fd =3D=3D -1) { + perror("open accept_dad"); + return -1; + } + if (write(fd, "0", 1) !=3D 1) { + perror("write"); + return -1; + } + return close(fd); +} + +/* create TAP interface that will be deleted when this process exits */ +static int create_interface(char *ifname, struct ifreq *ifr) +{ + int fd; + + fd =3D open("/dev/net/tun", O_RDWR); + if (fd =3D=3D -1) { + perror("open tun"); + return -1; + } + + ifr->ifr_flags =3D IFF_TAP | IFF_NO_PI; + if (ioctl(fd, TUNSETIFF, (void *)ifr) =3D=3D -1) { + close(fd); + perror("ioctl: TUNSETIFF"); + return -1; + } + strcpy(ifname, ifr->ifr_name); + + return fd; +} + +static int setup(void) +{ + struct ifreq ifr =3D {0}; + char ifname[IFNAMSIZ]; + int fd =3D -1; + int ctl; + + /* we need to set MTU, so do this in a namespace to play nicely */ + if (unshare(CLONE_NEWNET) =3D=3D -1) + return -1; + + ctl =3D socket(AF_LOCAL, SOCK_STREAM, 0); + if (ctl =3D=3D -1) + return -1; + + memset(ifname, 0, sizeof(ifname)); + fd =3D create_interface(ifname, &ifr); + if (fd =3D=3D -1) + goto err_close_ctl; + if (disable_dad(ifname) =3D=3D -1) + goto err_close_fd; + if (interface_up(ctl, &ifr) =3D=3D -1) + goto err_close_fd; + if (set_mtu(ctl, &ifr) =3D=3D -1) + goto err_close_fd; + goto err_close_ctl; +err_close_fd: + close(fd); + fd =3D -1; +err_close_ctl: + close(ctl); + return fd; +} + +int main(void) +{ + /* destination doesn't matter, use an IPv6 link-local multicast group */ + struct in6_addr addr =3D { + .s6_addr[0] =3D 0xff, /* multicast */ + .s6_addr[1] =3D 0x12, /* set flags (T, link-local) */ + }; + struct sockaddr_in6 sa =3D { + .sin6_family =3D AF_INET6, + .sin6_addr =3D addr, + .sin6_port =3D 9 /* port 9/udp (DISCARD) */ + }; + char buf[LARGER_THAN_MTU] =3D {0}; + struct iovec iov =3D { .iov_base =3D buf, .iov_len =3D sizeof(buf)}; + struct msghdr msg =3D { + .msg_iov =3D &iov, + .msg_iovlen =3D 1, + .msg_name =3D (struct sockaddr *)&sa, + .msg_namelen =3D sizeof(sa), + }; + ssize_t rc; + int ns_fd; + int err =3D KSFT_FAIL; + int s; + + printf("Testing IPv6 fragmentation\n"); + ns_fd =3D setup(); + if (ns_fd =3D=3D -1) { + printf("[FAIL] test setup failed\n"); + return KSFT_FAIL; + } + s =3D socket(AF_INET6, SOCK_DGRAM, 0); +send_again: + rc =3D sendmsg(s, &msg, 0); + if (rc =3D=3D -1) { + /* if interface wasn't ready, try again */ + if (errno =3D=3D EADDRNOTAVAIL) + goto send_again; + printf("[FAIL] sendmsg: %s\n", strerror(errno)); + goto err_close_socket; + } else if (rc !=3D LARGER_THAN_MTU) { + printf("[FAIL] sendmsg() returned %zi\n", rc); + goto err_close_socket; + } + printf("[PASS] sendmsg() returned %zi\n", rc); + err =3D KSFT_PASS; + +err_close_socket: + close(s); + close(ns_fd); + return err; +} --=20 2.49.1