blob: af87065309e2831a37c662f308f1bc793ee4afaf [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>
Eric W. Biederman0dc34c72011-07-13 09:48:26 -07007#include <sys/syscall.h>
8#include <stdio.h>
9#include <string.h>
10#include <sched.h>
11#include <fcntl.h>
12#include <dirent.h>
13#include <errno.h>
14#include <unistd.h>
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +000015#include <ctype.h>
Nicolas Dichteld652ccb2015-04-15 14:23:22 +020016#include <linux/limits.h>
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070017
Nicolas Dichteld182ee12015-02-17 17:30:37 +010018#include <linux/net_namespace.h>
19
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070020#include "utils.h"
Jiri Pirko4952b452016-03-22 10:02:20 +010021#include "list.h"
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070022#include "ip_common.h"
Vadim Kochaneb67e442014-12-24 23:04:08 +020023#include "namespace.h"
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070024
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +000025static int usage(void)
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070026{
27 fprintf(stderr, "Usage: ip netns list\n");
28 fprintf(stderr, " ip netns add NAME\n");
Nicolas Dichteld182ee12015-02-17 17:30:37 +010029 fprintf(stderr, " ip netns set NAME NETNSID\n");
Vadim Kochan33724932015-01-18 16:10:19 +020030 fprintf(stderr, " ip [-all] netns delete [NAME]\n");
vadimk0948adc2014-11-07 18:25:30 +020031 fprintf(stderr, " ip netns identify [PID]\n");
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +000032 fprintf(stderr, " ip netns pids NAME\n");
Vadim Kochanb13ba032015-01-18 16:10:18 +020033 fprintf(stderr, " ip [-all] netns exec [NAME] cmd ...\n");
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070034 fprintf(stderr, " ip netns monitor\n");
Nicolas Dichteld652ccb2015-04-15 14:23:22 +020035 fprintf(stderr, " ip netns list-id\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -070036 exit(-1);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070037}
38
Nicolas Dichteld652ccb2015-04-15 14:23:22 +020039/* This socket is used to get nsid */
40static struct rtnl_handle rtnsh = { .fd = -1 };
41
Nicolas Dichtel4c7d9a52015-04-13 10:34:26 +020042static int have_rtnl_getnsid = -1;
43
44static int ipnetns_accept_msg(const struct sockaddr_nl *who,
Nicolas Dichtel0628cdd2015-05-20 16:19:58 +020045 struct rtnl_ctrl_data *ctrl,
Nicolas Dichtel4c7d9a52015-04-13 10:34:26 +020046 struct nlmsghdr *n, void *arg)
47{
48 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(n);
49
50 if (n->nlmsg_type == NLMSG_ERROR &&
51 (err->error == -EOPNOTSUPP || err->error == -EINVAL))
52 have_rtnl_getnsid = 0;
53 else
54 have_rtnl_getnsid = 1;
55 return -1;
56}
57
58static int ipnetns_have_nsid(void)
59{
60 struct {
61 struct nlmsghdr n;
62 struct rtgenmsg g;
63 char buf[1024];
Phil Sutterd17b1362016-07-18 16:48:42 +020064 } req = {
65 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
66 .n.nlmsg_flags = NLM_F_REQUEST,
67 .n.nlmsg_type = RTM_GETNSID,
68 .g.rtgen_family = AF_UNSPEC,
69 };
Nicolas Dichtel4c7d9a52015-04-13 10:34:26 +020070 int fd;
71
72 if (have_rtnl_getnsid < 0) {
Nicolas Dichtel4c7d9a52015-04-13 10:34:26 +020073 fd = open("/proc/self/ns/net", O_RDONLY);
74 if (fd < 0) {
75 perror("open(\"/proc/self/ns/net\")");
76 exit(1);
77 }
78
79 addattr32(&req.n, 1024, NETNSA_FD, fd);
80
81 if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) {
82 perror("request send failed");
83 exit(1);
84 }
85 rtnl_listen(&rth, ipnetns_accept_msg, NULL);
86 close(fd);
87 }
88
89 return have_rtnl_getnsid;
90}
91
Nicolas Dichteld182ee12015-02-17 17:30:37 +010092static int get_netnsid_from_name(const char *name)
93{
94 struct {
95 struct nlmsghdr n;
96 struct rtgenmsg g;
97 char buf[1024];
Phil Sutterd17b1362016-07-18 16:48:42 +020098 } answer, req = {
99 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
100 .n.nlmsg_flags = NLM_F_REQUEST,
101 .n.nlmsg_type = RTM_GETNSID,
102 .g.rtgen_family = AF_UNSPEC,
103 };
Nicolas Dichteld182ee12015-02-17 17:30:37 +0100104 struct rtattr *tb[NETNSA_MAX + 1];
105 struct rtgenmsg *rthdr;
106 int len, fd;
107
Nicolas Dichteld182ee12015-02-17 17:30:37 +0100108 fd = netns_get_fd(name);
109 if (fd < 0)
110 return fd;
111
112 addattr32(&req.n, 1024, NETNSA_FD, fd);
Stephen Hemmingerc079e122015-05-27 12:26:14 -0700113 if (rtnl_talk(&rtnsh, &req.n, &answer.n, sizeof(answer)) < 0) {
Nicolas Dichteld182ee12015-02-17 17:30:37 +0100114 close(fd);
115 return -2;
116 }
117 close(fd);
118
119 /* Validate message and parse attributes */
120 if (answer.n.nlmsg_type == NLMSG_ERROR)
121 return -1;
122
123 rthdr = NLMSG_DATA(&answer.n);
124 len = answer.n.nlmsg_len - NLMSG_SPACE(sizeof(*rthdr));
125 if (len < 0)
126 return -1;
127
128 parse_rtattr(tb, NETNSA_MAX, NETNS_RTA(rthdr), len);
129
130 if (tb[NETNSA_NSID])
131 return rta_getattr_u32(tb[NETNSA_NSID]);
132
133 return -1;
134}
135
Nicolas Dichteld652ccb2015-04-15 14:23:22 +0200136struct nsid_cache {
137 struct hlist_node nsid_hash;
138 struct hlist_node name_hash;
139 int nsid;
Stephen Hemminger2f29d6b2015-08-12 08:35:54 -0700140 char name[0];
Nicolas Dichteld652ccb2015-04-15 14:23:22 +0200141};
142
143#define NSIDMAP_SIZE 128
144#define NSID_HASH_NSID(nsid) (nsid & (NSIDMAP_SIZE - 1))
145#define NSID_HASH_NAME(name) (namehash(name) & (NSIDMAP_SIZE - 1))
146
147static struct hlist_head nsid_head[NSIDMAP_SIZE];
148static struct hlist_head name_head[NSIDMAP_SIZE];
149
150static struct nsid_cache *netns_map_get_by_nsid(int nsid)
151{
152 uint32_t h = NSID_HASH_NSID(nsid);
153 struct hlist_node *n;
154
155 hlist_for_each(n, &nsid_head[h]) {
156 struct nsid_cache *c = container_of(n, struct nsid_cache,
157 nsid_hash);
158 if (c->nsid == nsid)
159 return c;
160 }
161
162 return NULL;
163}
164
Stephen Hemminger2f29d6b2015-08-12 08:35:54 -0700165static int netns_map_add(int nsid, const char *name)
Nicolas Dichteld652ccb2015-04-15 14:23:22 +0200166{
167 struct nsid_cache *c;
168 uint32_t h;
169
170 if (netns_map_get_by_nsid(nsid) != NULL)
171 return -EEXIST;
172
Nicolas Cavallaria1b4a272016-02-12 14:47:39 +0100173 c = malloc(sizeof(*c) + strlen(name) + 1);
Nicolas Dichteld652ccb2015-04-15 14:23:22 +0200174 if (c == NULL) {
175 perror("malloc");
176 return -ENOMEM;
177 }
178 c->nsid = nsid;
179 strcpy(c->name, name);
180
181 h = NSID_HASH_NSID(nsid);
182 hlist_add_head(&c->nsid_hash, &nsid_head[h]);
183
184 h = NSID_HASH_NAME(name);
185 hlist_add_head(&c->name_hash, &name_head[h]);
186
187 return 0;
188}
189
190static void netns_map_del(struct nsid_cache *c)
191{
192 hlist_del(&c->name_hash);
193 hlist_del(&c->nsid_hash);
194 free(c);
195}
196
197void netns_map_init(void)
198{
199 static int initialized;
200 struct dirent *entry;
201 DIR *dir;
202 int nsid;
203
204 if (initialized || !ipnetns_have_nsid())
205 return;
206
207 if (rtnl_open(&rtnsh, 0) < 0) {
208 fprintf(stderr, "Cannot open rtnetlink\n");
209 exit(1);
210 }
211
212 dir = opendir(NETNS_RUN_DIR);
213 if (!dir)
214 return;
215
216 while ((entry = readdir(dir)) != NULL) {
217 if (strcmp(entry->d_name, ".") == 0)
218 continue;
219 if (strcmp(entry->d_name, "..") == 0)
220 continue;
221 nsid = get_netnsid_from_name(entry->d_name);
222
223 if (nsid >= 0)
224 netns_map_add(nsid, entry->d_name);
225 }
226 closedir(dir);
227 initialized = 1;
228}
229
230static int netns_get_name(int nsid, char *name)
231{
232 struct dirent *entry;
233 DIR *dir;
234 int id;
235
236 dir = opendir(NETNS_RUN_DIR);
237 if (!dir)
238 return -ENOENT;
239
240 while ((entry = readdir(dir)) != NULL) {
241 if (strcmp(entry->d_name, ".") == 0)
242 continue;
243 if (strcmp(entry->d_name, "..") == 0)
244 continue;
245 id = get_netnsid_from_name(entry->d_name);
246
247 if (nsid == id) {
248 strcpy(name, entry->d_name);
249 closedir(dir);
250 return 0;
251 }
252 }
253 closedir(dir);
254 return -ENOENT;
255}
256
257int print_nsid(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
258{
259 struct rtgenmsg *rthdr = NLMSG_DATA(n);
260 struct rtattr *tb[NETNSA_MAX+1];
261 int len = n->nlmsg_len;
262 FILE *fp = (FILE *)arg;
263 struct nsid_cache *c;
264 char name[NAME_MAX];
265 int nsid;
266
267 if (n->nlmsg_type != RTM_NEWNSID && n->nlmsg_type != RTM_DELNSID)
268 return 0;
269
270 len -= NLMSG_SPACE(sizeof(*rthdr));
271 if (len < 0) {
272 fprintf(stderr, "BUG: wrong nlmsg len %d in %s\n", len,
273 __func__);
274 return -1;
275 }
276
277 parse_rtattr(tb, NETNSA_MAX, NETNS_RTA(rthdr), len);
278 if (tb[NETNSA_NSID] == NULL) {
279 fprintf(stderr, "BUG: NETNSA_NSID is missing %s\n", __func__);
280 return -1;
281 }
282
283 if (n->nlmsg_type == RTM_DELNSID)
284 fprintf(fp, "Deleted ");
285
286 nsid = rta_getattr_u32(tb[NETNSA_NSID]);
287 fprintf(fp, "nsid %u ", nsid);
288
289 c = netns_map_get_by_nsid(nsid);
290 if (c != NULL) {
291 fprintf(fp, "(iproute2 netns name: %s)", c->name);
292 netns_map_del(c);
293 }
294
295 /* During 'ip monitor nsid', no chance to have new nsid in cache. */
296 if (c == NULL && n->nlmsg_type == RTM_NEWNSID)
297 if (netns_get_name(nsid, name) == 0) {
298 fprintf(fp, "(iproute2 netns name: %s)", name);
299 netns_map_add(nsid, name);
300 }
301
302 fprintf(fp, "\n");
303 fflush(fp);
304 return 0;
305}
306
307static int netns_list_id(int argc, char **argv)
308{
309 if (!ipnetns_have_nsid()) {
310 fprintf(stderr,
311 "RTM_GETNSID is not supported by the kernel.\n");
312 return -ENOTSUP;
313 }
314
315 if (rtnl_wilddump_request(&rth, AF_UNSPEC, RTM_GETNSID) < 0) {
316 perror("Cannot send dump request");
317 exit(1);
318 }
319 if (rtnl_dump_filter(&rth, print_nsid, stdout) < 0) {
320 fprintf(stderr, "Dump terminated\n");
321 exit(1);
322 }
323 return 0;
324}
325
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700326static int netns_list(int argc, char **argv)
327{
328 struct dirent *entry;
329 DIR *dir;
Nicolas Dichteld182ee12015-02-17 17:30:37 +0100330 int id;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700331
332 dir = opendir(NETNS_RUN_DIR);
333 if (!dir)
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700334 return 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700335
336 while ((entry = readdir(dir)) != NULL) {
337 if (strcmp(entry->d_name, ".") == 0)
338 continue;
339 if (strcmp(entry->d_name, "..") == 0)
340 continue;
Nicolas Dichteld182ee12015-02-17 17:30:37 +0100341 printf("%s", entry->d_name);
Nicolas Dichtel4c7d9a52015-04-13 10:34:26 +0200342 if (ipnetns_have_nsid()) {
343 id = get_netnsid_from_name(entry->d_name);
344 if (id >= 0)
345 printf(" (id: %d)", id);
346 }
Nicolas Dichteld182ee12015-02-17 17:30:37 +0100347 printf("\n");
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700348 }
349 closedir(dir);
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700350 return 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700351}
352
Vadim Kochanb13ba032015-01-18 16:10:18 +0200353static int cmd_exec(const char *cmd, char **argv, bool do_fork)
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700354{
JunweiZhang95592b42013-07-09 08:55:20 -0700355 fflush(stdout);
Vadim Kochanb13ba032015-01-18 16:10:18 +0200356 if (do_fork) {
JunweiZhang95592b42013-07-09 08:55:20 -0700357 int status;
358 pid_t pid;
359
360 pid = fork();
361 if (pid < 0) {
362 perror("fork");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700363 exit(1);
JunweiZhang95592b42013-07-09 08:55:20 -0700364 }
365
366 if (pid != 0) {
367 /* Parent */
368 if (waitpid(pid, &status, 0) < 0) {
369 perror("waitpid");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700370 exit(1);
JunweiZhang95592b42013-07-09 08:55:20 -0700371 }
372
Nicolas Dichtel3c61c012013-08-29 14:29:07 +0200373 if (WIFEXITED(status)) {
Vadim Kochanb13ba032015-01-18 16:10:18 +0200374 return WEXITSTATUS(status);
Nicolas Dichtel3c61c012013-08-29 14:29:07 +0200375 }
JunweiZhang95592b42013-07-09 08:55:20 -0700376
Nicolas Dichtel3c61c012013-08-29 14:29:07 +0200377 exit(1);
JunweiZhang95592b42013-07-09 08:55:20 -0700378 }
379 }
380
Vadim Kochanb13ba032015-01-18 16:10:18 +0200381 if (execvp(cmd, argv) < 0)
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000382 fprintf(stderr, "exec of \"%s\" failed: %s\n",
Vadim Kochanb13ba032015-01-18 16:10:18 +0200383 cmd, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700384 _exit(1);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700385}
386
Vadim Kochanb13ba032015-01-18 16:10:18 +0200387static int on_netns_exec(char *nsname, void *arg)
388{
389 char **argv = arg;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700390
Vadim Kochanb13ba032015-01-18 16:10:18 +0200391 cmd_exec(argv[1], argv + 1, true);
392 return 0;
393}
394
395static int netns_exec(int argc, char **argv)
396{
397 /* Setup the proper environment for apps that are not netns
398 * aware, and execute a program in that environment.
399 */
400 const char *cmd;
401
402 if (argc < 1 && !do_all) {
403 fprintf(stderr, "No netns name specified\n");
404 return -1;
405 }
406 if ((argc < 2 && !do_all) || (argc < 1 && do_all)) {
407 fprintf(stderr, "No command specified\n");
408 return -1;
409 }
410
411 if (do_all)
412 return do_each_netns(on_netns_exec, --argv, 1);
413
414 if (netns_switch(argv[0]))
415 return -1;
416
417 /* ip must return the status of the child,
418 * but do_cmd() will add a minus to this,
419 * so let's add another one here to cancel it.
420 */
421 cmd = argv[1];
422 return -cmd_exec(cmd, argv + 1, !!batch_mode);
423}
424
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000425static int is_pid(const char *str)
426{
427 int ch;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700428
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000429 for (; (ch = *str); str++) {
430 if (!isdigit(ch))
431 return 0;
432 }
433 return 1;
434}
435
436static int netns_pids(int argc, char **argv)
437{
438 const char *name;
Felix Jandaea343662015-07-26 21:25:32 +0200439 char net_path[PATH_MAX];
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000440 int netns;
441 struct stat netst;
442 DIR *dir;
443 struct dirent *entry;
444
445 if (argc < 1) {
446 fprintf(stderr, "No netns name specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700447 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000448 }
449 if (argc > 1) {
450 fprintf(stderr, "extra arguments specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700451 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000452 }
453
454 name = argv[0];
455 snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
456 netns = open(net_path, O_RDONLY);
457 if (netns < 0) {
458 fprintf(stderr, "Cannot open network namespace: %s\n",
459 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700460 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000461 }
462 if (fstat(netns, &netst) < 0) {
463 fprintf(stderr, "Stat of netns failed: %s\n",
464 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700465 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000466 }
467 dir = opendir("/proc/");
468 if (!dir) {
469 fprintf(stderr, "Open of /proc failed: %s\n",
470 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700471 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000472 }
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700473 while ((entry = readdir(dir))) {
Felix Jandaea343662015-07-26 21:25:32 +0200474 char pid_net_path[PATH_MAX];
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000475 struct stat st;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700476
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000477 if (!is_pid(entry->d_name))
478 continue;
479 snprintf(pid_net_path, sizeof(pid_net_path), "/proc/%s/ns/net",
480 entry->d_name);
481 if (stat(pid_net_path, &st) != 0)
482 continue;
483 if ((st.st_dev == netst.st_dev) &&
484 (st.st_ino == netst.st_ino)) {
485 printf("%s\n", entry->d_name);
486 }
487 }
488 closedir(dir);
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700489 return 0;
Stephen Hemminger06125192014-02-17 10:55:31 -0800490
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000491}
492
493static int netns_identify(int argc, char **argv)
494{
495 const char *pidstr;
Felix Jandaea343662015-07-26 21:25:32 +0200496 char net_path[PATH_MAX];
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000497 int netns;
498 struct stat netst;
499 DIR *dir;
500 struct dirent *entry;
501
502 if (argc < 1) {
vadimk0948adc2014-11-07 18:25:30 +0200503 pidstr = "self";
504 } else if (argc > 1) {
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000505 fprintf(stderr, "extra arguments specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700506 return -1;
vadimk0948adc2014-11-07 18:25:30 +0200507 } else {
508 pidstr = argv[0];
509 if (!is_pid(pidstr)) {
510 fprintf(stderr, "Specified string '%s' is not a pid\n",
511 pidstr);
512 return -1;
513 }
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000514 }
515
516 snprintf(net_path, sizeof(net_path), "/proc/%s/ns/net", pidstr);
517 netns = open(net_path, O_RDONLY);
518 if (netns < 0) {
519 fprintf(stderr, "Cannot open network namespace: %s\n",
520 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700521 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000522 }
523 if (fstat(netns, &netst) < 0) {
524 fprintf(stderr, "Stat of netns failed: %s\n",
525 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700526 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000527 }
528 dir = opendir(NETNS_RUN_DIR);
529 if (!dir) {
530 /* Succeed treat a missing directory as an empty directory */
531 if (errno == ENOENT)
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700532 return 0;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000533
534 fprintf(stderr, "Failed to open directory %s:%s\n",
535 NETNS_RUN_DIR, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700536 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000537 }
538
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700539 while ((entry = readdir(dir))) {
Felix Jandaea343662015-07-26 21:25:32 +0200540 char name_path[PATH_MAX];
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000541 struct stat st;
542
543 if (strcmp(entry->d_name, ".") == 0)
544 continue;
545 if (strcmp(entry->d_name, "..") == 0)
546 continue;
547
548 snprintf(name_path, sizeof(name_path), "%s/%s", NETNS_RUN_DIR,
549 entry->d_name);
550
551 if (stat(name_path, &st) != 0)
552 continue;
553
554 if ((st.st_dev == netst.st_dev) &&
555 (st.st_ino == netst.st_ino)) {
556 printf("%s\n", entry->d_name);
557 }
558 }
559 closedir(dir);
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700560 return 0;
Stephen Hemminger06125192014-02-17 10:55:31 -0800561
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000562}
563
Vadim Kochan33724932015-01-18 16:10:19 +0200564static int on_netns_del(char *nsname, void *arg)
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700565{
Felix Jandaea343662015-07-26 21:25:32 +0200566 char netns_path[PATH_MAX];
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700567
Vadim Kochan33724932015-01-18 16:10:19 +0200568 snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, nsname);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700569 umount2(netns_path, MNT_DETACH);
570 if (unlink(netns_path) < 0) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000571 fprintf(stderr, "Cannot remove namespace file \"%s\": %s\n",
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700572 netns_path, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700573 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700574 }
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700575 return 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700576}
577
Vadim Kochan33724932015-01-18 16:10:19 +0200578static int netns_delete(int argc, char **argv)
579{
580 if (argc < 1 && !do_all) {
581 fprintf(stderr, "No netns name specified\n");
582 return -1;
583 }
584
585 if (do_all)
586 return netns_foreach(on_netns_del, NULL);
587
588 return on_netns_del(argv[0], NULL);
589}
590
vadimkc1cbb182014-08-31 22:45:29 +0300591static int create_netns_dir(void)
592{
593 /* Create the base netns directory if it doesn't exist */
594 if (mkdir(NETNS_RUN_DIR, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)) {
595 if (errno != EEXIST) {
596 fprintf(stderr, "mkdir %s failed: %s\n",
597 NETNS_RUN_DIR, strerror(errno));
598 return -1;
599 }
600 }
601
602 return 0;
603}
604
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700605static int netns_add(int argc, char **argv)
606{
607 /* This function creates a new network namespace and
608 * a new mount namespace and bind them into a well known
609 * location in the filesystem based on the name provided.
610 *
611 * The mount namespace is created so that any necessary
612 * userspace tweaks like remounting /sys, or bind mounting
613 * a new /etc/resolv.conf can be shared between uers.
614 */
Felix Jandaea343662015-07-26 21:25:32 +0200615 char netns_path[PATH_MAX];
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700616 const char *name;
Eric W. Biederman223f4d82011-07-15 14:29:41 +0000617 int fd;
Eric W. Biederman58a3e822013-01-17 14:47:18 +0000618 int made_netns_run_dir_mount = 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700619
620 if (argc < 1) {
621 fprintf(stderr, "No netns name specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700622 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700623 }
624 name = argv[0];
625
626 snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name);
627
vadimkc1cbb182014-08-31 22:45:29 +0300628 if (create_netns_dir())
629 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700630
Stephen Hemmingerd259f032013-08-04 15:00:56 -0700631 /* Make it possible for network namespace mounts to propagate between
Eric W. Biederman58a3e822013-01-17 14:47:18 +0000632 * mount namespaces. This makes it likely that a unmounting a network
633 * namespace file in one namespace will unmount the network namespace
634 * file in all namespaces allowing the network namespace to be freed
635 * sooner.
636 */
637 while (mount("", NETNS_RUN_DIR, "none", MS_SHARED | MS_REC, NULL)) {
638 /* Fail unless we need to make the mount point */
639 if (errno != EINVAL || made_netns_run_dir_mount) {
640 fprintf(stderr, "mount --make-shared %s failed: %s\n",
641 NETNS_RUN_DIR, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700642 return -1;
Eric W. Biederman58a3e822013-01-17 14:47:18 +0000643 }
644
645 /* Upgrade NETNS_RUN_DIR to a mount point */
646 if (mount(NETNS_RUN_DIR, NETNS_RUN_DIR, "none", MS_BIND, NULL)) {
647 fprintf(stderr, "mount --bind %s %s failed: %s\n",
648 NETNS_RUN_DIR, NETNS_RUN_DIR, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700649 return -1;
Eric W. Biederman58a3e822013-01-17 14:47:18 +0000650 }
651 made_netns_run_dir_mount = 1;
652 }
653
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700654 /* Create the filesystem state */
Eric W. Biederman223f4d82011-07-15 14:29:41 +0000655 fd = open(netns_path, O_RDONLY|O_CREAT|O_EXCL, 0);
656 if (fd < 0) {
Mike Rapoport55713c82014-05-11 12:21:26 +0300657 fprintf(stderr, "Cannot create namespace file \"%s\": %s\n",
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700658 netns_path, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700659 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700660 }
Eric W. Biederman223f4d82011-07-15 14:29:41 +0000661 close(fd);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700662 if (unshare(CLONE_NEWNET) < 0) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000663 fprintf(stderr, "Failed to create a new network namespace \"%s\": %s\n",
664 name, strerror(errno));
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700665 goto out_delete;
666 }
667
668 /* Bind the netns last so I can watch for it */
669 if (mount("/proc/self/ns/net", netns_path, "none", MS_BIND, NULL) < 0) {
670 fprintf(stderr, "Bind /proc/self/ns/net -> %s failed: %s\n",
671 netns_path, strerror(errno));
672 goto out_delete;
673 }
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700674 return 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700675out_delete:
676 netns_delete(argc, argv);
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700677 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700678}
679
Nicolas Dichteld182ee12015-02-17 17:30:37 +0100680static int set_netnsid_from_name(const char *name, int nsid)
681{
682 struct {
683 struct nlmsghdr n;
684 struct rtgenmsg g;
685 char buf[1024];
Phil Sutterd17b1362016-07-18 16:48:42 +0200686 } req = {
687 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
688 .n.nlmsg_flags = NLM_F_REQUEST,
689 .n.nlmsg_type = RTM_NEWNSID,
690 .g.rtgen_family = AF_UNSPEC,
691 };
Nicolas Dichteld182ee12015-02-17 17:30:37 +0100692 int fd, err = 0;
693
Nicolas Dichteld182ee12015-02-17 17:30:37 +0100694 fd = netns_get_fd(name);
695 if (fd < 0)
696 return fd;
697
698 addattr32(&req.n, 1024, NETNSA_FD, fd);
699 addattr32(&req.n, 1024, NETNSA_NSID, nsid);
Stephen Hemmingerc079e122015-05-27 12:26:14 -0700700 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0)
Nicolas Dichteld182ee12015-02-17 17:30:37 +0100701 err = -2;
702
703 close(fd);
704 return err;
705}
706
707static int netns_set(int argc, char **argv)
708{
Felix Jandaea343662015-07-26 21:25:32 +0200709 char netns_path[PATH_MAX];
Nicolas Dichteld182ee12015-02-17 17:30:37 +0100710 const char *name;
711 int netns, nsid;
712
713 if (argc < 1) {
714 fprintf(stderr, "No netns name specified\n");
715 return -1;
716 }
717 if (argc < 2) {
718 fprintf(stderr, "No nsid specified\n");
719 return -1;
720 }
721 name = argv[0];
722 nsid = atoi(argv[1]);
723
724 snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name);
725 netns = open(netns_path, O_RDONLY | O_CLOEXEC);
726 if (netns < 0) {
727 fprintf(stderr, "Cannot open network namespace \"%s\": %s\n",
728 name, strerror(errno));
729 return -1;
730 }
731
732 return set_netnsid_from_name(name, nsid);
733}
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700734
735static int netns_monitor(int argc, char **argv)
736{
737 char buf[4096];
738 struct inotify_event *event;
739 int fd;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700740
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700741 fd = inotify_init();
742 if (fd < 0) {
743 fprintf(stderr, "inotify_init failed: %s\n",
744 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700745 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700746 }
vadimkc1cbb182014-08-31 22:45:29 +0300747
748 if (create_netns_dir())
749 return -1;
750
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700751 if (inotify_add_watch(fd, NETNS_RUN_DIR, IN_CREATE | IN_DELETE) < 0) {
752 fprintf(stderr, "inotify_add_watch failed: %s\n",
753 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700754 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700755 }
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700756 for (;;) {
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700757 ssize_t len = read(fd, buf, sizeof(buf));
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700758
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700759 if (len < 0) {
760 fprintf(stderr, "read failed: %s\n",
761 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700762 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700763 }
764 for (event = (struct inotify_event *)buf;
765 (char *)event < &buf[len];
766 event = (struct inotify_event *)((char *)event + sizeof(*event) + event->len)) {
767 if (event->mask & IN_CREATE)
768 printf("add %s\n", event->name);
769 if (event->mask & IN_DELETE)
770 printf("delete %s\n", event->name);
771 }
772 }
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700773 return 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700774}
775
776int do_netns(int argc, char **argv)
777{
Nicolas Dichteld652ccb2015-04-15 14:23:22 +0200778 netns_map_init();
779
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700780 if (argc < 1)
781 return netns_list(0, NULL);
782
783 if ((matches(*argv, "list") == 0) || (matches(*argv, "show") == 0) ||
784 (matches(*argv, "lst") == 0))
785 return netns_list(argc-1, argv+1);
786
Nicolas Dichteld652ccb2015-04-15 14:23:22 +0200787 if ((matches(*argv, "list-id") == 0))
788 return netns_list_id(argc-1, argv+1);
789
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700790 if (matches(*argv, "help") == 0)
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000791 return usage();
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700792
793 if (matches(*argv, "add") == 0)
794 return netns_add(argc-1, argv+1);
795
Nicolas Dichteld182ee12015-02-17 17:30:37 +0100796 if (matches(*argv, "set") == 0)
797 return netns_set(argc-1, argv+1);
798
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700799 if (matches(*argv, "delete") == 0)
800 return netns_delete(argc-1, argv+1);
801
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000802 if (matches(*argv, "identify") == 0)
803 return netns_identify(argc-1, argv+1);
804
805 if (matches(*argv, "pids") == 0)
806 return netns_pids(argc-1, argv+1);
807
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700808 if (matches(*argv, "exec") == 0)
809 return netns_exec(argc-1, argv+1);
810
811 if (matches(*argv, "monitor") == 0)
812 return netns_monitor(argc-1, argv+1);
813
814 fprintf(stderr, "Command \"%s\" is unknown, try \"ip netns help\".\n", *argv);
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700815 exit(-1);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700816}