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