Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 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> |
| 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> |
Eric W. Biederman | 9a7b3d9 | 2013-01-17 14:48:15 +0000 | [diff] [blame] | 16 | #include <ctype.h> |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 17 | |
| 18 | #include "utils.h" |
| 19 | #include "ip_common.h" |
| 20 | |
| 21 | #define NETNS_RUN_DIR "/var/run/netns" |
| 22 | #define NETNS_ETC_DIR "/etc/netns" |
| 23 | |
| 24 | #ifndef CLONE_NEWNET |
| 25 | #define CLONE_NEWNET 0x40000000 /* New network namespace (lo, device, names sockets, etc) */ |
| 26 | #endif |
| 27 | |
| 28 | #ifndef MNT_DETACH |
| 29 | #define MNT_DETACH 0x00000002 /* Just detach from the tree */ |
| 30 | #endif /* MNT_DETACH */ |
| 31 | |
Stephen Hemminger | 03fdb01 | 2013-04-17 13:33:26 -0700 | [diff] [blame] | 32 | /* sys/mount.h may be out too old to have these */ |
| 33 | #ifndef MS_REC |
| 34 | #define MS_REC 16384 |
| 35 | #endif |
| 36 | |
| 37 | #ifndef MS_SLAVE |
| 38 | #define MS_SLAVE (1 << 19) |
| 39 | #endif |
| 40 | |
| 41 | #ifndef MS_SHARED |
| 42 | #define MS_SHARED (1 << 20) |
| 43 | #endif |
| 44 | |
Eric W. Biederman | 2e8a07f | 2011-07-15 14:26:59 +0000 | [diff] [blame] | 45 | #ifndef HAVE_SETNS |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 46 | static int setns(int fd, int nstype) |
| 47 | { |
| 48 | #ifdef __NR_setns |
| 49 | return syscall(__NR_setns, fd, nstype); |
| 50 | #else |
| 51 | errno = ENOSYS; |
| 52 | return -1; |
| 53 | #endif |
| 54 | } |
Eric W. Biederman | 2e8a07f | 2011-07-15 14:26:59 +0000 | [diff] [blame] | 55 | #endif /* HAVE_SETNS */ |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 56 | |
Eric W. Biederman | 8e2d47d | 2013-01-17 14:46:09 +0000 | [diff] [blame] | 57 | static int usage(void) |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 58 | { |
| 59 | fprintf(stderr, "Usage: ip netns list\n"); |
| 60 | fprintf(stderr, " ip netns add NAME\n"); |
| 61 | fprintf(stderr, " ip netns delete NAME\n"); |
Eric W. Biederman | 9a7b3d9 | 2013-01-17 14:48:15 +0000 | [diff] [blame] | 62 | fprintf(stderr, " ip netns identify PID\n"); |
| 63 | fprintf(stderr, " ip netns pids NAME\n"); |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 64 | fprintf(stderr, " ip netns exec NAME cmd ...\n"); |
| 65 | fprintf(stderr, " ip netns monitor\n"); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 66 | exit(-1); |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | int get_netns_fd(const char *name) |
| 70 | { |
| 71 | char pathbuf[MAXPATHLEN]; |
| 72 | const char *path, *ptr; |
| 73 | |
| 74 | path = name; |
| 75 | ptr = strchr(name, '/'); |
| 76 | if (!ptr) { |
| 77 | snprintf(pathbuf, sizeof(pathbuf), "%s/%s", |
| 78 | NETNS_RUN_DIR, name ); |
| 79 | path = pathbuf; |
| 80 | } |
| 81 | return open(path, O_RDONLY); |
| 82 | } |
| 83 | |
| 84 | static int netns_list(int argc, char **argv) |
| 85 | { |
| 86 | struct dirent *entry; |
| 87 | DIR *dir; |
| 88 | |
| 89 | dir = opendir(NETNS_RUN_DIR); |
| 90 | if (!dir) |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 91 | return 0; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 92 | |
| 93 | while ((entry = readdir(dir)) != NULL) { |
| 94 | if (strcmp(entry->d_name, ".") == 0) |
| 95 | continue; |
| 96 | if (strcmp(entry->d_name, "..") == 0) |
| 97 | continue; |
| 98 | printf("%s\n", entry->d_name); |
| 99 | } |
| 100 | closedir(dir); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 101 | return 0; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static void bind_etc(const char *name) |
| 105 | { |
| 106 | char etc_netns_path[MAXPATHLEN]; |
| 107 | char netns_name[MAXPATHLEN]; |
| 108 | char etc_name[MAXPATHLEN]; |
| 109 | struct dirent *entry; |
| 110 | DIR *dir; |
| 111 | |
| 112 | snprintf(etc_netns_path, sizeof(etc_netns_path), "%s/%s", NETNS_ETC_DIR, name); |
| 113 | dir = opendir(etc_netns_path); |
| 114 | if (!dir) |
| 115 | return; |
| 116 | |
| 117 | while ((entry = readdir(dir)) != NULL) { |
| 118 | if (strcmp(entry->d_name, ".") == 0) |
| 119 | continue; |
| 120 | if (strcmp(entry->d_name, "..") == 0) |
| 121 | continue; |
| 122 | snprintf(netns_name, sizeof(netns_name), "%s/%s", etc_netns_path, entry->d_name); |
| 123 | snprintf(etc_name, sizeof(etc_name), "/etc/%s", entry->d_name); |
| 124 | if (mount(netns_name, etc_name, "none", MS_BIND, NULL) < 0) { |
| 125 | fprintf(stderr, "Bind %s -> %s failed: %s\n", |
| 126 | netns_name, etc_name, strerror(errno)); |
| 127 | } |
| 128 | } |
| 129 | closedir(dir); |
| 130 | } |
| 131 | |
| 132 | static int netns_exec(int argc, char **argv) |
| 133 | { |
| 134 | /* Setup the proper environment for apps that are not netns |
| 135 | * aware, and execute a program in that environment. |
| 136 | */ |
| 137 | const char *name, *cmd; |
| 138 | char net_path[MAXPATHLEN]; |
| 139 | int netns; |
| 140 | |
| 141 | if (argc < 1) { |
| 142 | fprintf(stderr, "No netns name specified\n"); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 143 | return -1; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 144 | } |
| 145 | if (argc < 2) { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 146 | fprintf(stderr, "No command specified\n"); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 147 | return -1; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 148 | } |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 149 | |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 150 | name = argv[0]; |
| 151 | cmd = argv[1]; |
| 152 | snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name); |
Andrey Vagin | bcb9d40 | 2013-06-04 12:01:14 +0400 | [diff] [blame] | 153 | netns = open(net_path, O_RDONLY | O_CLOEXEC); |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 154 | if (netns < 0) { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 155 | fprintf(stderr, "Cannot open network namespace \"%s\": %s\n", |
Eric W. Biederman | 4395d48 | 2013-01-17 14:46:43 +0000 | [diff] [blame] | 156 | name, strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 157 | return -1; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 158 | } |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 159 | |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 160 | if (setns(netns, CLONE_NEWNET) < 0) { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 161 | fprintf(stderr, "seting the network namespace \"%s\" failed: %s\n", |
| 162 | name, strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 163 | return -1; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | if (unshare(CLONE_NEWNS) < 0) { |
| 167 | fprintf(stderr, "unshare failed: %s\n", strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 168 | return -1; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 169 | } |
Stephen Hemminger | d259f03 | 2013-08-04 15:00:56 -0700 | [diff] [blame^] | 170 | /* Don't let any mounts propagate back to the parent */ |
Eric W. Biederman | 144e6ce | 2013-01-17 14:45:33 +0000 | [diff] [blame] | 171 | if (mount("", "/", "none", MS_SLAVE | MS_REC, NULL)) { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 172 | fprintf(stderr, "\"mount --make-rslave /\" failed: %s\n", |
Eric W. Biederman | 144e6ce | 2013-01-17 14:45:33 +0000 | [diff] [blame] | 173 | strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 174 | return -1; |
Eric W. Biederman | 144e6ce | 2013-01-17 14:45:33 +0000 | [diff] [blame] | 175 | } |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 176 | /* Mount a version of /sys that describes the network namespace */ |
| 177 | if (umount2("/sys", MNT_DETACH) < 0) { |
| 178 | fprintf(stderr, "umount of /sys failed: %s\n", strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 179 | return -1; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 180 | } |
| 181 | if (mount(name, "/sys", "sysfs", 0, NULL) < 0) { |
| 182 | fprintf(stderr, "mount of /sys failed: %s\n",strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 183 | return -1; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /* Setup bind mounts for config files in /etc */ |
| 187 | bind_etc(name); |
| 188 | |
JunweiZhang | 95592b4 | 2013-07-09 08:55:20 -0700 | [diff] [blame] | 189 | fflush(stdout); |
| 190 | |
Stephen Hemminger | a3aa47a | 2013-07-16 10:04:05 -0700 | [diff] [blame] | 191 | if (batch_mode) { |
JunweiZhang | 95592b4 | 2013-07-09 08:55:20 -0700 | [diff] [blame] | 192 | int status; |
| 193 | pid_t pid; |
| 194 | |
| 195 | pid = fork(); |
| 196 | if (pid < 0) { |
| 197 | perror("fork"); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 198 | exit(1); |
JunweiZhang | 95592b4 | 2013-07-09 08:55:20 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | if (pid != 0) { |
| 202 | /* Parent */ |
| 203 | if (waitpid(pid, &status, 0) < 0) { |
| 204 | perror("waitpid"); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 205 | exit(1); |
JunweiZhang | 95592b4 | 2013-07-09 08:55:20 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Stephen Hemminger | d259f03 | 2013-08-04 15:00:56 -0700 | [diff] [blame^] | 208 | /* If child failed, propagate status */ |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 209 | if (WIFEXITED(status)) |
| 210 | exit(WEXITSTATUS(status)); |
JunweiZhang | 95592b4 | 2013-07-09 08:55:20 -0700 | [diff] [blame] | 211 | |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 212 | return 0; |
JunweiZhang | 95592b4 | 2013-07-09 08:55:20 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 216 | if (execvp(cmd, argv + 1) < 0) |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 217 | fprintf(stderr, "exec of \"%s\" failed: %s\n", |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 218 | cmd, strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 219 | _exit(1); |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Eric W. Biederman | 9a7b3d9 | 2013-01-17 14:48:15 +0000 | [diff] [blame] | 222 | static int is_pid(const char *str) |
| 223 | { |
| 224 | int ch; |
| 225 | for (; (ch = *str); str++) { |
| 226 | if (!isdigit(ch)) |
| 227 | return 0; |
| 228 | } |
| 229 | return 1; |
| 230 | } |
| 231 | |
| 232 | static int netns_pids(int argc, char **argv) |
| 233 | { |
| 234 | const char *name; |
| 235 | char net_path[MAXPATHLEN]; |
| 236 | int netns; |
| 237 | struct stat netst; |
| 238 | DIR *dir; |
| 239 | struct dirent *entry; |
| 240 | |
| 241 | if (argc < 1) { |
| 242 | fprintf(stderr, "No netns name specified\n"); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 243 | return -1; |
Eric W. Biederman | 9a7b3d9 | 2013-01-17 14:48:15 +0000 | [diff] [blame] | 244 | } |
| 245 | if (argc > 1) { |
| 246 | fprintf(stderr, "extra arguments specified\n"); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 247 | return -1; |
Eric W. Biederman | 9a7b3d9 | 2013-01-17 14:48:15 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | name = argv[0]; |
| 251 | snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name); |
| 252 | netns = open(net_path, O_RDONLY); |
| 253 | if (netns < 0) { |
| 254 | fprintf(stderr, "Cannot open network namespace: %s\n", |
| 255 | strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 256 | return -1; |
Eric W. Biederman | 9a7b3d9 | 2013-01-17 14:48:15 +0000 | [diff] [blame] | 257 | } |
| 258 | if (fstat(netns, &netst) < 0) { |
| 259 | fprintf(stderr, "Stat of netns failed: %s\n", |
| 260 | strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 261 | return -1; |
Eric W. Biederman | 9a7b3d9 | 2013-01-17 14:48:15 +0000 | [diff] [blame] | 262 | } |
| 263 | dir = opendir("/proc/"); |
| 264 | if (!dir) { |
| 265 | fprintf(stderr, "Open of /proc failed: %s\n", |
| 266 | strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 267 | return -1; |
Eric W. Biederman | 9a7b3d9 | 2013-01-17 14:48:15 +0000 | [diff] [blame] | 268 | } |
| 269 | while((entry = readdir(dir))) { |
| 270 | char pid_net_path[MAXPATHLEN]; |
| 271 | struct stat st; |
| 272 | if (!is_pid(entry->d_name)) |
| 273 | continue; |
| 274 | snprintf(pid_net_path, sizeof(pid_net_path), "/proc/%s/ns/net", |
| 275 | entry->d_name); |
| 276 | if (stat(pid_net_path, &st) != 0) |
| 277 | continue; |
| 278 | if ((st.st_dev == netst.st_dev) && |
| 279 | (st.st_ino == netst.st_ino)) { |
| 280 | printf("%s\n", entry->d_name); |
| 281 | } |
| 282 | } |
| 283 | closedir(dir); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 284 | return 0; |
Eric W. Biederman | 9a7b3d9 | 2013-01-17 14:48:15 +0000 | [diff] [blame] | 285 | |
| 286 | } |
| 287 | |
| 288 | static int netns_identify(int argc, char **argv) |
| 289 | { |
| 290 | const char *pidstr; |
| 291 | char net_path[MAXPATHLEN]; |
| 292 | int netns; |
| 293 | struct stat netst; |
| 294 | DIR *dir; |
| 295 | struct dirent *entry; |
| 296 | |
| 297 | if (argc < 1) { |
| 298 | fprintf(stderr, "No pid specified\n"); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 299 | return -1; |
Eric W. Biederman | 9a7b3d9 | 2013-01-17 14:48:15 +0000 | [diff] [blame] | 300 | } |
| 301 | if (argc > 1) { |
| 302 | fprintf(stderr, "extra arguments specified\n"); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 303 | return -1; |
Eric W. Biederman | 9a7b3d9 | 2013-01-17 14:48:15 +0000 | [diff] [blame] | 304 | } |
| 305 | pidstr = argv[0]; |
| 306 | |
| 307 | if (!is_pid(pidstr)) { |
| 308 | fprintf(stderr, "Specified string '%s' is not a pid\n", |
| 309 | pidstr); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 310 | return -1; |
Eric W. Biederman | 9a7b3d9 | 2013-01-17 14:48:15 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | snprintf(net_path, sizeof(net_path), "/proc/%s/ns/net", pidstr); |
| 314 | netns = open(net_path, O_RDONLY); |
| 315 | if (netns < 0) { |
| 316 | fprintf(stderr, "Cannot open network namespace: %s\n", |
| 317 | strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 318 | return -1; |
Eric W. Biederman | 9a7b3d9 | 2013-01-17 14:48:15 +0000 | [diff] [blame] | 319 | } |
| 320 | if (fstat(netns, &netst) < 0) { |
| 321 | fprintf(stderr, "Stat of netns failed: %s\n", |
| 322 | strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 323 | return -1; |
Eric W. Biederman | 9a7b3d9 | 2013-01-17 14:48:15 +0000 | [diff] [blame] | 324 | } |
| 325 | dir = opendir(NETNS_RUN_DIR); |
| 326 | if (!dir) { |
| 327 | /* Succeed treat a missing directory as an empty directory */ |
| 328 | if (errno == ENOENT) |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 329 | return 0; |
Eric W. Biederman | 9a7b3d9 | 2013-01-17 14:48:15 +0000 | [diff] [blame] | 330 | |
| 331 | fprintf(stderr, "Failed to open directory %s:%s\n", |
| 332 | NETNS_RUN_DIR, strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 333 | return -1; |
Eric W. Biederman | 9a7b3d9 | 2013-01-17 14:48:15 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | while((entry = readdir(dir))) { |
| 337 | char name_path[MAXPATHLEN]; |
| 338 | struct stat st; |
| 339 | |
| 340 | if (strcmp(entry->d_name, ".") == 0) |
| 341 | continue; |
| 342 | if (strcmp(entry->d_name, "..") == 0) |
| 343 | continue; |
| 344 | |
| 345 | snprintf(name_path, sizeof(name_path), "%s/%s", NETNS_RUN_DIR, |
| 346 | entry->d_name); |
| 347 | |
| 348 | if (stat(name_path, &st) != 0) |
| 349 | continue; |
| 350 | |
| 351 | if ((st.st_dev == netst.st_dev) && |
| 352 | (st.st_ino == netst.st_ino)) { |
| 353 | printf("%s\n", entry->d_name); |
| 354 | } |
| 355 | } |
| 356 | closedir(dir); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 357 | return 0; |
Eric W. Biederman | 9a7b3d9 | 2013-01-17 14:48:15 +0000 | [diff] [blame] | 358 | |
| 359 | } |
| 360 | |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 361 | static int netns_delete(int argc, char **argv) |
| 362 | { |
| 363 | const char *name; |
| 364 | char netns_path[MAXPATHLEN]; |
| 365 | |
| 366 | if (argc < 1) { |
| 367 | fprintf(stderr, "No netns name specified\n"); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 368 | return -1; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | name = argv[0]; |
| 372 | snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name); |
| 373 | umount2(netns_path, MNT_DETACH); |
| 374 | if (unlink(netns_path) < 0) { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 375 | fprintf(stderr, "Cannot remove namespace file \"%s\": %s\n", |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 376 | netns_path, strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 377 | return -1; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 378 | } |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 379 | return 0; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static int netns_add(int argc, char **argv) |
| 383 | { |
| 384 | /* This function creates a new network namespace and |
| 385 | * a new mount namespace and bind them into a well known |
| 386 | * location in the filesystem based on the name provided. |
| 387 | * |
| 388 | * The mount namespace is created so that any necessary |
| 389 | * userspace tweaks like remounting /sys, or bind mounting |
| 390 | * a new /etc/resolv.conf can be shared between uers. |
| 391 | */ |
| 392 | char netns_path[MAXPATHLEN]; |
| 393 | const char *name; |
Eric W. Biederman | 223f4d8 | 2011-07-15 14:29:41 +0000 | [diff] [blame] | 394 | int fd; |
Eric W. Biederman | 58a3e82 | 2013-01-17 14:47:18 +0000 | [diff] [blame] | 395 | int made_netns_run_dir_mount = 0; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 396 | |
| 397 | if (argc < 1) { |
| 398 | fprintf(stderr, "No netns name specified\n"); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 399 | return -1; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 400 | } |
| 401 | name = argv[0]; |
| 402 | |
| 403 | snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name); |
| 404 | |
| 405 | /* Create the base netns directory if it doesn't exist */ |
| 406 | mkdir(NETNS_RUN_DIR, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); |
| 407 | |
Stephen Hemminger | d259f03 | 2013-08-04 15:00:56 -0700 | [diff] [blame^] | 408 | /* Make it possible for network namespace mounts to propagate between |
Eric W. Biederman | 58a3e82 | 2013-01-17 14:47:18 +0000 | [diff] [blame] | 409 | * mount namespaces. This makes it likely that a unmounting a network |
| 410 | * namespace file in one namespace will unmount the network namespace |
| 411 | * file in all namespaces allowing the network namespace to be freed |
| 412 | * sooner. |
| 413 | */ |
| 414 | while (mount("", NETNS_RUN_DIR, "none", MS_SHARED | MS_REC, NULL)) { |
| 415 | /* Fail unless we need to make the mount point */ |
| 416 | if (errno != EINVAL || made_netns_run_dir_mount) { |
| 417 | fprintf(stderr, "mount --make-shared %s failed: %s\n", |
| 418 | NETNS_RUN_DIR, strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 419 | return -1; |
Eric W. Biederman | 58a3e82 | 2013-01-17 14:47:18 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /* Upgrade NETNS_RUN_DIR to a mount point */ |
| 423 | if (mount(NETNS_RUN_DIR, NETNS_RUN_DIR, "none", MS_BIND, NULL)) { |
| 424 | fprintf(stderr, "mount --bind %s %s failed: %s\n", |
| 425 | NETNS_RUN_DIR, NETNS_RUN_DIR, strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 426 | return -1; |
Eric W. Biederman | 58a3e82 | 2013-01-17 14:47:18 +0000 | [diff] [blame] | 427 | } |
| 428 | made_netns_run_dir_mount = 1; |
| 429 | } |
| 430 | |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 431 | /* Create the filesystem state */ |
Eric W. Biederman | 223f4d8 | 2011-07-15 14:29:41 +0000 | [diff] [blame] | 432 | fd = open(netns_path, O_RDONLY|O_CREAT|O_EXCL, 0); |
| 433 | if (fd < 0) { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 434 | fprintf(stderr, "Cannot not create namespace file \"%s\": %s\n", |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 435 | netns_path, strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 436 | return -1; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 437 | } |
Eric W. Biederman | 223f4d8 | 2011-07-15 14:29:41 +0000 | [diff] [blame] | 438 | close(fd); |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 439 | if (unshare(CLONE_NEWNET) < 0) { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 440 | fprintf(stderr, "Failed to create a new network namespace \"%s\": %s\n", |
| 441 | name, strerror(errno)); |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 442 | goto out_delete; |
| 443 | } |
| 444 | |
| 445 | /* Bind the netns last so I can watch for it */ |
| 446 | if (mount("/proc/self/ns/net", netns_path, "none", MS_BIND, NULL) < 0) { |
| 447 | fprintf(stderr, "Bind /proc/self/ns/net -> %s failed: %s\n", |
| 448 | netns_path, strerror(errno)); |
| 449 | goto out_delete; |
| 450 | } |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 451 | return 0; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 452 | out_delete: |
| 453 | netns_delete(argc, argv); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 454 | return -1; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | |
| 458 | static int netns_monitor(int argc, char **argv) |
| 459 | { |
| 460 | char buf[4096]; |
| 461 | struct inotify_event *event; |
| 462 | int fd; |
| 463 | fd = inotify_init(); |
| 464 | if (fd < 0) { |
| 465 | fprintf(stderr, "inotify_init failed: %s\n", |
| 466 | strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 467 | return -1; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 468 | } |
| 469 | if (inotify_add_watch(fd, NETNS_RUN_DIR, IN_CREATE | IN_DELETE) < 0) { |
| 470 | fprintf(stderr, "inotify_add_watch failed: %s\n", |
| 471 | strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 472 | return -1; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 473 | } |
| 474 | for(;;) { |
| 475 | ssize_t len = read(fd, buf, sizeof(buf)); |
| 476 | if (len < 0) { |
| 477 | fprintf(stderr, "read failed: %s\n", |
| 478 | strerror(errno)); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 479 | return -1; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 480 | } |
| 481 | for (event = (struct inotify_event *)buf; |
| 482 | (char *)event < &buf[len]; |
| 483 | event = (struct inotify_event *)((char *)event + sizeof(*event) + event->len)) { |
| 484 | if (event->mask & IN_CREATE) |
| 485 | printf("add %s\n", event->name); |
| 486 | if (event->mask & IN_DELETE) |
| 487 | printf("delete %s\n", event->name); |
| 488 | } |
| 489 | } |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 490 | return 0; |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | int do_netns(int argc, char **argv) |
| 494 | { |
| 495 | if (argc < 1) |
| 496 | return netns_list(0, NULL); |
| 497 | |
| 498 | if ((matches(*argv, "list") == 0) || (matches(*argv, "show") == 0) || |
| 499 | (matches(*argv, "lst") == 0)) |
| 500 | return netns_list(argc-1, argv+1); |
| 501 | |
| 502 | if (matches(*argv, "help") == 0) |
Eric W. Biederman | 8e2d47d | 2013-01-17 14:46:09 +0000 | [diff] [blame] | 503 | return usage(); |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 504 | |
| 505 | if (matches(*argv, "add") == 0) |
| 506 | return netns_add(argc-1, argv+1); |
| 507 | |
| 508 | if (matches(*argv, "delete") == 0) |
| 509 | return netns_delete(argc-1, argv+1); |
| 510 | |
Eric W. Biederman | 9a7b3d9 | 2013-01-17 14:48:15 +0000 | [diff] [blame] | 511 | if (matches(*argv, "identify") == 0) |
| 512 | return netns_identify(argc-1, argv+1); |
| 513 | |
| 514 | if (matches(*argv, "pids") == 0) |
| 515 | return netns_pids(argc-1, argv+1); |
| 516 | |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 517 | if (matches(*argv, "exec") == 0) |
| 518 | return netns_exec(argc-1, argv+1); |
| 519 | |
| 520 | if (matches(*argv, "monitor") == 0) |
| 521 | return netns_monitor(argc-1, argv+1); |
| 522 | |
| 523 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip netns help\".\n", *argv); |
Stephen Hemminger | a05f651 | 2013-07-12 08:43:23 -0700 | [diff] [blame] | 524 | exit(-1); |
Eric W. Biederman | 0dc34c7 | 2011-07-13 09:48:26 -0700 | [diff] [blame] | 525 | } |