1 /* Copyright (c) 2014 Red Hat, Inc.
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of version 2 the GNU General Public License as
5 * published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 ***********************************************************************
15 * File: netns_netlink.c
16 *
17 * Tests a netlink interface inside a new network namespace.
18 * Description:
19 * 1. Unshares a network namespace (so network related actions
20 * have no effect on a real system)
21 * 2. Forks a child which creates a NETLINK_ROUTE netlink socket
22 * and listens to RTMGRP_LINK (network interface create/delete/up/down)
23 * multicast group.
24 * 4. Child then waits for parent approval to receive data from socket
25 * 3. Parent creates a new TAP interface (dummy0) and immediately
26 * removes it (which should generate some data in child's netlink socket).
27 * Then it allows child to continue.
28 * 4. As the child was listening to RTMGRP_LINK multicast group, it should
29 * detect the new interface creation/deletion (by reading data from netlink
30 * socket), if so, the test passes, otherwise it fails.
31 */
32
33 #define _GNU_SOURCE
34 #include <sys/wait.h>
35 #include <asm/types.h>
36 #include <sys/socket.h>
37 #include <linux/netlink.h>
38 #include <linux/rtnetlink.h>
39 #include <unistd.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <errno.h>
43 #include "test.h"
44 #include "safe_macros.h"
45 #include "netns_helper.h"
46
47
48 #define MAX_TRIES 1000
49 #define IP_TUNTAP_MIN_VER 100519
50
51 char *TCID = "netns_netlink";
52 int TST_TOTAL = 1;
53
cleanup(void)54 static void cleanup(void)
55 {
56 tst_rmdir();
57 }
58
setup(void)59 static void setup(void)
60 {
61 tst_require_root();
62 check_iproute(IP_TUNTAP_MIN_VER);
63 check_netns();
64 tst_tmpdir();
65 TST_CHECKPOINT_INIT(tst_rmdir);
66 }
67
child_func(void)68 int child_func(void)
69 {
70 int fd, len, event_found, tries;
71 struct sockaddr_nl sa;
72 char buffer[4096];
73 struct nlmsghdr *nlh;
74
75 /* child will listen to a network interface create/delete/up/down
76 * events */
77 memset(&sa, 0, sizeof(sa));
78 sa.nl_family = AF_NETLINK;
79 sa.nl_groups = RTMGRP_LINK;
80
81 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
82 if (fd == -1) {
83 perror("socket");
84 return 1;
85 }
86 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) == -1) {
87 perror("bind");
88 close(fd);
89 return 1;
90 }
91
92 /* waits for parent to create an interface */
93 TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
94
95 /* To get rid of "resource temporarily unavailable" errors
96 * when testing with -i option */
97 tries = 0;
98 event_found = 0;
99 nlh = (struct nlmsghdr *) buffer;
100 while (tries < MAX_TRIES) {
101 len = recv(fd, nlh, sizeof(buffer), MSG_DONTWAIT);
102 if (len > 0) {
103 /* stop receiving only on interface create/delete
104 * event */
105 if (nlh->nlmsg_type == RTM_NEWLINK ||
106 nlh->nlmsg_type == RTM_DELLINK) {
107 event_found++;
108 break;
109 }
110 }
111 usleep(10000);
112 tries++;
113 }
114
115 close(fd);
116
117 if (!event_found) {
118 perror("recv");
119 return 1;
120 }
121
122 return 0;
123 }
124
test(void)125 static void test(void)
126 {
127 pid_t pid;
128 int status;
129
130 /* unshares the network namespace */
131 if (unshare(CLONE_NEWNET) == -1)
132 tst_brkm(TBROK | TERRNO, cleanup, "unshare failed");
133
134 pid = tst_fork();
135 if (pid < 0) {
136 tst_brkm(TBROK | TERRNO, cleanup, "fork failed");
137 }
138 if (pid == 0) {
139 _exit(child_func());
140 }
141
142 /* creates TAP network interface dummy0 */
143 if (WEXITSTATUS(system("ip tuntap add dev dummy0 mode tap")))
144 tst_brkm(TBROK, cleanup, "system() failed");
145
146 /* removes previously created dummy0 device */
147 if (WEXITSTATUS(system("ip tuntap del mode tap dummy0")))
148 tst_brkm(TBROK, cleanup, "system() failed");
149
150 /* allow child to continue */
151 TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);
152
153
154 SAFE_WAITPID(cleanup, pid, &status, 0);
155 if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
156 tst_resm(TFAIL, "netlink interface fail");
157 return;
158 }
159 if (WIFSIGNALED(status)) {
160 tst_resm(TFAIL, "child was killed with signal %s",
161 tst_strsig(WTERMSIG(status)));
162 return;
163 }
164
165 tst_resm(TPASS, "netlink interface pass");
166 }
167
main(int argc,char * argv[])168 int main(int argc, char *argv[])
169 {
170 int lc;
171
172 tst_parse_opts(argc, argv, NULL, NULL);
173
174 setup();
175
176 for (lc = 0; TEST_LOOPING(lc); lc++)
177 test();
178
179 cleanup();
180 tst_exit();
181 }
182