]>
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 YW |
211 | |
212 | link->lldp = sd_lldp_unref(link->lldp); | |
1c494872 | 213 | link_lldp_emit_stop(link); |
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 YW |
240 | link->nexthops = set_free(link->nexthops); |
241 | link->nexthops_foreign = set_free(link->nexthops_foreign); | |
c16c7808 | 242 | |
8eec0b9d YW |
243 | link->neighbors = set_free(link->neighbors); |
244 | link->neighbors_foreign = set_free(link->neighbors_foreign); | |
d1bdafd2 | 245 | |
8eec0b9d YW |
246 | link->addresses = set_free(link->addresses); |
247 | link->addresses_foreign = set_free(link->addresses_foreign); | |
76a86ffd | 248 | link->addresses_ipv4acd = set_free(link->addresses_ipv4acd); |
aa651e88 | 249 | link->pool_addresses = set_free(link->pool_addresses); |
e5526518 | 250 | link->static_addresses = set_free(link->static_addresses); |
1633c457 YW |
251 | link->dhcp6_addresses = set_free(link->dhcp6_addresses); |
252 | link->dhcp6_addresses_old = set_free(link->dhcp6_addresses_old); | |
253 | link->dhcp6_pd_addresses = set_free(link->dhcp6_pd_addresses); | |
254 | link->dhcp6_pd_addresses_old = set_free(link->dhcp6_pd_addresses_old); | |
69203fba | 255 | link->ndisc_addresses = set_free(link->ndisc_addresses); |
adda1ed9 | 256 | |
4b409e85 YW |
257 | link->dhcp6_pd_prefixes = set_free(link->dhcp6_pd_prefixes); |
258 | ||
1a6bb31f | 259 | link_free_engines(link); |
49699bac | 260 | |
c166a070 | 261 | free(link->ifname); |
572b21d9 | 262 | strv_free(link->alternative_names); |
ceac4078 | 263 | free(link->kind); |
8d968fdd | 264 | free(link->ssid); |
c643bda5 | 265 | free(link->driver); |
6cad256d | 266 | |
a34e58d4 YW |
267 | unlink_and_free(link->lease_file); |
268 | unlink_and_free(link->lldp_file); | |
269 | unlink_and_free(link->state_file); | |
c166a070 | 270 | |
51517f9e | 271 | sd_device_unref(link->sd_device); |
b5db00e5 | 272 | |
0d4ad91d | 273 | hashmap_free(link->bound_to_links); |
0d4ad91d AR |
274 | hashmap_free(link->bound_by_links); |
275 | ||
5f707e12 | 276 | set_free_with_destructor(link->slaves, link_unref); |
033295c1 | 277 | |
c9c908a6 YW |
278 | network_unref(link->network); |
279 | ||
8301aa0b | 280 | return mfree(link); |
14b746f7 TG |
281 | } |
282 | ||
8301aa0b | 283 | DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free); |
14b746f7 | 284 | |
6eab614d | 285 | int link_get_by_index(Manager *m, int ifindex, Link **ret) { |
11a7f229 | 286 | Link *link; |
11a7f229 TG |
287 | |
288 | assert(m); | |
e856ed00 | 289 | assert(ifindex > 0); |
11a7f229 | 290 | |
6eab614d | 291 | link = hashmap_get(m->links_by_index, INT_TO_PTR(ifindex)); |
11a7f229 TG |
292 | if (!link) |
293 | return -ENODEV; | |
294 | ||
0b54c870 YW |
295 | if (ret) |
296 | *ret = link; | |
297 | return 0; | |
298 | } | |
299 | ||
300 | int link_get_by_name(Manager *m, const char *ifname, Link **ret) { | |
301 | Link *link; | |
302 | ||
303 | assert(m); | |
304 | assert(ifname); | |
11a7f229 | 305 | |
0b54c870 YW |
306 | link = hashmap_get(m->links_by_name, ifname); |
307 | if (!link) | |
308 | return -ENODEV; | |
309 | ||
310 | if (ret) | |
311 | *ret = link; | |
11a7f229 TG |
312 | return 0; |
313 | } | |
314 | ||
fe321d45 YW |
315 | int link_get_by_hw_addr(Manager *m, const struct hw_addr_data *hw_addr, Link **ret) { |
316 | Link *link; | |
317 | ||
318 | assert(m); | |
319 | assert(hw_addr); | |
320 | ||
321 | link = hashmap_get(m->links_by_hw_addr, hw_addr); | |
322 | if (!link) | |
323 | return -ENODEV; | |
324 | ||
325 | if (ret) | |
326 | *ret = link; | |
327 | return 0; | |
328 | } | |
329 | ||
81357285 YW |
330 | int link_get_master(Link *link, Link **ret) { |
331 | assert(link); | |
332 | assert(link->manager); | |
333 | assert(ret); | |
334 | ||
335 | if (link->master_ifindex <= 0 || link->master_ifindex == link->ifindex) | |
336 | return -ENODEV; | |
337 | ||
6eab614d | 338 | return link_get_by_index(link->manager, link->master_ifindex, ret); |
81357285 YW |
339 | } |
340 | ||
af9ba57a | 341 | void link_set_state(Link *link, LinkState state) { |
e331e246 TG |
342 | assert(link); |
343 | ||
344 | if (link->state == state) | |
345 | return; | |
346 | ||
0beb9542 YW |
347 | log_link_debug(link, "State changed: %s -> %s", |
348 | link_state_to_string(link->state), | |
349 | link_state_to_string(state)); | |
350 | ||
e331e246 TG |
351 | link->state = state; |
352 | ||
353 | link_send_changed(link, "AdministrativeState", NULL); | |
9092113d | 354 | link_dirty(link); |
e331e246 TG |
355 | } |
356 | ||
2a99eed0 | 357 | int link_stop_engines(Link *link, bool may_keep_dhcp) { |
111bb8f9 TG |
358 | int r = 0, k; |
359 | ||
360 | assert(link); | |
361 | assert(link->manager); | |
362 | assert(link->manager->event); | |
363 | ||
80060352 ZJS |
364 | bool keep_dhcp = may_keep_dhcp && |
365 | link->network && | |
76a86ffd | 366 | !link->network->dhcp_send_decline && /* IPv4 ACD for the DHCPv4 address is running. */ |
80060352 ZJS |
367 | (link->manager->restarting || |
368 | FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP_ON_STOP)); | |
369 | ||
84add3cd | 370 | if (!keep_dhcp) { |
111bb8f9 | 371 | k = sd_dhcp_client_stop(link->dhcp_client); |
6a7a4e4d | 372 | if (k < 0) |
36c7d709 | 373 | r = log_link_warning_errno(link, k, "Could not stop DHCPv4 client: %m"); |
111bb8f9 TG |
374 | } |
375 | ||
2a99eed0 YW |
376 | k = sd_dhcp_server_stop(link->dhcp_server); |
377 | if (k < 0) | |
378 | r = log_link_warning_errno(link, k, "Could not stop DHCPv4 server: %m"); | |
379 | ||
9cc65242 YW |
380 | k = sd_lldp_stop(link->lldp); |
381 | if (k < 0) | |
382 | r = log_link_warning_errno(link, k, "Could not stop LLDP: %m"); | |
383 | ||
84add3cd YW |
384 | k = sd_ipv4ll_stop(link->ipv4ll); |
385 | if (k < 0) | |
386 | r = log_link_warning_errno(link, k, "Could not stop IPv4 link-local: %m"); | |
dd43110f | 387 | |
76a86ffd | 388 | k = ipv4acd_stop(link); |
2488e4d9 YW |
389 | if (k < 0) |
390 | r = log_link_warning_errno(link, k, "Could not stop IPv4 ACD client: %m"); | |
051e77ca | 391 | |
84add3cd YW |
392 | k = sd_dhcp6_client_stop(link->dhcp6_client); |
393 | if (k < 0) | |
394 | r = log_link_warning_errno(link, k, "Could not stop DHCPv6 client: %m"); | |
4138fb2c | 395 | |
2ffd6d73 YW |
396 | k = dhcp6_pd_remove(link); |
397 | if (k < 0) | |
398 | r = log_link_warning_errno(link, k, "Could not remove DHCPv6 PD addresses and routes: %m"); | |
1633c457 | 399 | |
84add3cd YW |
400 | k = sd_ndisc_stop(link->ndisc); |
401 | if (k < 0) | |
402 | r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Discovery: %m"); | |
4138fb2c | 403 | |
84add3cd YW |
404 | k = sd_radv_stop(link->radv); |
405 | if (k < 0) | |
406 | r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Advertisement: %m"); | |
7465dd22 | 407 | |
7272b25e | 408 | link_lldp_emit_stop(link); |
111bb8f9 TG |
409 | return r; |
410 | } | |
411 | ||
b22d8a00 | 412 | void link_enter_failed(Link *link) { |
ef1ba606 | 413 | assert(link); |
f882c247 | 414 | |
370e9930 | 415 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
2139694e TG |
416 | return; |
417 | ||
6a7a4e4d | 418 | log_link_warning(link, "Failed"); |
449f7554 | 419 | |
e331e246 | 420 | link_set_state(link, LINK_STATE_FAILED); |
fe8db0c5 | 421 | |
2a99eed0 | 422 | (void) link_stop_engines(link, false); |
f882c247 TG |
423 | } |
424 | ||
6accfd31 DA |
425 | void link_check_ready(Link *link) { |
426 | Address *a; | |
6accfd31 DA |
427 | |
428 | assert(link); | |
429 | ||
5f58af25 YW |
430 | if (link->state == LINK_STATE_CONFIGURED) |
431 | return; | |
432 | ||
d19b9939 YW |
433 | if (link->state != LINK_STATE_CONFIGURING) |
434 | return (void) log_link_debug(link, "%s(): link is in %s state.", __func__, link_state_to_string(link->state)); | |
6accfd31 DA |
435 | |
436 | if (!link->network) | |
a7f07cbe | 437 | return (void) log_link_debug(link, "%s(): link is unmanaged.", __func__); |
6accfd31 | 438 | |
7558f9e7 YW |
439 | if (!link->tc_configured) |
440 | return (void) log_link_debug(link, "%s(): traffic controls are not configured.", __func__); | |
600b7898 | 441 | |
a7f07cbe YW |
442 | if (link->set_link_messages > 0) |
443 | return (void) log_link_debug(link, "%s(): link layer is configuring.", __func__); | |
444 | ||
445 | if (!link->activated) | |
446 | return (void) log_link_debug(link, "%s(): link is not activated.", __func__); | |
447 | ||
7558f9e7 YW |
448 | if (link->iftype == ARPHRD_CAN) { |
449 | /* let's shortcut things for CAN which doesn't need most of checks below. */ | |
450 | link_set_state(link, LINK_STATE_CONFIGURED); | |
451 | return; | |
452 | } | |
453 | ||
76c5a0f2 | 454 | if (!link->static_addresses_configured) |
d19b9939 | 455 | return (void) log_link_debug(link, "%s(): static addresses are not configured.", __func__); |
6accfd31 | 456 | |
90e74a66 | 457 | SET_FOREACH(a, link->addresses) |
39373cb9 YW |
458 | if (!address_is_ready(a)) { |
459 | _cleanup_free_ char *str = NULL; | |
460 | ||
5380707a YW |
461 | (void) in_addr_prefix_to_string(a->family, &a->in_addr, a->prefixlen, &str); |
462 | return (void) log_link_debug(link, "%s(): an address %s is not ready.", __func__, strna(str)); | |
39373cb9 | 463 | } |
6aa5773b | 464 | |
354bc760 YW |
465 | if (!link->static_address_labels_configured) |
466 | return (void) log_link_debug(link, "%s(): static address labels are not configured.", __func__); | |
467 | ||
e5b35bf6 YW |
468 | if (!link->static_bridge_fdb_configured) |
469 | return (void) log_link_debug(link, "%s(): static bridge MDB entries are not configured.", __func__); | |
470 | ||
ff9e0783 YW |
471 | if (!link->static_bridge_mdb_configured) |
472 | return (void) log_link_debug(link, "%s(): static bridge MDB entries are not configured.", __func__); | |
473 | ||
fdeba3f5 YW |
474 | if (!link->static_ipv6_proxy_ndp_configured) |
475 | return (void) log_link_debug(link, "%s(): static IPv6 proxy NDP addresses are not configured.", __func__); | |
476 | ||
40ca350e YW |
477 | if (!link->static_neighbors_configured) |
478 | return (void) log_link_debug(link, "%s(): static neighbors are not configured.", __func__); | |
479 | ||
d19b9939 YW |
480 | if (!link->static_nexthops_configured) |
481 | return (void) log_link_debug(link, "%s(): static nexthops are not configured.", __func__); | |
c16c7808 | 482 | |
76c5a0f2 YW |
483 | if (!link->static_routes_configured) |
484 | return (void) log_link_debug(link, "%s(): static routes are not configured.", __func__); | |
485 | ||
0e5ef6be | 486 | if (!link->static_routing_policy_rules_configured) |
d19b9939 | 487 | return (void) log_link_debug(link, "%s(): static routing policy rules are not configured.", __func__); |
6accfd31 | 488 | |
d19b9939 YW |
489 | if (!link->sr_iov_configured) |
490 | return (void) log_link_debug(link, "%s(): SR-IOV is not configured.", __func__); | |
518cd6b5 | 491 | |
4bcb8625 YW |
492 | /* IPv6LL is assigned after the link gains its carrier. */ |
493 | if (!link->network->configure_without_carrier && | |
494 | link_ipv6ll_enabled(link) && | |
495 | !in6_addr_is_set(&link->ipv6ll_address)) | |
496 | return (void) log_link_debug(link, "%s(): IPv6LL is not configured yet.", __func__); | |
497 | ||
498 | bool has_ndisc_address = false; | |
499 | NDiscAddress *n; | |
500 | SET_FOREACH(n, link->ndisc_addresses) | |
501 | if (!n->marked) { | |
502 | has_ndisc_address = true; | |
503 | break; | |
504 | } | |
50550722 | 505 | |
4bcb8625 YW |
506 | if ((link_dhcp4_enabled(link) || link_dhcp6_with_address_enabled(link) || link_ipv4ll_enabled(link)) && |
507 | !link->dhcp_address && set_isempty(link->dhcp6_addresses) && !has_ndisc_address && | |
508 | !link->ipv4ll_address_configured) | |
509 | /* When DHCP[46] or IPv4LL is enabled, at least one address is acquired by them. */ | |
87160186 | 510 | return (void) log_link_debug(link, "%s(): DHCPv4, DHCPv6 or IPv4LL is enabled but no dynamic address is assigned yet.", __func__); |
4bcb8625 YW |
511 | |
512 | /* Ignore NDisc when ConfigureWithoutCarrier= is enabled, as IPv6AcceptRA= is enabled by default. */ | |
513 | if (link_dhcp4_enabled(link) || link_dhcp6_enabled(link) || link_dhcp6_pd_is_enabled(link) || | |
514 | (!link->network->configure_without_carrier && link_ipv6_accept_ra_enabled(link)) || | |
515 | link_ipv4ll_enabled(link)) { | |
516 | ||
517 | if (!link->dhcp4_configured && | |
518 | !(link->dhcp6_address_configured && link->dhcp6_route_configured) && | |
519 | !(link->dhcp6_pd_address_configured && link->dhcp6_pd_route_configured) && | |
520 | !(link->ndisc_addresses_configured && link->ndisc_routes_configured) && | |
0d0799da | 521 | !link->ipv4ll_address_configured) |
4bcb8625 YW |
522 | /* When DHCP[46], NDisc, or IPv4LL is enabled, at least one protocol must be finished. */ |
523 | return (void) log_link_debug(link, "%s(): dynamic addresses or routes are not configured.", __func__); | |
524 | ||
87160186 YW |
525 | log_link_debug(link, "%s(): DHCPv4:%s IPv4LL:%s DHCPv6_addresses:%s DHCPv6_routes:%s " |
526 | "DHCPv6PD_addresses:%s DHCPv6PD_routes:%s NDisc_addresses:%s NDisc_routes:%s", | |
4bcb8625 YW |
527 | __func__, |
528 | yes_no(link->dhcp4_configured), | |
529 | yes_no(link->ipv4ll_address_configured), | |
530 | yes_no(link->dhcp6_address_configured), | |
531 | yes_no(link->dhcp6_route_configured), | |
532 | yes_no(link->dhcp6_pd_address_configured), | |
533 | yes_no(link->dhcp6_pd_route_configured), | |
534 | yes_no(link->ndisc_addresses_configured), | |
535 | yes_no(link->ndisc_routes_configured)); | |
463797c1 | 536 | } |
6accfd31 | 537 | |
29836c16 | 538 | link_set_state(link, LINK_STATE_CONFIGURED); |
6accfd31 DA |
539 | } |
540 | ||
97108953 | 541 | static int link_request_static_configs(Link *link) { |
f882c247 TG |
542 | int r; |
543 | ||
544 | assert(link); | |
545 | assert(link->network); | |
f5be5601 | 546 | assert(link->state != _LINK_STATE_INVALID); |
f882c247 | 547 | |
354bc760 | 548 | r = link_request_static_addresses(link); |
0e5ef6be YW |
549 | if (r < 0) |
550 | return r; | |
551 | ||
354bc760 | 552 | r = link_request_static_address_labels(link); |
f3ef324d YW |
553 | if (r < 0) |
554 | return r; | |
e4a71bf3 | 555 | |
e5b35bf6 YW |
556 | r = link_request_static_bridge_fdb(link); |
557 | if (r < 0) | |
558 | return r; | |
559 | ||
9a038aac YW |
560 | r = link_request_static_bridge_mdb(link); |
561 | if (r < 0) | |
562 | return r; | |
563 | ||
fdeba3f5 YW |
564 | r = link_request_static_ipv6_proxy_ndp_addresses(link); |
565 | if (r < 0) | |
566 | return r; | |
567 | ||
40ca350e | 568 | r = link_request_static_neighbors(link); |
682c65b0 YW |
569 | if (r < 0) |
570 | return r; | |
bd6379ec | 571 | |
76c5a0f2 | 572 | r = link_request_static_nexthops(link, false); |
fe2bc17c YW |
573 | if (r < 0) |
574 | return r; | |
f882c247 | 575 | |
76c5a0f2 YW |
576 | r = link_request_static_routes(link, false); |
577 | if (r < 0) | |
578 | return r; | |
579 | ||
580 | r = link_request_static_routing_policy_rules(link); | |
5ae0fb7f YW |
581 | if (r < 0) |
582 | return r; | |
d4cdbea5 | 583 | |
f882c247 TG |
584 | return 0; |
585 | } | |
586 | ||
1187fc33 YW |
587 | static int link_request_stacked_netdevs(Link *link) { |
588 | NetDev *netdev; | |
e16e4b3b DS |
589 | int r; |
590 | ||
591 | assert(link); | |
592 | ||
1187fc33 YW |
593 | link->stacked_netdevs_created = false; |
594 | link->stacked_netdevs_after_configured_created = false; | |
e16e4b3b | 595 | |
1187fc33 | 596 | HASHMAP_FOREACH(netdev, link->network->stacked_netdevs) { |
b14686ff | 597 | r = link_request_stacked_netdev(link, netdev); |
1187fc33 YW |
598 | if (r < 0) |
599 | return r; | |
600 | } | |
e16e4b3b | 601 | |
1187fc33 YW |
602 | if (link->create_stacked_netdev_messages == 0) |
603 | link->stacked_netdevs_created = true; | |
604 | if (link->create_stacked_netdev_after_configured_messages == 0) | |
605 | link->stacked_netdevs_after_configured_created = true; | |
e16e4b3b DS |
606 | |
607 | return 0; | |
608 | } | |
609 | ||
8566df79 | 610 | static int link_acquire_dynamic_ipv6_conf(Link *link) { |
e7ab854c TG |
611 | int r; |
612 | ||
613 | assert(link); | |
614 | ||
086b8853 | 615 | if (link->radv) { |
7465dd22 | 616 | assert(link->radv); |
94876904 | 617 | assert(in6_addr_is_link_local(&link->ipv6ll_address)); |
7465dd22 PF |
618 | |
619 | log_link_debug(link, "Starting IPv6 Router Advertisements"); | |
620 | ||
621 | r = sd_radv_start(link->radv); | |
a254fab2 | 622 | if (r < 0) |
7465dd22 PF |
623 | return log_link_warning_errno(link, r, "Could not start IPv6 Router Advertisement: %m"); |
624 | } | |
625 | ||
294f129b YW |
626 | r = ndisc_start(link); |
627 | if (r < 0) | |
628 | return log_link_warning_errno(link, r, "Failed to start IPv6 Router Discovery: %m"); | |
cd305af1 | 629 | |
294f129b YW |
630 | r = dhcp6_start(link); |
631 | if (r < 0) | |
632 | return log_link_warning_errno(link, r, "Failed to start DHCPv6 client: %m"); | |
cd305af1 | 633 | |
1633c457 YW |
634 | r = dhcp6_request_prefix_delegation(link); |
635 | if (r < 0) | |
636 | return log_link_warning_errno(link, r, "Failed to request DHCPv6 prefix delegation: %m"); | |
10752343 | 637 | |
e7ab854c TG |
638 | return 0; |
639 | } | |
640 | ||
8566df79 | 641 | static int link_acquire_dynamic_ipv4_conf(Link *link) { |
ff254138 TG |
642 | int r; |
643 | ||
644 | assert(link); | |
ff254138 TG |
645 | assert(link->manager); |
646 | assert(link->manager->event); | |
647 | ||
0107b769 | 648 | if (link->dhcp_client) { |
294f129b | 649 | r = dhcp4_start(link); |
0107b769 | 650 | if (r < 0) |
294f129b | 651 | return log_link_warning_errno(link, r, "Failed to start DHCPv4 client: %m"); |
0107b769 | 652 | |
ccffa166 | 653 | log_link_debug(link, "Acquiring DHCPv4 lease."); |
5c1d3fc9 | 654 | |
ccffa166 | 655 | } else if (link->ipv4ll) { |
5c1d3fc9 | 656 | r = sd_ipv4ll_start(link->ipv4ll); |
6a7a4e4d LP |
657 | if (r < 0) |
658 | return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m"); | |
ccffa166 YW |
659 | |
660 | log_link_debug(link, "Acquiring IPv4 link-local address."); | |
5c1d3fc9 UTL |
661 | } |
662 | ||
ab486ef4 YW |
663 | if (link->dhcp_server) { |
664 | r = sd_dhcp_server_start(link->dhcp_server); | |
665 | if (r < 0) | |
666 | return log_link_warning_errno(link, r, "Could not start DHCP server: %m"); | |
667 | } | |
668 | ||
76a86ffd YW |
669 | r = ipv4acd_start(link); |
670 | if (r < 0) | |
671 | return log_link_warning_errno(link, r, "Could not start IPv4 ACD client: %m"); | |
672 | ||
6fc25497 SS |
673 | return 0; |
674 | } | |
675 | ||
8566df79 | 676 | static int link_acquire_dynamic_conf(Link *link) { |
6fc25497 SS |
677 | int r; |
678 | ||
679 | assert(link); | |
680 | ||
8566df79 | 681 | r = link_acquire_dynamic_ipv4_conf(link); |
6fc25497 SS |
682 | if (r < 0) |
683 | return r; | |
684 | ||
94876904 | 685 | if (in6_addr_is_set(&link->ipv6ll_address)) { |
8566df79 | 686 | r = link_acquire_dynamic_ipv6_conf(link); |
6fc25497 SS |
687 | if (r < 0) |
688 | return r; | |
689 | } | |
690 | ||
2ffd6d73 YW |
691 | r = link_lldp_emit_start(link); |
692 | if (r < 0) | |
693 | return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m"); | |
8e1ad1ea | 694 | |
f8549910 YW |
695 | if (link->lldp) { |
696 | r = sd_lldp_start(link->lldp); | |
697 | if (r < 0) | |
698 | return log_link_warning_errno(link, r, "Failed to start LLDP client: %m"); | |
699 | } | |
700 | ||
ff254138 TG |
701 | return 0; |
702 | } | |
703 | ||
0c9ee5d5 YW |
704 | int link_ipv6ll_gained(Link *link, const struct in6_addr *address) { |
705 | int r; | |
deb2e523 | 706 | |
0c9ee5d5 | 707 | assert(link); |
deb2e523 | 708 | |
0c9ee5d5 | 709 | log_link_info(link, "Gained IPv6LL"); |
deb2e523 | 710 | |
0c9ee5d5 YW |
711 | link->ipv6ll_address = *address; |
712 | link_check_ready(link); | |
713 | ||
714 | if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) { | |
715 | r = link_acquire_dynamic_ipv6_conf(link); | |
716 | if (r < 0) { | |
717 | link_enter_failed(link); | |
718 | return r; | |
719 | } | |
720 | } | |
721 | ||
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 | |
5e3bb5da YW |
1170 | r = link_drop_foreign_config(link); |
1171 | if (r < 0) | |
1172 | return r; | |
4c649652 | 1173 | |
1187fc33 YW |
1174 | r = link_request_static_configs(link); |
1175 | if (r < 0) | |
1176 | return r; | |
1177 | ||
1178 | if (!link_has_carrier(link)) | |
1179 | return 0; | |
1180 | ||
1181 | return link_acquire_dynamic_conf(link); | |
505f8da7 TG |
1182 | } |
1183 | ||
170e88c8 YW |
1184 | static int link_get_network(Link *link, Network **ret) { |
1185 | Network *network; | |
1a3caa49 | 1186 | int r; |
170e88c8 YW |
1187 | |
1188 | assert(link); | |
1189 | assert(link->manager); | |
1190 | assert(ret); | |
1191 | ||
1192 | ORDERED_HASHMAP_FOREACH(network, link->manager->networks) { | |
1193 | bool warn = false; | |
1194 | ||
1a3caa49 | 1195 | r = net_match_config( |
170e88c8 YW |
1196 | &network->match, |
1197 | link->sd_device, | |
ca2b7cd8 | 1198 | &link->hw_addr.ether, |
170e88c8 YW |
1199 | &link->permanent_mac, |
1200 | link->driver, | |
1201 | link->iftype, | |
1202 | link->ifname, | |
1203 | link->alternative_names, | |
1204 | link->wlan_iftype, | |
1205 | link->ssid, | |
1a3caa49 YW |
1206 | &link->bssid); |
1207 | if (r < 0) | |
1208 | return r; | |
1209 | if (r == 0) | |
170e88c8 YW |
1210 | continue; |
1211 | ||
1212 | if (network->match.ifname && link->sd_device) { | |
1213 | uint8_t name_assign_type = NET_NAME_UNKNOWN; | |
1214 | const char *attr; | |
1215 | ||
1216 | if (sd_device_get_sysattr_value(link->sd_device, "name_assign_type", &attr) >= 0) | |
1217 | (void) safe_atou8(attr, &name_assign_type); | |
1218 | ||
1219 | warn = name_assign_type == NET_NAME_ENUM; | |
1220 | } | |
1221 | ||
1222 | log_link_full(link, warn ? LOG_WARNING : LOG_DEBUG, | |
1223 | "found matching network '%s'%s.", | |
1224 | network->filename, | |
1225 | warn ? ", based on potentially unpredictable interface name" : ""); | |
1226 | ||
1227 | if (network->unmanaged) | |
1228 | return -ENOENT; | |
1229 | ||
1230 | *ret = network; | |
1231 | return 0; | |
1232 | } | |
1233 | ||
1234 | return -ENOENT; | |
1235 | } | |
1236 | ||
7f80fa12 | 1237 | static int link_reconfigure_impl(Link *link, bool force) { |
cb71e9c6 | 1238 | Network *network = NULL; |
ad932b15 YW |
1239 | int r; |
1240 | ||
5a1860f7 | 1241 | assert(link); |
572b21d9 | 1242 | |
170e88c8 | 1243 | r = link_get_network(link, &network); |
cb71e9c6 | 1244 | if (r < 0 && r != -ENOENT) |
ad932b15 YW |
1245 | return r; |
1246 | ||
99b8517c | 1247 | if (link->network == network && !force) |
ad932b15 YW |
1248 | return 0; |
1249 | ||
cb71e9c6 YW |
1250 | if (network) |
1251 | log_link_info(link, "Reconfiguring with %s.", network->filename); | |
1252 | else | |
1253 | log_link_info(link, "Unmanaging interface."); | |
ad932b15 YW |
1254 | |
1255 | /* Dropping old .network file */ | |
2a99eed0 | 1256 | r = link_stop_engines(link, false); |
572b21d9 | 1257 | if (r < 0) |
ad932b15 | 1258 | return r; |
ad932b15 | 1259 | |
19d9a5ad YW |
1260 | link_drop_requests(link); |
1261 | ||
ad932b15 YW |
1262 | r = link_drop_config(link); |
1263 | if (r < 0) | |
1264 | return r; | |
1265 | ||
ad932b15 YW |
1266 | link_free_carrier_maps(link); |
1267 | link_free_engines(link); | |
1268 | link->network = network_unref(link->network); | |
1269 | ||
cb71e9c6 YW |
1270 | if (!network) { |
1271 | link_set_state(link, LINK_STATE_UNMANAGED); | |
1272 | return 0; | |
1273 | } | |
1274 | ||
ad932b15 | 1275 | /* Then, apply new .network file */ |
9092113d | 1276 | link->network = network_ref(network); |
b156a95d | 1277 | link_update_operstate(link, true); |
9092113d | 1278 | link_dirty(link); |
ad932b15 YW |
1279 | |
1280 | r = link_new_carrier_maps(link); | |
1281 | if (r < 0) | |
1282 | return r; | |
1283 | ||
1284 | link_set_state(link, LINK_STATE_INITIALIZED); | |
61135582 | 1285 | link->activated = false; |
ad932b15 | 1286 | |
ad932b15 YW |
1287 | r = link_configure(link); |
1288 | if (r < 0) | |
1289 | return r; | |
1290 | ||
0e397560 | 1291 | return 1; |
ad932b15 YW |
1292 | } |
1293 | ||
d09a179e YW |
1294 | static int link_reconfigure_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, bool force, bool update_wifi) { |
1295 | bool link_was_lower_up; | |
572b21d9 YW |
1296 | int r; |
1297 | ||
d09a179e YW |
1298 | assert(link); |
1299 | ||
1300 | link_was_lower_up = link->flags & IFF_LOWER_UP; | |
1301 | ||
5a1860f7 YW |
1302 | r = link_getlink_handler_internal(rtnl, m, link, "Failed to update link state"); |
1303 | if (r <= 0) | |
1304 | return r; | |
1305 | ||
d09a179e YW |
1306 | if (update_wifi && link_was_lower_up && link->flags & IFF_LOWER_UP) { |
1307 | /* If the interface's L1 was not up, then wifi_get_info() is already called in | |
1308 | * link_update_flags(). So, it is not necessary to re-call here. */ | |
1309 | r = wifi_get_info(link); | |
1310 | if (r < 0) { | |
1311 | link_enter_failed(link); | |
1312 | return 0; | |
1313 | } | |
1314 | } | |
1315 | ||
7f80fa12 | 1316 | r = link_reconfigure_impl(link, force); |
d09a179e | 1317 | if (r < 0) { |
572b21d9 | 1318 | link_enter_failed(link); |
d09a179e YW |
1319 | return 0; |
1320 | } | |
572b21d9 | 1321 | |
d09a179e | 1322 | return r; |
572b21d9 YW |
1323 | } |
1324 | ||
5a1860f7 | 1325 | static int link_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
d09a179e | 1326 | return link_reconfigure_handler_internal(rtnl, m, link, /* force = */ false, /* update_wifi = */ false); |
5a1860f7 | 1327 | } |
572b21d9 | 1328 | |
5a1860f7 | 1329 | static int link_force_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
d09a179e | 1330 | return link_reconfigure_handler_internal(rtnl, m, link, /* force = */ true, /* update_wifi = */ false); |
572b21d9 YW |
1331 | } |
1332 | ||
d09a179e | 1333 | static int link_reconfigure_after_sleep_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
572b21d9 YW |
1334 | int r; |
1335 | ||
d09a179e YW |
1336 | assert(link); |
1337 | ||
1338 | r = link_reconfigure_handler_internal(rtnl, m, link, /* force = */ false, /* update_wifi = */ true); | |
1339 | if (r != 0) | |
1340 | return r; | |
1341 | ||
1342 | /* r == 0 means an error occurs, the link is unmanaged, or the matching network file is unchanged. */ | |
1343 | if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) | |
1344 | return 0; | |
1345 | ||
1346 | /* re-request static configs, and restart engines. */ | |
1347 | r = link_stop_engines(link, false); | |
1348 | if (r < 0) { | |
1349 | link_enter_failed(link); | |
1350 | return 0; | |
1351 | } | |
1352 | ||
1353 | r = link_acquire_dynamic_conf(link); | |
1354 | if (r < 0) { | |
1355 | link_enter_failed(link); | |
1356 | return 0; | |
1357 | } | |
1358 | ||
1359 | r = link_request_static_configs(link); | |
1360 | if (r < 0) { | |
1361 | link_enter_failed(link); | |
1362 | return 0; | |
1363 | } | |
1364 | ||
1365 | return 0; | |
1366 | } | |
1367 | ||
1368 | static int link_reconfigure_internal(Link *link, link_netlink_message_handler_t callback) { | |
1369 | int r; | |
1370 | ||
1371 | assert(link); | |
1372 | assert(callback); | |
1373 | ||
8bceafa7 | 1374 | /* When link in pending or initialized state, then link_configure() will be called. To prevent |
f8dd4077 ZJS |
1375 | * the function from being called multiple times simultaneously, refuse to reconfigure the |
1376 | * interface in these cases. */ | |
8bceafa7 | 1377 | if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED, LINK_STATE_LINGER)) |
f8dd4077 | 1378 | return 0; /* 0 means no-op. */ |
2c0d7ed3 | 1379 | |
d09a179e | 1380 | r = link_call_getlink(link, callback); |
572b21d9 YW |
1381 | if (r < 0) |
1382 | return r; | |
1383 | ||
8bceafa7 | 1384 | return 1; /* 1 means the interface will be reconfigured. */ |
572b21d9 YW |
1385 | } |
1386 | ||
d09a179e YW |
1387 | int link_reconfigure(Link *link, bool force) { |
1388 | return link_reconfigure_internal(link, force ? link_force_reconfigure_handler : link_reconfigure_handler); | |
1389 | } | |
1390 | ||
1391 | int link_reconfigure_after_sleep(Link *link) { | |
1392 | return link_reconfigure_internal(link, link_reconfigure_after_sleep_handler); | |
1393 | } | |
1394 | ||
e6bf7774 | 1395 | static int link_initialized_and_synced(Link *link) { |
505f8da7 | 1396 | Network *network; |
505f8da7 TG |
1397 | int r; |
1398 | ||
1399 | assert(link); | |
1400 | assert(link->ifname); | |
1401 | assert(link->manager); | |
1402 | ||
bd08ce56 | 1403 | /* We may get called either from the asynchronous netlink callback, |
0d411b7f | 1404 | * or directly from link_check_initialized() if running in a container. */ |
bd08ce56 | 1405 | if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED)) |
4232cf04 | 1406 | return 0; |
505f8da7 | 1407 | |
6a7a4e4d | 1408 | log_link_debug(link, "Link state is up-to-date"); |
bd08ce56 | 1409 | link_set_state(link, LINK_STATE_INITIALIZED); |
505f8da7 | 1410 | |
0d4ad91d AR |
1411 | r = link_new_bound_by_list(link); |
1412 | if (r < 0) | |
1413 | return r; | |
1414 | ||
1415 | r = link_handle_bound_by_list(link); | |
1416 | if (r < 0) | |
1417 | return r; | |
1418 | ||
c4a03a56 | 1419 | if (!link->network) { |
0894cfe4 | 1420 | r = wifi_get_info(link); |
8d968fdd YW |
1421 | if (r < 0) |
1422 | return r; | |
1423 | ||
170e88c8 | 1424 | r = link_get_network(link, &network); |
c4a03a56 | 1425 | if (r == -ENOENT) { |
29836c16 | 1426 | link_set_state(link, LINK_STATE_UNMANAGED); |
4232cf04 | 1427 | return 0; |
170e88c8 YW |
1428 | } |
1429 | if (r < 0) | |
c4a03a56 | 1430 | return r; |
505f8da7 | 1431 | |
c4a03a56 TG |
1432 | if (link->flags & IFF_LOOPBACK) { |
1433 | if (network->link_local != ADDRESS_FAMILY_NO) | |
1434 | log_link_debug(link, "Ignoring link-local autoconfiguration for loopback link"); | |
78c958f8 | 1435 | |
c4a03a56 TG |
1436 | if (network->dhcp != ADDRESS_FAMILY_NO) |
1437 | log_link_debug(link, "Ignoring DHCP clients for loopback link"); | |
78c958f8 | 1438 | |
c4a03a56 TG |
1439 | if (network->dhcp_server) |
1440 | log_link_debug(link, "Ignoring DHCP server for loopback link"); | |
1441 | } | |
bd2efe92 | 1442 | |
9092113d | 1443 | link->network = network_ref(network); |
bcdcc596 | 1444 | link_update_operstate(link, false); |
9092113d | 1445 | link_dirty(link); |
c4a03a56 | 1446 | } |
505f8da7 | 1447 | |
0d4ad91d AR |
1448 | r = link_new_bound_to_list(link); |
1449 | if (r < 0) | |
1450 | return r; | |
1451 | ||
852a3916 | 1452 | return link_configure(link); |
505f8da7 TG |
1453 | } |
1454 | ||
302a796f | 1455 | static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
4ff296b0 YW |
1456 | int r; |
1457 | ||
5a1860f7 YW |
1458 | r = link_getlink_handler_internal(rtnl, m, link, "Failed to wait for the interface to be initialized"); |
1459 | if (r <= 0) | |
0b54c870 | 1460 | return r; |
572b21d9 | 1461 | |
4ff296b0 YW |
1462 | r = link_initialized_and_synced(link); |
1463 | if (r < 0) | |
1464 | link_enter_failed(link); | |
5a1860f7 YW |
1465 | |
1466 | return 0; | |
e6bf7774 YW |
1467 | } |
1468 | ||
3be9d62a | 1469 | static int link_initialized(Link *link, sd_device *device) { |
4f561e8e | 1470 | assert(link); |
4f561e8e TG |
1471 | assert(device); |
1472 | ||
8434fd5c | 1473 | if (link->state != LINK_STATE_PENDING) |
4f561e8e TG |
1474 | return 0; |
1475 | ||
51517f9e | 1476 | if (link->sd_device) |
679b3605 TG |
1477 | return 0; |
1478 | ||
79008bdd | 1479 | log_link_debug(link, "udev initialized link"); |
bd08ce56 | 1480 | link_set_state(link, LINK_STATE_INITIALIZED); |
4f561e8e | 1481 | |
51517f9e | 1482 | link->sd_device = sd_device_ref(device); |
4f561e8e | 1483 | |
3c9b8860 TG |
1484 | /* udev has initialized the link, but we don't know if we have yet |
1485 | * processed the NEWLINK messages with the latest state. Do a GETLINK, | |
1486 | * when it returns we know that the pending NEWLINKs have already been | |
1487 | * processed and that we are up-to-date */ | |
4f561e8e | 1488 | |
5a1860f7 | 1489 | return link_call_getlink(link, link_initialized_handler); |
cc2d7efc YW |
1490 | } |
1491 | ||
0d411b7f | 1492 | static int link_check_initialized(Link *link) { |
51517f9e | 1493 | _cleanup_(sd_device_unrefp) sd_device *device = NULL; |
5a937ea2 | 1494 | int r; |
505f8da7 | 1495 | |
0d411b7f | 1496 | assert(link); |
505f8da7 | 1497 | |
0d411b7f | 1498 | if (path_is_read_only_fs("/sys") > 0) |
852a3916 | 1499 | /* no udev */ |
0d411b7f | 1500 | return link_initialized_and_synced(link); |
852a3916 YW |
1501 | |
1502 | /* udev should be around */ | |
0ac655a6 | 1503 | r = sd_device_new_from_ifindex(&device, link->ifindex); |
852a3916 YW |
1504 | if (r < 0) { |
1505 | log_link_debug_errno(link, r, "Could not find device, waiting for device initialization: %m"); | |
1506 | return 0; | |
1507 | } | |
1508 | ||
1509 | r = sd_device_get_is_initialized(device); | |
0d411b7f YW |
1510 | if (r < 0) |
1511 | return log_link_warning_errno(link, r, "Could not determine whether the device is initialized: %m"); | |
852a3916 YW |
1512 | if (r == 0) { |
1513 | /* not yet ready */ | |
1514 | log_link_debug(link, "link pending udev initialization..."); | |
1515 | return 0; | |
1516 | } | |
1517 | ||
1518 | r = device_is_renaming(device); | |
0d411b7f YW |
1519 | if (r < 0) |
1520 | return log_link_warning_errno(link, r, "Failed to determine the device is being renamed: %m"); | |
852a3916 YW |
1521 | if (r > 0) { |
1522 | log_link_debug(link, "Interface is being renamed, pending initialization."); | |
1523 | return 0; | |
4f561e8e | 1524 | } |
505f8da7 | 1525 | |
0d411b7f | 1526 | return link_initialized(link, device); |
a748b692 TG |
1527 | } |
1528 | ||
3be9d62a YW |
1529 | int manager_udev_process_link(sd_device_monitor *monitor, sd_device *device, void *userdata) { |
1530 | sd_device_action_t action; | |
1531 | Manager *m = userdata; | |
1532 | Link *link = NULL; | |
1533 | int r, ifindex; | |
1534 | ||
1535 | assert(m); | |
1536 | assert(device); | |
1537 | ||
1538 | r = sd_device_get_action(device, &action); | |
1539 | if (r < 0) { | |
1540 | log_device_debug_errno(device, r, "Failed to get udev action, ignoring device: %m"); | |
1541 | return 0; | |
1542 | } | |
1543 | ||
1544 | /* Ignore the "remove" uevent — let's remove a device only if rtnetlink says so. All other uevents | |
1545 | * are "positive" events in some form, i.e. inform us about a changed or new network interface, that | |
1546 | * still exists — and we are interested in that. */ | |
1547 | if (action == SD_DEVICE_REMOVE) | |
1548 | return 0; | |
1549 | ||
1550 | r = sd_device_get_ifindex(device, &ifindex); | |
1551 | if (r < 0) { | |
1552 | log_device_debug_errno(device, r, "Ignoring udev %s event for device without ifindex or with invalid ifindex: %m", | |
1553 | device_action_to_string(action)); | |
1554 | return 0; | |
1555 | } | |
1556 | ||
1557 | r = device_is_renaming(device); | |
1558 | if (r < 0) { | |
4b9a8c2b | 1559 | log_device_debug_errno(device, r, "Failed to determine the device is renamed or not, ignoring '%s' uevent: %m", |
3be9d62a YW |
1560 | device_action_to_string(action)); |
1561 | return 0; | |
1562 | } | |
1563 | if (r > 0) { | |
1564 | log_device_debug(device, "Interface is under renaming, wait for the interface to be renamed."); | |
1565 | return 0; | |
1566 | } | |
1567 | ||
6eab614d | 1568 | r = link_get_by_index(m, ifindex, &link); |
3be9d62a | 1569 | if (r < 0) { |
4b9a8c2b | 1570 | log_device_debug_errno(device, r, "Failed to get link from ifindex %i, ignoring: %m", ifindex); |
3be9d62a YW |
1571 | return 0; |
1572 | } | |
1573 | ||
4b9a8c2b YW |
1574 | r = link_initialized(link, device); |
1575 | if (r < 0) | |
1576 | link_enter_failed(link); | |
3be9d62a YW |
1577 | |
1578 | return 0; | |
1579 | } | |
1580 | ||
9c0a72f9 TG |
1581 | static int link_carrier_gained(Link *link) { |
1582 | int r; | |
1583 | ||
1584 | assert(link); | |
1585 | ||
aa9117c0 YW |
1586 | r = link_handle_bound_by_list(link); |
1587 | if (r < 0) | |
1588 | return r; | |
1589 | ||
600b7898 YW |
1590 | if (link->iftype == ARPHRD_CAN) |
1591 | /* let's shortcut things for CAN which doesn't need most of what's done below. */ | |
aa9117c0 | 1592 | return 0; |
600b7898 | 1593 | |
b9ea3d2e | 1594 | if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) { |
8566df79 | 1595 | r = link_acquire_dynamic_conf(link); |
f0269653 | 1596 | if (r < 0) |
9c0a72f9 | 1597 | return r; |
6fc25497 | 1598 | |
97108953 | 1599 | r = link_request_static_configs(link); |
6fc25497 SS |
1600 | if (r < 0) |
1601 | return r; | |
9c0a72f9 TG |
1602 | } |
1603 | ||
aa9117c0 | 1604 | return 0; |
9c0a72f9 TG |
1605 | } |
1606 | ||
1607 | static int link_carrier_lost(Link *link) { | |
1608 | int r; | |
1609 | ||
1610 | assert(link); | |
1611 | ||
aa9117c0 YW |
1612 | r = link_handle_bound_by_list(link); |
1613 | if (r < 0) | |
1614 | return r; | |
93b4dab5 | 1615 | |
600b7898 YW |
1616 | if (link->iftype == ARPHRD_CAN) |
1617 | /* let's shortcut things for CAN which doesn't need most of what's done below. */ | |
aa9117c0 YW |
1618 | return 0; |
1619 | ||
1620 | if (link->network && link->network->ignore_carrier_loss) | |
1621 | return 0; | |
600b7898 | 1622 | |
2a99eed0 | 1623 | r = link_stop_engines(link, false); |
9c0a72f9 TG |
1624 | if (r < 0) { |
1625 | link_enter_failed(link); | |
1626 | return r; | |
1627 | } | |
1628 | ||
3104883d SS |
1629 | r = link_drop_config(link); |
1630 | if (r < 0) | |
1631 | return r; | |
1632 | ||
5e3bb5da | 1633 | return link_drop_foreign_config(link); |
0c9ee5d5 YW |
1634 | } |
1635 | ||
0c9ee5d5 YW |
1636 | static int link_admin_state_up(Link *link) { |
1637 | int r; | |
1638 | ||
1639 | assert(link); | |
1640 | ||
1641 | /* This is called every time an interface admin state changes to up; | |
1642 | * specifically, when IFF_UP flag changes from unset to set. */ | |
1643 | ||
1644 | if (!link->network) | |
1645 | return 0; | |
1646 | ||
899b0e5e | 1647 | if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_DOWN) { |
712fd5d2 YW |
1648 | log_link_info(link, "ActivationPolicy is \"always-off\", forcing link down."); |
1649 | return link_request_to_bring_up_or_down(link, /* up = */ false); | |
0c9ee5d5 YW |
1650 | } |
1651 | ||
1652 | /* We set the ipv6 mtu after the device mtu, but the kernel resets | |
1653 | * ipv6 mtu on NETDEV_UP, so we need to reset it. */ | |
1654 | r = link_set_ipv6_mtu(link); | |
1655 | if (r < 0) | |
1656 | log_link_warning_errno(link, r, "Cannot set IPv6 MTU, ignoring: %m"); | |
1657 | ||
1658 | return 0; | |
1659 | } | |
1660 | ||
1661 | static int link_admin_state_down(Link *link) { | |
1662 | assert(link); | |
1663 | ||
1664 | if (!link->network) | |
1665 | return 0; | |
1666 | ||
899b0e5e | 1667 | if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_UP) { |
712fd5d2 YW |
1668 | log_link_info(link, "ActivationPolicy is \"always-on\", forcing link up."); |
1669 | return link_request_to_bring_up_or_down(link, /* up = */ true); | |
0c9ee5d5 YW |
1670 | } |
1671 | ||
1672 | return 0; | |
1673 | } | |
1674 | ||
1675 | bool link_has_carrier(Link *link) { | |
1676 | /* see Documentation/networking/operstates.txt in the kernel sources */ | |
1677 | ||
1678 | if (link->kernel_operstate == IF_OPER_UP) | |
1679 | return true; | |
1680 | ||
1681 | if (link->kernel_operstate == IF_OPER_UNKNOWN) | |
1682 | /* operstate may not be implemented, so fall back to flags */ | |
1683 | if (FLAGS_SET(link->flags, IFF_LOWER_UP | IFF_RUNNING) && | |
1684 | !FLAGS_SET(link->flags, IFF_DORMANT)) | |
1685 | return true; | |
1686 | ||
1687 | return false; | |
1688 | } | |
1689 | ||
1690 | static bool link_is_enslaved(Link *link) { | |
1691 | if (link->flags & IFF_SLAVE) | |
1692 | /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */ | |
1693 | return true; | |
1694 | ||
1695 | if (!link->network) | |
1696 | return false; | |
1697 | ||
1698 | if (link->master_ifindex > 0 && link->network->bridge) | |
1699 | return true; | |
1700 | ||
1701 | /* TODO: add conditions for other netdevs. */ | |
1702 | ||
1703 | return false; | |
1704 | } | |
1705 | ||
1706 | static LinkAddressState address_state_from_scope(uint8_t scope) { | |
1707 | if (scope < RT_SCOPE_SITE) | |
1708 | /* universally accessible addresses found */ | |
1709 | return LINK_ADDRESS_STATE_ROUTABLE; | |
1710 | ||
1711 | if (scope < RT_SCOPE_HOST) | |
1712 | /* only link or site local addresses found */ | |
1713 | return LINK_ADDRESS_STATE_DEGRADED; | |
1714 | ||
1715 | /* no useful addresses found */ | |
1716 | return LINK_ADDRESS_STATE_OFF; | |
1717 | } | |
1718 | ||
1719 | void link_update_operstate(Link *link, bool also_update_master) { | |
1720 | LinkOperationalState operstate; | |
1721 | LinkCarrierState carrier_state; | |
1722 | LinkAddressState ipv4_address_state, ipv6_address_state, address_state; | |
1723 | LinkOnlineState online_state; | |
1724 | _cleanup_strv_free_ char **p = NULL; | |
1725 | uint8_t ipv4_scope = RT_SCOPE_NOWHERE, ipv6_scope = RT_SCOPE_NOWHERE; | |
1726 | bool changed = false; | |
1727 | Address *address; | |
1728 | ||
1729 | assert(link); | |
1730 | ||
1731 | if (link->kernel_operstate == IF_OPER_DORMANT) | |
1732 | carrier_state = LINK_CARRIER_STATE_DORMANT; | |
1733 | else if (link_has_carrier(link)) { | |
1734 | if (link_is_enslaved(link)) | |
1735 | carrier_state = LINK_CARRIER_STATE_ENSLAVED; | |
1736 | else | |
1737 | carrier_state = LINK_CARRIER_STATE_CARRIER; | |
1738 | } else if (link->flags & IFF_UP) | |
1739 | carrier_state = LINK_CARRIER_STATE_NO_CARRIER; | |
1740 | else | |
1741 | carrier_state = LINK_CARRIER_STATE_OFF; | |
1742 | ||
1743 | if (carrier_state >= LINK_CARRIER_STATE_CARRIER) { | |
1744 | Link *slave; | |
1745 | ||
1746 | SET_FOREACH(slave, link->slaves) { | |
1747 | link_update_operstate(slave, false); | |
1748 | ||
1749 | if (slave->carrier_state < LINK_CARRIER_STATE_CARRIER) | |
1750 | carrier_state = LINK_CARRIER_STATE_DEGRADED_CARRIER; | |
1751 | } | |
1752 | } | |
1753 | ||
1754 | SET_FOREACH(address, link->addresses) { | |
1755 | if (!address_is_ready(address)) | |
1756 | continue; | |
1757 | ||
1758 | if (address->family == AF_INET) | |
1759 | ipv4_scope = MIN(ipv4_scope, address->scope); | |
1760 | ||
1761 | if (address->family == AF_INET6) | |
1762 | ipv6_scope = MIN(ipv6_scope, address->scope); | |
1763 | } | |
1764 | ||
1765 | /* for operstate we also take foreign addresses into account */ | |
1766 | SET_FOREACH(address, link->addresses_foreign) { | |
1767 | if (!address_is_ready(address)) | |
1768 | continue; | |
1769 | ||
1770 | if (address->family == AF_INET) | |
1771 | ipv4_scope = MIN(ipv4_scope, address->scope); | |
1772 | ||
1773 | if (address->family == AF_INET6) | |
1774 | ipv6_scope = MIN(ipv6_scope, address->scope); | |
1775 | } | |
1776 | ||
1777 | ipv4_address_state = address_state_from_scope(ipv4_scope); | |
1778 | ipv6_address_state = address_state_from_scope(ipv6_scope); | |
1779 | address_state = address_state_from_scope(MIN(ipv4_scope, ipv6_scope)); | |
1780 | ||
1781 | /* Mapping of address and carrier state vs operational state | |
1782 | * carrier state | |
1783 | * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved | |
1784 | * ------------------------------------------------------------------------------ | |
1785 | * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved | |
1786 | * address_state degraded | off | no-carrier | dormant | degraded-carrier | degraded | enslaved | |
1787 | * routable | off | no-carrier | dormant | degraded-carrier | routable | routable | |
1788 | */ | |
1789 | ||
1790 | if (carrier_state < LINK_CARRIER_STATE_CARRIER || address_state == LINK_ADDRESS_STATE_OFF) | |
1791 | operstate = (LinkOperationalState) carrier_state; | |
1792 | else if (address_state == LINK_ADDRESS_STATE_ROUTABLE) | |
1793 | operstate = LINK_OPERSTATE_ROUTABLE; | |
1794 | else if (carrier_state == LINK_CARRIER_STATE_CARRIER) | |
1795 | operstate = LINK_OPERSTATE_DEGRADED; | |
1796 | else | |
1797 | operstate = LINK_OPERSTATE_ENSLAVED; | |
1798 | ||
1799 | /* Only determine online state for managed links with RequiredForOnline=yes */ | |
1800 | if (!link->network || !link->network->required_for_online) | |
1801 | online_state = _LINK_ONLINE_STATE_INVALID; | |
1802 | else if (operstate < link->network->required_operstate_for_online.min || | |
1803 | operstate > link->network->required_operstate_for_online.max) | |
1804 | online_state = LINK_ONLINE_STATE_OFFLINE; | |
1805 | else { | |
1806 | AddressFamily required_family = link->network->required_family_for_online; | |
1807 | bool needs_ipv4 = required_family & ADDRESS_FAMILY_IPV4; | |
1808 | bool needs_ipv6 = required_family & ADDRESS_FAMILY_IPV6; | |
1809 | ||
1810 | /* The operational state is within the range required for online. | |
1811 | * If a particular address family is also required, we might revert | |
1812 | * to offline in the blocks below. */ | |
1813 | online_state = LINK_ONLINE_STATE_ONLINE; | |
1814 | ||
1815 | if (link->network->required_operstate_for_online.min >= LINK_OPERSTATE_DEGRADED) { | |
1816 | if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_DEGRADED) | |
1817 | online_state = LINK_ONLINE_STATE_OFFLINE; | |
1818 | if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_DEGRADED) | |
1819 | online_state = LINK_ONLINE_STATE_OFFLINE; | |
1820 | } | |
1821 | ||
1822 | if (link->network->required_operstate_for_online.min >= LINK_OPERSTATE_ROUTABLE) { | |
1823 | if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_ROUTABLE) | |
1824 | online_state = LINK_ONLINE_STATE_OFFLINE; | |
1825 | if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_ROUTABLE) | |
1826 | online_state = LINK_ONLINE_STATE_OFFLINE; | |
1827 | } | |
1828 | } | |
1829 | ||
1830 | if (link->carrier_state != carrier_state) { | |
1831 | link->carrier_state = carrier_state; | |
1832 | changed = true; | |
1833 | if (strv_extend(&p, "CarrierState") < 0) | |
1834 | log_oom(); | |
1835 | } | |
1836 | ||
1837 | if (link->address_state != address_state) { | |
1838 | link->address_state = address_state; | |
1839 | changed = true; | |
1840 | if (strv_extend(&p, "AddressState") < 0) | |
1841 | log_oom(); | |
1842 | } | |
0d4ad91d | 1843 | |
0c9ee5d5 YW |
1844 | if (link->ipv4_address_state != ipv4_address_state) { |
1845 | link->ipv4_address_state = ipv4_address_state; | |
1846 | changed = true; | |
1847 | if (strv_extend(&p, "IPv4AddressState") < 0) | |
1848 | log_oom(); | |
1849 | } | |
9c0a72f9 | 1850 | |
0c9ee5d5 YW |
1851 | if (link->ipv6_address_state != ipv6_address_state) { |
1852 | link->ipv6_address_state = ipv6_address_state; | |
1853 | changed = true; | |
1854 | if (strv_extend(&p, "IPv6AddressState") < 0) | |
1855 | log_oom(); | |
1856 | } | |
9c0a72f9 | 1857 | |
0c9ee5d5 YW |
1858 | if (link->operstate != operstate) { |
1859 | link->operstate = operstate; | |
1860 | changed = true; | |
1861 | if (strv_extend(&p, "OperationalState") < 0) | |
1862 | log_oom(); | |
1863 | } | |
9c0a72f9 | 1864 | |
0c9ee5d5 YW |
1865 | if (link->online_state != online_state) { |
1866 | link->online_state = online_state; | |
1867 | changed = true; | |
1868 | if (strv_extend(&p, "OnlineState") < 0) | |
1869 | log_oom(); | |
1870 | } | |
9c0a72f9 | 1871 | |
0c9ee5d5 YW |
1872 | if (p) |
1873 | link_send_changed_strv(link, p); | |
1874 | if (changed) | |
1875 | link_dirty(link); | |
9c0a72f9 | 1876 | |
0c9ee5d5 YW |
1877 | if (also_update_master) { |
1878 | Link *master; | |
9c0a72f9 | 1879 | |
0c9ee5d5 YW |
1880 | if (link_get_master(link, &master) >= 0) |
1881 | link_update_operstate(master, true); | |
1882 | } | |
9c0a72f9 TG |
1883 | } |
1884 | ||
0c9ee5d5 YW |
1885 | #define FLAG_STRING(string, flag, old, new) \ |
1886 | (((old ^ new) & flag) \ | |
1887 | ? ((old & flag) ? (" -" string) : (" +" string)) \ | |
1888 | : "") | |
1889 | ||
0d411b7f | 1890 | static int link_update_flags(Link *link, sd_netlink_message *message) { |
710fa1b3 | 1891 | bool link_was_lower_up, link_was_admin_up, had_carrier; |
0c9ee5d5 YW |
1892 | uint8_t operstate; |
1893 | unsigned flags; | |
d236718c DS |
1894 | int r; |
1895 | ||
61135582 | 1896 | assert(link); |
0d411b7f | 1897 | assert(message); |
61135582 | 1898 | |
0d411b7f | 1899 | r = sd_rtnl_message_link_get_flags(message, &flags); |
0c9ee5d5 | 1900 | if (r < 0) |
0d411b7f | 1901 | return log_link_debug_errno(link, r, "rtnl: failed to read link flags: %m"); |
3be9d62a | 1902 | |
0d411b7f YW |
1903 | r = sd_netlink_message_read_u8(message, IFLA_OPERSTATE, &operstate); |
1904 | if (r == -ENODATA) | |
1905 | /* If we got a message without operstate, assume the state was unchanged. */ | |
0c9ee5d5 | 1906 | operstate = link->kernel_operstate; |
0d411b7f YW |
1907 | else if (r < 0) |
1908 | return log_link_debug_errno(link, r, "rtnl: failed to read operational state: %m"); | |
0c9ee5d5 | 1909 | |
0d411b7f | 1910 | if (link->flags == flags && link->kernel_operstate == operstate) |
61135582 DS |
1911 | return 0; |
1912 | ||
0c9ee5d5 YW |
1913 | if (link->flags != flags) { |
1914 | unsigned unknown_flags, unknown_flags_added, unknown_flags_removed; | |
61135582 | 1915 | |
0c9ee5d5 YW |
1916 | 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", |
1917 | FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags), | |
1918 | FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags), | |
1919 | FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags), | |
1920 | FLAG_STRING("UP", IFF_UP, link->flags, flags), | |
1921 | FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags), | |
1922 | FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags), | |
1923 | FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags), | |
1924 | FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags), | |
1925 | FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags), | |
1926 | FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags), | |
1927 | FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags), | |
1928 | FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags), | |
1929 | FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags), | |
1930 | FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags), | |
1931 | FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags), | |
1932 | FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags), | |
1933 | FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags), | |
1934 | FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags), | |
1935 | FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags)); | |
d236718c | 1936 | |
0c9ee5d5 YW |
1937 | unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP | |
1938 | IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING | | |
1939 | IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT | | |
1940 | IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL | | |
1941 | IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP | | |
1942 | IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO); | |
1943 | unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags); | |
1944 | unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags); | |
d236718c | 1945 | |
0c9ee5d5 YW |
1946 | if (unknown_flags_added) |
1947 | log_link_debug(link, "Unknown link flags gained, ignoring: %#.5x", unknown_flags_added); | |
61135582 | 1948 | |
0c9ee5d5 YW |
1949 | if (unknown_flags_removed) |
1950 | log_link_debug(link, "Unknown link flags lost, ignoring: %#.5x", unknown_flags_removed); | |
1951 | } | |
61135582 | 1952 | |
710fa1b3 | 1953 | link_was_lower_up = link->flags & IFF_LOWER_UP; |
0d411b7f YW |
1954 | link_was_admin_up = link->flags & IFF_UP; |
1955 | had_carrier = link_has_carrier(link); | |
1956 | ||
0c9ee5d5 YW |
1957 | link->flags = flags; |
1958 | link->kernel_operstate = operstate; | |
36161cba | 1959 | |
0c9ee5d5 | 1960 | link_update_operstate(link, true); |
61135582 | 1961 | |
710fa1b3 YW |
1962 | if (!link_was_lower_up && (link->flags & IFF_LOWER_UP)) { |
1963 | r = wifi_get_info(link); | |
1964 | if (r < 0) | |
1965 | return r; | |
1966 | if (r > 0) { | |
1967 | /* All link information is up-to-date. So, it is not necessary to call | |
1968 | * RTM_GETLINK netlink method again. */ | |
1969 | r = link_reconfigure_impl(link, /* force = */ false); | |
1970 | if (r < 0) | |
1971 | return r; | |
1972 | } | |
1973 | } | |
1974 | ||
0d411b7f YW |
1975 | if (!link_was_admin_up && (link->flags & IFF_UP)) { |
1976 | log_link_info(link, "Link UP"); | |
22936833 | 1977 | |
0d411b7f YW |
1978 | r = link_admin_state_up(link); |
1979 | if (r < 0) | |
1980 | return r; | |
1981 | } else if (link_was_admin_up && !(link->flags & IFF_UP)) { | |
1982 | log_link_info(link, "Link DOWN"); | |
0d4ad91d | 1983 | |
0d411b7f | 1984 | r = link_admin_state_down(link); |
0d4ad91d AR |
1985 | if (r < 0) |
1986 | return r; | |
7619683b TG |
1987 | } |
1988 | ||
0d411b7f YW |
1989 | if (!had_carrier && link_has_carrier(link)) { |
1990 | log_link_info(link, "Gained carrier"); | |
0d4ad91d | 1991 | |
0d411b7f | 1992 | r = link_carrier_gained(link); |
30de2b89 YW |
1993 | if (r < 0) |
1994 | return r; | |
0d411b7f YW |
1995 | } else if (had_carrier && !link_has_carrier(link)) { |
1996 | log_link_info(link, "Lost carrier"); | |
1997 | ||
1998 | r = link_carrier_lost(link); | |
0b54c870 YW |
1999 | if (r < 0) |
2000 | return r; | |
b8941f74 TG |
2001 | } |
2002 | ||
0d411b7f YW |
2003 | return 0; |
2004 | } | |
afe7fd56 | 2005 | |
0d411b7f YW |
2006 | static int link_update_master(Link *link, sd_netlink_message *message) { |
2007 | int master_ifindex, r; | |
7465dd22 | 2008 | |
0d411b7f YW |
2009 | assert(link); |
2010 | assert(message); | |
69629de9 | 2011 | |
0d411b7f YW |
2012 | r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t*) &master_ifindex); |
2013 | if (r == -ENODATA) | |
2014 | return 0; | |
2015 | if (r < 0) | |
2016 | return log_link_debug_errno(link, r, "rtnl: failed to read master ifindex: %m"); | |
807667f7 | 2017 | |
571bf1aa YW |
2018 | if (master_ifindex == link->ifindex) |
2019 | master_ifindex = 0; | |
2020 | ||
0d411b7f YW |
2021 | if (master_ifindex == link->master_ifindex) |
2022 | return 0; | |
807667f7 | 2023 | |
0d411b7f YW |
2024 | if (link->master_ifindex == 0) |
2025 | log_link_debug(link, "Joined to master interface: %i", master_ifindex); | |
2026 | else if (master_ifindex == 0) | |
2027 | log_link_debug(link, "Leaved from master interface: %i", link->master_ifindex); | |
2028 | else | |
2029 | log_link_debug(link, "Master interface is changed: %i → %i", link->master_ifindex, master_ifindex); | |
807667f7 | 2030 | |
0d411b7f | 2031 | link_drop_from_master(link); |
807667f7 | 2032 | |
0d411b7f | 2033 | link->master_ifindex = master_ifindex; |
413708d1 | 2034 | |
0d411b7f YW |
2035 | r = link_append_to_master(link); |
2036 | if (r < 0) | |
2037 | return log_link_debug_errno(link, r, "Failed to append link to master: %m"); | |
7465dd22 | 2038 | |
0d411b7f YW |
2039 | return 0; |
2040 | } | |
807667f7 | 2041 | |
0d411b7f | 2042 | static int link_update_hardware_address(Link *link, sd_netlink_message *message) { |
e2bacccd | 2043 | struct hw_addr_data addr; |
0d411b7f | 2044 | int r; |
d93d655c | 2045 | |
0d411b7f YW |
2046 | assert(link); |
2047 | assert(message); | |
dfc58b47 | 2048 | |
0d411b7f YW |
2049 | r = netlink_message_read_hw_addr(message, IFLA_BROADCAST, &link->bcast_addr); |
2050 | if (r < 0 && r != -ENODATA) | |
2051 | return log_link_debug_errno(link, r, "rtnl: failed to read broadcast address: %m"); | |
4f882b2a | 2052 | |
e2bacccd | 2053 | r = netlink_message_read_hw_addr(message, IFLA_ADDRESS, &addr); |
0d411b7f YW |
2054 | if (r == -ENODATA) |
2055 | return 0; | |
2056 | if (r < 0) | |
fe321d45 | 2057 | return log_link_debug_errno(link, r, "rtnl: failed to read hardware address: %m"); |
bb262ef0 | 2058 | |
e2bacccd | 2059 | if (hw_addr_equal(&link->hw_addr, &addr)) |
0d411b7f | 2060 | return 0; |
a61bb41c | 2061 | |
e2bacccd YW |
2062 | if (hw_addr_is_null(&link->hw_addr)) |
2063 | log_link_debug(link, "Saved hardware address: %s", HW_ADDR_TO_STR(&addr)); | |
fe321d45 YW |
2064 | else { |
2065 | log_link_debug(link, "Hardware address is changed: %s → %s", | |
e2bacccd | 2066 | HW_ADDR_TO_STR(&link->hw_addr), HW_ADDR_TO_STR(&addr)); |
0d411b7f | 2067 | |
e2bacccd YW |
2068 | if (hashmap_get(link->manager->links_by_hw_addr, &link->hw_addr) == link) |
2069 | hashmap_remove(link->manager->links_by_hw_addr, &link->hw_addr); | |
fe321d45 YW |
2070 | } |
2071 | ||
e2bacccd YW |
2072 | link->hw_addr = addr; |
2073 | ||
fe321d45 YW |
2074 | if (!hw_addr_is_null(&link->hw_addr)) { |
2075 | r = hashmap_ensure_put(&link->manager->links_by_hw_addr, &hw_addr_hash_ops, &link->hw_addr, link); | |
2076 | if (r == -EEXIST && streq_ptr(link->kind, "bond")) | |
2077 | /* bonding master and its slaves have the same hardware address. */ | |
2078 | r = hashmap_replace(link->manager->links_by_hw_addr, &link->hw_addr, link); | |
2079 | if (r < 0) | |
2080 | log_link_debug_errno(link, r, "Failed to manage link by its new hardware address, ignoring: %m"); | |
2081 | } | |
0d411b7f | 2082 | |
76a86ffd YW |
2083 | r = ipv4ll_update_mac(link); |
2084 | if (r < 0) | |
2085 | return log_link_debug_errno(link, r, "Could not update MAC address in IPv4 ACD client: %m"); | |
2086 | ||
0d411b7f | 2087 | r = ipv4ll_update_mac(link); |
a61bb41c | 2088 | if (r < 0) |
0d411b7f | 2089 | return log_link_debug_errno(link, r, "Could not update MAC address in IPv4LL client: %m"); |
a61bb41c | 2090 | |
0d411b7f YW |
2091 | r = dhcp4_update_mac(link); |
2092 | if (r < 0) | |
2093 | return log_link_debug_errno(link, r, "Could not update MAC address in DHCP client: %m"); | |
d236718c | 2094 | |
0d411b7f YW |
2095 | r = dhcp6_update_mac(link); |
2096 | if (r < 0) | |
2097 | return log_link_debug_errno(link, r, "Could not update MAC address in DHCPv6 client: %m"); | |
2098 | ||
2099 | r = radv_update_mac(link); | |
2100 | if (r < 0) | |
2101 | return log_link_debug_errno(link, r, "Could not update MAC address for Router Advertisement: %m"); | |
2102 | ||
2103 | if (link->ndisc) { | |
ca2b7cd8 | 2104 | r = sd_ndisc_set_mac(link->ndisc, &link->hw_addr.ether); |
d236718c | 2105 | if (r < 0) |
0d411b7f YW |
2106 | return log_link_debug_errno(link, r, "Could not update MAC for NDisc: %m"); |
2107 | } | |
d236718c | 2108 | |
0d411b7f | 2109 | if (link->lldp) { |
ca2b7cd8 | 2110 | r = sd_lldp_set_filter_address(link->lldp, &link->hw_addr.ether); |
61135582 | 2111 | if (r < 0) |
0d411b7f | 2112 | return log_link_debug_errno(link, r, "Could not update MAC address for LLDP: %m"); |
61135582 DS |
2113 | } |
2114 | ||
0d411b7f YW |
2115 | return 0; |
2116 | } | |
a61bb41c | 2117 | |
0d411b7f | 2118 | static int link_update_mtu(Link *link, sd_netlink_message *message) { |
717ba5fc | 2119 | uint32_t mtu, min_mtu = 0, max_mtu = UINT32_MAX; |
0d411b7f | 2120 | int r; |
a61bb41c | 2121 | |
0d411b7f YW |
2122 | assert(link); |
2123 | assert(message); | |
2124 | ||
2125 | r = sd_netlink_message_read_u32(message, IFLA_MTU, &mtu); | |
2126 | if (r == -ENODATA) | |
2127 | return 0; | |
2128 | if (r < 0) | |
2129 | return log_link_debug_errno(link, r, "rtnl: failed to read MTU in RTM_NEWLINK message: %m"); | |
b5d0fd1e YW |
2130 | if (mtu == 0) |
2131 | return 0; | |
0d411b7f | 2132 | |
717ba5fc YW |
2133 | r = sd_netlink_message_read_u32(message, IFLA_MIN_MTU, &min_mtu); |
2134 | if (r < 0 && r != -ENODATA) | |
2135 | return log_link_debug_errno(link, r, "rtnl: failed to read minimum MTU in RTM_NEWLINK message: %m"); | |
2136 | ||
2137 | r = sd_netlink_message_read_u32(message, IFLA_MAX_MTU, &max_mtu); | |
2138 | if (r < 0 && r != -ENODATA) | |
2139 | return log_link_debug_errno(link, r, "rtnl: failed to read maximum MTU in RTM_NEWLINK message: %m"); | |
2140 | ||
717ba5fc YW |
2141 | if (max_mtu == 0) |
2142 | max_mtu = UINT32_MAX; | |
2143 | ||
2144 | link->min_mtu = min_mtu; | |
2145 | link->max_mtu = max_mtu; | |
2146 | ||
0d411b7f YW |
2147 | if (link->original_mtu == 0) { |
2148 | link->original_mtu = mtu; | |
717ba5fc YW |
2149 | log_link_debug(link, "Saved original MTU %" PRIu32" (min: %"PRIu32", max: %"PRIu32")", |
2150 | link->original_mtu, link->min_mtu, link->max_mtu); | |
0d411b7f YW |
2151 | } |
2152 | ||
717ba5fc YW |
2153 | if (link->mtu == mtu) |
2154 | return 0; | |
2155 | ||
b5d0fd1e YW |
2156 | if (link->mtu != 0) |
2157 | log_link_debug(link, "MTU is changed: %"PRIu32" → %"PRIu32" (min: %"PRIu32", max: %"PRIu32")", | |
2158 | link->mtu, mtu, link->min_mtu, link->max_mtu); | |
0d411b7f YW |
2159 | |
2160 | link->mtu = mtu; | |
2161 | ||
2162 | if (link->dhcp_client) { | |
2163 | r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu); | |
9c0a72f9 | 2164 | if (r < 0) |
0d411b7f YW |
2165 | return log_link_debug_errno(link, r, "Could not update MTU in DHCP client: %m"); |
2166 | } | |
a61bb41c | 2167 | |
0d411b7f YW |
2168 | if (link->radv) { |
2169 | r = sd_radv_set_mtu(link->radv, link->mtu); | |
9c0a72f9 | 2170 | if (r < 0) |
0d411b7f | 2171 | return log_link_debug_errno(link, r, "Could not set MTU for Router Advertisement: %m"); |
a61bb41c TG |
2172 | } |
2173 | ||
2174 | return 0; | |
dd3efc09 | 2175 | } |
fe8db0c5 | 2176 | |
0d411b7f YW |
2177 | static int link_update_alternative_names(Link *link, sd_netlink_message *message) { |
2178 | _cleanup_strv_free_ char **altnames = NULL; | |
2179 | char **n; | |
2180 | int r; | |
2181 | ||
2182 | assert(link); | |
2183 | assert(message); | |
2184 | ||
2185 | r = sd_netlink_message_read_strv(message, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &altnames); | |
2186 | if (r < 0 && r != -ENODATA) | |
2187 | return log_link_debug_errno(link, r, "rtnl: failed to read alternative names: %m"); | |
2188 | ||
2189 | STRV_FOREACH(n, link->alternative_names) | |
2190 | hashmap_remove(link->manager->links_by_name, *n); | |
2191 | ||
2192 | strv_free_and_replace(link->alternative_names, altnames); | |
2193 | ||
2194 | STRV_FOREACH(n, link->alternative_names) { | |
2195 | r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, *n, link); | |
2196 | if (r < 0) | |
2197 | return log_link_debug_errno(link, r, "Failed to manage link by its new alternative names: %m"); | |
2198 | } | |
2199 | ||
2200 | return 0; | |
2201 | } | |
2202 | ||
2203 | static int link_update_name(Link *link, sd_netlink_message *message) { | |
176b8be1 | 2204 | char ifname_from_index[IF_NAMESIZE + 1]; |
0d411b7f YW |
2205 | const char *ifname; |
2206 | int r; | |
2207 | ||
2208 | assert(link); | |
2209 | assert(message); | |
2210 | ||
2211 | r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname); | |
2212 | if (r == -ENODATA) | |
7802194a | 2213 | /* Hmm?? But ok. */ |
0d411b7f YW |
2214 | return 0; |
2215 | if (r < 0) | |
2216 | return log_link_debug_errno(link, r, "Failed to read interface name in RTM_NEWLINK message: %m"); | |
2217 | ||
2218 | if (streq(ifname, link->ifname)) | |
2219 | return 0; | |
2220 | ||
176b8be1 YW |
2221 | if (!format_ifname(link->ifindex, ifname_from_index)) |
2222 | return log_link_debug_errno(link, SYNTHETIC_ERRNO(ENXIO), "Could not get interface name for index %i.", link->ifindex); | |
2223 | ||
2224 | if (!streq(ifname, ifname_from_index)) { | |
2225 | log_link_debug(link, "New interface name '%s' received from the kernel does not correspond " | |
2226 | "with the name currently configured on the actual interface '%s'. Ignoring.", | |
2227 | ifname, ifname_from_index); | |
2228 | return 0; | |
2229 | } | |
2230 | ||
0d411b7f YW |
2231 | log_link_info(link, "Interface name change detected, renamed to %s.", ifname); |
2232 | ||
2233 | hashmap_remove(link->manager->links_by_name, link->ifname); | |
2234 | ||
2235 | r = free_and_strdup(&link->ifname, ifname); | |
2236 | if (r < 0) | |
2237 | return log_oom_debug(); | |
2238 | ||
2239 | r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, link->ifname, link); | |
2240 | if (r < 0) | |
2241 | return log_link_debug_errno(link, r, "Failed to manage link by its new name: %m"); | |
2242 | ||
54d1fdb2 YW |
2243 | if (link->dhcp_client) { |
2244 | r = sd_dhcp_client_set_ifname(link->dhcp_client, link->ifname); | |
2245 | if (r < 0) | |
2246 | return log_link_debug_errno(link, r, "Failed to update interface name in DHCP client: %m"); | |
2247 | } | |
2248 | ||
2249 | if (link->dhcp6_client) { | |
2250 | r = sd_dhcp6_client_set_ifname(link->dhcp6_client, link->ifname); | |
2251 | if (r < 0) | |
2252 | return log_link_debug_errno(link, r, "Failed to update interface name in DHCP6 client: %m"); | |
2253 | } | |
2254 | ||
2255 | if (link->ndisc) { | |
2256 | r = sd_ndisc_set_ifname(link->ndisc, link->ifname); | |
2257 | if (r < 0) | |
2258 | return log_link_debug_errno(link, r, "Failed to update interface name in NDisc: %m"); | |
2259 | } | |
2260 | ||
2261 | if (link->dhcp_server) { | |
2262 | r = sd_dhcp_server_set_ifname(link->dhcp_server, link->ifname); | |
2263 | if (r < 0) | |
2264 | return log_link_debug_errno(link, r, "Failed to update interface name in DHCP server: %m"); | |
2265 | } | |
2266 | ||
2267 | if (link->radv) { | |
2268 | r = sd_radv_set_ifname(link->radv, link->ifname); | |
2269 | if (r < 0) | |
2270 | return log_link_debug_errno(link, r, "Failed to update interface name in Router Advertisement: %m"); | |
2271 | } | |
2272 | ||
2273 | if (link->lldp) { | |
2274 | r = sd_lldp_set_ifname(link->lldp, link->ifname); | |
2275 | if (r < 0) | |
2276 | return log_link_debug_errno(link, r, "Failed to update interface name in LLDP: %m"); | |
2277 | } | |
2278 | ||
2279 | if (link->ipv4ll) { | |
2280 | r = sd_ipv4ll_set_ifname(link->ipv4ll, link->ifname); | |
2281 | if (r < 0) | |
2282 | return log_link_debug_errno(link, r, "Failed to update interface name in IPv4LL client: %m"); | |
2283 | } | |
2284 | ||
2285 | Address *a; | |
2286 | SET_FOREACH(a, link->addresses_ipv4acd) { | |
2287 | r = sd_ipv4acd_set_ifname(a->acd, link->ifname); | |
2288 | if (r < 0) | |
2289 | return log_link_debug_errno(link, r, "Failed to update interface name in IPv4ACD client: %m"); | |
2290 | } | |
2291 | ||
0d411b7f YW |
2292 | return 0; |
2293 | } | |
2294 | ||
2295 | static int link_update(Link *link, sd_netlink_message *message) { | |
2296 | int r; | |
2297 | ||
2298 | assert(link); | |
2299 | assert(message); | |
2300 | ||
2301 | r = link_update_name(link, message); | |
2302 | if (r < 0) | |
2303 | return r; | |
2304 | ||
2305 | r = link_update_alternative_names(link, message); | |
2306 | if (r < 0) | |
2307 | return r; | |
2308 | ||
2309 | r = link_update_mtu(link, message); | |
2310 | if (r < 0) | |
2311 | return r; | |
2312 | ||
2313 | r = link_update_hardware_address(link, message); | |
2314 | if (r < 0) | |
2315 | return r; | |
2316 | ||
2317 | r = link_update_master(link, message); | |
2318 | if (r < 0) | |
2319 | return r; | |
2320 | ||
2321 | return link_update_flags(link, message); | |
2322 | } | |
2323 | ||
0c9ee5d5 YW |
2324 | static Link *link_drop_or_unref(Link *link) { |
2325 | if (!link) | |
2326 | return NULL; | |
2327 | if (!link->manager) | |
2328 | return link_unref(link); | |
2329 | return link_drop(link); | |
2330 | } | |
2331 | ||
2332 | DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_drop_or_unref); | |
2333 | ||
2334 | static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) { | |
0d411b7f | 2335 | _cleanup_free_ char *ifname = NULL, *kind = NULL, *state_file = NULL, *lease_file = NULL, *lldp_file = NULL; |
0c9ee5d5 | 2336 | _cleanup_(link_drop_or_unrefp) Link *link = NULL; |
0c9ee5d5 YW |
2337 | unsigned short iftype; |
2338 | int r, ifindex; | |
0c9ee5d5 YW |
2339 | |
2340 | assert(manager); | |
2341 | assert(message); | |
2342 | assert(ret); | |
2343 | ||
0c9ee5d5 YW |
2344 | r = sd_rtnl_message_link_get_ifindex(message, &ifindex); |
2345 | if (r < 0) | |
0d411b7f | 2346 | return log_debug_errno(r, "rtnl: failed to read ifindex from link message: %m"); |
0c9ee5d5 | 2347 | else if (ifindex <= 0) |
0d411b7f | 2348 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "rtnl: received link message without valid ifindex."); |
0c9ee5d5 YW |
2349 | |
2350 | r = sd_rtnl_message_link_get_type(message, &iftype); | |
2351 | if (r < 0) | |
0d411b7f | 2352 | return log_debug_errno(r, "rtnl: failed to read interface type from link message: %m"); |
0c9ee5d5 YW |
2353 | |
2354 | r = sd_netlink_message_read_string_strdup(message, IFLA_IFNAME, &ifname); | |
2355 | if (r < 0) | |
0d411b7f | 2356 | return log_debug_errno(r, "rtnl: failed to read interface name from link message: %m"); |
0c9ee5d5 YW |
2357 | |
2358 | /* check for link kind */ | |
2359 | r = sd_netlink_message_enter_container(message, IFLA_LINKINFO); | |
2360 | if (r >= 0) { | |
0d411b7f YW |
2361 | r = sd_netlink_message_read_string_strdup(message, IFLA_INFO_KIND, &kind); |
2362 | if (r < 0 && r != -ENODATA) | |
2363 | return log_debug_errno(r, "rtnl: failed to read interface kind from link message: %m"); | |
0c9ee5d5 YW |
2364 | r = sd_netlink_message_exit_container(message); |
2365 | if (r < 0) | |
0d411b7f | 2366 | return log_debug_errno(r, "rtnl: failed to exit IFLA_LINKINFO container: %m"); |
0c9ee5d5 YW |
2367 | } |
2368 | ||
0d411b7f YW |
2369 | if (asprintf(&state_file, "/run/systemd/netif/links/%d", ifindex) < 0) |
2370 | return log_oom_debug(); | |
2371 | ||
2372 | if (asprintf(&lease_file, "/run/systemd/netif/leases/%d", ifindex) < 0) | |
2373 | return log_oom_debug(); | |
2374 | ||
2375 | if (asprintf(&lldp_file, "/run/systemd/netif/lldp/%d", ifindex) < 0) | |
2376 | return log_oom_debug(); | |
2377 | ||
0c9ee5d5 YW |
2378 | link = new(Link, 1); |
2379 | if (!link) | |
2380 | return -ENOMEM; | |
2381 | ||
2382 | *link = (Link) { | |
2383 | .n_ref = 1, | |
2384 | .state = LINK_STATE_PENDING, | |
2385 | .online_state = _LINK_ONLINE_STATE_INVALID, | |
2386 | .ifindex = ifindex, | |
2387 | .iftype = iftype, | |
2388 | .ifname = TAKE_PTR(ifname), | |
2389 | .kind = TAKE_PTR(kind), | |
2390 | ||
0d411b7f YW |
2391 | .state_file = TAKE_PTR(state_file), |
2392 | .lease_file = TAKE_PTR(lease_file), | |
2393 | .lldp_file = TAKE_PTR(lldp_file), | |
2394 | ||
0c9ee5d5 YW |
2395 | .n_dns = UINT_MAX, |
2396 | .dns_default_route = -1, | |
2397 | .llmnr = _RESOLVE_SUPPORT_INVALID, | |
2398 | .mdns = _RESOLVE_SUPPORT_INVALID, | |
2399 | .dnssec_mode = _DNSSEC_MODE_INVALID, | |
2400 | .dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID, | |
2401 | }; | |
2402 | ||
6eab614d | 2403 | r = hashmap_ensure_put(&manager->links_by_index, NULL, INT_TO_PTR(link->ifindex), link); |
0c9ee5d5 | 2404 | if (r < 0) |
0d411b7f | 2405 | return log_link_debug_errno(link, r, "Failed to store link into manager: %m"); |
0c9ee5d5 YW |
2406 | |
2407 | link->manager = manager; | |
2408 | ||
0d411b7f | 2409 | r = hashmap_ensure_put(&manager->links_by_name, &string_hash_ops, link->ifname, link); |
0c9ee5d5 | 2410 | if (r < 0) |
0d411b7f | 2411 | return log_link_debug_errno(link, r, "Failed to manage link by its interface name: %m"); |
0c9ee5d5 YW |
2412 | |
2413 | r = ethtool_get_permanent_macaddr(&manager->ethtool_fd, link->ifname, &link->permanent_mac); | |
2414 | if (r < 0) | |
2415 | log_link_debug_errno(link, r, "Permanent MAC address not found for new device, continuing without: %m"); | |
2416 | ||
2417 | r = ethtool_get_driver(&manager->ethtool_fd, link->ifname, &link->driver); | |
2418 | if (r < 0) | |
2419 | log_link_debug_errno(link, r, "Failed to get driver, continuing without: %m"); | |
2420 | ||
0d411b7f | 2421 | log_link_debug(link, "Link %d added", link->ifindex); |
0c9ee5d5 | 2422 | *ret = TAKE_PTR(link); |
0c9ee5d5 YW |
2423 | return 0; |
2424 | } | |
2425 | ||
0d411b7f | 2426 | int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, Manager *manager) { |
3be9d62a YW |
2427 | Link *link = NULL; |
2428 | NetDev *netdev = NULL; | |
2429 | uint16_t type; | |
2430 | const char *name; | |
2431 | int r, ifindex; | |
2432 | ||
2433 | assert(rtnl); | |
2434 | assert(message); | |
0d411b7f | 2435 | assert(manager); |
3be9d62a YW |
2436 | |
2437 | if (sd_netlink_message_is_error(message)) { | |
2438 | r = sd_netlink_message_get_errno(message); | |
2439 | if (r < 0) | |
2440 | log_message_warning_errno(message, r, "rtnl: Could not receive link message, ignoring"); | |
2441 | ||
2442 | return 0; | |
2443 | } | |
2444 | ||
2445 | r = sd_netlink_message_get_type(message, &type); | |
2446 | if (r < 0) { | |
2447 | log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m"); | |
2448 | return 0; | |
2449 | } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) { | |
2450 | log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type); | |
2451 | return 0; | |
2452 | } | |
2453 | ||
2454 | r = sd_rtnl_message_link_get_ifindex(message, &ifindex); | |
2455 | if (r < 0) { | |
2456 | log_warning_errno(r, "rtnl: Could not get ifindex from link message, ignoring: %m"); | |
2457 | return 0; | |
2458 | } else if (ifindex <= 0) { | |
2459 | log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex); | |
2460 | return 0; | |
2461 | } | |
2462 | ||
2463 | r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name); | |
2464 | if (r < 0) { | |
2465 | log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m"); | |
2466 | return 0; | |
2467 | } | |
2468 | ||
6eab614d | 2469 | (void) link_get_by_index(manager, ifindex, &link); |
0d411b7f | 2470 | (void) netdev_get(manager, name, &netdev); |
3be9d62a YW |
2471 | |
2472 | switch (type) { | |
2473 | case RTM_NEWLINK: | |
0d411b7f YW |
2474 | if (netdev) { |
2475 | /* netdev exists, so make sure the ifindex matches */ | |
2476 | r = netdev_set_ifindex(netdev, message); | |
2477 | if (r < 0) { | |
2478 | log_warning_errno(r, "Could not process new link message for netdev, ignoring: %m"); | |
2479 | return 0; | |
2480 | } | |
2481 | } | |
2482 | ||
3be9d62a YW |
2483 | if (!link) { |
2484 | /* link is new, so add it */ | |
0d411b7f | 2485 | r = link_new(manager, message, &link); |
3be9d62a | 2486 | if (r < 0) { |
0d411b7f | 2487 | log_warning_errno(r, "Could not process new link message: %m"); |
3be9d62a YW |
2488 | return 0; |
2489 | } | |
3be9d62a | 2490 | |
0d411b7f | 2491 | r = link_update(link, message); |
3be9d62a | 2492 | if (r < 0) { |
0d411b7f YW |
2493 | log_warning_errno(r, "Could not process link message: %m"); |
2494 | link_enter_failed(link); | |
3be9d62a YW |
2495 | return 0; |
2496 | } | |
3be9d62a | 2497 | |
0d411b7f YW |
2498 | r = link_check_initialized(link); |
2499 | if (r < 0) { | |
2500 | log_warning_errno(r, "Failed to check link is initialized: %m"); | |
2501 | link_enter_failed(link); | |
2502 | return 0; | |
2503 | } | |
2504 | } else { | |
2505 | r = link_update(link, message); | |
2506 | if (r < 0) { | |
2507 | log_warning_errno(r, "Could not process link message: %m"); | |
2508 | link_enter_failed(link); | |
2509 | return 0; | |
2510 | } | |
3be9d62a YW |
2511 | } |
2512 | ||
2513 | break; | |
2514 | ||
2515 | case RTM_DELLINK: | |
2516 | link_drop(link); | |
2517 | netdev_drop(netdev); | |
2518 | ||
2519 | break; | |
2520 | ||
2521 | default: | |
04499a70 | 2522 | assert_not_reached(); |
3be9d62a YW |
2523 | } |
2524 | ||
2525 | return 1; | |
2526 | } | |
2527 | ||
79c6e114 YW |
2528 | int link_getlink_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg) { |
2529 | uint16_t message_type; | |
2530 | int r; | |
2531 | ||
2532 | assert(m); | |
2533 | assert(link); | |
2534 | assert(error_msg); | |
2535 | ||
2536 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
2537 | return 0; | |
2538 | ||
2539 | r = sd_netlink_message_get_errno(m); | |
2540 | if (r < 0) { | |
2541 | log_link_message_warning_errno(link, m, r, error_msg); | |
2542 | link_enter_failed(link); | |
2543 | return 0; | |
2544 | } | |
2545 | ||
2546 | r = sd_netlink_message_get_type(m, &message_type); | |
2547 | if (r < 0) { | |
2548 | log_link_debug_errno(link, r, "rtnl: failed to read link message type, ignoring: %m"); | |
2549 | return 0; | |
2550 | } | |
2551 | if (message_type != RTM_NEWLINK) { | |
2552 | log_link_debug(link, "rtnl: received invalid link message type, ignoring."); | |
2553 | return 0; | |
2554 | } | |
2555 | ||
2556 | r = link_update(link, m); | |
2557 | if (r < 0) { | |
2558 | link_enter_failed(link); | |
2559 | return 0; | |
2560 | } | |
2561 | ||
2562 | return 1; | |
2563 | } | |
2564 | ||
2565 | int link_call_getlink(Link *link, link_netlink_message_handler_t callback) { | |
2566 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
2567 | int r; | |
2568 | ||
2569 | assert(link); | |
2570 | assert(link->manager); | |
2571 | assert(link->manager->rtnl); | |
2572 | assert(callback); | |
2573 | ||
2574 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex); | |
2575 | if (r < 0) | |
2576 | return r; | |
2577 | ||
2578 | r = netlink_call_async(link->manager->rtnl, NULL, req, callback, | |
2579 | link_netlink_destroy_callback, link); | |
2580 | if (r < 0) | |
2581 | return r; | |
2582 | ||
2583 | link_ref(link); | |
2584 | return 0; | |
2585 | } | |
2586 | ||
fe8db0c5 | 2587 | static const char* const link_state_table[_LINK_STATE_MAX] = { |
8434fd5c | 2588 | [LINK_STATE_PENDING] = "pending", |
bd08ce56 | 2589 | [LINK_STATE_INITIALIZED] = "initialized", |
289e6774 | 2590 | [LINK_STATE_CONFIGURING] = "configuring", |
fe8db0c5 | 2591 | [LINK_STATE_CONFIGURED] = "configured", |
57bd6899 | 2592 | [LINK_STATE_UNMANAGED] = "unmanaged", |
fe8db0c5 | 2593 | [LINK_STATE_FAILED] = "failed", |
370e9930 | 2594 | [LINK_STATE_LINGER] = "linger", |
fe8db0c5 TG |
2595 | }; |
2596 | ||
2597 | DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState); |