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 "netns_helper.h"
44 #include "test.h"
45 #include "safe_macros.h"
46
47 #define MAX_TRIES 1000
48 #define IP_TUNTAP_MIN_VER 100519
49
50 char *TCID = "netns_netlink";
51 int TST_TOTAL = 1;
52
cleanup(void)53 static void cleanup(void)
54 {
55 tst_rmdir();
56 }
57
setup(void)58 static void setup(void)
59 {
60 tst_require_root();
61 check_iproute(IP_TUNTAP_MIN_VER);
62 check_netns();
63 tst_tmpdir();
64 TST_CHECKPOINT_INIT(tst_rmdir);
65 }
66
child_func(void)67 int child_func(void)
68 {
69 int fd, len, event_found, tries;
70 struct sockaddr_nl sa;
71 char buffer[4096];
72 struct nlmsghdr *nlh;
73
74 /* child will listen to a network interface create/delete/up/down
75 * events */
76 memset(&sa, 0, sizeof(sa));
77 sa.nl_family = AF_NETLINK;
78 sa.nl_groups = RTMGRP_LINK;
79
80 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
81 if (fd == -1) {
82 perror("socket");
83 return 1;
84 }
85 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) == -1) {
86 perror("bind");
87 close(fd);
88 return 1;
89 }
90
91 /* waits for parent to create an interface */
92 TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
93
94 /* To get rid of "resource temporarily unavailable" errors
95 * when testing with -i option */
96 tries = 0;
97 event_found = 0;
98 nlh = (struct nlmsghdr *) buffer;
99 while (tries < MAX_TRIES) {
100 len = recv(fd, nlh, sizeof(buffer), MSG_DONTWAIT);
101 if (len > 0) {
102 /* stop receiving only on interface create/delete
103 * event */
104 if (nlh->nlmsg_type == RTM_NEWLINK ||
105 nlh->nlmsg_type == RTM_DELLINK) {
106 event_found++;
107 break;
108 }
109 }
110 usleep(10000);
111 tries++;
112 }
113
114 close(fd);
115
116 if (!event_found) {
117 perror("recv");
118 return 1;
119 }
120
121 return 0;
122 }
123
test(void)124 static void test(void)
125 {
126 pid_t pid;
127 int status;
128
129 /* unshares the network namespace */
130 if (unshare(CLONE_NEWNET) == -1)
131 tst_brkm(TBROK | TERRNO, cleanup, "unshare failed");
132
133 pid = tst_fork();
134 if (pid < 0) {
135 tst_brkm(TBROK | TERRNO, cleanup, "fork failed");
136 }
137 if (pid == 0) {
138 _exit(child_func());
139 }
140
141 /* creates TAP network interface dummy0 */
142 if (WEXITSTATUS(system("ip tuntap add dev dummy0 mode tap")))
143 tst_brkm(TBROK, cleanup, "system() failed");
144
145 /* removes previously created dummy0 device */
146 if (WEXITSTATUS(system("ip tuntap del mode tap dummy0")))
147 tst_brkm(TBROK, cleanup, "system() failed");
148
149 /* allow child to continue */
150 TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);
151
152
153 SAFE_WAITPID(cleanup, pid, &status, 0);
154 if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
155 tst_resm(TFAIL, "netlink interface fail");
156 return;
157 }
158 if (WIFSIGNALED(status)) {
159 tst_resm(TFAIL, "child was killed with signal %s",
160 tst_strsig(WTERMSIG(status)));
161 return;
162 }
163
164 tst_resm(TPASS, "netlink interface pass");
165 }
166
main(int argc,char * argv[])167 int main(int argc, char *argv[])
168 {
169 int lc;
170
171 tst_parse_opts(argc, argv, NULL, NULL);
172
173 setup();
174
175 for (lc = 0; TEST_LOOPING(lc); lc++)
176 test();
177
178 cleanup();
179 tst_exit();
180 }
181