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