tools/testing: add a selftest for SO_NETNS_COOKIE
authorLorenz Bauer <lmb@cloudflare.com>
Wed, 23 Jun 2021 13:56:46 +0000 (15:56 +0200)
committerDavid S. Miller <davem@davemloft.net>
Thu, 24 Jun 2021 18:13:05 +0000 (11:13 -0700)
Make sure that SO_NETNS_COOKIE returns a non-zero value, and
that sockets from different namespaces have a distinct cookie
value.

Signed-off-by: Lorenz Bauer <lmb@cloudflare.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
tools/testing/selftests/net/.gitignore
tools/testing/selftests/net/Makefile
tools/testing/selftests/net/config
tools/testing/selftests/net/so_netns_cookie.c [new file with mode: 0644]

index 61ae899..19deb9c 100644 (file)
@@ -30,3 +30,4 @@ hwtstamp_config
 rxtimestamp
 timestamping
 txtimestamp
+so_netns_cookie
index 3915bb7..79c9eb0 100644 (file)
@@ -30,7 +30,7 @@ TEST_GEN_FILES =  socket nettest
 TEST_GEN_FILES += psock_fanout psock_tpacket msg_zerocopy reuseport_addr_any
 TEST_GEN_FILES += tcp_mmap tcp_inq psock_snd txring_overwrite
 TEST_GEN_FILES += udpgso udpgso_bench_tx udpgso_bench_rx ip_defrag
-TEST_GEN_FILES += so_txtime ipv6_flowlabel ipv6_flowlabel_mgr
+TEST_GEN_FILES += so_txtime ipv6_flowlabel ipv6_flowlabel_mgr so_netns_cookie
 TEST_GEN_FILES += tcp_fastopen_backup_key
 TEST_GEN_FILES += fin_ack_lat
 TEST_GEN_FILES += reuseaddr_ports_exhausted
index 614d547..6f905b5 100644 (file)
@@ -1,4 +1,5 @@
 CONFIG_USER_NS=y
+CONFIG_NET_NS=y
 CONFIG_BPF_SYSCALL=y
 CONFIG_TEST_BPF=m
 CONFIG_NUMA=y
diff --git a/tools/testing/selftests/net/so_netns_cookie.c b/tools/testing/selftests/net/so_netns_cookie.c
new file mode 100644 (file)
index 0000000..b39e87e
--- /dev/null
@@ -0,0 +1,61 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <sched.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#ifndef SO_NETNS_COOKIE
+#define SO_NETNS_COOKIE 71
+#endif
+
+#define pr_err(fmt, ...) \
+       ({ \
+               fprintf(stderr, "%s:%d:" fmt ": %m\n", \
+                       __func__, __LINE__, ##__VA_ARGS__); \
+               1; \
+       })
+
+int main(int argc, char *argvp[])
+{
+       uint64_t cookie1, cookie2;
+       socklen_t vallen;
+       int sock1, sock2;
+
+       sock1 = socket(AF_INET, SOCK_STREAM, 0);
+       if (sock1 < 0)
+               return pr_err("Unable to create TCP socket");
+
+       vallen = sizeof(cookie1);
+       if (getsockopt(sock1, SOL_SOCKET, SO_NETNS_COOKIE, &cookie1, &vallen) != 0)
+               return pr_err("getsockopt(SOL_SOCKET, SO_NETNS_COOKIE)");
+
+       if (!cookie1)
+               return pr_err("SO_NETNS_COOKIE returned zero cookie");
+
+       if (unshare(CLONE_NEWNET))
+               return pr_err("unshare");
+
+       sock2 = socket(AF_INET, SOCK_STREAM, 0);
+       if (sock2 < 0)
+               return pr_err("Unable to create TCP socket");
+
+       vallen = sizeof(cookie2);
+       if (getsockopt(sock2, SOL_SOCKET, SO_NETNS_COOKIE, &cookie2, &vallen) != 0)
+               return pr_err("getsockopt(SOL_SOCKET, SO_NETNS_COOKIE)");
+
+       if (!cookie2)
+               return pr_err("SO_NETNS_COOKIE returned zero cookie");
+
+       if (cookie1 == cookie2)
+               return pr_err("SO_NETNS_COOKIE returned identical cookies for distinct ns");
+
+       close(sock1);
+       close(sock2);
+       return 0;
+}