| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* namespace.c |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <sys/statvfs.h> |
| 7 |
#include <fcntl.h> |
| 8 |
#include <dirent.h> |
| 9 |
#include <limits.h> |
| 10 |
#include <linux/net_namespace.h> |
| 11 |
|
| 12 |
#include "utils.h" |
| 13 |
#include "namespace.h" |
| 14 |
#include "libnetlink.h" |
| 15 |
|
| 16 |
static void bind_etc(const char *name) |
| 17 |
{ |
| 18 |
char etc_netns_path[sizeof(NETNS_ETC_DIR) + NAME_MAX]; |
| 19 |
char netns_name[PATH_MAX]; |
| 20 |
char etc_name[PATH_MAX]; |
| 21 |
struct dirent *entry; |
| 22 |
DIR *dir; |
| 23 |
|
| 24 |
if (strlen(name) >= NAME_MAX) |
| 25 |
return; |
| 26 |
|
| 27 |
snprintf(etc_netns_path, sizeof(etc_netns_path), "%s/%s", NETNS_ETC_DIR, name); |
| 28 |
dir = opendir(etc_netns_path); |
| 29 |
if (!dir) |
| 30 |
return; |
| 31 |
|
| 32 |
while ((entry = readdir(dir)) != NULL) { |
| 33 |
if (strcmp(entry->d_name, ".") == 0) |
| 34 |
continue; |
| 35 |
if (strcmp(entry->d_name, "..") == 0) |
| 36 |
continue; |
| 37 |
snprintf(netns_name, sizeof(netns_name), "%s/%s", etc_netns_path, entry->d_name); |
| 38 |
snprintf(etc_name, sizeof(etc_name), "/etc/%s", entry->d_name); |
| 39 |
if (mount(netns_name, etc_name, "none", MS_BIND, NULL) < 0) { |
| 40 |
fprintf(stderr, "Bind %s -> %s failed: %s\n", |
| 41 |
netns_name, etc_name, strerror(errno)); |
| 42 |
} |
| 43 |
} |
| 44 |
closedir(dir); |
| 45 |
} |
| 46 |
|
| 47 |
int netns_switch(char *name) |
| 48 |
{ |
| 49 |
char net_path[PATH_MAX]; |
| 50 |
int netns; |
| 51 |
unsigned long mountflags = 0; |
| 52 |
struct statvfs fsstat; |
| 53 |
|
| 54 |
snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name); |
| 55 |
netns = open(net_path, O_RDONLY | O_CLOEXEC); |
| 56 |
if (netns < 0) { |
| 57 |
fprintf(stderr, "Cannot open network namespace \"%s\": %s\n", |
| 58 |
name, strerror(errno)); |
| 59 |
return -1; |
| 60 |
} |
| 61 |
|
| 62 |
if (setns(netns, CLONE_NEWNET) < 0) { |
| 63 |
fprintf(stderr, "setting the network namespace \"%s\" failed: %s\n", |
| 64 |
name, strerror(errno)); |
| 65 |
close(netns); |
| 66 |
return -1; |
| 67 |
} |
| 68 |
close(netns); |
| 69 |
|
| 70 |
if (unshare(CLONE_NEWNS) < 0) { |
| 71 |
fprintf(stderr, "unshare failed: %s\n", strerror(errno)); |
| 72 |
return -1; |
| 73 |
} |
| 74 |
/* Don't let any mounts propagate back to the parent */ |
| 75 |
if (mount("", "/", "none", MS_SLAVE | MS_REC, NULL)) { |
| 76 |
fprintf(stderr, "\"mount --make-rslave /\" failed: %s\n", |
| 77 |
strerror(errno)); |
| 78 |
return -1; |
| 79 |
} |
| 80 |
|
| 81 |
/* Mount a version of /sys that describes the network namespace */ |
| 82 |
|
| 83 |
if (umount2("/sys", MNT_DETACH) < 0) { |
| 84 |
/* If this fails, perhaps there wasn't a sysfs instance mounted. Good. */ |
| 85 |
if (statvfs("/sys", &fsstat) == 0) { |
| 86 |
/* We couldn't umount the sysfs, we'll attempt to overlay it. |
| 87 |
* A read-only instance can't be shadowed with a read-write one. */ |
| 88 |
if (fsstat.f_flag & ST_RDONLY) |
| 89 |
mountflags = MS_RDONLY; |
| 90 |
} |
| 91 |
} |
| 92 |
if (mount(name, "/sys", "sysfs", mountflags, NULL) < 0) { |
| 93 |
fprintf(stderr, "mount of /sys failed: %s\n",strerror(errno)); |
| 94 |
return -1; |
| 95 |
} |
| 96 |
|
| 97 |
/* Setup bind mounts for config files in /etc */ |
| 98 |
bind_etc(name); |
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
int netns_get_fd(const char *name) |
| 103 |
{ |
| 104 |
char pathbuf[PATH_MAX]; |
| 105 |
const char *path, *ptr; |
| 106 |
|
| 107 |
path = name; |
| 108 |
ptr = strchr(name, '/'); |
| 109 |
if (!ptr) { |
| 110 |
snprintf(pathbuf, sizeof(pathbuf), "%s/%s", |
| 111 |
NETNS_RUN_DIR, name ); |
| 112 |
path = pathbuf; |
| 113 |
} |
| 114 |
return open(path, O_RDONLY); |
| 115 |
} |
| 116 |
|
| 117 |
int netns_foreach(int (*func)(char *nsname, void *arg), void *arg) |
| 118 |
{ |
| 119 |
DIR *dir; |
| 120 |
struct dirent *entry; |
| 121 |
|
| 122 |
dir = opendir(NETNS_RUN_DIR); |
| 123 |
if (!dir) { |
| 124 |
if (errno == ENOENT) |
| 125 |
return 0; |
| 126 |
|
| 127 |
fprintf(stderr, "Failed to open directory %s: %s\n", |
| 128 |
NETNS_RUN_DIR, strerror(errno)); |
| 129 |
return -1; |
| 130 |
} |
| 131 |
|
| 132 |
while ((entry = readdir(dir)) != NULL) { |
| 133 |
if (strcmp(entry->d_name, ".") == 0) |
| 134 |
continue; |
| 135 |
if (strcmp(entry->d_name, "..") == 0) |
| 136 |
continue; |
| 137 |
if (func(entry->d_name, arg)) |
| 138 |
break; |
| 139 |
} |
| 140 |
|
| 141 |
closedir(dir); |
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
int netns_id_from_name(struct rtnl_handle *rtnl, const char *name) |
| 146 |
{ |
| 147 |
struct { |
| 148 |
struct nlmsghdr n; |
| 149 |
struct rtgenmsg g; |
| 150 |
char buf[1024]; |
| 151 |
} req = { |
| 152 |
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)), |
| 153 |
.n.nlmsg_flags = NLM_F_REQUEST, |
| 154 |
.n.nlmsg_type = RTM_GETNSID, |
| 155 |
.g.rtgen_family = AF_UNSPEC, |
| 156 |
}; |
| 157 |
struct nlmsghdr *answer; |
| 158 |
struct rtattr *tb[NETNSA_MAX + 1]; |
| 159 |
struct rtgenmsg *rthdr; |
| 160 |
int len, fd, ret = -1; |
| 161 |
|
| 162 |
fd = netns_get_fd(name); |
| 163 |
if (fd < 0) |
| 164 |
return fd; |
| 165 |
|
| 166 |
addattr32(&req.n, 1024, NETNSA_FD, fd); |
| 167 |
if (rtnl_talk(rtnl, &req.n, &answer) < 0) { |
| 168 |
close(fd); |
| 169 |
return -2; |
| 170 |
} |
| 171 |
close(fd); |
| 172 |
|
| 173 |
/* Validate message and parse attributes */ |
| 174 |
if (answer->nlmsg_type == NLMSG_ERROR) |
| 175 |
goto out; |
| 176 |
|
| 177 |
rthdr = NLMSG_DATA(answer); |
| 178 |
len = answer->nlmsg_len - NLMSG_SPACE(sizeof(*rthdr)); |
| 179 |
if (len < 0) |
| 180 |
goto out; |
| 181 |
|
| 182 |
parse_rtattr(tb, NETNSA_MAX, NETNS_RTA(rthdr), len); |
| 183 |
|
| 184 |
if (tb[NETNSA_NSID]) |
| 185 |
ret = rta_getattr_s32(tb[NETNSA_NSID]); |
| 186 |
|
| 187 |
out: |
| 188 |
free(answer); |
| 189 |
return ret; |
| 190 |
} |
| 191 |
|
| 192 |
int set_netns_id_from_name(struct rtnl_handle *rtnl, const char *name, int nsid) |
| 193 |
{ |
| 194 |
struct { |
| 195 |
struct nlmsghdr n; |
| 196 |
struct rtgenmsg g; |
| 197 |
char buf[1024]; |
| 198 |
} req = { |
| 199 |
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)), |
| 200 |
.n.nlmsg_flags = NLM_F_REQUEST, |
| 201 |
.n.nlmsg_type = RTM_NEWNSID, |
| 202 |
.g.rtgen_family = AF_UNSPEC, |
| 203 |
}; |
| 204 |
int fd, err = 0; |
| 205 |
|
| 206 |
fd = netns_get_fd(name); |
| 207 |
if (fd < 0) |
| 208 |
return fd; |
| 209 |
|
| 210 |
addattr32(&req.n, 1024, NETNSA_FD, fd); |
| 211 |
addattr32(&req.n, 1024, NETNSA_NSID, nsid); |
| 212 |
if (rtnl_talk(rtnl, &req.n, NULL) < 0) |
| 213 |
err = -2; |
| 214 |
|
| 215 |
close(fd); |
| 216 |
return err; |
| 217 |
} |
| 218 |
|
| 219 |
struct netns_name_from_id_ctx { |
| 220 |
int32_t id; |
| 221 |
char *name; |
| 222 |
struct rtnl_handle *rth; |
| 223 |
}; |
| 224 |
|
| 225 |
static int netns_name_from_id_func(char *nsname, void *arg) |
| 226 |
{ |
| 227 |
struct netns_name_from_id_ctx *ctx = arg; |
| 228 |
int32_t ret; |
| 229 |
|
| 230 |
ret = netns_id_from_name(ctx->rth, nsname); |
| 231 |
if (ret < 0 || ret != ctx->id) |
| 232 |
return 0; |
| 233 |
ctx->name = strdup(nsname); |
| 234 |
return 1; |
| 235 |
} |
| 236 |
|
| 237 |
char *netns_name_from_id(int32_t id) |
| 238 |
{ |
| 239 |
struct rtnl_handle rth; |
| 240 |
struct netns_name_from_id_ctx ctx = { |
| 241 |
.id = id, |
| 242 |
.rth = &rth, |
| 243 |
}; |
| 244 |
|
| 245 |
if (rtnl_open(&rth, 0) < 0) |
| 246 |
return NULL; |
| 247 |
netns_foreach(netns_name_from_id_func, &ctx); |
| 248 |
rtnl_close(&rth); |
| 249 |
|
| 250 |
return ctx.name; |
| 251 |
} |