]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/nspawn/nspawn-network.c
Merge pull request #30284 from YHNdnzj/fstab-wantedby-defaultdeps
[thirdparty/systemd.git] / src / nspawn / nspawn-network.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
9a2a5625 2
6b50cb5c
YW
3#include <net/if.h>
4#include <linux/if.h>
9a2a5625 5#include <linux/veth.h>
fe993888 6#include <sys/file.h>
9a2a5625 7
f55b0d3f 8#include "sd-device.h"
9a2a5625
LP
9#include "sd-id128.h"
10#include "sd-netlink.h"
9a2a5625 11
b5efdb8a 12#include "alloc-util.h"
9a2a5625 13#include "ether-addr-util.h"
130298ba 14#include "hexdecoct.h"
64e89f56 15#include "lock-util.h"
204f52e3 16#include "missing_network.h"
bc5ea049 17#include "netif-naming-scheme.h"
9a2a5625 18#include "netlink-util.h"
cf0fbc49 19#include "nspawn-network.h"
a0267b30 20#include "parse-util.h"
07630cea 21#include "siphash24.h"
d308bb99 22#include "socket-netlink.h"
ef76dff2
LP
23#include "socket-util.h"
24#include "stat-util.h"
07630cea 25#include "string-util.h"
f55b0d3f 26#include "strv.h"
26208d5b 27#include "udev-util.h"
9a2a5625
LP
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)
f6d6bad1
LP
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)
9a2a5625 33#define MACVLAN_HASH_KEY SD_ID128_MAKE(00,13,6d,bc,66,83,44,81,bb,0c,f9,51,1f,24,a6,6f)
bc5ea049 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)
9a2a5625 35
22b28dfd
LP
36static 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
9a2a5625
LP
60static int generate_mac(
61 const char *machine_name,
62 struct ether_addr *mac,
63 sd_id128_t hash_key,
64 uint64_t idx) {
65
dbe81cbd 66 uint64_t result;
9a2a5625
LP
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
6e9417f5 76 v = newa(uint8_t, sz);
9a2a5625
LP
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. */
933f9cae 93 result = htole64(siphash24(v, sz, hash_key.bytes));
9a2a5625
LP
94
95 assert_cc(ETH_ALEN <= sizeof(result));
dbe81cbd 96 memcpy(mac->ether_addr_octet, &result, ETH_ALEN);
9a2a5625 97
e22ca700 98 ether_addr_mark_random(mac);
9a2a5625
LP
99
100 return 0;
101}
102
6b50cb5c
YW
103static int set_alternative_ifname(sd_netlink *rtnl, const char *ifname, const char *altifname) {
104 int r;
105
106 assert(rtnl);
107 assert(ifname);
108
109 if (!altifname)
110 return 0;
111
112 if (strlen(altifname) >= ALTIFNAMSIZ)
113 return log_warning_errno(SYNTHETIC_ERRNO(ERANGE),
114 "Alternative interface name '%s' for '%s' is too long, ignoring",
115 altifname, ifname);
116
117 r = rtnl_set_link_alternative_names_by_ifname(&rtnl, ifname, STRV_MAKE(altifname));
118 if (r < 0)
119 return log_warning_errno(r,
120 "Failed to set alternative interface name '%s' to '%s', ignoring: %m",
121 altifname, ifname);
122
123 return 0;
124}
125
f6d6bad1
LP
126static int add_veth(
127 sd_netlink *rtnl,
128 pid_t pid,
129 const char *ifname_host,
6b50cb5c 130 const char *altifname_host,
f6d6bad1
LP
131 const struct ether_addr *mac_host,
132 const char *ifname_container,
133 const struct ether_addr *mac_container) {
9a2a5625 134
4afd3348 135 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
f6d6bad1 136 int r;
9a2a5625 137
f6d6bad1
LP
138 assert(rtnl);
139 assert(ifname_host);
140 assert(mac_host);
141 assert(ifname_container);
142 assert(mac_container);
9a2a5625
LP
143
144 r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0);
145 if (r < 0)
146 return log_error_errno(r, "Failed to allocate netlink message: %m");
147
f6d6bad1 148 r = sd_netlink_message_append_string(m, IFLA_IFNAME, ifname_host);
9a2a5625
LP
149 if (r < 0)
150 return log_error_errno(r, "Failed to add netlink interface name: %m");
151
f6d6bad1 152 r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, mac_host);
9a2a5625
LP
153 if (r < 0)
154 return log_error_errno(r, "Failed to add netlink MAC address: %m");
155
156 r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
157 if (r < 0)
158 return log_error_errno(r, "Failed to open netlink container: %m");
159
160 r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "veth");
161 if (r < 0)
162 return log_error_errno(r, "Failed to open netlink container: %m");
163
164 r = sd_netlink_message_open_container(m, VETH_INFO_PEER);
165 if (r < 0)
166 return log_error_errno(r, "Failed to open netlink container: %m");
167
f6d6bad1 168 r = sd_netlink_message_append_string(m, IFLA_IFNAME, ifname_container);
9a2a5625
LP
169 if (r < 0)
170 return log_error_errno(r, "Failed to add netlink interface name: %m");
171
f6d6bad1 172 r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, mac_container);
9a2a5625
LP
173 if (r < 0)
174 return log_error_errno(r, "Failed to add netlink MAC address: %m");
175
176 r = sd_netlink_message_append_u32(m, IFLA_NET_NS_PID, pid);
177 if (r < 0)
178 return log_error_errno(r, "Failed to add netlink namespace field: %m");
179
180 r = sd_netlink_message_close_container(m);
181 if (r < 0)
182 return log_error_errno(r, "Failed to close netlink container: %m");
183
184 r = sd_netlink_message_close_container(m);
185 if (r < 0)
186 return log_error_errno(r, "Failed to close netlink container: %m");
187
188 r = sd_netlink_message_close_container(m);
189 if (r < 0)
190 return log_error_errno(r, "Failed to close netlink container: %m");
191
192 r = sd_netlink_call(rtnl, m, 0, NULL);
193 if (r < 0)
f6d6bad1
LP
194 return log_error_errno(r, "Failed to add new veth interfaces (%s:%s): %m", ifname_host, ifname_container);
195
6b50cb5c
YW
196 (void) set_alternative_ifname(rtnl, ifname_host, altifname_host);
197
f6d6bad1
LP
198 return 0;
199}
200
6b50cb5c 201static int shorten_ifname(char *ifname) {
bc5ea049
KK
202 char new_ifname[IFNAMSIZ];
203
204 assert(ifname);
205
206 if (strlen(ifname) < IFNAMSIZ) /* Name is short enough */
6b50cb5c 207 return 0;
bc5ea049
KK
208
209 if (naming_scheme_has(NAMING_NSPAWN_LONG_HASH)) {
210 uint64_t h;
211
da890466 212 /* Calculate 64-bit hash value */
bc5ea049
KK
213 h = siphash24(ifname, strlen(ifname), SHORTEN_IFNAME_HASH_KEY.bytes);
214
da890466 215 /* Set the final four bytes (i.e. 32-bit) to the lower 24bit of the hash, encoded in url-safe base64 */
bc5ea049
KK
216 memcpy(new_ifname, ifname, IFNAMSIZ - 5);
217 new_ifname[IFNAMSIZ - 5] = urlsafe_base64char(h >> 18);
218 new_ifname[IFNAMSIZ - 4] = urlsafe_base64char(h >> 12);
219 new_ifname[IFNAMSIZ - 3] = urlsafe_base64char(h >> 6);
220 new_ifname[IFNAMSIZ - 2] = urlsafe_base64char(h);
221 } else
222 /* On old nspawn versions we just truncated the name, provide compatibility */
223 memcpy(new_ifname, ifname, IFNAMSIZ-1);
224
225 new_ifname[IFNAMSIZ - 1] = 0;
226
227 /* Log the incident to make it more discoverable */
228 log_warning("Network interface name '%s' has been changed to '%s' to fit length constraints.", ifname, new_ifname);
229
230 strcpy(ifname, new_ifname);
6b50cb5c 231 return 1;
bc5ea049
KK
232}
233
f6d6bad1
LP
234int setup_veth(const char *machine_name,
235 pid_t pid,
236 char iface_name[IFNAMSIZ],
813dbff4 237 bool bridge,
5e21da87 238 const struct ether_addr *provided_mac) {
f6d6bad1 239
4afd3348 240 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
f6d6bad1 241 struct ether_addr mac_host, mac_container;
bc5ea049 242 unsigned u;
6b50cb5c 243 char *n, *a = NULL;
bc5ea049 244 int r;
f6d6bad1
LP
245
246 assert(machine_name);
247 assert(pid > 0);
248 assert(iface_name);
249
250 /* Use two different interface name prefixes depending whether
251 * we are in bridge mode or not. */
bc5ea049 252 n = strjoina(bridge ? "vb-" : "ve-", machine_name);
6b50cb5c
YW
253 r = shorten_ifname(n);
254 if (r > 0)
255 a = strjoina(bridge ? "vb-" : "ve-", machine_name);
f6d6bad1 256
813dbff4
RC
257 if (ether_addr_is_null(provided_mac)){
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 } else
262 mac_container = *provided_mac;
f6d6bad1
LP
263
264 r = generate_mac(machine_name, &mac_host, HOST_HASH_KEY, 0);
265 if (r < 0)
266 return log_error_errno(r, "Failed to generate predictable MAC address for host side: %m");
267
268 r = sd_netlink_open(&rtnl);
269 if (r < 0)
270 return log_error_errno(r, "Failed to connect to netlink: %m");
271
6b50cb5c 272 r = add_veth(rtnl, pid, n, a, &mac_host, "host0", &mac_container);
f6d6bad1
LP
273 if (r < 0)
274 return r;
9a2a5625 275
f6e49154 276 u = if_nametoindex(n); /* We don't need to use rtnl_resolve_ifname() here because the
d308bb99 277 * name we assigned is always the main name. */
bc5ea049
KK
278 if (u == 0)
279 return log_error_errno(errno, "Failed to resolve interface %s: %m", n);
9a2a5625 280
bc5ea049
KK
281 strcpy(iface_name, n);
282 return (int) u;
9a2a5625
LP
283}
284
f6d6bad1
LP
285int setup_veth_extra(
286 const char *machine_name,
287 pid_t pid,
288 char **pairs) {
289
4afd3348 290 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
f6d6bad1 291 uint64_t idx = 0;
f6d6bad1
LP
292 int r;
293
294 assert(machine_name);
295 assert(pid > 0);
296
297 if (strv_isempty(pairs))
298 return 0;
299
300 r = sd_netlink_open(&rtnl);
301 if (r < 0)
302 return log_error_errno(r, "Failed to connect to netlink: %m");
303
304 STRV_FOREACH_PAIR(a, b, pairs) {
305 struct ether_addr mac_host, mac_container;
306
307 r = generate_mac(machine_name, &mac_container, VETH_EXTRA_CONTAINER_HASH_KEY, idx);
308 if (r < 0)
309 return log_error_errno(r, "Failed to generate predictable MAC address for container side of extra veth link: %m");
310
311 r = generate_mac(machine_name, &mac_host, VETH_EXTRA_HOST_HASH_KEY, idx);
312 if (r < 0)
bcc0fe63 313 return log_error_errno(r, "Failed to generate predictable MAC address for host side of extra veth link: %m");
f6d6bad1 314
6b50cb5c 315 r = add_veth(rtnl, pid, *a, NULL, &mac_host, *b, &mac_container);
f6d6bad1
LP
316 if (r < 0)
317 return r;
318
313cefa1 319 idx++;
f6d6bad1
LP
320 }
321
322 return 0;
323}
324
22b28dfd 325static int join_bridge(sd_netlink *rtnl, const char *veth_name, const char *bridge_name) {
4afd3348 326 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
9a2a5625
LP
327 int r, bridge_ifi;
328
22b28dfd 329 assert(rtnl);
9a2a5625
LP
330 assert(veth_name);
331 assert(bridge_name);
332
f6e49154 333 bridge_ifi = rtnl_resolve_interface(&rtnl, bridge_name);
597da51b
ZJS
334 if (bridge_ifi < 0)
335 return bridge_ifi;
9a2a5625
LP
336
337 r = sd_rtnl_message_new_link(rtnl, &m, RTM_SETLINK, 0);
338 if (r < 0)
22b28dfd 339 return r;
9a2a5625
LP
340
341 r = sd_rtnl_message_link_set_flags(m, IFF_UP, IFF_UP);
342 if (r < 0)
22b28dfd 343 return r;
9a2a5625
LP
344
345 r = sd_netlink_message_append_string(m, IFLA_IFNAME, veth_name);
346 if (r < 0)
22b28dfd 347 return r;
9a2a5625
LP
348
349 r = sd_netlink_message_append_u32(m, IFLA_MASTER, bridge_ifi);
350 if (r < 0)
22b28dfd 351 return r;
9a2a5625
LP
352
353 r = sd_netlink_call(rtnl, m, 0, NULL);
354 if (r < 0)
22b28dfd 355 return r;
9a2a5625
LP
356
357 return bridge_ifi;
358}
359
22b28dfd
LP
360static int create_bridge(sd_netlink *rtnl, const char *bridge_name) {
361 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
362 int r;
363
364 r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0);
365 if (r < 0)
366 return r;
367
368 r = sd_netlink_message_append_string(m, IFLA_IFNAME, bridge_name);
369 if (r < 0)
370 return r;
371
372 r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
373 if (r < 0)
374 return r;
375
376 r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "bridge");
377 if (r < 0)
378 return r;
379
380 r = sd_netlink_message_close_container(m);
381 if (r < 0)
382 return r;
383
384 r = sd_netlink_message_close_container(m);
385 if (r < 0)
386 return r;
387
388 r = sd_netlink_call(rtnl, m, 0, NULL);
389 if (r < 0)
390 return r;
391
392 return 0;
393}
394
395int setup_bridge(const char *veth_name, const char *bridge_name, bool create) {
8e766630 396 _cleanup_(release_lock_file) LockFile bridge_lock = LOCK_FILE_INIT;
22b28dfd
LP
397 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
398 int r, bridge_ifi;
399 unsigned n = 0;
400
401 assert(veth_name);
402 assert(bridge_name);
403
404 r = sd_netlink_open(&rtnl);
405 if (r < 0)
406 return log_error_errno(r, "Failed to connect to netlink: %m");
407
408 if (create) {
409 /* We take a system-wide lock here, so that we can safely check whether there's still a member in the
6dd6a9c4 410 * bridge before removing it, without risking interference from other nspawn instances. */
22b28dfd
LP
411
412 r = make_lock_file("/run/systemd/nspawn-network-zone", LOCK_EX, &bridge_lock);
413 if (r < 0)
414 return log_error_errno(r, "Failed to take network zone lock: %m");
415 }
416
417 for (;;) {
418 bridge_ifi = join_bridge(rtnl, veth_name, bridge_name);
419 if (bridge_ifi >= 0)
420 return bridge_ifi;
421 if (bridge_ifi != -ENODEV || !create || n > 10)
422 return log_error_errno(bridge_ifi, "Failed to add interface %s to bridge %s: %m", veth_name, bridge_name);
423
424 /* Count attempts, so that we don't enter an endless loop here. */
425 n++;
426
427 /* The bridge doesn't exist yet. Let's create it */
428 r = create_bridge(rtnl, bridge_name);
429 if (r < 0)
430 return log_error_errno(r, "Failed to create bridge interface %s: %m", bridge_name);
431
432 /* Try again, now that the bridge exists */
433 }
434}
435
436int remove_bridge(const char *bridge_name) {
8e766630 437 _cleanup_(release_lock_file) LockFile bridge_lock = LOCK_FILE_INIT;
22b28dfd
LP
438 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
439 const char *path;
440 int r;
441
442 /* Removes the specified bridge, but only if it is currently empty */
443
444 if (isempty(bridge_name))
445 return 0;
446
447 r = make_lock_file("/run/systemd/nspawn-network-zone", LOCK_EX, &bridge_lock);
448 if (r < 0)
449 return log_error_errno(r, "Failed to take network zone lock: %m");
450
451 path = strjoina("/sys/class/net/", bridge_name, "/brif");
452
db55bbf2 453 r = dir_is_empty(path, /* ignore_hidden_or_backup= */ false);
22b28dfd
LP
454 if (r == -ENOENT) /* Already gone? */
455 return 0;
456 if (r < 0)
457 return log_error_errno(r, "Can't detect if bridge %s is empty: %m", bridge_name);
458 if (r == 0) /* Still populated, leave it around */
459 return 0;
460
461 r = sd_netlink_open(&rtnl);
462 if (r < 0)
463 return log_error_errno(r, "Failed to connect to netlink: %m");
464
465 return remove_one_link(rtnl, bridge_name);
466}
467
2f091b1b 468static int test_network_interface_initialized(const char *name) {
b390f178 469 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
0ac655a6 470 int r;
26208d5b 471
c76b8751 472 if (!udev_available())
b390f178 473 return 0;
26208d5b 474
b390f178 475 /* udev should be around. */
26208d5b 476
0ac655a6 477 r = sd_device_new_from_ifname(&d, name);
b390f178
DDM
478 if (r < 0)
479 return log_error_errno(r, "Failed to get device %s: %m", name);
480
481 r = sd_device_get_is_initialized(d);
482 if (r < 0)
483 return log_error_errno(r, "Failed to determine whether interface %s is initialized: %m", name);
484 if (r == 0)
485 return log_error_errno(SYNTHETIC_ERRNO(EBUSY), "Network interface %s is not initialized yet.", name);
486
487 r = device_is_renaming(d);
488 if (r < 0)
489 return log_error_errno(r, "Failed to determine the interface %s is being renamed: %m", name);
490 if (r > 0)
491 return log_error_errno(SYNTHETIC_ERRNO(EBUSY), "Interface %s is being renamed.", name);
492
493 return 0;
9a2a5625
LP
494}
495
2f091b1b
TM
496int test_network_interfaces_initialized(char **iface_pairs) {
497 int r;
498 STRV_FOREACH_PAIR(a, b, iface_pairs) {
499 r = test_network_interface_initialized(*a);
500 if (r < 0)
501 return r;
502 }
503 return 0;
504}
505
506int move_network_interfaces(int netns_fd, char **iface_pairs) {
4afd3348 507 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
9a2a5625
LP
508 int r;
509
2f091b1b 510 if (strv_isempty(iface_pairs))
9a2a5625
LP
511 return 0;
512
513 r = sd_netlink_open(&rtnl);
514 if (r < 0)
515 return log_error_errno(r, "Failed to connect to netlink: %m");
516
2f091b1b 517 STRV_FOREACH_PAIR(i, b, iface_pairs) {
4afd3348 518 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
9a2a5625
LP
519 int ifi;
520
f6e49154 521 ifi = rtnl_resolve_interface_or_warn(&rtnl, *i);
9a2a5625
LP
522 if (ifi < 0)
523 return ifi;
524
525 r = sd_rtnl_message_new_link(rtnl, &m, RTM_SETLINK, ifi);
526 if (r < 0)
527 return log_error_errno(r, "Failed to allocate netlink message: %m");
528
5b4855ab 529 r = sd_netlink_message_append_u32(m, IFLA_NET_NS_FD, netns_fd);
9a2a5625 530 if (r < 0)
5b4855ab 531 return log_error_errno(r, "Failed to append namespace fd to netlink message: %m");
9a2a5625 532
2f091b1b
TM
533 if (!streq(*b, *i)) {
534 r = sd_netlink_message_append_string(m, IFLA_IFNAME, *b);
535 if (r < 0)
536 return log_error_errno(r, "Failed to add netlink interface name: %m");
537 }
538
9a2a5625
LP
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
2f091b1b 547int setup_macvlan(const char *machine_name, pid_t pid, char **iface_pairs) {
4afd3348 548 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
9a2a5625 549 unsigned idx = 0;
9a2a5625
LP
550 int r;
551
2f091b1b 552 if (strv_isempty(iface_pairs))
9a2a5625
LP
553 return 0;
554
555 r = sd_netlink_open(&rtnl);
556 if (r < 0)
557 return log_error_errno(r, "Failed to connect to netlink: %m");
558
2f091b1b 559 STRV_FOREACH_PAIR(i, b, iface_pairs) {
4afd3348 560 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
2f091b1b
TM
561 _cleanup_free_ char *n = NULL;
562 int shortened, ifi;
9a2a5625 563 struct ether_addr mac;
9a2a5625 564
f6e49154 565 ifi = rtnl_resolve_interface_or_warn(&rtnl, *i);
9a2a5625
LP
566 if (ifi < 0)
567 return ifi;
568
569 r = generate_mac(machine_name, &mac, MACVLAN_HASH_KEY, idx++);
570 if (r < 0)
571 return log_error_errno(r, "Failed to create MACVLAN MAC address: %m");
572
573 r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0);
574 if (r < 0)
575 return log_error_errno(r, "Failed to allocate netlink message: %m");
576
577 r = sd_netlink_message_append_u32(m, IFLA_LINK, ifi);
578 if (r < 0)
579 return log_error_errno(r, "Failed to add netlink interface index: %m");
580
2f091b1b 581 n = strdup(*b);
9a2a5625
LP
582 if (!n)
583 return log_oom();
584
2f091b1b 585 shortened = shorten_ifname(n);
9a2a5625
LP
586
587 r = sd_netlink_message_append_string(m, IFLA_IFNAME, n);
588 if (r < 0)
589 return log_error_errno(r, "Failed to add netlink interface name: %m");
590
591 r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, &mac);
592 if (r < 0)
593 return log_error_errno(r, "Failed to add netlink MAC address: %m");
594
595 r = sd_netlink_message_append_u32(m, IFLA_NET_NS_PID, pid);
596 if (r < 0)
597 return log_error_errno(r, "Failed to add netlink namespace field: %m");
598
599 r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
600 if (r < 0)
601 return log_error_errno(r, "Failed to open netlink container: %m");
602
603 r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "macvlan");
604 if (r < 0)
605 return log_error_errno(r, "Failed to open netlink container: %m");
606
607 r = sd_netlink_message_append_u32(m, IFLA_MACVLAN_MODE, MACVLAN_MODE_BRIDGE);
608 if (r < 0)
609 return log_error_errno(r, "Failed to append macvlan mode: %m");
610
611 r = sd_netlink_message_close_container(m);
612 if (r < 0)
613 return log_error_errno(r, "Failed to close netlink container: %m");
614
615 r = sd_netlink_message_close_container(m);
616 if (r < 0)
617 return log_error_errno(r, "Failed to close netlink container: %m");
618
619 r = sd_netlink_call(rtnl, m, 0, NULL);
620 if (r < 0)
621 return log_error_errno(r, "Failed to add new macvlan interfaces: %m");
6b50cb5c 622
2f091b1b
TM
623 if (shortened > 0)
624 (void) set_alternative_ifname(rtnl, n, *b);
9a2a5625
LP
625 }
626
627 return 0;
628}
629
2f091b1b 630int setup_ipvlan(const char *machine_name, pid_t pid, char **iface_pairs) {
4afd3348 631 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
9a2a5625
LP
632 int r;
633
2f091b1b 634 if (strv_isempty(iface_pairs))
9a2a5625
LP
635 return 0;
636
637 r = sd_netlink_open(&rtnl);
638 if (r < 0)
639 return log_error_errno(r, "Failed to connect to netlink: %m");
640
2f091b1b 641 STRV_FOREACH_PAIR(i, b, iface_pairs) {
4afd3348 642 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
2f091b1b
TM
643 _cleanup_free_ char *n = NULL;
644 int shortened, ifi ;
9a2a5625 645
f6e49154 646 ifi = rtnl_resolve_interface_or_warn(&rtnl, *i);
9a2a5625
LP
647 if (ifi < 0)
648 return ifi;
649
650 r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0);
651 if (r < 0)
652 return log_error_errno(r, "Failed to allocate netlink message: %m");
653
654 r = sd_netlink_message_append_u32(m, IFLA_LINK, ifi);
655 if (r < 0)
656 return log_error_errno(r, "Failed to add netlink interface index: %m");
657
2f091b1b 658 n = strdup(*b);
9a2a5625
LP
659 if (!n)
660 return log_oom();
661
2f091b1b 662 shortened = shorten_ifname(n);
9a2a5625
LP
663
664 r = sd_netlink_message_append_string(m, IFLA_IFNAME, n);
665 if (r < 0)
666 return log_error_errno(r, "Failed to add netlink interface name: %m");
667
668 r = sd_netlink_message_append_u32(m, IFLA_NET_NS_PID, pid);
669 if (r < 0)
670 return log_error_errno(r, "Failed to add netlink namespace field: %m");
671
672 r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
673 if (r < 0)
674 return log_error_errno(r, "Failed to open netlink container: %m");
675
676 r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "ipvlan");
677 if (r < 0)
678 return log_error_errno(r, "Failed to open netlink container: %m");
679
680 r = sd_netlink_message_append_u16(m, IFLA_IPVLAN_MODE, IPVLAN_MODE_L2);
681 if (r < 0)
682 return log_error_errno(r, "Failed to add ipvlan mode: %m");
683
684 r = sd_netlink_message_close_container(m);
685 if (r < 0)
686 return log_error_errno(r, "Failed to close netlink container: %m");
687
688 r = sd_netlink_message_close_container(m);
689 if (r < 0)
690 return log_error_errno(r, "Failed to close netlink container: %m");
691
692 r = sd_netlink_call(rtnl, m, 0, NULL);
693 if (r < 0)
694 return log_error_errno(r, "Failed to add new ipvlan interfaces: %m");
6b50cb5c 695
2f091b1b
TM
696 if (shortened > 0)
697 (void) set_alternative_ifname(rtnl, n, *b);
9a2a5625
LP
698 }
699
700 return 0;
701}
f6d6bad1
LP
702
703int veth_extra_parse(char ***l, const char *p) {
704 _cleanup_free_ char *a = NULL, *b = NULL;
705 int r;
706
707 r = extract_first_word(&p, &a, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
708 if (r < 0)
709 return r;
ef76dff2 710 if (r == 0 || !ifname_valid(a))
f6d6bad1
LP
711 return -EINVAL;
712
713 r = extract_first_word(&p, &b, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
714 if (r < 0)
715 return r;
ef76dff2 716 if (r == 0 || !ifname_valid(b)) {
a73e5eb9
DT
717 r = free_and_strdup(&b, a);
718 if (r < 0)
719 return r;
f6d6bad1
LP
720 }
721
722 if (p)
723 return -EINVAL;
724
725 r = strv_push_pair(l, a, b);
726 if (r < 0)
727 return -ENOMEM;
728
729 a = b = NULL;
730 return 0;
731}
ef3b2aa7 732
ef3b2aa7
LP
733int remove_veth_links(const char *primary, char **pairs) {
734 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
ef3b2aa7
LP
735 int r;
736
737 /* In some cases the kernel might pin the veth links between host and container even after the namespace
738 * died. Hence, let's better remove them explicitly too. */
739
740 if (isempty(primary) && strv_isempty(pairs))
741 return 0;
742
743 r = sd_netlink_open(&rtnl);
744 if (r < 0)
745 return log_error_errno(r, "Failed to connect to netlink: %m");
746
22b28dfd 747 remove_one_link(rtnl, primary);
ef3b2aa7
LP
748
749 STRV_FOREACH_PAIR(a, b, pairs)
22b28dfd 750 remove_one_link(rtnl, *a);
ef3b2aa7
LP
751
752 return 0;
753}
2f091b1b
TM
754
755static int network_iface_pair_parse(const char* iftype, char ***l, const char *p, const char* ifprefix) {
2f091b1b
TM
756 int r;
757
8f4d843a
FS
758 for (;;) {
759 _cleanup_free_ char *word = NULL, *a = NULL, *b = NULL;
760 const char *interface;
2f091b1b 761
8f4d843a
FS
762 r = extract_first_word(&p, &word, NULL, 0);
763 if (r < 0)
764 return log_error_errno(r, "Failed to parse interface name: %m");
765 if (r == 0)
766 break;
2f091b1b 767
8f4d843a
FS
768 interface = word;
769 r = extract_first_word(&interface, &a, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
770 if (r < 0)
771 return log_error_errno(r, "Failed to extract first word in %s parameter: %m", iftype);
772 if (r == 0)
773 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
774 "Short read while reading %s parameter: %m", iftype);
775 if (!ifname_valid(a))
776 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
777 "%s, interface name not valid: %s", iftype, a);
778
927e20fa
YW
779 /* Here, we only check the validity of the specified second name. If it is not specified,
780 * the copied or prefixed name should be already valid, except for its length. If it is too
781 * long, then it will be shortened later. */
782 if (!isempty(interface)) {
783 if (!ifname_valid(interface))
784 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
785 "%s, interface name not valid: %s", iftype, interface);
786
8f4d843a 787 b = strdup(interface);
927e20fa
YW
788 } else if (ifprefix)
789 b = strjoin(ifprefix, a);
790 else
791 b = strdup(a);
8f4d843a
FS
792 if (!b)
793 return log_oom();
794
8f4d843a
FS
795 r = strv_consume_pair(l, TAKE_PTR(a), TAKE_PTR(b));
796 if (r < 0)
797 return log_oom();
798 }
2f091b1b 799
2f091b1b
TM
800 return 0;
801}
802
803int interface_pair_parse(char ***l, const char *p) {
804 return network_iface_pair_parse("Network interface", l, p, NULL);
805}
806
807int macvlan_pair_parse(char ***l, const char *p) {
808 return network_iface_pair_parse("MACVLAN network interface", l, p, "mv-");
809}
810
811int ipvlan_pair_parse(char ***l, const char *p) {
812 return network_iface_pair_parse("IPVLAN network interface", l, p, "iv-");
813}