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