blob: 33765b588a1a3c7fe5baaeb7e2fa222c48d96d49 [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>
16
17#include "utils.h"
18#include "ip_common.h"
19
20#define NETNS_RUN_DIR "/var/run/netns"
21#define NETNS_ETC_DIR "/etc/netns"
22
23#ifndef CLONE_NEWNET
24#define CLONE_NEWNET 0x40000000 /* New network namespace (lo, device, names sockets, etc) */
25#endif
26
27#ifndef MNT_DETACH
28#define MNT_DETACH 0x00000002 /* Just detach from the tree */
29#endif /* MNT_DETACH */
30
Eric W. Biederman2e8a07f2011-07-15 14:26:59 +000031#ifndef HAVE_SETNS
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070032static int setns(int fd, int nstype)
33{
34#ifdef __NR_setns
35 return syscall(__NR_setns, fd, nstype);
36#else
37 errno = ENOSYS;
38 return -1;
39#endif
40}
Eric W. Biederman2e8a07f2011-07-15 14:26:59 +000041#endif /* HAVE_SETNS */
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070042
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +000043static int usage(void)
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070044{
45 fprintf(stderr, "Usage: ip netns list\n");
46 fprintf(stderr, " ip netns add NAME\n");
47 fprintf(stderr, " ip netns delete NAME\n");
48 fprintf(stderr, " ip netns exec NAME cmd ...\n");
49 fprintf(stderr, " ip netns monitor\n");
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +000050 return EXIT_FAILURE;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070051}
52
53int get_netns_fd(const char *name)
54{
55 char pathbuf[MAXPATHLEN];
56 const char *path, *ptr;
57
58 path = name;
59 ptr = strchr(name, '/');
60 if (!ptr) {
61 snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
62 NETNS_RUN_DIR, name );
63 path = pathbuf;
64 }
65 return open(path, O_RDONLY);
66}
67
68static int netns_list(int argc, char **argv)
69{
70 struct dirent *entry;
71 DIR *dir;
72
73 dir = opendir(NETNS_RUN_DIR);
74 if (!dir)
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +000075 return EXIT_SUCCESS;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070076
77 while ((entry = readdir(dir)) != NULL) {
78 if (strcmp(entry->d_name, ".") == 0)
79 continue;
80 if (strcmp(entry->d_name, "..") == 0)
81 continue;
82 printf("%s\n", entry->d_name);
83 }
84 closedir(dir);
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +000085 return EXIT_SUCCESS;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070086}
87
88static void bind_etc(const char *name)
89{
90 char etc_netns_path[MAXPATHLEN];
91 char netns_name[MAXPATHLEN];
92 char etc_name[MAXPATHLEN];
93 struct dirent *entry;
94 DIR *dir;
95
96 snprintf(etc_netns_path, sizeof(etc_netns_path), "%s/%s", NETNS_ETC_DIR, name);
97 dir = opendir(etc_netns_path);
98 if (!dir)
99 return;
100
101 while ((entry = readdir(dir)) != NULL) {
102 if (strcmp(entry->d_name, ".") == 0)
103 continue;
104 if (strcmp(entry->d_name, "..") == 0)
105 continue;
106 snprintf(netns_name, sizeof(netns_name), "%s/%s", etc_netns_path, entry->d_name);
107 snprintf(etc_name, sizeof(etc_name), "/etc/%s", entry->d_name);
108 if (mount(netns_name, etc_name, "none", MS_BIND, NULL) < 0) {
109 fprintf(stderr, "Bind %s -> %s failed: %s\n",
110 netns_name, etc_name, strerror(errno));
111 }
112 }
113 closedir(dir);
114}
115
116static int netns_exec(int argc, char **argv)
117{
118 /* Setup the proper environment for apps that are not netns
119 * aware, and execute a program in that environment.
120 */
121 const char *name, *cmd;
122 char net_path[MAXPATHLEN];
123 int netns;
124
125 if (argc < 1) {
126 fprintf(stderr, "No netns name specified\n");
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000127 return EXIT_FAILURE;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700128 }
129 if (argc < 2) {
130 fprintf(stderr, "No cmd specified\n");
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000131 return EXIT_FAILURE;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700132 }
133 name = argv[0];
134 cmd = argv[1];
135 snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
136 netns = open(net_path, O_RDONLY);
137 if (netns < 0) {
Eric W. Biederman4395d482013-01-17 14:46:43 +0000138 fprintf(stderr, "Cannot open network namespace %s: %s\n",
139 name, strerror(errno));
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000140 return EXIT_FAILURE;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700141 }
142 if (setns(netns, CLONE_NEWNET) < 0) {
143 fprintf(stderr, "seting the network namespace failed: %s\n",
144 strerror(errno));
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000145 return EXIT_FAILURE;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700146 }
147
148 if (unshare(CLONE_NEWNS) < 0) {
149 fprintf(stderr, "unshare failed: %s\n", strerror(errno));
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000150 return EXIT_FAILURE;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700151 }
Eric W. Biederman144e6ce2013-01-17 14:45:33 +0000152 /* Don't let any mounts propogate back to the parent */
153 if (mount("", "/", "none", MS_SLAVE | MS_REC, NULL)) {
154 fprintf(stderr, "mount --make-rslave / failed: %s\n",
155 strerror(errno));
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000156 return EXIT_FAILURE;
Eric W. Biederman144e6ce2013-01-17 14:45:33 +0000157 }
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700158 /* Mount a version of /sys that describes the network namespace */
159 if (umount2("/sys", MNT_DETACH) < 0) {
160 fprintf(stderr, "umount of /sys failed: %s\n", strerror(errno));
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000161 return EXIT_FAILURE;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700162 }
163 if (mount(name, "/sys", "sysfs", 0, NULL) < 0) {
164 fprintf(stderr, "mount of /sys failed: %s\n",strerror(errno));
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000165 return EXIT_FAILURE;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700166 }
167
168 /* Setup bind mounts for config files in /etc */
169 bind_etc(name);
170
171 if (execvp(cmd, argv + 1) < 0)
172 fprintf(stderr, "exec of %s failed: %s\n",
173 cmd, strerror(errno));
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000174 return EXIT_FAILURE;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700175}
176
177static int netns_delete(int argc, char **argv)
178{
179 const char *name;
180 char netns_path[MAXPATHLEN];
181
182 if (argc < 1) {
183 fprintf(stderr, "No netns name specified\n");
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000184 return EXIT_FAILURE;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700185 }
186
187 name = argv[0];
188 snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name);
189 umount2(netns_path, MNT_DETACH);
190 if (unlink(netns_path) < 0) {
191 fprintf(stderr, "Cannot remove %s: %s\n",
192 netns_path, strerror(errno));
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000193 return EXIT_FAILURE;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700194 }
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000195 return EXIT_SUCCESS;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700196}
197
198static int netns_add(int argc, char **argv)
199{
200 /* This function creates a new network namespace and
201 * a new mount namespace and bind them into a well known
202 * location in the filesystem based on the name provided.
203 *
204 * The mount namespace is created so that any necessary
205 * userspace tweaks like remounting /sys, or bind mounting
206 * a new /etc/resolv.conf can be shared between uers.
207 */
208 char netns_path[MAXPATHLEN];
209 const char *name;
Eric W. Biederman223f4d82011-07-15 14:29:41 +0000210 int fd;
Eric W. Biederman58a3e822013-01-17 14:47:18 +0000211 int made_netns_run_dir_mount = 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700212
213 if (argc < 1) {
214 fprintf(stderr, "No netns name specified\n");
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000215 return EXIT_FAILURE;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700216 }
217 name = argv[0];
218
219 snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name);
220
221 /* Create the base netns directory if it doesn't exist */
222 mkdir(NETNS_RUN_DIR, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
223
Eric W. Biederman58a3e822013-01-17 14:47:18 +0000224 /* Make it possible for network namespace mounts to propogate between
225 * mount namespaces. This makes it likely that a unmounting a network
226 * namespace file in one namespace will unmount the network namespace
227 * file in all namespaces allowing the network namespace to be freed
228 * sooner.
229 */
230 while (mount("", NETNS_RUN_DIR, "none", MS_SHARED | MS_REC, NULL)) {
231 /* Fail unless we need to make the mount point */
232 if (errno != EINVAL || made_netns_run_dir_mount) {
233 fprintf(stderr, "mount --make-shared %s failed: %s\n",
234 NETNS_RUN_DIR, strerror(errno));
235 return EXIT_FAILURE;
236 }
237
238 /* Upgrade NETNS_RUN_DIR to a mount point */
239 if (mount(NETNS_RUN_DIR, NETNS_RUN_DIR, "none", MS_BIND, NULL)) {
240 fprintf(stderr, "mount --bind %s %s failed: %s\n",
241 NETNS_RUN_DIR, NETNS_RUN_DIR, strerror(errno));
242 return EXIT_FAILURE;
243 }
244 made_netns_run_dir_mount = 1;
245 }
246
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700247 /* Create the filesystem state */
Eric W. Biederman223f4d82011-07-15 14:29:41 +0000248 fd = open(netns_path, O_RDONLY|O_CREAT|O_EXCL, 0);
249 if (fd < 0) {
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700250 fprintf(stderr, "Could not create %s: %s\n",
251 netns_path, strerror(errno));
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000252 return EXIT_FAILURE;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700253 }
Eric W. Biederman223f4d82011-07-15 14:29:41 +0000254 close(fd);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700255 if (unshare(CLONE_NEWNET) < 0) {
256 fprintf(stderr, "Failed to create a new network namespace: %s\n",
257 strerror(errno));
258 goto out_delete;
259 }
260
261 /* Bind the netns last so I can watch for it */
262 if (mount("/proc/self/ns/net", netns_path, "none", MS_BIND, NULL) < 0) {
263 fprintf(stderr, "Bind /proc/self/ns/net -> %s failed: %s\n",
264 netns_path, strerror(errno));
265 goto out_delete;
266 }
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000267 return EXIT_SUCCESS;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700268out_delete:
269 netns_delete(argc, argv);
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000270 return EXIT_FAILURE;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700271}
272
273
274static int netns_monitor(int argc, char **argv)
275{
276 char buf[4096];
277 struct inotify_event *event;
278 int fd;
279 fd = inotify_init();
280 if (fd < 0) {
281 fprintf(stderr, "inotify_init failed: %s\n",
282 strerror(errno));
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000283 return EXIT_FAILURE;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700284 }
285 if (inotify_add_watch(fd, NETNS_RUN_DIR, IN_CREATE | IN_DELETE) < 0) {
286 fprintf(stderr, "inotify_add_watch failed: %s\n",
287 strerror(errno));
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000288 return EXIT_FAILURE;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700289 }
290 for(;;) {
291 ssize_t len = read(fd, buf, sizeof(buf));
292 if (len < 0) {
293 fprintf(stderr, "read failed: %s\n",
294 strerror(errno));
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000295 return EXIT_FAILURE;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700296 }
297 for (event = (struct inotify_event *)buf;
298 (char *)event < &buf[len];
299 event = (struct inotify_event *)((char *)event + sizeof(*event) + event->len)) {
300 if (event->mask & IN_CREATE)
301 printf("add %s\n", event->name);
302 if (event->mask & IN_DELETE)
303 printf("delete %s\n", event->name);
304 }
305 }
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000306 return EXIT_SUCCESS;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700307}
308
309int do_netns(int argc, char **argv)
310{
311 if (argc < 1)
312 return netns_list(0, NULL);
313
314 if ((matches(*argv, "list") == 0) || (matches(*argv, "show") == 0) ||
315 (matches(*argv, "lst") == 0))
316 return netns_list(argc-1, argv+1);
317
318 if (matches(*argv, "help") == 0)
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000319 return usage();
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700320
321 if (matches(*argv, "add") == 0)
322 return netns_add(argc-1, argv+1);
323
324 if (matches(*argv, "delete") == 0)
325 return netns_delete(argc-1, argv+1);
326
327 if (matches(*argv, "exec") == 0)
328 return netns_exec(argc-1, argv+1);
329
330 if (matches(*argv, "monitor") == 0)
331 return netns_monitor(argc-1, argv+1);
332
333 fprintf(stderr, "Command \"%s\" is unknown, try \"ip netns help\".\n", *argv);
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000334 return EXIT_FAILURE;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700335}