]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-network.c
nspawn: switch to BusLocator-oriented helpers
[thirdparty/systemd.git] / src / nspawn / nspawn-network.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <net/if.h>
4 #include <linux/if.h>
5 #include <linux/veth.h>
6 #include <sys/file.h>
7
8 #include "sd-device.h"
9 #include "sd-id128.h"
10 #include "sd-netlink.h"
11
12 #include "alloc-util.h"
13 #include "ether-addr-util.h"
14 #include "lockfile-util.h"
15 #include "missing_network.h"
16 #include "netif-naming-scheme.h"
17 #include "netlink-util.h"
18 #include "nspawn-network.h"
19 #include "parse-util.h"
20 #include "siphash24.h"
21 #include "socket-netlink.h"
22 #include "socket-util.h"
23 #include "stat-util.h"
24 #include "string-util.h"
25 #include "strv.h"
26 #include "udev-util.h"
27 #include "util.h"
28
29 #define HOST_HASH_KEY SD_ID128_MAKE(1a,37,6f,c7,46,ec,45,0b,ad,a3,d5,31,06,60,5d,b1)
30 #define CONTAINER_HASH_KEY SD_ID128_MAKE(c3,c4,f9,19,b5,57,b2,1c,e6,cf,14,27,03,9c,ee,a2)
31 #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)
32 #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)
33 #define MACVLAN_HASH_KEY SD_ID128_MAKE(00,13,6d,bc,66,83,44,81,bb,0c,f9,51,1f,24,a6,6f)
34 #define SHORTEN_IFNAME_HASH_KEY SD_ID128_MAKE(e1,90,a4,04,a8,ef,4b,51,8c,cc,c3,3a,9f,11,fc,a2)
35
36 static int remove_one_link(sd_netlink *rtnl, const char *name) {
37 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
38 int r;
39
40 if (isempty(name))
41 return 0;
42
43 r = sd_rtnl_message_new_link(rtnl, &m, RTM_DELLINK, 0);
44 if (r < 0)
45 return log_error_errno(r, "Failed to allocate netlink message: %m");
46
47 r = sd_netlink_message_append_string(m, IFLA_IFNAME, name);
48 if (r < 0)
49 return log_error_errno(r, "Failed to add netlink interface name: %m");
50
51 r = sd_netlink_call(rtnl, m, 0, NULL);
52 if (r == -ENODEV) /* Already gone */
53 return 0;
54 if (r < 0)
55 return log_error_errno(r, "Failed to remove interface %s: %m", name);
56
57 return 1;
58 }
59
60 static int generate_mac(
61 const char *machine_name,
62 struct ether_addr *mac,
63 sd_id128_t hash_key,
64 uint64_t idx) {
65
66 uint64_t result;
67 size_t l, sz;
68 uint8_t *v, *i;
69 int r;
70
71 l = strlen(machine_name);
72 sz = sizeof(sd_id128_t) + l;
73 if (idx > 0)
74 sz += sizeof(idx);
75
76 v = newa(uint8_t, sz);
77
78 /* fetch some persistent data unique to the host */
79 r = sd_id128_get_machine((sd_id128_t*) v);
80 if (r < 0)
81 return r;
82
83 /* combine with some data unique (on this host) to this
84 * container instance */
85 i = mempcpy(v + sizeof(sd_id128_t), machine_name, l);
86 if (idx > 0) {
87 idx = htole64(idx);
88 memcpy(i, &idx, sizeof(idx));
89 }
90
91 /* Let's hash the host machine ID plus the container name. We
92 * use a fixed, but originally randomly created hash key here. */
93 result = htole64(siphash24(v, sz, hash_key.bytes));
94
95 assert_cc(ETH_ALEN <= sizeof(result));
96 memcpy(mac->ether_addr_octet, &result, ETH_ALEN);
97
98 /* see eth_random_addr in the kernel */
99 mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
100 mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
101
102 return 0;
103 }
104
105 static int set_alternative_ifname(sd_netlink *rtnl, const char *ifname, const char *altifname) {
106 int r;
107
108 assert(rtnl);
109 assert(ifname);
110
111 if (!altifname)
112 return 0;
113
114 if (strlen(altifname) >= ALTIFNAMSIZ)
115 return log_warning_errno(SYNTHETIC_ERRNO(ERANGE),
116 "Alternative interface name '%s' for '%s' is too long, ignoring",
117 altifname, ifname);
118
119 r = rtnl_set_link_alternative_names_by_ifname(&rtnl, ifname, STRV_MAKE(altifname));
120 if (r < 0)
121 return log_warning_errno(r,
122 "Failed to set alternative interface name '%s' to '%s', ignoring: %m",
123 altifname, ifname);
124
125 return 0;
126 }
127
128 static int add_veth(
129 sd_netlink *rtnl,
130 pid_t pid,
131 const char *ifname_host,
132 const char *altifname_host,
133 const struct ether_addr *mac_host,
134 const char *ifname_container,
135 const struct ether_addr *mac_container) {
136
137 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
138 int r;
139
140 assert(rtnl);
141 assert(ifname_host);
142 assert(mac_host);
143 assert(ifname_container);
144 assert(mac_container);
145
146 r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0);
147 if (r < 0)
148 return log_error_errno(r, "Failed to allocate netlink message: %m");
149
150 r = sd_netlink_message_append_string(m, IFLA_IFNAME, ifname_host);
151 if (r < 0)
152 return log_error_errno(r, "Failed to add netlink interface name: %m");
153
154 r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, mac_host);
155 if (r < 0)
156 return log_error_errno(r, "Failed to add netlink MAC address: %m");
157
158 r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
159 if (r < 0)
160 return log_error_errno(r, "Failed to open netlink container: %m");
161
162 r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "veth");
163 if (r < 0)
164 return log_error_errno(r, "Failed to open netlink container: %m");
165
166 r = sd_netlink_message_open_container(m, VETH_INFO_PEER);
167 if (r < 0)
168 return log_error_errno(r, "Failed to open netlink container: %m");
169
170 r = sd_netlink_message_append_string(m, IFLA_IFNAME, ifname_container);
171 if (r < 0)
172 return log_error_errno(r, "Failed to add netlink interface name: %m");
173
174 r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, mac_container);
175 if (r < 0)
176 return log_error_errno(r, "Failed to add netlink MAC address: %m");
177
178 r = sd_netlink_message_append_u32(m, IFLA_NET_NS_PID, pid);
179 if (r < 0)
180 return log_error_errno(r, "Failed to add netlink namespace field: %m");
181
182 r = sd_netlink_message_close_container(m);
183 if (r < 0)
184 return log_error_errno(r, "Failed to close netlink container: %m");
185
186 r = sd_netlink_message_close_container(m);
187 if (r < 0)
188 return log_error_errno(r, "Failed to close netlink container: %m");
189
190 r = sd_netlink_message_close_container(m);
191 if (r < 0)
192 return log_error_errno(r, "Failed to close netlink container: %m");
193
194 r = sd_netlink_call(rtnl, m, 0, NULL);
195 if (r < 0)
196 return log_error_errno(r, "Failed to add new veth interfaces (%s:%s): %m", ifname_host, ifname_container);
197
198 (void) set_alternative_ifname(rtnl, ifname_host, altifname_host);
199
200 return 0;
201 }
202
203 /* This is almost base64char(), but not entirely, as it uses the "url and filename safe" alphabet, since we
204 * don't want "/" appear in interface names (since interfaces appear in sysfs as filenames). See section #5
205 * of RFC 4648. */
206 static char urlsafe_base64char(int x) {
207 static const char table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
208 "abcdefghijklmnopqrstuvwxyz"
209 "0123456789-_";
210 return table[x & 63];
211 }
212
213 static int shorten_ifname(char *ifname) {
214 char new_ifname[IFNAMSIZ];
215
216 assert(ifname);
217
218 if (strlen(ifname) < IFNAMSIZ) /* Name is short enough */
219 return 0;
220
221 if (naming_scheme_has(NAMING_NSPAWN_LONG_HASH)) {
222 uint64_t h;
223
224 /* Calculate 64bit hash value */
225 h = siphash24(ifname, strlen(ifname), SHORTEN_IFNAME_HASH_KEY.bytes);
226
227 /* Set the final four bytes (i.e. 32bit) to the lower 24bit of the hash, encoded in url-safe base64 */
228 memcpy(new_ifname, ifname, IFNAMSIZ - 5);
229 new_ifname[IFNAMSIZ - 5] = urlsafe_base64char(h >> 18);
230 new_ifname[IFNAMSIZ - 4] = urlsafe_base64char(h >> 12);
231 new_ifname[IFNAMSIZ - 3] = urlsafe_base64char(h >> 6);
232 new_ifname[IFNAMSIZ - 2] = urlsafe_base64char(h);
233 } else
234 /* On old nspawn versions we just truncated the name, provide compatibility */
235 memcpy(new_ifname, ifname, IFNAMSIZ-1);
236
237 new_ifname[IFNAMSIZ - 1] = 0;
238
239 /* Log the incident to make it more discoverable */
240 log_warning("Network interface name '%s' has been changed to '%s' to fit length constraints.", ifname, new_ifname);
241
242 strcpy(ifname, new_ifname);
243 return 1;
244 }
245
246 int setup_veth(const char *machine_name,
247 pid_t pid,
248 char iface_name[IFNAMSIZ],
249 bool bridge) {
250
251 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
252 struct ether_addr mac_host, mac_container;
253 unsigned u;
254 char *n, *a = NULL;
255 int r;
256
257 assert(machine_name);
258 assert(pid > 0);
259 assert(iface_name);
260
261 /* Use two different interface name prefixes depending whether
262 * we are in bridge mode or not. */
263 n = strjoina(bridge ? "vb-" : "ve-", machine_name);
264 r = shorten_ifname(n);
265 if (r > 0)
266 a = strjoina(bridge ? "vb-" : "ve-", machine_name);
267
268 r = generate_mac(machine_name, &mac_container, CONTAINER_HASH_KEY, 0);
269 if (r < 0)
270 return log_error_errno(r, "Failed to generate predictable MAC address for container side: %m");
271
272 r = generate_mac(machine_name, &mac_host, HOST_HASH_KEY, 0);
273 if (r < 0)
274 return log_error_errno(r, "Failed to generate predictable MAC address for host side: %m");
275
276 r = sd_netlink_open(&rtnl);
277 if (r < 0)
278 return log_error_errno(r, "Failed to connect to netlink: %m");
279
280 r = add_veth(rtnl, pid, n, a, &mac_host, "host0", &mac_container);
281 if (r < 0)
282 return r;
283
284 u = if_nametoindex(n); /* We don't need to use resolve_ifname() here because the
285 * name we assigned is always the main name. */
286 if (u == 0)
287 return log_error_errno(errno, "Failed to resolve interface %s: %m", n);
288
289 strcpy(iface_name, n);
290 return (int) u;
291 }
292
293 int setup_veth_extra(
294 const char *machine_name,
295 pid_t pid,
296 char **pairs) {
297
298 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
299 uint64_t idx = 0;
300 char **a, **b;
301 int r;
302
303 assert(machine_name);
304 assert(pid > 0);
305
306 if (strv_isempty(pairs))
307 return 0;
308
309 r = sd_netlink_open(&rtnl);
310 if (r < 0)
311 return log_error_errno(r, "Failed to connect to netlink: %m");
312
313 STRV_FOREACH_PAIR(a, b, pairs) {
314 struct ether_addr mac_host, mac_container;
315
316 r = generate_mac(machine_name, &mac_container, VETH_EXTRA_CONTAINER_HASH_KEY, idx);
317 if (r < 0)
318 return log_error_errno(r, "Failed to generate predictable MAC address for container side of extra veth link: %m");
319
320 r = generate_mac(machine_name, &mac_host, VETH_EXTRA_HOST_HASH_KEY, idx);
321 if (r < 0)
322 return log_error_errno(r, "Failed to generate predictable MAC address for host side of extra veth link: %m");
323
324 r = add_veth(rtnl, pid, *a, NULL, &mac_host, *b, &mac_container);
325 if (r < 0)
326 return r;
327
328 idx++;
329 }
330
331 return 0;
332 }
333
334 static int join_bridge(sd_netlink *rtnl, const char *veth_name, const char *bridge_name) {
335 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
336 int r, bridge_ifi;
337
338 assert(rtnl);
339 assert(veth_name);
340 assert(bridge_name);
341
342 bridge_ifi = resolve_interface(&rtnl, bridge_name);
343 if (bridge_ifi < 0)
344 return bridge_ifi;
345
346 r = sd_rtnl_message_new_link(rtnl, &m, RTM_SETLINK, 0);
347 if (r < 0)
348 return r;
349
350 r = sd_rtnl_message_link_set_flags(m, IFF_UP, IFF_UP);
351 if (r < 0)
352 return r;
353
354 r = sd_netlink_message_append_string(m, IFLA_IFNAME, veth_name);
355 if (r < 0)
356 return r;
357
358 r = sd_netlink_message_append_u32(m, IFLA_MASTER, bridge_ifi);
359 if (r < 0)
360 return r;
361
362 r = sd_netlink_call(rtnl, m, 0, NULL);
363 if (r < 0)
364 return r;
365
366 return bridge_ifi;
367 }
368
369 static int create_bridge(sd_netlink *rtnl, const char *bridge_name) {
370 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
371 int r;
372
373 r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0);
374 if (r < 0)
375 return r;
376
377 r = sd_netlink_message_append_string(m, IFLA_IFNAME, bridge_name);
378 if (r < 0)
379 return r;
380
381 r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
382 if (r < 0)
383 return r;
384
385 r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "bridge");
386 if (r < 0)
387 return r;
388
389 r = sd_netlink_message_close_container(m);
390 if (r < 0)
391 return r;
392
393 r = sd_netlink_message_close_container(m);
394 if (r < 0)
395 return r;
396
397 r = sd_netlink_call(rtnl, m, 0, NULL);
398 if (r < 0)
399 return r;
400
401 return 0;
402 }
403
404 int setup_bridge(const char *veth_name, const char *bridge_name, bool create) {
405 _cleanup_(release_lock_file) LockFile bridge_lock = LOCK_FILE_INIT;
406 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
407 int r, bridge_ifi;
408 unsigned n = 0;
409
410 assert(veth_name);
411 assert(bridge_name);
412
413 r = sd_netlink_open(&rtnl);
414 if (r < 0)
415 return log_error_errno(r, "Failed to connect to netlink: %m");
416
417 if (create) {
418 /* We take a system-wide lock here, so that we can safely check whether there's still a member in the
419 * bridge before removing it, without risking interference from other nspawn instances. */
420
421 r = make_lock_file("/run/systemd/nspawn-network-zone", LOCK_EX, &bridge_lock);
422 if (r < 0)
423 return log_error_errno(r, "Failed to take network zone lock: %m");
424 }
425
426 for (;;) {
427 bridge_ifi = join_bridge(rtnl, veth_name, bridge_name);
428 if (bridge_ifi >= 0)
429 return bridge_ifi;
430 if (bridge_ifi != -ENODEV || !create || n > 10)
431 return log_error_errno(bridge_ifi, "Failed to add interface %s to bridge %s: %m", veth_name, bridge_name);
432
433 /* Count attempts, so that we don't enter an endless loop here. */
434 n++;
435
436 /* The bridge doesn't exist yet. Let's create it */
437 r = create_bridge(rtnl, bridge_name);
438 if (r < 0)
439 return log_error_errno(r, "Failed to create bridge interface %s: %m", bridge_name);
440
441 /* Try again, now that the bridge exists */
442 }
443 }
444
445 int remove_bridge(const char *bridge_name) {
446 _cleanup_(release_lock_file) LockFile bridge_lock = LOCK_FILE_INIT;
447 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
448 const char *path;
449 int r;
450
451 /* Removes the specified bridge, but only if it is currently empty */
452
453 if (isempty(bridge_name))
454 return 0;
455
456 r = make_lock_file("/run/systemd/nspawn-network-zone", LOCK_EX, &bridge_lock);
457 if (r < 0)
458 return log_error_errno(r, "Failed to take network zone lock: %m");
459
460 path = strjoina("/sys/class/net/", bridge_name, "/brif");
461
462 r = dir_is_empty(path);
463 if (r == -ENOENT) /* Already gone? */
464 return 0;
465 if (r < 0)
466 return log_error_errno(r, "Can't detect if bridge %s is empty: %m", bridge_name);
467 if (r == 0) /* Still populated, leave it around */
468 return 0;
469
470 r = sd_netlink_open(&rtnl);
471 if (r < 0)
472 return log_error_errno(r, "Failed to connect to netlink: %m");
473
474 return remove_one_link(rtnl, bridge_name);
475 }
476
477 int test_network_interface_initialized(const char *name) {
478 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
479 int ifi, r;
480 char ifi_str[2 + DECIMAL_STR_MAX(int)];
481
482 if (path_is_read_only_fs("/sys"))
483 return 0;
484
485 /* udev should be around. */
486
487 ifi = resolve_interface_or_warn(NULL, name);
488 if (ifi < 0)
489 return ifi;
490
491 sprintf(ifi_str, "n%i", ifi);
492 r = sd_device_new_from_device_id(&d, ifi_str);
493 if (r < 0)
494 return log_error_errno(r, "Failed to get device %s: %m", name);
495
496 r = sd_device_get_is_initialized(d);
497 if (r < 0)
498 return log_error_errno(r, "Failed to determine whether interface %s is initialized: %m", name);
499 if (r == 0)
500 return log_error_errno(SYNTHETIC_ERRNO(EBUSY), "Network interface %s is not initialized yet.", name);
501
502 r = device_is_renaming(d);
503 if (r < 0)
504 return log_error_errno(r, "Failed to determine the interface %s is being renamed: %m", name);
505 if (r > 0)
506 return log_error_errno(SYNTHETIC_ERRNO(EBUSY), "Interface %s is being renamed.", name);
507
508 return 0;
509 }
510
511 int move_network_interfaces(int netns_fd, char **ifaces) {
512 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
513 char **i;
514 int r;
515
516 if (strv_isempty(ifaces))
517 return 0;
518
519 r = sd_netlink_open(&rtnl);
520 if (r < 0)
521 return log_error_errno(r, "Failed to connect to netlink: %m");
522
523 STRV_FOREACH(i, ifaces) {
524 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
525 int ifi;
526
527 ifi = resolve_interface_or_warn(&rtnl, *i);
528 if (ifi < 0)
529 return ifi;
530
531 r = sd_rtnl_message_new_link(rtnl, &m, RTM_SETLINK, ifi);
532 if (r < 0)
533 return log_error_errno(r, "Failed to allocate netlink message: %m");
534
535 r = sd_netlink_message_append_u32(m, IFLA_NET_NS_FD, netns_fd);
536 if (r < 0)
537 return log_error_errno(r, "Failed to append namespace fd to netlink message: %m");
538
539 r = sd_netlink_call(rtnl, m, 0, NULL);
540 if (r < 0)
541 return log_error_errno(r, "Failed to move interface %s to namespace: %m", *i);
542 }
543
544 return 0;
545 }
546
547 int setup_macvlan(const char *machine_name, pid_t pid, char **ifaces) {
548 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
549 unsigned idx = 0;
550 char **i;
551 int r;
552
553 if (strv_isempty(ifaces))
554 return 0;
555
556 r = sd_netlink_open(&rtnl);
557 if (r < 0)
558 return log_error_errno(r, "Failed to connect to netlink: %m");
559
560 STRV_FOREACH(i, ifaces) {
561 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
562 _cleanup_free_ char *n = NULL, *a = NULL;
563 struct ether_addr mac;
564 int ifi;
565
566 ifi = resolve_interface_or_warn(&rtnl, *i);
567 if (ifi < 0)
568 return ifi;
569
570 r = generate_mac(machine_name, &mac, MACVLAN_HASH_KEY, idx++);
571 if (r < 0)
572 return log_error_errno(r, "Failed to create MACVLAN MAC address: %m");
573
574 r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0);
575 if (r < 0)
576 return log_error_errno(r, "Failed to allocate netlink message: %m");
577
578 r = sd_netlink_message_append_u32(m, IFLA_LINK, ifi);
579 if (r < 0)
580 return log_error_errno(r, "Failed to add netlink interface index: %m");
581
582 n = strjoin("mv-", *i);
583 if (!n)
584 return log_oom();
585
586 r = shorten_ifname(n);
587 if (r > 0) {
588 a = strjoin("mv-", *i);
589 if (!a)
590 return log_oom();
591 }
592
593 r = sd_netlink_message_append_string(m, IFLA_IFNAME, n);
594 if (r < 0)
595 return log_error_errno(r, "Failed to add netlink interface name: %m");
596
597 r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, &mac);
598 if (r < 0)
599 return log_error_errno(r, "Failed to add netlink MAC address: %m");
600
601 r = sd_netlink_message_append_u32(m, IFLA_NET_NS_PID, pid);
602 if (r < 0)
603 return log_error_errno(r, "Failed to add netlink namespace field: %m");
604
605 r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
606 if (r < 0)
607 return log_error_errno(r, "Failed to open netlink container: %m");
608
609 r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "macvlan");
610 if (r < 0)
611 return log_error_errno(r, "Failed to open netlink container: %m");
612
613 r = sd_netlink_message_append_u32(m, IFLA_MACVLAN_MODE, MACVLAN_MODE_BRIDGE);
614 if (r < 0)
615 return log_error_errno(r, "Failed to append macvlan mode: %m");
616
617 r = sd_netlink_message_close_container(m);
618 if (r < 0)
619 return log_error_errno(r, "Failed to close netlink container: %m");
620
621 r = sd_netlink_message_close_container(m);
622 if (r < 0)
623 return log_error_errno(r, "Failed to close netlink container: %m");
624
625 r = sd_netlink_call(rtnl, m, 0, NULL);
626 if (r < 0)
627 return log_error_errno(r, "Failed to add new macvlan interfaces: %m");
628
629 (void) set_alternative_ifname(rtnl, n, a);
630 }
631
632 return 0;
633 }
634
635 int setup_ipvlan(const char *machine_name, pid_t pid, char **ifaces) {
636 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
637 char **i;
638 int r;
639
640 if (strv_isempty(ifaces))
641 return 0;
642
643 r = sd_netlink_open(&rtnl);
644 if (r < 0)
645 return log_error_errno(r, "Failed to connect to netlink: %m");
646
647 STRV_FOREACH(i, ifaces) {
648 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
649 _cleanup_free_ char *n = NULL, *a = NULL;
650 int ifi;
651
652 ifi = resolve_interface_or_warn(&rtnl, *i);
653 if (ifi < 0)
654 return ifi;
655
656 r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0);
657 if (r < 0)
658 return log_error_errno(r, "Failed to allocate netlink message: %m");
659
660 r = sd_netlink_message_append_u32(m, IFLA_LINK, ifi);
661 if (r < 0)
662 return log_error_errno(r, "Failed to add netlink interface index: %m");
663
664 n = strjoin("iv-", *i);
665 if (!n)
666 return log_oom();
667
668 r = shorten_ifname(n);
669 if (r > 0) {
670 a = strjoin("iv-", *i);
671 if (!a)
672 return log_oom();
673 }
674
675 r = sd_netlink_message_append_string(m, IFLA_IFNAME, n);
676 if (r < 0)
677 return log_error_errno(r, "Failed to add netlink interface name: %m");
678
679 r = sd_netlink_message_append_u32(m, IFLA_NET_NS_PID, pid);
680 if (r < 0)
681 return log_error_errno(r, "Failed to add netlink namespace field: %m");
682
683 r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
684 if (r < 0)
685 return log_error_errno(r, "Failed to open netlink container: %m");
686
687 r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "ipvlan");
688 if (r < 0)
689 return log_error_errno(r, "Failed to open netlink container: %m");
690
691 r = sd_netlink_message_append_u16(m, IFLA_IPVLAN_MODE, IPVLAN_MODE_L2);
692 if (r < 0)
693 return log_error_errno(r, "Failed to add ipvlan mode: %m");
694
695 r = sd_netlink_message_close_container(m);
696 if (r < 0)
697 return log_error_errno(r, "Failed to close netlink container: %m");
698
699 r = sd_netlink_message_close_container(m);
700 if (r < 0)
701 return log_error_errno(r, "Failed to close netlink container: %m");
702
703 r = sd_netlink_call(rtnl, m, 0, NULL);
704 if (r < 0)
705 return log_error_errno(r, "Failed to add new ipvlan interfaces: %m");
706
707 (void) set_alternative_ifname(rtnl, n, a);
708 }
709
710 return 0;
711 }
712
713 int veth_extra_parse(char ***l, const char *p) {
714 _cleanup_free_ char *a = NULL, *b = NULL;
715 int r;
716
717 r = extract_first_word(&p, &a, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
718 if (r < 0)
719 return r;
720 if (r == 0 || !ifname_valid(a))
721 return -EINVAL;
722
723 r = extract_first_word(&p, &b, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
724 if (r < 0)
725 return r;
726 if (r == 0 || !ifname_valid(b)) {
727 free(b);
728 b = strdup(a);
729 if (!b)
730 return -ENOMEM;
731 }
732
733 if (p)
734 return -EINVAL;
735
736 r = strv_push_pair(l, a, b);
737 if (r < 0)
738 return -ENOMEM;
739
740 a = b = NULL;
741 return 0;
742 }
743
744 int remove_veth_links(const char *primary, char **pairs) {
745 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
746 char **a, **b;
747 int r;
748
749 /* In some cases the kernel might pin the veth links between host and container even after the namespace
750 * died. Hence, let's better remove them explicitly too. */
751
752 if (isempty(primary) && strv_isempty(pairs))
753 return 0;
754
755 r = sd_netlink_open(&rtnl);
756 if (r < 0)
757 return log_error_errno(r, "Failed to connect to netlink: %m");
758
759 remove_one_link(rtnl, primary);
760
761 STRV_FOREACH_PAIR(a, b, pairs)
762 remove_one_link(rtnl, *a);
763
764 return 0;
765 }