]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
9a2a5625 LP |
2 | |
3 | #include <linux/veth.h> | |
4 | #include <net/if.h> | |
fe993888 | 5 | #include <sys/file.h> |
9a2a5625 | 6 | |
b4bbcaa9 | 7 | #include "libudev.h" |
9a2a5625 LP |
8 | #include "sd-id128.h" |
9 | #include "sd-netlink.h" | |
9a2a5625 | 10 | |
b5efdb8a | 11 | #include "alloc-util.h" |
9a2a5625 | 12 | #include "ether-addr-util.h" |
22b28dfd | 13 | #include "lockfile-util.h" |
9a2a5625 | 14 | #include "netlink-util.h" |
cf0fbc49 | 15 | #include "nspawn-network.h" |
07630cea | 16 | #include "siphash24.h" |
ef76dff2 LP |
17 | #include "socket-util.h" |
18 | #include "stat-util.h" | |
07630cea | 19 | #include "string-util.h" |
9a2a5625 | 20 | #include "udev-util.h" |
07630cea | 21 | #include "util.h" |
9a2a5625 LP |
22 | |
23 | #define HOST_HASH_KEY SD_ID128_MAKE(1a,37,6f,c7,46,ec,45,0b,ad,a3,d5,31,06,60,5d,b1) | |
24 | #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 |
25 | #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) |
26 | #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 LP |
27 | #define MACVLAN_HASH_KEY SD_ID128_MAKE(00,13,6d,bc,66,83,44,81,bb,0c,f9,51,1f,24,a6,6f) |
28 | ||
22b28dfd LP |
29 | static int remove_one_link(sd_netlink *rtnl, const char *name) { |
30 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; | |
31 | int r; | |
32 | ||
33 | if (isempty(name)) | |
34 | return 0; | |
35 | ||
36 | r = sd_rtnl_message_new_link(rtnl, &m, RTM_DELLINK, 0); | |
37 | if (r < 0) | |
38 | return log_error_errno(r, "Failed to allocate netlink message: %m"); | |
39 | ||
40 | r = sd_netlink_message_append_string(m, IFLA_IFNAME, name); | |
41 | if (r < 0) | |
42 | return log_error_errno(r, "Failed to add netlink interface name: %m"); | |
43 | ||
44 | r = sd_netlink_call(rtnl, m, 0, NULL); | |
45 | if (r == -ENODEV) /* Already gone */ | |
46 | return 0; | |
47 | if (r < 0) | |
48 | return log_error_errno(r, "Failed to remove interface %s: %m", name); | |
49 | ||
50 | return 1; | |
51 | } | |
52 | ||
9a2a5625 LP |
53 | static int generate_mac( |
54 | const char *machine_name, | |
55 | struct ether_addr *mac, | |
56 | sd_id128_t hash_key, | |
57 | uint64_t idx) { | |
58 | ||
dbe81cbd | 59 | uint64_t result; |
9a2a5625 LP |
60 | size_t l, sz; |
61 | uint8_t *v, *i; | |
62 | int r; | |
63 | ||
64 | l = strlen(machine_name); | |
65 | sz = sizeof(sd_id128_t) + l; | |
66 | if (idx > 0) | |
67 | sz += sizeof(idx); | |
68 | ||
69 | v = alloca(sz); | |
70 | ||
71 | /* fetch some persistent data unique to the host */ | |
72 | r = sd_id128_get_machine((sd_id128_t*) v); | |
73 | if (r < 0) | |
74 | return r; | |
75 | ||
76 | /* combine with some data unique (on this host) to this | |
77 | * container instance */ | |
78 | i = mempcpy(v + sizeof(sd_id128_t), machine_name, l); | |
79 | if (idx > 0) { | |
80 | idx = htole64(idx); | |
81 | memcpy(i, &idx, sizeof(idx)); | |
82 | } | |
83 | ||
84 | /* Let's hash the host machine ID plus the container name. We | |
85 | * use a fixed, but originally randomly created hash key here. */ | |
933f9cae | 86 | result = htole64(siphash24(v, sz, hash_key.bytes)); |
9a2a5625 LP |
87 | |
88 | assert_cc(ETH_ALEN <= sizeof(result)); | |
dbe81cbd | 89 | memcpy(mac->ether_addr_octet, &result, ETH_ALEN); |
9a2a5625 LP |
90 | |
91 | /* see eth_random_addr in the kernel */ | |
92 | mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */ | |
93 | mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */ | |
94 | ||
95 | return 0; | |
96 | } | |
97 | ||
f6d6bad1 LP |
98 | static int add_veth( |
99 | sd_netlink *rtnl, | |
100 | pid_t pid, | |
101 | const char *ifname_host, | |
102 | const struct ether_addr *mac_host, | |
103 | const char *ifname_container, | |
104 | const struct ether_addr *mac_container) { | |
9a2a5625 | 105 | |
4afd3348 | 106 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
f6d6bad1 | 107 | int r; |
9a2a5625 | 108 | |
f6d6bad1 LP |
109 | assert(rtnl); |
110 | assert(ifname_host); | |
111 | assert(mac_host); | |
112 | assert(ifname_container); | |
113 | assert(mac_container); | |
9a2a5625 LP |
114 | |
115 | r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0); | |
116 | if (r < 0) | |
117 | return log_error_errno(r, "Failed to allocate netlink message: %m"); | |
118 | ||
f6d6bad1 | 119 | r = sd_netlink_message_append_string(m, IFLA_IFNAME, ifname_host); |
9a2a5625 LP |
120 | if (r < 0) |
121 | return log_error_errno(r, "Failed to add netlink interface name: %m"); | |
122 | ||
f6d6bad1 | 123 | r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, mac_host); |
9a2a5625 LP |
124 | if (r < 0) |
125 | return log_error_errno(r, "Failed to add netlink MAC address: %m"); | |
126 | ||
127 | r = sd_netlink_message_open_container(m, IFLA_LINKINFO); | |
128 | if (r < 0) | |
129 | return log_error_errno(r, "Failed to open netlink container: %m"); | |
130 | ||
131 | r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "veth"); | |
132 | if (r < 0) | |
133 | return log_error_errno(r, "Failed to open netlink container: %m"); | |
134 | ||
135 | r = sd_netlink_message_open_container(m, VETH_INFO_PEER); | |
136 | if (r < 0) | |
137 | return log_error_errno(r, "Failed to open netlink container: %m"); | |
138 | ||
f6d6bad1 | 139 | r = sd_netlink_message_append_string(m, IFLA_IFNAME, ifname_container); |
9a2a5625 LP |
140 | if (r < 0) |
141 | return log_error_errno(r, "Failed to add netlink interface name: %m"); | |
142 | ||
f6d6bad1 | 143 | r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, mac_container); |
9a2a5625 LP |
144 | if (r < 0) |
145 | return log_error_errno(r, "Failed to add netlink MAC address: %m"); | |
146 | ||
147 | r = sd_netlink_message_append_u32(m, IFLA_NET_NS_PID, pid); | |
148 | if (r < 0) | |
149 | return log_error_errno(r, "Failed to add netlink namespace field: %m"); | |
150 | ||
151 | r = sd_netlink_message_close_container(m); | |
152 | if (r < 0) | |
153 | return log_error_errno(r, "Failed to close netlink container: %m"); | |
154 | ||
155 | r = sd_netlink_message_close_container(m); | |
156 | if (r < 0) | |
157 | return log_error_errno(r, "Failed to close netlink container: %m"); | |
158 | ||
159 | r = sd_netlink_message_close_container(m); | |
160 | if (r < 0) | |
161 | return log_error_errno(r, "Failed to close netlink container: %m"); | |
162 | ||
163 | r = sd_netlink_call(rtnl, m, 0, NULL); | |
164 | if (r < 0) | |
f6d6bad1 LP |
165 | return log_error_errno(r, "Failed to add new veth interfaces (%s:%s): %m", ifname_host, ifname_container); |
166 | ||
167 | return 0; | |
168 | } | |
169 | ||
170 | int setup_veth(const char *machine_name, | |
171 | pid_t pid, | |
172 | char iface_name[IFNAMSIZ], | |
173 | bool bridge) { | |
174 | ||
4afd3348 | 175 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
f6d6bad1 LP |
176 | struct ether_addr mac_host, mac_container; |
177 | int r, i; | |
178 | ||
179 | assert(machine_name); | |
180 | assert(pid > 0); | |
181 | assert(iface_name); | |
182 | ||
183 | /* Use two different interface name prefixes depending whether | |
184 | * we are in bridge mode or not. */ | |
185 | snprintf(iface_name, IFNAMSIZ - 1, "%s-%s", | |
186 | bridge ? "vb" : "ve", machine_name); | |
187 | ||
188 | r = generate_mac(machine_name, &mac_container, CONTAINER_HASH_KEY, 0); | |
189 | if (r < 0) | |
190 | return log_error_errno(r, "Failed to generate predictable MAC address for container side: %m"); | |
191 | ||
192 | r = generate_mac(machine_name, &mac_host, HOST_HASH_KEY, 0); | |
193 | if (r < 0) | |
194 | return log_error_errno(r, "Failed to generate predictable MAC address for host side: %m"); | |
195 | ||
196 | r = sd_netlink_open(&rtnl); | |
197 | if (r < 0) | |
198 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
199 | ||
200 | r = add_veth(rtnl, pid, iface_name, &mac_host, "host0", &mac_container); | |
201 | if (r < 0) | |
202 | return r; | |
9a2a5625 LP |
203 | |
204 | i = (int) if_nametoindex(iface_name); | |
205 | if (i <= 0) | |
206 | return log_error_errno(errno, "Failed to resolve interface %s: %m", iface_name); | |
207 | ||
208 | return i; | |
209 | } | |
210 | ||
f6d6bad1 LP |
211 | int setup_veth_extra( |
212 | const char *machine_name, | |
213 | pid_t pid, | |
214 | char **pairs) { | |
215 | ||
4afd3348 | 216 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
f6d6bad1 LP |
217 | uint64_t idx = 0; |
218 | char **a, **b; | |
219 | int r; | |
220 | ||
221 | assert(machine_name); | |
222 | assert(pid > 0); | |
223 | ||
224 | if (strv_isempty(pairs)) | |
225 | return 0; | |
226 | ||
227 | r = sd_netlink_open(&rtnl); | |
228 | if (r < 0) | |
229 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
230 | ||
231 | STRV_FOREACH_PAIR(a, b, pairs) { | |
232 | struct ether_addr mac_host, mac_container; | |
233 | ||
234 | r = generate_mac(machine_name, &mac_container, VETH_EXTRA_CONTAINER_HASH_KEY, idx); | |
235 | if (r < 0) | |
236 | return log_error_errno(r, "Failed to generate predictable MAC address for container side of extra veth link: %m"); | |
237 | ||
238 | r = generate_mac(machine_name, &mac_host, VETH_EXTRA_HOST_HASH_KEY, idx); | |
239 | if (r < 0) | |
240 | return log_error_errno(r, "Failed to generate predictable MAC address for container side of extra veth link: %m"); | |
241 | ||
242 | r = add_veth(rtnl, pid, *a, &mac_host, *b, &mac_container); | |
243 | if (r < 0) | |
244 | return r; | |
245 | ||
313cefa1 | 246 | idx++; |
f6d6bad1 LP |
247 | } |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
22b28dfd | 252 | static int join_bridge(sd_netlink *rtnl, const char *veth_name, const char *bridge_name) { |
4afd3348 | 253 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
9a2a5625 LP |
254 | int r, bridge_ifi; |
255 | ||
22b28dfd | 256 | assert(rtnl); |
9a2a5625 LP |
257 | assert(veth_name); |
258 | assert(bridge_name); | |
259 | ||
260 | bridge_ifi = (int) if_nametoindex(bridge_name); | |
261 | if (bridge_ifi <= 0) | |
22b28dfd | 262 | return -errno; |
9a2a5625 LP |
263 | |
264 | r = sd_rtnl_message_new_link(rtnl, &m, RTM_SETLINK, 0); | |
265 | if (r < 0) | |
22b28dfd | 266 | return r; |
9a2a5625 LP |
267 | |
268 | r = sd_rtnl_message_link_set_flags(m, IFF_UP, IFF_UP); | |
269 | if (r < 0) | |
22b28dfd | 270 | return r; |
9a2a5625 LP |
271 | |
272 | r = sd_netlink_message_append_string(m, IFLA_IFNAME, veth_name); | |
273 | if (r < 0) | |
22b28dfd | 274 | return r; |
9a2a5625 LP |
275 | |
276 | r = sd_netlink_message_append_u32(m, IFLA_MASTER, bridge_ifi); | |
277 | if (r < 0) | |
22b28dfd | 278 | return r; |
9a2a5625 LP |
279 | |
280 | r = sd_netlink_call(rtnl, m, 0, NULL); | |
281 | if (r < 0) | |
22b28dfd | 282 | return r; |
9a2a5625 LP |
283 | |
284 | return bridge_ifi; | |
285 | } | |
286 | ||
22b28dfd LP |
287 | static int create_bridge(sd_netlink *rtnl, const char *bridge_name) { |
288 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; | |
289 | int r; | |
290 | ||
291 | r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0); | |
292 | if (r < 0) | |
293 | return r; | |
294 | ||
295 | r = sd_netlink_message_append_string(m, IFLA_IFNAME, bridge_name); | |
296 | if (r < 0) | |
297 | return r; | |
298 | ||
299 | r = sd_netlink_message_open_container(m, IFLA_LINKINFO); | |
300 | if (r < 0) | |
301 | return r; | |
302 | ||
303 | r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "bridge"); | |
304 | if (r < 0) | |
305 | return r; | |
306 | ||
307 | r = sd_netlink_message_close_container(m); | |
308 | if (r < 0) | |
309 | return r; | |
310 | ||
311 | r = sd_netlink_message_close_container(m); | |
312 | if (r < 0) | |
313 | return r; | |
314 | ||
315 | r = sd_netlink_call(rtnl, m, 0, NULL); | |
316 | if (r < 0) | |
317 | return r; | |
318 | ||
319 | return 0; | |
320 | } | |
321 | ||
322 | int setup_bridge(const char *veth_name, const char *bridge_name, bool create) { | |
8e766630 | 323 | _cleanup_(release_lock_file) LockFile bridge_lock = LOCK_FILE_INIT; |
22b28dfd LP |
324 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
325 | int r, bridge_ifi; | |
326 | unsigned n = 0; | |
327 | ||
328 | assert(veth_name); | |
329 | assert(bridge_name); | |
330 | ||
331 | r = sd_netlink_open(&rtnl); | |
332 | if (r < 0) | |
333 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
334 | ||
335 | if (create) { | |
336 | /* We take a system-wide lock here, so that we can safely check whether there's still a member in the | |
6dd6a9c4 | 337 | * bridge before removing it, without risking interference from other nspawn instances. */ |
22b28dfd LP |
338 | |
339 | r = make_lock_file("/run/systemd/nspawn-network-zone", LOCK_EX, &bridge_lock); | |
340 | if (r < 0) | |
341 | return log_error_errno(r, "Failed to take network zone lock: %m"); | |
342 | } | |
343 | ||
344 | for (;;) { | |
345 | bridge_ifi = join_bridge(rtnl, veth_name, bridge_name); | |
346 | if (bridge_ifi >= 0) | |
347 | return bridge_ifi; | |
348 | if (bridge_ifi != -ENODEV || !create || n > 10) | |
349 | return log_error_errno(bridge_ifi, "Failed to add interface %s to bridge %s: %m", veth_name, bridge_name); | |
350 | ||
351 | /* Count attempts, so that we don't enter an endless loop here. */ | |
352 | n++; | |
353 | ||
354 | /* The bridge doesn't exist yet. Let's create it */ | |
355 | r = create_bridge(rtnl, bridge_name); | |
356 | if (r < 0) | |
357 | return log_error_errno(r, "Failed to create bridge interface %s: %m", bridge_name); | |
358 | ||
359 | /* Try again, now that the bridge exists */ | |
360 | } | |
361 | } | |
362 | ||
363 | int remove_bridge(const char *bridge_name) { | |
8e766630 | 364 | _cleanup_(release_lock_file) LockFile bridge_lock = LOCK_FILE_INIT; |
22b28dfd LP |
365 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
366 | const char *path; | |
367 | int r; | |
368 | ||
369 | /* Removes the specified bridge, but only if it is currently empty */ | |
370 | ||
371 | if (isempty(bridge_name)) | |
372 | return 0; | |
373 | ||
374 | r = make_lock_file("/run/systemd/nspawn-network-zone", LOCK_EX, &bridge_lock); | |
375 | if (r < 0) | |
376 | return log_error_errno(r, "Failed to take network zone lock: %m"); | |
377 | ||
378 | path = strjoina("/sys/class/net/", bridge_name, "/brif"); | |
379 | ||
380 | r = dir_is_empty(path); | |
381 | if (r == -ENOENT) /* Already gone? */ | |
382 | return 0; | |
383 | if (r < 0) | |
384 | return log_error_errno(r, "Can't detect if bridge %s is empty: %m", bridge_name); | |
385 | if (r == 0) /* Still populated, leave it around */ | |
386 | return 0; | |
387 | ||
388 | r = sd_netlink_open(&rtnl); | |
389 | if (r < 0) | |
390 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
391 | ||
392 | return remove_one_link(rtnl, bridge_name); | |
393 | } | |
394 | ||
9a2a5625 | 395 | static int parse_interface(struct udev *udev, const char *name) { |
8e766630 | 396 | _cleanup_(udev_device_unrefp) struct udev_device *d = NULL; |
9a2a5625 LP |
397 | char ifi_str[2 + DECIMAL_STR_MAX(int)]; |
398 | int ifi; | |
399 | ||
400 | ifi = (int) if_nametoindex(name); | |
401 | if (ifi <= 0) | |
402 | return log_error_errno(errno, "Failed to resolve interface %s: %m", name); | |
403 | ||
404 | sprintf(ifi_str, "n%i", ifi); | |
405 | d = udev_device_new_from_device_id(udev, ifi_str); | |
406 | if (!d) | |
407 | return log_error_errno(errno, "Failed to get udev device for interface %s: %m", name); | |
408 | ||
409 | if (udev_device_get_is_initialized(d) <= 0) { | |
410 | log_error("Network interface %s is not initialized yet.", name); | |
411 | return -EBUSY; | |
412 | } | |
413 | ||
414 | return ifi; | |
415 | } | |
416 | ||
417 | int move_network_interfaces(pid_t pid, char **ifaces) { | |
8e766630 | 418 | _cleanup_(udev_unrefp) struct udev *udev = NULL; |
4afd3348 | 419 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
9a2a5625 LP |
420 | char **i; |
421 | int r; | |
422 | ||
423 | if (strv_isempty(ifaces)) | |
424 | return 0; | |
425 | ||
426 | r = sd_netlink_open(&rtnl); | |
427 | if (r < 0) | |
428 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
429 | ||
430 | udev = udev_new(); | |
431 | if (!udev) { | |
432 | log_error("Failed to connect to udev."); | |
433 | return -ENOMEM; | |
434 | } | |
435 | ||
436 | STRV_FOREACH(i, ifaces) { | |
4afd3348 | 437 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
9a2a5625 LP |
438 | int ifi; |
439 | ||
440 | ifi = parse_interface(udev, *i); | |
441 | if (ifi < 0) | |
442 | return ifi; | |
443 | ||
444 | r = sd_rtnl_message_new_link(rtnl, &m, RTM_SETLINK, ifi); | |
445 | if (r < 0) | |
446 | return log_error_errno(r, "Failed to allocate netlink message: %m"); | |
447 | ||
448 | r = sd_netlink_message_append_u32(m, IFLA_NET_NS_PID, pid); | |
449 | if (r < 0) | |
450 | return log_error_errno(r, "Failed to append namespace PID to netlink message: %m"); | |
451 | ||
452 | r = sd_netlink_call(rtnl, m, 0, NULL); | |
453 | if (r < 0) | |
454 | return log_error_errno(r, "Failed to move interface %s to namespace: %m", *i); | |
455 | } | |
456 | ||
457 | return 0; | |
458 | } | |
459 | ||
460 | int setup_macvlan(const char *machine_name, pid_t pid, char **ifaces) { | |
8e766630 | 461 | _cleanup_(udev_unrefp) struct udev *udev = NULL; |
4afd3348 | 462 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
9a2a5625 LP |
463 | unsigned idx = 0; |
464 | char **i; | |
465 | int r; | |
466 | ||
467 | if (strv_isempty(ifaces)) | |
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 | udev = udev_new(); | |
475 | if (!udev) { | |
476 | log_error("Failed to connect to udev."); | |
477 | return -ENOMEM; | |
478 | } | |
479 | ||
480 | STRV_FOREACH(i, ifaces) { | |
4afd3348 | 481 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
9a2a5625 LP |
482 | _cleanup_free_ char *n = NULL; |
483 | struct ether_addr mac; | |
484 | int ifi; | |
485 | ||
486 | ifi = parse_interface(udev, *i); | |
487 | if (ifi < 0) | |
488 | return ifi; | |
489 | ||
490 | r = generate_mac(machine_name, &mac, MACVLAN_HASH_KEY, idx++); | |
491 | if (r < 0) | |
492 | return log_error_errno(r, "Failed to create MACVLAN MAC address: %m"); | |
493 | ||
494 | r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0); | |
495 | if (r < 0) | |
496 | return log_error_errno(r, "Failed to allocate netlink message: %m"); | |
497 | ||
498 | r = sd_netlink_message_append_u32(m, IFLA_LINK, ifi); | |
499 | if (r < 0) | |
500 | return log_error_errno(r, "Failed to add netlink interface index: %m"); | |
501 | ||
502 | n = strappend("mv-", *i); | |
503 | if (!n) | |
504 | return log_oom(); | |
505 | ||
506 | strshorten(n, IFNAMSIZ-1); | |
507 | ||
508 | r = sd_netlink_message_append_string(m, IFLA_IFNAME, n); | |
509 | if (r < 0) | |
510 | return log_error_errno(r, "Failed to add netlink interface name: %m"); | |
511 | ||
512 | r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, &mac); | |
513 | if (r < 0) | |
514 | return log_error_errno(r, "Failed to add netlink MAC address: %m"); | |
515 | ||
516 | r = sd_netlink_message_append_u32(m, IFLA_NET_NS_PID, pid); | |
517 | if (r < 0) | |
518 | return log_error_errno(r, "Failed to add netlink namespace field: %m"); | |
519 | ||
520 | r = sd_netlink_message_open_container(m, IFLA_LINKINFO); | |
521 | if (r < 0) | |
522 | return log_error_errno(r, "Failed to open netlink container: %m"); | |
523 | ||
524 | r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "macvlan"); | |
525 | if (r < 0) | |
526 | return log_error_errno(r, "Failed to open netlink container: %m"); | |
527 | ||
528 | r = sd_netlink_message_append_u32(m, IFLA_MACVLAN_MODE, MACVLAN_MODE_BRIDGE); | |
529 | if (r < 0) | |
530 | return log_error_errno(r, "Failed to append macvlan mode: %m"); | |
531 | ||
532 | r = sd_netlink_message_close_container(m); | |
533 | if (r < 0) | |
534 | return log_error_errno(r, "Failed to close netlink container: %m"); | |
535 | ||
536 | r = sd_netlink_message_close_container(m); | |
537 | if (r < 0) | |
538 | return log_error_errno(r, "Failed to close netlink container: %m"); | |
539 | ||
540 | r = sd_netlink_call(rtnl, m, 0, NULL); | |
541 | if (r < 0) | |
542 | return log_error_errno(r, "Failed to add new macvlan interfaces: %m"); | |
543 | } | |
544 | ||
545 | return 0; | |
546 | } | |
547 | ||
548 | int setup_ipvlan(const char *machine_name, pid_t pid, char **ifaces) { | |
8e766630 | 549 | _cleanup_(udev_unrefp) struct udev *udev = NULL; |
4afd3348 | 550 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
9a2a5625 LP |
551 | char **i; |
552 | int r; | |
553 | ||
554 | if (strv_isempty(ifaces)) | |
555 | return 0; | |
556 | ||
557 | r = sd_netlink_open(&rtnl); | |
558 | if (r < 0) | |
559 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
560 | ||
561 | udev = udev_new(); | |
562 | if (!udev) { | |
563 | log_error("Failed to connect to udev."); | |
564 | return -ENOMEM; | |
565 | } | |
566 | ||
567 | STRV_FOREACH(i, ifaces) { | |
4afd3348 | 568 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
9a2a5625 LP |
569 | _cleanup_free_ char *n = NULL; |
570 | int ifi; | |
571 | ||
572 | ifi = parse_interface(udev, *i); | |
573 | if (ifi < 0) | |
574 | return ifi; | |
575 | ||
576 | r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0); | |
577 | if (r < 0) | |
578 | return log_error_errno(r, "Failed to allocate netlink message: %m"); | |
579 | ||
580 | r = sd_netlink_message_append_u32(m, IFLA_LINK, ifi); | |
581 | if (r < 0) | |
582 | return log_error_errno(r, "Failed to add netlink interface index: %m"); | |
583 | ||
584 | n = strappend("iv-", *i); | |
585 | if (!n) | |
586 | return log_oom(); | |
587 | ||
588 | strshorten(n, IFNAMSIZ-1); | |
589 | ||
590 | r = sd_netlink_message_append_string(m, IFLA_IFNAME, n); | |
591 | if (r < 0) | |
592 | return log_error_errno(r, "Failed to add netlink interface name: %m"); | |
593 | ||
594 | r = sd_netlink_message_append_u32(m, IFLA_NET_NS_PID, pid); | |
595 | if (r < 0) | |
596 | return log_error_errno(r, "Failed to add netlink namespace field: %m"); | |
597 | ||
598 | r = sd_netlink_message_open_container(m, IFLA_LINKINFO); | |
599 | if (r < 0) | |
600 | return log_error_errno(r, "Failed to open netlink container: %m"); | |
601 | ||
602 | r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "ipvlan"); | |
603 | if (r < 0) | |
604 | return log_error_errno(r, "Failed to open netlink container: %m"); | |
605 | ||
606 | r = sd_netlink_message_append_u16(m, IFLA_IPVLAN_MODE, IPVLAN_MODE_L2); | |
607 | if (r < 0) | |
608 | return log_error_errno(r, "Failed to add ipvlan mode: %m"); | |
609 | ||
610 | r = sd_netlink_message_close_container(m); | |
611 | if (r < 0) | |
612 | return log_error_errno(r, "Failed to close netlink container: %m"); | |
613 | ||
614 | r = sd_netlink_message_close_container(m); | |
615 | if (r < 0) | |
616 | return log_error_errno(r, "Failed to close netlink container: %m"); | |
617 | ||
618 | r = sd_netlink_call(rtnl, m, 0, NULL); | |
619 | if (r < 0) | |
620 | return log_error_errno(r, "Failed to add new ipvlan interfaces: %m"); | |
621 | } | |
622 | ||
623 | return 0; | |
624 | } | |
f6d6bad1 LP |
625 | |
626 | int veth_extra_parse(char ***l, const char *p) { | |
627 | _cleanup_free_ char *a = NULL, *b = NULL; | |
628 | int r; | |
629 | ||
630 | r = extract_first_word(&p, &a, ":", EXTRACT_DONT_COALESCE_SEPARATORS); | |
631 | if (r < 0) | |
632 | return r; | |
ef76dff2 | 633 | if (r == 0 || !ifname_valid(a)) |
f6d6bad1 LP |
634 | return -EINVAL; |
635 | ||
636 | r = extract_first_word(&p, &b, ":", EXTRACT_DONT_COALESCE_SEPARATORS); | |
637 | if (r < 0) | |
638 | return r; | |
ef76dff2 | 639 | if (r == 0 || !ifname_valid(b)) { |
f6d6bad1 LP |
640 | free(b); |
641 | b = strdup(a); | |
642 | if (!b) | |
643 | return -ENOMEM; | |
644 | } | |
645 | ||
646 | if (p) | |
647 | return -EINVAL; | |
648 | ||
649 | r = strv_push_pair(l, a, b); | |
650 | if (r < 0) | |
651 | return -ENOMEM; | |
652 | ||
653 | a = b = NULL; | |
654 | return 0; | |
655 | } | |
ef3b2aa7 | 656 | |
ef3b2aa7 LP |
657 | int remove_veth_links(const char *primary, char **pairs) { |
658 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
659 | char **a, **b; | |
660 | int r; | |
661 | ||
662 | /* In some cases the kernel might pin the veth links between host and container even after the namespace | |
663 | * died. Hence, let's better remove them explicitly too. */ | |
664 | ||
665 | if (isempty(primary) && strv_isempty(pairs)) | |
666 | return 0; | |
667 | ||
668 | r = sd_netlink_open(&rtnl); | |
669 | if (r < 0) | |
670 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
671 | ||
22b28dfd | 672 | remove_one_link(rtnl, primary); |
ef3b2aa7 LP |
673 | |
674 | STRV_FOREACH_PAIR(a, b, pairs) | |
22b28dfd | 675 | remove_one_link(rtnl, *a); |
ef3b2aa7 LP |
676 | |
677 | return 0; | |
678 | } |