Debian Codegraph @

src/network/netdev/netdev.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/if_arp.h>
4 #include <unistd.h>
5
6 #include "sd-netlink.h"
7
8 #include "alloc-util.h"
9 #include "arphrd-util.h"
10 #include "bareudp.h"
11 #include "batadv.h"
12 #include "bond.h"
13 #include "bridge.h"
14 #include "condition.h"
15 #include "conf-files.h"
16 #include "conf-parser.h"
17 #include "dummy.h"
18 #include "fou-tunnel.h"
19 #include "geneve.h"
20 #include "hashmap.h"
21 #include "hsr.h"
22 #include "ifb.h"
23 #include "ipoib.h"
24 #include "ipvlan.h"
25 #include "l2tp-tunnel.h"
26 #include "macsec.h"
27 #include "macvlan.h"
28 #include "netdev.h"
29 #include "netif-util.h"
30 #include "netlink-util.h"
31 #include "network-util.h"
32 #include "networkd-manager.h"
33 #include "networkd-queue.h"
34 #include "networkd-sriov.h"
35 #include "networkd-state-file.h"
36 #include "nlmon.h"
37 #include "path-util.h"
38 #include "stat-util.h"
39 #include "string-table.h"
40 #include "string-util.h"
41 #include "strv.h"
42 #include "tunnel.h"
43 #include "tuntap.h"
44 #include "vcan.h"
45 #include "veth.h"
46 #include "vlan.h"
47 #include "vrf.h"
48 #include "vxcan.h"
49 #include "vxlan.h"
50 #include "wireguard.h"
51 #include "wlan.h"
52 #include "xfrm.h"
53
54 const NetDevVTable * const netdev_vtable[_NETDEV_KIND_MAX] = {
55 [NETDEV_KIND_BAREUDP] = &bare_udp_vtable,
56 [NETDEV_KIND_BATADV] = &batadv_vtable,
57 [NETDEV_KIND_BOND] = &bond_vtable,
58 [NETDEV_KIND_BRIDGE] = &bridge_vtable,
59 [NETDEV_KIND_DUMMY] = &dummy_vtable,
60 [NETDEV_KIND_ERSPAN] = &erspan_vtable,
61 [NETDEV_KIND_FOU] = &foutnl_vtable,
62 [NETDEV_KIND_GENEVE] = &geneve_vtable,
63 [NETDEV_KIND_GRE] = &gre_vtable,
64 [NETDEV_KIND_GRETAP] = &gretap_vtable,
65 [NETDEV_KIND_HSR] = &hsr_vtable,
66 [NETDEV_KIND_IFB] = &ifb_vtable,
67 [NETDEV_KIND_IP6GRE] = &ip6gre_vtable,
68 [NETDEV_KIND_IP6GRETAP] = &ip6gretap_vtable,
69 [NETDEV_KIND_IP6TNL] = &ip6tnl_vtable,
70 [NETDEV_KIND_IPIP] = &ipip_vtable,
71 [NETDEV_KIND_IPOIB] = &ipoib_vtable,
72 [NETDEV_KIND_IPVLAN] = &ipvlan_vtable,
73 [NETDEV_KIND_IPVTAP] = &ipvtap_vtable,
74 [NETDEV_KIND_L2TP] = &l2tptnl_vtable,
75 [NETDEV_KIND_MACSEC] = &macsec_vtable,
76 [NETDEV_KIND_MACVLAN] = &macvlan_vtable,
77 [NETDEV_KIND_MACVTAP] = &macvtap_vtable,
78 [NETDEV_KIND_NLMON] = &nlmon_vtable,
79 [NETDEV_KIND_SIT] = &sit_vtable,
80 [NETDEV_KIND_TAP] = &tap_vtable,
81 [NETDEV_KIND_TUN] = &tun_vtable,
82 [NETDEV_KIND_VCAN] = &vcan_vtable,
83 [NETDEV_KIND_VETH] = &veth_vtable,
84 [NETDEV_KIND_VLAN] = &vlan_vtable,
85 [NETDEV_KIND_VRF] = &vrf_vtable,
86 [NETDEV_KIND_VTI6] = &vti6_vtable,
87 [NETDEV_KIND_VTI] = &vti_vtable,
88 [NETDEV_KIND_VXCAN] = &vxcan_vtable,
89 [NETDEV_KIND_VXLAN] = &vxlan_vtable,
90 [NETDEV_KIND_WIREGUARD] = &wireguard_vtable,
91 [NETDEV_KIND_WLAN] = &wlan_vtable,
92 [NETDEV_KIND_XFRM] = &xfrm_vtable,
93 };
94
95 static const char* const netdev_kind_table[_NETDEV_KIND_MAX] = {
96 [NETDEV_KIND_BAREUDP] = "bareudp",
97 [NETDEV_KIND_BATADV] = "batadv",
98 [NETDEV_KIND_BOND] = "bond",
99 [NETDEV_KIND_BRIDGE] = "bridge",
100 [NETDEV_KIND_DUMMY] = "dummy",
101 [NETDEV_KIND_ERSPAN] = "erspan",
102 [NETDEV_KIND_FOU] = "fou",
103 [NETDEV_KIND_GENEVE] = "geneve",
104 [NETDEV_KIND_GRE] = "gre",
105 [NETDEV_KIND_GRETAP] = "gretap",
106 [NETDEV_KIND_HSR] = "hsr",
107 [NETDEV_KIND_IFB] = "ifb",
108 [NETDEV_KIND_IP6GRE] = "ip6gre",
109 [NETDEV_KIND_IP6GRETAP] = "ip6gretap",
110 [NETDEV_KIND_IP6TNL] = "ip6tnl",
111 [NETDEV_KIND_IPIP] = "ipip",
112 [NETDEV_KIND_IPOIB] = "ipoib",
113 [NETDEV_KIND_IPVLAN] = "ipvlan",
114 [NETDEV_KIND_IPVTAP] = "ipvtap",
115 [NETDEV_KIND_L2TP] = "l2tp",
116 [NETDEV_KIND_MACSEC] = "macsec",
117 [NETDEV_KIND_MACVLAN] = "macvlan",
118 [NETDEV_KIND_MACVTAP] = "macvtap",
119 [NETDEV_KIND_NLMON] = "nlmon",
120 [NETDEV_KIND_SIT] = "sit",
121 [NETDEV_KIND_TAP] = "tap",
122 [NETDEV_KIND_TUN] = "tun",
123 [NETDEV_KIND_VCAN] = "vcan",
124 [NETDEV_KIND_VETH] = "veth",
125 [NETDEV_KIND_VLAN] = "vlan",
126 [NETDEV_KIND_VRF] = "vrf",
127 [NETDEV_KIND_VTI6] = "vti6",
128 [NETDEV_KIND_VTI] = "vti",
129 [NETDEV_KIND_VXCAN] = "vxcan",
130 [NETDEV_KIND_VXLAN] = "vxlan",
131 [NETDEV_KIND_WIREGUARD] = "wireguard",
132 [NETDEV_KIND_WLAN] = "wlan",
133 [NETDEV_KIND_XFRM] = "xfrm",
134 };
135
136 DEFINE_STRING_TABLE_LOOKUP(netdev_kind, NetDevKind);
137
138 bool netdev_is_managed(NetDev *netdev) {
139 if (!netdev || !netdev->manager || !netdev->ifname)
140 return false;
141
142 return hashmap_get(netdev->manager->netdevs, netdev->ifname) == netdev;
143 }
144
145 static bool netdev_is_stacked_and_independent(NetDev *netdev) {
146 assert(netdev);
147
148 if (netdev_get_create_type(netdev) != NETDEV_CREATE_STACKED)
149 return false;
150
151 switch (netdev->kind) {
152 case NETDEV_KIND_ERSPAN:
153 return ERSPAN(netdev)->independent;
154 case NETDEV_KIND_GRE:
155 return GRE(netdev)->independent;
156 case NETDEV_KIND_GRETAP:
157 return GRETAP(netdev)->independent;
158 case NETDEV_KIND_IP6GRE:
159 return IP6GRE(netdev)->independent;
160 case NETDEV_KIND_IP6GRETAP:
161 return IP6GRETAP(netdev)->independent;
162 case NETDEV_KIND_IP6TNL:
163 return IP6TNL(netdev)->independent;
164 case NETDEV_KIND_IPIP:
165 return IPIP(netdev)->independent;
166 case NETDEV_KIND_SIT:
167 return SIT(netdev)->independent;
168 case NETDEV_KIND_VTI:
169 return VTI(netdev)->independent;
170 case NETDEV_KIND_VTI6:
171 return VTI6(netdev)->independent;
172 case NETDEV_KIND_VXLAN:
173 return VXLAN(netdev)->independent;
174 case NETDEV_KIND_XFRM:
175 return XFRM(netdev)->independent;
176 default:
177 return false;
178 }
179 }
180
181 static bool netdev_is_stacked(NetDev *netdev) {
182 assert(netdev);
183
184 if (netdev_get_create_type(netdev) != NETDEV_CREATE_STACKED)
185 return false;
186
187 if (netdev_is_stacked_and_independent(netdev))
188 return false;
189
190 return true;
191 }
192
193 NetDev* netdev_detach_name(NetDev *netdev, const char *name) {
194 assert(netdev);
195
196 if (!netdev->manager || !name)
197 return NULL; /* Already detached or not attached yet. */
198
199 return hashmap_remove_value(netdev->manager->netdevs, name, netdev);
200 }
201
202 static NetDev* netdev_detach_impl(NetDev *netdev) {
203 assert(netdev);
204
205 if (netdev->state != _NETDEV_STATE_INVALID &&
206 NETDEV_VTABLE(netdev) &&
207 NETDEV_VTABLE(netdev)->detach)
208 NETDEV_VTABLE(netdev)->detach(netdev);
209
210 NetDev *n = netdev_detach_name(netdev, netdev->ifname);
211
212 netdev->manager = NULL;
213 return n; /* Return NULL when it is not attached yet, or already detached. */
214 }
215
216 void netdev_detach(NetDev *netdev) {
217 assert(netdev);
218
219 netdev_unref(netdev_detach_impl(netdev));
220 }
221
222 static NetDev* netdev_free(NetDev *netdev) {
223 assert(netdev);
224
225 netdev_detach_impl(netdev);
226
227 /* Invoke the per-kind done() destructor, but only if the state field is initialized. We conditionalize that
228 * because we parse .netdev files twice: once to determine the kind (with a short, minimal NetDev structure
229 * allocation, with no room for per-kind fields), and once to read the kind's properties (with a full,
230 * comprehensive NetDev structure allocation with enough space for whatever the specific kind needs). Now, in
231 * the first case we shouldn't try to destruct the per-kind NetDev fields on destruction, in the second case we
232 * should. We use the state field to discern the two cases: it's _NETDEV_STATE_INVALID on the first "raw"
233 * call. */
234 if (netdev->state != _NETDEV_STATE_INVALID &&
235 NETDEV_VTABLE(netdev) &&
236 NETDEV_VTABLE(netdev)->done)
237 NETDEV_VTABLE(netdev)->done(netdev);
238
239 condition_free_list(netdev->conditions);
240 free(netdev->filename);
241 strv_free(netdev->dropins);
242 hashmap_free(netdev->stats_by_path);
243 free(netdev->description);
244 free(netdev->ifname);
245
246 return mfree(netdev);
247 }
248
249 DEFINE_TRIVIAL_REF_UNREF_FUNC(NetDev, netdev, netdev_free);
250
251 void netdev_drop(NetDev *netdev) {
252 if (!netdev)
253 return;
254
255 if (netdev_is_stacked(netdev)) {
256 /* The netdev may be removed due to the underlying device removal, and the device may
257 * be re-added later. */
258 netdev->state = NETDEV_STATE_LOADING;
259 netdev->ifindex = 0;
260
261 log_netdev_debug(netdev, "netdev removed");
262 return;
263 }
264
265 if (NETDEV_VTABLE(netdev) && NETDEV_VTABLE(netdev)->drop)
266 NETDEV_VTABLE(netdev)->drop(netdev);
267
268 netdev->state = NETDEV_STATE_LINGER;
269
270 log_netdev_debug(netdev, "netdev removed");
271
272 netdev_detach(netdev);
273 }
274
275 static int netdev_attach_name_full(NetDev *netdev, const char *name, Hashmap **netdevs) {
276 int r;
277
278 assert(netdev);
279 assert(name);
280 assert(netdevs);
281
282 r = hashmap_ensure_put(netdevs, &string_hash_ops, name, netdev);
283 if (r == -ENOMEM)
284 return log_oom();
285 if (r == -EEXIST) {
286 NetDev *n = hashmap_get(*netdevs, name);
287
288 assert(n);
289 if (!streq(netdev->filename, n->filename))
290 log_netdev_warning_errno(netdev, r,
291 "Device \"%s\" was already configured by \"%s\", ignoring %s.",
292 name, n->filename, netdev->filename);
293
294 return -EEXIST;
295 }
296 assert(r > 0);
297
298 return 0;
299 }
300
301 int netdev_attach_name(NetDev *netdev, const char *name) {
302 assert(netdev);
303 assert(netdev->manager);
304
305 return netdev_attach_name_full(netdev, name, &netdev->manager->netdevs);
306 }
307
308 static int netdev_attach(NetDev *netdev) {
309 int r;
310
311 assert(netdev);
312 assert(netdev->ifname);
313
314 r = netdev_attach_name(netdev, netdev->ifname);
315 if (r < 0)
316 return r;
317
318 if (NETDEV_VTABLE(netdev)->attach) {
319 r = NETDEV_VTABLE(netdev)->attach(netdev);
320 if (r < 0)
321 return r;
322 }
323
324 return 0;
325 }
326
327 int netdev_get(Manager *manager, const char *name, NetDev **ret) {
328 NetDev *netdev;
329
330 assert(manager);
331 assert(name);
332 assert(ret);
333
334 netdev = hashmap_get(manager->netdevs, name);
335 if (!netdev)
336 return -ENOENT;
337
338 *ret = netdev;
339
340 return 0;
341 }
342
343 void link_assign_netdev(Link *link) {
344 _unused_ _cleanup_(netdev_unrefp) NetDev *old = NULL;
345 NetDev *netdev;
346
347 assert(link);
348 assert(link->manager);
349 assert(link->ifname);
350
351 old = TAKE_PTR(link->netdev);
352
353 if (netdev_get(link->manager, link->ifname, &netdev) < 0)
354 goto not_found;
355
356 int ifindex = NETDEV_VTABLE(netdev)->get_ifindex ?
357 NETDEV_VTABLE(netdev)->get_ifindex(netdev, link->ifname) :
358 netdev->ifindex;
359 if (ifindex != link->ifindex)
360 goto not_found;
361
362 if (NETDEV_VTABLE(netdev)->iftype != link->iftype)
363 goto not_found;
364
365 if (!NETDEV_VTABLE(netdev)->skip_netdev_kind_check) {
366 const char *kind;
367
368 if (netdev->kind == NETDEV_KIND_TAP)
369 kind = "tun"; /* the kernel does not distinguish between tun and tap */
370 else
371 kind = netdev_kind_to_string(netdev->kind);
372
373 if (!streq_ptr(kind, link->kind))
374 goto not_found;
375 }
376
377 link->netdev = netdev_ref(netdev);
378
379 if (netdev == old)
380 return; /* The same NetDev found. */
381
382 log_link_debug(link, "Found matching .netdev file: %s", netdev->filename);
383 link_dirty(link);
384 return;
385
386 not_found:
387
388 if (old)
389 /* Previously assigned NetDev is detached from Manager? Update the state file. */
390 link_dirty(link);
391 }
392
393 void netdev_enter_failed(NetDev *netdev) {
394 netdev->state = NETDEV_STATE_FAILED;
395 }
396
397 int netdev_enter_ready(NetDev *netdev) {
398 assert(netdev);
399 assert(netdev->ifname);
400
401 if (!IN_SET(netdev->state, NETDEV_STATE_LOADING, NETDEV_STATE_CREATING))
402 return 0;
403
404 netdev->state = NETDEV_STATE_READY;
405
406 log_netdev_info(netdev, "netdev ready");
407
408 if (NETDEV_VTABLE(netdev)->post_create)
409 NETDEV_VTABLE(netdev)->post_create(netdev, NULL);
410
411 return 0;
412 }
413
414 bool netdev_needs_reconfigure(NetDev *netdev, NetDevLocalAddressType type) {
415 assert(netdev);
416 assert(type < _NETDEV_LOCAL_ADDRESS_TYPE_MAX);
417
418 if (type < 0)
419 return true;
420
421 return NETDEV_VTABLE(netdev)->needs_reconfigure &&
422 NETDEV_VTABLE(netdev)->needs_reconfigure(netdev, type);
423 }
424
425 /* callback for netdev's created without a backing Link */
426 static int netdev_create_handler(sd_netlink *rtnl, sd_netlink_message *m, NetDev *netdev) {
427 int r;
428
429 assert(netdev);
430 assert(netdev->state != _NETDEV_STATE_INVALID);
431
432 r = sd_netlink_message_get_errno(m);
433 if (r >= 0)
434 log_netdev_debug(netdev, "Created.");
435 else if (r == -EEXIST && netdev->ifindex > 0)
436 log_netdev_debug(netdev, "Already exists.");
437 else {
438 log_netdev_warning_errno(netdev, r, "Failed to create netdev: %m");
439 netdev_enter_failed(netdev);
440 return 0;
441 }
442
443 return netdev_enter_ready(netdev);
444 }
445
446 int netdev_set_ifindex_internal(NetDev *netdev, int ifindex) {
447 assert(netdev);
448 assert(ifindex > 0);
449
450 if (netdev->ifindex == ifindex)
451 return 0; /* Already set. */
452
453 if (netdev->ifindex > 0 && netdev->ifindex != ifindex)
454 return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EEXIST),
455 "Could not set ifindex to %i, already set to %i.",
456 ifindex, netdev->ifindex);
457
458 netdev->ifindex = ifindex;
459 log_netdev_debug(netdev, "Gained index %i.", ifindex);
460 return 1; /* set new ifindex. */
461 }
462
463 static int netdev_set_ifindex_impl(NetDev *netdev, const char *name, int ifindex) {
464 assert(netdev);
465 assert(name);
466 assert(ifindex > 0);
467
468 if (NETDEV_VTABLE(netdev)->set_ifindex)
469 return NETDEV_VTABLE(netdev)->set_ifindex(netdev, name, ifindex);
470
471 if (!streq(netdev->ifname, name))
472 return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
473 "Received netlink message with unexpected interface name %s (ifindex=%i).",
474 name, ifindex);
475
476 return netdev_set_ifindex_internal(netdev, ifindex);
477 }
478
479 int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *message) {
480 uint16_t type;
481 const char *kind;
482 const char *received_kind;
483 const char *received_name;
484 int r, ifindex, family;
485
486 assert(netdev);
487 assert(message);
488
489 r = sd_netlink_message_get_type(message, &type);
490 if (r < 0)
491 return log_netdev_warning_errno(netdev, r, "Could not get rtnl message type: %m");
492
493 if (type != RTM_NEWLINK)
494 return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL), "Cannot set ifindex from unexpected rtnl message type.");
495
496 r = sd_rtnl_message_get_family(message, &family);
497 if (r < 0)
498 return log_netdev_warning_errno(netdev, r, "Failed to get family from received rtnl message: %m");
499
500 if (family != AF_UNSPEC)
501 return 0; /* IFLA_LINKINFO is only contained in the message with AF_UNSPEC. */
502
503 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
504 if (r < 0)
505 return log_netdev_warning_errno(netdev, r, "Could not get ifindex: %m");
506 if (ifindex <= 0)
507 return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL), "Got invalid ifindex: %d", ifindex);
508
509 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &received_name);
510 if (r < 0)
511 return log_netdev_warning_errno(netdev, r, "Could not get IFNAME: %m");
512
513 if (!NETDEV_VTABLE(netdev)->skip_netdev_kind_check) {
514
515 r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
516 if (r < 0)
517 return log_netdev_warning_errno(netdev, r, "Could not get LINKINFO: %m");
518
519 r = sd_netlink_message_read_string(message, IFLA_INFO_KIND, &received_kind);
520 if (r < 0)
521 return log_netdev_warning_errno(netdev, r, "Could not get KIND: %m");
522
523 r = sd_netlink_message_exit_container(message);
524 if (r < 0)
525 return log_netdev_warning_errno(netdev, r, "Could not exit container: %m");
526
527 if (netdev->kind == NETDEV_KIND_TAP)
528 /* the kernel does not distinguish between tun and tap */
529 kind = "tun";
530 else
531 kind = netdev_kind_to_string(netdev->kind);
532 if (!kind)
533 return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL), "Could not get netdev kind.");
534
535 if (!streq(kind, received_kind))
536 return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
537 "Received newlink with wrong KIND %s, expected %s",
538 received_kind, kind);
539 }
540
541 return netdev_set_ifindex_impl(netdev, received_name, ifindex);
542 }
543
544 #define HASH_KEY SD_ID128_MAKE(52,e1,45,bd,00,6f,29,96,21,c6,30,6d,83,71,04,48)
545
546 int netdev_generate_hw_addr(
547 NetDev *netdev,
548 Link *parent,
549 const char *name,
550 const struct hw_addr_data *hw_addr,
551 struct hw_addr_data *ret) {
552
553 struct hw_addr_data a = HW_ADDR_NULL;
554 bool is_static = false;
555 int r;
556
557 assert(netdev);
558 assert(name);
559 assert(hw_addr);
560 assert(ret);
561
562 if (hw_addr_equal(hw_addr, &HW_ADDR_NONE)) {
563 *ret = HW_ADDR_NULL;
564 return 0;
565 }
566
567 if (hw_addr->length == 0) {
568 uint64_t result;
569
570 /* HardwareAddress= is not specified. */
571
572 if (!NETDEV_VTABLE(netdev)->generate_mac)
573 goto finalize;
574
575 if (!IN_SET(NETDEV_VTABLE(netdev)->iftype, ARPHRD_ETHER, ARPHRD_INFINIBAND))
576 goto finalize;
577
578 r = net_get_unique_predictable_data_from_name(name, &HASH_KEY, &result);
579 if (r < 0) {
580 log_netdev_warning_errno(netdev, r,
581 "Failed to generate persistent MAC address, ignoring: %m");
582 goto finalize;
583 }
584
585 a.length = arphrd_to_hw_addr_len(NETDEV_VTABLE(netdev)->iftype);
586
587 switch (NETDEV_VTABLE(netdev)->iftype) {
588 case ARPHRD_ETHER:
589 assert(a.length <= sizeof(result));
590 memcpy(a.bytes, &result, a.length);
591
592 if (ether_addr_is_null(&a.ether) || ether_addr_is_broadcast(&a.ether)) {
593 log_netdev_warning(netdev, "Failed to generate persistent MAC address, ignoring.");
594 a = HW_ADDR_NULL;
595 goto finalize;
596 }
597
598 break;
599 case ARPHRD_INFINIBAND:
600 if (result == 0) {
601 log_netdev_warning(netdev, "Failed to generate persistent MAC address.");
602 goto finalize;
603 }
604
605 assert(a.length >= sizeof(result));
606 memzero(a.bytes, a.length - sizeof(result));
607 memcpy(a.bytes + a.length - sizeof(result), &result, sizeof(result));
608 break;
609 default:
610 assert_not_reached();
611 }
612
613 } else {
614 a = *hw_addr;
615 is_static = true;
616 }
617
618 r = net_verify_hardware_address(name, is_static, NETDEV_VTABLE(netdev)->iftype,
619 parent ? &parent->hw_addr : NULL, &a);
620 if (r < 0)
621 return r;
622
623 finalize:
624 *ret = a;
625 return 0;
626 }
627
628 static bool netdev_can_set_mac(NetDev *netdev, const struct hw_addr_data *hw_addr) {
629 assert(netdev);
630 assert(netdev->manager);
631 assert(hw_addr);
632
633 if (hw_addr->length <= 0)
634 return false;
635
636 Link *link;
637 if (link_get_by_index(netdev->manager, netdev->ifindex, &link) < 0)
638 return true; /* The netdev does not exist yet. We can set MAC address. */
639
640 if (hw_addr_equal(&link->hw_addr, hw_addr))
641 return false; /* Unchanged, not necessary to set. */
642
643 /* Some netdevs refuse to update MAC address even if the interface is not running, e.g. ipvlan.
644 * Some other netdevs have the IFF_LIVE_ADDR_CHANGE flag and can update update MAC address even if
645 * the interface is running, e.g. dummy. For those cases, use custom checkers. */
646 if (NETDEV_VTABLE(netdev)->can_set_mac)
647 return NETDEV_VTABLE(netdev)->can_set_mac(netdev, hw_addr);
648
649 /* Before ad72c4a06acc6762e84994ac2f722da7a07df34e and 0ec92a8f56ff07237dbe8af7c7a72aba7f957baf
650 * (both in v6.5), the kernel refuse to set MAC address for existing netdevs even if it is unchanged.
651 * So, by default, do not update MAC address if the it is running. See eth_prepare_mac_addr_change(),
652 * which is called by eth_mac_addr(). Note, the result of netif_running() is mapped to operstate
653 * and flags. See rtnl_fill_ifinfo() and dev_get_flags(). */
654 return link->kernel_operstate == IF_OPER_DOWN &&
655 (link->flags & (IFF_RUNNING | IFF_LOWER_UP | IFF_DORMANT)) == 0;
656 }
657
658 static bool netdev_can_set_mtu(NetDev *netdev, uint32_t mtu) {
659 assert(netdev);
660
661 if (mtu <= 0)
662 return false;
663
664 Link *link;
665 if (link_get_by_index(netdev->manager, netdev->ifindex, &link) < 0)
666 return true; /* The netdev does not exist yet. We can set MTU. */
667
668 if (mtu < link->min_mtu || link->max_mtu < mtu)
669 return false; /* The MTU is out of range. */
670
671 if (link->mtu == mtu)
672 return false; /* Unchanged, not necessary to set. */
673
674 /* Some netdevs cannot change MTU, e.g. vxlan. Let's use the custom checkers in such cases. */
675 if (NETDEV_VTABLE(netdev)->can_set_mtu)
676 return NETDEV_VTABLE(netdev)->can_set_mtu(netdev, mtu);
677
678 /* By default, allow to update the MTU. */
679 return true;
680 }
681
682 static int netdev_create_message(NetDev *netdev, Link *link, sd_netlink_message *m) {
683 int r;
684
685 if (netdev->ifindex <= 0) {
686 /* Set interface name when it is newly created. Otherwise, the kernel older than
687 * bd039b5ea2a91ea707ee8539df26456bd5be80af (v6.2) will refuse the netlink message even if
688 * the name is unchanged. */
689 r = sd_netlink_message_append_string(m, IFLA_IFNAME, netdev->ifname);
690 if (r < 0)
691 return r;
692 }
693
694 struct hw_addr_data hw_addr;
695 r = netdev_generate_hw_addr(netdev, link, netdev->ifname, &netdev->hw_addr, &hw_addr);
696 if (r < 0)
697 return r;
698
699 if (netdev_can_set_mac(netdev, &hw_addr)) {
700 log_netdev_debug(netdev, "Using MAC address: %s", HW_ADDR_TO_STR(&hw_addr));
701 r = netlink_message_append_hw_addr(m, IFLA_ADDRESS, &hw_addr);
702 if (r < 0)
703 return r;
704 }
705
706 if (netdev_can_set_mtu(netdev, netdev->mtu)) {
707 r = sd_netlink_message_append_u32(m, IFLA_MTU, netdev->mtu);
708 if (r < 0)
709 return r;
710 }
711
712 if (link) {
713 r = sd_netlink_message_append_u32(m, IFLA_LINK, link->ifindex);
714 if (r < 0)
715 return r;
716 }
717
718 r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
719 if (r < 0)
720 return r;
721
722 if (NETDEV_VTABLE(netdev)->fill_message_create) {
723 r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, netdev_kind_to_string(netdev->kind));
724 if (r < 0)
725 return r;
726
727 r = NETDEV_VTABLE(netdev)->fill_message_create(netdev, link, m);
728 if (r < 0)
729 return r;
730
731 r = sd_netlink_message_close_container(m);
732 if (r < 0)
733 return r;
734 } else {
735 r = sd_netlink_message_append_string(m, IFLA_INFO_KIND, netdev_kind_to_string(netdev->kind));
736 if (r < 0)
737 return r;
738 }
739
740 r = sd_netlink_message_close_container(m);
741 if (r < 0)
742 return r;
743
744 return 0;
745 }
746
747 static int independent_netdev_create(NetDev *netdev) {
748 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
749 int r;
750
751 assert(netdev);
752 assert(netdev->manager);
753
754 /* create netdev */
755 if (NETDEV_VTABLE(netdev)->create) {
756 r = NETDEV_VTABLE(netdev)->create(netdev);
757 if (r < 0)
758 return r;
759
760 log_netdev_debug(netdev, "Created");
761 return 0;
762 }
763
764 r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, netdev->ifindex);
765 if (r < 0)
766 return r;
767
768 r = netdev_create_message(netdev, NULL, m);
769 if (r < 0)
770 return r;
771
772 r = netlink_call_async(netdev->manager->rtnl, NULL, m, netdev_create_handler,
773 netdev_destroy_callback, netdev);
774 if (r < 0)
775 return r;
776
777 netdev_ref(netdev);
778
779 netdev->state = NETDEV_STATE_CREATING;
780 log_netdev_debug(netdev, "Creating");
781 return 0;
782 }
783
784 static int stacked_netdev_create(NetDev *netdev, Link *link, Request *req) {
785 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
786 int r;
787
788 assert(netdev);
789 assert(netdev->manager);
790 assert(link);
791 assert(req);
792
793 r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, netdev->ifindex);
794 if (r < 0)
795 return r;
796
797 r = netdev_create_message(netdev, link, m);
798 if (r < 0)
799 return r;
800
801 r = request_call_netlink_async(netdev->manager->rtnl, m, req);
802 if (r < 0)
803 return r;
804
805 netdev->state = NETDEV_STATE_CREATING;
806 log_netdev_debug(netdev, "Creating");
807 return 0;
808 }
809
810 static bool link_is_ready_to_create_stacked_netdev_one(Link *link, bool allow_unmanaged) {
811 assert(link);
812
813 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED, LINK_STATE_UNMANAGED))
814 return false;
815
816 if (!link->network)
817 return allow_unmanaged;
818
819 if (link->set_link_messages > 0)
820 return false;
821
822 /* If stacked netdevs are created before the underlying interface being activated, then
823 * the activation policy for the netdevs are ignored. See issue #22593. */
824 if (!link->activated)
825 return false;
826
827 return true;
828 }
829
830 static bool link_is_ready_to_create_stacked_netdev(Link *link) {
831 return check_ready_for_all_sr_iov_ports(link, /* allow_unmanaged= */ false,
832 link_is_ready_to_create_stacked_netdev_one);
833 }
834
835 static int netdev_is_ready_to_create(NetDev *netdev, Link *link) {
836 assert(netdev);
837
838 if (link && !link_is_ready_to_create_stacked_netdev(link))
839 return false;
840
841 if (NETDEV_VTABLE(netdev)->is_ready_to_create)
842 return NETDEV_VTABLE(netdev)->is_ready_to_create(netdev, link);
843
844 return true;
845 }
846
847 static int stacked_netdev_process_request(Request *req, Link *link, void *userdata) {
848 NetDev *netdev = ASSERT_PTR(userdata);
849 int r;
850
851 assert(req);
852 assert(link);
853
854 if (!netdev_is_managed(netdev))
855 goto cancelled; /* Already detached, due to e.g. reloading .netdev files, cancelling the request. */
856
857 if (NETDEV_VTABLE(netdev)->keep_existing && netdev->ifindex > 0) {
858 /* Already exists, and the netdev does not support updating, entering the ready state. */
859 r = netdev_enter_ready(netdev);
860 if (r < 0)
861 return r;
862
863 goto cancelled;
864 }
865
866 r = netdev_is_ready_to_create(netdev, link);
867 if (r <= 0)
868 return r;
869
870 r = stacked_netdev_create(netdev, link, req);
871 if (r < 0)
872 return log_netdev_warning_errno(netdev, r, "Failed to create netdev: %m");
873
874 return 1;
875
876 cancelled:
877 assert_se(TAKE_PTR(req->counter) == &link->create_stacked_netdev_messages);
878 link->create_stacked_netdev_messages--;
879
880 if (link->create_stacked_netdev_messages == 0) {
881 link->stacked_netdevs_created = true;
882 log_link_debug(link, "Stacked netdevs created.");
883 link_check_ready(link);
884 }
885
886 return 1;
887 }
888
889 static int create_stacked_netdev_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, void *userdata) {
890 NetDev *netdev = ASSERT_PTR(userdata);
891 int r;
892
893 assert(m);
894 assert(link);
895
896 r = sd_netlink_message_get_errno(m);
897 if (r >= 0)
898 log_netdev_debug(netdev, "Created.");
899 else if (r == -EEXIST && netdev->ifindex > 0)
900 log_netdev_debug(netdev, "Already exists.");
901 else {
902 log_netdev_warning_errno(netdev, r, "Failed to create netdev: %m");
903 netdev_enter_failed(netdev);
904 link_enter_failed(link);
905 return 0;
906 }
907
908 (void) netdev_enter_ready(netdev);
909
910 if (link->create_stacked_netdev_messages == 0) {
911 link->stacked_netdevs_created = true;
912 log_link_debug(link, "Stacked netdevs created.");
913 link_check_ready(link);
914 }
915
916 return 0;
917 }
918
919 int link_request_stacked_netdev(Link *link, NetDev *netdev) {
920 int r;
921
922 assert(link);
923 assert(netdev);
924
925 if (!netdev_is_stacked(netdev))
926 return -EINVAL;
927
928 if (!netdev_is_managed(netdev))
929 return 0; /* Already detached, due to e.g. reloading .netdev files. */
930
931 link->stacked_netdevs_created = false;
932 r = link_queue_request_full(link, REQUEST_TYPE_NETDEV_STACKED,
933 netdev, (mfree_func_t) netdev_unref,
934 trivial_hash_func, trivial_compare_func,
935 stacked_netdev_process_request,
936 &link->create_stacked_netdev_messages,
937 create_stacked_netdev_handler, NULL);
938 if (r < 0)
939 return log_link_error_errno(link, r, "Failed to request stacked netdev '%s': %m",
940 netdev->ifname);
941 if (r == 0)
942 return 0;
943
944 netdev_ref(netdev);
945 log_link_debug(link, "Requested stacked netdev '%s'", netdev->ifname);
946 return 1;
947 }
948
949 static int independent_netdev_process_request(Request *req, Link *link, void *userdata) {
950 NetDev *netdev = ASSERT_PTR(userdata);
951 int r;
952
953 assert(!link);
954
955 if (!netdev_is_managed(netdev))
956 return 1; /* Already detached, due to e.g. reloading .netdev files, cancelling the request. */
957
958 if (NETDEV_VTABLE(netdev)->keep_existing && netdev->ifindex > 0) {
959 /* Already exists, and the netdev does not support updating, entering the ready state. */
960 r = netdev_enter_ready(netdev);
961 if (r < 0)
962 return r;
963
964 return 1; /* Skip this request. */
965 }
966
967 r = netdev_is_ready_to_create(netdev, NULL);
968 if (r <= 0)
969 return r;
970
971 r = independent_netdev_create(netdev);
972 if (r < 0)
973 return log_netdev_warning_errno(netdev, r, "Failed to create netdev: %m");
974
975 return 1;
976 }
977
978 static int netdev_request_to_create(NetDev *netdev) {
979 int r;
980
981 assert(netdev);
982 assert(netdev->manager);
983
984 if (netdev->manager->test_mode)
985 return 0;
986
987 if (netdev_is_stacked(netdev))
988 return 0;
989
990 if (!netdev_is_managed(netdev))
991 return 0; /* Already detached, due to e.g. reloading .netdev files. */
992
993 if (netdev->state != NETDEV_STATE_LOADING)
994 return 0; /* Already configured (at least tried previously). Not necessary to reconfigure. */
995
996 r = netdev_queue_request(netdev, independent_netdev_process_request, NULL);
997 if (r < 0)
998 return log_netdev_warning_errno(netdev, r, "Failed to request to create netdev: %m");
999
1000 return 0;
1001 }
1002
1003 int netdev_load_one(Manager *manager, const char *filename, NetDev **ret) {
1004 _cleanup_(netdev_unrefp) NetDev *netdev_raw = NULL, *netdev = NULL;
1005 _cleanup_free_ char *file_basename = NULL;
1006 const char *dropin_dirname;
1007 int r;
1008
1009 assert(manager);
1010 assert(filename);
1011 assert(ret);
1012
1013 r = null_or_empty_path(filename);
1014 if (r < 0)
1015 return log_warning_errno(r, "Failed to check if \"%s\" is empty: %m", filename);
1016 if (r > 0)
1017 return log_debug_errno(SYNTHETIC_ERRNO(ENOENT), "Skipping empty file: %s", filename);
1018
1019 netdev_raw = new(NetDev, 1);
1020 if (!netdev_raw)
1021 return log_oom();
1022
1023 *netdev_raw = (NetDev) {
1024 .n_ref = 1,
1025 .kind = _NETDEV_KIND_INVALID,
1026 .state = _NETDEV_STATE_INVALID, /* an invalid state means done() of the implementation won't be called on destruction */
1027 };
1028
1029 r = path_extract_filename(filename, &file_basename);
1030 if (r < 0)
1031 return log_warning_errno(r, "Failed to extract file name of '%s': %m", filename);
1032
1033 dropin_dirname = strjoina(file_basename, ".d");
1034 r = config_parse_many(
1035 STRV_MAKE_CONST(filename),
1036 NETWORK_DIRS,
1037 dropin_dirname,
1038 NETDEV_COMMON_SECTIONS NETDEV_OTHER_SECTIONS,
1039 config_item_perf_lookup,
1040 network_netdev_gperf_lookup,
1041 CONFIG_PARSE_WARN,
1042 netdev_raw);
1043 if (r < 0)
1044 return r; /* config_parse_many() logs internally. */
1045
1046 /* skip out early if configuration does not match the environment */
1047 if (!condition_test_list(netdev_raw->conditions, environ, NULL, NULL, NULL))
1048 return log_debug_errno(SYNTHETIC_ERRNO(ESTALE), "%s: Conditions in the file do not match the system environment, skipping.", filename);
1049
1050 if (netdev_raw->kind == _NETDEV_KIND_INVALID)
1051 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "NetDev has no Kind= configured in \"%s\", ignoring.", filename);
1052
1053 if (!netdev_raw->ifname)
1054 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "NetDev without Name= configured in \"%s\", ignoring.", filename);
1055
1056 netdev = malloc0(NETDEV_VTABLE(netdev_raw)->object_size);
1057 if (!netdev)
1058 return log_oom();
1059
1060 netdev->n_ref = 1;
1061 netdev->manager = manager;
1062 netdev->kind = netdev_raw->kind;
1063 netdev->state = NETDEV_STATE_LOADING; /* we initialize the state here for the first time,
1064 so that done() will be called on destruction */
1065
1066 if (NETDEV_VTABLE(netdev)->init)
1067 NETDEV_VTABLE(netdev)->init(netdev);
1068
1069 r = config_parse_many_full(
1070 STRV_MAKE_CONST(filename),
1071 NETWORK_DIRS,
1072 dropin_dirname,
1073 /* root= */ NULL,
1074 /* root_fd= */ -EBADF,
1075 NETDEV_VTABLE(netdev)->sections,
1076 config_item_perf_lookup,
1077 network_netdev_gperf_lookup,
1078 CONFIG_PARSE_WARN,
1079 netdev,
1080 &netdev->stats_by_path,
1081 &netdev->dropins);
1082 if (r < 0)
1083 return r; /* config_parse_many() logs internally. */
1084
1085 /* verify configuration */
1086 if (NETDEV_VTABLE(netdev)->config_verify) {
1087 r = NETDEV_VTABLE(netdev)->config_verify(netdev, filename);
1088 if (r < 0)
1089 return r; /* config_verify() logs internally. */
1090 }
1091
1092 netdev->filename = strdup(filename);
1093 if (!netdev->filename)
1094 return log_oom();
1095
1096 log_syntax(/* unit= */ NULL, LOG_DEBUG, filename, /* config_line= */ 0, /* error= */ 0, "Successfully loaded.");
1097
1098 *ret = TAKE_PTR(netdev);
1099 return 0;
1100 }
1101
1102 int netdev_load(Manager *manager) {
1103 _cleanup_strv_free_ char **files = NULL;
1104 int r;
1105
1106 assert(manager);
1107
1108 r = conf_files_list_strv(&files, ".netdev", /* root= */ NULL, CONF_FILES_WARN, NETWORK_DIRS);
1109 if (r < 0)
1110 return log_error_errno(r, "Failed to enumerate netdev files: %m");
1111
1112 STRV_FOREACH(f, files) {
1113 _cleanup_(netdev_unrefp) NetDev *netdev = NULL;
1114
1115 if (netdev_load_one(manager, *f, &netdev) < 0)
1116 continue;
1117
1118 if (netdev_attach(netdev) < 0)
1119 continue;
1120
1121 if (netdev_request_to_create(netdev) < 0)
1122 continue;
1123
1124 TAKE_PTR(netdev);
1125 }
1126
1127 return 0;
1128 }
1129
1130 int netdev_reload(Manager *manager) {
1131 _cleanup_hashmap_free_ Hashmap *new_netdevs = NULL;
1132 _cleanup_strv_free_ char **files = NULL;
1133 int r;
1134
1135 assert(manager);
1136
1137 r = conf_files_list_strv(&files, ".netdev", /* root= */ NULL, CONF_FILES_WARN, NETWORK_DIRS);
1138 if (r < 0)
1139 return log_error_errno(r, "Failed to enumerate netdev files: %m");
1140
1141 STRV_FOREACH(f, files) {
1142 _cleanup_(netdev_unrefp) NetDev *netdev = NULL;
1143 NetDev *old;
1144
1145 if (netdev_load_one(manager, *f, &netdev) < 0)
1146 continue;
1147
1148 if (netdev_get(manager, netdev->ifname, &old) < 0) {
1149 log_netdev_debug(netdev, "Found new .netdev file: %s", netdev->filename);
1150
1151 if (netdev_attach_name_full(netdev, netdev->ifname, &new_netdevs) >= 0)
1152 TAKE_PTR(netdev);
1153
1154 continue;
1155 }
1156
1157 if (!stats_by_path_equal(netdev->stats_by_path, old->stats_by_path)) {
1158 log_netdev_debug(netdev, "Found updated .netdev file: %s", netdev->filename);
1159
1160 /* Copy ifindex. */
1161 netdev->ifindex = old->ifindex;
1162
1163 if (netdev_attach_name_full(netdev, netdev->ifname, &new_netdevs) >= 0)
1164 TAKE_PTR(netdev);
1165
1166 continue;
1167 }
1168
1169 /* Keep the original object, and drop the new one. */
1170 if (netdev_attach_name_full(old, old->ifname, &new_netdevs) >= 0)
1171 netdev_ref(old);
1172 }
1173
1174 /* Detach old NetDev objects from Manager.
1175 * The same object may be registered with multiple names, and netdev_detach() may drop multiple entries. */
1176 for (NetDev *n; (n = hashmap_first(manager->netdevs)); )
1177 netdev_detach(n);
1178
1179 /* Attach new NetDev objects to Manager. */
1180 for (;;) {
1181 _cleanup_(netdev_unrefp) NetDev *netdev = hashmap_steal_first(new_netdevs);
1182 if (!netdev)
1183 break;
1184
1185 netdev->manager = manager;
1186 if (netdev_attach(netdev) < 0)
1187 continue;
1188
1189 /* Create a new netdev or update existing netdev, */
1190 if (netdev_request_to_create(netdev) < 0)
1191 continue;
1192
1193 TAKE_PTR(netdev);
1194 }
1195
1196 /* Reassign NetDev objects to Link object. */
1197 Link *link;
1198 HASHMAP_FOREACH(link, manager->links_by_index)
1199 link_assign_netdev(link);
1200
1201 return 0;
1202 }
1203
1204 int config_parse_netdev_kind(
1205 const char *unit,
1206 const char *filename,
1207 unsigned line,
1208 const char *section,
1209 unsigned section_line,
1210 const char *lvalue,
1211 int ltype,
1212 const char *rvalue,
1213 void *data,
1214 void *userdata) {
1215
1216 NetDevKind k, *kind = ASSERT_PTR(data);
1217
1218 assert(filename);
1219 assert(rvalue);
1220
1221 k = netdev_kind_from_string(rvalue);
1222 if (k < 0) {
1223 log_syntax(unit, LOG_WARNING, filename, line, k, "Failed to parse netdev kind, ignoring assignment: %s", rvalue);
1224 return 0;
1225 }
1226
1227 if (*kind != _NETDEV_KIND_INVALID && *kind != k) {
1228 log_syntax(unit, LOG_WARNING, filename, line, 0,
1229 "Specified netdev kind is different from the previous value '%s', ignoring assignment: %s",
1230 netdev_kind_to_string(*kind), rvalue);
1231 return 0;
1232 }
1233
1234 *kind = k;
1235
1236 return 0;
1237 }
1238
1239 int config_parse_netdev_hw_addr(
1240 const char *unit,
1241 const char *filename,
1242 unsigned line,
1243 const char *section,
1244 unsigned section_line,
1245 const char *lvalue,
1246 int ltype,
1247 const char *rvalue,
1248 void *data,
1249 void *userdata) {
1250
1251 struct hw_addr_data *hw_addr = ASSERT_PTR(data);
1252
1253 assert(rvalue);
1254
1255 if (streq(rvalue, "none")) {
1256 *hw_addr = HW_ADDR_NONE;
1257 return 0;
1258 }
1259
1260 return config_parse_hw_addr(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata);
1261 }