blob: db7007cfde3a0a260814b8d42f6fa51da4185d78 [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
31static int setns(int fd, int nstype)
32{
33#ifdef __NR_setns
34 return syscall(__NR_setns, fd, nstype);
35#else
36 errno = ENOSYS;
37 return -1;
38#endif
39}
40
41
42static int touch(const char *path, mode_t mode)
43{
44 int fd;
45 fd = open(path, O_RDONLY|O_CREAT, mode);
46 if (fd < 0)
47 return -1;
48 close(fd);
49 return 0;
50}
51
52static void usage(void) __attribute__((noreturn));
53
54static void usage(void)
55{
56 fprintf(stderr, "Usage: ip netns list\n");
57 fprintf(stderr, " ip netns add NAME\n");
58 fprintf(stderr, " ip netns delete NAME\n");
59 fprintf(stderr, " ip netns exec NAME cmd ...\n");
60 fprintf(stderr, " ip netns monitor\n");
61 exit(-1);
62}
63
64int get_netns_fd(const char *name)
65{
66 char pathbuf[MAXPATHLEN];
67 const char *path, *ptr;
68
69 path = name;
70 ptr = strchr(name, '/');
71 if (!ptr) {
72 snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
73 NETNS_RUN_DIR, name );
74 path = pathbuf;
75 }
76 return open(path, O_RDONLY);
77}
78
79static int netns_list(int argc, char **argv)
80{
81 struct dirent *entry;
82 DIR *dir;
83
84 dir = opendir(NETNS_RUN_DIR);
85 if (!dir)
86 return 0;
87
88 while ((entry = readdir(dir)) != NULL) {
89 if (strcmp(entry->d_name, ".") == 0)
90 continue;
91 if (strcmp(entry->d_name, "..") == 0)
92 continue;
93 printf("%s\n", entry->d_name);
94 }
95 closedir(dir);
96 return 0;
97}
98
99static void bind_etc(const char *name)
100{
101 char etc_netns_path[MAXPATHLEN];
102 char netns_name[MAXPATHLEN];
103 char etc_name[MAXPATHLEN];
104 struct dirent *entry;
105 DIR *dir;
106
107 snprintf(etc_netns_path, sizeof(etc_netns_path), "%s/%s", NETNS_ETC_DIR, name);
108 dir = opendir(etc_netns_path);
109 if (!dir)
110 return;
111
112 while ((entry = readdir(dir)) != NULL) {
113 if (strcmp(entry->d_name, ".") == 0)
114 continue;
115 if (strcmp(entry->d_name, "..") == 0)
116 continue;
117 snprintf(netns_name, sizeof(netns_name), "%s/%s", etc_netns_path, entry->d_name);
118 snprintf(etc_name, sizeof(etc_name), "/etc/%s", entry->d_name);
119 if (mount(netns_name, etc_name, "none", MS_BIND, NULL) < 0) {
120 fprintf(stderr, "Bind %s -> %s failed: %s\n",
121 netns_name, etc_name, strerror(errno));
122 }
123 }
124 closedir(dir);
125}
126
127static int netns_exec(int argc, char **argv)
128{
129 /* Setup the proper environment for apps that are not netns
130 * aware, and execute a program in that environment.
131 */
132 const char *name, *cmd;
133 char net_path[MAXPATHLEN];
134 int netns;
135
136 if (argc < 1) {
137 fprintf(stderr, "No netns name specified\n");
138 return -1;
139 }
140 if (argc < 2) {
141 fprintf(stderr, "No cmd specified\n");
142 return -1;
143 }
144 name = argv[0];
145 cmd = argv[1];
146 snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
147 netns = open(net_path, O_RDONLY);
148 if (netns < 0) {
149 fprintf(stderr, "Cannot open network namespace: %s\n",
150 strerror(errno));
151 return -1;
152 }
153 if (setns(netns, CLONE_NEWNET) < 0) {
154 fprintf(stderr, "seting the network namespace failed: %s\n",
155 strerror(errno));
156 return -1;
157 }
158
159 if (unshare(CLONE_NEWNS) < 0) {
160 fprintf(stderr, "unshare failed: %s\n", strerror(errno));
161 return -1;
162 }
163 /* Mount a version of /sys that describes the network namespace */
164 if (umount2("/sys", MNT_DETACH) < 0) {
165 fprintf(stderr, "umount of /sys failed: %s\n", strerror(errno));
166 return -1;
167 }
168 if (mount(name, "/sys", "sysfs", 0, NULL) < 0) {
169 fprintf(stderr, "mount of /sys failed: %s\n",strerror(errno));
170 return -1;
171 }
172
173 /* Setup bind mounts for config files in /etc */
174 bind_etc(name);
175
176 if (execvp(cmd, argv + 1) < 0)
177 fprintf(stderr, "exec of %s failed: %s\n",
178 cmd, strerror(errno));
179 exit(-1);
180}
181
182static int netns_delete(int argc, char **argv)
183{
184 const char *name;
185 char netns_path[MAXPATHLEN];
186
187 if (argc < 1) {
188 fprintf(stderr, "No netns name specified\n");
189 return -1;
190 }
191
192 name = argv[0];
193 snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name);
194 umount2(netns_path, MNT_DETACH);
195 if (unlink(netns_path) < 0) {
196 fprintf(stderr, "Cannot remove %s: %s\n",
197 netns_path, strerror(errno));
198 return -1;
199 }
200 return 0;
201}
202
203static int netns_add(int argc, char **argv)
204{
205 /* This function creates a new network namespace and
206 * a new mount namespace and bind them into a well known
207 * location in the filesystem based on the name provided.
208 *
209 * The mount namespace is created so that any necessary
210 * userspace tweaks like remounting /sys, or bind mounting
211 * a new /etc/resolv.conf can be shared between uers.
212 */
213 char netns_path[MAXPATHLEN];
214 const char *name;
215
216 if (argc < 1) {
217 fprintf(stderr, "No netns name specified\n");
218 return -1;
219 }
220 name = argv[0];
221
222 snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name);
223
224 /* Create the base netns directory if it doesn't exist */
225 mkdir(NETNS_RUN_DIR, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
226
227 /* Create the filesystem state */
228 if (touch(netns_path, 0) < 0) {
229 fprintf(stderr, "Could not create %s: %s\n",
230 netns_path, strerror(errno));
231 goto out_delete;
232 }
233 if (unshare(CLONE_NEWNET) < 0) {
234 fprintf(stderr, "Failed to create a new network namespace: %s\n",
235 strerror(errno));
236 goto out_delete;
237 }
238
239 /* Bind the netns last so I can watch for it */
240 if (mount("/proc/self/ns/net", netns_path, "none", MS_BIND, NULL) < 0) {
241 fprintf(stderr, "Bind /proc/self/ns/net -> %s failed: %s\n",
242 netns_path, strerror(errno));
243 goto out_delete;
244 }
245 return 0;
246out_delete:
247 netns_delete(argc, argv);
248 exit(-1);
249 return -1;
250}
251
252
253static int netns_monitor(int argc, char **argv)
254{
255 char buf[4096];
256 struct inotify_event *event;
257 int fd;
258 fd = inotify_init();
259 if (fd < 0) {
260 fprintf(stderr, "inotify_init failed: %s\n",
261 strerror(errno));
262 return -1;
263 }
264 if (inotify_add_watch(fd, NETNS_RUN_DIR, IN_CREATE | IN_DELETE) < 0) {
265 fprintf(stderr, "inotify_add_watch failed: %s\n",
266 strerror(errno));
267 return -1;
268 }
269 for(;;) {
270 ssize_t len = read(fd, buf, sizeof(buf));
271 if (len < 0) {
272 fprintf(stderr, "read failed: %s\n",
273 strerror(errno));
274 return -1;
275 }
276 for (event = (struct inotify_event *)buf;
277 (char *)event < &buf[len];
278 event = (struct inotify_event *)((char *)event + sizeof(*event) + event->len)) {
279 if (event->mask & IN_CREATE)
280 printf("add %s\n", event->name);
281 if (event->mask & IN_DELETE)
282 printf("delete %s\n", event->name);
283 }
284 }
285 return 0;
286}
287
288int do_netns(int argc, char **argv)
289{
290 if (argc < 1)
291 return netns_list(0, NULL);
292
293 if ((matches(*argv, "list") == 0) || (matches(*argv, "show") == 0) ||
294 (matches(*argv, "lst") == 0))
295 return netns_list(argc-1, argv+1);
296
297 if (matches(*argv, "help") == 0)
298 usage();
299
300 if (matches(*argv, "add") == 0)
301 return netns_add(argc-1, argv+1);
302
303 if (matches(*argv, "delete") == 0)
304 return netns_delete(argc-1, argv+1);
305
306 if (matches(*argv, "exec") == 0)
307 return netns_exec(argc-1, argv+1);
308
309 if (matches(*argv, "monitor") == 0)
310 return netns_monitor(argc-1, argv+1);
311
312 fprintf(stderr, "Command \"%s\" is unknown, try \"ip netns help\".\n", *argv);
313 exit(-1);
314}