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