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