| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/if.h> |
| 4 |
#include <linux/nl80211.h> |
| 5 |
#include <linux/veth.h> |
| 6 |
#include <net/if.h> |
| 7 |
#include <sys/file.h> |
| 8 |
#include <sys/mount.h> |
| 9 |
#include <unistd.h> |
| 10 |
|
| 11 |
#include "sd-device.h" |
| 12 |
#include "sd-id128.h" |
| 13 |
#include "sd-netlink.h" |
| 14 |
|
| 15 |
#include "alloc-util.h" |
| 16 |
#include "device-private.h" |
| 17 |
#include "device-util.h" |
| 18 |
#include "ether-addr-util.h" |
| 19 |
#include "extract-word.h" |
| 20 |
#include "fd-util.h" |
| 21 |
#include "lock-util.h" |
| 22 |
#include "mkdir.h" |
| 23 |
#include "mount-util.h" |
| 24 |
#include "namespace-util.h" |
| 25 |
#include "netif-util.h" |
| 26 |
#include "netlink-util.h" |
| 27 |
#include "nspawn-network.h" |
| 28 |
#include "pidref.h" |
| 29 |
#include "process-util.h" |
| 30 |
#include "socket-util.h" |
| 31 |
#include "stat-util.h" |
| 32 |
#include "string-util.h" |
| 33 |
#include "strv.h" |
| 34 |
#include "udev-util.h" |
| 35 |
|
| 36 |
#define HOST_HASH_KEY SD_ID128_MAKE(1a,37,6f,c7,46,ec,45,0b,ad,a3,d5,31,06,60,5d,b1) |
| 37 |
#define CONTAINER_HASH_KEY SD_ID128_MAKE(c3,c4,f9,19,b5,57,b2,1c,e6,cf,14,27,03,9c,ee,a2) |
| 38 |
#define VETH_EXTRA_HOST_HASH_KEY SD_ID128_MAKE(48,c7,f6,b7,ea,9d,4c,9e,b7,28,d4,de,91,d5,bf,66) |
| 39 |
#define VETH_EXTRA_CONTAINER_HASH_KEY SD_ID128_MAKE(af,50,17,61,ce,f9,4d,35,84,0d,2b,20,54,be,ce,59) |
| 40 |
#define MACVLAN_HASH_KEY SD_ID128_MAKE(00,13,6d,bc,66,83,44,81,bb,0c,f9,51,1f,24,a6,6f) |
| 41 |
|
| 42 |
static int remove_one_link(sd_netlink *rtnl, const char *name) { |
| 43 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 44 |
int r; |
| 45 |
|
| 46 |
if (isempty(name)) |
| 47 |
return 0; |
| 48 |
|
| 49 |
r = sd_rtnl_message_new_link(rtnl, &m, RTM_DELLINK, 0); |
| 50 |
if (r < 0) |
| 51 |
return log_error_errno(r, "Failed to allocate netlink message: %m"); |
| 52 |
|
| 53 |
r = sd_netlink_message_append_string(m, IFLA_IFNAME, name); |
| 54 |
if (r < 0) |
| 55 |
return log_error_errno(r, "Failed to add netlink interface name: %m"); |
| 56 |
|
| 57 |
r = sd_netlink_call(rtnl, m, 0, NULL); |
| 58 |
if (r == -ENODEV) /* Already gone */ |
| 59 |
return 0; |
| 60 |
if (r < 0) |
| 61 |
return log_error_errno(r, "Failed to remove interface %s: %m", name); |
| 62 |
|
| 63 |
return 1; |
| 64 |
} |
| 65 |
|
| 66 |
static int set_alternative_ifname(sd_netlink *rtnl, const char *ifname, const char *altifname) { |
| 67 |
int r; |
| 68 |
|
| 69 |
assert(rtnl); |
| 70 |
assert(ifname); |
| 71 |
|
| 72 |
if (!altifname) |
| 73 |
return 0; |
| 74 |
|
| 75 |
if (strlen(altifname) >= ALTIFNAMSIZ) |
| 76 |
return log_warning_errno(SYNTHETIC_ERRNO(ERANGE), |
| 77 |
"Alternative interface name '%s' for '%s' is too long, ignoring", |
| 78 |
altifname, ifname); |
| 79 |
|
| 80 |
r = rtnl_set_link_alternative_names_by_ifname(&rtnl, ifname, STRV_MAKE(altifname)); |
| 81 |
if (r < 0) |
| 82 |
return log_warning_errno(r, |
| 83 |
"Failed to set alternative interface name '%s' to '%s', ignoring: %m", |
| 84 |
altifname, ifname); |
| 85 |
|
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
static int add_veth( |
| 90 |
sd_netlink *rtnl, |
| 91 |
const PidRef *pid, |
| 92 |
const char *ifname_host, |
| 93 |
const char *altifname_host, |
| 94 |
const struct ether_addr *mac_host, |
| 95 |
const char *ifname_container, |
| 96 |
const struct ether_addr *mac_container) { |
| 97 |
|
| 98 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 99 |
int r; |
| 100 |
|
| 101 |
assert(rtnl); |
| 102 |
assert(pidref_is_set(pid)); |
| 103 |
assert(ifname_host); |
| 104 |
assert(mac_host); |
| 105 |
assert(ifname_container); |
| 106 |
assert(mac_container); |
| 107 |
|
| 108 |
r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0); |
| 109 |
if (r < 0) |
| 110 |
return log_error_errno(r, "Failed to allocate netlink message: %m"); |
| 111 |
|
| 112 |
r = sd_netlink_message_append_string(m, IFLA_IFNAME, ifname_host); |
| 113 |
if (r < 0) |
| 114 |
return log_error_errno(r, "Failed to add netlink interface name: %m"); |
| 115 |
|
| 116 |
r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, mac_host); |
| 117 |
if (r < 0) |
| 118 |
return log_error_errno(r, "Failed to add netlink MAC address: %m"); |
| 119 |
|
| 120 |
r = sd_netlink_message_open_container(m, IFLA_LINKINFO); |
| 121 |
if (r < 0) |
| 122 |
return log_error_errno(r, "Failed to open netlink container: %m"); |
| 123 |
|
| 124 |
r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "veth"); |
| 125 |
if (r < 0) |
| 126 |
return log_error_errno(r, "Failed to open netlink container: %m"); |
| 127 |
|
| 128 |
r = sd_netlink_message_open_container(m, VETH_INFO_PEER); |
| 129 |
if (r < 0) |
| 130 |
return log_error_errno(r, "Failed to open netlink container: %m"); |
| 131 |
|
| 132 |
r = sd_netlink_message_append_string(m, IFLA_IFNAME, ifname_container); |
| 133 |
if (r < 0) |
| 134 |
return log_error_errno(r, "Failed to add netlink interface name: %m"); |
| 135 |
|
| 136 |
r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, mac_container); |
| 137 |
if (r < 0) |
| 138 |
return log_error_errno(r, "Failed to add netlink MAC address: %m"); |
| 139 |
|
| 140 |
r = sd_netlink_message_append_u32(m, IFLA_NET_NS_PID, pid->pid); |
| 141 |
if (r < 0) |
| 142 |
return log_error_errno(r, "Failed to add netlink namespace field: %m"); |
| 143 |
|
| 144 |
r = sd_netlink_message_close_container(m); |
| 145 |
if (r < 0) |
| 146 |
return log_error_errno(r, "Failed to close netlink container: %m"); |
| 147 |
|
| 148 |
r = sd_netlink_message_close_container(m); |
| 149 |
if (r < 0) |
| 150 |
return log_error_errno(r, "Failed to close netlink container: %m"); |
| 151 |
|
| 152 |
r = sd_netlink_message_close_container(m); |
| 153 |
if (r < 0) |
| 154 |
return log_error_errno(r, "Failed to close netlink container: %m"); |
| 155 |
|
| 156 |
r = sd_netlink_call(rtnl, m, 0, NULL); |
| 157 |
if (r < 0) |
| 158 |
return log_error_errno(r, "Failed to add new veth interfaces (%s:%s): %m", ifname_host, ifname_container); |
| 159 |
|
| 160 |
(void) set_alternative_ifname(rtnl, ifname_host, altifname_host); |
| 161 |
|
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
int setup_veth(const char *machine_name, |
| 166 |
const PidRef *pid, |
| 167 |
char iface_name[IFNAMSIZ], |
| 168 |
bool bridge, |
| 169 |
const struct ether_addr *provided_mac) { |
| 170 |
|
| 171 |
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
| 172 |
struct ether_addr mac_host, mac_container; |
| 173 |
unsigned u; |
| 174 |
char *n, *a = NULL; |
| 175 |
int r; |
| 176 |
|
| 177 |
assert(machine_name); |
| 178 |
assert(pidref_is_set(pid)); |
| 179 |
assert(iface_name); |
| 180 |
assert(provided_mac); |
| 181 |
|
| 182 |
/* Use two different interface name prefixes depending whether |
| 183 |
* we are in bridge mode or not. */ |
| 184 |
n = strjoina(bridge ? "vb-" : "ve-", machine_name); |
| 185 |
r = net_shorten_ifname(n, /* check_naming_scheme= */ true); |
| 186 |
if (r > 0) |
| 187 |
a = strjoina(bridge ? "vb-" : "ve-", machine_name); |
| 188 |
|
| 189 |
if (ether_addr_is_null(provided_mac)){ |
| 190 |
r = net_generate_mac(machine_name, &mac_container, CONTAINER_HASH_KEY, 0); |
| 191 |
if (r < 0) |
| 192 |
return log_error_errno(r, "Failed to generate predictable MAC address for container side: %m"); |
| 193 |
} else |
| 194 |
mac_container = *provided_mac; |
| 195 |
|
| 196 |
r = net_generate_mac(machine_name, &mac_host, HOST_HASH_KEY, 0); |
| 197 |
if (r < 0) |
| 198 |
return log_error_errno(r, "Failed to generate predictable MAC address for host side: %m"); |
| 199 |
|
| 200 |
r = sd_netlink_open(&rtnl); |
| 201 |
if (r < 0) |
| 202 |
return log_error_errno(r, "Failed to connect to netlink: %m"); |
| 203 |
|
| 204 |
r = add_veth(rtnl, pid, n, a, &mac_host, "host0", &mac_container); |
| 205 |
if (r < 0) |
| 206 |
return r; |
| 207 |
|
| 208 |
u = if_nametoindex(n); /* We don't need to use rtnl_resolve_ifname() here because the |
| 209 |
* name we assigned is always the main name. */ |
| 210 |
if (u == 0) |
| 211 |
return log_error_errno(errno, "Failed to resolve interface %s: %m", n); |
| 212 |
|
| 213 |
strcpy(iface_name, n); |
| 214 |
return (int) u; |
| 215 |
} |
| 216 |
|
| 217 |
int setup_veth_extra( |
| 218 |
const char *machine_name, |
| 219 |
const PidRef *pid, |
| 220 |
char **pairs) { |
| 221 |
|
| 222 |
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
| 223 |
uint64_t idx = 0; |
| 224 |
int r; |
| 225 |
|
| 226 |
assert(machine_name); |
| 227 |
assert(pidref_is_set(pid)); |
| 228 |
|
| 229 |
if (strv_isempty(pairs)) |
| 230 |
return 0; |
| 231 |
|
| 232 |
r = sd_netlink_open(&rtnl); |
| 233 |
if (r < 0) |
| 234 |
return log_error_errno(r, "Failed to connect to netlink: %m"); |
| 235 |
|
| 236 |
STRV_FOREACH_PAIR(a, b, pairs) { |
| 237 |
struct ether_addr mac_host, mac_container; |
| 238 |
|
| 239 |
r = net_generate_mac(machine_name, &mac_container, VETH_EXTRA_CONTAINER_HASH_KEY, idx); |
| 240 |
if (r < 0) |
| 241 |
return log_error_errno(r, "Failed to generate predictable MAC address for container side of extra veth link: %m"); |
| 242 |
|
| 243 |
r = net_generate_mac(machine_name, &mac_host, VETH_EXTRA_HOST_HASH_KEY, idx); |
| 244 |
if (r < 0) |
| 245 |
return log_error_errno(r, "Failed to generate predictable MAC address for host side of extra veth link: %m"); |
| 246 |
|
| 247 |
r = add_veth(rtnl, pid, *a, NULL, &mac_host, *b, &mac_container); |
| 248 |
if (r < 0) |
| 249 |
return r; |
| 250 |
|
| 251 |
idx++; |
| 252 |
} |
| 253 |
|
| 254 |
return 0; |
| 255 |
} |
| 256 |
|
| 257 |
static int join_bridge(sd_netlink *rtnl, const char *veth_name, const char *bridge_name) { |
| 258 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 259 |
int r, bridge_ifi; |
| 260 |
|
| 261 |
assert(rtnl); |
| 262 |
assert(veth_name); |
| 263 |
assert(bridge_name); |
| 264 |
|
| 265 |
bridge_ifi = rtnl_resolve_interface(&rtnl, bridge_name); |
| 266 |
if (bridge_ifi < 0) |
| 267 |
return bridge_ifi; |
| 268 |
|
| 269 |
r = sd_rtnl_message_new_link(rtnl, &m, RTM_SETLINK, 0); |
| 270 |
if (r < 0) |
| 271 |
return r; |
| 272 |
|
| 273 |
r = sd_rtnl_message_link_set_flags(m, IFF_UP, IFF_UP); |
| 274 |
if (r < 0) |
| 275 |
return r; |
| 276 |
|
| 277 |
r = sd_netlink_message_append_string(m, IFLA_IFNAME, veth_name); |
| 278 |
if (r < 0) |
| 279 |
return r; |
| 280 |
|
| 281 |
r = sd_netlink_message_append_u32(m, IFLA_MASTER, bridge_ifi); |
| 282 |
if (r < 0) |
| 283 |
return r; |
| 284 |
|
| 285 |
r = sd_netlink_call(rtnl, m, 0, NULL); |
| 286 |
if (r < 0) |
| 287 |
return r; |
| 288 |
|
| 289 |
return bridge_ifi; |
| 290 |
} |
| 291 |
|
| 292 |
static int create_bridge(sd_netlink *rtnl, const char *bridge_name) { |
| 293 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 294 |
int r; |
| 295 |
|
| 296 |
r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0); |
| 297 |
if (r < 0) |
| 298 |
return r; |
| 299 |
|
| 300 |
r = sd_netlink_message_append_string(m, IFLA_IFNAME, bridge_name); |
| 301 |
if (r < 0) |
| 302 |
return r; |
| 303 |
|
| 304 |
r = sd_netlink_message_open_container(m, IFLA_LINKINFO); |
| 305 |
if (r < 0) |
| 306 |
return r; |
| 307 |
|
| 308 |
r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "bridge"); |
| 309 |
if (r < 0) |
| 310 |
return r; |
| 311 |
|
| 312 |
r = sd_netlink_message_close_container(m); |
| 313 |
if (r < 0) |
| 314 |
return r; |
| 315 |
|
| 316 |
r = sd_netlink_message_close_container(m); |
| 317 |
if (r < 0) |
| 318 |
return r; |
| 319 |
|
| 320 |
r = sd_netlink_call(rtnl, m, 0, NULL); |
| 321 |
if (r < 0) |
| 322 |
return r; |
| 323 |
|
| 324 |
return 0; |
| 325 |
} |
| 326 |
|
| 327 |
int setup_bridge(const char *veth_name, const char *bridge_name, bool create) { |
| 328 |
_cleanup_(release_lock_file) LockFile bridge_lock = LOCK_FILE_INIT; |
| 329 |
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
| 330 |
int r, bridge_ifi; |
| 331 |
unsigned n = 0; |
| 332 |
|
| 333 |
assert(veth_name); |
| 334 |
assert(bridge_name); |
| 335 |
|
| 336 |
r = sd_netlink_open(&rtnl); |
| 337 |
if (r < 0) |
| 338 |
return log_error_errno(r, "Failed to connect to netlink: %m"); |
| 339 |
|
| 340 |
if (create) { |
| 341 |
/* We take a system-wide lock here, so that we can safely check whether there's still a member in the |
| 342 |
* bridge before removing it, without risking interference from other nspawn instances. */ |
| 343 |
|
| 344 |
r = make_lock_file("/run/systemd/nspawn-network-zone", LOCK_EX, &bridge_lock); |
| 345 |
if (r < 0) |
| 346 |
return log_error_errno(r, "Failed to take network zone lock: %m"); |
| 347 |
} |
| 348 |
|
| 349 |
for (;;) { |
| 350 |
bridge_ifi = join_bridge(rtnl, veth_name, bridge_name); |
| 351 |
if (bridge_ifi >= 0) |
| 352 |
return bridge_ifi; |
| 353 |
if (bridge_ifi != -ENODEV || !create || n > 10) |
| 354 |
return log_error_errno(bridge_ifi, "Failed to add interface %s to bridge %s: %m", veth_name, bridge_name); |
| 355 |
|
| 356 |
/* Count attempts, so that we don't enter an endless loop here. */ |
| 357 |
n++; |
| 358 |
|
| 359 |
/* The bridge doesn't exist yet. Let's create it */ |
| 360 |
r = create_bridge(rtnl, bridge_name); |
| 361 |
if (r < 0) |
| 362 |
return log_error_errno(r, "Failed to create bridge interface %s: %m", bridge_name); |
| 363 |
|
| 364 |
/* Try again, now that the bridge exists */ |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
int remove_bridge(const char *bridge_name) { |
| 369 |
_cleanup_(release_lock_file) LockFile bridge_lock = LOCK_FILE_INIT; |
| 370 |
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
| 371 |
const char *path; |
| 372 |
int r; |
| 373 |
|
| 374 |
/* Removes the specified bridge, but only if it is currently empty */ |
| 375 |
|
| 376 |
if (isempty(bridge_name)) |
| 377 |
return 0; |
| 378 |
|
| 379 |
r = make_lock_file("/run/systemd/nspawn-network-zone", LOCK_EX, &bridge_lock); |
| 380 |
if (r < 0) |
| 381 |
return log_error_errno(r, "Failed to take network zone lock: %m"); |
| 382 |
|
| 383 |
path = strjoina("/sys/class/net/", bridge_name, "/brif"); |
| 384 |
|
| 385 |
r = dir_is_empty(path, /* ignore_hidden_or_backup= */ false); |
| 386 |
if (r == -ENOENT) /* Already gone? */ |
| 387 |
return 0; |
| 388 |
if (r < 0) |
| 389 |
return log_error_errno(r, "Can't detect if bridge %s is empty: %m", bridge_name); |
| 390 |
if (r == 0) /* Still populated, leave it around */ |
| 391 |
return 0; |
| 392 |
|
| 393 |
r = sd_netlink_open(&rtnl); |
| 394 |
if (r < 0) |
| 395 |
return log_error_errno(r, "Failed to connect to netlink: %m"); |
| 396 |
|
| 397 |
return remove_one_link(rtnl, bridge_name); |
| 398 |
} |
| 399 |
|
| 400 |
static int test_network_interface_initialized(const char *name) { |
| 401 |
_cleanup_(sd_device_unrefp) sd_device *d = NULL; |
| 402 |
int r; |
| 403 |
|
| 404 |
if (!udev_available()) |
| 405 |
return 0; |
| 406 |
|
| 407 |
/* udev should be around. */ |
| 408 |
|
| 409 |
r = sd_device_new_from_ifname(&d, name); |
| 410 |
if (r < 0) |
| 411 |
return log_error_errno(r, "Failed to get device %s: %m", name); |
| 412 |
|
| 413 |
r = device_is_processed(d); |
| 414 |
if (r < 0) |
| 415 |
return log_error_errno(r, "Failed to determine whether interface %s is initialized: %m", name); |
| 416 |
if (r == 0) |
| 417 |
return log_error_errno(SYNTHETIC_ERRNO(EBUSY), "Network interface %s is not initialized yet.", name); |
| 418 |
|
| 419 |
r = device_is_renaming(d); |
| 420 |
if (r < 0) |
| 421 |
return log_error_errno(r, "Failed to determine the interface %s is being renamed: %m", name); |
| 422 |
if (r > 0) |
| 423 |
return log_error_errno(SYNTHETIC_ERRNO(EBUSY), "Interface %s is being renamed.", name); |
| 424 |
|
| 425 |
return 0; |
| 426 |
} |
| 427 |
|
| 428 |
int test_network_interfaces_initialized(char **iface_pairs) { |
| 429 |
int r; |
| 430 |
STRV_FOREACH_PAIR(a, b, iface_pairs) { |
| 431 |
r = test_network_interface_initialized(*a); |
| 432 |
if (r < 0) |
| 433 |
return r; |
| 434 |
} |
| 435 |
return 0; |
| 436 |
} |
| 437 |
|
| 438 |
int resolve_network_interface_names(char **iface_pairs) { |
| 439 |
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
| 440 |
int r; |
| 441 |
|
| 442 |
/* Due to a bug in kernel fixed by 8e15aee621618a3ee3abecaf1fd8c1428098b7ef (v6.6, backported to |
| 443 |
* 6.1.60 and 6.5.9), an interface with alternative names cannot be resolved by the alternative name |
| 444 |
* if the interface is moved to another network namespace. Hence, we need to adjust the provided |
| 445 |
* names before moving interfaces to container namespace. */ |
| 446 |
|
| 447 |
STRV_FOREACH_PAIR(from, to, iface_pairs) { |
| 448 |
_cleanup_free_ char *name = NULL; |
| 449 |
_cleanup_strv_free_ char **altnames = NULL; |
| 450 |
|
| 451 |
r = rtnl_resolve_ifname_full(&rtnl, _RESOLVE_IFNAME_ALL, *from, &name, &altnames); |
| 452 |
if (r < 0) |
| 453 |
return r; |
| 454 |
|
| 455 |
/* Always use the resolved name for 'from'. */ |
| 456 |
free_and_replace(*from, name); |
| 457 |
|
| 458 |
/* If the name 'to' is assigned as an alternative name, we cannot rename the interface. |
| 459 |
* Hence, use the assigned interface name (including the alternative names) as is, and |
| 460 |
* use the resolved name for 'to'. */ |
| 461 |
if (strv_contains(altnames, *to)) { |
| 462 |
r = free_and_strdup_warn(to, *from); |
| 463 |
if (r < 0) |
| 464 |
return r; |
| 465 |
} |
| 466 |
} |
| 467 |
return 0; |
| 468 |
} |
| 469 |
|
| 470 |
static int netns_child_begin(int netns_fd, int *ret_original_netns_fd) { |
| 471 |
_cleanup_close_ int original_netns_fd = -EBADF; |
| 472 |
int r; |
| 473 |
|
| 474 |
assert(netns_fd >= 0); |
| 475 |
|
| 476 |
if (ret_original_netns_fd) { |
| 477 |
original_netns_fd = namespace_open_by_type(NAMESPACE_NET); |
| 478 |
if (original_netns_fd < 0) |
| 479 |
return log_error_errno(original_netns_fd, "Failed to open original network namespace: %m"); |
| 480 |
} |
| 481 |
|
| 482 |
r = namespace_enter(/* pidns_fd= */ -EBADF, |
| 483 |
/* mntns_fd= */ -EBADF, |
| 484 |
netns_fd, |
| 485 |
/* userns_fd= */ -EBADF, |
| 486 |
/* root_fd= */ -EBADF); |
| 487 |
if (r < 0) |
| 488 |
return log_error_errno(r, "Failed to enter child network namespace: %m"); |
| 489 |
|
| 490 |
r = umount_recursive("/sys/", /* flags= */ 0); |
| 491 |
if (r < 0) |
| 492 |
log_debug_errno(r, "Failed to unmount directories below /sys/, ignoring: %m"); |
| 493 |
|
| 494 |
(void) mkdir_p("/sys/", 0755); |
| 495 |
|
| 496 |
/* Populate new sysfs instance associated with the client netns, to make sd_device usable. */ |
| 497 |
r = mount_nofollow_verbose(LOG_ERR, "sysfs", "/sys/", "sysfs", |
| 498 |
MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV, /* options= */ NULL); |
| 499 |
if (r < 0) |
| 500 |
return log_error_errno(r, "Failed to mount sysfs on /sys/: %m"); |
| 501 |
|
| 502 |
/* udev_available() might be called previously and the result may be cached. |
| 503 |
* Now, we (re-)mount sysfs. Hence, we need to reset the cache. */ |
| 504 |
reset_cached_udev_availability(); |
| 505 |
|
| 506 |
if (ret_original_netns_fd) |
| 507 |
*ret_original_netns_fd = TAKE_FD(original_netns_fd); |
| 508 |
|
| 509 |
return 0; |
| 510 |
} |
| 511 |
|
| 512 |
static int netns_fork_and_wait(int netns_fd, int *ret_original_netns_fd) { |
| 513 |
int r; |
| 514 |
|
| 515 |
assert(netns_fd >= 0); |
| 516 |
|
| 517 |
r = pidref_safe_fork( |
| 518 |
"(sd-netns)", |
| 519 |
FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_WAIT|FORK_LOG|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, |
| 520 |
/* ret= */ NULL); |
| 521 |
if (r < 0) |
| 522 |
return log_error_errno(r, "Failed to fork process (sd-netns): %m"); |
| 523 |
if (r == 0) { |
| 524 |
if (netns_child_begin(netns_fd, ret_original_netns_fd) < 0) |
| 525 |
_exit(EXIT_FAILURE); |
| 526 |
|
| 527 |
return 0; |
| 528 |
} |
| 529 |
|
| 530 |
if (ret_original_netns_fd) |
| 531 |
*ret_original_netns_fd = -EBADF; |
| 532 |
|
| 533 |
return 1; |
| 534 |
} |
| 535 |
|
| 536 |
static int move_wlan_interface_impl(sd_netlink **genl, int netns_fd, sd_device *dev) { |
| 537 |
_cleanup_(sd_netlink_unrefp) sd_netlink *our_genl = NULL; |
| 538 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 539 |
int r; |
| 540 |
|
| 541 |
assert(netns_fd >= 0); |
| 542 |
assert(dev); |
| 543 |
|
| 544 |
if (!genl) |
| 545 |
genl = &our_genl; |
| 546 |
if (!*genl) { |
| 547 |
r = sd_genl_socket_open(genl); |
| 548 |
if (r < 0) |
| 549 |
return log_error_errno(r, "Failed to connect to generic netlink: %m"); |
| 550 |
} |
| 551 |
|
| 552 |
r = sd_genl_message_new(*genl, NL80211_GENL_NAME, NL80211_CMD_SET_WIPHY_NETNS, &m); |
| 553 |
if (r < 0) |
| 554 |
return log_device_error_errno(dev, r, "Failed to allocate netlink message: %m"); |
| 555 |
|
| 556 |
uint32_t phy_index; |
| 557 |
r = device_get_sysattr_u32(dev, "phy80211/index", &phy_index); |
| 558 |
if (r < 0) |
| 559 |
return log_device_error_errno(dev, r, "Failed to get phy index: %m"); |
| 560 |
|
| 561 |
r = sd_netlink_message_append_u32(m, NL80211_ATTR_WIPHY, phy_index); |
| 562 |
if (r < 0) |
| 563 |
return log_device_error_errno(dev, r, "Failed to append phy index to netlink message: %m"); |
| 564 |
|
| 565 |
r = sd_netlink_message_append_u32(m, NL80211_ATTR_NETNS_FD, netns_fd); |
| 566 |
if (r < 0) |
| 567 |
return log_device_error_errno(dev, r, "Failed to append namespace fd to netlink message: %m"); |
| 568 |
|
| 569 |
r = sd_netlink_call(*genl, m, 0, NULL); |
| 570 |
if (r < 0) |
| 571 |
return log_device_error_errno(dev, r, "Failed to move interface to namespace: %m"); |
| 572 |
|
| 573 |
return 0; |
| 574 |
} |
| 575 |
|
| 576 |
static int move_wlan_interface_one( |
| 577 |
sd_netlink **rtnl, |
| 578 |
sd_netlink **genl, |
| 579 |
int *temp_netns_fd, |
| 580 |
int netns_fd, |
| 581 |
sd_device *dev, |
| 582 |
const char *name) { |
| 583 |
|
| 584 |
int r; |
| 585 |
|
| 586 |
assert(rtnl); |
| 587 |
assert(genl); |
| 588 |
assert(temp_netns_fd); |
| 589 |
assert(netns_fd >= 0); |
| 590 |
assert(dev); |
| 591 |
|
| 592 |
if (!name) |
| 593 |
return move_wlan_interface_impl(genl, netns_fd, dev); |
| 594 |
|
| 595 |
/* The command NL80211_CMD_SET_WIPHY_NETNS takes phy instead of network interface, and does not take |
| 596 |
* an interface name in the passed network namespace. Hence, we need to move the phy and interface to |
| 597 |
* a temporary network namespace, rename the interface in it, and move them to the requested netns. */ |
| 598 |
|
| 599 |
if (*temp_netns_fd < 0) { |
| 600 |
r = netns_acquire(); |
| 601 |
if (r < 0) |
| 602 |
return log_error_errno(r, "Failed to acquire new network namespace: %m"); |
| 603 |
*temp_netns_fd = r; |
| 604 |
} |
| 605 |
|
| 606 |
r = move_wlan_interface_impl(genl, *temp_netns_fd, dev); |
| 607 |
if (r < 0) |
| 608 |
return r; |
| 609 |
|
| 610 |
const char *sysname; |
| 611 |
r = sd_device_get_sysname(dev, &sysname); |
| 612 |
if (r < 0) |
| 613 |
return log_device_error_errno(dev, r, "Failed to get interface name: %m"); |
| 614 |
|
| 615 |
r = netns_fork_and_wait(*temp_netns_fd, NULL); |
| 616 |
if (r < 0) |
| 617 |
return log_error_errno(r, "Failed to fork process (nspawn-rename-wlan): %m"); |
| 618 |
if (r == 0) { |
| 619 |
_cleanup_(sd_device_unrefp) sd_device *temp_dev = NULL; |
| 620 |
|
| 621 |
r = rtnl_rename_link(NULL, sysname, name); |
| 622 |
if (r < 0) { |
| 623 |
log_error_errno(r, "Failed to rename network interface '%s' to '%s': %m", sysname, name); |
| 624 |
goto finalize; |
| 625 |
} |
| 626 |
|
| 627 |
r = sd_device_new_from_ifname(&temp_dev, name); |
| 628 |
if (r < 0) { |
| 629 |
log_error_errno(r, "Failed to acquire device '%s': %m", name); |
| 630 |
goto finalize; |
| 631 |
} |
| 632 |
|
| 633 |
r = move_wlan_interface_impl(NULL, netns_fd, temp_dev); |
| 634 |
|
| 635 |
finalize: |
| 636 |
_exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
| 637 |
} |
| 638 |
|
| 639 |
return 0; |
| 640 |
} |
| 641 |
|
| 642 |
static int move_network_interface_one(sd_netlink **rtnl, int netns_fd, sd_device *dev, const char *name) { |
| 643 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 644 |
int r; |
| 645 |
|
| 646 |
assert(rtnl); |
| 647 |
assert(netns_fd >= 0); |
| 648 |
assert(dev); |
| 649 |
|
| 650 |
if (!*rtnl) { |
| 651 |
r = sd_netlink_open(rtnl); |
| 652 |
if (r < 0) |
| 653 |
return log_error_errno(r, "Failed to connect to rtnetlink: %m"); |
| 654 |
} |
| 655 |
|
| 656 |
int ifindex; |
| 657 |
r = sd_device_get_ifindex(dev, &ifindex); |
| 658 |
if (r < 0) |
| 659 |
return log_device_error_errno(dev, r, "Failed to get ifindex: %m"); |
| 660 |
|
| 661 |
r = sd_rtnl_message_new_link(*rtnl, &m, RTM_SETLINK, ifindex); |
| 662 |
if (r < 0) |
| 663 |
return log_device_error_errno(dev, r, "Failed to allocate netlink message: %m"); |
| 664 |
|
| 665 |
r = sd_netlink_message_append_u32(m, IFLA_NET_NS_FD, netns_fd); |
| 666 |
if (r < 0) |
| 667 |
return log_device_error_errno(dev, r, "Failed to append namespace fd to netlink message: %m"); |
| 668 |
|
| 669 |
if (name) { |
| 670 |
r = sd_netlink_message_append_string(m, IFLA_IFNAME, name); |
| 671 |
if (r < 0) |
| 672 |
return log_device_error_errno(dev, r, "Failed to add netlink interface name: %m"); |
| 673 |
} |
| 674 |
|
| 675 |
r = sd_netlink_call(*rtnl, m, 0, NULL); |
| 676 |
if (r < 0) |
| 677 |
return log_device_error_errno(dev, r, "Failed to move interface to namespace: %m"); |
| 678 |
|
| 679 |
return 0; |
| 680 |
} |
| 681 |
|
| 682 |
int move_network_interfaces(int netns_fd, char **iface_pairs) { |
| 683 |
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL, *genl = NULL; |
| 684 |
_cleanup_close_ int temp_netns_fd = -EBADF; |
| 685 |
int r; |
| 686 |
|
| 687 |
assert(netns_fd >= 0); |
| 688 |
|
| 689 |
if (strv_isempty(iface_pairs)) |
| 690 |
return 0; |
| 691 |
|
| 692 |
STRV_FOREACH_PAIR(from, to, iface_pairs) { |
| 693 |
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
| 694 |
const char *name; |
| 695 |
|
| 696 |
name = streq(*from, *to) ? NULL : *to; |
| 697 |
|
| 698 |
r = sd_device_new_from_ifname(&dev, *from); |
| 699 |
if (r < 0) |
| 700 |
return log_error_errno(r, "Unknown interface name %s: %m", *from); |
| 701 |
|
| 702 |
if (device_is_devtype(dev, "wlan")) |
| 703 |
r = move_wlan_interface_one(&rtnl, &genl, &temp_netns_fd, netns_fd, dev, name); |
| 704 |
else |
| 705 |
r = move_network_interface_one(&rtnl, netns_fd, dev, name); |
| 706 |
if (r < 0) |
| 707 |
return r; |
| 708 |
} |
| 709 |
|
| 710 |
return 0; |
| 711 |
} |
| 712 |
|
| 713 |
int move_back_network_interfaces(int child_netns_fd, char **interface_pairs) { |
| 714 |
_cleanup_close_ int parent_netns_fd = -EBADF; |
| 715 |
int r; |
| 716 |
|
| 717 |
assert(child_netns_fd >= 0); |
| 718 |
|
| 719 |
if (strv_isempty(interface_pairs)) |
| 720 |
return 0; |
| 721 |
|
| 722 |
r = netns_fork_and_wait(child_netns_fd, &parent_netns_fd); |
| 723 |
if (r < 0) |
| 724 |
return r; |
| 725 |
if (r == 0) { |
| 726 |
/* Reverse network interfaces pair list so that interfaces get their initial name back. |
| 727 |
* This is about ensuring interfaces get their old name back when being moved back. */ |
| 728 |
interface_pairs = strv_reverse(interface_pairs); |
| 729 |
|
| 730 |
r = move_network_interfaces(parent_netns_fd, interface_pairs); |
| 731 |
_exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
| 732 |
} |
| 733 |
|
| 734 |
return 0; |
| 735 |
} |
| 736 |
|
| 737 |
int setup_macvlan(const char *machine_name, const PidRef *pid, char **iface_pairs) { |
| 738 |
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
| 739 |
unsigned idx = 0; |
| 740 |
int r; |
| 741 |
|
| 742 |
assert(pidref_is_set(pid)); |
| 743 |
|
| 744 |
if (strv_isempty(iface_pairs)) |
| 745 |
return 0; |
| 746 |
|
| 747 |
r = sd_netlink_open(&rtnl); |
| 748 |
if (r < 0) |
| 749 |
return log_error_errno(r, "Failed to connect to netlink: %m"); |
| 750 |
|
| 751 |
STRV_FOREACH_PAIR(i, b, iface_pairs) { |
| 752 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 753 |
_cleanup_free_ char *n = NULL; |
| 754 |
int shortened, ifi; |
| 755 |
struct ether_addr mac; |
| 756 |
|
| 757 |
ifi = rtnl_resolve_interface_or_warn(&rtnl, *i); |
| 758 |
if (ifi < 0) |
| 759 |
return ifi; |
| 760 |
|
| 761 |
r = net_generate_mac(machine_name, &mac, MACVLAN_HASH_KEY, idx++); |
| 762 |
if (r < 0) |
| 763 |
return log_error_errno(r, "Failed to create MACVLAN MAC address: %m"); |
| 764 |
|
| 765 |
r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0); |
| 766 |
if (r < 0) |
| 767 |
return log_error_errno(r, "Failed to allocate netlink message: %m"); |
| 768 |
|
| 769 |
r = sd_netlink_message_append_u32(m, IFLA_LINK, ifi); |
| 770 |
if (r < 0) |
| 771 |
return log_error_errno(r, "Failed to add netlink interface index: %m"); |
| 772 |
|
| 773 |
n = strdup(*b); |
| 774 |
if (!n) |
| 775 |
return log_oom(); |
| 776 |
|
| 777 |
shortened = net_shorten_ifname(n, /* check_naming_scheme= */ true); |
| 778 |
|
| 779 |
r = sd_netlink_message_append_string(m, IFLA_IFNAME, n); |
| 780 |
if (r < 0) |
| 781 |
return log_error_errno(r, "Failed to add netlink interface name: %m"); |
| 782 |
|
| 783 |
r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, &mac); |
| 784 |
if (r < 0) |
| 785 |
return log_error_errno(r, "Failed to add netlink MAC address: %m"); |
| 786 |
|
| 787 |
r = sd_netlink_message_append_u32(m, IFLA_NET_NS_PID, pid->pid); |
| 788 |
if (r < 0) |
| 789 |
return log_error_errno(r, "Failed to add netlink namespace field: %m"); |
| 790 |
|
| 791 |
r = sd_netlink_message_open_container(m, IFLA_LINKINFO); |
| 792 |
if (r < 0) |
| 793 |
return log_error_errno(r, "Failed to open netlink container: %m"); |
| 794 |
|
| 795 |
r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "macvlan"); |
| 796 |
if (r < 0) |
| 797 |
return log_error_errno(r, "Failed to open netlink container: %m"); |
| 798 |
|
| 799 |
r = sd_netlink_message_append_u32(m, IFLA_MACVLAN_MODE, MACVLAN_MODE_BRIDGE); |
| 800 |
if (r < 0) |
| 801 |
return log_error_errno(r, "Failed to append macvlan mode: %m"); |
| 802 |
|
| 803 |
r = sd_netlink_message_close_container(m); |
| 804 |
if (r < 0) |
| 805 |
return log_error_errno(r, "Failed to close netlink container: %m"); |
| 806 |
|
| 807 |
r = sd_netlink_message_close_container(m); |
| 808 |
if (r < 0) |
| 809 |
return log_error_errno(r, "Failed to close netlink container: %m"); |
| 810 |
|
| 811 |
r = sd_netlink_call(rtnl, m, 0, NULL); |
| 812 |
if (r < 0) |
| 813 |
return log_error_errno(r, "Failed to add new macvlan interfaces: %m"); |
| 814 |
|
| 815 |
if (shortened > 0) |
| 816 |
(void) set_alternative_ifname(rtnl, n, *b); |
| 817 |
} |
| 818 |
|
| 819 |
return 0; |
| 820 |
} |
| 821 |
|
| 822 |
static int remove_macvlan_impl(char **interface_pairs) { |
| 823 |
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
| 824 |
int r; |
| 825 |
|
| 826 |
assert(interface_pairs); |
| 827 |
|
| 828 |
r = sd_netlink_open(&rtnl); |
| 829 |
if (r < 0) |
| 830 |
return log_error_errno(r, "Failed to connect to netlink: %m"); |
| 831 |
|
| 832 |
STRV_FOREACH_PAIR(a, b, interface_pairs) { |
| 833 |
_cleanup_free_ char *n = NULL; |
| 834 |
|
| 835 |
n = strdup(*b); |
| 836 |
if (!n) |
| 837 |
return log_oom(); |
| 838 |
|
| 839 |
(void) net_shorten_ifname(n, /* check_naming_scheme= */ true); |
| 840 |
|
| 841 |
r = remove_one_link(rtnl, n); |
| 842 |
if (r < 0) |
| 843 |
log_warning_errno(r, "Failed to remove macvlan interface %s, ignoring: %m", n); |
| 844 |
} |
| 845 |
|
| 846 |
return 0; |
| 847 |
} |
| 848 |
|
| 849 |
int remove_macvlan(int child_netns_fd, char **interface_pairs) { |
| 850 |
_cleanup_close_ int parent_netns_fd = -EBADF; |
| 851 |
int r; |
| 852 |
|
| 853 |
/* In some cases the kernel might pin the macvlan links on the container even after the namespace |
| 854 |
* died. Hence, let's better remove them explicitly too. See issue #680. */ |
| 855 |
|
| 856 |
assert(child_netns_fd >= 0); |
| 857 |
|
| 858 |
if (strv_isempty(interface_pairs)) |
| 859 |
return 0; |
| 860 |
|
| 861 |
r = netns_fork_and_wait(child_netns_fd, &parent_netns_fd); |
| 862 |
if (r < 0) |
| 863 |
return r; |
| 864 |
if (r == 0) { |
| 865 |
r = remove_macvlan_impl(interface_pairs); |
| 866 |
_exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
| 867 |
} |
| 868 |
|
| 869 |
return 0; |
| 870 |
} |
| 871 |
|
| 872 |
int setup_ipvlan(const char *machine_name, const PidRef *pid, char **iface_pairs) { |
| 873 |
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
| 874 |
int r; |
| 875 |
|
| 876 |
assert(pidref_is_set(pid)); |
| 877 |
|
| 878 |
if (strv_isempty(iface_pairs)) |
| 879 |
return 0; |
| 880 |
|
| 881 |
r = sd_netlink_open(&rtnl); |
| 882 |
if (r < 0) |
| 883 |
return log_error_errno(r, "Failed to connect to netlink: %m"); |
| 884 |
|
| 885 |
STRV_FOREACH_PAIR(i, b, iface_pairs) { |
| 886 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 887 |
_cleanup_free_ char *n = NULL; |
| 888 |
int shortened, ifi ; |
| 889 |
|
| 890 |
ifi = rtnl_resolve_interface_or_warn(&rtnl, *i); |
| 891 |
if (ifi < 0) |
| 892 |
return ifi; |
| 893 |
|
| 894 |
r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0); |
| 895 |
if (r < 0) |
| 896 |
return log_error_errno(r, "Failed to allocate netlink message: %m"); |
| 897 |
|
| 898 |
r = sd_netlink_message_append_u32(m, IFLA_LINK, ifi); |
| 899 |
if (r < 0) |
| 900 |
return log_error_errno(r, "Failed to add netlink interface index: %m"); |
| 901 |
|
| 902 |
n = strdup(*b); |
| 903 |
if (!n) |
| 904 |
return log_oom(); |
| 905 |
|
| 906 |
shortened = net_shorten_ifname(n, /* check_naming_scheme= */ true); |
| 907 |
|
| 908 |
r = sd_netlink_message_append_string(m, IFLA_IFNAME, n); |
| 909 |
if (r < 0) |
| 910 |
return log_error_errno(r, "Failed to add netlink interface name: %m"); |
| 911 |
|
| 912 |
r = sd_netlink_message_append_u32(m, IFLA_NET_NS_PID, pid->pid); |
| 913 |
if (r < 0) |
| 914 |
return log_error_errno(r, "Failed to add netlink namespace field: %m"); |
| 915 |
|
| 916 |
r = sd_netlink_message_open_container(m, IFLA_LINKINFO); |
| 917 |
if (r < 0) |
| 918 |
return log_error_errno(r, "Failed to open netlink container: %m"); |
| 919 |
|
| 920 |
r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "ipvlan"); |
| 921 |
if (r < 0) |
| 922 |
return log_error_errno(r, "Failed to open netlink container: %m"); |
| 923 |
|
| 924 |
r = sd_netlink_message_append_u16(m, IFLA_IPVLAN_MODE, IPVLAN_MODE_L2); |
| 925 |
if (r < 0) |
| 926 |
return log_error_errno(r, "Failed to add ipvlan mode: %m"); |
| 927 |
|
| 928 |
r = sd_netlink_message_close_container(m); |
| 929 |
if (r < 0) |
| 930 |
return log_error_errno(r, "Failed to close netlink container: %m"); |
| 931 |
|
| 932 |
r = sd_netlink_message_close_container(m); |
| 933 |
if (r < 0) |
| 934 |
return log_error_errno(r, "Failed to close netlink container: %m"); |
| 935 |
|
| 936 |
r = sd_netlink_call(rtnl, m, 0, NULL); |
| 937 |
if (r < 0) |
| 938 |
return log_error_errno(r, "Failed to add new ipvlan interfaces: %m"); |
| 939 |
|
| 940 |
if (shortened > 0) |
| 941 |
(void) set_alternative_ifname(rtnl, n, *b); |
| 942 |
} |
| 943 |
|
| 944 |
return 0; |
| 945 |
} |
| 946 |
|
| 947 |
int veth_extra_parse(char ***l, const char *p) { |
| 948 |
_cleanup_free_ char *a = NULL, *b = NULL; |
| 949 |
int r; |
| 950 |
|
| 951 |
r = extract_first_word(&p, &a, ":", EXTRACT_DONT_COALESCE_SEPARATORS); |
| 952 |
if (r < 0) |
| 953 |
return r; |
| 954 |
if (r == 0 || !ifname_valid(a)) |
| 955 |
return -EINVAL; |
| 956 |
|
| 957 |
r = extract_first_word(&p, &b, ":", EXTRACT_DONT_COALESCE_SEPARATORS); |
| 958 |
if (r < 0) |
| 959 |
return r; |
| 960 |
if (r == 0 || !ifname_valid(b)) { |
| 961 |
r = free_and_strdup(&b, a); |
| 962 |
if (r < 0) |
| 963 |
return r; |
| 964 |
} |
| 965 |
|
| 966 |
if (p) |
| 967 |
return -EINVAL; |
| 968 |
|
| 969 |
r = strv_push_pair(l, a, b); |
| 970 |
if (r < 0) |
| 971 |
return -ENOMEM; |
| 972 |
|
| 973 |
a = b = NULL; |
| 974 |
return 0; |
| 975 |
} |
| 976 |
|
| 977 |
int remove_veth_links(const char *primary, char **pairs) { |
| 978 |
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
| 979 |
int r; |
| 980 |
|
| 981 |
/* In some cases the kernel might pin the veth links between host and container even after the namespace |
| 982 |
* died. Hence, let's better remove them explicitly too. */ |
| 983 |
|
| 984 |
if (isempty(primary) && strv_isempty(pairs)) |
| 985 |
return 0; |
| 986 |
|
| 987 |
r = sd_netlink_open(&rtnl); |
| 988 |
if (r < 0) |
| 989 |
return log_error_errno(r, "Failed to connect to netlink: %m"); |
| 990 |
|
| 991 |
remove_one_link(rtnl, primary); |
| 992 |
|
| 993 |
STRV_FOREACH_PAIR(a, b, pairs) |
| 994 |
remove_one_link(rtnl, *a); |
| 995 |
|
| 996 |
return 0; |
| 997 |
} |
| 998 |
|
| 999 |
static int network_iface_pair_parse(const char* iftype, char ***l, const char *p, const char* ifprefix) { |
| 1000 |
int r; |
| 1001 |
|
| 1002 |
for (;;) { |
| 1003 |
_cleanup_free_ char *word = NULL, *a = NULL, *b = NULL; |
| 1004 |
const char *interface; |
| 1005 |
|
| 1006 |
r = extract_first_word(&p, &word, NULL, 0); |
| 1007 |
if (r < 0) |
| 1008 |
return log_error_errno(r, "Failed to parse interface name: %m"); |
| 1009 |
if (r == 0) |
| 1010 |
break; |
| 1011 |
|
| 1012 |
interface = word; |
| 1013 |
r = extract_first_word(&interface, &a, ":", EXTRACT_DONT_COALESCE_SEPARATORS); |
| 1014 |
if (r < 0) |
| 1015 |
return log_error_errno(r, "Failed to extract first word in %s parameter: %m", iftype); |
| 1016 |
if (r == 0) |
| 1017 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 1018 |
"Short read while reading %s parameter.", iftype); |
| 1019 |
if (!ifname_valid(a)) |
| 1020 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 1021 |
"%s, interface name not valid: %s", iftype, a); |
| 1022 |
|
| 1023 |
/* Here, we only check the validity of the specified second name. If it is not specified, |
| 1024 |
* the copied or prefixed name should be already valid, except for its length. If it is too |
| 1025 |
* long, then it will be shortened later. */ |
| 1026 |
if (!isempty(interface)) { |
| 1027 |
if (!ifname_valid(interface)) |
| 1028 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 1029 |
"%s, interface name not valid: %s", iftype, interface); |
| 1030 |
|
| 1031 |
b = strdup(interface); |
| 1032 |
} else if (ifprefix) |
| 1033 |
b = strjoin(ifprefix, a); |
| 1034 |
else |
| 1035 |
b = strdup(a); |
| 1036 |
if (!b) |
| 1037 |
return log_oom(); |
| 1038 |
|
| 1039 |
r = strv_consume_pair(l, TAKE_PTR(a), TAKE_PTR(b)); |
| 1040 |
if (r < 0) |
| 1041 |
return log_oom(); |
| 1042 |
} |
| 1043 |
|
| 1044 |
return 0; |
| 1045 |
} |
| 1046 |
|
| 1047 |
int interface_pair_parse(char ***l, const char *p) { |
| 1048 |
return network_iface_pair_parse("Network interface", l, p, NULL); |
| 1049 |
} |
| 1050 |
|
| 1051 |
int macvlan_pair_parse(char ***l, const char *p) { |
| 1052 |
return network_iface_pair_parse("MACVLAN network interface", l, p, "mv-"); |
| 1053 |
} |
| 1054 |
|
| 1055 |
int ipvlan_pair_parse(char ***l, const char *p) { |
| 1056 |
return network_iface_pair_parse("IPVLAN network interface", l, p, "iv-"); |
| 1057 |
} |