forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools/testing: add a selftest for SO_NETNS_COOKIE
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 <[email protected]> Signed-off-by: David S. Miller <[email protected]>
- Loading branch information
Showing
4 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,4 @@ hwtstamp_config | |
rxtimestamp | ||
timestamping | ||
txtimestamp | ||
so_netns_cookie |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
CONFIG_USER_NS=y | ||
CONFIG_NET_NS=y | ||
CONFIG_BPF_SYSCALL=y | ||
CONFIG_TEST_BPF=m | ||
CONFIG_NUMA=y | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |