Skip to contentSkip to conversation

selftests: net: add test for ipv6 fragmentation

PATCH Superseded Brett A C Sheffield 3 participants 3 messages Sep 1, 2025

Subsystems (3)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 selftests: net: add test for ipv6 fragmentation

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.

---
v4 changes:
- fix "else should follow close brace" (checkpatch ERROR)

v3 changes:
- add usleep instead of busy polling on sendmsg
- simplify error handling by using error() and leaving cleanup to O/S
- use loopback interface - don't bother creating TAP
- send to localhost (::1)

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

v3: https://lore.kernel.org/netdev/[email protected]
v2: https://lore.kernel.org/netdev/[email protected]
v1: https://lore.kernel.org/netdev/[email protected]

 tools/testing/selftests/net/.gitignore        |   1 +
 tools/testing/selftests/net/Makefile          |   1 +
 .../selftests/net/ipv6_fragmentation.c        | 144 ++++++++++++++++++
 3 files changed, 146 insertions(+)
 create mode 100644 tools/testing/selftests/net/ipv6_fragmentation.c
tools/testing/selftests/net/.gitignore +1 -0
index 47c293c2962f..3d4b4a53dfda 100644
@@ -16,6 +16,7 @@ ip_local_port_range
 ipsec
 ipv6_flowlabel
 ipv6_flowlabel_mgr
+ipv6_fragmentation
 log.txt
 msg_oob
 msg_zerocopy
tools/testing/selftests/net/Makefile +1 -0
index eef0b8f8a7b0..276e0481d996 100644
@@ -117,6 +117,7 @@ TEST_GEN_FILES += tfo
 TEST_PROGS += tfo_passive.sh
 TEST_PROGS += broadcast_pmtu.sh
 TEST_PROGS += ipv6_force_forwarding.sh
+TEST_GEN_PROGS += ipv6_fragmentation
 TEST_PROGS += route_hint.sh
 
 # YNL files, must be before "include ..lib.mk"
tools/testing/selftests/net/ipv6_fragmentation.c new file +144 -0

144 lines not on this page · raw message

base-commit: 864ecc4a6dade82d3f70eab43dad0e277aa6fc78

Signature · 1 line
2.49.1
Reviewed-by on v4 1/1
#1 lore raw
Quoted 8 lines · Brett A C Sheffield

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).

Quoted 8 lines

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.

Quoted 3 lines

---
v4 changes:
- fix "else should follow close brace" (checkpatch ERROR)

Reminder to send only only iteration to netdev per 24 hrs.

Quoted 13 lines

v3 changes:
- add usleep instead of busy polling on sendmsg
- simplify error handling by using error() and leaving cleanup to O/S
- use loopback interface - don't bother creating TAP
- send to localhost (::1)

tools/testing/selftests/net/ipv6_fragmentation.c new file +6 -0
+/* no need to wait for DAD in our namespace */
+static int disable_dad(char *ifname)
+{
+	char sysvar[] = "/proc/sys/net/ipv6/conf/%s/accept_dad";
+	char fname[IFNAMSIZ + sizeof(sysvar)];
+	int fd;

tools/testing/selftests/net/ipv6_fragmentation.c line 70

Quoted 10 lines
tools/testing/selftests/net/ipv6_fragmentation.c new file +10 -0
+
+	snprintf(fname, sizeof(fname), sysvar, ifname);
+	fd = open(fname, O_WRONLY);
+	if (fd == -1)
+		error(KSFT_FAIL, errno, "open accept_dad");
+	if (write(fd, "0", 1) != 1)
+		error(KSFT_FAIL, errno, "write accept_dad");
+
+	return close(fd);
+}

Is this needed with loopback?

tools/testing/selftests/net/ipv6_fragmentation.c line 108

Quoted 9 lines
tools/testing/selftests/net/ipv6_fragmentation.c new file +9 -0
+int main(void)
+{
+	struct in6_addr addr = {
+		.s6_addr[15] = 0x01, /* ::1 */
+	};
+	struct sockaddr_in6 sa = {
+		.sin6_family = AF_INET6,
+		.sin6_addr = addr,
+		.sin6_port = 9      /* port 9/udp (DISCARD) */

htons

Review

Quoted 2 lines
(quoted diff) +2 -0
+	};
+	char buf[LARGER_THAN_MTU] = {0};

That's a large stack allocation. static char?

Quoted 6 lines
tools/testing/selftests/net/ipv6_fragmentation.c new file +6 -0
+	ns_fd = setup();
+	s = socket(AF_INET6, SOCK_DGRAM, 0);
+send_again:
+	rc = sendmsg(s, &msg, 0);
+	if (rc == -1) {
+		/* if interface wasn't ready, try again */

tools/testing/selftests/net/ipv6_fragmentation.c line 139

Quoted 10 lines
tools/testing/selftests/net/ipv6_fragmentation.c new file +10 -0
+		if (errno == EADDRNOTAVAIL) {
+			usleep(1000);
+			goto send_again;
+		}
+		printf("[FAIL] sendmsg: %s\n", strerror(errno));
+	} else if (rc != LARGER_THAN_MTU) {
+		printf("[FAIL] sendmsg() returned %zi, expected %i\n", rc, LARGER_THAN_MTU);
+	} else {
+		printf("[PASS] sendmsg() returned %zi\n", rc);
+		err = KSFT_PASS;

slight preference to just fail with error() in the two error cases and let the expected common path be linear and succeed:

    if (rc == -1) {
            if (..)
                    goto send_again;
            error(KSFT_FAIL, ..);
    }
    if (rc != LARGER_THAN_MTU) {
            error(KSFT_FAIL, ..);
    }

    printf(..)
    return KSFT_PASS;

tools/testing/selftests/net/ipv6_fragmentation.c line 142

Quoted 3 lines
tools/testing/selftests/net/ipv6_fragmentation.c new file +3 -0
+	}
+	close(s);
+	close(ns_fd);

ns_fd is always -1. Check all system calls return values. Now that setup internally can fail with error() no need for return values at all.

Quoted 7 lines
tools/testing/selftests/net/ipv6_fragmentation.c new file +2 -0
+	return err;
+}

base-commit: 864ecc4a6dade82d3f70eab43dad0e277aa6fc78

Signature · 1 line
2.49.1
maintainer
#2 lore raw
In reply to Brett A C Sheffield
Quoted 16 lines · On Mon, 1 Sep 2025 12:37:14 +0000 Brett A C Sheffield wrote:
tools/testing/selftests/net/ipv6_fragmentation.c new file +16 -0
+static int setup(void)
+{
+	struct ifreq ifr = {
+		.ifr_name = "lo"
+	};
+	int fd = -1;
+	int ctl;
+
+	/* we need to set MTU, so do this in a namespace to play nicely */
+	if (unshare(CLONE_NEWNET) == -1)
+		error(KSFT_FAIL, errno, "unshare");
+
+	ctl = socket(AF_LOCAL, SOCK_STREAM, 0);
+	if (ctl == -1)
+		error(KSFT_FAIL, errno, "socket");
+

tools/testing/selftests/net/ipv6_fragmentation.c line 97

Quoted 10 lines
tools/testing/selftests/net/ipv6_fragmentation.c new file +10 -0
+	/* ensure MTU is smaller than what we plan to send */
+	ifr.ifr_mtu = MTU;
+	if (ioctl(ctl, SIOCSIFMTU, &ifr) == -1)
+		error(KSFT_FAIL, errno, "ioctl: set MTU");
+
+	disable_dad("lo");
+	interface_up(ctl, &ifr);
+
+	close(ctl);
+	return fd;

fd is unused here

Signature · 1 line
pw-bot: cr