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