]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-link.c
network,udev/net: add Kind= settings in [Match] section
[thirdparty/systemd.git] / src / network / networkd-link.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <net/if.h>
4 #include <netinet/in.h>
5 #include <linux/if.h>
6 #include <linux/if_arp.h>
7 #include <linux/if_link.h>
8 #include <linux/netdevice.h>
9 #include <sys/socket.h>
10 #include <unistd.h>
11
12 #include "alloc-util.h"
13 #include "arphrd-util.h"
14 #include "batadv.h"
15 #include "bond.h"
16 #include "bridge.h"
17 #include "bus-util.h"
18 #include "device-private.h"
19 #include "device-util.h"
20 #include "dhcp-identifier.h"
21 #include "dhcp-lease-internal.h"
22 #include "env-file.h"
23 #include "ethtool-util.h"
24 #include "event-util.h"
25 #include "fd-util.h"
26 #include "fileio.h"
27 #include "format-util.h"
28 #include "fs-util.h"
29 #include "ipvlan.h"
30 #include "missing_network.h"
31 #include "netlink-util.h"
32 #include "network-internal.h"
33 #include "networkd-address-label.h"
34 #include "networkd-address.h"
35 #include "networkd-bridge-fdb.h"
36 #include "networkd-bridge-mdb.h"
37 #include "networkd-can.h"
38 #include "networkd-dhcp-prefix-delegation.h"
39 #include "networkd-dhcp-server.h"
40 #include "networkd-dhcp4.h"
41 #include "networkd-dhcp6.h"
42 #include "networkd-ipv4acd.h"
43 #include "networkd-ipv4ll.h"
44 #include "networkd-ipv6-proxy-ndp.h"
45 #include "networkd-link-bus.h"
46 #include "networkd-link.h"
47 #include "networkd-lldp-tx.h"
48 #include "networkd-manager.h"
49 #include "networkd-ndisc.h"
50 #include "networkd-neighbor.h"
51 #include "networkd-nexthop.h"
52 #include "networkd-queue.h"
53 #include "networkd-radv.h"
54 #include "networkd-route.h"
55 #include "networkd-routing-policy-rule.h"
56 #include "networkd-setlink.h"
57 #include "networkd-sriov.h"
58 #include "networkd-state-file.h"
59 #include "networkd-sysctl.h"
60 #include "set.h"
61 #include "socket-util.h"
62 #include "stat-util.h"
63 #include "stdio-util.h"
64 #include "string-table.h"
65 #include "strv.h"
66 #include "tc.h"
67 #include "tmpfile-util.h"
68 #include "udev-util.h"
69 #include "util.h"
70 #include "vrf.h"
71
72 bool link_ipv4ll_enabled(Link *link) {
73 assert(link);
74
75 if (link->flags & IFF_LOOPBACK)
76 return false;
77
78 if (!link->network)
79 return false;
80
81 if (link->iftype == ARPHRD_CAN)
82 return false;
83
84 if (link->hw_addr.length != ETH_ALEN)
85 return false;
86
87 if (ether_addr_is_null(&link->hw_addr.ether))
88 return false;
89
90 /* ARPHRD_INFINIBAND seems to potentially support IPv4LL.
91 * But currently sd-ipv4ll and sd-ipv4acd only support ARPHRD_ETHER. */
92 if (link->iftype != ARPHRD_ETHER)
93 return false;
94
95 if (streq_ptr(link->kind, "vrf"))
96 return false;
97
98 /* L3 or L3S mode do not support ARP. */
99 if (IN_SET(link_get_ipvlan_mode(link), NETDEV_IPVLAN_MODE_L3, NETDEV_IPVLAN_MODE_L3S))
100 return false;
101
102 if (link->network->bond)
103 return false;
104
105 return link->network->link_local & ADDRESS_FAMILY_IPV4;
106 }
107
108 bool link_ipv6_enabled(Link *link) {
109 assert(link);
110
111 if (!socket_ipv6_is_supported())
112 return false;
113
114 if (link->network->bond)
115 return false;
116
117 if (link->iftype == ARPHRD_CAN)
118 return false;
119
120 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
121 if (link_ipv6ll_enabled(link))
122 return true;
123
124 if (network_has_static_ipv6_configurations(link->network))
125 return true;
126
127 return false;
128 }
129
130 bool link_is_ready_to_configure(Link *link, bool allow_unmanaged) {
131 assert(link);
132
133 if (!link->network) {
134 if (!allow_unmanaged)
135 return false;
136
137 return link_has_carrier(link);
138 }
139
140 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
141 return false;
142
143 if (!link->network->configure_without_carrier) {
144 if (link->set_flags_messages > 0)
145 return false;
146
147 if (!link_has_carrier(link))
148 return false;
149 }
150
151 if (link->set_link_messages > 0)
152 return false;
153
154 if (!link->activated)
155 return false;
156
157 return true;
158 }
159
160 void link_ntp_settings_clear(Link *link) {
161 link->ntp = strv_free(link->ntp);
162 }
163
164 void link_dns_settings_clear(Link *link) {
165 if (link->n_dns != UINT_MAX)
166 for (unsigned i = 0; i < link->n_dns; i++)
167 in_addr_full_free(link->dns[i]);
168 link->dns = mfree(link->dns);
169 link->n_dns = UINT_MAX;
170
171 link->search_domains = ordered_set_free(link->search_domains);
172 link->route_domains = ordered_set_free(link->route_domains);
173
174 link->dns_default_route = -1;
175 link->llmnr = _RESOLVE_SUPPORT_INVALID;
176 link->mdns = _RESOLVE_SUPPORT_INVALID;
177 link->dnssec_mode = _DNSSEC_MODE_INVALID;
178 link->dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID;
179
180 link->dnssec_negative_trust_anchors = set_free_free(link->dnssec_negative_trust_anchors);
181 }
182
183 static void link_free_engines(Link *link) {
184 if (!link)
185 return;
186
187 link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server);
188 link->dhcp_client = sd_dhcp_client_unref(link->dhcp_client);
189 link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
190 link->dhcp4_6rd_tunnel_name = mfree(link->dhcp4_6rd_tunnel_name);
191
192 link->lldp_rx = sd_lldp_rx_unref(link->lldp_rx);
193 link->lldp_tx = sd_lldp_tx_unref(link->lldp_tx);
194
195 ndisc_flush(link);
196
197 link->ipv4ll = sd_ipv4ll_unref(link->ipv4ll);
198 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
199 link->dhcp6_lease = sd_dhcp6_lease_unref(link->dhcp6_lease);
200 link->ndisc = sd_ndisc_unref(link->ndisc);
201 link->radv = sd_radv_unref(link->radv);
202 }
203
204 static Link *link_free(Link *link) {
205 assert(link);
206
207 link_ntp_settings_clear(link);
208 link_dns_settings_clear(link);
209
210 link->routes = set_free(link->routes);
211 link->nexthops = set_free(link->nexthops);
212 link->neighbors = set_free(link->neighbors);
213 link->addresses = set_free(link->addresses);
214 link->traffic_control = set_free(link->traffic_control);
215
216 link->dhcp_pd_prefixes = set_free(link->dhcp_pd_prefixes);
217
218 link_free_engines(link);
219
220 free(link->ifname);
221 strv_free(link->alternative_names);
222 free(link->kind);
223 free(link->ssid);
224 free(link->previous_ssid);
225 free(link->driver);
226
227 unlink_and_free(link->lease_file);
228 unlink_and_free(link->lldp_file);
229 unlink_and_free(link->state_file);
230
231 sd_device_unref(link->sd_device);
232
233 hashmap_free(link->bound_to_links);
234 hashmap_free(link->bound_by_links);
235
236 set_free_with_destructor(link->slaves, link_unref);
237
238 network_unref(link->network);
239
240 sd_event_source_disable_unref(link->carrier_lost_timer);
241
242 return mfree(link);
243 }
244
245 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free);
246
247 int link_get_by_index(Manager *m, int ifindex, Link **ret) {
248 Link *link;
249
250 assert(m);
251 assert(ifindex > 0);
252
253 link = hashmap_get(m->links_by_index, INT_TO_PTR(ifindex));
254 if (!link)
255 return -ENODEV;
256
257 if (ret)
258 *ret = link;
259 return 0;
260 }
261
262 int link_get_by_name(Manager *m, const char *ifname, Link **ret) {
263 Link *link;
264
265 assert(m);
266 assert(ifname);
267
268 link = hashmap_get(m->links_by_name, ifname);
269 if (!link)
270 return -ENODEV;
271
272 if (ret)
273 *ret = link;
274 return 0;
275 }
276
277 int link_get_by_hw_addr(Manager *m, const struct hw_addr_data *hw_addr, Link **ret) {
278 Link *link;
279
280 assert(m);
281 assert(hw_addr);
282
283 link = hashmap_get(m->links_by_hw_addr, hw_addr);
284 if (!link)
285 return -ENODEV;
286
287 if (ret)
288 *ret = link;
289 return 0;
290 }
291
292 int link_get_master(Link *link, Link **ret) {
293 assert(link);
294 assert(link->manager);
295 assert(ret);
296
297 if (link->master_ifindex <= 0 || link->master_ifindex == link->ifindex)
298 return -ENODEV;
299
300 return link_get_by_index(link->manager, link->master_ifindex, ret);
301 }
302
303 void link_set_state(Link *link, LinkState state) {
304 assert(link);
305
306 if (link->state == state)
307 return;
308
309 log_link_debug(link, "State changed: %s -> %s",
310 link_state_to_string(link->state),
311 link_state_to_string(state));
312
313 link->state = state;
314
315 link_send_changed(link, "AdministrativeState", NULL);
316 link_dirty(link);
317 }
318
319 int link_stop_engines(Link *link, bool may_keep_dhcp) {
320 int r = 0, k;
321
322 assert(link);
323 assert(link->manager);
324 assert(link->manager->event);
325
326 bool keep_dhcp = may_keep_dhcp &&
327 link->network &&
328 !link->network->dhcp_send_decline && /* IPv4 ACD for the DHCPv4 address is running. */
329 (link->manager->restarting ||
330 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP_ON_STOP));
331
332 if (!keep_dhcp) {
333 k = sd_dhcp_client_stop(link->dhcp_client);
334 if (k < 0)
335 r = log_link_warning_errno(link, k, "Could not stop DHCPv4 client: %m");
336 }
337
338 k = sd_dhcp_server_stop(link->dhcp_server);
339 if (k < 0)
340 r = log_link_warning_errno(link, k, "Could not stop DHCPv4 server: %m");
341
342 k = sd_lldp_rx_stop(link->lldp_rx);
343 if (k < 0)
344 r = log_link_warning_errno(link, k, "Could not stop LLDP Rx: %m");
345
346 k = sd_lldp_tx_stop(link->lldp_tx);
347 if (k < 0)
348 r = log_link_warning_errno(link, k, "Could not stop LLDP Tx: %m");
349
350 k = sd_ipv4ll_stop(link->ipv4ll);
351 if (k < 0)
352 r = log_link_warning_errno(link, k, "Could not stop IPv4 link-local: %m");
353
354 k = ipv4acd_stop(link);
355 if (k < 0)
356 r = log_link_warning_errno(link, k, "Could not stop IPv4 ACD client: %m");
357
358 k = sd_dhcp6_client_stop(link->dhcp6_client);
359 if (k < 0)
360 r = log_link_warning_errno(link, k, "Could not stop DHCPv6 client: %m");
361
362 k = dhcp_pd_remove(link, /* only_marked = */ false);
363 if (k < 0)
364 r = log_link_warning_errno(link, k, "Could not remove DHCPv6 PD addresses and routes: %m");
365
366 k = sd_ndisc_stop(link->ndisc);
367 if (k < 0)
368 r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Discovery: %m");
369
370 ndisc_flush(link);
371
372 k = sd_radv_stop(link->radv);
373 if (k < 0)
374 r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Advertisement: %m");
375
376 return r;
377 }
378
379 void link_enter_failed(Link *link) {
380 assert(link);
381
382 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
383 return;
384
385 log_link_warning(link, "Failed");
386
387 link_set_state(link, LINK_STATE_FAILED);
388
389 (void) link_stop_engines(link, false);
390 }
391
392 void link_check_ready(Link *link) {
393 Address *a;
394
395 assert(link);
396
397 if (link->state == LINK_STATE_CONFIGURED)
398 return;
399
400 if (link->state != LINK_STATE_CONFIGURING)
401 return (void) log_link_debug(link, "%s(): link is in %s state.", __func__, link_state_to_string(link->state));
402
403 if (!link->network)
404 return (void) log_link_debug(link, "%s(): link is unmanaged.", __func__);
405
406 if (!link->tc_configured)
407 return (void) log_link_debug(link, "%s(): traffic controls are not configured.", __func__);
408
409 if (link->set_link_messages > 0)
410 return (void) log_link_debug(link, "%s(): link layer is configuring.", __func__);
411
412 if (!link->activated)
413 return (void) log_link_debug(link, "%s(): link is not activated.", __func__);
414
415 if (link->iftype == ARPHRD_CAN) {
416 /* let's shortcut things for CAN which doesn't need most of checks below. */
417 link_set_state(link, LINK_STATE_CONFIGURED);
418 return;
419 }
420
421 if (!link->stacked_netdevs_created)
422 return (void) log_link_debug(link, "%s(): stacked netdevs are not created.", __func__);
423
424 if (!link->static_addresses_configured)
425 return (void) log_link_debug(link, "%s(): static addresses are not configured.", __func__);
426
427 SET_FOREACH(a, link->addresses)
428 if (!address_is_ready(a)) {
429 _cleanup_free_ char *str = NULL;
430
431 (void) in_addr_prefix_to_string(a->family, &a->in_addr, a->prefixlen, &str);
432 return (void) log_link_debug(link, "%s(): address %s is not ready.", __func__, strna(str));
433 }
434
435 if (!link->static_address_labels_configured)
436 return (void) log_link_debug(link, "%s(): static address labels are not configured.", __func__);
437
438 if (!link->static_bridge_fdb_configured)
439 return (void) log_link_debug(link, "%s(): static bridge MDB entries are not configured.", __func__);
440
441 if (!link->static_bridge_mdb_configured)
442 return (void) log_link_debug(link, "%s(): static bridge MDB entries are not configured.", __func__);
443
444 if (!link->static_ipv6_proxy_ndp_configured)
445 return (void) log_link_debug(link, "%s(): static IPv6 proxy NDP addresses are not configured.", __func__);
446
447 if (!link->static_neighbors_configured)
448 return (void) log_link_debug(link, "%s(): static neighbors are not configured.", __func__);
449
450 if (!link->static_nexthops_configured)
451 return (void) log_link_debug(link, "%s(): static nexthops are not configured.", __func__);
452
453 if (!link->static_routes_configured)
454 return (void) log_link_debug(link, "%s(): static routes are not configured.", __func__);
455
456 if (!link->static_routing_policy_rules_configured)
457 return (void) log_link_debug(link, "%s(): static routing policy rules are not configured.", __func__);
458
459 if (!link->sr_iov_configured)
460 return (void) log_link_debug(link, "%s(): SR-IOV is not configured.", __func__);
461
462 /* IPv6LL is assigned after the link gains its carrier. */
463 if (!link->network->configure_without_carrier &&
464 link_ipv6ll_enabled(link) &&
465 !in6_addr_is_set(&link->ipv6ll_address))
466 return (void) log_link_debug(link, "%s(): IPv6LL is not configured yet.", __func__);
467
468 bool has_dynamic_address = false;
469 SET_FOREACH(a, link->addresses) {
470 if (address_is_marked(a))
471 continue;
472 if (!address_exists(a))
473 continue;
474 if (IN_SET(a->source,
475 NETWORK_CONFIG_SOURCE_IPV4LL,
476 NETWORK_CONFIG_SOURCE_DHCP4,
477 NETWORK_CONFIG_SOURCE_DHCP6,
478 NETWORK_CONFIG_SOURCE_DHCP_PD,
479 NETWORK_CONFIG_SOURCE_NDISC)) {
480 has_dynamic_address = true;
481 break;
482 }
483 }
484
485 if ((link_ipv4ll_enabled(link) || link_dhcp4_enabled(link) || link_dhcp6_with_address_enabled(link) ||
486 (link_dhcp_pd_is_enabled(link) && link->network->dhcp_pd_assign)) && !has_dynamic_address)
487 /* When DHCP[46] or IPv4LL is enabled, at least one address is acquired by them. */
488 return (void) log_link_debug(link, "%s(): DHCPv4, DHCPv6, DHCP-PD or IPv4LL is enabled but no dynamic address is assigned yet.", __func__);
489
490 /* Ignore NDisc when ConfigureWithoutCarrier= is enabled, as IPv6AcceptRA= is enabled by default. */
491 if (link_ipv4ll_enabled(link) || link_dhcp4_enabled(link) ||
492 link_dhcp6_enabled(link) || link_dhcp_pd_is_enabled(link) ||
493 (!link->network->configure_without_carrier && link_ipv6_accept_ra_enabled(link))) {
494
495 if (!link->ipv4ll_address_configured && !link->dhcp4_configured &&
496 !link->dhcp6_configured && !link->dhcp_pd_configured && !link->ndisc_configured)
497 /* When DHCP[46], NDisc, or IPv4LL is enabled, at least one protocol must be finished. */
498 return (void) log_link_debug(link, "%s(): dynamic addresses or routes are not configured.", __func__);
499
500 log_link_debug(link, "%s(): IPv4LL:%s DHCPv4:%s DHCPv6:%s DHCP-PD:%s NDisc:%s",
501 __func__,
502 yes_no(link->ipv4ll_address_configured),
503 yes_no(link->dhcp4_configured),
504 yes_no(link->dhcp6_configured),
505 yes_no(link->dhcp_pd_configured),
506 yes_no(link->ndisc_configured));
507 }
508
509 link_set_state(link, LINK_STATE_CONFIGURED);
510 }
511
512 static int link_request_static_configs(Link *link) {
513 int r;
514
515 assert(link);
516 assert(link->network);
517 assert(link->state != _LINK_STATE_INVALID);
518
519 r = link_request_static_addresses(link);
520 if (r < 0)
521 return r;
522
523 r = link_request_static_address_labels(link);
524 if (r < 0)
525 return r;
526
527 r = link_request_static_bridge_fdb(link);
528 if (r < 0)
529 return r;
530
531 r = link_request_static_bridge_mdb(link);
532 if (r < 0)
533 return r;
534
535 r = link_request_static_ipv6_proxy_ndp_addresses(link);
536 if (r < 0)
537 return r;
538
539 r = link_request_static_neighbors(link);
540 if (r < 0)
541 return r;
542
543 r = link_request_static_nexthops(link, false);
544 if (r < 0)
545 return r;
546
547 r = link_request_static_routes(link, false);
548 if (r < 0)
549 return r;
550
551 r = link_request_static_routing_policy_rules(link);
552 if (r < 0)
553 return r;
554
555 return 0;
556 }
557
558 static int link_request_stacked_netdevs(Link *link) {
559 NetDev *netdev;
560 int r;
561
562 assert(link);
563
564 link->stacked_netdevs_created = false;
565 link->stacked_netdevs_after_configured_created = false;
566
567 HASHMAP_FOREACH(netdev, link->network->stacked_netdevs) {
568 r = link_request_stacked_netdev(link, netdev);
569 if (r < 0)
570 return r;
571 }
572
573 if (link->create_stacked_netdev_messages == 0) {
574 link->stacked_netdevs_created = true;
575 link_check_ready(link);
576 }
577 if (link->create_stacked_netdev_after_configured_messages == 0)
578 link->stacked_netdevs_after_configured_created = true;
579
580 return 0;
581 }
582
583 static int link_acquire_dynamic_ipv6_conf(Link *link) {
584 int r;
585
586 assert(link);
587
588 r = radv_start(link);
589 if (r < 0)
590 return log_link_warning_errno(link, r, "Failed to start IPv6 Router Advertisement engine: %m");
591
592 r = ndisc_start(link);
593 if (r < 0)
594 return log_link_warning_errno(link, r, "Failed to start IPv6 Router Discovery: %m");
595
596 r = dhcp6_start(link);
597 if (r < 0)
598 return log_link_warning_errno(link, r, "Failed to start DHCPv6 client: %m");
599
600 return 0;
601 }
602
603 static int link_acquire_dynamic_ipv4_conf(Link *link) {
604 int r;
605
606 assert(link);
607 assert(link->manager);
608 assert(link->manager->event);
609
610 if (link->dhcp_client) {
611 r = dhcp4_start(link);
612 if (r < 0)
613 return log_link_warning_errno(link, r, "Failed to start DHCPv4 client: %m");
614
615 log_link_debug(link, "Acquiring DHCPv4 lease.");
616
617 } else if (link->ipv4ll) {
618 r = sd_ipv4ll_start(link->ipv4ll);
619 if (r < 0)
620 return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
621
622 log_link_debug(link, "Acquiring IPv4 link-local address.");
623 }
624
625 if (link->dhcp_server) {
626 r = sd_dhcp_server_start(link->dhcp_server);
627 if (r < 0)
628 return log_link_warning_errno(link, r, "Could not start DHCP server: %m");
629 }
630
631 r = ipv4acd_start(link);
632 if (r < 0)
633 return log_link_warning_errno(link, r, "Could not start IPv4 ACD client: %m");
634
635 return 0;
636 }
637
638 static int link_acquire_dynamic_conf(Link *link) {
639 int r;
640
641 assert(link);
642 assert(link->network);
643
644 r = link_acquire_dynamic_ipv4_conf(link);
645 if (r < 0)
646 return r;
647
648 if (in6_addr_is_set(&link->ipv6ll_address)) {
649 r = link_acquire_dynamic_ipv6_conf(link);
650 if (r < 0)
651 return r;
652 }
653
654 if (!link_radv_enabled(link) || !link->network->dhcp_pd_announce) {
655 /* DHCPv6PD downstream does not require IPv6LL address. But may require RADV to be
656 * configured, and RADV may not be configured yet here. Only acquire subnet prefix when
657 * RADV is disabled, or the announcement of the prefix is disabled. Otherwise, the
658 * below will be called in radv_start(). */
659 r = dhcp_request_prefix_delegation(link);
660 if (r < 0)
661 return log_link_warning_errno(link, r, "Failed to request DHCP delegated subnet prefix: %m");
662 }
663
664 if (link->lldp_tx) {
665 r = sd_lldp_tx_start(link->lldp_tx);
666 if (r < 0)
667 return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m");
668 }
669
670 if (link->lldp_rx) {
671 r = sd_lldp_rx_start(link->lldp_rx);
672 if (r < 0)
673 return log_link_warning_errno(link, r, "Failed to start LLDP client: %m");
674 }
675
676 return 0;
677 }
678
679 int link_ipv6ll_gained(Link *link) {
680 int r;
681
682 assert(link);
683
684 log_link_info(link, "Gained IPv6LL");
685
686 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
687 return 0;
688
689 r = link_acquire_dynamic_ipv6_conf(link);
690 if (r < 0)
691 return r;
692
693 link_check_ready(link);
694 return 0;
695 }
696
697 int link_handle_bound_to_list(Link *link) {
698 bool required_up = false;
699 bool link_is_up = false;
700 Link *l;
701
702 assert(link);
703
704 /* If at least one interface in bound_to_links has carrier, then make this interface up.
705 * If all interfaces in bound_to_links do not, then make this interface down. */
706
707 if (hashmap_isempty(link->bound_to_links))
708 return 0;
709
710 if (link->flags & IFF_UP)
711 link_is_up = true;
712
713 HASHMAP_FOREACH(l, link->bound_to_links)
714 if (link_has_carrier(l)) {
715 required_up = true;
716 break;
717 }
718
719 if (!required_up && link_is_up)
720 return link_request_to_bring_up_or_down(link, /* up = */ false);
721 if (required_up && !link_is_up)
722 return link_request_to_bring_up_or_down(link, /* up = */ true);
723
724 return 0;
725 }
726
727 static int link_handle_bound_by_list(Link *link) {
728 Link *l;
729 int r;
730
731 assert(link);
732
733 /* Update up or down state of interfaces which depend on this interface's carrier state. */
734
735 if (hashmap_isempty(link->bound_by_links))
736 return 0;
737
738 HASHMAP_FOREACH(l, link->bound_by_links) {
739 r = link_handle_bound_to_list(l);
740 if (r < 0)
741 return r;
742 }
743
744 return 0;
745 }
746
747 static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) {
748 int r;
749
750 assert(link);
751 assert(carrier);
752
753 if (link == carrier)
754 return 0;
755
756 if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex)))
757 return 0;
758
759 r = hashmap_ensure_put(h, NULL, INT_TO_PTR(carrier->ifindex), carrier);
760 if (r < 0)
761 return r;
762
763 link_dirty(link);
764
765 return 0;
766 }
767
768 static int link_new_bound_by_list(Link *link) {
769 Manager *m;
770 Link *carrier;
771 int r;
772
773 assert(link);
774 assert(link->manager);
775
776 m = link->manager;
777
778 HASHMAP_FOREACH(carrier, m->links_by_index) {
779 if (!carrier->network)
780 continue;
781
782 if (strv_isempty(carrier->network->bind_carrier))
783 continue;
784
785 if (strv_fnmatch(carrier->network->bind_carrier, link->ifname)) {
786 r = link_put_carrier(link, carrier, &link->bound_by_links);
787 if (r < 0)
788 return r;
789 }
790 }
791
792 HASHMAP_FOREACH(carrier, link->bound_by_links) {
793 r = link_put_carrier(carrier, link, &carrier->bound_to_links);
794 if (r < 0)
795 return r;
796 }
797
798 return 0;
799 }
800
801 static int link_new_bound_to_list(Link *link) {
802 Manager *m;
803 Link *carrier;
804 int r;
805
806 assert(link);
807 assert(link->manager);
808
809 if (!link->network)
810 return 0;
811
812 if (strv_isempty(link->network->bind_carrier))
813 return 0;
814
815 m = link->manager;
816
817 HASHMAP_FOREACH(carrier, m->links_by_index) {
818 if (strv_fnmatch(link->network->bind_carrier, carrier->ifname)) {
819 r = link_put_carrier(link, carrier, &link->bound_to_links);
820 if (r < 0)
821 return r;
822 }
823 }
824
825 HASHMAP_FOREACH(carrier, link->bound_to_links) {
826 r = link_put_carrier(carrier, link, &carrier->bound_by_links);
827 if (r < 0)
828 return r;
829 }
830
831 return 0;
832 }
833
834 static void link_free_bound_to_list(Link *link) {
835 bool updated = false;
836 Link *bound_to;
837
838 assert(link);
839
840 while ((bound_to = hashmap_steal_first(link->bound_to_links))) {
841 updated = true;
842
843 if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex)))
844 link_dirty(bound_to);
845 }
846
847 if (updated)
848 link_dirty(link);
849 }
850
851 static void link_free_bound_by_list(Link *link) {
852 bool updated = false;
853 Link *bound_by;
854
855 assert(link);
856
857 while ((bound_by = hashmap_steal_first(link->bound_by_links))) {
858 updated = true;
859
860 if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) {
861 link_dirty(bound_by);
862 link_handle_bound_to_list(bound_by);
863 }
864 }
865
866 if (updated)
867 link_dirty(link);
868 }
869
870 static int link_append_to_master(Link *link) {
871 Link *master;
872 int r;
873
874 assert(link);
875
876 /* - The link may have no master.
877 * - RTM_NEWLINK message about master interface may not be received yet. */
878 if (link_get_master(link, &master) < 0)
879 return 0;
880
881 r = set_ensure_put(&master->slaves, NULL, link);
882 if (r <= 0)
883 return r;
884
885 link_ref(link);
886 return 0;
887 }
888
889 static void link_drop_from_master(Link *link) {
890 Link *master;
891
892 assert(link);
893
894 if (!link->manager)
895 return;
896
897 if (link_get_master(link, &master) < 0)
898 return;
899
900 link_unref(set_remove(master->slaves, link));
901 }
902
903 static void link_drop_requests(Link *link) {
904 Request *req;
905
906 assert(link);
907 assert(link->manager);
908
909 ORDERED_SET_FOREACH(req, link->manager->request_queue)
910 if (req->link == link)
911 request_drop(req);
912 }
913
914 static Link *link_drop(Link *link) {
915 char **n;
916
917 if (!link)
918 return NULL;
919
920 assert(link->manager);
921
922 link_set_state(link, LINK_STATE_LINGER);
923
924 /* Drop all references from other links and manager. Note that async netlink calls may have
925 * references to the link, and they will be dropped when we receive replies. */
926
927 link_drop_requests(link);
928
929 link_free_bound_to_list(link);
930 link_free_bound_by_list(link);
931
932 link_drop_from_master(link);
933
934 if (link->state_file)
935 (void) unlink(link->state_file);
936
937 link_clean(link);
938
939 STRV_FOREACH(n, link->alternative_names)
940 hashmap_remove(link->manager->links_by_name, *n);
941 hashmap_remove(link->manager->links_by_name, link->ifname);
942
943 /* bonding master and its slaves have the same hardware address. */
944 hashmap_remove_value(link->manager->links_by_hw_addr, &link->hw_addr, link);
945
946 /* The following must be called at last. */
947 assert_se(hashmap_remove(link->manager->links_by_index, INT_TO_PTR(link->ifindex)) == link);
948 return link_unref(link);
949 }
950
951 static int link_drop_foreign_config(Link *link) {
952 int k, r;
953
954 assert(link);
955 assert(link->manager);
956
957 /* Drop foreign config, but ignore unmanaged, loopback, or critical interfaces. We do not want
958 * to remove loopback address or addresses used for root NFS. */
959
960 if (IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING, LINK_STATE_INITIALIZED))
961 return 0;
962 if (FLAGS_SET(link->flags, IFF_LOOPBACK))
963 return 0;
964 if (link->network->keep_configuration == KEEP_CONFIGURATION_YES)
965 return 0;
966
967 r = link_drop_foreign_routes(link);
968
969 k = link_drop_foreign_nexthops(link);
970 if (k < 0 && r >= 0)
971 r = k;
972
973 k = link_drop_foreign_addresses(link);
974 if (k < 0 && r >= 0)
975 r = k;
976
977 k = link_drop_foreign_neighbors(link);
978 if (k < 0 && r >= 0)
979 r = k;
980
981 k = manager_drop_foreign_routing_policy_rules(link->manager);
982 if (k < 0 && r >= 0)
983 r = k;
984
985 return r;
986 }
987
988 static int link_drop_managed_config(Link *link) {
989 int k, r;
990
991 assert(link);
992 assert(link->manager);
993
994 r = link_drop_managed_routes(link);
995
996 k = link_drop_managed_nexthops(link);
997 if (k < 0 && r >= 0)
998 r = k;
999
1000 k = link_drop_managed_addresses(link);
1001 if (k < 0 && r >= 0)
1002 r = k;
1003
1004 k = link_drop_managed_neighbors(link);
1005 if (k < 0 && r >= 0)
1006 r = k;
1007
1008 k = link_drop_managed_routing_policy_rules(link);
1009 if (k < 0 && r >= 0)
1010 r = k;
1011
1012 return r;
1013 }
1014
1015 static void link_foreignize_config(Link *link) {
1016 assert(link);
1017 assert(link->manager);
1018
1019 link_foreignize_routes(link);
1020 link_foreignize_nexthops(link);
1021 link_foreignize_addresses(link);
1022 link_foreignize_neighbors(link);
1023 link_foreignize_routing_policy_rules(link);
1024 }
1025
1026 static int link_configure(Link *link) {
1027 int r;
1028
1029 assert(link);
1030 assert(link->network);
1031 assert(link->state == LINK_STATE_INITIALIZED);
1032
1033 link_set_state(link, LINK_STATE_CONFIGURING);
1034
1035 r = link_new_bound_to_list(link);
1036 if (r < 0)
1037 return r;
1038
1039 r = link_request_traffic_control(link);
1040 if (r < 0)
1041 return r;
1042
1043 if (link->iftype == ARPHRD_CAN) {
1044 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1045 r = link_request_to_set_can(link);
1046 if (r < 0)
1047 return r;
1048
1049 return link_request_to_activate(link);
1050 }
1051
1052 r = link_configure_sr_iov(link);
1053 if (r < 0)
1054 return r;
1055
1056 r = link_set_sysctl(link);
1057 if (r < 0)
1058 return r;
1059
1060 r = link_request_to_set_mac(link, /* allow_retry = */ true);
1061 if (r < 0)
1062 return r;
1063
1064 r = link_request_to_set_ipoib(link);
1065 if (r < 0)
1066 return r;
1067
1068 r = link_request_to_set_flags(link);
1069 if (r < 0)
1070 return r;
1071
1072 r = link_request_to_set_group(link);
1073 if (r < 0)
1074 return r;
1075
1076 r = link_configure_mtu(link);
1077 if (r < 0)
1078 return r;
1079
1080 r = link_request_to_set_addrgen_mode(link);
1081 if (r < 0)
1082 return r;
1083
1084 r = link_request_to_set_master(link);
1085 if (r < 0)
1086 return r;
1087
1088 r = link_request_stacked_netdevs(link);
1089 if (r < 0)
1090 return r;
1091
1092 r = link_request_to_set_bond(link);
1093 if (r < 0)
1094 return r;
1095
1096 r = link_request_to_set_bridge(link);
1097 if (r < 0)
1098 return r;
1099
1100 r = link_request_to_set_bridge_vlan(link);
1101 if (r < 0)
1102 return r;
1103
1104 r = link_request_to_activate(link);
1105 if (r < 0)
1106 return r;
1107
1108 r = ipv4ll_configure(link);
1109 if (r < 0)
1110 return r;
1111
1112 r = link_request_dhcp4_client(link);
1113 if (r < 0)
1114 return r;
1115
1116 r = link_request_dhcp6_client(link);
1117 if (r < 0)
1118 return r;
1119
1120 r = link_request_ndisc(link);
1121 if (r < 0)
1122 return r;
1123
1124 r = link_request_dhcp_server(link);
1125 if (r < 0)
1126 return r;
1127
1128 r = link_request_radv(link);
1129 if (r < 0)
1130 return r;
1131
1132 r = link_lldp_rx_configure(link);
1133 if (r < 0)
1134 return r;
1135
1136 r = link_lldp_tx_configure(link);
1137 if (r < 0)
1138 return r;
1139
1140 r = link_drop_foreign_config(link);
1141 if (r < 0)
1142 return r;
1143
1144 r = link_request_static_configs(link);
1145 if (r < 0)
1146 return r;
1147
1148 if (!link_has_carrier(link))
1149 return 0;
1150
1151 return link_acquire_dynamic_conf(link);
1152 }
1153
1154 static int link_get_network(Link *link, Network **ret) {
1155 Network *network;
1156 int r;
1157
1158 assert(link);
1159 assert(link->manager);
1160 assert(ret);
1161
1162 ORDERED_HASHMAP_FOREACH(network, link->manager->networks) {
1163 bool warn = false;
1164
1165 r = net_match_config(
1166 &network->match,
1167 link->sd_device,
1168 &link->hw_addr,
1169 &link->permanent_hw_addr,
1170 link->driver,
1171 link->iftype,
1172 link->kind,
1173 link->ifname,
1174 link->alternative_names,
1175 link->wlan_iftype,
1176 link->ssid,
1177 &link->bssid);
1178 if (r < 0)
1179 return r;
1180 if (r == 0)
1181 continue;
1182
1183 if (network->match.ifname && link->sd_device) {
1184 uint8_t name_assign_type = NET_NAME_UNKNOWN;
1185 const char *attr;
1186
1187 if (sd_device_get_sysattr_value(link->sd_device, "name_assign_type", &attr) >= 0)
1188 (void) safe_atou8(attr, &name_assign_type);
1189
1190 warn = name_assign_type == NET_NAME_ENUM;
1191 }
1192
1193 log_link_full(link, warn ? LOG_WARNING : LOG_DEBUG,
1194 "found matching network '%s'%s.",
1195 network->filename,
1196 warn ? ", based on potentially unpredictable interface name" : "");
1197
1198 if (network->unmanaged)
1199 return -ENOENT;
1200
1201 *ret = network;
1202 return 0;
1203 }
1204
1205 return -ENOENT;
1206 }
1207
1208 static int link_reconfigure_impl(Link *link, bool force) {
1209 Network *network = NULL;
1210 int r;
1211
1212 assert(link);
1213
1214 if (!IN_SET(link->state, LINK_STATE_INITIALIZED, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED, LINK_STATE_UNMANAGED))
1215 return 0;
1216
1217 r = link_get_network(link, &network);
1218 if (r < 0 && r != -ENOENT)
1219 return r;
1220
1221 if (link->state != LINK_STATE_UNMANAGED && !network)
1222 /* If link is in initialized state, then link->network is also NULL. */
1223 force = true;
1224
1225 if (link->network == network && !force)
1226 return 0;
1227
1228 if (network) {
1229 if (link->state == LINK_STATE_INITIALIZED)
1230 log_link_info(link, "Configuring with %s.", network->filename);
1231 else
1232 log_link_info(link, "Reconfiguring with %s.", network->filename);
1233 } else
1234 log_link_full(link, link->state == LINK_STATE_INITIALIZED ? LOG_DEBUG : LOG_INFO,
1235 "Unmanaging interface.");
1236
1237 /* Dropping old .network file */
1238 r = link_stop_engines(link, false);
1239 if (r < 0)
1240 return r;
1241
1242 link_drop_requests(link);
1243
1244 if (network && !force && network->keep_configuration != KEEP_CONFIGURATION_YES)
1245 /* When a new/updated .network file is assigned, first make all configs (addresses,
1246 * routes, and so on) foreign, and then drop unnecessary configs later by
1247 * link_drop_foreign_config() in link_configure().
1248 * Note, when KeepConfiguration=yes, link_drop_foreign_config() does nothing. Hence,
1249 * here we need to drop the configs such as addresses, routes, and so on configured by
1250 * the previously assigned .network file. */
1251 link_foreignize_config(link);
1252 else {
1253 /* Remove all managed configs. Note, foreign configs are removed in later by
1254 * link_configure() -> link_drop_foreign_config() if the link is managed by us. */
1255 r = link_drop_managed_config(link);
1256 if (r < 0)
1257 return r;
1258 }
1259
1260 /* The bound_to map depends on .network file, hence it needs to be freed. But, do not free the
1261 * bound_by map. Otherwise, if a link enters unmanaged state below, then its carrier state will
1262 * not propagated to other interfaces anymore. Moreover, it is not necessary to recreate the
1263 * map here, as it depends on .network files assigned to other links. */
1264 link_free_bound_to_list(link);
1265
1266 link_free_engines(link);
1267 link->network = network_unref(link->network);
1268
1269 if (!network) {
1270 link_set_state(link, LINK_STATE_UNMANAGED);
1271 return 0;
1272 }
1273
1274 /* Then, apply new .network file */
1275 link->network = network_ref(network);
1276 link_update_operstate(link, true);
1277 link_dirty(link);
1278
1279 link_set_state(link, LINK_STATE_INITIALIZED);
1280 link->activated = false;
1281
1282 r = link_configure(link);
1283 if (r < 0)
1284 return r;
1285
1286 return 1;
1287 }
1288
1289 static int link_reconfigure_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, bool force) {
1290 int r;
1291
1292 assert(link);
1293
1294 r = link_getlink_handler_internal(rtnl, m, link, "Failed to update link state");
1295 if (r <= 0)
1296 return r;
1297
1298 r = link_reconfigure_impl(link, force);
1299 if (r < 0) {
1300 link_enter_failed(link);
1301 return 0;
1302 }
1303
1304 return r;
1305 }
1306
1307 static int link_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1308 return link_reconfigure_handler_internal(rtnl, m, link, /* force = */ false);
1309 }
1310
1311 static int link_force_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1312 return link_reconfigure_handler_internal(rtnl, m, link, /* force = */ true);
1313 }
1314
1315 static int link_reconfigure_after_sleep_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1316 int r;
1317
1318 assert(link);
1319
1320 r = link_reconfigure_handler_internal(rtnl, m, link, /* force = */ false);
1321 if (r != 0)
1322 return r;
1323
1324 /* r == 0 means an error occurs, the link is unmanaged, or the matching network file is unchanged. */
1325 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
1326 return 0;
1327
1328 /* re-request static configs, and restart engines. */
1329 r = link_stop_engines(link, false);
1330 if (r < 0) {
1331 link_enter_failed(link);
1332 return 0;
1333 }
1334
1335 r = link_acquire_dynamic_conf(link);
1336 if (r < 0) {
1337 link_enter_failed(link);
1338 return 0;
1339 }
1340
1341 r = link_request_static_configs(link);
1342 if (r < 0) {
1343 link_enter_failed(link);
1344 return 0;
1345 }
1346
1347 return 0;
1348 }
1349
1350 static int link_reconfigure_internal(Link *link, link_netlink_message_handler_t callback) {
1351 int r;
1352
1353 assert(link);
1354 assert(callback);
1355
1356 /* When link in pending or initialized state, then link_configure() will be called. To prevent
1357 * the function from being called multiple times simultaneously, refuse to reconfigure the
1358 * interface in these cases. */
1359 if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED, LINK_STATE_LINGER))
1360 return 0; /* 0 means no-op. */
1361
1362 r = link_call_getlink(link, callback);
1363 if (r < 0)
1364 return r;
1365
1366 return 1; /* 1 means the interface will be reconfigured. */
1367 }
1368
1369 int link_reconfigure(Link *link, bool force) {
1370 return link_reconfigure_internal(link, force ? link_force_reconfigure_handler : link_reconfigure_handler);
1371 }
1372
1373 int link_reconfigure_after_sleep(Link *link) {
1374 return link_reconfigure_internal(link, link_reconfigure_after_sleep_handler);
1375 }
1376
1377 static int link_initialized_and_synced(Link *link) {
1378 int r;
1379
1380 assert(link);
1381 assert(link->manager);
1382
1383 if (link->manager->test_mode) {
1384 log_link_debug(link, "Running in test mode, refusing to enter initialized state.");
1385 link_set_state(link, LINK_STATE_UNMANAGED);
1386 return 0;
1387 }
1388
1389 /* This may get called either from the asynchronous netlink callback,
1390 * or directly from link_check_initialized() if running in a container. */
1391 if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED))
1392 return 0;
1393
1394 log_link_debug(link, "Link state is up-to-date");
1395 link_set_state(link, LINK_STATE_INITIALIZED);
1396
1397 r = link_new_bound_by_list(link);
1398 if (r < 0)
1399 return r;
1400
1401 r = link_handle_bound_by_list(link);
1402 if (r < 0)
1403 return r;
1404
1405 return link_reconfigure_impl(link, /* force = */ false);
1406 }
1407
1408 static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1409 int r;
1410
1411 r = link_getlink_handler_internal(rtnl, m, link, "Failed to wait for the interface to be initialized");
1412 if (r <= 0)
1413 return r;
1414
1415 r = link_initialized_and_synced(link);
1416 if (r < 0)
1417 link_enter_failed(link);
1418
1419 return 0;
1420 }
1421
1422 static int link_initialized(Link *link, sd_device *device) {
1423 assert(link);
1424 assert(device);
1425
1426 /* Always replace with the new sd_device object. As the sysname (and possibly other properties
1427 * or sysattrs) may be outdated. */
1428 sd_device_ref(device);
1429 sd_device_unref(link->sd_device);
1430 link->sd_device = device;
1431
1432 /* Do not ignore unamanaged state case here. If an interface is renamed after being once
1433 * configured, and the corresponding .network file has Name= in [Match] section, then the
1434 * interface may be already in unmanaged state. See #20657. */
1435 if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_UNMANAGED))
1436 return 0;
1437
1438 log_link_debug(link, "udev initialized link");
1439 link_set_state(link, LINK_STATE_INITIALIZED);
1440
1441 /* udev has initialized the link, but we don't know if we have yet
1442 * processed the NEWLINK messages with the latest state. Do a GETLINK,
1443 * when it returns we know that the pending NEWLINKs have already been
1444 * processed and that we are up-to-date */
1445
1446 return link_call_getlink(link, link_initialized_handler);
1447 }
1448
1449 static int link_check_initialized(Link *link) {
1450 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
1451 int r;
1452
1453 assert(link);
1454
1455 if (path_is_read_only_fs("/sys") > 0)
1456 /* no udev */
1457 return link_initialized_and_synced(link);
1458
1459 /* udev should be around */
1460 r = sd_device_new_from_ifindex(&device, link->ifindex);
1461 if (r < 0) {
1462 log_link_debug_errno(link, r, "Could not find device, waiting for device initialization: %m");
1463 return 0;
1464 }
1465
1466 r = sd_device_get_is_initialized(device);
1467 if (r < 0)
1468 return log_link_warning_errno(link, r, "Could not determine whether the device is initialized: %m");
1469 if (r == 0) {
1470 /* not yet ready */
1471 log_link_debug(link, "link pending udev initialization...");
1472 return 0;
1473 }
1474
1475 r = device_is_renaming(device);
1476 if (r < 0)
1477 return log_link_warning_errno(link, r, "Failed to determine the device is being renamed: %m");
1478 if (r > 0) {
1479 log_link_debug(link, "Interface is being renamed, pending initialization.");
1480 return 0;
1481 }
1482
1483 return link_initialized(link, device);
1484 }
1485
1486 int manager_udev_process_link(sd_device_monitor *monitor, sd_device *device, void *userdata) {
1487 sd_device_action_t action;
1488 Manager *m = userdata;
1489 Link *link = NULL;
1490 int r, ifindex;
1491
1492 assert(m);
1493 assert(device);
1494
1495 r = sd_device_get_action(device, &action);
1496 if (r < 0) {
1497 log_device_debug_errno(device, r, "Failed to get udev action, ignoring device: %m");
1498 return 0;
1499 }
1500
1501 /* Ignore the "remove" uevent — let's remove a device only if rtnetlink says so. All other uevents
1502 * are "positive" events in some form, i.e. inform us about a changed or new network interface, that
1503 * still exists — and we are interested in that. */
1504 if (action == SD_DEVICE_REMOVE)
1505 return 0;
1506
1507 r = sd_device_get_ifindex(device, &ifindex);
1508 if (r < 0) {
1509 log_device_debug_errno(device, r, "Ignoring udev %s event for device without ifindex or with invalid ifindex: %m",
1510 device_action_to_string(action));
1511 return 0;
1512 }
1513
1514 r = device_is_renaming(device);
1515 if (r < 0) {
1516 log_device_debug_errno(device, r, "Failed to determine the device is renamed or not, ignoring '%s' uevent: %m",
1517 device_action_to_string(action));
1518 return 0;
1519 }
1520 if (r > 0) {
1521 log_device_debug(device, "Interface is under renaming, wait for the interface to be renamed.");
1522 return 0;
1523 }
1524
1525 r = link_get_by_index(m, ifindex, &link);
1526 if (r < 0) {
1527 log_device_debug_errno(device, r, "Failed to get link from ifindex %i, ignoring: %m", ifindex);
1528 return 0;
1529 }
1530
1531 r = link_initialized(link, device);
1532 if (r < 0)
1533 link_enter_failed(link);
1534
1535 return 0;
1536 }
1537
1538 static int link_carrier_gained(Link *link) {
1539 bool force_reconfigure;
1540 int r;
1541
1542 assert(link);
1543
1544 r = event_source_disable(link->carrier_lost_timer);
1545 if (r < 0)
1546 log_link_warning_errno(link, r, "Failed to disable carrier lost timer, ignoring: %m");
1547
1548 /* If a wireless interface was connected to an access point, and the SSID is changed (that is,
1549 * both previous_ssid and ssid are non-NULL), then the connected wireless network could be
1550 * changed. So, always reconfigure the link. Which means e.g. the DHCP client will be
1551 * restarted, and the correct network information will be gained.
1552 *
1553 * However, do not reconfigure the wireless interface forcibly if it was not connected to any
1554 * access points previously (previous_ssid is NULL in this case). As, a .network file may be
1555 * already assigned to the interface (in that case, the .network file does not have the SSID=
1556 * setting in the [Match] section), and the interface is already being configured. Of course,
1557 * there may exist another .network file with higher priority and a matching SSID= setting. But
1558 * in that case, link_reconfigure_impl() can handle that without the force_reconfigure flag.
1559 *
1560 * For non-wireless interfaces, we have no way to detect the connected network change. So,
1561 * setting force_reconfigure = false. Note, both ssid and previous_ssid are NULL in that case. */
1562 force_reconfigure = link->previous_ssid && !streq_ptr(link->previous_ssid, link->ssid);
1563 link->previous_ssid = mfree(link->previous_ssid);
1564
1565 if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_FAILED, LINK_STATE_LINGER)) {
1566 /* At this stage, both wlan and link information should be up-to-date. Hence,
1567 * it is not necessary to call RTM_GETLINK, NL80211_CMD_GET_INTERFACE, or
1568 * NL80211_CMD_GET_STATION commands, and simply call link_reconfigure_impl().
1569 * Note, link_reconfigure_impl() returns 1 when the link is reconfigured. */
1570 r = link_reconfigure_impl(link, force_reconfigure);
1571 if (r != 0)
1572 return r;
1573 }
1574
1575 r = link_handle_bound_by_list(link);
1576 if (r < 0)
1577 return r;
1578
1579 if (link->iftype == ARPHRD_CAN)
1580 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1581 return 0;
1582
1583 if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) {
1584 r = link_acquire_dynamic_conf(link);
1585 if (r < 0)
1586 return r;
1587
1588 r = link_request_static_configs(link);
1589 if (r < 0)
1590 return r;
1591 }
1592
1593 return 0;
1594 }
1595
1596 static int link_carrier_lost_impl(Link *link) {
1597 int r, ret = 0;
1598
1599 assert(link);
1600
1601 link->previous_ssid = mfree(link->previous_ssid);
1602
1603 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1604 return 0;
1605
1606 if (!link->network)
1607 return 0;
1608
1609 r = link_stop_engines(link, false);
1610 if (r < 0)
1611 ret = r;
1612
1613 r = link_drop_managed_config(link);
1614 if (r < 0 && ret >= 0)
1615 ret = r;
1616
1617 return ret;
1618 }
1619
1620 static int link_carrier_lost_handler(sd_event_source *s, uint64_t usec, void *userdata) {
1621 Link *link = userdata;
1622 int r;
1623
1624 assert(link);
1625
1626 r = link_carrier_lost_impl(link);
1627 if (r < 0) {
1628 log_link_warning_errno(link, r, "Failed to process carrier lost event: %m");
1629 link_enter_failed(link);
1630 }
1631
1632 return 0;
1633 }
1634
1635 static int link_carrier_lost(Link *link) {
1636 int r;
1637
1638 assert(link);
1639
1640 r = link_handle_bound_by_list(link);
1641 if (r < 0)
1642 return r;
1643
1644 if (link->iftype == ARPHRD_CAN)
1645 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1646 return 0;
1647
1648 if (!link->network)
1649 return 0;
1650
1651 if (link->network->ignore_carrier_loss_usec == USEC_INFINITY)
1652 return 0;
1653
1654 if (link->network->ignore_carrier_loss_usec == 0)
1655 return link_carrier_lost_impl(link);
1656
1657 return event_reset_time_relative(link->manager->event,
1658 &link->carrier_lost_timer,
1659 clock_boottime_or_monotonic(),
1660 link->network->ignore_carrier_loss_usec,
1661 0,
1662 link_carrier_lost_handler,
1663 link,
1664 0,
1665 "link-carrier-loss",
1666 true);
1667 }
1668
1669 static int link_admin_state_up(Link *link) {
1670 int r;
1671
1672 assert(link);
1673
1674 /* This is called every time an interface admin state changes to up;
1675 * specifically, when IFF_UP flag changes from unset to set. */
1676
1677 if (!link->network)
1678 return 0;
1679
1680 if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_DOWN) {
1681 log_link_info(link, "Activation policy is \"always-down\", forcing link down.");
1682 return link_request_to_bring_up_or_down(link, /* up = */ false);
1683 }
1684
1685 /* We set the ipv6 mtu after the device mtu, but the kernel resets
1686 * ipv6 mtu on NETDEV_UP, so we need to reset it. */
1687 r = link_set_ipv6_mtu(link);
1688 if (r < 0)
1689 log_link_warning_errno(link, r, "Cannot set IPv6 MTU, ignoring: %m");
1690
1691 return 0;
1692 }
1693
1694 static int link_admin_state_down(Link *link) {
1695 assert(link);
1696
1697 if (!link->network)
1698 return 0;
1699
1700 if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_UP) {
1701 log_link_info(link, "Activation policy is \"always-up\", forcing link up.");
1702 return link_request_to_bring_up_or_down(link, /* up = */ true);
1703 }
1704
1705 return 0;
1706 }
1707
1708 static bool link_is_enslaved(Link *link) {
1709 if (link->flags & IFF_SLAVE)
1710 return true;
1711
1712 if (link->master_ifindex > 0)
1713 return true;
1714
1715 return false;
1716 }
1717
1718 static LinkAddressState address_state_from_scope(uint8_t scope) {
1719 if (scope < RT_SCOPE_SITE)
1720 /* universally accessible addresses found */
1721 return LINK_ADDRESS_STATE_ROUTABLE;
1722
1723 if (scope < RT_SCOPE_HOST)
1724 /* only link or site local addresses found */
1725 return LINK_ADDRESS_STATE_DEGRADED;
1726
1727 /* no useful addresses found */
1728 return LINK_ADDRESS_STATE_OFF;
1729 }
1730
1731 void link_update_operstate(Link *link, bool also_update_master) {
1732 LinkOperationalState operstate;
1733 LinkCarrierState carrier_state;
1734 LinkAddressState ipv4_address_state, ipv6_address_state, address_state;
1735 LinkOnlineState online_state;
1736 _cleanup_strv_free_ char **p = NULL;
1737 uint8_t ipv4_scope = RT_SCOPE_NOWHERE, ipv6_scope = RT_SCOPE_NOWHERE;
1738 bool changed = false;
1739 Address *address;
1740
1741 assert(link);
1742
1743 if (link->kernel_operstate == IF_OPER_DORMANT)
1744 carrier_state = LINK_CARRIER_STATE_DORMANT;
1745 else if (link_has_carrier(link)) {
1746 if (link_is_enslaved(link))
1747 carrier_state = LINK_CARRIER_STATE_ENSLAVED;
1748 else
1749 carrier_state = LINK_CARRIER_STATE_CARRIER;
1750 } else if (link->flags & IFF_UP)
1751 carrier_state = LINK_CARRIER_STATE_NO_CARRIER;
1752 else
1753 carrier_state = LINK_CARRIER_STATE_OFF;
1754
1755 if (carrier_state >= LINK_CARRIER_STATE_CARRIER) {
1756 Link *slave;
1757
1758 SET_FOREACH(slave, link->slaves) {
1759 link_update_operstate(slave, false);
1760
1761 if (slave->carrier_state < LINK_CARRIER_STATE_CARRIER)
1762 carrier_state = LINK_CARRIER_STATE_DEGRADED_CARRIER;
1763 }
1764 }
1765
1766 SET_FOREACH(address, link->addresses) {
1767 if (!address_is_ready(address))
1768 continue;
1769
1770 if (address->family == AF_INET)
1771 ipv4_scope = MIN(ipv4_scope, address->scope);
1772
1773 if (address->family == AF_INET6)
1774 ipv6_scope = MIN(ipv6_scope, address->scope);
1775 }
1776
1777 ipv4_address_state = address_state_from_scope(ipv4_scope);
1778 ipv6_address_state = address_state_from_scope(ipv6_scope);
1779 address_state = address_state_from_scope(MIN(ipv4_scope, ipv6_scope));
1780
1781 /* Mapping of address and carrier state vs operational state
1782 * carrier state
1783 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
1784 * ------------------------------------------------------------------------------
1785 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
1786 * address_state degraded | off | no-carrier | dormant | degraded-carrier | degraded | enslaved
1787 * routable | off | no-carrier | dormant | degraded-carrier | routable | routable
1788 */
1789
1790 if (carrier_state < LINK_CARRIER_STATE_CARRIER || address_state == LINK_ADDRESS_STATE_OFF)
1791 operstate = (LinkOperationalState) carrier_state;
1792 else if (address_state == LINK_ADDRESS_STATE_ROUTABLE)
1793 operstate = LINK_OPERSTATE_ROUTABLE;
1794 else if (carrier_state == LINK_CARRIER_STATE_CARRIER)
1795 operstate = LINK_OPERSTATE_DEGRADED;
1796 else
1797 operstate = LINK_OPERSTATE_ENSLAVED;
1798
1799 /* Only determine online state for managed links with RequiredForOnline=yes */
1800 if (!link->network || !link->network->required_for_online)
1801 online_state = _LINK_ONLINE_STATE_INVALID;
1802 else if (operstate < link->network->required_operstate_for_online.min ||
1803 operstate > link->network->required_operstate_for_online.max)
1804 online_state = LINK_ONLINE_STATE_OFFLINE;
1805 else {
1806 AddressFamily required_family = link->network->required_family_for_online;
1807 bool needs_ipv4 = required_family & ADDRESS_FAMILY_IPV4;
1808 bool needs_ipv6 = required_family & ADDRESS_FAMILY_IPV6;
1809
1810 /* The operational state is within the range required for online.
1811 * If a particular address family is also required, we might revert
1812 * to offline in the blocks below. */
1813 online_state = LINK_ONLINE_STATE_ONLINE;
1814
1815 if (link->network->required_operstate_for_online.min >= LINK_OPERSTATE_DEGRADED) {
1816 if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_DEGRADED)
1817 online_state = LINK_ONLINE_STATE_OFFLINE;
1818 if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_DEGRADED)
1819 online_state = LINK_ONLINE_STATE_OFFLINE;
1820 }
1821
1822 if (link->network->required_operstate_for_online.min >= LINK_OPERSTATE_ROUTABLE) {
1823 if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_ROUTABLE)
1824 online_state = LINK_ONLINE_STATE_OFFLINE;
1825 if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_ROUTABLE)
1826 online_state = LINK_ONLINE_STATE_OFFLINE;
1827 }
1828 }
1829
1830 if (link->carrier_state != carrier_state) {
1831 link->carrier_state = carrier_state;
1832 changed = true;
1833 if (strv_extend(&p, "CarrierState") < 0)
1834 log_oom();
1835 }
1836
1837 if (link->address_state != address_state) {
1838 link->address_state = address_state;
1839 changed = true;
1840 if (strv_extend(&p, "AddressState") < 0)
1841 log_oom();
1842 }
1843
1844 if (link->ipv4_address_state != ipv4_address_state) {
1845 link->ipv4_address_state = ipv4_address_state;
1846 changed = true;
1847 if (strv_extend(&p, "IPv4AddressState") < 0)
1848 log_oom();
1849 }
1850
1851 if (link->ipv6_address_state != ipv6_address_state) {
1852 link->ipv6_address_state = ipv6_address_state;
1853 changed = true;
1854 if (strv_extend(&p, "IPv6AddressState") < 0)
1855 log_oom();
1856 }
1857
1858 if (link->operstate != operstate) {
1859 link->operstate = operstate;
1860 changed = true;
1861 if (strv_extend(&p, "OperationalState") < 0)
1862 log_oom();
1863 }
1864
1865 if (link->online_state != online_state) {
1866 link->online_state = online_state;
1867 changed = true;
1868 if (strv_extend(&p, "OnlineState") < 0)
1869 log_oom();
1870 }
1871
1872 if (p)
1873 link_send_changed_strv(link, p);
1874 if (changed)
1875 link_dirty(link);
1876
1877 if (also_update_master) {
1878 Link *master;
1879
1880 if (link_get_master(link, &master) >= 0)
1881 link_update_operstate(master, true);
1882 }
1883 }
1884
1885 #define FLAG_STRING(string, flag, old, new) \
1886 (((old ^ new) & flag) \
1887 ? ((old & flag) ? (" -" string) : (" +" string)) \
1888 : "")
1889
1890 static int link_update_flags(Link *link, sd_netlink_message *message) {
1891 bool link_was_admin_up, had_carrier;
1892 uint8_t operstate;
1893 unsigned flags;
1894 int r;
1895
1896 assert(link);
1897 assert(message);
1898
1899 r = sd_rtnl_message_link_get_flags(message, &flags);
1900 if (r < 0)
1901 return log_link_debug_errno(link, r, "rtnl: failed to read link flags: %m");
1902
1903 r = sd_netlink_message_read_u8(message, IFLA_OPERSTATE, &operstate);
1904 if (r == -ENODATA)
1905 /* If we got a message without operstate, assume the state was unchanged. */
1906 operstate = link->kernel_operstate;
1907 else if (r < 0)
1908 return log_link_debug_errno(link, r, "rtnl: failed to read operational state: %m");
1909
1910 if (link->flags == flags && link->kernel_operstate == operstate)
1911 return 0;
1912
1913 if (link->flags != flags) {
1914 unsigned unknown_flags, unknown_flags_added, unknown_flags_removed;
1915
1916 log_link_debug(link, "Flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1917 FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
1918 FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
1919 FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
1920 FLAG_STRING("UP", IFF_UP, link->flags, flags),
1921 FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
1922 FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
1923 FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
1924 FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
1925 FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
1926 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
1927 FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
1928 FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
1929 FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
1930 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
1931 FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
1932 FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
1933 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
1934 FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
1935 FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
1936
1937 unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
1938 IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
1939 IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
1940 IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
1941 IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
1942 IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
1943 unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
1944 unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
1945
1946 if (unknown_flags_added)
1947 log_link_debug(link, "Unknown link flags gained, ignoring: %#.5x", unknown_flags_added);
1948
1949 if (unknown_flags_removed)
1950 log_link_debug(link, "Unknown link flags lost, ignoring: %#.5x", unknown_flags_removed);
1951 }
1952
1953 link_was_admin_up = link->flags & IFF_UP;
1954 had_carrier = link_has_carrier(link);
1955
1956 link->flags = flags;
1957 link->kernel_operstate = operstate;
1958
1959 link_update_operstate(link, true);
1960
1961 if (!link_was_admin_up && (link->flags & IFF_UP)) {
1962 log_link_info(link, "Link UP");
1963
1964 r = link_admin_state_up(link);
1965 if (r < 0)
1966 return r;
1967 } else if (link_was_admin_up && !(link->flags & IFF_UP)) {
1968 log_link_info(link, "Link DOWN");
1969
1970 r = link_admin_state_down(link);
1971 if (r < 0)
1972 return r;
1973 }
1974
1975 if (!had_carrier && link_has_carrier(link)) {
1976 log_link_info(link, "Gained carrier");
1977
1978 r = link_carrier_gained(link);
1979 if (r < 0)
1980 return r;
1981 } else if (had_carrier && !link_has_carrier(link)) {
1982 log_link_info(link, "Lost carrier");
1983
1984 r = link_carrier_lost(link);
1985 if (r < 0)
1986 return r;
1987 }
1988
1989 return 0;
1990 }
1991
1992 static int link_update_master(Link *link, sd_netlink_message *message) {
1993 int master_ifindex, r;
1994
1995 assert(link);
1996 assert(message);
1997
1998 r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t*) &master_ifindex);
1999 if (r == -ENODATA)
2000 return 0;
2001 if (r < 0)
2002 return log_link_debug_errno(link, r, "rtnl: failed to read master ifindex: %m");
2003
2004 if (master_ifindex == link->ifindex)
2005 master_ifindex = 0;
2006
2007 if (master_ifindex == link->master_ifindex)
2008 return 0;
2009
2010 if (link->master_ifindex == 0)
2011 log_link_debug(link, "Joined to master interface: %i", master_ifindex);
2012 else if (master_ifindex == 0)
2013 log_link_debug(link, "Leaved from master interface: %i", link->master_ifindex);
2014 else
2015 log_link_debug(link, "Master interface is changed: %i → %i", link->master_ifindex, master_ifindex);
2016
2017 link_drop_from_master(link);
2018
2019 link->master_ifindex = master_ifindex;
2020
2021 r = link_append_to_master(link);
2022 if (r < 0)
2023 return log_link_debug_errno(link, r, "Failed to append link to master: %m");
2024
2025 return 0;
2026 }
2027
2028 static int link_update_hardware_address(Link *link, sd_netlink_message *message) {
2029 struct hw_addr_data addr;
2030 int r;
2031
2032 assert(link);
2033 assert(message);
2034
2035 r = netlink_message_read_hw_addr(message, IFLA_BROADCAST, &link->bcast_addr);
2036 if (r < 0 && r != -ENODATA)
2037 return log_link_debug_errno(link, r, "rtnl: failed to read broadcast address: %m");
2038
2039 r = netlink_message_read_hw_addr(message, IFLA_ADDRESS, &addr);
2040 if (r == -ENODATA)
2041 return 0;
2042 if (r < 0)
2043 return log_link_debug_errno(link, r, "rtnl: failed to read hardware address: %m");
2044
2045 if (hw_addr_equal(&link->hw_addr, &addr))
2046 return 0;
2047
2048 if (link->hw_addr.length == 0)
2049 log_link_debug(link, "Saved hardware address: %s", HW_ADDR_TO_STR(&addr));
2050 else {
2051 log_link_debug(link, "Hardware address is changed: %s → %s",
2052 HW_ADDR_TO_STR(&link->hw_addr), HW_ADDR_TO_STR(&addr));
2053
2054 hashmap_remove_value(link->manager->links_by_hw_addr, &link->hw_addr, link);
2055 }
2056
2057 link->hw_addr = addr;
2058
2059 if (!hw_addr_is_null(&link->hw_addr)) {
2060 r = hashmap_ensure_put(&link->manager->links_by_hw_addr, &hw_addr_hash_ops, &link->hw_addr, link);
2061 if (r == -EEXIST && streq_ptr(link->kind, "bond"))
2062 /* bonding master and its slaves have the same hardware address. */
2063 r = hashmap_replace(link->manager->links_by_hw_addr, &link->hw_addr, link);
2064 if (r < 0)
2065 log_link_debug_errno(link, r, "Failed to manage link by its new hardware address, ignoring: %m");
2066 }
2067
2068 r = ipv4ll_update_mac(link);
2069 if (r < 0)
2070 return log_link_debug_errno(link, r, "Could not update MAC address in IPv4 ACD client: %m");
2071
2072 r = ipv4ll_update_mac(link);
2073 if (r < 0)
2074 return log_link_debug_errno(link, r, "Could not update MAC address in IPv4LL client: %m");
2075
2076 r = dhcp4_update_mac(link);
2077 if (r < 0)
2078 return log_link_debug_errno(link, r, "Could not update MAC address in DHCP client: %m");
2079
2080 r = dhcp6_update_mac(link);
2081 if (r < 0)
2082 return log_link_debug_errno(link, r, "Could not update MAC address in DHCPv6 client: %m");
2083
2084 r = radv_update_mac(link);
2085 if (r < 0)
2086 return log_link_debug_errno(link, r, "Could not update MAC address for Router Advertisement: %m");
2087
2088 if (link->ndisc) {
2089 r = sd_ndisc_set_mac(link->ndisc, &link->hw_addr.ether);
2090 if (r < 0)
2091 return log_link_debug_errno(link, r, "Could not update MAC for NDisc: %m");
2092 }
2093
2094 if (link->lldp_rx) {
2095 r = sd_lldp_rx_set_filter_address(link->lldp_rx, &link->hw_addr.ether);
2096 if (r < 0)
2097 return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Rx: %m");
2098 }
2099
2100 if (link->lldp_tx) {
2101 r = sd_lldp_tx_set_hwaddr(link->lldp_tx, &link->hw_addr.ether);
2102 if (r < 0)
2103 return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Tx: %m");
2104 }
2105
2106 return 0;
2107 }
2108
2109 static int link_update_mtu(Link *link, sd_netlink_message *message) {
2110 uint32_t mtu, min_mtu = 0, max_mtu = UINT32_MAX;
2111 int r;
2112
2113 assert(link);
2114 assert(message);
2115
2116 r = sd_netlink_message_read_u32(message, IFLA_MTU, &mtu);
2117 if (r == -ENODATA)
2118 return 0;
2119 if (r < 0)
2120 return log_link_debug_errno(link, r, "rtnl: failed to read MTU in RTM_NEWLINK message: %m");
2121 if (mtu == 0)
2122 return 0;
2123
2124 r = sd_netlink_message_read_u32(message, IFLA_MIN_MTU, &min_mtu);
2125 if (r < 0 && r != -ENODATA)
2126 return log_link_debug_errno(link, r, "rtnl: failed to read minimum MTU in RTM_NEWLINK message: %m");
2127
2128 r = sd_netlink_message_read_u32(message, IFLA_MAX_MTU, &max_mtu);
2129 if (r < 0 && r != -ENODATA)
2130 return log_link_debug_errno(link, r, "rtnl: failed to read maximum MTU in RTM_NEWLINK message: %m");
2131
2132 if (max_mtu == 0)
2133 max_mtu = UINT32_MAX;
2134
2135 link->min_mtu = min_mtu;
2136 link->max_mtu = max_mtu;
2137
2138 if (link->original_mtu == 0) {
2139 link->original_mtu = mtu;
2140 log_link_debug(link, "Saved original MTU %" PRIu32" (min: %"PRIu32", max: %"PRIu32")",
2141 link->original_mtu, link->min_mtu, link->max_mtu);
2142 }
2143
2144 if (link->mtu == mtu)
2145 return 0;
2146
2147 if (link->mtu != 0)
2148 log_link_debug(link, "MTU is changed: %"PRIu32" → %"PRIu32" (min: %"PRIu32", max: %"PRIu32")",
2149 link->mtu, mtu, link->min_mtu, link->max_mtu);
2150
2151 link->mtu = mtu;
2152
2153 if (link->dhcp_client) {
2154 r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
2155 if (r < 0)
2156 return log_link_debug_errno(link, r, "Could not update MTU in DHCP client: %m");
2157 }
2158
2159 if (link->radv) {
2160 r = sd_radv_set_mtu(link->radv, link->mtu);
2161 if (r < 0)
2162 return log_link_debug_errno(link, r, "Could not set MTU for Router Advertisement: %m");
2163 }
2164
2165 return 0;
2166 }
2167
2168 static int link_update_alternative_names(Link *link, sd_netlink_message *message) {
2169 _cleanup_strv_free_ char **altnames = NULL;
2170 char **n;
2171 int r;
2172
2173 assert(link);
2174 assert(message);
2175
2176 r = sd_netlink_message_read_strv(message, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &altnames);
2177 if (r == -ENODATA)
2178 /* The message does not have IFLA_PROP_LIST container attribute. It does not means the
2179 * interface has no alternative name. */
2180 return 0;
2181 if (r < 0)
2182 return log_link_debug_errno(link, r, "rtnl: failed to read alternative names: %m");
2183
2184 if (strv_equal(altnames, link->alternative_names))
2185 return 0;
2186
2187 STRV_FOREACH(n, link->alternative_names)
2188 hashmap_remove(link->manager->links_by_name, *n);
2189
2190 strv_free_and_replace(link->alternative_names, altnames);
2191
2192 STRV_FOREACH(n, link->alternative_names) {
2193 r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, *n, link);
2194 if (r < 0)
2195 return log_link_debug_errno(link, r, "Failed to manage link by its new alternative names: %m");
2196 }
2197
2198 return 0;
2199 }
2200
2201 static int link_update_name(Link *link, sd_netlink_message *message) {
2202 char ifname_from_index[IF_NAMESIZE];
2203 const char *ifname;
2204 int r;
2205
2206 assert(link);
2207 assert(message);
2208
2209 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname);
2210 if (r == -ENODATA)
2211 /* Hmm?? But ok. */
2212 return 0;
2213 if (r < 0)
2214 return log_link_debug_errno(link, r, "Failed to read interface name in RTM_NEWLINK message: %m");
2215
2216 if (streq(ifname, link->ifname))
2217 return 0;
2218
2219 r = format_ifname(link->ifindex, ifname_from_index);
2220 if (r < 0)
2221 return log_link_debug_errno(link, r, "Could not get interface name for index %i.", link->ifindex);
2222
2223 if (!streq(ifname, ifname_from_index)) {
2224 log_link_debug(link, "New interface name '%s' received from the kernel does not correspond "
2225 "with the name currently configured on the actual interface '%s'. Ignoring.",
2226 ifname, ifname_from_index);
2227 return 0;
2228 }
2229
2230 log_link_info(link, "Interface name change detected, renamed to %s.", ifname);
2231
2232 hashmap_remove(link->manager->links_by_name, link->ifname);
2233
2234 r = free_and_strdup(&link->ifname, ifname);
2235 if (r < 0)
2236 return log_oom_debug();
2237
2238 r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, link->ifname, link);
2239 if (r < 0)
2240 return log_link_debug_errno(link, r, "Failed to manage link by its new name: %m");
2241
2242 if (link->dhcp_client) {
2243 r = sd_dhcp_client_set_ifname(link->dhcp_client, link->ifname);
2244 if (r < 0)
2245 return log_link_debug_errno(link, r, "Failed to update interface name in DHCP client: %m");
2246 }
2247
2248 if (link->dhcp6_client) {
2249 r = sd_dhcp6_client_set_ifname(link->dhcp6_client, link->ifname);
2250 if (r < 0)
2251 return log_link_debug_errno(link, r, "Failed to update interface name in DHCP6 client: %m");
2252 }
2253
2254 if (link->ndisc) {
2255 r = sd_ndisc_set_ifname(link->ndisc, link->ifname);
2256 if (r < 0)
2257 return log_link_debug_errno(link, r, "Failed to update interface name in NDisc: %m");
2258 }
2259
2260 if (link->dhcp_server) {
2261 r = sd_dhcp_server_set_ifname(link->dhcp_server, link->ifname);
2262 if (r < 0)
2263 return log_link_debug_errno(link, r, "Failed to update interface name in DHCP server: %m");
2264 }
2265
2266 if (link->radv) {
2267 r = sd_radv_set_ifname(link->radv, link->ifname);
2268 if (r < 0)
2269 return log_link_debug_errno(link, r, "Failed to update interface name in Router Advertisement: %m");
2270 }
2271
2272 if (link->lldp_rx) {
2273 r = sd_lldp_rx_set_ifname(link->lldp_rx, link->ifname);
2274 if (r < 0)
2275 return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Rx: %m");
2276 }
2277
2278 if (link->lldp_tx) {
2279 r = sd_lldp_tx_set_ifname(link->lldp_tx, link->ifname);
2280 if (r < 0)
2281 return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Tx: %m");
2282 }
2283
2284 if (link->ipv4ll) {
2285 r = sd_ipv4ll_set_ifname(link->ipv4ll, link->ifname);
2286 if (r < 0)
2287 return log_link_debug_errno(link, r, "Failed to update interface name in IPv4LL client: %m");
2288 }
2289
2290 r = ipv4acd_set_ifname(link);
2291 if (r < 0)
2292 return log_link_debug_errno(link, r, "Failed to update interface name in IPv4ACD client: %m");
2293
2294 return 0;
2295 }
2296
2297 static int link_update(Link *link, sd_netlink_message *message) {
2298 int r;
2299
2300 assert(link);
2301 assert(message);
2302
2303 r = link_update_name(link, message);
2304 if (r < 0)
2305 return r;
2306
2307 r = link_update_alternative_names(link, message);
2308 if (r < 0)
2309 return r;
2310
2311 r = link_update_mtu(link, message);
2312 if (r < 0)
2313 return r;
2314
2315 r = link_update_hardware_address(link, message);
2316 if (r < 0)
2317 return r;
2318
2319 r = link_update_master(link, message);
2320 if (r < 0)
2321 return r;
2322
2323 r = link_update_ipv6ll_addrgen_mode(link, message);
2324 if (r < 0)
2325 return r;
2326
2327 return link_update_flags(link, message);
2328 }
2329
2330 static Link *link_drop_or_unref(Link *link) {
2331 if (!link)
2332 return NULL;
2333 if (!link->manager)
2334 return link_unref(link);
2335 return link_drop(link);
2336 }
2337
2338 DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_drop_or_unref);
2339
2340 static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
2341 _cleanup_free_ char *ifname = NULL, *kind = NULL, *state_file = NULL, *lease_file = NULL, *lldp_file = NULL;
2342 _cleanup_(link_drop_or_unrefp) Link *link = NULL;
2343 unsigned short iftype;
2344 int r, ifindex;
2345
2346 assert(manager);
2347 assert(message);
2348 assert(ret);
2349
2350 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2351 if (r < 0)
2352 return log_debug_errno(r, "rtnl: failed to read ifindex from link message: %m");
2353 else if (ifindex <= 0)
2354 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "rtnl: received link message without valid ifindex.");
2355
2356 r = sd_rtnl_message_link_get_type(message, &iftype);
2357 if (r < 0)
2358 return log_debug_errno(r, "rtnl: failed to read interface type from link message: %m");
2359
2360 r = sd_netlink_message_read_string_strdup(message, IFLA_IFNAME, &ifname);
2361 if (r < 0)
2362 return log_debug_errno(r, "rtnl: failed to read interface name from link message: %m");
2363
2364 /* check for link kind */
2365 r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
2366 if (r >= 0) {
2367 r = sd_netlink_message_read_string_strdup(message, IFLA_INFO_KIND, &kind);
2368 if (r < 0 && r != -ENODATA)
2369 return log_debug_errno(r, "rtnl: failed to read interface kind from link message: %m");
2370 r = sd_netlink_message_exit_container(message);
2371 if (r < 0)
2372 return log_debug_errno(r, "rtnl: failed to exit IFLA_LINKINFO container: %m");
2373 }
2374
2375 if (!manager->test_mode) {
2376 /* Do not update state files when running in test mode. */
2377 if (asprintf(&state_file, "/run/systemd/netif/links/%d", ifindex) < 0)
2378 return log_oom_debug();
2379
2380 if (asprintf(&lease_file, "/run/systemd/netif/leases/%d", ifindex) < 0)
2381 return log_oom_debug();
2382
2383 if (asprintf(&lldp_file, "/run/systemd/netif/lldp/%d", ifindex) < 0)
2384 return log_oom_debug();
2385 }
2386
2387 link = new(Link, 1);
2388 if (!link)
2389 return -ENOMEM;
2390
2391 *link = (Link) {
2392 .n_ref = 1,
2393 .state = LINK_STATE_PENDING,
2394 .online_state = _LINK_ONLINE_STATE_INVALID,
2395 .ifindex = ifindex,
2396 .iftype = iftype,
2397 .ifname = TAKE_PTR(ifname),
2398 .kind = TAKE_PTR(kind),
2399
2400 .ipv6ll_address_gen_mode = _IPV6_LINK_LOCAL_ADDRESS_GEN_MODE_INVALID,
2401
2402 .state_file = TAKE_PTR(state_file),
2403 .lease_file = TAKE_PTR(lease_file),
2404 .lldp_file = TAKE_PTR(lldp_file),
2405
2406 .n_dns = UINT_MAX,
2407 .dns_default_route = -1,
2408 .llmnr = _RESOLVE_SUPPORT_INVALID,
2409 .mdns = _RESOLVE_SUPPORT_INVALID,
2410 .dnssec_mode = _DNSSEC_MODE_INVALID,
2411 .dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID,
2412 };
2413
2414 r = hashmap_ensure_put(&manager->links_by_index, NULL, INT_TO_PTR(link->ifindex), link);
2415 if (r < 0)
2416 return log_link_debug_errno(link, r, "Failed to store link into manager: %m");
2417
2418 link->manager = manager;
2419
2420 r = hashmap_ensure_put(&manager->links_by_name, &string_hash_ops, link->ifname, link);
2421 if (r < 0)
2422 return log_link_debug_errno(link, r, "Failed to manage link by its interface name: %m");
2423
2424 log_link_debug(link, "Saved new link: ifindex=%i, iftype=%s(%u), kind=%s",
2425 link->ifindex, strna(arphrd_to_name(link->iftype)), link->iftype, strna(link->kind));
2426
2427 r = netlink_message_read_hw_addr(message, IFLA_PERM_ADDRESS, &link->permanent_hw_addr);
2428 if (r < 0) {
2429 if (r != -ENODATA)
2430 log_link_debug_errno(link, r, "Failed to read IFLA_PERM_ADDRESS attribute, ignoring: %m");
2431
2432 if (netlink_message_read_hw_addr(message, IFLA_ADDRESS, NULL) >= 0) {
2433 /* Fallback to ethtool, if the link has a hardware address. */
2434 r = ethtool_get_permanent_hw_addr(&manager->ethtool_fd, link->ifname, &link->permanent_hw_addr);
2435 if (r < 0)
2436 log_link_debug_errno(link, r, "Permanent hardware address not found, continuing without: %m");
2437 }
2438 }
2439 if (link->permanent_hw_addr.length > 0)
2440 log_link_debug(link, "Saved permanent hardware address: %s", HW_ADDR_TO_STR(&link->permanent_hw_addr));
2441
2442 r = ethtool_get_driver(&manager->ethtool_fd, link->ifname, &link->driver);
2443 if (r < 0)
2444 log_link_debug_errno(link, r, "Failed to get driver, continuing without: %m");
2445 else
2446 log_link_debug(link, "Found driver: %s", strna(link->driver));
2447
2448 if (streq_ptr(link->driver, "dsa")) {
2449 uint32_t dsa_master_ifindex;
2450
2451 r = sd_netlink_message_read_u32(message, IFLA_LINK, &dsa_master_ifindex);
2452 if (r < 0) {
2453 dsa_master_ifindex = 0;
2454 if (r != -ENODATA)
2455 log_link_warning_errno(link, r, "rtnl: failed to read ifindex of the DSA master interface, ignoring: %m");
2456 } else if (dsa_master_ifindex > INT_MAX) {
2457 dsa_master_ifindex = 0;
2458 log_link_warning(link, "rtnl: received too large DSA master ifindex (%"PRIu32" > INT_MAX), ignoring.",
2459 dsa_master_ifindex);
2460 }
2461
2462 link->dsa_master_ifindex = (int) dsa_master_ifindex;
2463 }
2464
2465 *ret = TAKE_PTR(link);
2466 return 0;
2467 }
2468
2469 int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, Manager *manager) {
2470 Link *link = NULL;
2471 NetDev *netdev = NULL;
2472 uint16_t type;
2473 const char *name;
2474 int r, ifindex;
2475
2476 assert(rtnl);
2477 assert(message);
2478 assert(manager);
2479
2480 if (sd_netlink_message_is_error(message)) {
2481 r = sd_netlink_message_get_errno(message);
2482 if (r < 0)
2483 log_message_warning_errno(message, r, "rtnl: Could not receive link message, ignoring");
2484
2485 return 0;
2486 }
2487
2488 r = sd_netlink_message_get_type(message, &type);
2489 if (r < 0) {
2490 log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m");
2491 return 0;
2492 } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) {
2493 log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type);
2494 return 0;
2495 }
2496
2497 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2498 if (r < 0) {
2499 log_warning_errno(r, "rtnl: Could not get ifindex from link message, ignoring: %m");
2500 return 0;
2501 } else if (ifindex <= 0) {
2502 log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex);
2503 return 0;
2504 }
2505
2506 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
2507 if (r < 0) {
2508 log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m");
2509 return 0;
2510 }
2511
2512 (void) link_get_by_index(manager, ifindex, &link);
2513 (void) netdev_get(manager, name, &netdev);
2514
2515 switch (type) {
2516 case RTM_NEWLINK:
2517 if (netdev) {
2518 /* netdev exists, so make sure the ifindex matches */
2519 r = netdev_set_ifindex(netdev, message);
2520 if (r < 0) {
2521 log_netdev_warning_errno(netdev, r, "Could not process new link message for netdev, ignoring: %m");
2522 return 0;
2523 }
2524 }
2525
2526 if (!link) {
2527 /* link is new, so add it */
2528 r = link_new(manager, message, &link);
2529 if (r < 0) {
2530 log_warning_errno(r, "Could not process new link message: %m");
2531 return 0;
2532 }
2533
2534 r = link_update(link, message);
2535 if (r < 0) {
2536 log_link_warning_errno(link, r, "Could not process link message: %m");
2537 link_enter_failed(link);
2538 return 0;
2539 }
2540
2541 r = link_check_initialized(link);
2542 if (r < 0) {
2543 log_link_warning_errno(link, r, "Failed to check link is initialized: %m");
2544 link_enter_failed(link);
2545 return 0;
2546 }
2547 } else {
2548 r = link_update(link, message);
2549 if (r < 0) {
2550 log_link_warning_errno(link, r, "Could not process link message: %m");
2551 link_enter_failed(link);
2552 return 0;
2553 }
2554 }
2555 break;
2556
2557 case RTM_DELLINK:
2558 link_drop(link);
2559 netdev_drop(netdev);
2560 break;
2561
2562 default:
2563 assert_not_reached();
2564 }
2565
2566 return 1;
2567 }
2568
2569 int link_getlink_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg) {
2570 uint16_t message_type;
2571 int r;
2572
2573 assert(m);
2574 assert(link);
2575 assert(error_msg);
2576
2577 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
2578 return 0;
2579
2580 r = sd_netlink_message_get_errno(m);
2581 if (r < 0) {
2582 log_link_message_warning_errno(link, m, r, error_msg);
2583 link_enter_failed(link);
2584 return 0;
2585 }
2586
2587 r = sd_netlink_message_get_type(m, &message_type);
2588 if (r < 0) {
2589 log_link_debug_errno(link, r, "rtnl: failed to read link message type, ignoring: %m");
2590 return 0;
2591 }
2592 if (message_type != RTM_NEWLINK) {
2593 log_link_debug(link, "rtnl: received invalid link message type, ignoring.");
2594 return 0;
2595 }
2596
2597 r = link_update(link, m);
2598 if (r < 0) {
2599 link_enter_failed(link);
2600 return 0;
2601 }
2602
2603 return 1;
2604 }
2605
2606 int link_call_getlink(Link *link, link_netlink_message_handler_t callback) {
2607 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
2608 int r;
2609
2610 assert(link);
2611 assert(link->manager);
2612 assert(link->manager->rtnl);
2613 assert(callback);
2614
2615 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
2616 if (r < 0)
2617 return r;
2618
2619 r = netlink_call_async(link->manager->rtnl, NULL, req, callback,
2620 link_netlink_destroy_callback, link);
2621 if (r < 0)
2622 return r;
2623
2624 link_ref(link);
2625 return 0;
2626 }
2627
2628 static const char* const link_state_table[_LINK_STATE_MAX] = {
2629 [LINK_STATE_PENDING] = "pending",
2630 [LINK_STATE_INITIALIZED] = "initialized",
2631 [LINK_STATE_CONFIGURING] = "configuring",
2632 [LINK_STATE_CONFIGURED] = "configured",
2633 [LINK_STATE_UNMANAGED] = "unmanaged",
2634 [LINK_STATE_FAILED] = "failed",
2635 [LINK_STATE_LINGER] = "linger",
2636 };
2637
2638 DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
2639
2640 int link_flags_to_string_alloc(uint32_t flags, char **ret) {
2641 _cleanup_free_ char *str = NULL;
2642 static const char* map[] = {
2643 [LOG2U(IFF_UP)] = "up", /* interface is up. */
2644 [LOG2U(IFF_BROADCAST)] = "broadcast", /* broadcast address valid.*/
2645 [LOG2U(IFF_DEBUG)] = "debug", /* turn on debugging. */
2646 [LOG2U(IFF_LOOPBACK)] = "loopback", /* interface is a loopback net. */
2647 [LOG2U(IFF_POINTOPOINT)] = "point-to-point", /* interface has p-p link. */
2648 [LOG2U(IFF_NOTRAILERS)] = "no-trailers", /* avoid use of trailers. */
2649 [LOG2U(IFF_RUNNING)] = "running", /* interface RFC2863 OPER_UP. */
2650 [LOG2U(IFF_NOARP)] = "no-arp", /* no ARP protocol. */
2651 [LOG2U(IFF_PROMISC)] = "promiscuous", /* receive all packets. */
2652 [LOG2U(IFF_ALLMULTI)] = "all-multicast", /* receive all multicast packets. */
2653 [LOG2U(IFF_MASTER)] = "master", /* master of a load balancer. */
2654 [LOG2U(IFF_SLAVE)] = "slave", /* slave of a load balancer. */
2655 [LOG2U(IFF_MULTICAST)] = "multicast", /* supports multicast.*/
2656 [LOG2U(IFF_PORTSEL)] = "portsel", /* can set media type. */
2657 [LOG2U(IFF_AUTOMEDIA)] = "auto-media", /* auto media select active. */
2658 [LOG2U(IFF_DYNAMIC)] = "dynamic", /* dialup device with changing addresses. */
2659 [LOG2U(IFF_LOWER_UP)] = "lower-up", /* driver signals L1 up. */
2660 [LOG2U(IFF_DORMANT)] = "dormant", /* driver signals dormant. */
2661 [LOG2U(IFF_ECHO)] = "echo", /* echo sent packets. */
2662 };
2663
2664 assert(ret);
2665
2666 for (size_t i = 0; i < ELEMENTSOF(map); i++)
2667 if (FLAGS_SET(flags, 1 << i) && map[i])
2668 if (!strextend_with_separator(&str, ",", map[i]))
2669 return -ENOMEM;
2670
2671 *ret = TAKE_PTR(str);
2672 return 0;
2673 }
2674
2675 static const char * const kernel_operstate_table[] = {
2676 [IF_OPER_UNKNOWN] = "unknown",
2677 [IF_OPER_NOTPRESENT] = "not-present",
2678 [IF_OPER_DOWN] = "down",
2679 [IF_OPER_LOWERLAYERDOWN] = "lower-layer-down",
2680 [IF_OPER_TESTING] = "testing",
2681 [IF_OPER_DORMANT] = "dormant",
2682 [IF_OPER_UP] = "up",
2683 };
2684
2685 DEFINE_STRING_TABLE_LOOKUP_TO_STRING(kernel_operstate, int);