]> git.proxmox.com Git - mirror_iproute2.git/blob - lib/namespace.c
ip: add support for seg6local End.BPF action
[mirror_iproute2.git] / lib / namespace.c
1 /*
2 * namespace.c
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9
10 #include <sys/statvfs.h>
11 #include <fcntl.h>
12 #include <dirent.h>
13 #include <limits.h>
14
15 #include "utils.h"
16 #include "namespace.h"
17
18 static void bind_etc(const char *name)
19 {
20 char etc_netns_path[sizeof(NETNS_ETC_DIR) + NAME_MAX];
21 char netns_name[PATH_MAX];
22 char etc_name[PATH_MAX];
23 struct dirent *entry;
24 DIR *dir;
25
26 if (strlen(name) >= NAME_MAX)
27 return;
28
29 snprintf(etc_netns_path, sizeof(etc_netns_path), "%s/%s", NETNS_ETC_DIR, name);
30 dir = opendir(etc_netns_path);
31 if (!dir)
32 return;
33
34 while ((entry = readdir(dir)) != NULL) {
35 if (strcmp(entry->d_name, ".") == 0)
36 continue;
37 if (strcmp(entry->d_name, "..") == 0)
38 continue;
39 snprintf(netns_name, sizeof(netns_name), "%s/%s", etc_netns_path, entry->d_name);
40 snprintf(etc_name, sizeof(etc_name), "/etc/%s", entry->d_name);
41 if (mount(netns_name, etc_name, "none", MS_BIND, NULL) < 0) {
42 fprintf(stderr, "Bind %s -> %s failed: %s\n",
43 netns_name, etc_name, strerror(errno));
44 }
45 }
46 closedir(dir);
47 }
48
49 int netns_switch(char *name)
50 {
51 char net_path[PATH_MAX];
52 int netns;
53 unsigned long mountflags = 0;
54 struct statvfs fsstat;
55
56 snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
57 netns = open(net_path, O_RDONLY | O_CLOEXEC);
58 if (netns < 0) {
59 fprintf(stderr, "Cannot open network namespace \"%s\": %s\n",
60 name, strerror(errno));
61 return -1;
62 }
63
64 if (setns(netns, CLONE_NEWNET) < 0) {
65 fprintf(stderr, "setting the network namespace \"%s\" failed: %s\n",
66 name, strerror(errno));
67 close(netns);
68 return -1;
69 }
70 close(netns);
71
72 if (unshare(CLONE_NEWNS) < 0) {
73 fprintf(stderr, "unshare failed: %s\n", strerror(errno));
74 return -1;
75 }
76 /* Don't let any mounts propagate back to the parent */
77 if (mount("", "/", "none", MS_SLAVE | MS_REC, NULL)) {
78 fprintf(stderr, "\"mount --make-rslave /\" failed: %s\n",
79 strerror(errno));
80 return -1;
81 }
82
83 /* Mount a version of /sys that describes the network namespace */
84
85 if (statvfs("/sys", &fsstat) < 0) {
86 fprintf(stderr, "could not stat /sys (not mounted?): %s\n",strerror(errno));
87 return -1;
88 }
89 if (fsstat.f_flag & ST_RDONLY) {
90 /* If /sys is not writable (e.g. in a container), we can't
91 * unmount the old /sys instance, but we can still mount a new
92 * read-only instance over it. */
93 mountflags = MS_RDONLY;
94 } else {
95 if (umount2("/sys", MNT_DETACH) < 0) {
96 fprintf(stderr, "umount of /sys failed: %s\n", strerror(errno));
97 return -1;
98 }
99 }
100 if (mount(name, "/sys", "sysfs", mountflags, NULL) < 0) {
101 fprintf(stderr, "mount of /sys failed: %s\n",strerror(errno));
102 return -1;
103 }
104
105 /* Setup bind mounts for config files in /etc */
106 bind_etc(name);
107 return 0;
108 }
109
110 int netns_get_fd(const char *name)
111 {
112 char pathbuf[PATH_MAX];
113 const char *path, *ptr;
114
115 path = name;
116 ptr = strchr(name, '/');
117 if (!ptr) {
118 snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
119 NETNS_RUN_DIR, name );
120 path = pathbuf;
121 }
122 return open(path, O_RDONLY);
123 }
124
125 int netns_foreach(int (*func)(char *nsname, void *arg), void *arg)
126 {
127 DIR *dir;
128 struct dirent *entry;
129
130 dir = opendir(NETNS_RUN_DIR);
131 if (!dir)
132 return -1;
133
134 while ((entry = readdir(dir)) != NULL) {
135 if (strcmp(entry->d_name, ".") == 0)
136 continue;
137 if (strcmp(entry->d_name, "..") == 0)
138 continue;
139 if (func(entry->d_name, arg))
140 break;
141 }
142
143 closedir(dir);
144 return 0;
145 }