Debian Codegraph @

src/network/networkd-link.c

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