blob: 519d5183626c015ab845d3f375f82c858fd3bf09 [file] [log] [blame]
Eric W. Biederman0dc34c72011-07-13 09:48:26 -07001#define _ATFILE_SOURCE
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <sys/wait.h>
5#include <sys/inotify.h>
6#include <sys/mount.h>
7#include <sys/param.h>
8#include <sys/syscall.h>
9#include <stdio.h>
10#include <string.h>
11#include <sched.h>
12#include <fcntl.h>
13#include <dirent.h>
14#include <errno.h>
15#include <unistd.h>
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +000016#include <ctype.h>
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070017
18#include "utils.h"
19#include "ip_common.h"
Vadim Kochaneb67e442014-12-24 23:04:08 +020020#include "namespace.h"
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070021
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +000022static int usage(void)
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070023{
24 fprintf(stderr, "Usage: ip netns list\n");
25 fprintf(stderr, " ip netns add NAME\n");
26 fprintf(stderr, " ip netns delete NAME\n");
vadimk0948adc2014-11-07 18:25:30 +020027 fprintf(stderr, " ip netns identify [PID]\n");
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +000028 fprintf(stderr, " ip netns pids NAME\n");
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070029 fprintf(stderr, " ip netns exec NAME cmd ...\n");
30 fprintf(stderr, " ip netns monitor\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -070031 exit(-1);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070032}
33
34int get_netns_fd(const char *name)
35{
36 char pathbuf[MAXPATHLEN];
37 const char *path, *ptr;
38
39 path = name;
40 ptr = strchr(name, '/');
41 if (!ptr) {
42 snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
43 NETNS_RUN_DIR, name );
44 path = pathbuf;
45 }
46 return open(path, O_RDONLY);
47}
48
49static int netns_list(int argc, char **argv)
50{
51 struct dirent *entry;
52 DIR *dir;
53
54 dir = opendir(NETNS_RUN_DIR);
55 if (!dir)
Stephen Hemmingera05f6512013-07-12 08:43:23 -070056 return 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070057
58 while ((entry = readdir(dir)) != NULL) {
59 if (strcmp(entry->d_name, ".") == 0)
60 continue;
61 if (strcmp(entry->d_name, "..") == 0)
62 continue;
63 printf("%s\n", entry->d_name);
64 }
65 closedir(dir);
Stephen Hemmingera05f6512013-07-12 08:43:23 -070066 return 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070067}
68
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070069static int netns_exec(int argc, char **argv)
70{
71 /* Setup the proper environment for apps that are not netns
72 * aware, and execute a program in that environment.
73 */
Vadim Kochaneb67e442014-12-24 23:04:08 +020074 const char *cmd;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070075
76 if (argc < 1) {
77 fprintf(stderr, "No netns name specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -070078 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070079 }
80 if (argc < 2) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +000081 fprintf(stderr, "No command specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -070082 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070083 }
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070084 cmd = argv[1];
Stephen Hemmingera05f6512013-07-12 08:43:23 -070085
Vadim Kochaneb67e442014-12-24 23:04:08 +020086 if (netns_switch(argv[0]))
Stephen Hemmingera05f6512013-07-12 08:43:23 -070087 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070088
JunweiZhang95592b42013-07-09 08:55:20 -070089 fflush(stdout);
90
Stephen Hemmingera3aa47a2013-07-16 10:04:05 -070091 if (batch_mode) {
JunweiZhang95592b42013-07-09 08:55:20 -070092 int status;
93 pid_t pid;
94
95 pid = fork();
96 if (pid < 0) {
97 perror("fork");
Stephen Hemmingera05f6512013-07-12 08:43:23 -070098 exit(1);
JunweiZhang95592b42013-07-09 08:55:20 -070099 }
100
101 if (pid != 0) {
102 /* Parent */
103 if (waitpid(pid, &status, 0) < 0) {
104 perror("waitpid");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700105 exit(1);
JunweiZhang95592b42013-07-09 08:55:20 -0700106 }
107
Nicolas Dichtel3c61c012013-08-29 14:29:07 +0200108 if (WIFEXITED(status)) {
109 /* ip must return the status of the child,
110 * but do_cmd() will add a minus to this,
111 * so let's add another one here to cancel it.
112 */
113 return -WEXITSTATUS(status);
114 }
JunweiZhang95592b42013-07-09 08:55:20 -0700115
Nicolas Dichtel3c61c012013-08-29 14:29:07 +0200116 exit(1);
JunweiZhang95592b42013-07-09 08:55:20 -0700117 }
118 }
119
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700120 if (execvp(cmd, argv + 1) < 0)
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000121 fprintf(stderr, "exec of \"%s\" failed: %s\n",
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700122 cmd, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700123 _exit(1);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700124}
125
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000126static int is_pid(const char *str)
127{
128 int ch;
129 for (; (ch = *str); str++) {
130 if (!isdigit(ch))
131 return 0;
132 }
133 return 1;
134}
135
136static int netns_pids(int argc, char **argv)
137{
138 const char *name;
139 char net_path[MAXPATHLEN];
140 int netns;
141 struct stat netst;
142 DIR *dir;
143 struct dirent *entry;
144
145 if (argc < 1) {
146 fprintf(stderr, "No netns name specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700147 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000148 }
149 if (argc > 1) {
150 fprintf(stderr, "extra arguments specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700151 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000152 }
153
154 name = argv[0];
155 snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
156 netns = open(net_path, O_RDONLY);
157 if (netns < 0) {
158 fprintf(stderr, "Cannot open network namespace: %s\n",
159 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700160 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000161 }
162 if (fstat(netns, &netst) < 0) {
163 fprintf(stderr, "Stat of netns failed: %s\n",
164 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700165 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000166 }
167 dir = opendir("/proc/");
168 if (!dir) {
169 fprintf(stderr, "Open of /proc failed: %s\n",
170 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700171 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000172 }
173 while((entry = readdir(dir))) {
174 char pid_net_path[MAXPATHLEN];
175 struct stat st;
176 if (!is_pid(entry->d_name))
177 continue;
178 snprintf(pid_net_path, sizeof(pid_net_path), "/proc/%s/ns/net",
179 entry->d_name);
180 if (stat(pid_net_path, &st) != 0)
181 continue;
182 if ((st.st_dev == netst.st_dev) &&
183 (st.st_ino == netst.st_ino)) {
184 printf("%s\n", entry->d_name);
185 }
186 }
187 closedir(dir);
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700188 return 0;
Stephen Hemminger06125192014-02-17 10:55:31 -0800189
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000190}
191
192static int netns_identify(int argc, char **argv)
193{
194 const char *pidstr;
195 char net_path[MAXPATHLEN];
196 int netns;
197 struct stat netst;
198 DIR *dir;
199 struct dirent *entry;
200
201 if (argc < 1) {
vadimk0948adc2014-11-07 18:25:30 +0200202 pidstr = "self";
203 } else if (argc > 1) {
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000204 fprintf(stderr, "extra arguments specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700205 return -1;
vadimk0948adc2014-11-07 18:25:30 +0200206 } else {
207 pidstr = argv[0];
208 if (!is_pid(pidstr)) {
209 fprintf(stderr, "Specified string '%s' is not a pid\n",
210 pidstr);
211 return -1;
212 }
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000213 }
214
215 snprintf(net_path, sizeof(net_path), "/proc/%s/ns/net", pidstr);
216 netns = open(net_path, O_RDONLY);
217 if (netns < 0) {
218 fprintf(stderr, "Cannot open network namespace: %s\n",
219 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700220 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000221 }
222 if (fstat(netns, &netst) < 0) {
223 fprintf(stderr, "Stat of netns failed: %s\n",
224 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700225 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000226 }
227 dir = opendir(NETNS_RUN_DIR);
228 if (!dir) {
229 /* Succeed treat a missing directory as an empty directory */
230 if (errno == ENOENT)
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700231 return 0;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000232
233 fprintf(stderr, "Failed to open directory %s:%s\n",
234 NETNS_RUN_DIR, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700235 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000236 }
237
238 while((entry = readdir(dir))) {
239 char name_path[MAXPATHLEN];
240 struct stat st;
241
242 if (strcmp(entry->d_name, ".") == 0)
243 continue;
244 if (strcmp(entry->d_name, "..") == 0)
245 continue;
246
247 snprintf(name_path, sizeof(name_path), "%s/%s", NETNS_RUN_DIR,
248 entry->d_name);
249
250 if (stat(name_path, &st) != 0)
251 continue;
252
253 if ((st.st_dev == netst.st_dev) &&
254 (st.st_ino == netst.st_ino)) {
255 printf("%s\n", entry->d_name);
256 }
257 }
258 closedir(dir);
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700259 return 0;
Stephen Hemminger06125192014-02-17 10:55:31 -0800260
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000261}
262
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700263static int netns_delete(int argc, char **argv)
264{
265 const char *name;
266 char netns_path[MAXPATHLEN];
267
268 if (argc < 1) {
269 fprintf(stderr, "No netns name specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700270 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700271 }
272
273 name = argv[0];
274 snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name);
275 umount2(netns_path, MNT_DETACH);
276 if (unlink(netns_path) < 0) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000277 fprintf(stderr, "Cannot remove namespace file \"%s\": %s\n",
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700278 netns_path, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700279 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700280 }
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700281 return 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700282}
283
vadimkc1cbb182014-08-31 22:45:29 +0300284static int create_netns_dir(void)
285{
286 /* Create the base netns directory if it doesn't exist */
287 if (mkdir(NETNS_RUN_DIR, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)) {
288 if (errno != EEXIST) {
289 fprintf(stderr, "mkdir %s failed: %s\n",
290 NETNS_RUN_DIR, strerror(errno));
291 return -1;
292 }
293 }
294
295 return 0;
296}
297
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700298static int netns_add(int argc, char **argv)
299{
300 /* This function creates a new network namespace and
301 * a new mount namespace and bind them into a well known
302 * location in the filesystem based on the name provided.
303 *
304 * The mount namespace is created so that any necessary
305 * userspace tweaks like remounting /sys, or bind mounting
306 * a new /etc/resolv.conf can be shared between uers.
307 */
308 char netns_path[MAXPATHLEN];
309 const char *name;
Eric W. Biederman223f4d82011-07-15 14:29:41 +0000310 int fd;
Eric W. Biederman58a3e822013-01-17 14:47:18 +0000311 int made_netns_run_dir_mount = 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700312
313 if (argc < 1) {
314 fprintf(stderr, "No netns name specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700315 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700316 }
317 name = argv[0];
318
319 snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name);
320
vadimkc1cbb182014-08-31 22:45:29 +0300321 if (create_netns_dir())
322 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700323
Stephen Hemmingerd259f032013-08-04 15:00:56 -0700324 /* Make it possible for network namespace mounts to propagate between
Eric W. Biederman58a3e822013-01-17 14:47:18 +0000325 * mount namespaces. This makes it likely that a unmounting a network
326 * namespace file in one namespace will unmount the network namespace
327 * file in all namespaces allowing the network namespace to be freed
328 * sooner.
329 */
330 while (mount("", NETNS_RUN_DIR, "none", MS_SHARED | MS_REC, NULL)) {
331 /* Fail unless we need to make the mount point */
332 if (errno != EINVAL || made_netns_run_dir_mount) {
333 fprintf(stderr, "mount --make-shared %s failed: %s\n",
334 NETNS_RUN_DIR, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700335 return -1;
Eric W. Biederman58a3e822013-01-17 14:47:18 +0000336 }
337
338 /* Upgrade NETNS_RUN_DIR to a mount point */
339 if (mount(NETNS_RUN_DIR, NETNS_RUN_DIR, "none", MS_BIND, NULL)) {
340 fprintf(stderr, "mount --bind %s %s failed: %s\n",
341 NETNS_RUN_DIR, NETNS_RUN_DIR, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700342 return -1;
Eric W. Biederman58a3e822013-01-17 14:47:18 +0000343 }
344 made_netns_run_dir_mount = 1;
345 }
346
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700347 /* Create the filesystem state */
Eric W. Biederman223f4d82011-07-15 14:29:41 +0000348 fd = open(netns_path, O_RDONLY|O_CREAT|O_EXCL, 0);
349 if (fd < 0) {
Mike Rapoport55713c82014-05-11 12:21:26 +0300350 fprintf(stderr, "Cannot create namespace file \"%s\": %s\n",
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700351 netns_path, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700352 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700353 }
Eric W. Biederman223f4d82011-07-15 14:29:41 +0000354 close(fd);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700355 if (unshare(CLONE_NEWNET) < 0) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000356 fprintf(stderr, "Failed to create a new network namespace \"%s\": %s\n",
357 name, strerror(errno));
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700358 goto out_delete;
359 }
360
361 /* Bind the netns last so I can watch for it */
362 if (mount("/proc/self/ns/net", netns_path, "none", MS_BIND, NULL) < 0) {
363 fprintf(stderr, "Bind /proc/self/ns/net -> %s failed: %s\n",
364 netns_path, strerror(errno));
365 goto out_delete;
366 }
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700367 return 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700368out_delete:
369 netns_delete(argc, argv);
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700370 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700371}
372
373
374static int netns_monitor(int argc, char **argv)
375{
376 char buf[4096];
377 struct inotify_event *event;
378 int fd;
379 fd = inotify_init();
380 if (fd < 0) {
381 fprintf(stderr, "inotify_init failed: %s\n",
382 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700383 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700384 }
vadimkc1cbb182014-08-31 22:45:29 +0300385
386 if (create_netns_dir())
387 return -1;
388
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700389 if (inotify_add_watch(fd, NETNS_RUN_DIR, IN_CREATE | IN_DELETE) < 0) {
390 fprintf(stderr, "inotify_add_watch failed: %s\n",
391 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700392 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700393 }
394 for(;;) {
395 ssize_t len = read(fd, buf, sizeof(buf));
396 if (len < 0) {
397 fprintf(stderr, "read failed: %s\n",
398 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700399 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700400 }
401 for (event = (struct inotify_event *)buf;
402 (char *)event < &buf[len];
403 event = (struct inotify_event *)((char *)event + sizeof(*event) + event->len)) {
404 if (event->mask & IN_CREATE)
405 printf("add %s\n", event->name);
406 if (event->mask & IN_DELETE)
407 printf("delete %s\n", event->name);
408 }
409 }
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700410 return 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700411}
412
413int do_netns(int argc, char **argv)
414{
415 if (argc < 1)
416 return netns_list(0, NULL);
417
418 if ((matches(*argv, "list") == 0) || (matches(*argv, "show") == 0) ||
419 (matches(*argv, "lst") == 0))
420 return netns_list(argc-1, argv+1);
421
422 if (matches(*argv, "help") == 0)
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000423 return usage();
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700424
425 if (matches(*argv, "add") == 0)
426 return netns_add(argc-1, argv+1);
427
428 if (matches(*argv, "delete") == 0)
429 return netns_delete(argc-1, argv+1);
430
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000431 if (matches(*argv, "identify") == 0)
432 return netns_identify(argc-1, argv+1);
433
434 if (matches(*argv, "pids") == 0)
435 return netns_pids(argc-1, argv+1);
436
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700437 if (matches(*argv, "exec") == 0)
438 return netns_exec(argc-1, argv+1);
439
440 if (matches(*argv, "monitor") == 0)
441 return netns_monitor(argc-1, argv+1);
442
443 fprintf(stderr, "Command \"%s\" is unknown, try \"ip netns help\".\n", *argv);
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700444 exit(-1);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700445}