]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-network.c
tree-wide: "<n>bit" → "<n>-bit"
[thirdparty/systemd.git] / src / nspawn / nspawn-network.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 "hexdecoct.h"
15 #include "lock-util.h"
16 #include "missing_network.h"
17 #include "netif-naming-scheme.h"
18 #include "netlink-util.h"
19 #include "nspawn-network.h"
20 #include "parse-util.h"
21 #include "siphash24.h"
22 #include "socket-netlink.h"
23 #include "socket-util.h"
24 #include "stat-util.h"
25 #include "string-util.h"
26 #include "strv.h"
27 #include "udev-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 static int shorten_ifname(char *ifname) {
204 char new_ifname[IFNAMSIZ];
205
206 assert(ifname);
207
208 if (strlen(ifname) < IFNAMSIZ) /* Name is short enough */
209 return 0;
210
211 if (naming_scheme_has(NAMING_NSPAWN_LONG_HASH)) {
212 uint64_t h;
213
214 /* Calculate 64-bit hash value */
215 h = siphash24(ifname, strlen(ifname), SHORTEN_IFNAME_HASH_KEY.bytes);
216
217 /* Set the final four bytes (i.e. 32-bit) to the lower 24bit of the hash, encoded in url-safe base64 */
218 memcpy(new_ifname, ifname, IFNAMSIZ - 5);
219 new_ifname[IFNAMSIZ - 5] = urlsafe_base64char(h >> 18);
220 new_ifname[IFNAMSIZ - 4] = urlsafe_base64char(h >> 12);
221 new_ifname[IFNAMSIZ - 3] = urlsafe_base64char(h >> 6);
222 new_ifname[IFNAMSIZ - 2] = urlsafe_base64char(h);
223 } else
224 /* On old nspawn versions we just truncated the name, provide compatibility */
225 memcpy(new_ifname, ifname, IFNAMSIZ-1);
226
227 new_ifname[IFNAMSIZ - 1] = 0;
228
229 /* Log the incident to make it more discoverable */
230 log_warning("Network interface name '%s' has been changed to '%s' to fit length constraints.", ifname, new_ifname);
231
232 strcpy(ifname, new_ifname);
233 return 1;
234 }
235
236 int setup_veth(const char *machine_name,
237 pid_t pid,
238 char iface_name[IFNAMSIZ],
239 bool bridge) {
240
241 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
242 struct ether_addr mac_host, mac_container;
243 unsigned u;
244 char *n, *a = NULL;
245 int r;
246
247 assert(machine_name);
248 assert(pid > 0);
249 assert(iface_name);
250
251 /* Use two different interface name prefixes depending whether
252 * we are in bridge mode or not. */
253 n = strjoina(bridge ? "vb-" : "ve-", machine_name);
254 r = shorten_ifname(n);
255 if (r > 0)
256 a = strjoina(bridge ? "vb-" : "ve-", machine_name);
257
258 r = generate_mac(machine_name, &mac_container, CONTAINER_HASH_KEY, 0);
259 if (r < 0)
260 return log_error_errno(r, "Failed to generate predictable MAC address for container side: %m");
261
262 r = generate_mac(machine_name, &mac_host, HOST_HASH_KEY, 0);
263 if (r < 0)
264 return log_error_errno(r, "Failed to generate predictable MAC address for host side: %m");
265
266 r = sd_netlink_open(&rtnl);
267 if (r < 0)
268 return log_error_errno(r, "Failed to connect to netlink: %m");
269
270 r = add_veth(rtnl, pid, n, a, &mac_host, "host0", &mac_container);
271 if (r < 0)
272 return r;
273
274 u = if_nametoindex(n); /* We don't need to use rtnl_resolve_ifname() here because the
275 * name we assigned is always the main name. */
276 if (u == 0)
277 return log_error_errno(errno, "Failed to resolve interface %s: %m", n);
278
279 strcpy(iface_name, n);
280 return (int) u;
281 }
282
283 int setup_veth_extra(
284 const char *machine_name,
285 pid_t pid,
286 char **pairs) {
287
288 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
289 uint64_t idx = 0;
290 int r;
291
292 assert(machine_name);
293 assert(pid > 0);
294
295 if (strv_isempty(pairs))
296 return 0;
297
298 r = sd_netlink_open(&rtnl);
299 if (r < 0)
300 return log_error_errno(r, "Failed to connect to netlink: %m");
301
302 STRV_FOREACH_PAIR(a, b, pairs) {
303 struct ether_addr mac_host, mac_container;
304
305 r = generate_mac(machine_name, &mac_container, VETH_EXTRA_CONTAINER_HASH_KEY, idx);
306 if (r < 0)
307 return log_error_errno(r, "Failed to generate predictable MAC address for container side of extra veth link: %m");
308
309 r = generate_mac(machine_name, &mac_host, VETH_EXTRA_HOST_HASH_KEY, idx);
310 if (r < 0)
311 return log_error_errno(r, "Failed to generate predictable MAC address for host side of extra veth link: %m");
312
313 r = add_veth(rtnl, pid, *a, NULL, &mac_host, *b, &mac_container);
314 if (r < 0)
315 return r;
316
317 idx++;
318 }
319
320 return 0;
321 }
322
323 static int join_bridge(sd_netlink *rtnl, const char *veth_name, const char *bridge_name) {
324 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
325 int r, bridge_ifi;
326
327 assert(rtnl);
328 assert(veth_name);
329 assert(bridge_name);
330
331 bridge_ifi = rtnl_resolve_interface(&rtnl, bridge_name);
332 if (bridge_ifi < 0)
333 return bridge_ifi;
334
335 r = sd_rtnl_message_new_link(rtnl, &m, RTM_SETLINK, 0);
336 if (r < 0)
337 return r;
338
339 r = sd_rtnl_message_link_set_flags(m, IFF_UP, IFF_UP);
340 if (r < 0)
341 return r;
342
343 r = sd_netlink_message_append_string(m, IFLA_IFNAME, veth_name);
344 if (r < 0)
345 return r;
346
347 r = sd_netlink_message_append_u32(m, IFLA_MASTER, bridge_ifi);
348 if (r < 0)
349 return r;
350
351 r = sd_netlink_call(rtnl, m, 0, NULL);
352 if (r < 0)
353 return r;
354
355 return bridge_ifi;
356 }
357
358 static int create_bridge(sd_netlink *rtnl, const char *bridge_name) {
359 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
360 int r;
361
362 r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0);
363 if (r < 0)
364 return r;
365
366 r = sd_netlink_message_append_string(m, IFLA_IFNAME, bridge_name);
367 if (r < 0)
368 return r;
369
370 r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
371 if (r < 0)
372 return r;
373
374 r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "bridge");
375 if (r < 0)
376 return r;
377
378 r = sd_netlink_message_close_container(m);
379 if (r < 0)
380 return r;
381
382 r = sd_netlink_message_close_container(m);
383 if (r < 0)
384 return r;
385
386 r = sd_netlink_call(rtnl, m, 0, NULL);
387 if (r < 0)
388 return r;
389
390 return 0;
391 }
392
393 int setup_bridge(const char *veth_name, const char *bridge_name, bool create) {
394 _cleanup_(release_lock_file) LockFile bridge_lock = LOCK_FILE_INIT;
395 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
396 int r, bridge_ifi;
397 unsigned n = 0;
398
399 assert(veth_name);
400 assert(bridge_name);
401
402 r = sd_netlink_open(&rtnl);
403 if (r < 0)
404 return log_error_errno(r, "Failed to connect to netlink: %m");
405
406 if (create) {
407 /* We take a system-wide lock here, so that we can safely check whether there's still a member in the
408 * bridge before removing it, without risking interference from other nspawn instances. */
409
410 r = make_lock_file("/run/systemd/nspawn-network-zone", LOCK_EX, &bridge_lock);
411 if (r < 0)
412 return log_error_errno(r, "Failed to take network zone lock: %m");
413 }
414
415 for (;;) {
416 bridge_ifi = join_bridge(rtnl, veth_name, bridge_name);
417 if (bridge_ifi >= 0)
418 return bridge_ifi;
419 if (bridge_ifi != -ENODEV || !create || n > 10)
420 return log_error_errno(bridge_ifi, "Failed to add interface %s to bridge %s: %m", veth_name, bridge_name);
421
422 /* Count attempts, so that we don't enter an endless loop here. */
423 n++;
424
425 /* The bridge doesn't exist yet. Let's create it */
426 r = create_bridge(rtnl, bridge_name);
427 if (r < 0)
428 return log_error_errno(r, "Failed to create bridge interface %s: %m", bridge_name);
429
430 /* Try again, now that the bridge exists */
431 }
432 }
433
434 int remove_bridge(const char *bridge_name) {
435 _cleanup_(release_lock_file) LockFile bridge_lock = LOCK_FILE_INIT;
436 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
437 const char *path;
438 int r;
439
440 /* Removes the specified bridge, but only if it is currently empty */
441
442 if (isempty(bridge_name))
443 return 0;
444
445 r = make_lock_file("/run/systemd/nspawn-network-zone", LOCK_EX, &bridge_lock);
446 if (r < 0)
447 return log_error_errno(r, "Failed to take network zone lock: %m");
448
449 path = strjoina("/sys/class/net/", bridge_name, "/brif");
450
451 r = dir_is_empty(path, /* ignore_hidden_or_backup= */ false);
452 if (r == -ENOENT) /* Already gone? */
453 return 0;
454 if (r < 0)
455 return log_error_errno(r, "Can't detect if bridge %s is empty: %m", bridge_name);
456 if (r == 0) /* Still populated, leave it around */
457 return 0;
458
459 r = sd_netlink_open(&rtnl);
460 if (r < 0)
461 return log_error_errno(r, "Failed to connect to netlink: %m");
462
463 return remove_one_link(rtnl, bridge_name);
464 }
465
466 static int test_network_interface_initialized(const char *name) {
467 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
468 int r;
469
470 if (!udev_available())
471 return 0;
472
473 /* udev should be around. */
474
475 r = sd_device_new_from_ifname(&d, name);
476 if (r < 0)
477 return log_error_errno(r, "Failed to get device %s: %m", name);
478
479 r = sd_device_get_is_initialized(d);
480 if (r < 0)
481 return log_error_errno(r, "Failed to determine whether interface %s is initialized: %m", name);
482 if (r == 0)
483 return log_error_errno(SYNTHETIC_ERRNO(EBUSY), "Network interface %s is not initialized yet.", name);
484
485 r = device_is_renaming(d);
486 if (r < 0)
487 return log_error_errno(r, "Failed to determine the interface %s is being renamed: %m", name);
488 if (r > 0)
489 return log_error_errno(SYNTHETIC_ERRNO(EBUSY), "Interface %s is being renamed.", name);
490
491 return 0;
492 }
493
494 int test_network_interfaces_initialized(char **iface_pairs) {
495 int r;
496 STRV_FOREACH_PAIR(a, b, iface_pairs) {
497 r = test_network_interface_initialized(*a);
498 if (r < 0)
499 return r;
500 }
501 return 0;
502 }
503
504 int move_network_interfaces(int netns_fd, char **iface_pairs) {
505 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
506 int r;
507
508 if (strv_isempty(iface_pairs))
509 return 0;
510
511 r = sd_netlink_open(&rtnl);
512 if (r < 0)
513 return log_error_errno(r, "Failed to connect to netlink: %m");
514
515 STRV_FOREACH_PAIR(i, b, iface_pairs) {
516 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
517 int ifi;
518
519 ifi = rtnl_resolve_interface_or_warn(&rtnl, *i);
520 if (ifi < 0)
521 return ifi;
522
523 r = sd_rtnl_message_new_link(rtnl, &m, RTM_SETLINK, ifi);
524 if (r < 0)
525 return log_error_errno(r, "Failed to allocate netlink message: %m");
526
527 r = sd_netlink_message_append_u32(m, IFLA_NET_NS_FD, netns_fd);
528 if (r < 0)
529 return log_error_errno(r, "Failed to append namespace fd to netlink message: %m");
530
531 if (!streq(*b, *i)) {
532 r = sd_netlink_message_append_string(m, IFLA_IFNAME, *b);
533 if (r < 0)
534 return log_error_errno(r, "Failed to add netlink interface name: %m");
535 }
536
537 r = sd_netlink_call(rtnl, m, 0, NULL);
538 if (r < 0)
539 return log_error_errno(r, "Failed to move interface %s to namespace: %m", *i);
540 }
541
542 return 0;
543 }
544
545 int setup_macvlan(const char *machine_name, pid_t pid, char **iface_pairs) {
546 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
547 unsigned idx = 0;
548 int r;
549
550 if (strv_isempty(iface_pairs))
551 return 0;
552
553 r = sd_netlink_open(&rtnl);
554 if (r < 0)
555 return log_error_errno(r, "Failed to connect to netlink: %m");
556
557 STRV_FOREACH_PAIR(i, b, iface_pairs) {
558 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
559 _cleanup_free_ char *n = NULL;
560 int shortened, ifi;
561 struct ether_addr mac;
562
563 ifi = rtnl_resolve_interface_or_warn(&rtnl, *i);
564 if (ifi < 0)
565 return ifi;
566
567 r = generate_mac(machine_name, &mac, MACVLAN_HASH_KEY, idx++);
568 if (r < 0)
569 return log_error_errno(r, "Failed to create MACVLAN MAC address: %m");
570
571 r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0);
572 if (r < 0)
573 return log_error_errno(r, "Failed to allocate netlink message: %m");
574
575 r = sd_netlink_message_append_u32(m, IFLA_LINK, ifi);
576 if (r < 0)
577 return log_error_errno(r, "Failed to add netlink interface index: %m");
578
579 n = strdup(*b);
580 if (!n)
581 return log_oom();
582
583 shortened = shorten_ifname(n);
584
585 r = sd_netlink_message_append_string(m, IFLA_IFNAME, n);
586 if (r < 0)
587 return log_error_errno(r, "Failed to add netlink interface name: %m");
588
589 r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, &mac);
590 if (r < 0)
591 return log_error_errno(r, "Failed to add netlink MAC address: %m");
592
593 r = sd_netlink_message_append_u32(m, IFLA_NET_NS_PID, pid);
594 if (r < 0)
595 return log_error_errno(r, "Failed to add netlink namespace field: %m");
596
597 r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
598 if (r < 0)
599 return log_error_errno(r, "Failed to open netlink container: %m");
600
601 r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "macvlan");
602 if (r < 0)
603 return log_error_errno(r, "Failed to open netlink container: %m");
604
605 r = sd_netlink_message_append_u32(m, IFLA_MACVLAN_MODE, MACVLAN_MODE_BRIDGE);
606 if (r < 0)
607 return log_error_errno(r, "Failed to append macvlan mode: %m");
608
609 r = sd_netlink_message_close_container(m);
610 if (r < 0)
611 return log_error_errno(r, "Failed to close netlink container: %m");
612
613 r = sd_netlink_message_close_container(m);
614 if (r < 0)
615 return log_error_errno(r, "Failed to close netlink container: %m");
616
617 r = sd_netlink_call(rtnl, m, 0, NULL);
618 if (r < 0)
619 return log_error_errno(r, "Failed to add new macvlan interfaces: %m");
620
621 if (shortened > 0)
622 (void) set_alternative_ifname(rtnl, n, *b);
623 }
624
625 return 0;
626 }
627
628 int setup_ipvlan(const char *machine_name, pid_t pid, char **iface_pairs) {
629 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
630 int r;
631
632 if (strv_isempty(iface_pairs))
633 return 0;
634
635 r = sd_netlink_open(&rtnl);
636 if (r < 0)
637 return log_error_errno(r, "Failed to connect to netlink: %m");
638
639 STRV_FOREACH_PAIR(i, b, iface_pairs) {
640 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
641 _cleanup_free_ char *n = NULL;
642 int shortened, ifi ;
643
644 ifi = rtnl_resolve_interface_or_warn(&rtnl, *i);
645 if (ifi < 0)
646 return ifi;
647
648 r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0);
649 if (r < 0)
650 return log_error_errno(r, "Failed to allocate netlink message: %m");
651
652 r = sd_netlink_message_append_u32(m, IFLA_LINK, ifi);
653 if (r < 0)
654 return log_error_errno(r, "Failed to add netlink interface index: %m");
655
656 n = strdup(*b);
657 if (!n)
658 return log_oom();
659
660 shortened = shorten_ifname(n);
661
662 r = sd_netlink_message_append_string(m, IFLA_IFNAME, n);
663 if (r < 0)
664 return log_error_errno(r, "Failed to add netlink interface name: %m");
665
666 r = sd_netlink_message_append_u32(m, IFLA_NET_NS_PID, pid);
667 if (r < 0)
668 return log_error_errno(r, "Failed to add netlink namespace field: %m");
669
670 r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
671 if (r < 0)
672 return log_error_errno(r, "Failed to open netlink container: %m");
673
674 r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "ipvlan");
675 if (r < 0)
676 return log_error_errno(r, "Failed to open netlink container: %m");
677
678 r = sd_netlink_message_append_u16(m, IFLA_IPVLAN_MODE, IPVLAN_MODE_L2);
679 if (r < 0)
680 return log_error_errno(r, "Failed to add ipvlan mode: %m");
681
682 r = sd_netlink_message_close_container(m);
683 if (r < 0)
684 return log_error_errno(r, "Failed to close netlink container: %m");
685
686 r = sd_netlink_message_close_container(m);
687 if (r < 0)
688 return log_error_errno(r, "Failed to close netlink container: %m");
689
690 r = sd_netlink_call(rtnl, m, 0, NULL);
691 if (r < 0)
692 return log_error_errno(r, "Failed to add new ipvlan interfaces: %m");
693
694 if (shortened > 0)
695 (void) set_alternative_ifname(rtnl, n, *b);
696 }
697
698 return 0;
699 }
700
701 int veth_extra_parse(char ***l, const char *p) {
702 _cleanup_free_ char *a = NULL, *b = NULL;
703 int r;
704
705 r = extract_first_word(&p, &a, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
706 if (r < 0)
707 return r;
708 if (r == 0 || !ifname_valid(a))
709 return -EINVAL;
710
711 r = extract_first_word(&p, &b, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
712 if (r < 0)
713 return r;
714 if (r == 0 || !ifname_valid(b)) {
715 r = free_and_strdup(&b, a);
716 if (r < 0)
717 return r;
718 }
719
720 if (p)
721 return -EINVAL;
722
723 r = strv_push_pair(l, a, b);
724 if (r < 0)
725 return -ENOMEM;
726
727 a = b = NULL;
728 return 0;
729 }
730
731 int remove_veth_links(const char *primary, char **pairs) {
732 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
733 int r;
734
735 /* In some cases the kernel might pin the veth links between host and container even after the namespace
736 * died. Hence, let's better remove them explicitly too. */
737
738 if (isempty(primary) && strv_isempty(pairs))
739 return 0;
740
741 r = sd_netlink_open(&rtnl);
742 if (r < 0)
743 return log_error_errno(r, "Failed to connect to netlink: %m");
744
745 remove_one_link(rtnl, primary);
746
747 STRV_FOREACH_PAIR(a, b, pairs)
748 remove_one_link(rtnl, *a);
749
750 return 0;
751 }
752
753 static int network_iface_pair_parse(const char* iftype, char ***l, const char *p, const char* ifprefix) {
754 int r;
755
756 for (;;) {
757 _cleanup_free_ char *word = NULL, *a = NULL, *b = NULL;
758 const char *interface;
759
760 r = extract_first_word(&p, &word, NULL, 0);
761 if (r < 0)
762 return log_error_errno(r, "Failed to parse interface name: %m");
763 if (r == 0)
764 break;
765
766 interface = word;
767 r = extract_first_word(&interface, &a, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
768 if (r < 0)
769 return log_error_errno(r, "Failed to extract first word in %s parameter: %m", iftype);
770 if (r == 0)
771 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
772 "Short read while reading %s parameter: %m", iftype);
773 if (!ifname_valid(a))
774 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
775 "%s, interface name not valid: %s", iftype, a);
776
777 if (isempty(interface)) {
778 if (ifprefix)
779 b = strjoin(ifprefix, a);
780 else
781 b = strdup(a);
782 } else
783 b = strdup(interface);
784 if (!b)
785 return log_oom();
786
787 if (!ifname_valid(b))
788 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
789 "%s, interface name not valid: %s", iftype, b);
790
791 r = strv_consume_pair(l, TAKE_PTR(a), TAKE_PTR(b));
792 if (r < 0)
793 return log_oom();
794 }
795
796 return 0;
797 }
798
799 int interface_pair_parse(char ***l, const char *p) {
800 return network_iface_pair_parse("Network interface", l, p, NULL);
801 }
802
803 int macvlan_pair_parse(char ***l, const char *p) {
804 return network_iface_pair_parse("MACVLAN network interface", l, p, "mv-");
805 }
806
807 int ipvlan_pair_parse(char ***l, const char *p) {
808 return network_iface_pair_parse("IPVLAN network interface", l, p, "iv-");
809 }