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