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