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