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