• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2014 Red Hat, Inc.
4  * Copyright (c) 2021 Petr Vorel <pvorel@suse.cz>
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Tests a netlink interface inside a new network namespace.
11  *
12  * - Unshares a network namespace (so network related actions
13  *   have no effect on a real system).
14  * - Forks a child which creates a NETLINK_ROUTE netlink socket
15  *   and listens to RTMGRP_LINK (network interface create/delete/up/down)
16  *   multicast group.
17  * - Child then waits for parent approval to receive data from socket
18  * - Parent creates a new TAP interface (dummy0) and immediately
19  *   removes it (which should generate some data in child's netlink socket).
20  *   Then it allows child to continue.
21  * - As the child was listening to RTMGRP_LINK multicast group, it should
22  *   detect the new interface creation/deletion (by reading data from netlink
23  *   socket), if so, the test passes, otherwise it fails.
24  */
25 
26 #define _GNU_SOURCE
27 #include <sys/wait.h>
28 #include <asm/types.h>
29 #include <sys/socket.h>
30 #include <linux/netlink.h>
31 #include <linux/rtnetlink.h>
32 #include <unistd.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 
37 #include "tst_test.h"
38 #include "tst_safe_macros.h"
39 #include "lapi/sched.h"
40 
41 #define MAX_TRIES 1000
42 
child_func(void)43 static void child_func(void)
44 {
45 	int fd, len, event_found, tries;
46 	struct sockaddr_nl sa;
47 	char buffer[4096];
48 	struct nlmsghdr *nlh;
49 
50 	/* child will listen to a network interface create/delete/up/down events */
51 	memset(&sa, 0, sizeof(sa));
52 	sa.nl_family = AF_NETLINK;
53 	sa.nl_groups = RTMGRP_LINK;
54 
55 	fd = SAFE_SOCKET(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
56 	SAFE_BIND(fd, (struct sockaddr *) &sa, sizeof(sa));
57 
58 	/* waits for parent to create an interface */
59 	TST_CHECKPOINT_WAKE_AND_WAIT(0);
60 
61 	/*
62 	 * To get rid of "resource temporarily unavailable" errors
63 	 * when testing with -i option
64 	 */
65 	tries = 0;
66 	event_found = 0;
67 	nlh = (struct nlmsghdr *) buffer;
68 	while (tries < MAX_TRIES) {
69 		len = recv(fd, nlh, sizeof(buffer), MSG_DONTWAIT);
70 		if (len > 0) {
71 			/* stop receiving only on interface create/delete event */
72 			if (nlh->nlmsg_type == RTM_NEWLINK ||
73 			    nlh->nlmsg_type == RTM_DELLINK) {
74 				event_found++;
75 				break;
76 			}
77 		}
78 		usleep(10000);
79 		tries++;
80 	}
81 
82 	SAFE_CLOSE(fd);
83 
84 	if (event_found)
85 		tst_res(TPASS, "interface changes detected");
86 	else
87 		tst_res(TFAIL, "failed to detect interface changes");
88 
89 	exit(0);
90 }
91 
test_netns_netlink(void)92 static void test_netns_netlink(void)
93 {
94 	/* unshares the network namespace */
95 	SAFE_UNSHARE(CLONE_NEWNET);
96 
97 	if (SAFE_FORK() == 0)
98 		child_func();
99 
100 	/* wait until child opens netlink socket */
101 	TST_CHECKPOINT_WAIT(0);
102 
103 	/* creates TAP network interface dummy0 */
104 	if (WEXITSTATUS(system("ip tuntap add dev dummy0 mode tap")))
105 		tst_brk(TBROK, "adding interface failed");
106 
107 	/* removes previously created dummy0 device */
108 	if (WEXITSTATUS(system("ip tuntap del mode tap dummy0")))
109 		tst_brk(TBROK, "removing interface failed");
110 
111 	/* allow child to continue */
112 	TST_CHECKPOINT_WAKE(0);
113 
114 	tst_reap_children();
115 }
116 
117 
118 static struct tst_test test = {
119 	.test_all = test_netns_netlink,
120 	.needs_checkpoints = 1,
121 	.needs_root = 1,
122 	.forks_child = 1,
123 	.needs_kconfigs = (const char *[]) {
124 		"CONFIG_NET_NS=y",
125 		"CONFIG_TUN",
126 		NULL
127 	},
128 };
129