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