]>
Commit | Line | Data |
---|---|---|
0dc34c77 EB |
1 | #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> | |
0dc34c77 EB |
7 | #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> | |
9a7b3d91 | 15 | #include <ctype.h> |
d652ccbf | 16 | #include <linux/limits.h> |
0dc34c77 | 17 | |
d182ee13 ND |
18 | #include <linux/net_namespace.h> |
19 | ||
0dc34c77 | 20 | #include "utils.h" |
4952b459 | 21 | #include "list.h" |
0dc34c77 | 22 | #include "ip_common.h" |
eb67e449 | 23 | #include "namespace.h" |
0dc34c77 | 24 | |
8e2d47dc | 25 | static int usage(void) |
0dc34c77 EB |
26 | { |
27 | fprintf(stderr, "Usage: ip netns list\n"); | |
28 | fprintf(stderr, " ip netns add NAME\n"); | |
d182ee13 | 29 | fprintf(stderr, " ip netns set NAME NETNSID\n"); |
33724939 | 30 | fprintf(stderr, " ip [-all] netns delete [NAME]\n"); |
0948adc0 | 31 | fprintf(stderr, " ip netns identify [PID]\n"); |
9a7b3d91 | 32 | fprintf(stderr, " ip netns pids NAME\n"); |
b13ba03f | 33 | fprintf(stderr, " ip [-all] netns exec [NAME] cmd ...\n"); |
0dc34c77 | 34 | fprintf(stderr, " ip netns monitor\n"); |
d652ccbf | 35 | fprintf(stderr, " ip netns list-id\n"); |
a05f6511 | 36 | exit(-1); |
0dc34c77 EB |
37 | } |
38 | ||
d652ccbf ND |
39 | /* This socket is used to get nsid */ |
40 | static struct rtnl_handle rtnsh = { .fd = -1 }; | |
41 | ||
4c7d9a58 ND |
42 | static int have_rtnl_getnsid = -1; |
43 | ||
44 | static int ipnetns_accept_msg(const struct sockaddr_nl *who, | |
0628cddd | 45 | struct rtnl_ctrl_data *ctrl, |
4c7d9a58 ND |
46 | 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 | ||
58 | static int ipnetns_have_nsid(void) | |
59 | { | |
60 | struct { | |
61 | struct nlmsghdr n; | |
62 | struct rtgenmsg g; | |
63 | char buf[1024]; | |
d17b136f PS |
64 | } 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 | }; | |
4c7d9a58 ND |
70 | int fd; |
71 | ||
72 | if (have_rtnl_getnsid < 0) { | |
4c7d9a58 ND |
73 | fd = open("/proc/self/ns/net", O_RDONLY); |
74 | if (fd < 0) { | |
c44003f7 LZ |
75 | have_rtnl_getnsid = 0; |
76 | return 0; | |
4c7d9a58 ND |
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 | ||
d182ee13 ND |
92 | static int get_netnsid_from_name(const char *name) |
93 | { | |
94 | struct { | |
95 | struct nlmsghdr n; | |
96 | struct rtgenmsg g; | |
97 | char buf[1024]; | |
d17b136f PS |
98 | } 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 | }; | |
d182ee13 ND |
104 | struct rtattr *tb[NETNSA_MAX + 1]; |
105 | struct rtgenmsg *rthdr; | |
106 | int len, fd; | |
107 | ||
d182ee13 ND |
108 | fd = netns_get_fd(name); |
109 | if (fd < 0) | |
110 | return fd; | |
111 | ||
112 | addattr32(&req.n, 1024, NETNSA_FD, fd); | |
c079e121 | 113 | if (rtnl_talk(&rtnsh, &req.n, &answer.n, sizeof(answer)) < 0) { |
d182ee13 ND |
114 | 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 | ||
d652ccbf ND |
136 | struct nsid_cache { |
137 | struct hlist_node nsid_hash; | |
138 | struct hlist_node name_hash; | |
139 | int nsid; | |
2f29d6bb | 140 | char name[0]; |
d652ccbf ND |
141 | }; |
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 | ||
147 | static struct hlist_head nsid_head[NSIDMAP_SIZE]; | |
148 | static struct hlist_head name_head[NSIDMAP_SIZE]; | |
149 | ||
150 | static 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 | ||
2f29d6bb | 165 | static int netns_map_add(int nsid, const char *name) |
d652ccbf ND |
166 | { |
167 | struct nsid_cache *c; | |
168 | uint32_t h; | |
169 | ||
170 | if (netns_map_get_by_nsid(nsid) != NULL) | |
171 | return -EEXIST; | |
172 | ||
a1b4a274 | 173 | c = malloc(sizeof(*c) + strlen(name) + 1); |
d652ccbf ND |
174 | 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 | ||
190 | static 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 | ||
e29a8e05 AA |
197 | void netns_nsid_socket_init(void) |
198 | { | |
199 | if (rtnsh.fd > -1 || !ipnetns_have_nsid()) | |
200 | return; | |
201 | ||
202 | if (rtnl_open(&rtnsh, 0) < 0) { | |
203 | fprintf(stderr, "Cannot open rtnetlink\n"); | |
204 | exit(1); | |
205 | } | |
206 | ||
207 | } | |
208 | ||
d652ccbf ND |
209 | void netns_map_init(void) |
210 | { | |
211 | static int initialized; | |
212 | struct dirent *entry; | |
213 | DIR *dir; | |
214 | int nsid; | |
215 | ||
216 | if (initialized || !ipnetns_have_nsid()) | |
217 | return; | |
218 | ||
d652ccbf ND |
219 | dir = opendir(NETNS_RUN_DIR); |
220 | if (!dir) | |
221 | return; | |
222 | ||
223 | while ((entry = readdir(dir)) != NULL) { | |
224 | if (strcmp(entry->d_name, ".") == 0) | |
225 | continue; | |
226 | if (strcmp(entry->d_name, "..") == 0) | |
227 | continue; | |
228 | nsid = get_netnsid_from_name(entry->d_name); | |
229 | ||
230 | if (nsid >= 0) | |
231 | netns_map_add(nsid, entry->d_name); | |
232 | } | |
233 | closedir(dir); | |
234 | initialized = 1; | |
235 | } | |
236 | ||
237 | static int netns_get_name(int nsid, char *name) | |
238 | { | |
239 | struct dirent *entry; | |
240 | DIR *dir; | |
241 | int id; | |
242 | ||
243 | dir = opendir(NETNS_RUN_DIR); | |
244 | if (!dir) | |
245 | return -ENOENT; | |
246 | ||
247 | while ((entry = readdir(dir)) != NULL) { | |
248 | if (strcmp(entry->d_name, ".") == 0) | |
249 | continue; | |
250 | if (strcmp(entry->d_name, "..") == 0) | |
251 | continue; | |
252 | id = get_netnsid_from_name(entry->d_name); | |
253 | ||
254 | if (nsid == id) { | |
255 | strcpy(name, entry->d_name); | |
256 | closedir(dir); | |
257 | return 0; | |
258 | } | |
259 | } | |
260 | closedir(dir); | |
261 | return -ENOENT; | |
262 | } | |
263 | ||
264 | int print_nsid(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) | |
265 | { | |
266 | struct rtgenmsg *rthdr = NLMSG_DATA(n); | |
267 | struct rtattr *tb[NETNSA_MAX+1]; | |
268 | int len = n->nlmsg_len; | |
269 | FILE *fp = (FILE *)arg; | |
270 | struct nsid_cache *c; | |
271 | char name[NAME_MAX]; | |
272 | int nsid; | |
273 | ||
274 | if (n->nlmsg_type != RTM_NEWNSID && n->nlmsg_type != RTM_DELNSID) | |
275 | return 0; | |
276 | ||
277 | len -= NLMSG_SPACE(sizeof(*rthdr)); | |
278 | if (len < 0) { | |
279 | fprintf(stderr, "BUG: wrong nlmsg len %d in %s\n", len, | |
280 | __func__); | |
281 | return -1; | |
282 | } | |
283 | ||
284 | parse_rtattr(tb, NETNSA_MAX, NETNS_RTA(rthdr), len); | |
285 | if (tb[NETNSA_NSID] == NULL) { | |
286 | fprintf(stderr, "BUG: NETNSA_NSID is missing %s\n", __func__); | |
287 | return -1; | |
288 | } | |
289 | ||
290 | if (n->nlmsg_type == RTM_DELNSID) | |
291 | fprintf(fp, "Deleted "); | |
292 | ||
293 | nsid = rta_getattr_u32(tb[NETNSA_NSID]); | |
294 | fprintf(fp, "nsid %u ", nsid); | |
295 | ||
296 | c = netns_map_get_by_nsid(nsid); | |
297 | if (c != NULL) { | |
298 | fprintf(fp, "(iproute2 netns name: %s)", c->name); | |
299 | netns_map_del(c); | |
300 | } | |
301 | ||
302 | /* During 'ip monitor nsid', no chance to have new nsid in cache. */ | |
303 | if (c == NULL && n->nlmsg_type == RTM_NEWNSID) | |
304 | if (netns_get_name(nsid, name) == 0) { | |
305 | fprintf(fp, "(iproute2 netns name: %s)", name); | |
306 | netns_map_add(nsid, name); | |
307 | } | |
308 | ||
309 | fprintf(fp, "\n"); | |
310 | fflush(fp); | |
311 | return 0; | |
312 | } | |
313 | ||
314 | static int netns_list_id(int argc, char **argv) | |
315 | { | |
316 | if (!ipnetns_have_nsid()) { | |
317 | fprintf(stderr, | |
318 | "RTM_GETNSID is not supported by the kernel.\n"); | |
319 | return -ENOTSUP; | |
320 | } | |
321 | ||
322 | if (rtnl_wilddump_request(&rth, AF_UNSPEC, RTM_GETNSID) < 0) { | |
323 | perror("Cannot send dump request"); | |
324 | exit(1); | |
325 | } | |
326 | if (rtnl_dump_filter(&rth, print_nsid, stdout) < 0) { | |
327 | fprintf(stderr, "Dump terminated\n"); | |
328 | exit(1); | |
329 | } | |
330 | return 0; | |
331 | } | |
332 | ||
0dc34c77 EB |
333 | static int netns_list(int argc, char **argv) |
334 | { | |
335 | struct dirent *entry; | |
336 | DIR *dir; | |
d182ee13 | 337 | int id; |
0dc34c77 EB |
338 | |
339 | dir = opendir(NETNS_RUN_DIR); | |
340 | if (!dir) | |
a05f6511 | 341 | return 0; |
0dc34c77 EB |
342 | |
343 | while ((entry = readdir(dir)) != NULL) { | |
344 | if (strcmp(entry->d_name, ".") == 0) | |
345 | continue; | |
346 | if (strcmp(entry->d_name, "..") == 0) | |
347 | continue; | |
d182ee13 | 348 | printf("%s", entry->d_name); |
4c7d9a58 ND |
349 | if (ipnetns_have_nsid()) { |
350 | id = get_netnsid_from_name(entry->d_name); | |
351 | if (id >= 0) | |
352 | printf(" (id: %d)", id); | |
353 | } | |
d182ee13 | 354 | printf("\n"); |
0dc34c77 EB |
355 | } |
356 | closedir(dir); | |
a05f6511 | 357 | return 0; |
0dc34c77 EB |
358 | } |
359 | ||
b13ba03f VK |
360 | static int on_netns_exec(char *nsname, void *arg) |
361 | { | |
362 | char **argv = arg; | |
56f5daac | 363 | |
b13ba03f VK |
364 | cmd_exec(argv[1], argv + 1, true); |
365 | return 0; | |
366 | } | |
367 | ||
368 | static int netns_exec(int argc, char **argv) | |
369 | { | |
370 | /* Setup the proper environment for apps that are not netns | |
371 | * aware, and execute a program in that environment. | |
372 | */ | |
373 | const char *cmd; | |
374 | ||
375 | if (argc < 1 && !do_all) { | |
376 | fprintf(stderr, "No netns name specified\n"); | |
377 | return -1; | |
378 | } | |
379 | if ((argc < 2 && !do_all) || (argc < 1 && do_all)) { | |
380 | fprintf(stderr, "No command specified\n"); | |
381 | return -1; | |
382 | } | |
383 | ||
384 | if (do_all) | |
385 | return do_each_netns(on_netns_exec, --argv, 1); | |
386 | ||
387 | if (netns_switch(argv[0])) | |
388 | return -1; | |
389 | ||
ee9369a0 DA |
390 | /* we just changed namespaces. clear any vrf association |
391 | * with prior namespace before exec'ing command | |
392 | */ | |
393 | vrf_reset(); | |
394 | ||
b13ba03f VK |
395 | /* ip must return the status of the child, |
396 | * but do_cmd() will add a minus to this, | |
397 | * so let's add another one here to cancel it. | |
398 | */ | |
399 | cmd = argv[1]; | |
400 | return -cmd_exec(cmd, argv + 1, !!batch_mode); | |
401 | } | |
402 | ||
9a7b3d91 EB |
403 | static int is_pid(const char *str) |
404 | { | |
405 | int ch; | |
56f5daac | 406 | |
9a7b3d91 EB |
407 | for (; (ch = *str); str++) { |
408 | if (!isdigit(ch)) | |
409 | return 0; | |
410 | } | |
411 | return 1; | |
412 | } | |
413 | ||
414 | static int netns_pids(int argc, char **argv) | |
415 | { | |
416 | const char *name; | |
ea343669 | 417 | char net_path[PATH_MAX]; |
9a7b3d91 EB |
418 | int netns; |
419 | struct stat netst; | |
420 | DIR *dir; | |
421 | struct dirent *entry; | |
422 | ||
423 | if (argc < 1) { | |
424 | fprintf(stderr, "No netns name specified\n"); | |
a05f6511 | 425 | return -1; |
9a7b3d91 EB |
426 | } |
427 | if (argc > 1) { | |
428 | fprintf(stderr, "extra arguments specified\n"); | |
a05f6511 | 429 | return -1; |
9a7b3d91 EB |
430 | } |
431 | ||
432 | name = argv[0]; | |
433 | snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name); | |
434 | netns = open(net_path, O_RDONLY); | |
435 | if (netns < 0) { | |
436 | fprintf(stderr, "Cannot open network namespace: %s\n", | |
437 | strerror(errno)); | |
a05f6511 | 438 | return -1; |
9a7b3d91 EB |
439 | } |
440 | if (fstat(netns, &netst) < 0) { | |
441 | fprintf(stderr, "Stat of netns failed: %s\n", | |
442 | strerror(errno)); | |
a05f6511 | 443 | return -1; |
9a7b3d91 EB |
444 | } |
445 | dir = opendir("/proc/"); | |
446 | if (!dir) { | |
447 | fprintf(stderr, "Open of /proc failed: %s\n", | |
448 | strerror(errno)); | |
a05f6511 | 449 | return -1; |
9a7b3d91 | 450 | } |
56f5daac | 451 | while ((entry = readdir(dir))) { |
ea343669 | 452 | char pid_net_path[PATH_MAX]; |
9a7b3d91 | 453 | struct stat st; |
56f5daac | 454 | |
9a7b3d91 EB |
455 | if (!is_pid(entry->d_name)) |
456 | continue; | |
457 | snprintf(pid_net_path, sizeof(pid_net_path), "/proc/%s/ns/net", | |
458 | entry->d_name); | |
459 | if (stat(pid_net_path, &st) != 0) | |
460 | continue; | |
461 | if ((st.st_dev == netst.st_dev) && | |
462 | (st.st_ino == netst.st_ino)) { | |
463 | printf("%s\n", entry->d_name); | |
464 | } | |
465 | } | |
466 | closedir(dir); | |
a05f6511 | 467 | return 0; |
0612519e | 468 | |
9a7b3d91 EB |
469 | } |
470 | ||
9c49438a | 471 | int netns_identify_pid(const char *pidstr, char *name, int len) |
9a7b3d91 | 472 | { |
ea343669 | 473 | char net_path[PATH_MAX]; |
9a7b3d91 EB |
474 | int netns; |
475 | struct stat netst; | |
476 | DIR *dir; | |
477 | struct dirent *entry; | |
478 | ||
9c49438a | 479 | name[0] = '\0'; |
9a7b3d91 EB |
480 | |
481 | snprintf(net_path, sizeof(net_path), "/proc/%s/ns/net", pidstr); | |
482 | netns = open(net_path, O_RDONLY); | |
483 | if (netns < 0) { | |
484 | fprintf(stderr, "Cannot open network namespace: %s\n", | |
485 | strerror(errno)); | |
a05f6511 | 486 | return -1; |
9a7b3d91 EB |
487 | } |
488 | if (fstat(netns, &netst) < 0) { | |
489 | fprintf(stderr, "Stat of netns failed: %s\n", | |
490 | strerror(errno)); | |
a05f6511 | 491 | return -1; |
9a7b3d91 EB |
492 | } |
493 | dir = opendir(NETNS_RUN_DIR); | |
494 | if (!dir) { | |
495 | /* Succeed treat a missing directory as an empty directory */ | |
496 | if (errno == ENOENT) | |
a05f6511 | 497 | return 0; |
9a7b3d91 EB |
498 | |
499 | fprintf(stderr, "Failed to open directory %s:%s\n", | |
500 | NETNS_RUN_DIR, strerror(errno)); | |
a05f6511 | 501 | return -1; |
9a7b3d91 EB |
502 | } |
503 | ||
56f5daac | 504 | while ((entry = readdir(dir))) { |
ea343669 | 505 | char name_path[PATH_MAX]; |
9a7b3d91 EB |
506 | struct stat st; |
507 | ||
508 | if (strcmp(entry->d_name, ".") == 0) | |
509 | continue; | |
510 | if (strcmp(entry->d_name, "..") == 0) | |
511 | continue; | |
512 | ||
513 | snprintf(name_path, sizeof(name_path), "%s/%s", NETNS_RUN_DIR, | |
514 | entry->d_name); | |
515 | ||
516 | if (stat(name_path, &st) != 0) | |
517 | continue; | |
518 | ||
519 | if ((st.st_dev == netst.st_dev) && | |
520 | (st.st_ino == netst.st_ino)) { | |
9c49438a DA |
521 | strncpy(name, entry->d_name, len - 1); |
522 | name[len - 1] = '\0'; | |
9a7b3d91 EB |
523 | } |
524 | } | |
525 | closedir(dir); | |
a05f6511 | 526 | return 0; |
0612519e | 527 | |
9a7b3d91 EB |
528 | } |
529 | ||
9c49438a DA |
530 | static int netns_identify(int argc, char **argv) |
531 | { | |
532 | const char *pidstr; | |
533 | char name[256]; | |
534 | int rc; | |
535 | ||
536 | if (argc < 1) { | |
537 | pidstr = "self"; | |
538 | } else if (argc > 1) { | |
539 | fprintf(stderr, "extra arguments specified\n"); | |
540 | return -1; | |
541 | } else { | |
542 | pidstr = argv[0]; | |
543 | if (!is_pid(pidstr)) { | |
544 | fprintf(stderr, "Specified string '%s' is not a pid\n", | |
545 | pidstr); | |
546 | return -1; | |
547 | } | |
548 | } | |
549 | ||
550 | rc = netns_identify_pid(pidstr, name, sizeof(name)); | |
551 | if (!rc) | |
552 | printf("%s\n", name); | |
553 | ||
554 | return rc; | |
555 | } | |
556 | ||
33724939 | 557 | static int on_netns_del(char *nsname, void *arg) |
0dc34c77 | 558 | { |
ea343669 | 559 | char netns_path[PATH_MAX]; |
0dc34c77 | 560 | |
33724939 | 561 | snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, nsname); |
0dc34c77 EB |
562 | umount2(netns_path, MNT_DETACH); |
563 | if (unlink(netns_path) < 0) { | |
14645ec2 | 564 | fprintf(stderr, "Cannot remove namespace file \"%s\": %s\n", |
0dc34c77 | 565 | netns_path, strerror(errno)); |
a05f6511 | 566 | return -1; |
0dc34c77 | 567 | } |
a05f6511 | 568 | return 0; |
0dc34c77 EB |
569 | } |
570 | ||
33724939 VK |
571 | static int netns_delete(int argc, char **argv) |
572 | { | |
573 | if (argc < 1 && !do_all) { | |
574 | fprintf(stderr, "No netns name specified\n"); | |
575 | return -1; | |
576 | } | |
577 | ||
578 | if (do_all) | |
579 | return netns_foreach(on_netns_del, NULL); | |
580 | ||
581 | return on_netns_del(argv[0], NULL); | |
582 | } | |
583 | ||
c1cbb18a | 584 | static int create_netns_dir(void) |
585 | { | |
586 | /* Create the base netns directory if it doesn't exist */ | |
587 | if (mkdir(NETNS_RUN_DIR, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)) { | |
588 | if (errno != EEXIST) { | |
589 | fprintf(stderr, "mkdir %s failed: %s\n", | |
590 | NETNS_RUN_DIR, strerror(errno)); | |
591 | return -1; | |
592 | } | |
593 | } | |
594 | ||
595 | return 0; | |
596 | } | |
597 | ||
0dc34c77 EB |
598 | static int netns_add(int argc, char **argv) |
599 | { | |
600 | /* This function creates a new network namespace and | |
601 | * a new mount namespace and bind them into a well known | |
602 | * location in the filesystem based on the name provided. | |
603 | * | |
604 | * The mount namespace is created so that any necessary | |
605 | * userspace tweaks like remounting /sys, or bind mounting | |
606 | * a new /etc/resolv.conf can be shared between uers. | |
607 | */ | |
ea343669 | 608 | char netns_path[PATH_MAX]; |
0dc34c77 | 609 | const char *name; |
223f4d8e | 610 | int fd; |
58a3e827 | 611 | int made_netns_run_dir_mount = 0; |
0dc34c77 EB |
612 | |
613 | if (argc < 1) { | |
614 | fprintf(stderr, "No netns name specified\n"); | |
a05f6511 | 615 | return -1; |
0dc34c77 EB |
616 | } |
617 | name = argv[0]; | |
618 | ||
619 | snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name); | |
620 | ||
c1cbb18a | 621 | if (create_netns_dir()) |
622 | return -1; | |
0dc34c77 | 623 | |
d259f030 | 624 | /* Make it possible for network namespace mounts to propagate between |
58a3e827 EB |
625 | * mount namespaces. This makes it likely that a unmounting a network |
626 | * namespace file in one namespace will unmount the network namespace | |
627 | * file in all namespaces allowing the network namespace to be freed | |
628 | * sooner. | |
629 | */ | |
630 | while (mount("", NETNS_RUN_DIR, "none", MS_SHARED | MS_REC, NULL)) { | |
631 | /* Fail unless we need to make the mount point */ | |
632 | if (errno != EINVAL || made_netns_run_dir_mount) { | |
633 | fprintf(stderr, "mount --make-shared %s failed: %s\n", | |
634 | NETNS_RUN_DIR, strerror(errno)); | |
a05f6511 | 635 | return -1; |
58a3e827 EB |
636 | } |
637 | ||
638 | /* Upgrade NETNS_RUN_DIR to a mount point */ | |
d6a4076b | 639 | if (mount(NETNS_RUN_DIR, NETNS_RUN_DIR, "none", MS_BIND | MS_REC, NULL)) { |
58a3e827 EB |
640 | fprintf(stderr, "mount --bind %s %s failed: %s\n", |
641 | NETNS_RUN_DIR, NETNS_RUN_DIR, strerror(errno)); | |
a05f6511 | 642 | return -1; |
58a3e827 EB |
643 | } |
644 | made_netns_run_dir_mount = 1; | |
645 | } | |
646 | ||
0dc34c77 | 647 | /* Create the filesystem state */ |
223f4d8e EB |
648 | fd = open(netns_path, O_RDONLY|O_CREAT|O_EXCL, 0); |
649 | if (fd < 0) { | |
55713c8c | 650 | fprintf(stderr, "Cannot create namespace file \"%s\": %s\n", |
0dc34c77 | 651 | netns_path, strerror(errno)); |
a05f6511 | 652 | return -1; |
0dc34c77 | 653 | } |
223f4d8e | 654 | close(fd); |
0dc34c77 | 655 | if (unshare(CLONE_NEWNET) < 0) { |
14645ec2 KR |
656 | fprintf(stderr, "Failed to create a new network namespace \"%s\": %s\n", |
657 | name, strerror(errno)); | |
0dc34c77 EB |
658 | goto out_delete; |
659 | } | |
660 | ||
661 | /* Bind the netns last so I can watch for it */ | |
662 | if (mount("/proc/self/ns/net", netns_path, "none", MS_BIND, NULL) < 0) { | |
663 | fprintf(stderr, "Bind /proc/self/ns/net -> %s failed: %s\n", | |
664 | netns_path, strerror(errno)); | |
665 | goto out_delete; | |
666 | } | |
a05f6511 | 667 | return 0; |
0dc34c77 EB |
668 | out_delete: |
669 | netns_delete(argc, argv); | |
a05f6511 | 670 | return -1; |
0dc34c77 EB |
671 | } |
672 | ||
d182ee13 ND |
673 | static int set_netnsid_from_name(const char *name, int nsid) |
674 | { | |
675 | struct { | |
676 | struct nlmsghdr n; | |
677 | struct rtgenmsg g; | |
678 | char buf[1024]; | |
d17b136f PS |
679 | } req = { |
680 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)), | |
681 | .n.nlmsg_flags = NLM_F_REQUEST, | |
682 | .n.nlmsg_type = RTM_NEWNSID, | |
683 | .g.rtgen_family = AF_UNSPEC, | |
684 | }; | |
d182ee13 ND |
685 | int fd, err = 0; |
686 | ||
d182ee13 ND |
687 | fd = netns_get_fd(name); |
688 | if (fd < 0) | |
689 | return fd; | |
690 | ||
691 | addattr32(&req.n, 1024, NETNSA_FD, fd); | |
692 | addattr32(&req.n, 1024, NETNSA_NSID, nsid); | |
c079e121 | 693 | if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) |
d182ee13 ND |
694 | err = -2; |
695 | ||
696 | close(fd); | |
697 | return err; | |
698 | } | |
699 | ||
700 | static int netns_set(int argc, char **argv) | |
701 | { | |
ea343669 | 702 | char netns_path[PATH_MAX]; |
d182ee13 ND |
703 | const char *name; |
704 | int netns, nsid; | |
705 | ||
706 | if (argc < 1) { | |
707 | fprintf(stderr, "No netns name specified\n"); | |
708 | return -1; | |
709 | } | |
710 | if (argc < 2) { | |
711 | fprintf(stderr, "No nsid specified\n"); | |
712 | return -1; | |
713 | } | |
714 | name = argv[0]; | |
715 | nsid = atoi(argv[1]); | |
716 | ||
717 | snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name); | |
718 | netns = open(netns_path, O_RDONLY | O_CLOEXEC); | |
719 | if (netns < 0) { | |
720 | fprintf(stderr, "Cannot open network namespace \"%s\": %s\n", | |
721 | name, strerror(errno)); | |
722 | return -1; | |
723 | } | |
724 | ||
725 | return set_netnsid_from_name(name, nsid); | |
726 | } | |
0dc34c77 EB |
727 | |
728 | static int netns_monitor(int argc, char **argv) | |
729 | { | |
730 | char buf[4096]; | |
731 | struct inotify_event *event; | |
732 | int fd; | |
56f5daac | 733 | |
0dc34c77 EB |
734 | fd = inotify_init(); |
735 | if (fd < 0) { | |
736 | fprintf(stderr, "inotify_init failed: %s\n", | |
737 | strerror(errno)); | |
a05f6511 | 738 | return -1; |
0dc34c77 | 739 | } |
c1cbb18a | 740 | |
741 | if (create_netns_dir()) | |
742 | return -1; | |
743 | ||
0dc34c77 EB |
744 | if (inotify_add_watch(fd, NETNS_RUN_DIR, IN_CREATE | IN_DELETE) < 0) { |
745 | fprintf(stderr, "inotify_add_watch failed: %s\n", | |
746 | strerror(errno)); | |
a05f6511 | 747 | return -1; |
0dc34c77 | 748 | } |
56f5daac | 749 | for (;;) { |
0dc34c77 | 750 | ssize_t len = read(fd, buf, sizeof(buf)); |
56f5daac | 751 | |
0dc34c77 EB |
752 | if (len < 0) { |
753 | fprintf(stderr, "read failed: %s\n", | |
754 | strerror(errno)); | |
a05f6511 | 755 | return -1; |
0dc34c77 EB |
756 | } |
757 | for (event = (struct inotify_event *)buf; | |
758 | (char *)event < &buf[len]; | |
759 | event = (struct inotify_event *)((char *)event + sizeof(*event) + event->len)) { | |
760 | if (event->mask & IN_CREATE) | |
761 | printf("add %s\n", event->name); | |
762 | if (event->mask & IN_DELETE) | |
763 | printf("delete %s\n", event->name); | |
764 | } | |
765 | } | |
a05f6511 | 766 | return 0; |
0dc34c77 EB |
767 | } |
768 | ||
79928fd0 MC |
769 | static int invalid_name(const char *name) |
770 | { | |
d3f0b091 MC |
771 | return !*name || strlen(name) > NAME_MAX || |
772 | strchr(name, '/') || !strcmp(name, ".") || !strcmp(name, ".."); | |
79928fd0 MC |
773 | } |
774 | ||
0dc34c77 EB |
775 | int do_netns(int argc, char **argv) |
776 | { | |
e29a8e05 | 777 | netns_nsid_socket_init(); |
d652ccbf | 778 | |
e29a8e05 AA |
779 | if (argc < 1) { |
780 | netns_map_init(); | |
0dc34c77 | 781 | return netns_list(0, NULL); |
e29a8e05 | 782 | } |
0dc34c77 | 783 | |
79928fd0 MC |
784 | if (argc > 1 && invalid_name(argv[1])) { |
785 | fprintf(stderr, "Invalid netns name \"%s\"\n", argv[1]); | |
786 | exit(-1); | |
787 | } | |
788 | ||
0dc34c77 | 789 | if ((matches(*argv, "list") == 0) || (matches(*argv, "show") == 0) || |
e29a8e05 AA |
790 | (matches(*argv, "lst") == 0)) { |
791 | netns_map_init(); | |
0dc34c77 | 792 | return netns_list(argc-1, argv+1); |
e29a8e05 | 793 | } |
0dc34c77 | 794 | |
e29a8e05 AA |
795 | if ((matches(*argv, "list-id") == 0)) { |
796 | netns_map_init(); | |
d652ccbf | 797 | return netns_list_id(argc-1, argv+1); |
e29a8e05 | 798 | } |
d652ccbf | 799 | |
0dc34c77 | 800 | if (matches(*argv, "help") == 0) |
8e2d47dc | 801 | return usage(); |
0dc34c77 EB |
802 | |
803 | if (matches(*argv, "add") == 0) | |
804 | return netns_add(argc-1, argv+1); | |
805 | ||
d182ee13 ND |
806 | if (matches(*argv, "set") == 0) |
807 | return netns_set(argc-1, argv+1); | |
808 | ||
0dc34c77 EB |
809 | if (matches(*argv, "delete") == 0) |
810 | return netns_delete(argc-1, argv+1); | |
811 | ||
9a7b3d91 EB |
812 | if (matches(*argv, "identify") == 0) |
813 | return netns_identify(argc-1, argv+1); | |
814 | ||
815 | if (matches(*argv, "pids") == 0) | |
816 | return netns_pids(argc-1, argv+1); | |
817 | ||
0dc34c77 EB |
818 | if (matches(*argv, "exec") == 0) |
819 | return netns_exec(argc-1, argv+1); | |
820 | ||
821 | if (matches(*argv, "monitor") == 0) | |
822 | return netns_monitor(argc-1, argv+1); | |
823 | ||
824 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip netns help\".\n", *argv); | |
a05f6511 | 825 | exit(-1); |
0dc34c77 | 826 | } |