]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
f579559b | 2 | |
9aa5d8ba | 3 | #include <netinet/in.h> |
f579559b | 4 | #include <linux/if.h> |
8f815e8b | 5 | #include <linux/if_arp.h> |
518cd6b5 | 6 | #include <linux/if_link.h> |
4cc7a82c | 7 | #include <unistd.h> |
f579559b | 8 | |
b5efdb8a | 9 | #include "alloc-util.h" |
737f1405 YW |
10 | #include "bond.h" |
11 | #include "bridge.h" | |
1346b1f0 | 12 | #include "bus-util.h" |
27dfc982 | 13 | #include "dhcp-identifier.h" |
bd91b83e | 14 | #include "dhcp-lease-internal.h" |
686d13b9 | 15 | #include "env-file.h" |
4bb7cc82 | 16 | #include "ethtool-util.h" |
3ffd4af2 | 17 | #include "fd-util.h" |
cf1d700d | 18 | #include "fileio.h" |
737f1405 | 19 | #include "ipvlan.h" |
ef118d00 | 20 | #include "missing_network.h" |
cf1d700d | 21 | #include "netlink-util.h" |
c6f7c917 | 22 | #include "network-internal.h" |
3ddcbeea | 23 | #include "networkd-can.h" |
8fcf1d61 | 24 | #include "networkd-dhcp-server.h" |
ca5ad760 YW |
25 | #include "networkd-dhcp4.h" |
26 | #include "networkd-dhcp6.h" | |
27 | #include "networkd-ipv4ll.h" | |
a0e5c15d | 28 | #include "networkd-ipv6-proxy-ndp.h" |
6a1af3d4 YW |
29 | #include "networkd-link-bus.h" |
30 | #include "networkd-link.h" | |
8e1ad1ea | 31 | #include "networkd-lldp-tx.h" |
23f53b99 | 32 | #include "networkd-manager.h" |
1e7a0e21 | 33 | #include "networkd-ndisc.h" |
e4a71bf3 | 34 | #include "networkd-neighbor.h" |
518cd6b5 | 35 | #include "networkd-sriov.h" |
7465dd22 | 36 | #include "networkd-radv.h" |
bce67bbe | 37 | #include "networkd-routing-policy-rule.h" |
8d968fdd | 38 | #include "networkd-wifi.h" |
cf1d700d TG |
39 | #include "set.h" |
40 | #include "socket-util.h" | |
bf331d87 | 41 | #include "stat-util.h" |
15a5e950 | 42 | #include "stdio-util.h" |
8b43440b | 43 | #include "string-table.h" |
51517f9e | 44 | #include "strv.h" |
62e021a9 | 45 | #include "sysctl-util.h" |
34658df2 | 46 | #include "tc.h" |
e4de7287 | 47 | #include "tmpfile-util.h" |
299ad32d | 48 | #include "udev-util.h" |
cf1d700d | 49 | #include "util.h" |
737f1405 | 50 | #include "vrf.h" |
fc2f9534 | 51 | |
bdb9f580 YW |
52 | uint32_t link_get_vrf_table(Link *link) { |
53 | return link->network->vrf ? VRF(link->network->vrf)->table : RT_TABLE_MAIN; | |
54 | } | |
55 | ||
56 | uint32_t link_get_dhcp_route_table(Link *link) { | |
57 | /* When the interface is part of an VRF use the VRFs routing table, unless | |
58 | * another table is explicitly specified. */ | |
59 | if (link->network->dhcp_route_table_set) | |
60 | return link->network->dhcp_route_table; | |
61 | return link_get_vrf_table(link); | |
62 | } | |
63 | ||
64 | uint32_t link_get_ipv6_accept_ra_route_table(Link *link) { | |
65 | if (link->network->ipv6_accept_ra_route_table_set) | |
66 | return link->network->ipv6_accept_ra_route_table; | |
67 | return link_get_vrf_table(link); | |
68 | } | |
69 | ||
f24648a6 YW |
70 | DUID* link_get_duid(Link *link) { |
71 | if (link->network->duid.type != _DUID_TYPE_INVALID) | |
72 | return &link->network->duid; | |
73 | else | |
74 | return &link->manager->duid; | |
75 | } | |
76 | ||
b9d74c40 LP |
77 | static bool link_dhcp6_enabled(Link *link) { |
78 | assert(link); | |
79 | ||
fa709992 LP |
80 | if (!socket_ipv6_is_supported()) |
81 | return false; | |
82 | ||
78c958f8 TG |
83 | if (link->flags & IFF_LOOPBACK) |
84 | return false; | |
85 | ||
86 | if (!link->network) | |
87 | return false; | |
88 | ||
f2bfcdb9 YW |
89 | if (link->network->bond) |
90 | return false; | |
91 | ||
500b96eb | 92 | if (link->iftype == ARPHRD_CAN) |
af9ba57a YW |
93 | return false; |
94 | ||
e0ee46f2 | 95 | return link->network->dhcp & ADDRESS_FAMILY_IPV6; |
78c958f8 TG |
96 | } |
97 | ||
b9d74c40 LP |
98 | static bool link_dhcp4_enabled(Link *link) { |
99 | assert(link); | |
100 | ||
78c958f8 TG |
101 | if (link->flags & IFF_LOOPBACK) |
102 | return false; | |
103 | ||
104 | if (!link->network) | |
105 | return false; | |
106 | ||
f2bfcdb9 YW |
107 | if (link->network->bond) |
108 | return false; | |
109 | ||
500b96eb | 110 | if (link->iftype == ARPHRD_CAN) |
af9ba57a YW |
111 | return false; |
112 | ||
e0ee46f2 | 113 | return link->network->dhcp & ADDRESS_FAMILY_IPV4; |
78c958f8 TG |
114 | } |
115 | ||
b9d74c40 LP |
116 | static bool link_dhcp4_server_enabled(Link *link) { |
117 | assert(link); | |
118 | ||
78c958f8 TG |
119 | if (link->flags & IFF_LOOPBACK) |
120 | return false; | |
121 | ||
122 | if (!link->network) | |
123 | return false; | |
124 | ||
f2bfcdb9 YW |
125 | if (link->network->bond) |
126 | return false; | |
127 | ||
500b96eb | 128 | if (link->iftype == ARPHRD_CAN) |
af9ba57a YW |
129 | return false; |
130 | ||
78c958f8 TG |
131 | return link->network->dhcp_server; |
132 | } | |
133 | ||
2d792895 | 134 | bool link_ipv4ll_enabled(Link *link, AddressFamily mask) { |
b9d74c40 | 135 | assert(link); |
910feb78 | 136 | assert((mask & ~(ADDRESS_FAMILY_IPV4 | ADDRESS_FAMILY_FALLBACK_IPV4)) == 0); |
b9d74c40 | 137 | |
78c958f8 TG |
138 | if (link->flags & IFF_LOOPBACK) |
139 | return false; | |
140 | ||
141 | if (!link->network) | |
142 | return false; | |
143 | ||
500b96eb | 144 | if (link->iftype == ARPHRD_CAN) |
c6ac3729 YW |
145 | return false; |
146 | ||
98d20a17 | 147 | if (STRPTR_IN_SET(link->kind, |
148 | "vrf", "wireguard", "ipip", "gre", "ip6gre","ip6tnl", "sit", "vti", | |
c6ac3729 | 149 | "vti6", "nlmon", "xfrm")) |
a8f5bba6 JD |
150 | return false; |
151 | ||
f410d463 YW |
152 | /* L3 or L3S mode do not support ARP. */ |
153 | if (IN_SET(link_get_ipvlan_mode(link), NETDEV_IPVLAN_MODE_L3, NETDEV_IPVLAN_MODE_L3S)) | |
154 | return false; | |
155 | ||
f2bfcdb9 YW |
156 | if (link->network->bond) |
157 | return false; | |
158 | ||
910feb78 | 159 | return link->network->link_local & mask; |
8bc17bb3 SS |
160 | } |
161 | ||
b9d74c40 LP |
162 | static bool link_ipv6ll_enabled(Link *link) { |
163 | assert(link); | |
164 | ||
fa709992 LP |
165 | if (!socket_ipv6_is_supported()) |
166 | return false; | |
167 | ||
d0d6a4cd TG |
168 | if (link->flags & IFF_LOOPBACK) |
169 | return false; | |
170 | ||
171 | if (!link->network) | |
172 | return false; | |
173 | ||
500b96eb | 174 | if (link->iftype == ARPHRD_CAN) |
c6ac3729 YW |
175 | return false; |
176 | ||
177 | if (STRPTR_IN_SET(link->kind, "vrf", "wireguard", "ipip", "gre", "sit", "vti", "nlmon")) | |
a8f5bba6 JD |
178 | return false; |
179 | ||
f2bfcdb9 YW |
180 | if (link->network->bond) |
181 | return false; | |
182 | ||
e0ee46f2 | 183 | return link->network->link_local & ADDRESS_FAMILY_IPV6; |
78c958f8 TG |
184 | } |
185 | ||
439689c6 SS |
186 | static bool link_ipv6_enabled(Link *link) { |
187 | assert(link); | |
188 | ||
189 | if (!socket_ipv6_is_supported()) | |
190 | return false; | |
191 | ||
b102cdca | 192 | if (link->network->bond) |
2b00a4e0 TY |
193 | return false; |
194 | ||
500b96eb | 195 | if (link->iftype == ARPHRD_CAN) |
af9ba57a YW |
196 | return false; |
197 | ||
4cef7fe3 | 198 | /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */ |
adfeee49 YW |
199 | if (link_ipv6ll_enabled(link)) |
200 | return true; | |
201 | ||
202 | if (network_has_static_ipv6_configurations(link->network)) | |
203 | return true; | |
204 | ||
205 | return false; | |
439689c6 SS |
206 | } |
207 | ||
7465dd22 PF |
208 | static bool link_radv_enabled(Link *link) { |
209 | assert(link); | |
210 | ||
211 | if (!link_ipv6ll_enabled(link)) | |
212 | return false; | |
213 | ||
55a7c78b | 214 | return link->network->router_prefix_delegation != RADV_PREFIX_DELEGATION_NONE; |
7465dd22 PF |
215 | } |
216 | ||
769d324c | 217 | static bool link_ipv4_forward_enabled(Link *link) { |
b9d74c40 LP |
218 | assert(link); |
219 | ||
5a8bcb67 LP |
220 | if (link->flags & IFF_LOOPBACK) |
221 | return false; | |
222 | ||
223 | if (!link->network) | |
224 | return false; | |
225 | ||
2d792895 | 226 | if (link->network->ip_forward == _ADDRESS_FAMILY_INVALID) |
765afd5c LP |
227 | return false; |
228 | ||
e0ee46f2 | 229 | return link->network->ip_forward & ADDRESS_FAMILY_IPV4; |
769d324c LP |
230 | } |
231 | ||
232 | static bool link_ipv6_forward_enabled(Link *link) { | |
b9d74c40 | 233 | assert(link); |
765afd5c LP |
234 | |
235 | if (!socket_ipv6_is_supported()) | |
236 | return false; | |
237 | ||
769d324c LP |
238 | if (link->flags & IFF_LOOPBACK) |
239 | return false; | |
240 | ||
241 | if (!link->network) | |
242 | return false; | |
243 | ||
2d792895 | 244 | if (link->network->ip_forward == _ADDRESS_FAMILY_INVALID) |
765afd5c LP |
245 | return false; |
246 | ||
e0ee46f2 | 247 | return link->network->ip_forward & ADDRESS_FAMILY_IPV6; |
5a8bcb67 LP |
248 | } |
249 | ||
23d8b221 SS |
250 | static bool link_proxy_arp_enabled(Link *link) { |
251 | assert(link); | |
252 | ||
253 | if (link->flags & IFF_LOOPBACK) | |
254 | return false; | |
255 | ||
256 | if (!link->network) | |
257 | return false; | |
258 | ||
259 | if (link->network->proxy_arp < 0) | |
260 | return false; | |
261 | ||
262 | return true; | |
263 | } | |
264 | ||
b9d74c40 LP |
265 | static bool link_ipv6_accept_ra_enabled(Link *link) { |
266 | assert(link); | |
267 | ||
fa709992 LP |
268 | if (!socket_ipv6_is_supported()) |
269 | return false; | |
270 | ||
f5a8c43f TG |
271 | if (link->flags & IFF_LOOPBACK) |
272 | return false; | |
273 | ||
274 | if (!link->network) | |
275 | return false; | |
276 | ||
702c979f SS |
277 | if (!link_ipv6ll_enabled(link)) |
278 | return false; | |
279 | ||
f5a8c43f TG |
280 | /* If unset use system default (enabled if local forwarding is disabled. |
281 | * disabled if local forwarding is enabled). | |
282 | * If set, ignore or enforce RA independent of local forwarding state. | |
283 | */ | |
284 | if (link->network->ipv6_accept_ra < 0) | |
285 | /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */ | |
286 | return !link_ipv6_forward_enabled(link); | |
287 | else if (link->network->ipv6_accept_ra > 0) | |
288 | /* accept RA even if ip_forward is enabled */ | |
289 | return true; | |
290 | else | |
291 | /* ignore RA */ | |
292 | return false; | |
293 | } | |
294 | ||
1f0d9695 | 295 | static IPv6PrivacyExtensions link_ipv6_privacy_extensions(Link *link) { |
fa709992 | 296 | assert(link); |
d68e2e59 LP |
297 | |
298 | if (!socket_ipv6_is_supported()) | |
299 | return _IPV6_PRIVACY_EXTENSIONS_INVALID; | |
300 | ||
49092e22 | 301 | if (link->flags & IFF_LOOPBACK) |
1f0d9695 | 302 | return _IPV6_PRIVACY_EXTENSIONS_INVALID; |
49092e22 SS |
303 | |
304 | if (!link->network) | |
1f0d9695 | 305 | return _IPV6_PRIVACY_EXTENSIONS_INVALID; |
49092e22 SS |
306 | |
307 | return link->network->ipv6_privacy_extensions; | |
308 | } | |
309 | ||
57ad7607 | 310 | static int link_update_ipv6_sysctl(Link *link) { |
482efedc | 311 | bool enabled; |
439689c6 SS |
312 | int r; |
313 | ||
314 | if (link->flags & IFF_LOOPBACK) | |
315 | return 0; | |
316 | ||
482efedc SS |
317 | enabled = link_ipv6_enabled(link); |
318 | if (enabled) { | |
319 | r = sysctl_write_ip_property_boolean(AF_INET6, link->ifname, "disable_ipv6", false); | |
320 | if (r < 0) | |
57ad7607 ZJS |
321 | return log_link_warning_errno(link, r, "Cannot enable IPv6: %m"); |
322 | ||
323 | log_link_info(link, "IPv6 successfully enabled"); | |
482efedc | 324 | } |
439689c6 SS |
325 | |
326 | return 0; | |
327 | } | |
328 | ||
34bf3c00 YW |
329 | static bool link_is_enslaved(Link *link) { |
330 | if (link->flags & IFF_SLAVE) | |
331 | /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */ | |
332 | return true; | |
333 | ||
34bf3c00 YW |
334 | if (!link->network) |
335 | return false; | |
336 | ||
bb262ef0 | 337 | if (link->master_ifindex > 0 && link->network->bridge) |
34bf3c00 YW |
338 | return true; |
339 | ||
bb262ef0 YW |
340 | /* TODO: add conditions for other netdevs. */ |
341 | ||
34bf3c00 YW |
342 | return false; |
343 | } | |
344 | ||
45e11abf YW |
345 | static void link_update_master_operstate(Link *link, NetDev *netdev) { |
346 | Link *master; | |
347 | ||
348 | if (!netdev) | |
349 | return; | |
350 | ||
7936917e YW |
351 | if (netdev->ifindex <= 0) |
352 | return; | |
353 | ||
45e11abf YW |
354 | if (link_get(link->manager, netdev->ifindex, &master) < 0) |
355 | return; | |
356 | ||
357 | link_update_operstate(master, true); | |
358 | } | |
359 | ||
360 | void link_update_operstate(Link *link, bool also_update_master) { | |
84de38c5 | 361 | LinkOperationalState operstate; |
1678fbb3 YW |
362 | LinkCarrierState carrier_state; |
363 | LinkAddressState address_state; | |
35c5a9ca | 364 | _cleanup_strv_free_ char **p = NULL; |
1678fbb3 | 365 | uint8_t scope = RT_SCOPE_NOWHERE; |
35c5a9ca | 366 | bool changed = false; |
1678fbb3 | 367 | Address *address; |
45e11abf | 368 | Iterator i; |
14153d1b | 369 | |
84de38c5 TG |
370 | assert(link); |
371 | ||
372 | if (link->kernel_operstate == IF_OPER_DORMANT) | |
1678fbb3 | 373 | carrier_state = LINK_CARRIER_STATE_DORMANT; |
84de38c5 | 374 | else if (link_has_carrier(link)) { |
1678fbb3 YW |
375 | if (link_is_enslaved(link)) |
376 | carrier_state = LINK_CARRIER_STATE_ENSLAVED; | |
84de38c5 | 377 | else |
1678fbb3 | 378 | carrier_state = LINK_CARRIER_STATE_CARRIER; |
84de38c5 | 379 | } else if (link->flags & IFF_UP) |
1678fbb3 | 380 | carrier_state = LINK_CARRIER_STATE_NO_CARRIER; |
84de38c5 | 381 | else |
1678fbb3 | 382 | carrier_state = LINK_CARRIER_STATE_OFF; |
84de38c5 | 383 | |
1678fbb3 | 384 | if (carrier_state >= LINK_CARRIER_STATE_CARRIER) { |
959f65d3 YW |
385 | Link *slave; |
386 | ||
5f707e12 | 387 | SET_FOREACH(slave, link->slaves, i) { |
959f65d3 | 388 | link_update_operstate(slave, false); |
84de38c5 | 389 | |
1678fbb3 YW |
390 | if (slave->carrier_state < LINK_CARRIER_STATE_CARRIER) |
391 | carrier_state = LINK_CARRIER_STATE_DEGRADED_CARRIER; | |
84de38c5 | 392 | } |
959f65d3 | 393 | } |
84de38c5 | 394 | |
1678fbb3 YW |
395 | SET_FOREACH(address, link->addresses, i) { |
396 | if (!address_is_ready(address)) | |
397 | continue; | |
84de38c5 | 398 | |
1678fbb3 YW |
399 | if (address->scope < scope) |
400 | scope = address->scope; | |
401 | } | |
84de38c5 | 402 | |
1678fbb3 YW |
403 | /* for operstate we also take foreign addresses into account */ |
404 | SET_FOREACH(address, link->addresses_foreign, i) { | |
405 | if (!address_is_ready(address)) | |
406 | continue; | |
407 | ||
408 | if (address->scope < scope) | |
409 | scope = address->scope; | |
410 | } | |
411 | ||
412 | if (scope < RT_SCOPE_SITE) | |
413 | /* universally accessible addresses found */ | |
414 | address_state = LINK_ADDRESS_STATE_ROUTABLE; | |
415 | else if (scope < RT_SCOPE_HOST) | |
416 | /* only link or site local addresses found */ | |
417 | address_state = LINK_ADDRESS_STATE_DEGRADED; | |
84de38c5 | 418 | else |
1678fbb3 YW |
419 | /* no useful addresses found */ |
420 | address_state = LINK_ADDRESS_STATE_OFF; | |
421 | ||
422 | /* Mapping of address and carrier state vs operational state | |
423 | * carrier state | |
424 | * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved | |
425 | * ------------------------------------------------------------------------------ | |
426 | * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved | |
427 | * address_state degraded | off | no-carrier | dormant | degraded-carrier | degraded | enslaved | |
428 | * routable | off | no-carrier | dormant | degraded-carrier | routable | routable | |
429 | */ | |
84de38c5 | 430 | |
1678fbb3 YW |
431 | if (carrier_state < LINK_CARRIER_STATE_CARRIER || address_state == LINK_ADDRESS_STATE_OFF) |
432 | operstate = (LinkOperationalState) carrier_state; | |
433 | else if (address_state == LINK_ADDRESS_STATE_ROUTABLE) | |
434 | operstate = LINK_OPERSTATE_ROUTABLE; | |
435 | else if (carrier_state == LINK_CARRIER_STATE_CARRIER) | |
436 | operstate = LINK_OPERSTATE_DEGRADED; | |
437 | else | |
14153d1b YW |
438 | operstate = LINK_OPERSTATE_ENSLAVED; |
439 | ||
35c5a9ca YW |
440 | if (link->carrier_state != carrier_state) { |
441 | link->carrier_state = carrier_state; | |
442 | changed = true; | |
443 | if (strv_extend(&p, "CarrierState") < 0) | |
444 | log_oom(); | |
445 | } | |
959f65d3 | 446 | |
35c5a9ca YW |
447 | if (link->address_state != address_state) { |
448 | link->address_state = address_state; | |
449 | changed = true; | |
450 | if (strv_extend(&p, "AddressState") < 0) | |
451 | log_oom(); | |
959f65d3 YW |
452 | } |
453 | ||
84de38c5 TG |
454 | if (link->operstate != operstate) { |
455 | link->operstate = operstate; | |
35c5a9ca YW |
456 | changed = true; |
457 | if (strv_extend(&p, "OperationalState") < 0) | |
458 | log_oom(); | |
84de38c5 | 459 | } |
959f65d3 | 460 | |
35c5a9ca YW |
461 | if (p) |
462 | link_send_changed_strv(link, p); | |
463 | if (changed) | |
464 | link_dirty(link); | |
465 | ||
45e11abf YW |
466 | if (also_update_master && link->network) { |
467 | link_update_master_operstate(link, link->network->bond); | |
468 | link_update_master_operstate(link, link->network->bridge); | |
959f65d3 | 469 | } |
84de38c5 TG |
470 | } |
471 | ||
51d18171 TG |
472 | #define FLAG_STRING(string, flag, old, new) \ |
473 | (((old ^ new) & flag) \ | |
474 | ? ((old & flag) ? (" -" string) : (" +" string)) \ | |
475 | : "") | |
476 | ||
bb262ef0 | 477 | static int link_update_flags(Link *link, sd_netlink_message *m, bool force_update_operstate) { |
51d18171 TG |
478 | unsigned flags, unknown_flags_added, unknown_flags_removed, unknown_flags; |
479 | uint8_t operstate; | |
480 | int r; | |
481 | ||
482 | assert(link); | |
483 | ||
484 | r = sd_rtnl_message_link_get_flags(m, &flags); | |
6a7a4e4d LP |
485 | if (r < 0) |
486 | return log_link_warning_errno(link, r, "Could not get link flags: %m"); | |
51d18171 | 487 | |
1c4baffc | 488 | r = sd_netlink_message_read_u8(m, IFLA_OPERSTATE, &operstate); |
51d18171 TG |
489 | if (r < 0) |
490 | /* if we got a message without operstate, take it to mean | |
491 | the state was unchanged */ | |
492 | operstate = link->kernel_operstate; | |
493 | ||
bb262ef0 | 494 | if (!force_update_operstate && (link->flags == flags) && (link->kernel_operstate == operstate)) |
51d18171 TG |
495 | return 0; |
496 | ||
497 | if (link->flags != flags) { | |
6a7a4e4d | 498 | 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", |
51d18171 TG |
499 | FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags), |
500 | FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags), | |
501 | FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags), | |
502 | FLAG_STRING("UP", IFF_UP, link->flags, flags), | |
503 | FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags), | |
504 | FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags), | |
505 | FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags), | |
506 | FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags), | |
507 | FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags), | |
508 | FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags), | |
509 | FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags), | |
510 | FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags), | |
511 | FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags), | |
512 | FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags), | |
513 | FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags), | |
514 | FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags), | |
515 | FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags), | |
516 | FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags), | |
517 | FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags)); | |
518 | ||
519 | unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP | | |
520 | IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING | | |
521 | IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT | | |
522 | IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL | | |
523 | IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP | | |
524 | IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO); | |
525 | unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags); | |
526 | unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags); | |
527 | ||
528 | /* link flags are currently at most 18 bits, let's align to | |
529 | * printing 20 */ | |
530 | if (unknown_flags_added) | |
79008bdd | 531 | log_link_debug(link, |
6a7a4e4d | 532 | "Unknown link flags gained: %#.5x (ignoring)", |
51d18171 TG |
533 | unknown_flags_added); |
534 | ||
535 | if (unknown_flags_removed) | |
79008bdd | 536 | log_link_debug(link, |
6a7a4e4d | 537 | "Unknown link flags lost: %#.5x (ignoring)", |
51d18171 TG |
538 | unknown_flags_removed); |
539 | } | |
540 | ||
541 | link->flags = flags; | |
542 | link->kernel_operstate = operstate; | |
543 | ||
959f65d3 | 544 | link_update_operstate(link, true); |
51d18171 TG |
545 | |
546 | return 0; | |
547 | } | |
548 | ||
1c4baffc | 549 | static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) { |
8e766630 | 550 | _cleanup_(link_unrefp) Link *link = NULL; |
6cad256d | 551 | const char *ifname, *kind = NULL; |
b710e6b6 | 552 | unsigned short iftype; |
572b21d9 YW |
553 | int r, ifindex; |
554 | uint16_t type; | |
f579559b | 555 | |
0c2f9b84 | 556 | assert(manager); |
505f8da7 | 557 | assert(message); |
f579559b TG |
558 | assert(ret); |
559 | ||
6cad256d TJ |
560 | /* check for link kind */ |
561 | r = sd_netlink_message_enter_container(message, IFLA_LINKINFO); | |
562 | if (r == 0) { | |
db2f8a2e | 563 | (void) sd_netlink_message_read_string(message, IFLA_INFO_KIND, &kind); |
6cad256d TJ |
564 | r = sd_netlink_message_exit_container(message); |
565 | if (r < 0) | |
566 | return r; | |
567 | } | |
568 | ||
1c4baffc | 569 | r = sd_netlink_message_get_type(message, &type); |
505f8da7 TG |
570 | if (r < 0) |
571 | return r; | |
572 | else if (type != RTM_NEWLINK) | |
573 | return -EINVAL; | |
574 | ||
575 | r = sd_rtnl_message_link_get_ifindex(message, &ifindex); | |
576 | if (r < 0) | |
577 | return r; | |
578 | else if (ifindex <= 0) | |
579 | return -EINVAL; | |
580 | ||
b710e6b6 LP |
581 | r = sd_rtnl_message_link_get_type(message, &iftype); |
582 | if (r < 0) | |
583 | return r; | |
584 | ||
1c4baffc | 585 | r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname); |
505f8da7 TG |
586 | if (r < 0) |
587 | return r; | |
588 | ||
17f9c355 | 589 | link = new(Link, 1); |
f579559b TG |
590 | if (!link) |
591 | return -ENOMEM; | |
592 | ||
17f9c355 YW |
593 | *link = (Link) { |
594 | .n_ref = 1, | |
595 | .manager = manager, | |
596 | .state = LINK_STATE_PENDING, | |
17f9c355 YW |
597 | .ifindex = ifindex, |
598 | .iftype = iftype, | |
15761549 YW |
599 | |
600 | .n_dns = (unsigned) -1, | |
601 | .dns_default_route = -1, | |
602 | .llmnr = _RESOLVE_SUPPORT_INVALID, | |
603 | .mdns = _RESOLVE_SUPPORT_INVALID, | |
604 | .dnssec_mode = _DNSSEC_MODE_INVALID, | |
605 | .dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID, | |
17f9c355 YW |
606 | }; |
607 | ||
505f8da7 TG |
608 | link->ifname = strdup(ifname); |
609 | if (!link->ifname) | |
610 | return -ENOMEM; | |
f579559b | 611 | |
6cad256d TJ |
612 | if (kind) { |
613 | link->kind = strdup(kind); | |
614 | if (!link->kind) | |
615 | return -ENOMEM; | |
616 | } | |
617 | ||
cbff7170 TJ |
618 | r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t *)&link->master_ifindex); |
619 | if (r < 0) | |
620 | log_link_debug_errno(link, r, "New device has no master, continuing without"); | |
621 | ||
1c4baffc | 622 | r = sd_netlink_message_read_ether_addr(message, IFLA_ADDRESS, &link->mac); |
512922f8 | 623 | if (r < 0) |
34437b4f | 624 | log_link_debug_errno(link, r, "MAC address not found for new device, continuing without"); |
512922f8 | 625 | |
c643bda5 | 626 | r = ethtool_get_permanent_macaddr(&manager->ethtool_fd, link->ifname, &link->permanent_mac); |
4bb7cc82 YW |
627 | if (r < 0) |
628 | log_link_debug_errno(link, r, "Permanent MAC address not found for new device, continuing without: %m"); | |
629 | ||
c643bda5 YW |
630 | r = ethtool_get_driver(&manager->ethtool_fd, link->ifname, &link->driver); |
631 | if (r < 0) | |
632 | log_link_debug_errno(link, r, "Failed to get driver, continuing without: %m"); | |
633 | ||
572b21d9 YW |
634 | r = sd_netlink_message_read_strv(message, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &link->alternative_names); |
635 | if (r < 0 && r != -ENODATA) | |
636 | return r; | |
637 | ||
34437b4f | 638 | if (asprintf(&link->state_file, "/run/systemd/netif/links/%d", link->ifindex) < 0) |
315db1a8 | 639 | return -ENOMEM; |
fe8db0c5 | 640 | |
34437b4f | 641 | if (asprintf(&link->lease_file, "/run/systemd/netif/leases/%d", link->ifindex) < 0) |
68a8723c TG |
642 | return -ENOMEM; |
643 | ||
34437b4f | 644 | if (asprintf(&link->lldp_file, "/run/systemd/netif/lldp/%d", link->ifindex) < 0) |
49699bac SS |
645 | return -ENOMEM; |
646 | ||
d5099efc | 647 | r = hashmap_ensure_allocated(&manager->links, NULL); |
ae06ab10 TG |
648 | if (r < 0) |
649 | return r; | |
650 | ||
651 | r = hashmap_put(manager->links, INT_TO_PTR(link->ifindex), link); | |
f579559b TG |
652 | if (r < 0) |
653 | return r; | |
654 | ||
bb262ef0 | 655 | r = link_update_flags(link, message, false); |
51d18171 TG |
656 | if (r < 0) |
657 | return r; | |
658 | ||
1cc6c93a | 659 | *ret = TAKE_PTR(link); |
f579559b TG |
660 | |
661 | return 0; | |
662 | } | |
663 | ||
15761549 YW |
664 | void link_ntp_settings_clear(Link *link) { |
665 | link->ntp = strv_free(link->ntp); | |
666 | } | |
667 | ||
668 | void link_dns_settings_clear(Link *link) { | |
669 | link->dns = mfree(link->dns); | |
670 | link->n_dns = (unsigned) -1; | |
671 | ||
672 | link->search_domains = ordered_set_free_free(link->search_domains); | |
673 | link->route_domains = ordered_set_free_free(link->route_domains); | |
674 | ||
675 | link->dns_default_route = -1; | |
676 | link->llmnr = _RESOLVE_SUPPORT_INVALID; | |
677 | link->mdns = _RESOLVE_SUPPORT_INVALID; | |
678 | link->dnssec_mode = _DNSSEC_MODE_INVALID; | |
679 | link->dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID; | |
680 | ||
681 | link->dnssec_negative_trust_anchors = set_free_free(link->dnssec_negative_trust_anchors); | |
682 | } | |
683 | ||
1a6bb31f YW |
684 | static void link_free_engines(Link *link) { |
685 | if (!link) | |
686 | return; | |
687 | ||
688 | link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server); | |
689 | link->dhcp_client = sd_dhcp_client_unref(link->dhcp_client); | |
690 | link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease); | |
691 | link->dhcp_routes = set_free(link->dhcp_routes); | |
692 | ||
693 | link->lldp = sd_lldp_unref(link->lldp); | |
694 | ||
695 | ndisc_flush(link); | |
696 | ||
697 | link->ipv4ll = sd_ipv4ll_unref(link->ipv4ll); | |
698 | link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client); | |
699 | link->ndisc = sd_ndisc_unref(link->ndisc); | |
700 | link->radv = sd_radv_unref(link->radv); | |
701 | } | |
702 | ||
8301aa0b | 703 | static Link *link_free(Link *link) { |
428fd0a7 TG |
704 | Address *address; |
705 | ||
8301aa0b | 706 | assert(link); |
f579559b | 707 | |
15761549 YW |
708 | link_ntp_settings_clear(link); |
709 | link_dns_settings_clear(link); | |
710 | ||
f535e354 YW |
711 | link->routes = set_free_with_destructor(link->routes, route_free); |
712 | link->routes_foreign = set_free_with_destructor(link->routes_foreign, route_free); | |
adda1ed9 | 713 | |
c16c7808 SS |
714 | link->nexthops = set_free_with_destructor(link->nexthops, nexthop_free); |
715 | link->nexthops_foreign = set_free_with_destructor(link->nexthops_foreign, nexthop_free); | |
716 | ||
d1bdafd2 WKI |
717 | link->neighbors = set_free_with_destructor(link->neighbors, neighbor_free); |
718 | link->neighbors_foreign = set_free_with_destructor(link->neighbors_foreign, neighbor_free); | |
719 | ||
f535e354 YW |
720 | link->addresses = set_free_with_destructor(link->addresses, address_free); |
721 | link->addresses_foreign = set_free_with_destructor(link->addresses_foreign, address_free); | |
adda1ed9 | 722 | |
11bf3cce LP |
723 | while ((address = link->pool_addresses)) { |
724 | LIST_REMOVE(addresses, link->pool_addresses, address); | |
725 | address_free(address); | |
726 | } | |
727 | ||
7272b25e | 728 | link_lldp_emit_stop(link); |
1a6bb31f | 729 | link_free_engines(link); |
68a8723c | 730 | free(link->lease_file); |
49699bac SS |
731 | free(link->lldp_file); |
732 | ||
c166a070 | 733 | free(link->ifname); |
572b21d9 | 734 | strv_free(link->alternative_names); |
ceac4078 | 735 | free(link->kind); |
8d968fdd | 736 | free(link->ssid); |
c643bda5 | 737 | free(link->driver); |
6cad256d | 738 | |
db2f8a2e | 739 | (void) unlink(link->state_file); |
fe8db0c5 | 740 | free(link->state_file); |
c166a070 | 741 | |
51517f9e | 742 | sd_device_unref(link->sd_device); |
b5db00e5 | 743 | |
0d4ad91d | 744 | hashmap_free(link->bound_to_links); |
0d4ad91d AR |
745 | hashmap_free(link->bound_by_links); |
746 | ||
5f707e12 | 747 | set_free_with_destructor(link->slaves, link_unref); |
033295c1 | 748 | |
c9c908a6 YW |
749 | network_unref(link->network); |
750 | ||
8301aa0b | 751 | return mfree(link); |
14b746f7 TG |
752 | } |
753 | ||
8301aa0b | 754 | DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free); |
14b746f7 | 755 | |
11a7f229 TG |
756 | int link_get(Manager *m, int ifindex, Link **ret) { |
757 | Link *link; | |
11a7f229 TG |
758 | |
759 | assert(m); | |
e856ed00 | 760 | assert(ifindex > 0); |
11a7f229 TG |
761 | assert(ret); |
762 | ||
ae06ab10 | 763 | link = hashmap_get(m->links, INT_TO_PTR(ifindex)); |
11a7f229 TG |
764 | if (!link) |
765 | return -ENODEV; | |
766 | ||
767 | *ret = link; | |
768 | ||
769 | return 0; | |
770 | } | |
771 | ||
af9ba57a | 772 | void link_set_state(Link *link, LinkState state) { |
e331e246 TG |
773 | assert(link); |
774 | ||
775 | if (link->state == state) | |
776 | return; | |
777 | ||
0beb9542 YW |
778 | log_link_debug(link, "State changed: %s -> %s", |
779 | link_state_to_string(link->state), | |
780 | link_state_to_string(state)); | |
781 | ||
e331e246 TG |
782 | link->state = state; |
783 | ||
784 | link_send_changed(link, "AdministrativeState", NULL); | |
e331e246 TG |
785 | } |
786 | ||
57bd6899 TG |
787 | static void link_enter_unmanaged(Link *link) { |
788 | assert(link); | |
789 | ||
e331e246 | 790 | link_set_state(link, LINK_STATE_UNMANAGED); |
57bd6899 | 791 | |
84de38c5 | 792 | link_dirty(link); |
57bd6899 TG |
793 | } |
794 | ||
95355a28 | 795 | int link_stop_clients(Link *link, bool may_keep_dhcp) { |
111bb8f9 | 796 | int r = 0, k; |
051e77ca | 797 | Address *ad; |
111bb8f9 TG |
798 | |
799 | assert(link); | |
800 | assert(link->manager); | |
801 | assert(link->manager->event); | |
802 | ||
d03073dd YW |
803 | dhcp4_release_old_lease(link); |
804 | ||
80060352 ZJS |
805 | bool keep_dhcp = may_keep_dhcp && |
806 | link->network && | |
807 | (link->manager->restarting || | |
808 | FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP_ON_STOP)); | |
809 | ||
810 | if (link->dhcp_client && !keep_dhcp) { | |
111bb8f9 | 811 | k = sd_dhcp_client_stop(link->dhcp_client); |
6a7a4e4d | 812 | if (k < 0) |
36c7d709 | 813 | r = log_link_warning_errno(link, k, "Could not stop DHCPv4 client: %m"); |
111bb8f9 TG |
814 | } |
815 | ||
ba179154 | 816 | if (link->ipv4ll) { |
111bb8f9 | 817 | k = sd_ipv4ll_stop(link->ipv4ll); |
6a7a4e4d | 818 | if (k < 0) |
36c7d709 | 819 | r = log_link_warning_errno(link, k, "Could not stop IPv4 link-local: %m"); |
dd43110f TG |
820 | } |
821 | ||
051e77ca SS |
822 | if (link->network) |
823 | LIST_FOREACH(addresses, ad, link->network->static_addresses) | |
824 | if (ad->acd && sd_ipv4acd_is_running(ad->acd) == 0) { | |
825 | k = sd_ipv4acd_stop(ad->acd); | |
826 | if (k < 0) | |
827 | r = log_link_warning_errno(link, k, "Could not stop IPv4 ACD client: %m"); | |
828 | } | |
829 | ||
f5a8c43f TG |
830 | if (link->dhcp6_client) { |
831 | k = sd_dhcp6_client_stop(link->dhcp6_client); | |
832 | if (k < 0) | |
36c7d709 | 833 | r = log_link_warning_errno(link, k, "Could not stop DHCPv6 client: %m"); |
f5a8c43f | 834 | } |
4138fb2c | 835 | |
1e7a0e21 LP |
836 | if (link->ndisc) { |
837 | k = sd_ndisc_stop(link->ndisc); | |
6a7a4e4d | 838 | if (k < 0) |
36c7d709 | 839 | r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Discovery: %m"); |
4138fb2c PF |
840 | } |
841 | ||
7465dd22 | 842 | if (link->radv) { |
290696e5 | 843 | k = sd_radv_stop(link->radv); |
7465dd22 PF |
844 | if (k < 0) |
845 | r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Advertisement: %m"); | |
846 | } | |
847 | ||
7272b25e | 848 | link_lldp_emit_stop(link); |
111bb8f9 TG |
849 | return r; |
850 | } | |
851 | ||
b22d8a00 | 852 | void link_enter_failed(Link *link) { |
ef1ba606 | 853 | assert(link); |
f882c247 | 854 | |
370e9930 | 855 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
2139694e TG |
856 | return; |
857 | ||
6a7a4e4d | 858 | log_link_warning(link, "Failed"); |
449f7554 | 859 | |
e331e246 | 860 | link_set_state(link, LINK_STATE_FAILED); |
fe8db0c5 | 861 | |
95355a28 | 862 | link_stop_clients(link, false); |
111bb8f9 | 863 | |
84de38c5 | 864 | link_dirty(link); |
f882c247 TG |
865 | } |
866 | ||
7033af49 YW |
867 | static int link_join_netdevs_after_configured(Link *link) { |
868 | NetDev *netdev; | |
869 | Iterator i; | |
870 | int r; | |
871 | ||
872 | HASHMAP_FOREACH(netdev, link->network->stacked_netdevs, i) { | |
873 | if (netdev->ifindex > 0) | |
874 | /* Assume already enslaved. */ | |
875 | continue; | |
876 | ||
877 | if (netdev_get_create_type(netdev) != NETDEV_CREATE_AFTER_CONFIGURED) | |
878 | continue; | |
879 | ||
880 | log_struct(LOG_DEBUG, | |
881 | LOG_LINK_INTERFACE(link), | |
882 | LOG_NETDEV_INTERFACE(netdev), | |
883 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", netdev->ifname)); | |
884 | ||
885 | r = netdev_join(netdev, link, NULL); | |
886 | if (r < 0) | |
887 | return log_struct_errno(LOG_WARNING, r, | |
888 | LOG_LINK_INTERFACE(link), | |
889 | LOG_NETDEV_INTERFACE(netdev), | |
890 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", netdev->ifname)); | |
891 | } | |
892 | ||
893 | return 0; | |
894 | } | |
895 | ||
e3a7b048 | 896 | static void link_enter_configured(Link *link) { |
dd43110f TG |
897 | assert(link); |
898 | assert(link->network); | |
e3a7b048 | 899 | |
289e6774 | 900 | if (link->state != LINK_STATE_CONFIGURING) |
e3a7b048 | 901 | return; |
dd43110f | 902 | |
e331e246 | 903 | link_set_state(link, LINK_STATE_CONFIGURED); |
dd43110f | 904 | |
7033af49 YW |
905 | (void) link_join_netdevs_after_configured(link); |
906 | ||
84de38c5 | 907 | link_dirty(link); |
dd43110f TG |
908 | } |
909 | ||
47079967 | 910 | static int link_request_set_routing_policy_rule(Link *link) { |
8a9b3a23 SS |
911 | RoutingPolicyRule *rule, *rrule = NULL; |
912 | int r; | |
913 | ||
914 | assert(link); | |
915 | assert(link->network); | |
916 | ||
2428613f YW |
917 | link->routing_policy_rules_configured = false; |
918 | ||
8a9b3a23 | 919 | LIST_FOREACH(rules, rule, link->network->rules) { |
b80a511b | 920 | r = routing_policy_rule_get(link->manager, rule, &rrule); |
ff14e2eb YW |
921 | if (r >= 0) { |
922 | if (r == 0) | |
923 | (void) routing_policy_rule_make_local(link->manager, rrule); | |
8a9b3a23 SS |
924 | continue; |
925 | } | |
926 | ||
9f08a578 | 927 | r = routing_policy_rule_configure(rule, link, NULL); |
4ff296b0 YW |
928 | if (r < 0) |
929 | return log_link_warning_errno(link, r, "Could not set routing policy rules: %m"); | |
7ef7e550 YW |
930 | if (r > 0) |
931 | link->routing_policy_rule_messages++; | |
8a9b3a23 SS |
932 | } |
933 | ||
934 | routing_policy_rule_purge(link->manager, link); | |
7715629e ST |
935 | if (link->routing_policy_rule_messages == 0) { |
936 | link->routing_policy_rules_configured = true; | |
937 | link_check_ready(link); | |
b425c3ae | 938 | } else { |
7715629e | 939 | log_link_debug(link, "Setting routing policy rules"); |
b425c3ae YW |
940 | link_set_state(link, LINK_STATE_CONFIGURING); |
941 | } | |
8a9b3a23 SS |
942 | |
943 | return 0; | |
944 | } | |
945 | ||
c16c7808 SS |
946 | static int nexthop_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
947 | int r; | |
948 | ||
949 | assert(link); | |
950 | assert(link->nexthop_messages > 0); | |
951 | assert(IN_SET(link->state, LINK_STATE_CONFIGURING, | |
952 | LINK_STATE_FAILED, LINK_STATE_LINGER)); | |
953 | ||
954 | link->nexthop_messages--; | |
955 | ||
956 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
957 | return 1; | |
958 | ||
959 | r = sd_netlink_message_get_errno(m); | |
960 | if (r < 0 && r != -EEXIST) { | |
5ecb131d | 961 | log_link_message_warning_errno(link, m, r, "Could not set nexthop"); |
c16c7808 SS |
962 | link_enter_failed(link); |
963 | return 1; | |
964 | } | |
965 | ||
966 | if (link->nexthop_messages == 0) { | |
967 | log_link_debug(link, "Nexthop set"); | |
968 | link->static_nexthops_configured = true; | |
969 | link_check_ready(link); | |
970 | } | |
971 | ||
972 | return 1; | |
973 | } | |
974 | ||
eb01a2df | 975 | static int link_request_set_nexthop(Link *link) { |
c16c7808 SS |
976 | NextHop *nh; |
977 | int r; | |
978 | ||
979 | LIST_FOREACH(nexthops, nh, link->network->static_nexthops) { | |
980 | r = nexthop_configure(nh, link, nexthop_handler); | |
981 | if (r < 0) | |
982 | return log_link_warning_errno(link, r, "Could not set nexthop: %m"); | |
983 | if (r > 0) | |
984 | link->nexthop_messages++; | |
985 | } | |
986 | ||
987 | if (link->nexthop_messages == 0) { | |
988 | link->static_nexthops_configured = true; | |
989 | link_check_ready(link); | |
990 | } else { | |
991 | log_link_debug(link, "Setting nexthop"); | |
992 | link_set_state(link, LINK_STATE_CONFIGURING); | |
993 | } | |
994 | ||
995 | return 1; | |
996 | } | |
997 | ||
302a796f | 998 | static int route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
f882c247 TG |
999 | int r; |
1000 | ||
1046bf9b | 1001 | assert(link); |
7715629e | 1002 | assert(link->route_messages > 0); |
289e6774 WKI |
1003 | assert(IN_SET(link->state, LINK_STATE_CONFIGURING, |
1004 | LINK_STATE_FAILED, LINK_STATE_LINGER)); | |
f882c247 | 1005 | |
7715629e | 1006 | link->route_messages--; |
f882c247 | 1007 | |
77a008c0 | 1008 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
f882c247 TG |
1009 | return 1; |
1010 | ||
1c4baffc | 1011 | r = sd_netlink_message_get_errno(m); |
4ff296b0 | 1012 | if (r < 0 && r != -EEXIST) { |
5ecb131d | 1013 | log_link_message_warning_errno(link, m, r, "Could not set route"); |
4ff296b0 YW |
1014 | link_enter_failed(link); |
1015 | return 1; | |
1016 | } | |
f882c247 | 1017 | |
7715629e | 1018 | if (link->route_messages == 0) { |
6a7a4e4d | 1019 | log_link_debug(link, "Routes set"); |
7715629e | 1020 | link->static_routes_configured = true; |
8012cd39 | 1021 | link_check_ready(link); |
dd3efc09 | 1022 | } |
f882c247 TG |
1023 | |
1024 | return 1; | |
1025 | } | |
1026 | ||
b5799eeb | 1027 | int link_request_set_routes(Link *link) { |
bf61b05a LP |
1028 | enum { |
1029 | PHASE_NON_GATEWAY, /* First phase: Routes without a gateway */ | |
1030 | PHASE_GATEWAY, /* Second phase: Routes with a gateway */ | |
1031 | _PHASE_MAX | |
1032 | } phase; | |
a6cc569e | 1033 | Route *rt; |
f882c247 TG |
1034 | int r; |
1035 | ||
1036 | assert(link); | |
1037 | assert(link->network); | |
c42ff3a1 WKI |
1038 | assert(link->addresses_configured); |
1039 | assert(link->address_messages == 0); | |
289e6774 | 1040 | assert(link->state != _LINK_STATE_INVALID); |
f882c247 | 1041 | |
2428613f | 1042 | link->static_routes_configured = false; |
c16c7808 | 1043 | link->static_routes_ready = false; |
27c34f73 | 1044 | |
4e2ef9d9 YW |
1045 | if (!link_has_carrier(link) && !link->network->configure_without_carrier) |
1046 | /* During configuring addresses, the link lost its carrier. As networkd is dropping | |
1047 | * the addresses now, let's not configure the routes either. */ | |
1048 | return 0; | |
1049 | ||
47079967 | 1050 | r = link_request_set_routing_policy_rule(link); |
f3ef324d YW |
1051 | if (r < 0) |
1052 | return r; | |
f882c247 | 1053 | |
bf61b05a LP |
1054 | /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */ |
1055 | for (phase = 0; phase < _PHASE_MAX; phase++) | |
1056 | LIST_FOREACH(routes, rt, link->network->static_routes) { | |
1985c54f YW |
1057 | if (rt->gateway_from_dhcp) |
1058 | continue; | |
0d34228f | 1059 | |
6ff5cc6b | 1060 | if ((in_addr_is_null(rt->family, &rt->gw) && ordered_set_isempty(rt->multipath_routes)) != (phase == PHASE_NON_GATEWAY)) |
bf61b05a | 1061 | continue; |
a6cc569e | 1062 | |
0d34228f | 1063 | r = route_configure(rt, link, route_handler); |
4ff296b0 YW |
1064 | if (r < 0) |
1065 | return log_link_warning_errno(link, r, "Could not set routes: %m"); | |
c4423317 YW |
1066 | if (r > 0) |
1067 | link->route_messages++; | |
0d34228f | 1068 | } |
f5be5601 | 1069 | |
7715629e ST |
1070 | if (link->route_messages == 0) { |
1071 | link->static_routes_configured = true; | |
8012cd39 | 1072 | link_check_ready(link); |
b425c3ae | 1073 | } else { |
6a7a4e4d | 1074 | log_link_debug(link, "Setting routes"); |
b425c3ae YW |
1075 | link_set_state(link, LINK_STATE_CONFIGURING); |
1076 | } | |
f882c247 TG |
1077 | |
1078 | return 0; | |
1079 | } | |
1080 | ||
6accfd31 DA |
1081 | void link_check_ready(Link *link) { |
1082 | Address *a; | |
1083 | Iterator i; | |
4ff296b0 | 1084 | int r; |
6accfd31 DA |
1085 | |
1086 | assert(link); | |
1087 | ||
39373cb9 YW |
1088 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) { |
1089 | log_link_debug(link, "%s(): link is in failed or linger state.", __func__); | |
6accfd31 | 1090 | return; |
39373cb9 | 1091 | } |
6accfd31 DA |
1092 | |
1093 | if (!link->network) | |
1094 | return; | |
1095 | ||
39373cb9 YW |
1096 | if (!link->addresses_configured) { |
1097 | log_link_debug(link, "%s(): static addresses are not configured.", __func__); | |
6accfd31 | 1098 | return; |
39373cb9 | 1099 | } |
6accfd31 | 1100 | |
39373cb9 YW |
1101 | if (!link->neighbors_configured) { |
1102 | log_link_debug(link, "%s(): static neighbors are not configured.", __func__); | |
6accfd31 | 1103 | return; |
39373cb9 | 1104 | } |
6accfd31 | 1105 | |
6aa5773b | 1106 | SET_FOREACH(a, link->addresses, i) |
39373cb9 YW |
1107 | if (!address_is_ready(a)) { |
1108 | _cleanup_free_ char *str = NULL; | |
1109 | ||
1110 | (void) in_addr_to_string(a->family, &a->in_addr, &str); | |
1111 | log_link_debug(link, "%s(): an address %s/%d is not ready.", __func__, strnull(str), a->prefixlen); | |
6aa5773b | 1112 | return; |
39373cb9 | 1113 | } |
6aa5773b DA |
1114 | |
1115 | if (!link->addresses_ready) { | |
1116 | link->addresses_ready = true; | |
4ff296b0 YW |
1117 | r = link_request_set_routes(link); |
1118 | if (r < 0) | |
1119 | link_enter_failed(link); | |
39373cb9 | 1120 | log_link_debug(link, "%s(): static addresses are configured. Configuring static routes.", __func__); |
710ce9e5 | 1121 | return; |
6aa5773b DA |
1122 | } |
1123 | ||
39373cb9 YW |
1124 | if (!link->static_routes_configured) { |
1125 | log_link_debug(link, "%s(): static routes are not configured.", __func__); | |
6accfd31 | 1126 | return; |
39373cb9 | 1127 | } |
6accfd31 | 1128 | |
c16c7808 SS |
1129 | if (!link->static_routes_ready) { |
1130 | link->static_routes_ready = true; | |
1131 | r = link_request_set_nexthop(link); | |
1132 | if (r < 0) | |
1133 | link_enter_failed(link); | |
39373cb9 | 1134 | log_link_debug(link, "%s(): static routes are configured. Configuring static nexthops.", __func__); |
c16c7808 SS |
1135 | return; |
1136 | } | |
1137 | ||
39373cb9 YW |
1138 | if (!link->static_nexthops_configured) { |
1139 | log_link_debug(link, "%s(): static nexthops are not configured.", __func__); | |
c16c7808 | 1140 | return; |
39373cb9 | 1141 | } |
c16c7808 | 1142 | |
39373cb9 YW |
1143 | if (!link->routing_policy_rules_configured) { |
1144 | log_link_debug(link, "%s(): static routing policy rules are not configured.", __func__); | |
6accfd31 | 1145 | return; |
39373cb9 | 1146 | } |
6accfd31 | 1147 | |
39373cb9 YW |
1148 | if (!link->tc_configured) { |
1149 | log_link_debug(link, "%s(): traffic controls are not configured.", __func__); | |
4ecdcb07 | 1150 | return; |
39373cb9 | 1151 | } |
4ecdcb07 | 1152 | |
39373cb9 YW |
1153 | if (!link->sr_iov_configured) { |
1154 | log_link_debug(link, "%s(): SR-IOV is not configured.", __func__); | |
518cd6b5 | 1155 | return; |
39373cb9 | 1156 | } |
518cd6b5 | 1157 | |
463797c1 | 1158 | if (link_has_carrier(link) || !link->network->configure_without_carrier) { |
6accfd31 | 1159 | |
39373cb9 YW |
1160 | if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_IPV4) && !link->ipv4ll_address) { |
1161 | log_link_debug(link, "%s(): IPv4LL is not configured.", __func__); | |
463797c1 | 1162 | return; |
39373cb9 | 1163 | } |
6accfd31 | 1164 | |
463797c1 | 1165 | if (link_ipv6ll_enabled(link) && |
39373cb9 YW |
1166 | in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address)) { |
1167 | log_link_debug(link, "%s(): IPv6LL is not configured.", __func__); | |
463797c1 | 1168 | return; |
39373cb9 | 1169 | } |
6accfd31 | 1170 | |
39373cb9 YW |
1171 | if (link_dhcp4_enabled(link) || link_dhcp6_enabled(link) || dhcp6_get_prefix_delegation(link) || link_ipv6_accept_ra_enabled(link)) { |
1172 | if (!link->dhcp4_configured && | |
1173 | !(link->dhcp6_address_configured && link->dhcp6_route_configured) && | |
1174 | !(link->dhcp6_pd_address_configured && link->dhcp6_pd_route_configured) && | |
1175 | !link->ndisc_configured && | |
1176 | !(link_ipv4ll_enabled(link, ADDRESS_FAMILY_FALLBACK_IPV4) && link->ipv4ll_address)) { | |
1177 | /* When DHCP or RA is enabled, at least one protocol must provide an address, or | |
1178 | * an IPv4ll fallback address must be configured. */ | |
1179 | log_link_debug(link, "%s(): dynamic addresses or routes are not configured.", __func__); | |
1180 | return; | |
1181 | } | |
1182 | ||
1183 | log_link_debug(link, "%s(): dhcp4:%s dhcp6_addresses:%s dhcp_routes:%s dhcp_pd_addresses:%s dhcp_pd_routes:%s ndisc:%s", | |
1184 | __func__, | |
1185 | yes_no(link->dhcp4_configured), | |
1186 | yes_no(link->dhcp6_address_configured), | |
1187 | yes_no(link->dhcp6_route_configured), | |
1188 | yes_no(link->dhcp6_pd_address_configured), | |
1189 | yes_no(link->dhcp6_pd_route_configured), | |
1190 | yes_no(link->ndisc_configured)); | |
1191 | } | |
463797c1 | 1192 | } |
6accfd31 | 1193 | |
6accfd31 DA |
1194 | if (link->state != LINK_STATE_CONFIGURED) |
1195 | link_enter_configured(link); | |
1196 | ||
1197 | return; | |
1198 | } | |
1199 | ||
e4a71bf3 WKI |
1200 | static int link_request_set_neighbors(Link *link) { |
1201 | Neighbor *neighbor; | |
1202 | int r; | |
1203 | ||
1204 | assert(link); | |
1205 | assert(link->network); | |
1206 | assert(link->state != _LINK_STATE_INVALID); | |
1207 | ||
2428613f | 1208 | link->neighbors_configured = false; |
e4a71bf3 WKI |
1209 | |
1210 | LIST_FOREACH(neighbors, neighbor, link->network->neighbors) { | |
1211 | r = neighbor_configure(neighbor, link, NULL); | |
4ff296b0 YW |
1212 | if (r < 0) |
1213 | return log_link_warning_errno(link, r, "Could not set neighbor: %m"); | |
e4a71bf3 WKI |
1214 | } |
1215 | ||
1216 | if (link->neighbor_messages == 0) { | |
1217 | link->neighbors_configured = true; | |
1218 | link_check_ready(link); | |
b425c3ae | 1219 | } else { |
e4a71bf3 | 1220 | log_link_debug(link, "Setting neighbors"); |
b425c3ae YW |
1221 | link_set_state(link, LINK_STATE_CONFIGURING); |
1222 | } | |
e4a71bf3 WKI |
1223 | |
1224 | return 0; | |
1225 | } | |
1226 | ||
3650173f | 1227 | static int address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
f882c247 TG |
1228 | int r; |
1229 | ||
4958aee4 | 1230 | assert(rtnl); |
f5be5601 TG |
1231 | assert(m); |
1232 | assert(link); | |
1233 | assert(link->ifname); | |
7715629e | 1234 | assert(link->address_messages > 0); |
289e6774 | 1235 | assert(IN_SET(link->state, LINK_STATE_CONFIGURING, |
370e9930 | 1236 | LINK_STATE_FAILED, LINK_STATE_LINGER)); |
f882c247 | 1237 | |
7715629e | 1238 | link->address_messages--; |
f882c247 | 1239 | |
5da8149f | 1240 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
f882c247 TG |
1241 | return 1; |
1242 | ||
1c4baffc | 1243 | r = sd_netlink_message_get_errno(m); |
4ff296b0 | 1244 | if (r < 0 && r != -EEXIST) { |
5ecb131d | 1245 | log_link_message_warning_errno(link, m, r, "Could not set address"); |
4ff296b0 YW |
1246 | link_enter_failed(link); |
1247 | return 1; | |
1248 | } else if (r >= 0) | |
1249 | (void) manager_rtnl_process_address(rtnl, m, link->manager); | |
f882c247 | 1250 | |
7715629e | 1251 | if (link->address_messages == 0) { |
6a7a4e4d | 1252 | log_link_debug(link, "Addresses set"); |
c42ff3a1 | 1253 | link->addresses_configured = true; |
6aa5773b | 1254 | link_check_ready(link); |
dd3efc09 | 1255 | } |
f882c247 TG |
1256 | |
1257 | return 1; | |
1258 | } | |
1259 | ||
f6bb7ac5 TJ |
1260 | static int link_set_bridge_fdb(Link *link) { |
1261 | FdbEntry *fdb_entry; | |
197e2809 | 1262 | int r; |
f6bb7ac5 TJ |
1263 | |
1264 | LIST_FOREACH(static_fdb_entries, fdb_entry, link->network->static_fdb_entries) { | |
1265 | r = fdb_entry_configure(link, fdb_entry); | |
197e2809 | 1266 | if (r < 0) |
f6bb7ac5 | 1267 | return log_link_error_errno(link, r, "Failed to add MAC entry to static MAC table: %m"); |
f6bb7ac5 TJ |
1268 | } |
1269 | ||
197e2809 | 1270 | return 0; |
f6bb7ac5 TJ |
1271 | } |
1272 | ||
289e6774 | 1273 | static int link_request_set_addresses(Link *link) { |
95b74ef6 | 1274 | AddressLabel *label; |
a6cc569e | 1275 | Address *ad; |
bd6379ec | 1276 | Prefix *p; |
f882c247 TG |
1277 | int r; |
1278 | ||
1279 | assert(link); | |
1280 | assert(link->network); | |
f5be5601 | 1281 | assert(link->state != _LINK_STATE_INVALID); |
f882c247 | 1282 | |
2428613f YW |
1283 | /* Reset all *_configured flags we are configuring. */ |
1284 | link->addresses_configured = false; | |
6aa5773b | 1285 | link->addresses_ready = false; |
2428613f YW |
1286 | link->neighbors_configured = false; |
1287 | link->static_routes_configured = false; | |
c16c7808 SS |
1288 | link->static_routes_ready = false; |
1289 | link->static_nexthops_configured = false; | |
2428613f YW |
1290 | link->routing_policy_rules_configured = false; |
1291 | ||
f6bb7ac5 TJ |
1292 | r = link_set_bridge_fdb(link); |
1293 | if (r < 0) | |
1294 | return r; | |
1295 | ||
f3ef324d YW |
1296 | r = link_request_set_neighbors(link); |
1297 | if (r < 0) | |
1298 | return r; | |
e4a71bf3 | 1299 | |
3d3d4255 | 1300 | LIST_FOREACH(addresses, ad, link->network->static_addresses) { |
a47a6dae YW |
1301 | bool update; |
1302 | ||
1303 | update = address_get(link, ad->family, &ad->in_addr, ad->prefixlen, NULL) > 0; | |
1304 | ||
1305 | r = address_configure(ad, link, address_handler, update); | |
4ff296b0 YW |
1306 | if (r < 0) |
1307 | return log_link_warning_errno(link, r, "Could not set addresses: %m"); | |
54a1a535 YW |
1308 | if (r > 0) |
1309 | link->address_messages++; | |
95b74ef6 SS |
1310 | } |
1311 | ||
bd6379ec SS |
1312 | if (IN_SET(link->network->router_prefix_delegation, |
1313 | RADV_PREFIX_DELEGATION_STATIC, | |
1314 | RADV_PREFIX_DELEGATION_BOTH)) | |
1315 | LIST_FOREACH(prefixes, p, link->network->static_prefixes) { | |
1316 | _cleanup_(address_freep) Address *address = NULL; | |
1317 | ||
1318 | if (!p->assign) | |
1319 | continue; | |
1320 | ||
1321 | r = address_new(&address); | |
1322 | if (r < 0) | |
1323 | return log_link_error_errno(link, r, "Could not allocate address: %m"); | |
1324 | ||
1325 | r = sd_radv_prefix_get_prefix(p->radv_prefix, &address->in_addr.in6, &address->prefixlen); | |
1326 | if (r < 0) | |
1327 | return r; | |
1328 | ||
1329 | r = generate_ipv6_eui_64_address(link, &address->in_addr.in6); | |
1330 | if (r < 0) | |
1331 | return r; | |
1332 | ||
1333 | address->family = AF_INET6; | |
1334 | r = address_configure(address, link, address_handler, true); | |
1335 | if (r < 0) | |
1336 | return log_link_warning_errno(link, r, "Could not set addresses: %m"); | |
1337 | if (r > 0) | |
1338 | link->address_messages++; | |
1339 | } | |
1340 | ||
95b74ef6 | 1341 | LIST_FOREACH(labels, label, link->network->address_labels) { |
cccf9517 | 1342 | r = address_label_configure(label, link, NULL, false); |
4ff296b0 YW |
1343 | if (r < 0) |
1344 | return log_link_warning_errno(link, r, "Could not set address label: %m"); | |
f5be5601 | 1345 | |
7715629e | 1346 | link->address_label_messages++; |
f882c247 TG |
1347 | } |
1348 | ||
d4cdbea5 TG |
1349 | /* now that we can figure out a default address for the dhcp server, |
1350 | start it */ | |
708c425d | 1351 | if (link_dhcp4_server_enabled(link) && (link->flags & IFF_UP)) { |
8fcf1d61 | 1352 | r = dhcp4_server_configure(link); |
4ff296b0 | 1353 | if (r < 0) |
d4cdbea5 | 1354 | return r; |
6a7a4e4d | 1355 | log_link_debug(link, "Offering DHCPv4 leases"); |
d4cdbea5 TG |
1356 | } |
1357 | ||
c42ff3a1 WKI |
1358 | if (link->address_messages == 0) { |
1359 | link->addresses_configured = true; | |
6aa5773b | 1360 | link_check_ready(link); |
b425c3ae | 1361 | } else { |
6a7a4e4d | 1362 | log_link_debug(link, "Setting addresses"); |
b425c3ae YW |
1363 | link_set_state(link, LINK_STATE_CONFIGURING); |
1364 | } | |
431ca2ce | 1365 | |
f882c247 TG |
1366 | return 0; |
1367 | } | |
1368 | ||
13b498f9 | 1369 | static int link_set_bridge_vlan(Link *link) { |
86e2be7b | 1370 | int r; |
13b498f9 TJ |
1371 | |
1372 | r = br_vlan_configure(link, link->network->pvid, link->network->br_vid_bitmap, link->network->br_untagged_bitmap); | |
1373 | if (r < 0) | |
1374 | log_link_error_errno(link, r, "Failed to assign VLANs to bridge port: %m"); | |
1375 | ||
1376 | return r; | |
1377 | } | |
1378 | ||
a60a720c | 1379 | static int link_set_proxy_arp(Link *link) { |
23d8b221 SS |
1380 | int r; |
1381 | ||
1382 | if (!link_proxy_arp_enabled(link)) | |
1383 | return 0; | |
1384 | ||
62e021a9 | 1385 | r = sysctl_write_ip_property_boolean(AF_INET, link->ifname, "proxy_arp", link->network->proxy_arp > 0); |
23d8b221 SS |
1386 | if (r < 0) |
1387 | log_link_warning_errno(link, r, "Cannot configure proxy ARP for interface: %m"); | |
1388 | ||
1389 | return 0; | |
1390 | } | |
1391 | ||
3a390124 | 1392 | static int link_configure_continue(Link *link); |
55dc8c4a | 1393 | |
302a796f | 1394 | static int set_mtu_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
4f882b2a TG |
1395 | int r; |
1396 | ||
1397 | assert(m); | |
1398 | assert(link); | |
1399 | assert(link->ifname); | |
1400 | ||
55dc8c4a YW |
1401 | link->setting_mtu = false; |
1402 | ||
5da8149f | 1403 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
4f882b2a TG |
1404 | return 1; |
1405 | ||
1c4baffc | 1406 | r = sd_netlink_message_get_errno(m); |
4ff296b0 | 1407 | if (r < 0) |
5ecb131d | 1408 | log_link_message_warning_errno(link, m, r, "Could not set MTU, ignoring"); |
4ff296b0 YW |
1409 | else |
1410 | log_link_debug(link, "Setting MTU done."); | |
55dc8c4a | 1411 | |
4ff296b0 | 1412 | if (link->state == LINK_STATE_INITIALIZED) { |
3a390124 | 1413 | r = link_configure_continue(link); |
4ff296b0 YW |
1414 | if (r < 0) |
1415 | link_enter_failed(link); | |
1416 | } | |
4f882b2a TG |
1417 | |
1418 | return 1; | |
1419 | } | |
1420 | ||
933c70a0 | 1421 | int link_set_mtu(Link *link, uint32_t mtu) { |
4afd3348 | 1422 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
4f882b2a TG |
1423 | int r; |
1424 | ||
1425 | assert(link); | |
1426 | assert(link->manager); | |
1427 | assert(link->manager->rtnl); | |
1428 | ||
40288ece YW |
1429 | if (mtu == 0 || link->setting_mtu) |
1430 | return 0; | |
1431 | ||
933c70a0 | 1432 | if (link->mtu == mtu) |
ee493106 YW |
1433 | return 0; |
1434 | ||
6a7a4e4d | 1435 | log_link_debug(link, "Setting MTU: %" PRIu32, mtu); |
4f882b2a | 1436 | |
6a7a4e4d LP |
1437 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1438 | if (r < 0) | |
1439 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
4f882b2a | 1440 | |
44b598a1 | 1441 | /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes |
72c2500b YW |
1442 | * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */ |
1443 | if (link_ipv6_enabled(link) && mtu < IPV6_MIN_MTU) { | |
44b598a1 SS |
1444 | |
1445 | log_link_warning(link, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU) ", as " | |
d236718c | 1446 | "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU) " bytes"); |
44b598a1 | 1447 | |
72c2500b | 1448 | mtu = IPV6_MIN_MTU; |
44b598a1 SS |
1449 | } |
1450 | ||
1c4baffc | 1451 | r = sd_netlink_message_append_u32(req, IFLA_MTU, mtu); |
6a7a4e4d LP |
1452 | if (r < 0) |
1453 | return log_link_error_errno(link, r, "Could not append MTU: %m"); | |
4f882b2a | 1454 | |
302a796f YW |
1455 | r = netlink_call_async(link->manager->rtnl, NULL, req, set_mtu_handler, |
1456 | link_netlink_destroy_callback, link); | |
6a7a4e4d LP |
1457 | if (r < 0) |
1458 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
4f882b2a | 1459 | |
ae941762 | 1460 | link_ref(link); |
1046bf9b | 1461 | link->setting_mtu = true; |
b226d99b | 1462 | |
4f882b2a TG |
1463 | return 0; |
1464 | } | |
1465 | ||
f6fcc1c2 YW |
1466 | static bool link_reduces_vlan_mtu(Link *link) { |
1467 | /* See netif_reduces_vlan_mtu() in kernel. */ | |
1468 | return streq_ptr(link->kind, "macsec"); | |
1469 | } | |
1470 | ||
1471 | static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link *link) { | |
1472 | uint32_t mtu = 0; | |
1473 | NetDev *dev; | |
1474 | Iterator i; | |
1475 | ||
1476 | HASHMAP_FOREACH(dev, link->network->stacked_netdevs, i) | |
1477 | if (dev->kind == NETDEV_KIND_VLAN && dev->mtu > 0) | |
1478 | /* See vlan_dev_change_mtu() in kernel. */ | |
1479 | mtu = MAX(mtu, link_reduces_vlan_mtu(link) ? dev->mtu + 4 : dev->mtu); | |
1480 | ||
1481 | else if (dev->kind == NETDEV_KIND_MACVLAN && dev->mtu > mtu) | |
1482 | /* See macvlan_change_mtu() in kernel. */ | |
1483 | mtu = dev->mtu; | |
1484 | ||
1485 | return mtu; | |
1486 | } | |
1487 | ||
1488 | static int link_configure_mtu(Link *link) { | |
1489 | uint32_t mtu; | |
1490 | ||
1491 | assert(link); | |
1492 | assert(link->network); | |
1493 | ||
1494 | if (link->network->mtu > 0) | |
1495 | return link_set_mtu(link, link->network->mtu); | |
1496 | ||
1497 | mtu = link_get_requested_mtu_by_stacked_netdevs(link); | |
1498 | if (link->mtu >= mtu) | |
1499 | return 0; | |
1500 | ||
1501 | log_link_notice(link, "Bumping MTU bytes from %"PRIu32" to %"PRIu32" because of stacked device. " | |
1502 | "If it is not desired, then please explicitly specify MTUBytes= setting.", | |
1503 | link->mtu, mtu); | |
1504 | ||
1505 | return link_set_mtu(link, mtu); | |
1506 | } | |
1507 | ||
302a796f | 1508 | static int set_flags_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
99d2baa2 SS |
1509 | int r; |
1510 | ||
1511 | assert(m); | |
1512 | assert(link); | |
1513 | assert(link->ifname); | |
1514 | ||
1515 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1516 | return 1; | |
1517 | ||
1518 | r = sd_netlink_message_get_errno(m); | |
1519 | if (r < 0) | |
5ecb131d | 1520 | log_link_message_warning_errno(link, m, r, "Could not set link flags, ignoring"); |
99d2baa2 SS |
1521 | |
1522 | return 1; | |
1523 | } | |
1524 | ||
1525 | static int link_set_flags(Link *link) { | |
1526 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1527 | unsigned ifi_change = 0; | |
1528 | unsigned ifi_flags = 0; | |
1529 | int r; | |
1530 | ||
1531 | assert(link); | |
1532 | assert(link->manager); | |
1533 | assert(link->manager->rtnl); | |
1534 | ||
1535 | if (link->flags & IFF_LOOPBACK) | |
1536 | return 0; | |
1537 | ||
1538 | if (!link->network) | |
1539 | return 0; | |
1540 | ||
866e6b7a | 1541 | if (link->network->arp < 0 && link->network->multicast < 0 && link->network->allmulticast < 0) |
99d2baa2 SS |
1542 | return 0; |
1543 | ||
1544 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); | |
1545 | if (r < 0) | |
1546 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
1547 | ||
1548 | if (link->network->arp >= 0) { | |
1549 | ifi_change |= IFF_NOARP; | |
e6ebebbe SS |
1550 | SET_FLAG(ifi_flags, IFF_NOARP, link->network->arp == 0); |
1551 | } | |
1552 | ||
1553 | if (link->network->multicast >= 0) { | |
1554 | ifi_change |= IFF_MULTICAST; | |
1555 | SET_FLAG(ifi_flags, IFF_MULTICAST, link->network->multicast); | |
99d2baa2 SS |
1556 | } |
1557 | ||
866e6b7a SS |
1558 | if (link->network->allmulticast >= 0) { |
1559 | ifi_change |= IFF_ALLMULTI; | |
1560 | SET_FLAG(ifi_flags, IFF_ALLMULTI, link->network->allmulticast); | |
1561 | } | |
1562 | ||
99d2baa2 SS |
1563 | r = sd_rtnl_message_link_set_flags(req, ifi_flags, ifi_change); |
1564 | if (r < 0) | |
1565 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
1566 | ||
302a796f YW |
1567 | r = netlink_call_async(link->manager->rtnl, NULL, req, set_flags_handler, |
1568 | link_netlink_destroy_callback, link); | |
99d2baa2 SS |
1569 | if (r < 0) |
1570 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1571 | ||
1572 | link_ref(link); | |
1573 | ||
1574 | return 0; | |
1575 | } | |
1576 | ||
e7ab854c TG |
1577 | static int link_acquire_ipv6_conf(Link *link) { |
1578 | int r; | |
1579 | ||
1580 | assert(link); | |
1581 | ||
e7ab854c | 1582 | if (link_ipv6_accept_ra_enabled(link)) { |
1e7a0e21 | 1583 | assert(link->ndisc); |
e7ab854c TG |
1584 | |
1585 | log_link_debug(link, "Discovering IPv6 routers"); | |
1586 | ||
1e7a0e21 | 1587 | r = sd_ndisc_start(link->ndisc); |
63348d13 | 1588 | if (r < 0 && r != -EBUSY) |
e7ab854c TG |
1589 | return log_link_warning_errno(link, r, "Could not start IPv6 Router Discovery: %m"); |
1590 | } | |
1591 | ||
7465dd22 PF |
1592 | if (link_radv_enabled(link)) { |
1593 | assert(link->radv); | |
1594 | assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0); | |
1595 | ||
1596 | log_link_debug(link, "Starting IPv6 Router Advertisements"); | |
1597 | ||
fd3ef936 YW |
1598 | r = radv_emit_dns(link); |
1599 | if (r < 0) | |
1600 | return log_link_warning_errno(link, r, "Failed to configure DNS or Domains in IPv6 Router Advertisement: %m"); | |
1601 | ||
7465dd22 PF |
1602 | r = sd_radv_start(link->radv); |
1603 | if (r < 0 && r != -EBUSY) | |
1604 | return log_link_warning_errno(link, r, "Could not start IPv6 Router Advertisement: %m"); | |
1605 | } | |
1606 | ||
838d39af SS |
1607 | if (link_dhcp6_enabled(link) && IN_SET(link->network->dhcp6_without_ra, |
1608 | DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST, | |
1609 | DHCP6_CLIENT_START_MODE_SOLICIT)) { | |
cd305af1 SS |
1610 | assert(link->dhcp6_client); |
1611 | assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0); | |
1612 | ||
838d39af | 1613 | r = dhcp6_request_address(link, link->network->dhcp6_without_ra == DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST); |
cd305af1 SS |
1614 | if (r < 0 && r != -EBUSY) |
1615 | return log_link_warning_errno(link, r, "Could not acquire DHCPv6 lease: %m"); | |
1616 | else | |
1617 | log_link_debug(link, "Acquiring DHCPv6 lease"); | |
1618 | } | |
1619 | ||
10752343 PF |
1620 | (void) dhcp6_request_prefix_delegation(link); |
1621 | ||
e7ab854c TG |
1622 | return 0; |
1623 | } | |
1624 | ||
6fc25497 | 1625 | static int link_acquire_ipv4_conf(Link *link) { |
ff254138 TG |
1626 | int r; |
1627 | ||
1628 | assert(link); | |
ff254138 TG |
1629 | assert(link->manager); |
1630 | assert(link->manager->event); | |
1631 | ||
910feb78 | 1632 | if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_IPV4)) { |
eb34d4af | 1633 | assert(link->ipv4ll); |
ff254138 | 1634 | |
6a7a4e4d | 1635 | log_link_debug(link, "Acquiring IPv4 link-local address"); |
5c1d3fc9 UTL |
1636 | |
1637 | r = sd_ipv4ll_start(link->ipv4ll); | |
6a7a4e4d LP |
1638 | if (r < 0) |
1639 | return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m"); | |
5c1d3fc9 UTL |
1640 | } |
1641 | ||
78c958f8 | 1642 | if (link_dhcp4_enabled(link)) { |
eb34d4af | 1643 | assert(link->dhcp_client); |
ff254138 | 1644 | |
6a7a4e4d | 1645 | log_link_debug(link, "Acquiring DHCPv4 lease"); |
ab47d620 | 1646 | |
5c1d3fc9 | 1647 | r = sd_dhcp_client_start(link->dhcp_client); |
6a7a4e4d LP |
1648 | if (r < 0) |
1649 | return log_link_warning_errno(link, r, "Could not acquire DHCPv4 lease: %m"); | |
5c1d3fc9 | 1650 | } |
ff254138 | 1651 | |
6fc25497 SS |
1652 | return 0; |
1653 | } | |
1654 | ||
1655 | static int link_acquire_conf(Link *link) { | |
1656 | int r; | |
1657 | ||
1658 | assert(link); | |
1659 | ||
1660 | r = link_acquire_ipv4_conf(link); | |
1661 | if (r < 0) | |
1662 | return r; | |
1663 | ||
3f7cc080 | 1664 | if (!in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address)) { |
6fc25497 SS |
1665 | r = link_acquire_ipv6_conf(link); |
1666 | if (r < 0) | |
1667 | return r; | |
1668 | } | |
1669 | ||
7272b25e LP |
1670 | if (link_lldp_emit_enabled(link)) { |
1671 | r = link_lldp_emit_start(link); | |
8e1ad1ea LP |
1672 | if (r < 0) |
1673 | return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m"); | |
1674 | } | |
1675 | ||
ff254138 TG |
1676 | return 0; |
1677 | } | |
1678 | ||
a61bb41c | 1679 | bool link_has_carrier(Link *link) { |
deb2e523 TG |
1680 | /* see Documentation/networking/operstates.txt in the kernel sources */ |
1681 | ||
a61bb41c | 1682 | if (link->kernel_operstate == IF_OPER_UP) |
deb2e523 TG |
1683 | return true; |
1684 | ||
a61bb41c | 1685 | if (link->kernel_operstate == IF_OPER_UNKNOWN) |
deb2e523 | 1686 | /* operstate may not be implemented, so fall back to flags */ |
b26ea308 YW |
1687 | if (FLAGS_SET(link->flags, IFF_LOWER_UP | IFF_RUNNING) && |
1688 | !FLAGS_SET(link->flags, IFF_DORMANT)) | |
deb2e523 TG |
1689 | return true; |
1690 | ||
1691 | return false; | |
1692 | } | |
1693 | ||
0e2fdb83 SS |
1694 | static int link_address_genmode_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
1695 | int r; | |
1696 | ||
1697 | assert(link); | |
1698 | ||
9524014e DS |
1699 | link->setting_genmode = false; |
1700 | ||
0e2fdb83 SS |
1701 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
1702 | return 1; | |
1703 | ||
1704 | r = sd_netlink_message_get_errno(m); | |
1705 | if (r < 0) | |
5ecb131d | 1706 | log_link_message_warning_errno(link, m, r, "Could not set address genmode for interface, ignoring"); |
9524014e DS |
1707 | else |
1708 | log_link_debug(link, "Setting address genmode done."); | |
1709 | ||
1710 | if (link->state == LINK_STATE_INITIALIZED) { | |
1711 | r = link_configure_continue(link); | |
1712 | if (r < 0) | |
1713 | link_enter_failed(link); | |
1714 | } | |
0e2fdb83 SS |
1715 | |
1716 | return 1; | |
1717 | } | |
1718 | ||
1719 | static int link_configure_addrgen_mode(Link *link) { | |
1720 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1721 | uint8_t ipv6ll_mode; | |
1722 | int r; | |
1723 | ||
1724 | assert(link); | |
1725 | assert(link->network); | |
1726 | assert(link->manager); | |
1727 | assert(link->manager->rtnl); | |
1728 | ||
9524014e | 1729 | if (!socket_ipv6_is_supported() || link->setting_genmode) |
9f6e82e6 YW |
1730 | return 0; |
1731 | ||
0e2fdb83 SS |
1732 | log_link_debug(link, "Setting address genmode for link"); |
1733 | ||
1734 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); | |
1735 | if (r < 0) | |
1736 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
1737 | ||
1738 | r = sd_netlink_message_open_container(req, IFLA_AF_SPEC); | |
1739 | if (r < 0) | |
1740 | return log_link_error_errno(link, r, "Could not open IFLA_AF_SPEC container: %m"); | |
1741 | ||
1742 | r = sd_netlink_message_open_container(req, AF_INET6); | |
1743 | if (r < 0) | |
1744 | return log_link_error_errno(link, r, "Could not open AF_INET6 container: %m"); | |
1745 | ||
1746 | if (!link_ipv6ll_enabled(link)) | |
1747 | ipv6ll_mode = IN6_ADDR_GEN_MODE_NONE; | |
6f6296b9 | 1748 | else if (link->network->ipv6ll_address_gen_mode < 0) { |
a6f1848a SS |
1749 | r = sysctl_read_ip_property(AF_INET6, link->ifname, "stable_secret", NULL); |
1750 | if (r < 0) { | |
1751 | /* The file may not exist. And even if it exists, when stable_secret is unset, | |
1752 | * reading the file fails with EIO. */ | |
1753 | log_link_debug_errno(link, r, "Failed to read sysctl property stable_secret: %m"); | |
1754 | ||
1755 | ipv6ll_mode = IN6_ADDR_GEN_MODE_EUI64; | |
1756 | } else | |
1757 | ipv6ll_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; | |
1758 | } else | |
6f6296b9 | 1759 | ipv6ll_mode = link->network->ipv6ll_address_gen_mode; |
0e2fdb83 SS |
1760 | |
1761 | r = sd_netlink_message_append_u8(req, IFLA_INET6_ADDR_GEN_MODE, ipv6ll_mode); | |
1762 | if (r < 0) | |
1763 | return log_link_error_errno(link, r, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m"); | |
1764 | ||
1765 | r = sd_netlink_message_close_container(req); | |
1766 | if (r < 0) | |
1767 | return log_link_error_errno(link, r, "Could not close AF_INET6 container: %m"); | |
1768 | ||
1769 | r = sd_netlink_message_close_container(req); | |
1770 | if (r < 0) | |
1771 | return log_link_error_errno(link, r, "Could not close IFLA_AF_SPEC container: %m"); | |
1772 | ||
1773 | r = netlink_call_async(link->manager->rtnl, NULL, req, link_address_genmode_handler, | |
1774 | link_netlink_destroy_callback, link); | |
1775 | if (r < 0) | |
1776 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1777 | ||
1778 | link_ref(link); | |
9524014e | 1779 | link->setting_genmode = true; |
0e2fdb83 SS |
1780 | |
1781 | return 0; | |
1782 | } | |
1783 | ||
302a796f | 1784 | static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
dd3efc09 TG |
1785 | int r; |
1786 | ||
1746cf2a TG |
1787 | assert(link); |
1788 | ||
5da8149f | 1789 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
1746cf2a TG |
1790 | return 1; |
1791 | ||
1c4baffc | 1792 | r = sd_netlink_message_get_errno(m); |
6a7a4e4d | 1793 | if (r < 0) |
26d6b214 | 1794 | /* we warn but don't fail the link, as it may be brought up later */ |
5ecb131d | 1795 | log_link_message_warning_errno(link, m, r, "Could not bring up interface"); |
45ad2c13 | 1796 | |
f882c247 TG |
1797 | return 1; |
1798 | } | |
1799 | ||
1cc84f3b | 1800 | static int link_up(Link *link) { |
4afd3348 | 1801 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
f579559b TG |
1802 | int r; |
1803 | ||
f882c247 | 1804 | assert(link); |
c106cc36 | 1805 | assert(link->network); |
f882c247 TG |
1806 | assert(link->manager); |
1807 | assert(link->manager->rtnl); | |
1808 | ||
6a7a4e4d | 1809 | log_link_debug(link, "Bringing link up"); |
449f7554 | 1810 | |
6a7a4e4d LP |
1811 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1812 | if (r < 0) | |
1813 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
f579559b | 1814 | |
2b2d8603 | 1815 | /* set it free if not enslaved with networkd */ |
6cb955c6 | 1816 | if (!link->network->bridge && !link->network->bond && !link->network->vrf) { |
2b2d8603 TY |
1817 | r = sd_netlink_message_append_u32(req, IFLA_MASTER, 0); |
1818 | if (r < 0) | |
1819 | return log_link_error_errno(link, r, "Could not append IFLA_MASTER attribute: %m"); | |
1820 | } | |
1821 | ||
5d4795f3 | 1822 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); |
6a7a4e4d LP |
1823 | if (r < 0) |
1824 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
fc25d7f8 | 1825 | |
c106cc36 | 1826 | if (link->network->mac) { |
1c4baffc | 1827 | r = sd_netlink_message_append_ether_addr(req, IFLA_ADDRESS, link->network->mac); |
6a7a4e4d LP |
1828 | if (r < 0) |
1829 | return log_link_error_errno(link, r, "Could not set MAC address: %m"); | |
c106cc36 TG |
1830 | } |
1831 | ||
302a796f YW |
1832 | r = netlink_call_async(link->manager->rtnl, NULL, req, link_up_handler, |
1833 | link_netlink_destroy_callback, link); | |
6a7a4e4d LP |
1834 | if (r < 0) |
1835 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
f579559b | 1836 | |
b226d99b TG |
1837 | link_ref(link); |
1838 | ||
f882c247 TG |
1839 | return 0; |
1840 | } | |
1841 | ||
302a796f | 1842 | static int link_down_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
0d4ad91d AR |
1843 | int r; |
1844 | ||
1845 | assert(link); | |
1846 | ||
1847 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1848 | return 1; | |
1849 | ||
1c4baffc | 1850 | r = sd_netlink_message_get_errno(m); |
0d4ad91d | 1851 | if (r < 0) |
5ecb131d | 1852 | log_link_message_warning_errno(link, m, r, "Could not bring down interface"); |
0d4ad91d AR |
1853 | |
1854 | return 1; | |
1855 | } | |
1856 | ||
8e54db83 | 1857 | int link_down(Link *link, link_netlink_message_handler_t callback) { |
4afd3348 | 1858 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
0d4ad91d AR |
1859 | int r; |
1860 | ||
1861 | assert(link); | |
1862 | assert(link->manager); | |
1863 | assert(link->manager->rtnl); | |
1864 | ||
6a7a4e4d | 1865 | log_link_debug(link, "Bringing link down"); |
0d4ad91d AR |
1866 | |
1867 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, | |
1868 | RTM_SETLINK, link->ifindex); | |
6a7a4e4d LP |
1869 | if (r < 0) |
1870 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
0d4ad91d AR |
1871 | |
1872 | r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP); | |
6a7a4e4d LP |
1873 | if (r < 0) |
1874 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
0d4ad91d | 1875 | |
8e54db83 YW |
1876 | r = netlink_call_async(link->manager->rtnl, NULL, req, |
1877 | callback ?: link_down_handler, | |
302a796f | 1878 | link_netlink_destroy_callback, link); |
6a7a4e4d LP |
1879 | if (r < 0) |
1880 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
0d4ad91d AR |
1881 | |
1882 | link_ref(link); | |
1883 | ||
1884 | return 0; | |
1885 | } | |
1886 | ||
89fe6535 SS |
1887 | static int link_group_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
1888 | int r; | |
1889 | ||
1890 | assert(link); | |
1891 | ||
1892 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1893 | return 1; | |
1894 | ||
1895 | r = sd_netlink_message_get_errno(m); | |
1896 | if (r < 0) | |
1897 | log_link_message_warning_errno(link, m, r, "Could not set group for the interface"); | |
1898 | ||
1899 | return 1; | |
1900 | } | |
1901 | ||
1902 | static int link_set_group(Link *link) { | |
1903 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1904 | int r; | |
1905 | ||
1906 | assert(link); | |
1907 | assert(link->network); | |
1908 | assert(link->manager); | |
1909 | assert(link->manager->rtnl); | |
1910 | ||
1911 | if (link->network->group <= 0) | |
1912 | return 0; | |
1913 | ||
1914 | log_link_debug(link, "Setting group"); | |
1915 | ||
1916 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); | |
1917 | if (r < 0) | |
1918 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
1919 | ||
1920 | r = sd_netlink_message_append_u32(req, IFLA_GROUP, link->network->group); | |
1921 | if (r < 0) | |
1922 | return log_link_error_errno(link, r, "Could not set link group: %m"); | |
1923 | ||
1924 | r = netlink_call_async(link->manager->rtnl, NULL, req, link_group_handler, | |
1925 | link_netlink_destroy_callback, link); | |
1926 | if (r < 0) | |
1927 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1928 | ||
1929 | link_ref(link); | |
1930 | ||
1931 | return 0; | |
1932 | } | |
1933 | ||
0d4ad91d AR |
1934 | static int link_handle_bound_to_list(Link *link) { |
1935 | Link *l; | |
1936 | Iterator i; | |
1937 | int r; | |
1938 | bool required_up = false; | |
1939 | bool link_is_up = false; | |
1940 | ||
1941 | assert(link); | |
1942 | ||
1943 | if (hashmap_isempty(link->bound_to_links)) | |
1944 | return 0; | |
1945 | ||
1946 | if (link->flags & IFF_UP) | |
1947 | link_is_up = true; | |
1948 | ||
1949 | HASHMAP_FOREACH (l, link->bound_to_links, i) | |
1950 | if (link_has_carrier(l)) { | |
1951 | required_up = true; | |
1952 | break; | |
1953 | } | |
1954 | ||
1955 | if (!required_up && link_is_up) { | |
8e54db83 | 1956 | r = link_down(link, NULL); |
0d4ad91d AR |
1957 | if (r < 0) |
1958 | return r; | |
1959 | } else if (required_up && !link_is_up) { | |
1960 | r = link_up(link); | |
1961 | if (r < 0) | |
1962 | return r; | |
1963 | } | |
1964 | ||
1965 | return 0; | |
1966 | } | |
1967 | ||
1968 | static int link_handle_bound_by_list(Link *link) { | |
1969 | Iterator i; | |
1970 | Link *l; | |
1971 | int r; | |
1972 | ||
1973 | assert(link); | |
1974 | ||
1975 | if (hashmap_isempty(link->bound_by_links)) | |
1976 | return 0; | |
1977 | ||
1978 | HASHMAP_FOREACH (l, link->bound_by_links, i) { | |
1979 | r = link_handle_bound_to_list(l); | |
1980 | if (r < 0) | |
1981 | return r; | |
1982 | } | |
1983 | ||
1984 | return 0; | |
1985 | } | |
1986 | ||
1987 | static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) { | |
1988 | int r; | |
1989 | ||
1990 | assert(link); | |
1991 | assert(carrier); | |
1992 | ||
1993 | if (link == carrier) | |
1994 | return 0; | |
1995 | ||
1996 | if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex))) | |
1997 | return 0; | |
1998 | ||
1999 | r = hashmap_ensure_allocated(h, NULL); | |
2000 | if (r < 0) | |
2001 | return r; | |
2002 | ||
2003 | r = hashmap_put(*h, INT_TO_PTR(carrier->ifindex), carrier); | |
2004 | if (r < 0) | |
2005 | return r; | |
2006 | ||
2007 | return 0; | |
2008 | } | |
2009 | ||
2010 | static int link_new_bound_by_list(Link *link) { | |
2011 | Manager *m; | |
2012 | Link *carrier; | |
2013 | Iterator i; | |
2014 | int r; | |
2015 | bool list_updated = false; | |
2016 | ||
2017 | assert(link); | |
2018 | assert(link->manager); | |
2019 | ||
2020 | m = link->manager; | |
2021 | ||
b295beea | 2022 | HASHMAP_FOREACH(carrier, m->links, i) { |
0d4ad91d AR |
2023 | if (!carrier->network) |
2024 | continue; | |
2025 | ||
2026 | if (strv_isempty(carrier->network->bind_carrier)) | |
2027 | continue; | |
2028 | ||
191a3f16 | 2029 | if (strv_fnmatch(carrier->network->bind_carrier, link->ifname)) { |
0d4ad91d AR |
2030 | r = link_put_carrier(link, carrier, &link->bound_by_links); |
2031 | if (r < 0) | |
2032 | return r; | |
2033 | ||
2034 | list_updated = true; | |
2035 | } | |
2036 | } | |
2037 | ||
2038 | if (list_updated) | |
84de38c5 | 2039 | link_dirty(link); |
0d4ad91d | 2040 | |
b295beea | 2041 | HASHMAP_FOREACH(carrier, link->bound_by_links, i) { |
0d4ad91d AR |
2042 | r = link_put_carrier(carrier, link, &carrier->bound_to_links); |
2043 | if (r < 0) | |
2044 | return r; | |
2045 | ||
84de38c5 | 2046 | link_dirty(carrier); |
0d4ad91d AR |
2047 | } |
2048 | ||
2049 | return 0; | |
2050 | } | |
2051 | ||
2052 | static int link_new_bound_to_list(Link *link) { | |
2053 | Manager *m; | |
2054 | Link *carrier; | |
2055 | Iterator i; | |
2056 | int r; | |
2057 | bool list_updated = false; | |
2058 | ||
2059 | assert(link); | |
2060 | assert(link->manager); | |
2061 | ||
2062 | if (!link->network) | |
2063 | return 0; | |
2064 | ||
2065 | if (strv_isempty(link->network->bind_carrier)) | |
2066 | return 0; | |
2067 | ||
2068 | m = link->manager; | |
2069 | ||
2070 | HASHMAP_FOREACH (carrier, m->links, i) { | |
191a3f16 | 2071 | if (strv_fnmatch(link->network->bind_carrier, carrier->ifname)) { |
0d4ad91d AR |
2072 | r = link_put_carrier(link, carrier, &link->bound_to_links); |
2073 | if (r < 0) | |
2074 | return r; | |
2075 | ||
2076 | list_updated = true; | |
2077 | } | |
2078 | } | |
2079 | ||
2080 | if (list_updated) | |
84de38c5 | 2081 | link_dirty(link); |
0d4ad91d AR |
2082 | |
2083 | HASHMAP_FOREACH (carrier, link->bound_to_links, i) { | |
2084 | r = link_put_carrier(carrier, link, &carrier->bound_by_links); | |
2085 | if (r < 0) | |
2086 | return r; | |
2087 | ||
84de38c5 | 2088 | link_dirty(carrier); |
0d4ad91d AR |
2089 | } |
2090 | ||
2091 | return 0; | |
2092 | } | |
2093 | ||
2094 | static int link_new_carrier_maps(Link *link) { | |
2095 | int r; | |
2096 | ||
2097 | r = link_new_bound_by_list(link); | |
2098 | if (r < 0) | |
2099 | return r; | |
2100 | ||
2101 | r = link_handle_bound_by_list(link); | |
2102 | if (r < 0) | |
2103 | return r; | |
2104 | ||
2105 | r = link_new_bound_to_list(link); | |
2106 | if (r < 0) | |
2107 | return r; | |
2108 | ||
2109 | r = link_handle_bound_to_list(link); | |
2110 | if (r < 0) | |
2111 | return r; | |
2112 | ||
2113 | return 0; | |
2114 | } | |
2115 | ||
2116 | static void link_free_bound_to_list(Link *link) { | |
2117 | Link *bound_to; | |
2118 | Iterator i; | |
2119 | ||
2120 | HASHMAP_FOREACH (bound_to, link->bound_to_links, i) { | |
2121 | hashmap_remove(link->bound_to_links, INT_TO_PTR(bound_to->ifindex)); | |
2122 | ||
2123 | if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex))) | |
84de38c5 | 2124 | link_dirty(bound_to); |
0d4ad91d AR |
2125 | } |
2126 | ||
2127 | return; | |
2128 | } | |
2129 | ||
2130 | static void link_free_bound_by_list(Link *link) { | |
2131 | Link *bound_by; | |
2132 | Iterator i; | |
2133 | ||
2134 | HASHMAP_FOREACH (bound_by, link->bound_by_links, i) { | |
2135 | hashmap_remove(link->bound_by_links, INT_TO_PTR(bound_by->ifindex)); | |
2136 | ||
2137 | if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) { | |
84de38c5 | 2138 | link_dirty(bound_by); |
0d4ad91d AR |
2139 | link_handle_bound_to_list(bound_by); |
2140 | } | |
2141 | } | |
2142 | ||
2143 | return; | |
2144 | } | |
2145 | ||
2146 | static void link_free_carrier_maps(Link *link) { | |
2147 | bool list_updated = false; | |
2148 | ||
2149 | assert(link); | |
2150 | ||
2151 | if (!hashmap_isempty(link->bound_to_links)) { | |
2152 | link_free_bound_to_list(link); | |
2153 | list_updated = true; | |
2154 | } | |
2155 | ||
2156 | if (!hashmap_isempty(link->bound_by_links)) { | |
2157 | link_free_bound_by_list(link); | |
2158 | list_updated = true; | |
2159 | } | |
2160 | ||
2161 | if (list_updated) | |
84de38c5 | 2162 | link_dirty(link); |
0d4ad91d AR |
2163 | |
2164 | return; | |
2165 | } | |
2166 | ||
5f707e12 YW |
2167 | static int link_append_to_master(Link *link, NetDev *netdev) { |
2168 | Link *master; | |
2169 | int r; | |
2170 | ||
2171 | assert(link); | |
2172 | assert(netdev); | |
2173 | ||
2174 | r = link_get(link->manager, netdev->ifindex, &master); | |
2175 | if (r < 0) | |
2176 | return r; | |
2177 | ||
de7fef4b | 2178 | r = set_ensure_put(&master->slaves, NULL, link); |
38288f0b | 2179 | if (r <= 0) |
5f707e12 YW |
2180 | return r; |
2181 | ||
2182 | link_ref(link); | |
2183 | return 0; | |
2184 | } | |
2185 | ||
2186 | static void link_drop_from_master(Link *link, NetDev *netdev) { | |
2187 | Link *master; | |
2188 | ||
2189 | assert(link); | |
2190 | ||
2191 | if (!link->manager || !netdev) | |
2192 | return; | |
2193 | ||
2194 | if (link_get(link->manager, netdev->ifindex, &master) < 0) | |
2195 | return; | |
2196 | ||
2197 | link_unref(set_remove(master->slaves, link)); | |
2198 | } | |
2199 | ||
2200 | static void link_detach_from_manager(Link *link) { | |
2201 | if (!link || !link->manager) | |
2202 | return; | |
2203 | ||
2204 | link_unref(set_remove(link->manager->links_requesting_uuid, link)); | |
2205 | link_clean(link); | |
2206 | ||
2207 | /* The following must be called at last. */ | |
2208 | assert_se(hashmap_remove(link->manager->links, INT_TO_PTR(link->ifindex)) == link); | |
2209 | link_unref(link); | |
2210 | } | |
2211 | ||
0d4ad91d AR |
2212 | void link_drop(Link *link) { |
2213 | if (!link || link->state == LINK_STATE_LINGER) | |
2214 | return; | |
2215 | ||
2216 | link_set_state(link, LINK_STATE_LINGER); | |
2217 | ||
2218 | link_free_carrier_maps(link); | |
2219 | ||
5f707e12 YW |
2220 | if (link->network) { |
2221 | link_drop_from_master(link, link->network->bridge); | |
2222 | link_drop_from_master(link, link->network->bond); | |
2223 | } | |
2224 | ||
6a7a4e4d | 2225 | log_link_debug(link, "Link removed"); |
0d4ad91d | 2226 | |
db2f8a2e | 2227 | (void) unlink(link->state_file); |
c4397d94 | 2228 | link_detach_from_manager(link); |
0d4ad91d AR |
2229 | } |
2230 | ||
3f265037 | 2231 | static int link_joined(Link *link) { |
f882c247 TG |
2232 | int r; |
2233 | ||
ef1ba606 | 2234 | assert(link); |
f5be5601 | 2235 | assert(link->network); |
dd3efc09 | 2236 | |
0d4ad91d AR |
2237 | if (!hashmap_isempty(link->bound_to_links)) { |
2238 | r = link_handle_bound_to_list(link); | |
2239 | if (r < 0) | |
2240 | return r; | |
2241 | } else if (!(link->flags & IFF_UP)) { | |
505f8da7 TG |
2242 | r = link_up(link); |
2243 | if (r < 0) { | |
2244 | link_enter_failed(link); | |
2245 | return r; | |
2246 | } | |
ef1ba606 | 2247 | } |
f882c247 | 2248 | |
9ed794a3 | 2249 | if (link->network->bridge) { |
e1853b00 | 2250 | r = link_set_bridge(link); |
6a7a4e4d LP |
2251 | if (r < 0) |
2252 | log_link_error_errno(link, r, "Could not set bridge message: %m"); | |
45e11abf YW |
2253 | |
2254 | r = link_append_to_master(link, link->network->bridge); | |
2255 | if (r < 0) | |
2256 | log_link_error_errno(link, r, "Failed to add to bridge master's slave list: %m"); | |
e1853b00 SS |
2257 | } |
2258 | ||
eb64b435 | 2259 | if (link->network->bond) { |
7fcee284 | 2260 | r = link_set_bond(link); |
eb64b435 SS |
2261 | if (r < 0) |
2262 | log_link_error_errno(link, r, "Could not set bond message: %m"); | |
959f65d3 | 2263 | |
45e11abf | 2264 | r = link_append_to_master(link, link->network->bond); |
959f65d3 YW |
2265 | if (r < 0) |
2266 | log_link_error_errno(link, r, "Failed to add to bond master's slave list: %m"); | |
eb64b435 SS |
2267 | } |
2268 | ||
ffff9abe TJ |
2269 | if (link->network->use_br_vlan && |
2270 | (link->network->bridge || streq_ptr("bridge", link->kind))) { | |
13b498f9 TJ |
2271 | r = link_set_bridge_vlan(link); |
2272 | if (r < 0) | |
2273 | log_link_error_errno(link, r, "Could not set bridge vlan: %m"); | |
2274 | } | |
2275 | ||
c1835a42 RM |
2276 | /* Skip setting up addresses until it gets carrier, |
2277 | or it would try to set addresses twice, | |
2278 | which is bad for non-idempotent steps. */ | |
dad2d78e | 2279 | if (!link_has_carrier(link) && !link->network->configure_without_carrier) |
c1835a42 RM |
2280 | return 0; |
2281 | ||
b425c3ae | 2282 | link_set_state(link, LINK_STATE_CONFIGURING); |
289e6774 | 2283 | return link_request_set_addresses(link); |
02b59d57 TG |
2284 | } |
2285 | ||
302a796f | 2286 | static int netdev_join_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
02b59d57 TG |
2287 | int r; |
2288 | ||
1746cf2a | 2289 | assert(link); |
ef1ba606 | 2290 | assert(link->network); |
34bf3c00 | 2291 | assert(link->enslaving > 0); |
02b59d57 | 2292 | |
313cefa1 | 2293 | link->enslaving--; |
52433f6b | 2294 | |
5da8149f | 2295 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
02b59d57 TG |
2296 | return 1; |
2297 | ||
1c4baffc | 2298 | r = sd_netlink_message_get_errno(m); |
856f962c | 2299 | if (r < 0 && r != -EEXIST) { |
5ecb131d | 2300 | log_link_message_warning_errno(link, m, r, "Could not join netdev"); |
ef1ba606 TG |
2301 | link_enter_failed(link); |
2302 | return 1; | |
4ff296b0 YW |
2303 | } |
2304 | ||
2305 | log_link_debug(link, "Joined netdev"); | |
02b59d57 | 2306 | |
34bf3c00 | 2307 | if (link->enslaving == 0) { |
4ff296b0 YW |
2308 | r = link_joined(link); |
2309 | if (r < 0) | |
2310 | link_enter_failed(link); | |
34bf3c00 | 2311 | } |
02b59d57 TG |
2312 | |
2313 | return 1; | |
2314 | } | |
2315 | ||
3f265037 | 2316 | static int link_enter_join_netdev(Link *link) { |
6a0a2f86 | 2317 | NetDev *netdev; |
672682a6 | 2318 | Iterator i; |
02b59d57 TG |
2319 | int r; |
2320 | ||
2321 | assert(link); | |
2322 | assert(link->network); | |
bd08ce56 | 2323 | assert(link->state == LINK_STATE_INITIALIZED); |
02b59d57 | 2324 | |
289e6774 | 2325 | link_set_state(link, LINK_STATE_CONFIGURING); |
02b59d57 | 2326 | |
84de38c5 | 2327 | link_dirty(link); |
34bf3c00 | 2328 | link->enslaving = 0; |
02b59d57 | 2329 | |
d9c67ea1 | 2330 | if (link->network->bond) { |
cbff7170 TJ |
2331 | if (link->network->bond->state == NETDEV_STATE_READY && |
2332 | link->network->bond->ifindex == link->master_ifindex) | |
2333 | return link_joined(link); | |
2334 | ||
f2341e0a LP |
2335 | log_struct(LOG_DEBUG, |
2336 | LOG_LINK_INTERFACE(link), | |
2337 | LOG_NETDEV_INTERFACE(link->network->bond), | |
a1230ff9 | 2338 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bond->ifname)); |
f2341e0a | 2339 | |
34bf3c00 YW |
2340 | link->enslaving++; |
2341 | ||
f2341e0a | 2342 | r = netdev_join(link->network->bond, link, netdev_join_handler); |
52433f6b | 2343 | if (r < 0) { |
f2341e0a LP |
2344 | log_struct_errno(LOG_WARNING, r, |
2345 | LOG_LINK_INTERFACE(link), | |
2346 | LOG_NETDEV_INTERFACE(link->network->bond), | |
a1230ff9 | 2347 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bond->ifname)); |
52433f6b TG |
2348 | link_enter_failed(link); |
2349 | return r; | |
2350 | } | |
0ad6148e MO |
2351 | } |
2352 | ||
d9c67ea1 | 2353 | if (link->network->bridge) { |
f2341e0a LP |
2354 | log_struct(LOG_DEBUG, |
2355 | LOG_LINK_INTERFACE(link), | |
2356 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
a1230ff9 | 2357 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bridge->ifname)); |
f2341e0a | 2358 | |
34bf3c00 YW |
2359 | link->enslaving++; |
2360 | ||
f2341e0a | 2361 | r = netdev_join(link->network->bridge, link, netdev_join_handler); |
0ad6148e | 2362 | if (r < 0) { |
f2341e0a LP |
2363 | log_struct_errno(LOG_WARNING, r, |
2364 | LOG_LINK_INTERFACE(link), | |
2365 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
a1230ff9 | 2366 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bridge->ifname)); |
0ad6148e MO |
2367 | link_enter_failed(link); |
2368 | return r; | |
2369 | } | |
52433f6b TG |
2370 | } |
2371 | ||
6cb955c6 AR |
2372 | if (link->network->vrf) { |
2373 | log_struct(LOG_DEBUG, | |
2374 | LOG_LINK_INTERFACE(link), | |
2375 | LOG_NETDEV_INTERFACE(link->network->vrf), | |
a1230ff9 ZJS |
2376 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->vrf->ifname)); |
2377 | ||
34bf3c00 YW |
2378 | link->enslaving++; |
2379 | ||
6cb955c6 AR |
2380 | r = netdev_join(link->network->vrf, link, netdev_join_handler); |
2381 | if (r < 0) { | |
2382 | log_struct_errno(LOG_WARNING, r, | |
2383 | LOG_LINK_INTERFACE(link), | |
2384 | LOG_NETDEV_INTERFACE(link->network->vrf), | |
a1230ff9 | 2385 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->vrf->ifname)); |
6cb955c6 AR |
2386 | link_enter_failed(link); |
2387 | return r; | |
2388 | } | |
6cb955c6 AR |
2389 | } |
2390 | ||
6a0a2f86 | 2391 | HASHMAP_FOREACH(netdev, link->network->stacked_netdevs, i) { |
7951dea2 | 2392 | |
2b6db913 YW |
2393 | if (netdev->ifindex > 0) |
2394 | /* Assume already enslaved. */ | |
a63e5daa | 2395 | continue; |
a63e5daa | 2396 | |
7033af49 YW |
2397 | if (netdev_get_create_type(netdev) != NETDEV_CREATE_STACKED) |
2398 | continue; | |
2399 | ||
f2341e0a LP |
2400 | log_struct(LOG_DEBUG, |
2401 | LOG_LINK_INTERFACE(link), | |
2402 | LOG_NETDEV_INTERFACE(netdev), | |
a1230ff9 | 2403 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", netdev->ifname)); |
f2341e0a | 2404 | |
34bf3c00 YW |
2405 | link->enslaving++; |
2406 | ||
f2341e0a | 2407 | r = netdev_join(netdev, link, netdev_join_handler); |
7951dea2 | 2408 | if (r < 0) { |
f2341e0a LP |
2409 | log_struct_errno(LOG_WARNING, r, |
2410 | LOG_LINK_INTERFACE(link), | |
2411 | LOG_NETDEV_INTERFACE(netdev), | |
a1230ff9 | 2412 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", netdev->ifname)); |
326cb406 SS |
2413 | link_enter_failed(link); |
2414 | return r; | |
2415 | } | |
326cb406 SS |
2416 | } |
2417 | ||
34bf3c00 YW |
2418 | if (link->enslaving == 0) |
2419 | return link_joined(link); | |
2420 | ||
ef1ba606 TG |
2421 | return 0; |
2422 | } | |
2423 | ||
769d324c | 2424 | static int link_set_ipv4_forward(Link *link) { |
5a8bcb67 LP |
2425 | int r; |
2426 | ||
765afd5c | 2427 | if (!link_ipv4_forward_enabled(link)) |
15dee3f0 LP |
2428 | return 0; |
2429 | ||
765afd5c LP |
2430 | /* We propagate the forwarding flag from one interface to the |
2431 | * global setting one way. This means: as long as at least one | |
2432 | * interface was configured at any time that had IP forwarding | |
2433 | * enabled the setting will stay on for good. We do this | |
2434 | * primarily to keep IPv4 and IPv6 packet forwarding behaviour | |
2435 | * somewhat in sync (see below). */ | |
15dee3f0 | 2436 | |
62e021a9 | 2437 | r = sysctl_write_ip_property(AF_INET, NULL, "ip_forward", "1"); |
eb3da901 | 2438 | if (r < 0) |
765afd5c | 2439 | log_link_warning_errno(link, r, "Cannot turn on IPv4 packet forwarding, ignoring: %m"); |
43c6d5ab | 2440 | |
769d324c LP |
2441 | return 0; |
2442 | } | |
2443 | ||
2444 | static int link_set_ipv6_forward(Link *link) { | |
769d324c LP |
2445 | int r; |
2446 | ||
765afd5c | 2447 | if (!link_ipv6_forward_enabled(link)) |
8add5f79 NO |
2448 | return 0; |
2449 | ||
61233823 | 2450 | /* On Linux, the IPv6 stack does not know a per-interface |
765afd5c LP |
2451 | * packet forwarding setting: either packet forwarding is on |
2452 | * for all, or off for all. We hence don't bother with a | |
2453 | * per-interface setting, but simply propagate the interface | |
2454 | * flag, if it is set, to the global flag, one-way. Note that | |
2455 | * while IPv4 would allow a per-interface flag, we expose the | |
2456 | * same behaviour there and also propagate the setting from | |
2457 | * one to all, to keep things simple (see above). */ | |
15dee3f0 | 2458 | |
62e021a9 | 2459 | r = sysctl_write_ip_property(AF_INET6, "all", "forwarding", "1"); |
eb3da901 | 2460 | if (r < 0) |
765afd5c | 2461 | log_link_warning_errno(link, r, "Cannot configure IPv6 packet forwarding, ignoring: %m"); |
5a8bcb67 LP |
2462 | |
2463 | return 0; | |
2464 | } | |
2465 | ||
49092e22 | 2466 | static int link_set_ipv6_privacy_extensions(Link *link) { |
1f0d9695 | 2467 | IPv6PrivacyExtensions s; |
49092e22 SS |
2468 | int r; |
2469 | ||
1f0d9695 | 2470 | s = link_ipv6_privacy_extensions(link); |
66a6bd68 | 2471 | if (s < 0) |
49092e22 SS |
2472 | return 0; |
2473 | ||
62e021a9 | 2474 | r = sysctl_write_ip_property_int(AF_INET6, link->ifname, "use_tempaddr", (int) link->network->ipv6_privacy_extensions); |
eb3da901 | 2475 | if (r < 0) |
49092e22 SS |
2476 | log_link_warning_errno(link, r, "Cannot configure IPv6 privacy extension for interface: %m"); |
2477 | ||
2478 | return 0; | |
2479 | } | |
2480 | ||
4f2e437a | 2481 | static int link_set_ipv6_accept_ra(Link *link) { |
4f2e437a SS |
2482 | int r; |
2483 | ||
2484 | /* Make this a NOP if IPv6 is not available */ | |
2485 | if (!socket_ipv6_is_supported()) | |
2486 | return 0; | |
2487 | ||
2488 | if (link->flags & IFF_LOOPBACK) | |
2489 | return 0; | |
2490 | ||
d68e2e59 LP |
2491 | if (!link->network) |
2492 | return 0; | |
2493 | ||
62e021a9 | 2494 | r = sysctl_write_ip_property(AF_INET6, link->ifname, "accept_ra", "0"); |
eb3da901 | 2495 | if (r < 0) |
fe307276 | 2496 | log_link_warning_errno(link, r, "Cannot disable kernel IPv6 accept_ra for interface: %m"); |
4f2e437a SS |
2497 | |
2498 | return 0; | |
2499 | } | |
2500 | ||
8749cbcd | 2501 | static int link_set_ipv6_dad_transmits(Link *link) { |
8749cbcd SS |
2502 | int r; |
2503 | ||
2504 | /* Make this a NOP if IPv6 is not available */ | |
2505 | if (!socket_ipv6_is_supported()) | |
2506 | return 0; | |
2507 | ||
2508 | if (link->flags & IFF_LOOPBACK) | |
2509 | return 0; | |
2510 | ||
d68e2e59 LP |
2511 | if (!link->network) |
2512 | return 0; | |
2513 | ||
8749cbcd SS |
2514 | if (link->network->ipv6_dad_transmits < 0) |
2515 | return 0; | |
2516 | ||
62e021a9 | 2517 | r = sysctl_write_ip_property_int(AF_INET6, link->ifname, "dad_transmits", link->network->ipv6_dad_transmits); |
eb3da901 | 2518 | if (r < 0) |
8749cbcd | 2519 | log_link_warning_errno(link, r, "Cannot set IPv6 dad transmits for interface: %m"); |
8749cbcd SS |
2520 | |
2521 | return 0; | |
2522 | } | |
2523 | ||
b69c3180 | 2524 | static int link_set_ipv6_hop_limit(Link *link) { |
b69c3180 SS |
2525 | int r; |
2526 | ||
2527 | /* Make this a NOP if IPv6 is not available */ | |
2528 | if (!socket_ipv6_is_supported()) | |
2529 | return 0; | |
2530 | ||
2531 | if (link->flags & IFF_LOOPBACK) | |
2532 | return 0; | |
2533 | ||
d68e2e59 LP |
2534 | if (!link->network) |
2535 | return 0; | |
2536 | ||
b69c3180 SS |
2537 | if (link->network->ipv6_hop_limit < 0) |
2538 | return 0; | |
2539 | ||
62e021a9 | 2540 | r = sysctl_write_ip_property_int(AF_INET6, link->ifname, "hop_limit", link->network->ipv6_hop_limit); |
eb3da901 | 2541 | if (r < 0) |
b69c3180 | 2542 | log_link_warning_errno(link, r, "Cannot set IPv6 hop limit for interface: %m"); |
b69c3180 SS |
2543 | |
2544 | return 0; | |
2545 | } | |
2546 | ||
11102cba | 2547 | static int link_set_ipv6_mtu(Link *link) { |
11102cba SS |
2548 | int r; |
2549 | ||
2550 | /* Make this a NOP if IPv6 is not available */ | |
2551 | if (!socket_ipv6_is_supported()) | |
2552 | return 0; | |
2553 | ||
2554 | if (link->flags & IFF_LOOPBACK) | |
2555 | return 0; | |
2556 | ||
2557 | if (link->network->ipv6_mtu == 0) | |
2558 | return 0; | |
2559 | ||
d236718c DS |
2560 | /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes |
2561 | * on the interface. Bump up IPv6 MTU bytes to IPV6_MTU_MIN. */ | |
2562 | if (link->network->ipv6_mtu < IPV6_MIN_MTU) { | |
2563 | log_link_notice(link, "Bumping IPv6 MTU to "STRINGIFY(IPV6_MIN_MTU)" byte minimum required"); | |
2564 | link->network->ipv6_mtu = IPV6_MIN_MTU; | |
2565 | } | |
2566 | ||
62e021a9 | 2567 | r = sysctl_write_ip_property_uint32(AF_INET6, link->ifname, "mtu", link->network->ipv6_mtu); |
d236718c DS |
2568 | if (r < 0) { |
2569 | if (link->mtu < link->network->ipv6_mtu) | |
2570 | log_link_warning(link, "Cannot set IPv6 MTU %"PRIu32" higher than device MTU %"PRIu32, | |
2571 | link->network->ipv6_mtu, link->mtu); | |
2572 | else | |
2573 | log_link_warning_errno(link, r, "Cannot set IPv6 MTU for interface: %m"); | |
2574 | } | |
2575 | ||
2576 | link->ipv6_mtu_set = true; | |
11102cba SS |
2577 | |
2578 | return 0; | |
2579 | } | |
2580 | ||
94d76d07 YW |
2581 | static int link_set_ipv4_accept_local(Link *link) { |
2582 | int r; | |
2583 | ||
2584 | if (link->flags & IFF_LOOPBACK) | |
2585 | return 0; | |
2586 | ||
2587 | if (link->network->ipv4_accept_local < 0) | |
2588 | return 0; | |
2589 | ||
2590 | r = sysctl_write_ip_property_boolean(AF_INET, link->ifname, "accept_local", link->network->ipv4_accept_local); | |
2591 | if (r < 0) | |
2592 | log_link_warning_errno(link, r, "Cannot set IPv4 accept_local flag for interface: %m"); | |
2593 | ||
2594 | return 0; | |
2595 | } | |
2596 | ||
30226d27 TJ |
2597 | static bool link_is_static_address_configured(Link *link, Address *address) { |
2598 | Address *net_address; | |
2599 | ||
2600 | assert(link); | |
2601 | assert(address); | |
2602 | ||
2603 | if (!link->network) | |
2604 | return false; | |
2605 | ||
2606 | LIST_FOREACH(addresses, net_address, link->network->static_addresses) | |
2607 | if (address_equal(net_address, address)) | |
2608 | return true; | |
2609 | ||
2610 | return false; | |
2611 | } | |
2612 | ||
d1bdafd2 WKI |
2613 | static bool link_is_neighbor_configured(Link *link, Neighbor *neighbor) { |
2614 | Neighbor *net_neighbor; | |
2615 | ||
2616 | assert(link); | |
2617 | assert(neighbor); | |
2618 | ||
2619 | if (!link->network) | |
2620 | return false; | |
2621 | ||
2622 | LIST_FOREACH(neighbors, net_neighbor, link->network->neighbors) | |
2623 | if (neighbor_equal(net_neighbor, neighbor)) | |
2624 | return true; | |
2625 | ||
2626 | return false; | |
2627 | } | |
2628 | ||
7ecf0c3e TJ |
2629 | static bool link_is_static_route_configured(Link *link, Route *route) { |
2630 | Route *net_route; | |
2631 | ||
2632 | assert(link); | |
2633 | assert(route); | |
2634 | ||
2635 | if (!link->network) | |
2636 | return false; | |
2637 | ||
2638 | LIST_FOREACH(routes, net_route, link->network->static_routes) | |
2639 | if (route_equal(net_route, route)) | |
2640 | return true; | |
2641 | ||
2642 | return false; | |
2643 | } | |
2644 | ||
db51778f YW |
2645 | static bool link_address_is_dynamic(Link *link, Address *address) { |
2646 | Route *route; | |
2647 | Iterator i; | |
2648 | ||
2649 | assert(link); | |
2650 | assert(address); | |
2651 | ||
2652 | if (address->cinfo.ifa_prefered != CACHE_INFO_INFINITY_LIFE_TIME) | |
2653 | return true; | |
2654 | ||
2655 | /* Even when the address is leased from a DHCP server, networkd assign the address | |
2656 | * without lifetime when KeepConfiguration=dhcp. So, let's check that we have | |
2657 | * corresponding routes with RTPROT_DHCP. */ | |
2658 | SET_FOREACH(route, link->routes_foreign, i) { | |
2659 | if (route->protocol != RTPROT_DHCP) | |
2660 | continue; | |
2661 | ||
2662 | if (address->family != route->family) | |
2663 | continue; | |
2664 | ||
2665 | if (in_addr_equal(address->family, &address->in_addr, &route->prefsrc)) | |
2666 | return true; | |
2667 | } | |
2668 | ||
2669 | return false; | |
2670 | } | |
2671 | ||
0917a271 DS |
2672 | static int link_enumerate_ipv6_tentative_addresses(Link *link) { |
2673 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; | |
2674 | sd_netlink_message *addr; | |
2675 | int r; | |
2676 | ||
2677 | assert(link); | |
2678 | assert(link->manager); | |
2679 | assert(link->manager->rtnl); | |
2680 | ||
2681 | r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_GETADDR, 0, AF_INET6); | |
2682 | if (r < 0) | |
2683 | return r; | |
2684 | ||
2685 | r = sd_netlink_call(link->manager->rtnl, req, 0, &reply); | |
2686 | if (r < 0) | |
2687 | return r; | |
2688 | ||
2689 | for (addr = reply; addr; addr = sd_netlink_message_next(addr)) { | |
2690 | unsigned char flags; | |
2691 | int ifindex; | |
2692 | ||
2693 | r = sd_rtnl_message_addr_get_ifindex(addr, &ifindex); | |
2694 | if (r < 0) { | |
2695 | log_link_warning_errno(link, r, "rtnl: invalid ifindex, ignoring: %m"); | |
2696 | continue; | |
2697 | } else if (link->ifindex != ifindex) | |
2698 | continue; | |
2699 | ||
2700 | r = sd_rtnl_message_addr_get_flags(addr, &flags); | |
2701 | if (r < 0) { | |
2702 | log_link_warning_errno(link, r, "rtnl: received address message with invalid flags, ignoring: %m"); | |
2703 | continue; | |
2704 | } else if (!(flags & IFA_F_TENTATIVE)) | |
2705 | continue; | |
2706 | ||
2707 | log_link_debug(link, "Found tentative ipv6 link-local address"); | |
2708 | (void) manager_rtnl_process_address(link->manager->rtnl, addr, link->manager); | |
2709 | } | |
2710 | ||
2711 | return 0; | |
2712 | } | |
2713 | ||
5e5b137a TG |
2714 | static int link_drop_foreign_config(Link *link) { |
2715 | Address *address; | |
d1bdafd2 | 2716 | Neighbor *neighbor; |
5e5b137a TG |
2717 | Route *route; |
2718 | Iterator i; | |
2719 | int r; | |
2720 | ||
0917a271 DS |
2721 | /* The kernel doesn't notify us about tentative addresses; |
2722 | * so if ipv6ll is disabled, we need to enumerate them now so we can drop them below */ | |
2723 | if (!link_ipv6ll_enabled(link)) { | |
2724 | r = link_enumerate_ipv6_tentative_addresses(link); | |
2725 | if (r < 0) | |
2726 | return r; | |
2727 | } | |
2728 | ||
5e5b137a | 2729 | SET_FOREACH(address, link->addresses_foreign, i) { |
fe307276 | 2730 | /* we consider IPv6LL addresses to be managed by the kernel */ |
5539fc0f | 2731 | if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1 && link_ipv6ll_enabled(link)) |
5e5b137a TG |
2732 | continue; |
2733 | ||
db51778f | 2734 | if (link_address_is_dynamic(link, address)) { |
b1b0b42e | 2735 | if (link->network && FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) |
db51778f | 2736 | continue; |
b1b0b42e | 2737 | } else if (link->network && FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC)) |
db51778f YW |
2738 | continue; |
2739 | ||
30226d27 TJ |
2740 | if (link_is_static_address_configured(link, address)) { |
2741 | r = address_add(link, address->family, &address->in_addr, address->prefixlen, NULL); | |
2742 | if (r < 0) | |
2743 | return log_link_error_errno(link, r, "Failed to add address: %m"); | |
2744 | } else { | |
63ae0569 | 2745 | r = address_remove(address, link, NULL); |
30226d27 TJ |
2746 | if (r < 0) |
2747 | return r; | |
2748 | } | |
5e5b137a TG |
2749 | } |
2750 | ||
d1bdafd2 WKI |
2751 | SET_FOREACH(neighbor, link->neighbors_foreign, i) { |
2752 | if (link_is_neighbor_configured(link, neighbor)) { | |
2753 | r = neighbor_add(link, neighbor->family, &neighbor->in_addr, &neighbor->lladdr, neighbor->lladdr_size, NULL); | |
2754 | if (r < 0) | |
2755 | return r; | |
2756 | } else { | |
2757 | r = neighbor_remove(neighbor, link, NULL); | |
2758 | if (r < 0) | |
2759 | return r; | |
2760 | } | |
2761 | } | |
2762 | ||
5e5b137a | 2763 | SET_FOREACH(route, link->routes_foreign, i) { |
fe307276 | 2764 | /* do not touch routes managed by the kernel */ |
5e5b137a TG |
2765 | if (route->protocol == RTPROT_KERNEL) |
2766 | continue; | |
2767 | ||
bd7d6cec YW |
2768 | /* do not touch multicast route added by kernel */ |
2769 | /* FIXME: Why the kernel adds this route with protocol RTPROT_BOOT??? We need to investigate that. | |
2770 | * https://tools.ietf.org/html/rfc4862#section-5.4 may explain why. */ | |
2771 | if (route->protocol == RTPROT_BOOT && | |
2772 | route->family == AF_INET6 && | |
2773 | route->dst_prefixlen == 8 && | |
2774 | in_addr_equal(AF_INET6, &route->dst, &(union in_addr_union) { .in6 = {{{ 0xff,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }}} })) | |
2775 | continue; | |
2776 | ||
b1b0b42e | 2777 | if (route->protocol == RTPROT_STATIC && link->network && |
db51778f YW |
2778 | FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC)) |
2779 | continue; | |
2780 | ||
b1b0b42e | 2781 | if (route->protocol == RTPROT_DHCP && link->network && |
db51778f YW |
2782 | FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) |
2783 | continue; | |
2784 | ||
7ecf0c3e | 2785 | if (link_is_static_route_configured(link, route)) { |
f1368755 | 2786 | r = route_add(link, route, NULL); |
7ecf0c3e TJ |
2787 | if (r < 0) |
2788 | return r; | |
2789 | } else { | |
4645ad47 | 2790 | r = route_remove(route, link, NULL); |
7ecf0c3e TJ |
2791 | if (r < 0) |
2792 | return r; | |
2793 | } | |
5e5b137a TG |
2794 | } |
2795 | ||
2796 | return 0; | |
2797 | } | |
2798 | ||
3104883d | 2799 | static int link_drop_config(Link *link) { |
410a7f15 | 2800 | Address *address, *pool_address; |
d1bdafd2 | 2801 | Neighbor *neighbor; |
3104883d SS |
2802 | Route *route; |
2803 | Iterator i; | |
2804 | int r; | |
2805 | ||
2806 | SET_FOREACH(address, link->addresses, i) { | |
2807 | /* we consider IPv6LL addresses to be managed by the kernel */ | |
5539fc0f | 2808 | if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1 && link_ipv6ll_enabled(link)) |
3104883d SS |
2809 | continue; |
2810 | ||
63ae0569 | 2811 | r = address_remove(address, link, NULL); |
3104883d SS |
2812 | if (r < 0) |
2813 | return r; | |
410a7f15 RM |
2814 | |
2815 | /* If this address came from an address pool, clean up the pool */ | |
2816 | LIST_FOREACH(addresses, pool_address, link->pool_addresses) { | |
2817 | if (address_equal(address, pool_address)) { | |
2818 | LIST_REMOVE(addresses, link->pool_addresses, pool_address); | |
2819 | address_free(pool_address); | |
2820 | break; | |
2821 | } | |
2822 | } | |
3104883d SS |
2823 | } |
2824 | ||
d1bdafd2 WKI |
2825 | SET_FOREACH(neighbor, link->neighbors, i) { |
2826 | r = neighbor_remove(neighbor, link, NULL); | |
2827 | if (r < 0) | |
2828 | return r; | |
2829 | } | |
2830 | ||
3104883d SS |
2831 | SET_FOREACH(route, link->routes, i) { |
2832 | /* do not touch routes managed by the kernel */ | |
2833 | if (route->protocol == RTPROT_KERNEL) | |
2834 | continue; | |
2835 | ||
4645ad47 | 2836 | r = route_remove(route, link, NULL); |
3104883d SS |
2837 | if (r < 0) |
2838 | return r; | |
2839 | } | |
2840 | ||
c69305ff LP |
2841 | ndisc_flush(link); |
2842 | ||
3104883d SS |
2843 | return 0; |
2844 | } | |
2845 | ||
051e77ca SS |
2846 | static int link_configure_ipv4_dad(Link *link) { |
2847 | Address *address; | |
2848 | int r; | |
2849 | ||
2850 | assert(link); | |
2851 | assert(link->network); | |
2852 | ||
2853 | LIST_FOREACH(addresses, address, link->network->static_addresses) | |
2854 | if (address->family == AF_INET && | |
2855 | FLAGS_SET(address->duplicate_address_detection, ADDRESS_FAMILY_IPV4)) { | |
2856 | r = configure_ipv4_duplicate_address_detection(link, address); | |
2857 | if (r < 0) | |
2858 | return log_link_error_errno(link, r, "Failed to configure IPv4ACD: %m"); | |
2859 | } | |
2860 | ||
2861 | return 0; | |
2862 | } | |
2863 | ||
34658df2 YW |
2864 | static int link_configure_traffic_control(Link *link) { |
2865 | TrafficControl *tc; | |
0f5bd7fe | 2866 | Iterator i; |
02b59d57 TG |
2867 | int r; |
2868 | ||
34658df2 YW |
2869 | link->tc_configured = false; |
2870 | link->tc_messages = 0; | |
4ecdcb07 | 2871 | |
34658df2 YW |
2872 | ORDERED_HASHMAP_FOREACH(tc, link->network->tc_by_section, i) { |
2873 | r = traffic_control_configure(link, tc); | |
4ecdcb07 YW |
2874 | if (r < 0) |
2875 | return r; | |
2876 | } | |
2877 | ||
34658df2 YW |
2878 | if (link->tc_messages == 0) |
2879 | link->tc_configured = true; | |
4ecdcb07 | 2880 | else |
34658df2 | 2881 | log_link_debug(link, "Configuring traffic control"); |
4ecdcb07 YW |
2882 | |
2883 | return 0; | |
2884 | } | |
2885 | ||
518cd6b5 SS |
2886 | static int link_configure_sr_iov(Link *link) { |
2887 | SRIOV *sr_iov; | |
2888 | Iterator i; | |
2889 | int r; | |
2890 | ||
2891 | link->sr_iov_configured = false; | |
2892 | link->sr_iov_messages = 0; | |
2893 | ||
2894 | ORDERED_HASHMAP_FOREACH(sr_iov, link->network->sr_iov_by_section, i) { | |
2895 | r = sr_iov_configure(link, sr_iov); | |
2896 | if (r < 0) | |
2897 | return r; | |
2898 | } | |
2899 | ||
2900 | if (link->sr_iov_messages == 0) | |
2901 | link->sr_iov_configured = true; | |
2902 | else | |
2903 | log_link_debug(link, "Configuring SR-IOV"); | |
2904 | ||
2905 | return 0; | |
2906 | } | |
2907 | ||
4ecdcb07 YW |
2908 | static int link_configure(Link *link) { |
2909 | int r; | |
2910 | ||
ef1ba606 | 2911 | assert(link); |
b22d8a00 | 2912 | assert(link->network); |
bd08ce56 | 2913 | assert(link->state == LINK_STATE_INITIALIZED); |
a748b692 | 2914 | |
34658df2 | 2915 | r = link_configure_traffic_control(link); |
4ecdcb07 YW |
2916 | if (r < 0) |
2917 | return r; | |
2918 | ||
518cd6b5 SS |
2919 | r = link_configure_sr_iov(link); |
2920 | if (r < 0) | |
2921 | return r; | |
2922 | ||
500b96eb | 2923 | if (link->iftype == ARPHRD_CAN) |
93ea7750 | 2924 | return link_configure_can(link); |
92c918b0 | 2925 | |
482efedc SS |
2926 | /* If IPv6 configured that is static IPv6 address and IPv6LL autoconfiguration is enabled |
2927 | * for this interface, then enable IPv6 */ | |
57ad7607 | 2928 | (void) link_update_ipv6_sysctl(link); |
482efedc | 2929 | |
23d8b221 SS |
2930 | r = link_set_proxy_arp(link); |
2931 | if (r < 0) | |
2932 | return r; | |
2933 | ||
a0e5c15d FK |
2934 | r = ipv6_proxy_ndp_addresses_configure(link); |
2935 | if (r < 0) | |
2936 | return r; | |
2937 | ||
769d324c LP |
2938 | r = link_set_ipv4_forward(link); |
2939 | if (r < 0) | |
2940 | return r; | |
2941 | ||
2942 | r = link_set_ipv6_forward(link); | |
5a8bcb67 LP |
2943 | if (r < 0) |
2944 | return r; | |
2945 | ||
49092e22 SS |
2946 | r = link_set_ipv6_privacy_extensions(link); |
2947 | if (r < 0) | |
2948 | return r; | |
2949 | ||
4f2e437a SS |
2950 | r = link_set_ipv6_accept_ra(link); |
2951 | if (r < 0) | |
2952 | return r; | |
2953 | ||
8749cbcd SS |
2954 | r = link_set_ipv6_dad_transmits(link); |
2955 | if (r < 0) | |
2956 | return r; | |
b69c3180 SS |
2957 | |
2958 | r = link_set_ipv6_hop_limit(link); | |
2959 | if (r < 0) | |
2960 | return r; | |
8749cbcd | 2961 | |
94d76d07 YW |
2962 | r = link_set_ipv4_accept_local(link); |
2963 | if (r < 0) | |
2964 | return r; | |
2965 | ||
99d2baa2 SS |
2966 | r = link_set_flags(link); |
2967 | if (r < 0) | |
2968 | return r; | |
2969 | ||
89fe6535 SS |
2970 | r = link_set_group(link); |
2971 | if (r < 0) | |
2972 | return r; | |
2973 | ||
910feb78 | 2974 | if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_IPV4 | ADDRESS_FAMILY_FALLBACK_IPV4)) { |
b22d8a00 | 2975 | r = ipv4ll_configure(link); |
eb34d4af TG |
2976 | if (r < 0) |
2977 | return r; | |
2978 | } | |
2979 | ||
78c958f8 | 2980 | if (link_dhcp4_enabled(link)) { |
64b21ece MV |
2981 | r = dhcp4_set_promote_secondaries(link); |
2982 | if (r < 0) | |
2983 | return r; | |
2984 | ||
3c9b8860 | 2985 | r = dhcp4_configure(link); |
eb34d4af TG |
2986 | if (r < 0) |
2987 | return r; | |
eb34d4af TG |
2988 | } |
2989 | ||
78c958f8 | 2990 | if (link_dhcp4_server_enabled(link)) { |
dd43110f TG |
2991 | r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex); |
2992 | if (r < 0) | |
2993 | return r; | |
2994 | ||
2995 | r = sd_dhcp_server_attach_event(link->dhcp_server, NULL, 0); | |
2996 | if (r < 0) | |
2997 | return r; | |
dd43110f TG |
2998 | } |
2999 | ||
62379e88 TG |
3000 | if (link_dhcp6_enabled(link) || |
3001 | link_ipv6_accept_ra_enabled(link)) { | |
f5a8c43f TG |
3002 | r = dhcp6_configure(link); |
3003 | if (r < 0) | |
3004 | return r; | |
3005 | } | |
3006 | ||
3007 | if (link_ipv6_accept_ra_enabled(link)) { | |
de1e9928 | 3008 | r = ndisc_configure(link); |
4138fb2c PF |
3009 | if (r < 0) |
3010 | return r; | |
3011 | } | |
3012 | ||
7465dd22 PF |
3013 | if (link_radv_enabled(link)) { |
3014 | r = radv_configure(link); | |
3015 | if (r < 0) | |
3016 | return r; | |
3017 | } | |
3018 | ||
8e1ad1ea | 3019 | if (link_lldp_rx_enabled(link)) { |
7f853950 | 3020 | r = link_lldp_rx_configure(link); |
273eec24 LP |
3021 | if (r < 0) |
3022 | return r; | |
ce43e484 SS |
3023 | } |
3024 | ||
f6fcc1c2 | 3025 | r = link_configure_mtu(link); |
40288ece YW |
3026 | if (r < 0) |
3027 | return r; | |
44b598a1 | 3028 | |
9f6e82e6 YW |
3029 | r = link_configure_addrgen_mode(link); |
3030 | if (r < 0) | |
3031 | return r; | |
0e2fdb83 | 3032 | |
051e77ca SS |
3033 | r = link_configure_ipv4_dad(link); |
3034 | if (r < 0) | |
3035 | return r; | |
3036 | ||
3a390124 DS |
3037 | return link_configure_continue(link); |
3038 | } | |
3039 | ||
3040 | /* The configuration continues in this separate function, instead of | |
3041 | * including this in the above link_configure() function, for two | |
3042 | * reasons: | |
3043 | * 1) some devices reset the link when the mtu is set, which caused | |
3044 | * an infinite loop here in networkd; see: | |
3045 | * https://github.com/systemd/systemd/issues/6593 | |
3046 | * https://github.com/systemd/systemd/issues/9831 | |
3047 | * 2) if ipv6ll is disabled, then bringing the interface up must be | |
3048 | * delayed until after we get confirmation from the kernel that | |
3049 | * the addr_gen_mode parameter has been set (via netlink), see: | |
3050 | * https://github.com/systemd/systemd/issues/13882 | |
3051 | */ | |
3052 | static int link_configure_continue(Link *link) { | |
55dc8c4a YW |
3053 | int r; |
3054 | ||
3055 | assert(link); | |
3056 | assert(link->network); | |
bd08ce56 | 3057 | assert(link->state == LINK_STATE_INITIALIZED); |
55dc8c4a | 3058 | |
9524014e | 3059 | if (link->setting_mtu || link->setting_genmode) |
55dc8c4a YW |
3060 | return 0; |
3061 | ||
4c649652 DS |
3062 | /* Drop foreign config, but ignore loopback or critical devices. |
3063 | * We do not want to remove loopback address or addresses used for root NFS. */ | |
3064 | if (!(link->flags & IFF_LOOPBACK) && | |
3065 | link->network->keep_configuration != KEEP_CONFIGURATION_YES) { | |
3066 | r = link_drop_foreign_config(link); | |
3067 | if (r < 0) | |
3068 | return r; | |
3069 | } | |
3070 | ||
d236718c DS |
3071 | /* The kernel resets ipv6 mtu after changing device mtu; |
3072 | * we must set this here, after we've set device mtu */ | |
3073 | r = link_set_ipv6_mtu(link); | |
3074 | if (r < 0) | |
3075 | return r; | |
3076 | ||
dad2d78e | 3077 | if (link_has_carrier(link) || link->network->configure_without_carrier) { |
1e9be60b TG |
3078 | r = link_acquire_conf(link); |
3079 | if (r < 0) | |
3080 | return r; | |
cc544d5f | 3081 | } |
1e9be60b | 3082 | |
3f265037 | 3083 | return link_enter_join_netdev(link); |
505f8da7 TG |
3084 | } |
3085 | ||
27dfc982 YW |
3086 | static int duid_set_uuid(DUID *duid, sd_id128_t uuid) { |
3087 | assert(duid); | |
3088 | ||
3089 | if (duid->raw_data_len > 0) | |
3090 | return 0; | |
3091 | ||
3092 | if (duid->type != DUID_TYPE_UUID) | |
3093 | return -EINVAL; | |
3094 | ||
3095 | memcpy(&duid->raw_data, &uuid, sizeof(sd_id128_t)); | |
3096 | duid->raw_data_len = sizeof(sd_id128_t); | |
3097 | ||
3098 | return 1; | |
3099 | } | |
3100 | ||
3101 | int get_product_uuid_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { | |
3102 | Manager *manager = userdata; | |
3103 | const sd_bus_error *e; | |
3104 | const void *a; | |
3105 | size_t sz; | |
3106 | DUID *duid; | |
3107 | Link *link; | |
3108 | int r; | |
3109 | ||
3110 | assert(m); | |
3111 | assert(manager); | |
3112 | ||
3113 | e = sd_bus_message_get_error(m); | |
3114 | if (e) { | |
3115 | log_error_errno(sd_bus_error_get_errno(e), | |
ab4dd984 | 3116 | "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s", |
27dfc982 YW |
3117 | e->message); |
3118 | goto configure; | |
3119 | } | |
3120 | ||
3121 | r = sd_bus_message_read_array(m, 'y', &a, &sz); | |
3122 | if (r < 0) | |
3123 | goto configure; | |
3124 | ||
3125 | if (sz != sizeof(sd_id128_t)) { | |
ab4dd984 | 3126 | log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID."); |
27dfc982 YW |
3127 | goto configure; |
3128 | } | |
3129 | ||
3130 | memcpy(&manager->product_uuid, a, sz); | |
3131 | while ((duid = set_steal_first(manager->duids_requesting_uuid))) | |
3132 | (void) duid_set_uuid(duid, manager->product_uuid); | |
3133 | ||
3134 | manager->duids_requesting_uuid = set_free(manager->duids_requesting_uuid); | |
3135 | ||
3136 | configure: | |
3137 | while ((link = set_steal_first(manager->links_requesting_uuid))) { | |
3dc2e7af YW |
3138 | link_unref(link); |
3139 | ||
27dfc982 YW |
3140 | r = link_configure(link); |
3141 | if (r < 0) | |
4ff296b0 | 3142 | link_enter_failed(link); |
27dfc982 YW |
3143 | } |
3144 | ||
3145 | manager->links_requesting_uuid = set_free(manager->links_requesting_uuid); | |
3146 | ||
3147 | /* To avoid calling GetProductUUID() bus method so frequently, set the flag below | |
3148 | * even if the method fails. */ | |
3149 | manager->has_product_uuid = true; | |
3150 | ||
3151 | return 1; | |
3152 | } | |
3153 | ||
3154 | static bool link_requires_uuid(Link *link) { | |
3155 | const DUID *duid; | |
3156 | ||
3157 | assert(link); | |
3158 | assert(link->manager); | |
3159 | assert(link->network); | |
3160 | ||
3161 | duid = link_get_duid(link); | |
3162 | if (duid->type != DUID_TYPE_UUID || duid->raw_data_len != 0) | |
3163 | return false; | |
3164 | ||
3165 | if (link_dhcp4_enabled(link) && IN_SET(link->network->dhcp_client_identifier, DHCP_CLIENT_ID_DUID, DHCP_CLIENT_ID_DUID_ONLY)) | |
3166 | return true; | |
3167 | ||
3168 | if (link_dhcp6_enabled(link) || link_ipv6_accept_ra_enabled(link)) | |
3169 | return true; | |
3170 | ||
3171 | return false; | |
3172 | } | |
3173 | ||
3174 | static int link_configure_duid(Link *link) { | |
3175 | Manager *m; | |
3176 | DUID *duid; | |
3177 | int r; | |
3178 | ||
3179 | assert(link); | |
3180 | assert(link->manager); | |
3181 | assert(link->network); | |
3182 | ||
3183 | m = link->manager; | |
3184 | duid = link_get_duid(link); | |
3185 | ||
3186 | if (!link_requires_uuid(link)) | |
3187 | return 1; | |
3188 | ||
3189 | if (m->has_product_uuid) { | |
3190 | (void) duid_set_uuid(duid, m->product_uuid); | |
3191 | return 1; | |
3192 | } | |
3193 | ||
3194 | if (!m->links_requesting_uuid) { | |
3195 | r = manager_request_product_uuid(m, link); | |
3196 | if (r < 0) { | |
3197 | if (r == -ENOMEM) | |
3198 | return r; | |
3199 | ||
ab4dd984 ZJS |
3200 | log_link_warning_errno(link, r, |
3201 | "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m"); | |
27dfc982 YW |
3202 | return 1; |
3203 | } | |
3204 | } else { | |
3205 | r = set_put(m->links_requesting_uuid, link); | |
3206 | if (r < 0) | |
3207 | return log_oom(); | |
39dbd0c7 ZJS |
3208 | if (r > 0) |
3209 | link_ref(link); | |
27dfc982 YW |
3210 | |
3211 | r = set_put(m->duids_requesting_uuid, duid); | |
3212 | if (r < 0) | |
3213 | return log_oom(); | |
3214 | } | |
3215 | ||
3216 | return 0; | |
3217 | } | |
3218 | ||
572b21d9 | 3219 | static int link_reconfigure_internal(Link *link, sd_netlink_message *m, bool force) { |
ad932b15 YW |
3220 | Network *network; |
3221 | int r; | |
3222 | ||
572b21d9 YW |
3223 | if (m) { |
3224 | _cleanup_strv_free_ char **s = NULL; | |
3225 | ||
3226 | r = sd_netlink_message_get_errno(m); | |
3227 | if (r < 0) | |
3228 | return r; | |
3229 | ||
3230 | r = sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &s); | |
3231 | if (r < 0 && r != -ENODATA) | |
3232 | return r; | |
3233 | ||
3234 | strv_free_and_replace(link->alternative_names, s); | |
3235 | } | |
3236 | ||
c643bda5 YW |
3237 | r = network_get(link->manager, link->iftype, link->sd_device, |
3238 | link->ifname, link->alternative_names, link->driver, | |
3239 | &link->mac, &link->permanent_mac, | |
3240 | link->wlan_iftype, link->ssid, &link->bssid, &network); | |
ad932b15 YW |
3241 | if (r == -ENOENT) { |
3242 | link_enter_unmanaged(link); | |
3243 | return 0; | |
3244 | } else if (r == 0 && network->unmanaged) { | |
3245 | link_enter_unmanaged(link); | |
3246 | return 0; | |
3247 | } else if (r < 0) | |
3248 | return r; | |
3249 | ||
99b8517c | 3250 | if (link->network == network && !force) |
ad932b15 YW |
3251 | return 0; |
3252 | ||
3253 | log_link_info(link, "Re-configuring with %s", network->filename); | |
3254 | ||
3255 | /* Dropping old .network file */ | |
3256 | r = link_stop_clients(link, false); | |
572b21d9 | 3257 | if (r < 0) |
ad932b15 | 3258 | return r; |
ad932b15 YW |
3259 | |
3260 | if (link_dhcp4_server_enabled(link)) | |
3261 | (void) sd_dhcp_server_stop(link->dhcp_server); | |
3262 | ||
3263 | r = link_drop_config(link); | |
3264 | if (r < 0) | |
3265 | return r; | |
3266 | ||
2c7b826d | 3267 | if (!IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING, LINK_STATE_INITIALIZED)) { |
ad932b15 YW |
3268 | log_link_debug(link, "State is %s, dropping config", link_state_to_string(link->state)); |
3269 | r = link_drop_foreign_config(link); | |
3270 | if (r < 0) | |
3271 | return r; | |
3272 | } | |
3273 | ||
3274 | link_free_carrier_maps(link); | |
3275 | link_free_engines(link); | |
3276 | link->network = network_unref(link->network); | |
3277 | ||
3278 | /* Then, apply new .network file */ | |
3279 | r = network_apply(network, link); | |
3280 | if (r < 0) | |
3281 | return r; | |
3282 | ||
3283 | r = link_new_carrier_maps(link); | |
3284 | if (r < 0) | |
3285 | return r; | |
3286 | ||
3287 | link_set_state(link, LINK_STATE_INITIALIZED); | |
8ae7b8a1 | 3288 | link_dirty(link); |
ad932b15 YW |
3289 | |
3290 | /* link_configure_duid() returns 0 if it requests product UUID. In that case, | |
3291 | * link_configure() is called later asynchronously. */ | |
3292 | r = link_configure_duid(link); | |
3293 | if (r <= 0) | |
3294 | return r; | |
3295 | ||
3296 | r = link_configure(link); | |
3297 | if (r < 0) | |
3298 | return r; | |
3299 | ||
3300 | return 0; | |
3301 | } | |
3302 | ||
572b21d9 YW |
3303 | static int link_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
3304 | int r; | |
3305 | ||
3306 | r = link_reconfigure_internal(link, m, false); | |
3307 | if (r < 0) | |
3308 | link_enter_failed(link); | |
3309 | ||
3310 | return 1; | |
3311 | } | |
3312 | ||
3313 | static int link_force_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { | |
3314 | int r; | |
3315 | ||
3316 | r = link_reconfigure_internal(link, m, true); | |
3317 | if (r < 0) | |
3318 | link_enter_failed(link); | |
3319 | ||
3320 | return 1; | |
3321 | } | |
3322 | ||
3323 | int link_reconfigure(Link *link, bool force) { | |
3324 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
3325 | int r; | |
3326 | ||
2c0d7ed3 YW |
3327 | if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_LINGER)) |
3328 | return 0; | |
3329 | ||
572b21d9 YW |
3330 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, |
3331 | link->ifindex); | |
3332 | if (r < 0) | |
3333 | return r; | |
3334 | ||
3335 | r = netlink_call_async(link->manager->rtnl, NULL, req, | |
3336 | force ? link_force_reconfigure_handler : link_reconfigure_handler, | |
3337 | link_netlink_destroy_callback, link); | |
3338 | if (r < 0) | |
3339 | return r; | |
3340 | ||
3341 | link_ref(link); | |
3342 | ||
3343 | return 0; | |
3344 | } | |
3345 | ||
e6bf7774 | 3346 | static int link_initialized_and_synced(Link *link) { |
505f8da7 | 3347 | Network *network; |
505f8da7 TG |
3348 | int r; |
3349 | ||
3350 | assert(link); | |
3351 | assert(link->ifname); | |
3352 | assert(link->manager); | |
3353 | ||
bd08ce56 YW |
3354 | /* We may get called either from the asynchronous netlink callback, |
3355 | * or directly for link_add() if running in a container. See link_add(). */ | |
3356 | if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED)) | |
4232cf04 | 3357 | return 0; |
505f8da7 | 3358 | |
6a7a4e4d | 3359 | log_link_debug(link, "Link state is up-to-date"); |
bd08ce56 | 3360 | link_set_state(link, LINK_STATE_INITIALIZED); |
505f8da7 | 3361 | |
0d4ad91d AR |
3362 | r = link_new_bound_by_list(link); |
3363 | if (r < 0) | |
3364 | return r; | |
3365 | ||
3366 | r = link_handle_bound_by_list(link); | |
3367 | if (r < 0) | |
3368 | return r; | |
3369 | ||
c4a03a56 | 3370 | if (!link->network) { |
0894cfe4 | 3371 | r = wifi_get_info(link); |
8d968fdd YW |
3372 | if (r < 0) |
3373 | return r; | |
3374 | ||
c643bda5 YW |
3375 | r = network_get(link->manager, link->iftype, link->sd_device, |
3376 | link->ifname, link->alternative_names, link->driver, | |
3377 | &link->mac, &link->permanent_mac, | |
3378 | link->wlan_iftype, link->ssid, &link->bssid, &network); | |
c4a03a56 TG |
3379 | if (r == -ENOENT) { |
3380 | link_enter_unmanaged(link); | |
4232cf04 | 3381 | return 0; |
a09dc546 DM |
3382 | } else if (r == 0 && network->unmanaged) { |
3383 | link_enter_unmanaged(link); | |
3384 | return 0; | |
c4a03a56 TG |
3385 | } else if (r < 0) |
3386 | return r; | |
505f8da7 | 3387 | |
c4a03a56 TG |
3388 | if (link->flags & IFF_LOOPBACK) { |
3389 | if (network->link_local != ADDRESS_FAMILY_NO) | |
3390 | log_link_debug(link, "Ignoring link-local autoconfiguration for loopback link"); | |
78c958f8 | 3391 | |
c4a03a56 TG |
3392 | if (network->dhcp != ADDRESS_FAMILY_NO) |
3393 | log_link_debug(link, "Ignoring DHCP clients for loopback link"); | |
78c958f8 | 3394 | |
c4a03a56 TG |
3395 | if (network->dhcp_server) |
3396 | log_link_debug(link, "Ignoring DHCP server for loopback link"); | |
3397 | } | |
bd2efe92 | 3398 | |
7d342c03 | 3399 | r = network_apply(network, link); |
c4a03a56 TG |
3400 | if (r < 0) |
3401 | return r; | |
3402 | } | |
505f8da7 | 3403 | |
0d4ad91d AR |
3404 | r = link_new_bound_to_list(link); |
3405 | if (r < 0) | |
3406 | return r; | |
3407 | ||
27dfc982 YW |
3408 | /* link_configure_duid() returns 0 if it requests product UUID. In that case, |
3409 | * link_configure() is called later asynchronously. */ | |
3410 | r = link_configure_duid(link); | |
3411 | if (r <= 0) | |
3412 | return r; | |
3413 | ||
a748b692 TG |
3414 | r = link_configure(link); |
3415 | if (r < 0) | |
3416 | return r; | |
3417 | ||
4232cf04 | 3418 | return 0; |
505f8da7 TG |
3419 | } |
3420 | ||
302a796f | 3421 | static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
572b21d9 | 3422 | _cleanup_strv_free_ char **s = NULL; |
4ff296b0 YW |
3423 | int r; |
3424 | ||
572b21d9 YW |
3425 | r = sd_netlink_message_get_errno(m); |
3426 | if (r < 0) { | |
40681e5c | 3427 | log_link_warning_errno(link, r, "Failed to wait for the interface to be initialized: %m"); |
572b21d9 YW |
3428 | link_enter_failed(link); |
3429 | return 0; | |
3430 | } | |
3431 | ||
3432 | r = sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &s); | |
3433 | if (r < 0 && r != -ENODATA) { | |
3434 | link_enter_failed(link); | |
3435 | return 0; | |
3436 | } | |
3437 | ||
3438 | strv_free_and_replace(link->alternative_names, s); | |
3439 | ||
4ff296b0 YW |
3440 | r = link_initialized_and_synced(link); |
3441 | if (r < 0) | |
3442 | link_enter_failed(link); | |
e6bf7774 YW |
3443 | return 1; |
3444 | } | |
3445 | ||
51517f9e | 3446 | int link_initialized(Link *link, sd_device *device) { |
4afd3348 | 3447 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
4f561e8e TG |
3448 | int r; |
3449 | ||
3450 | assert(link); | |
3451 | assert(link->manager); | |
3452 | assert(link->manager->rtnl); | |
3453 | assert(device); | |
3454 | ||
8434fd5c | 3455 | if (link->state != LINK_STATE_PENDING) |
4f561e8e TG |
3456 | return 0; |
3457 | ||
51517f9e | 3458 | if (link->sd_device) |
679b3605 TG |
3459 | return 0; |
3460 | ||
79008bdd | 3461 | log_link_debug(link, "udev initialized link"); |
bd08ce56 | 3462 | link_set_state(link, LINK_STATE_INITIALIZED); |
4f561e8e | 3463 | |
51517f9e | 3464 | link->sd_device = sd_device_ref(device); |
4f561e8e | 3465 | |
3c9b8860 TG |
3466 | /* udev has initialized the link, but we don't know if we have yet |
3467 | * processed the NEWLINK messages with the latest state. Do a GETLINK, | |
3468 | * when it returns we know that the pending NEWLINKs have already been | |
3469 | * processed and that we are up-to-date */ | |
4f561e8e | 3470 | |
3c9b8860 TG |
3471 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, |
3472 | link->ifindex); | |
4f561e8e TG |
3473 | if (r < 0) |
3474 | return r; | |
3475 | ||
302a796f YW |
3476 | r = netlink_call_async(link->manager->rtnl, NULL, req, link_initialized_handler, |
3477 | link_netlink_destroy_callback, link); | |
4f561e8e TG |
3478 | if (r < 0) |
3479 | return r; | |
3480 | ||
5da8149f TG |
3481 | link_ref(link); |
3482 | ||
4f561e8e TG |
3483 | return 0; |
3484 | } | |
3485 | ||
c4a03a56 | 3486 | static int link_load(Link *link) { |
0bc70f1d TG |
3487 | _cleanup_free_ char *network_file = NULL, |
3488 | *addresses = NULL, | |
f703cc2c | 3489 | *routes = NULL, |
0bc70f1d TG |
3490 | *dhcp4_address = NULL, |
3491 | *ipv4ll_address = NULL; | |
3492 | union in_addr_union address; | |
c4a03a56 TG |
3493 | int r; |
3494 | ||
3495 | assert(link); | |
3496 | ||
aa8fbc74 | 3497 | r = parse_env_file(NULL, link->state_file, |
c4a03a56 TG |
3498 | "NETWORK_FILE", &network_file, |
3499 | "ADDRESSES", &addresses, | |
f703cc2c | 3500 | "ROUTES", &routes, |
0bc70f1d | 3501 | "DHCP4_ADDRESS", &dhcp4_address, |
13df9c39 | 3502 | "IPV4LL_ADDRESS", &ipv4ll_address); |
c4a03a56 TG |
3503 | if (r < 0 && r != -ENOENT) |
3504 | return log_link_error_errno(link, r, "Failed to read %s: %m", link->state_file); | |
3505 | ||
3506 | if (network_file) { | |
3507 | Network *network; | |
3508 | char *suffix; | |
3509 | ||
3510 | /* drop suffix */ | |
3511 | suffix = strrchr(network_file, '.'); | |
3512 | if (!suffix) { | |
3513 | log_link_debug(link, "Failed to get network name from %s", network_file); | |
3514 | goto network_file_fail; | |
3515 | } | |
3516 | *suffix = '\0'; | |
3517 | ||
3518 | r = network_get_by_name(link->manager, basename(network_file), &network); | |
3519 | if (r < 0) { | |
3520 | log_link_debug_errno(link, r, "Failed to get network %s: %m", basename(network_file)); | |
3521 | goto network_file_fail; | |
3522 | } | |
3523 | ||
7d342c03 | 3524 | r = network_apply(network, link); |
c4a03a56 TG |
3525 | if (r < 0) |
3526 | return log_link_error_errno(link, r, "Failed to apply network %s: %m", basename(network_file)); | |
3527 | } | |
3528 | ||
3529 | network_file_fail: | |
3530 | ||
1f68f772 ZJS |
3531 | for (const char *p = addresses; p; ) { |
3532 | _cleanup_free_ char *address_str = NULL; | |
3533 | char *prefixlen_str; | |
3534 | int family; | |
3535 | unsigned char prefixlen; | |
c4a03a56 | 3536 | |
1f68f772 ZJS |
3537 | r = extract_first_word(&p, &address_str, NULL, 0); |
3538 | if (r < 0) | |
3539 | log_link_warning_errno(link, r, "failed to parse ADDRESSES: %m"); | |
3540 | if (r <= 0) | |
3541 | break; | |
c4a03a56 | 3542 | |
1f68f772 ZJS |
3543 | prefixlen_str = strchr(address_str, '/'); |
3544 | if (!prefixlen_str) { | |
3545 | log_link_debug(link, "Failed to parse address and prefix length %s", address_str); | |
3546 | continue; | |
3547 | } | |
3548 | *prefixlen_str++ = '\0'; | |
c4a03a56 | 3549 | |
1f68f772 ZJS |
3550 | r = sscanf(prefixlen_str, "%hhu", &prefixlen); |
3551 | if (r != 1) { | |
3552 | log_link_error(link, "Failed to parse prefixlen %s", prefixlen_str); | |
3553 | continue; | |
3554 | } | |
c4a03a56 | 3555 | |
1f68f772 ZJS |
3556 | r = in_addr_from_string_auto(address_str, &family, &address); |
3557 | if (r < 0) { | |
3558 | log_link_debug_errno(link, r, "Failed to parse address %s: %m", address_str); | |
3559 | continue; | |
c4a03a56 | 3560 | } |
c4a03a56 | 3561 | |
1f68f772 ZJS |
3562 | r = address_add(link, family, &address, prefixlen, NULL); |
3563 | if (r < 0) | |
3564 | return log_link_error_errno(link, r, "Failed to add address: %m"); | |
3565 | } | |
74544b4e | 3566 | |
1f68f772 ZJS |
3567 | for (const char *p = routes; p; ) { |
3568 | _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL; | |
3569 | _cleanup_(route_freep) Route *tmp = NULL; | |
3570 | _cleanup_free_ char *route_str = NULL; | |
3571 | char *prefixlen_str; | |
3572 | Route *route; | |
f703cc2c | 3573 | |
1f68f772 ZJS |
3574 | r = extract_first_word(&p, &route_str, NULL, 0); |
3575 | if (r < 0) | |
3576 | log_link_debug_errno(link, r, "failed to parse ROUTES: %m"); | |
3577 | if (r <= 0) | |
3578 | break; | |
c598ac76 | 3579 | |
1f68f772 ZJS |
3580 | prefixlen_str = strchr(route_str, '/'); |
3581 | if (!prefixlen_str) { | |
3582 | log_link_debug(link, "Failed to parse route %s", route_str); | |
3583 | continue; | |
3584 | } | |
3585 | *prefixlen_str++ = '\0'; | |
f703cc2c | 3586 | |
1f68f772 ZJS |
3587 | r = route_new(&tmp); |
3588 | if (r < 0) | |
3589 | return log_oom(); | |
f703cc2c | 3590 | |
1f68f772 ZJS |
3591 | r = sscanf(prefixlen_str, |
3592 | "%hhu/%hhu/%"SCNu32"/%"PRIu32"/"USEC_FMT, | |
3593 | &tmp->dst_prefixlen, | |
3594 | &tmp->tos, | |
3595 | &tmp->priority, | |
3596 | &tmp->table, | |
3597 | &tmp->lifetime); | |
3598 | if (r != 5) { | |
3599 | log_link_debug(link, | |
3600 | "Failed to parse destination prefix length, tos, priority, table or expiration %s", | |
3601 | prefixlen_str); | |
3602 | continue; | |
3603 | } | |
f1368755 | 3604 | |
1f68f772 ZJS |
3605 | r = in_addr_from_string_auto(route_str, &tmp->family, &tmp->dst); |
3606 | if (r < 0) { | |
3607 | log_link_debug_errno(link, r, "Failed to parse route destination %s: %m", route_str); | |
3608 | continue; | |
3609 | } | |
f703cc2c | 3610 | |
1f68f772 ZJS |
3611 | r = route_add(link, tmp, &route); |
3612 | if (r < 0) | |
3613 | return log_link_error_errno(link, r, "Failed to add route: %m"); | |
f703cc2c | 3614 | |
1f68f772 ZJS |
3615 | if (route->lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) { |
3616 | r = sd_event_add_time(link->manager->event, &expire, | |
3617 | clock_boottime_or_monotonic(), | |
3618 | route->lifetime, 0, route_expire_handler, route); | |
f703cc2c | 3619 | if (r < 0) |
1f68f772 | 3620 | log_link_warning_errno(link, r, "Could not arm route expiration handler: %m"); |
f703cc2c | 3621 | } |
1f68f772 ZJS |
3622 | |
3623 | sd_event_source_unref(route->expire); | |
3624 | route->expire = TAKE_PTR(expire); | |
f703cc2c TG |
3625 | } |
3626 | ||
0bc70f1d TG |
3627 | if (dhcp4_address) { |
3628 | r = in_addr_from_string(AF_INET, dhcp4_address, &address); | |
3629 | if (r < 0) { | |
b68d26b8 | 3630 | log_link_debug_errno(link, r, "Failed to parse DHCPv4 address %s: %m", dhcp4_address); |
0bc70f1d TG |
3631 | goto dhcp4_address_fail; |
3632 | } | |
3633 | ||
b1c626f6 | 3634 | r = sd_dhcp_client_new(&link->dhcp_client, link->network ? link->network->dhcp_anonymize : 0); |
0bc70f1d | 3635 | if (r < 0) |
b68d26b8 | 3636 | return log_link_error_errno(link, r, "Failed to create DHCPv4 client: %m"); |
0bc70f1d | 3637 | |
1b43e246 DS |
3638 | r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0); |
3639 | if (r < 0) | |
3640 | return log_link_error_errno(link, r, "Failed to attach DHCPv4 event: %m"); | |
3641 | ||
0bc70f1d TG |
3642 | r = sd_dhcp_client_set_request_address(link->dhcp_client, &address.in); |
3643 | if (r < 0) | |
b68d26b8 | 3644 | return log_link_error_errno(link, r, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address); |
0bc70f1d TG |
3645 | } |
3646 | ||
3647 | dhcp4_address_fail: | |
3648 | ||
3649 | if (ipv4ll_address) { | |
3650 | r = in_addr_from_string(AF_INET, ipv4ll_address, &address); | |
3651 | if (r < 0) { | |
b68d26b8 | 3652 | log_link_debug_errno(link, r, "Failed to parse IPv4LL address %s: %m", ipv4ll_address); |
0bc70f1d TG |
3653 | goto ipv4ll_address_fail; |
3654 | } | |
3655 | ||
3656 | r = sd_ipv4ll_new(&link->ipv4ll); | |
3657 | if (r < 0) | |
b68d26b8 | 3658 | return log_link_error_errno(link, r, "Failed to create IPv4LL client: %m"); |
0bc70f1d | 3659 | |
1b43e246 DS |
3660 | r = sd_ipv4ll_attach_event(link->ipv4ll, NULL, 0); |
3661 | if (r < 0) | |
3662 | return log_link_error_errno(link, r, "Failed to attach IPv4LL event: %m"); | |
3663 | ||
0bc70f1d TG |
3664 | r = sd_ipv4ll_set_address(link->ipv4ll, &address.in); |
3665 | if (r < 0) | |
b68d26b8 | 3666 | return log_link_error_errno(link, r, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address); |
0bc70f1d TG |
3667 | } |
3668 | ||
3669 | ipv4ll_address_fail: | |
3670 | ||
c4a03a56 TG |
3671 | return 0; |
3672 | } | |
3673 | ||
1c4baffc | 3674 | int link_add(Manager *m, sd_netlink_message *message, Link **ret) { |
51517f9e | 3675 | _cleanup_(sd_device_unrefp) sd_device *device = NULL; |
505f8da7 | 3676 | char ifindex_str[2 + DECIMAL_STR_MAX(int)]; |
51517f9e | 3677 | Link *link; |
5a937ea2 | 3678 | int r; |
505f8da7 TG |
3679 | |
3680 | assert(m); | |
fbbeb65a | 3681 | assert(m->rtnl); |
505f8da7 TG |
3682 | assert(message); |
3683 | assert(ret); | |
3684 | ||
3685 | r = link_new(m, message, ret); | |
3686 | if (r < 0) | |
3687 | return r; | |
3688 | ||
3689 | link = *ret; | |
3690 | ||
6a7a4e4d | 3691 | log_link_debug(link, "Link %d added", link->ifindex); |
505f8da7 | 3692 | |
c4a03a56 TG |
3693 | r = link_load(link); |
3694 | if (r < 0) | |
3695 | return r; | |
3696 | ||
bf331d87 YW |
3697 | if (path_is_read_only_fs("/sys") <= 0) { |
3698 | /* udev should be around */ | |
ae06ab10 | 3699 | sprintf(ifindex_str, "n%d", link->ifindex); |
51517f9e YW |
3700 | r = sd_device_new_from_device_id(&device, ifindex_str); |
3701 | if (r < 0) { | |
4d59e14f YW |
3702 | log_link_warning_errno(link, r, "Could not find device, waiting for device initialization: %m"); |
3703 | return 0; | |
5c416fc4 | 3704 | } |
505f8da7 | 3705 | |
5a937ea2 | 3706 | r = sd_device_get_is_initialized(device); |
51517f9e | 3707 | if (r < 0) { |
bf331d87 | 3708 | log_link_warning_errno(link, r, "Could not determine whether the device is initialized: %m"); |
51517f9e YW |
3709 | goto failed; |
3710 | } | |
5a937ea2 | 3711 | if (r == 0) { |
505f8da7 | 3712 | /* not yet ready */ |
79008bdd | 3713 | log_link_debug(link, "link pending udev initialization..."); |
505f8da7 | 3714 | return 0; |
3c4cb064 | 3715 | } |
505f8da7 | 3716 | |
299ad32d YW |
3717 | r = device_is_renaming(device); |
3718 | if (r < 0) { | |
bf331d87 | 3719 | log_link_warning_errno(link, r, "Failed to determine the device is being renamed: %m"); |
299ad32d YW |
3720 | goto failed; |
3721 | } | |
3722 | if (r > 0) { | |
bf331d87 | 3723 | log_link_debug(link, "Interface is being renamed, pending initialization."); |
299ad32d YW |
3724 | return 0; |
3725 | } | |
3726 | ||
4f561e8e TG |
3727 | r = link_initialized(link, device); |
3728 | if (r < 0) | |
5c416fc4 | 3729 | goto failed; |
4f561e8e | 3730 | } else { |
e6bf7774 | 3731 | r = link_initialized_and_synced(link); |
4f561e8e | 3732 | if (r < 0) |
5c416fc4 | 3733 | goto failed; |
4f561e8e | 3734 | } |
505f8da7 | 3735 | |
a748b692 | 3736 | return 0; |
5c416fc4 TG |
3737 | failed: |
3738 | link_enter_failed(link); | |
3739 | return r; | |
a748b692 TG |
3740 | } |
3741 | ||
c601ebf7 | 3742 | int link_ipv6ll_gained(Link *link, const struct in6_addr *address) { |
e7ab854c TG |
3743 | int r; |
3744 | ||
3745 | assert(link); | |
3746 | ||
3747 | log_link_info(link, "Gained IPv6LL"); | |
3748 | ||
c601ebf7 | 3749 | link->ipv6ll_address = *address; |
e7ab854c TG |
3750 | link_check_ready(link); |
3751 | ||
b9ea3d2e | 3752 | if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) { |
e7ab854c TG |
3753 | r = link_acquire_ipv6_conf(link); |
3754 | if (r < 0) { | |
3755 | link_enter_failed(link); | |
3756 | return r; | |
3757 | } | |
3758 | } | |
3759 | ||
3760 | return 0; | |
3761 | } | |
3762 | ||
9c0a72f9 TG |
3763 | static int link_carrier_gained(Link *link) { |
3764 | int r; | |
3765 | ||
3766 | assert(link); | |
3767 | ||
0894cfe4 | 3768 | r = wifi_get_info(link); |
8d968fdd YW |
3769 | if (r < 0) |
3770 | return r; | |
3771 | if (r > 0) { | |
572b21d9 YW |
3772 | r = link_reconfigure_internal(link, NULL, false); |
3773 | if (r < 0) { | |
3774 | link_enter_failed(link); | |
8d968fdd | 3775 | return r; |
572b21d9 | 3776 | } |
8d968fdd YW |
3777 | } |
3778 | ||
b9ea3d2e | 3779 | if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) { |
9c0a72f9 TG |
3780 | r = link_acquire_conf(link); |
3781 | if (r < 0) { | |
3782 | link_enter_failed(link); | |
3783 | return r; | |
3784 | } | |
6fc25497 | 3785 | |
b425c3ae | 3786 | link_set_state(link, LINK_STATE_CONFIGURING); |
289e6774 | 3787 | r = link_request_set_addresses(link); |
6fc25497 SS |
3788 | if (r < 0) |
3789 | return r; | |
9c0a72f9 TG |
3790 | } |
3791 | ||
0d4ad91d AR |
3792 | r = link_handle_bound_by_list(link); |
3793 | if (r < 0) | |
3794 | return r; | |
3795 | ||
9c0a72f9 TG |
3796 | return 0; |
3797 | } | |
3798 | ||
3799 | static int link_carrier_lost(Link *link) { | |
3800 | int r; | |
3801 | ||
3802 | assert(link); | |
3803 | ||
25e992ba | 3804 | if (link->network && link->network->ignore_carrier_loss) |
93b4dab5 SS |
3805 | return 0; |
3806 | ||
fbdb6605 | 3807 | /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop. |
55dc8c4a YW |
3808 | * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the |
3809 | * configuration and stop the clients as well. */ | |
97e7fb39 SS |
3810 | if (link->setting_mtu) |
3811 | return 0; | |
3812 | ||
95355a28 | 3813 | r = link_stop_clients(link, false); |
9c0a72f9 TG |
3814 | if (r < 0) { |
3815 | link_enter_failed(link); | |
3816 | return r; | |
3817 | } | |
3818 | ||
28464ae0 SS |
3819 | if (link_dhcp4_server_enabled(link)) |
3820 | (void) sd_dhcp_server_stop(link->dhcp_server); | |
45a9eac9 | 3821 | |
3104883d SS |
3822 | r = link_drop_config(link); |
3823 | if (r < 0) | |
3824 | return r; | |
3825 | ||
2c7b826d | 3826 | if (!IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING, LINK_STATE_INITIALIZED)) { |
f258e948 | 3827 | log_link_debug(link, "State is %s, dropping config", link_state_to_string(link->state)); |
c436d553 MM |
3828 | r = link_drop_foreign_config(link); |
3829 | if (r < 0) | |
3830 | return r; | |
3831 | } | |
3104883d | 3832 | |
0d4ad91d AR |
3833 | r = link_handle_bound_by_list(link); |
3834 | if (r < 0) | |
3835 | return r; | |
3836 | ||
9c0a72f9 TG |
3837 | return 0; |
3838 | } | |
3839 | ||
3840 | int link_carrier_reset(Link *link) { | |
3841 | int r; | |
3842 | ||
3843 | assert(link); | |
3844 | ||
3845 | if (link_has_carrier(link)) { | |
3846 | r = link_carrier_lost(link); | |
3847 | if (r < 0) | |
3848 | return r; | |
3849 | ||
3850 | r = link_carrier_gained(link); | |
3851 | if (r < 0) | |
3852 | return r; | |
3853 | ||
6a7a4e4d | 3854 | log_link_info(link, "Reset carrier"); |
9c0a72f9 TG |
3855 | } |
3856 | ||
3857 | return 0; | |
3858 | } | |
3859 | ||
d236718c DS |
3860 | /* This is called every time an interface admin state changes to up; |
3861 | * specifically, when IFF_UP flag changes from unset to set */ | |
3862 | static int link_admin_state_up(Link *link) { | |
3863 | int r; | |
3864 | ||
3865 | /* We set the ipv6 mtu after the device mtu, but the kernel resets | |
3866 | * ipv6 mtu on NETDEV_UP, so we need to reset it. The check for | |
3867 | * ipv6_mtu_set prevents this from trying to set it too early before | |
3868 | * the link->network has been setup; we only need to reset it | |
3869 | * here if we've already set it during normal initialization. */ | |
3870 | if (link->ipv6_mtu_set) { | |
3871 | r = link_set_ipv6_mtu(link); | |
3872 | if (r < 0) | |
3873 | return r; | |
3874 | } | |
3875 | ||
3876 | return 0; | |
3877 | } | |
3878 | ||
1c4baffc | 3879 | int link_update(Link *link, sd_netlink_message *m) { |
572b21d9 | 3880 | _cleanup_strv_free_ char **s = NULL; |
c49b33ac | 3881 | struct ether_addr mac; |
ca4e095a | 3882 | const char *ifname; |
afe7fd56 | 3883 | uint32_t mtu; |
d236718c | 3884 | bool had_carrier, carrier_gained, carrier_lost, link_was_admin_up; |
bb262ef0 | 3885 | int old_master, r; |
22936833 | 3886 | |
dd3efc09 | 3887 | assert(link); |
b8941f74 | 3888 | assert(link->ifname); |
22936833 TG |
3889 | assert(m); |
3890 | ||
7619683b | 3891 | if (link->state == LINK_STATE_LINGER) { |
5238e957 | 3892 | log_link_info(link, "Link re-added"); |
289e6774 | 3893 | link_set_state(link, LINK_STATE_CONFIGURING); |
0d4ad91d AR |
3894 | |
3895 | r = link_new_carrier_maps(link); | |
3896 | if (r < 0) | |
3897 | return r; | |
7619683b TG |
3898 | } |
3899 | ||
1c4baffc | 3900 | r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname); |
b8941f74 | 3901 | if (r >= 0 && !streq(ifname, link->ifname)) { |
30de2b89 | 3902 | Manager *manager = link->manager; |
b8941f74 | 3903 | |
30de2b89 | 3904 | log_link_info(link, "Interface name change detected, %s has been renamed to %s.", link->ifname, ifname); |
0d4ad91d | 3905 | |
30de2b89 YW |
3906 | link_drop(link); |
3907 | r = link_add(manager, m, &link); | |
3908 | if (r < 0) | |
3909 | return r; | |
b8941f74 TG |
3910 | } |
3911 | ||
572b21d9 YW |
3912 | r = sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &s); |
3913 | if (r >= 0) | |
3914 | strv_free_and_replace(link->alternative_names, s); | |
3915 | ||
1c4baffc | 3916 | r = sd_netlink_message_read_u32(m, IFLA_MTU, &mtu); |
afe7fd56 TG |
3917 | if (r >= 0 && mtu > 0) { |
3918 | link->mtu = mtu; | |
4e964aa0 | 3919 | if (link->original_mtu == 0) { |
afe7fd56 | 3920 | link->original_mtu = mtu; |
6a7a4e4d | 3921 | log_link_debug(link, "Saved original MTU: %" PRIu32, link->original_mtu); |
afe7fd56 TG |
3922 | } |
3923 | ||
3924 | if (link->dhcp_client) { | |
3c9b8860 TG |
3925 | r = sd_dhcp_client_set_mtu(link->dhcp_client, |
3926 | link->mtu); | |
fc95c359 YW |
3927 | if (r < 0) |
3928 | return log_link_warning_errno(link, r, "Could not update MTU in DHCP client: %m"); | |
afe7fd56 | 3929 | } |
7465dd22 PF |
3930 | |
3931 | if (link->radv) { | |
3932 | r = sd_radv_set_mtu(link->radv, link->mtu); | |
3933 | if (r < 0) | |
3934 | return log_link_warning_errno(link, r, "Could not set MTU for Router Advertisement: %m"); | |
3935 | } | |
9842de0d | 3936 | } |
69629de9 | 3937 | |
e9189a1f TG |
3938 | /* The kernel may broadcast NEWLINK messages without the MAC address |
3939 | set, simply ignore them. */ | |
1c4baffc | 3940 | r = sd_netlink_message_read_ether_addr(m, IFLA_ADDRESS, &mac); |
e9189a1f | 3941 | if (r >= 0) { |
3c9b8860 TG |
3942 | if (memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet, |
3943 | ETH_ALEN)) { | |
c49b33ac | 3944 | |
3c9b8860 TG |
3945 | memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet, |
3946 | ETH_ALEN); | |
c49b33ac | 3947 | |
79008bdd | 3948 | log_link_debug(link, "MAC address: " |
20861203 TG |
3949 | "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", |
3950 | mac.ether_addr_octet[0], | |
3951 | mac.ether_addr_octet[1], | |
3952 | mac.ether_addr_octet[2], | |
3953 | mac.ether_addr_octet[3], | |
3954 | mac.ether_addr_octet[4], | |
3955 | mac.ether_addr_octet[5]); | |
c49b33ac | 3956 | |
20861203 TG |
3957 | if (link->ipv4ll) { |
3958 | r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac); | |
6a7a4e4d LP |
3959 | if (r < 0) |
3960 | return log_link_warning_errno(link, r, "Could not update MAC address in IPv4LL client: %m"); | |
c49b33ac | 3961 | } |
c49b33ac | 3962 | |
20861203 | 3963 | if (link->dhcp_client) { |
3c9b8860 | 3964 | r = sd_dhcp_client_set_mac(link->dhcp_client, |
76253e73 DW |
3965 | (const uint8_t *) &link->mac, |
3966 | sizeof (link->mac), | |
3967 | ARPHRD_ETHER); | |
6a7a4e4d LP |
3968 | if (r < 0) |
3969 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCP client: %m"); | |
413708d1 | 3970 | |
fff1f40c YW |
3971 | r = dhcp4_set_client_identifier(link); |
3972 | if (r < 0) | |
3973 | return r; | |
c49b33ac | 3974 | } |
4138fb2c PF |
3975 | |
3976 | if (link->dhcp6_client) { | |
f24648a6 | 3977 | const DUID* duid = link_get_duid(link); |
8341a5c3 | 3978 | |
4138fb2c | 3979 | r = sd_dhcp6_client_set_mac(link->dhcp6_client, |
76253e73 DW |
3980 | (const uint8_t *) &link->mac, |
3981 | sizeof (link->mac), | |
3982 | ARPHRD_ETHER); | |
6a7a4e4d LP |
3983 | if (r < 0) |
3984 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCPv6 client: %m"); | |
413708d1 | 3985 | |
8217ed5e TH |
3986 | if (link->network->iaid_set) { |
3987 | r = sd_dhcp6_client_set_iaid(link->dhcp6_client, | |
3988 | link->network->iaid); | |
3989 | if (r < 0) | |
3990 | return log_link_warning_errno(link, r, "Could not update DHCPv6 IAID: %m"); | |
3991 | } | |
413708d1 | 3992 | |
8341a5c3 ZJS |
3993 | r = sd_dhcp6_client_set_duid(link->dhcp6_client, |
3994 | duid->type, | |
3995 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
3996 | duid->raw_data_len); | |
413708d1 VK |
3997 | if (r < 0) |
3998 | return log_link_warning_errno(link, r, "Could not update DHCPv6 DUID: %m"); | |
4138fb2c | 3999 | } |
7465dd22 PF |
4000 | |
4001 | if (link->radv) { | |
4002 | r = sd_radv_set_mac(link->radv, &link->mac); | |
4003 | if (r < 0) | |
4004 | return log_link_warning_errno(link, r, "Could not update MAC for Router Advertisement: %m"); | |
4005 | } | |
420d2058 SS |
4006 | |
4007 | if (link->ndisc) { | |
4008 | r = sd_ndisc_set_mac(link->ndisc, &link->mac); | |
4009 | if (r < 0) | |
4010 | return log_link_warning_errno(link, r, "Could not update MAC for ndisc: %m"); | |
4011 | } | |
c49b33ac | 4012 | } |
4f882b2a TG |
4013 | } |
4014 | ||
bb262ef0 YW |
4015 | old_master = link->master_ifindex; |
4016 | (void) sd_netlink_message_read_u32(m, IFLA_MASTER, (uint32_t *) &link->master_ifindex); | |
4017 | ||
d236718c | 4018 | link_was_admin_up = link->flags & IFF_UP; |
a61bb41c TG |
4019 | had_carrier = link_has_carrier(link); |
4020 | ||
bb262ef0 | 4021 | r = link_update_flags(link, m, old_master != link->master_ifindex); |
a61bb41c TG |
4022 | if (r < 0) |
4023 | return r; | |
4024 | ||
d236718c DS |
4025 | if (!link_was_admin_up && (link->flags & IFF_UP)) { |
4026 | log_link_info(link, "Link UP"); | |
4027 | ||
4028 | r = link_admin_state_up(link); | |
4029 | if (r < 0) | |
4030 | return r; | |
4031 | } else if (link_was_admin_up && !(link->flags & IFF_UP)) | |
4032 | log_link_info(link, "Link DOWN"); | |
4033 | ||
273eec24 LP |
4034 | r = link_update_lldp(link); |
4035 | if (r < 0) | |
4036 | return r; | |
4037 | ||
a61bb41c TG |
4038 | carrier_gained = !had_carrier && link_has_carrier(link); |
4039 | carrier_lost = had_carrier && !link_has_carrier(link); | |
4040 | ||
4041 | if (carrier_gained) { | |
6a7a4e4d | 4042 | log_link_info(link, "Gained carrier"); |
a61bb41c | 4043 | |
9c0a72f9 TG |
4044 | r = link_carrier_gained(link); |
4045 | if (r < 0) | |
4046 | return r; | |
a61bb41c | 4047 | } else if (carrier_lost) { |
6a7a4e4d | 4048 | log_link_info(link, "Lost carrier"); |
a61bb41c | 4049 | |
9c0a72f9 TG |
4050 | r = link_carrier_lost(link); |
4051 | if (r < 0) | |
a61bb41c | 4052 | return r; |
a61bb41c TG |
4053 | } |
4054 | ||
4055 | return 0; | |
dd3efc09 | 4056 | } |
fe8db0c5 | 4057 | |
b295beea LP |
4058 | static void print_link_hashmap(FILE *f, const char *prefix, Hashmap* h) { |
4059 | bool space = false; | |
4060 | Iterator i; | |
4061 | Link *link; | |
4062 | ||
4063 | assert(f); | |
4064 | assert(prefix); | |
4065 | ||
4066 | if (hashmap_isempty(h)) | |
4067 | return; | |
4068 | ||
0d536673 | 4069 | fputs(prefix, f); |
b295beea LP |
4070 | HASHMAP_FOREACH(link, h, i) { |
4071 | if (space) | |
0d536673 | 4072 | fputc(' ', f); |
b295beea LP |
4073 | |
4074 | fprintf(f, "%i", link->ifindex); | |
4075 | space = true; | |
4076 | } | |
4077 | ||
0d536673 | 4078 | fputc('\n', f); |
b295beea LP |
4079 | } |
4080 | ||
15761549 | 4081 | static void link_save_dns(FILE *f, struct in_addr_data *dns, unsigned n_dns, bool *space) { |
dddc8d1e | 4082 | for (unsigned j = 0; j < n_dns; j++) { |
15761549 | 4083 | _cleanup_free_ char *b = NULL; |
dddc8d1e | 4084 | int r; |
15761549 YW |
4085 | |
4086 | r = in_addr_to_string(dns[j].family, &dns[j].address, &b); | |
4087 | if (r < 0) { | |
4088 | log_debug_errno(r, "Failed to format address, ignoring: %m"); | |
4089 | continue; | |
4090 | } | |
4091 | ||
4092 | if (*space) | |
4093 | fputc(' ', f); | |
4094 | fputs(b, f); | |
4095 | *space = true; | |
4096 | } | |
4097 | } | |
4098 | ||
d5e172d2 ZJS |
4099 | static void serialize_addresses( |
4100 | FILE *f, | |
4101 | const char *lvalue, | |
4102 | bool *space, | |
4103 | char **addresses, | |
4104 | sd_dhcp_lease *lease, | |
4105 | bool conditional, | |
ddb82ec2 | 4106 | sd_dhcp_lease_server_type what, |
d5e172d2 ZJS |
4107 | sd_dhcp6_lease *lease6, |
4108 | bool conditional6, | |
4109 | int (*lease6_get_addr)(sd_dhcp6_lease*, const struct in6_addr**), | |
4110 | int (*lease6_get_fqdn)(sd_dhcp6_lease*, char ***)) { | |
4111 | int r; | |
4112 | ||
4113 | bool _space = false; | |
4114 | if (!space) | |
4115 | space = &_space; | |
4116 | ||
4117 | if (lvalue) | |
4118 | fprintf(f, "%s=", lvalue); | |
4119 | fputstrv(f, addresses, NULL, space); | |
4120 | ||
4121 | if (lease && conditional) { | |
4122 | const struct in_addr *lease_addresses; | |
4123 | ||
4124 | r = sd_dhcp_lease_get_servers(lease, what, &lease_addresses); | |
4125 | if (r > 0) | |
d8bff5cc | 4126 | serialize_in_addrs(f, lease_addresses, r, space, in4_addr_is_non_local); |
d5e172d2 ZJS |
4127 | } |
4128 | ||
4129 | if (lease6 && conditional6 && lease6_get_addr) { | |
4130 | const struct in6_addr *in6_addrs; | |
4131 | ||
4132 | r = lease6_get_addr(lease6, &in6_addrs); | |
d8bff5cc ZJS |
4133 | if (r > 0) |
4134 | serialize_in6_addrs(f, in6_addrs, r, space); | |
d5e172d2 ZJS |
4135 | } |
4136 | ||
4137 | if (lease6 && conditional6 && lease6_get_fqdn) { | |
4138 | char **in6_hosts; | |
4139 | ||
4140 | r = lease6_get_fqdn(lease6, &in6_hosts); | |
4141 | if (r > 0) | |
4142 | fputstrv(f, in6_hosts, NULL, space); | |
4143 | } | |
4144 | ||
4145 | if (lvalue) | |
4146 | fputc('\n', f); | |
4147 | } | |
4148 | ||
fe8db0c5 | 4149 | int link_save(Link *link) { |
d361b373 | 4150 | const char *admin_state, *oper_state, *carrier_state, *address_state; |
68a8723c | 4151 | _cleanup_free_ char *temp_path = NULL; |
fe8db0c5 | 4152 | _cleanup_fclose_ FILE *f = NULL; |
c1eb9872 | 4153 | Route *route; |
18d8a2cf | 4154 | Address *a; |
e7780c8d | 4155 | Iterator i; |
fe8db0c5 TG |
4156 | int r; |
4157 | ||
4158 | assert(link); | |
4159 | assert(link->state_file); | |
68a8723c | 4160 | assert(link->lease_file); |
bbf7c048 TG |
4161 | assert(link->manager); |
4162 | ||
370e9930 | 4163 | if (link->state == LINK_STATE_LINGER) { |
6990fb6b | 4164 | (void) unlink(link->state_file); |
370e9930 TG |
4165 | return 0; |
4166 | } | |
4167 | ||
34437b4f LP |
4168 | link_lldp_save(link); |
4169 | ||
deb2e523 TG |
4170 | admin_state = link_state_to_string(link->state); |
4171 | assert(admin_state); | |
4172 | ||
e375dcde TG |
4173 | oper_state = link_operstate_to_string(link->operstate); |
4174 | assert(oper_state); | |
deb2e523 | 4175 | |
ac999bf0 YW |
4176 | carrier_state = link_carrier_state_to_string(link->carrier_state); |
4177 | assert(carrier_state); | |
4178 | ||
4179 | address_state = link_address_state_to_string(link->address_state); | |
4180 | assert(address_state); | |
4181 | ||
fe8db0c5 TG |
4182 | r = fopen_temporary(link->state_file, &f, &temp_path); |
4183 | if (r < 0) | |
6a7a4e4d | 4184 | goto fail; |
fe8db0c5 | 4185 | |
5512a963 | 4186 | (void) fchmod(fileno(f), 0644); |
fe8db0c5 TG |
4187 | |
4188 | fprintf(f, | |
4189 | "# This is private data. Do not parse.\n" | |
deb2e523 | 4190 | "ADMIN_STATE=%s\n" |
ac999bf0 YW |
4191 | "OPER_STATE=%s\n" |
4192 | "CARRIER_STATE=%s\n" | |
4193 | "ADDRESS_STATE=%s\n", | |
4194 | admin_state, oper_state, carrier_state, address_state); | |
fe8db0c5 | 4195 | |
bcb7a07e | 4196 | if (link->network) { |
fe0e16db YW |
4197 | char **dhcp6_domains = NULL, **dhcp_domains = NULL; |
4198 | const char *dhcp_domainname = NULL, *p; | |
07bdc70d | 4199 | sd_dhcp6_lease *dhcp6_lease = NULL; |
fe0e16db | 4200 | bool space; |
07bdc70d | 4201 | |
c1a38904 MTL |
4202 | fprintf(f, "REQUIRED_FOR_ONLINE=%s\n", |
4203 | yes_no(link->network->required_for_online)); | |
4204 | ||
d5e172d2 ZJS |
4205 | LinkOperationalStateRange st = link->network->required_operstate_for_online; |
4206 | fprintf(f, "REQUIRED_OPER_STATE_FOR_ONLINE=%s%s%s\n", | |
4207 | strempty(link_operstate_to_string(st.min)), | |
4208 | st.max != LINK_OPERSTATE_RANGE_DEFAULT.max ? ":" : "", | |
4209 | st.max != LINK_OPERSTATE_RANGE_DEFAULT.max ? strempty(link_operstate_to_string(st.max)) : ""); | |
4ac77d63 | 4210 | |
07bdc70d | 4211 | if (link->dhcp6_client) { |
4058d339 TG |
4212 | r = sd_dhcp6_client_get_lease(link->dhcp6_client, &dhcp6_lease); |
4213 | if (r < 0 && r != -ENOMSG) | |
2206aa5c | 4214 | log_link_debug_errno(link, r, "Failed to get DHCPv6 lease: %m"); |
07bdc70d | 4215 | } |
b0e39c82 | 4216 | |
adc5b2e2 TG |
4217 | fprintf(f, "NETWORK_FILE=%s\n", link->network->filename); |
4218 | ||
d5e172d2 ZJS |
4219 | /************************************************************/ |
4220 | ||
0d536673 | 4221 | fputs("DNS=", f); |
ea352b40 | 4222 | space = false; |
15761549 YW |
4223 | if (link->n_dns != (unsigned) -1) |
4224 | link_save_dns(f, link->dns, link->n_dns, &space); | |
4225 | else | |
4226 | link_save_dns(f, link->network->dns, link->network->n_dns, &space); | |
d5314fff | 4227 | |
d5e172d2 ZJS |
4228 | serialize_addresses(f, NULL, &space, |
4229 | NULL, | |
4230 | link->dhcp_lease, | |
4231 | link->network->dhcp_use_dns, | |
ddb82ec2 | 4232 | SD_DHCP_LEASE_DNS, |
d5e172d2 ZJS |
4233 | dhcp6_lease, |
4234 | link->network->dhcp6_use_dns, | |
4235 | sd_dhcp6_lease_get_dns, | |
4236 | NULL); | |
1e7a0e21 LP |
4237 | |
4238 | /* Make sure to flush out old entries before we use the NDISC data */ | |
4239 | ndisc_vacuum(link); | |
4240 | ||
b296797f | 4241 | if (link->network->ipv6_accept_ra_use_dns && link->ndisc_rdnss) { |
1e7a0e21 LP |
4242 | NDiscRDNSS *dd; |
4243 | ||
d8bff5cc ZJS |
4244 | SET_FOREACH(dd, link->ndisc_rdnss, i) |
4245 | serialize_in6_addrs(f, &dd->address, 1, &space); | |
299d578f SS |
4246 | } |
4247 | ||
4248 | fputc('\n', f); | |
4249 | ||
d5e172d2 ZJS |
4250 | /************************************************************/ |
4251 | ||
4252 | serialize_addresses(f, "NTP", NULL, | |
4253 | link->ntp ?: link->network->ntp, | |
4254 | link->dhcp_lease, | |
4255 | link->network->dhcp_use_ntp, | |
ddb82ec2 | 4256 | SD_DHCP_LEASE_NTP, |
d5e172d2 ZJS |
4257 | dhcp6_lease, |
4258 | link->network->dhcp6_use_ntp, | |
4259 | sd_dhcp6_lease_get_ntp_addrs, | |
4260 | sd_dhcp6_lease_get_ntp_fqdn); | |
4261 | ||
4262 | serialize_addresses(f, "SIP", NULL, | |
2a71d57f | 4263 | NULL, |
d5e172d2 ZJS |
4264 | link->dhcp_lease, |
4265 | link->network->dhcp_use_sip, | |
ddb82ec2 | 4266 | SD_DHCP_LEASE_SIP, |
2a71d57f | 4267 | NULL, false, NULL, NULL); |
d5e172d2 ZJS |
4268 | |
4269 | /************************************************************/ | |
bd8f6538 | 4270 | |
b2a81c0b | 4271 | if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) { |
b85bc551 | 4272 | if (link->dhcp_lease) { |
b2a81c0b | 4273 | (void) sd_dhcp_lease_get_domainname(link->dhcp_lease, &dhcp_domainname); |
b85bc551 DW |
4274 | (void) sd_dhcp_lease_get_search_domains(link->dhcp_lease, &dhcp_domains); |
4275 | } | |
b2a81c0b LP |
4276 | if (dhcp6_lease) |
4277 | (void) sd_dhcp6_lease_get_domains(dhcp6_lease, &dhcp6_domains); | |
07bdc70d PF |
4278 | } |
4279 | ||
fe0e16db YW |
4280 | fputs("DOMAINS=", f); |
4281 | space = false; | |
15761549 | 4282 | ORDERED_SET_FOREACH(p, link->search_domains ?: link->network->search_domains, i) |
fe0e16db | 4283 | fputs_with_space(f, p, NULL, &space); |
07bdc70d | 4284 | |
1e7a0e21 | 4285 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) { |
1e7a0e21 LP |
4286 | if (dhcp_domainname) |
4287 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
b85bc551 DW |
4288 | if (dhcp_domains) |
4289 | fputstrv(f, dhcp_domains, NULL, &space); | |
1e7a0e21 LP |
4290 | if (dhcp6_domains) |
4291 | fputstrv(f, dhcp6_domains, NULL, &space); | |
a0cd6da5 YW |
4292 | } |
4293 | ||
4294 | if (link->network->ipv6_accept_ra_use_domains == DHCP_USE_DOMAINS_YES) { | |
4295 | NDiscDNSSL *dd; | |
1e7a0e21 LP |
4296 | |
4297 | SET_FOREACH(dd, link->ndisc_dnssl, i) | |
4298 | fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space); | |
4299 | } | |
b2a81c0b | 4300 | |
0d536673 | 4301 | fputc('\n', f); |
6192b846 | 4302 | |
d5e172d2 ZJS |
4303 | /************************************************************/ |
4304 | ||
fe0e16db YW |
4305 | fputs("ROUTE_DOMAINS=", f); |
4306 | space = false; | |
15761549 | 4307 | ORDERED_SET_FOREACH(p, link->route_domains ?: link->network->route_domains, i) |
fe0e16db | 4308 | fputs_with_space(f, p, NULL, &space); |
b2a81c0b | 4309 | |
1e7a0e21 | 4310 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_ROUTE) { |
1e7a0e21 LP |
4311 | if (dhcp_domainname) |
4312 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
b85bc551 DW |
4313 | if (dhcp_domains) |
4314 | fputstrv(f, dhcp_domains, NULL, &space); | |
1e7a0e21 LP |
4315 | if (dhcp6_domains) |
4316 | fputstrv(f, dhcp6_domains, NULL, &space); | |
a0cd6da5 YW |
4317 | } |
4318 | ||
4319 | if (link->network->ipv6_accept_ra_use_domains == DHCP_USE_DOMAINS_ROUTE) { | |
4320 | NDiscDNSSL *dd; | |
1e7a0e21 LP |
4321 | |
4322 | SET_FOREACH(dd, link->ndisc_dnssl, i) | |
4323 | fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space); | |
4324 | } | |
b2a81c0b | 4325 | |
0d536673 | 4326 | fputc('\n', f); |
67272d15 | 4327 | |
d5e172d2 ZJS |
4328 | /************************************************************/ |
4329 | ||
3c9b8860 | 4330 | fprintf(f, "LLMNR=%s\n", |
15761549 | 4331 | resolve_support_to_string(link->llmnr >= 0 ? link->llmnr : link->network->llmnr)); |
d5e172d2 ZJS |
4332 | |
4333 | /************************************************************/ | |
4334 | ||
aaa297d4 | 4335 | fprintf(f, "MDNS=%s\n", |
15761549 | 4336 | resolve_support_to_string(link->mdns >= 0 ? link->mdns : link->network->mdns)); |
15761549 | 4337 | |
d5e172d2 ZJS |
4338 | /************************************************************/ |
4339 | ||
4340 | int dns_default_route = | |
4341 | link->dns_default_route >= 0 ? link->dns_default_route : | |
4342 | link->network->dns_default_route; | |
4343 | if (dns_default_route >= 0) | |
4344 | fprintf(f, "DNS_DEFAULT_ROUTE=%s\n", yes_no(dns_default_route)); | |
4345 | ||
4346 | /************************************************************/ | |
4347 | ||
4348 | DnsOverTlsMode dns_over_tls_mode = | |
4349 | link->dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID ? link->dns_over_tls_mode : | |
4350 | link->network->dns_over_tls_mode; | |
4351 | if (dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID) | |
4352 | fprintf(f, "DNS_OVER_TLS=%s\n", dns_over_tls_mode_to_string(dns_over_tls_mode)); | |
4353 | ||
4354 | /************************************************************/ | |
4355 | ||
4356 | DnssecMode dnssec_mode = | |
4357 | link->dnssec_mode != _DNSSEC_MODE_INVALID ? link->dnssec_mode : | |
4358 | link->network->dnssec_mode; | |
4359 | if (dnssec_mode != _DNSSEC_MODE_INVALID) | |
4360 | fprintf(f, "DNSSEC=%s\n", dnssec_mode_to_string(dnssec_mode)); | |
4361 | ||
4362 | /************************************************************/ | |
4363 | ||
4364 | Set *nta_anchors = link->dnssec_negative_trust_anchors; | |
4365 | if (set_isempty(nta_anchors)) | |
4366 | nta_anchors = link->network->dnssec_negative_trust_anchors; | |
4367 | ||
4368 | if (!set_isempty(nta_anchors)) { | |
8a516214 LP |
4369 | const char *n; |
4370 | ||
0d536673 | 4371 | fputs("DNSSEC_NTA=", f); |
8a516214 | 4372 | space = false; |
d5e172d2 | 4373 | SET_FOREACH(n, nta_anchors, i) |
d390f8ef | 4374 | fputs_with_space(f, n, NULL, &space); |
0d536673 | 4375 | fputc('\n', f); |
8a516214 LP |
4376 | } |
4377 | ||
d5e172d2 ZJS |
4378 | /************************************************************/ |
4379 | ||
0d536673 | 4380 | fputs("ADDRESSES=", f); |
e7780c8d TG |
4381 | space = false; |
4382 | SET_FOREACH(a, link->addresses, i) { | |
4383 | _cleanup_free_ char *address_str = NULL; | |
4384 | ||
4385 | r = in_addr_to_string(a->family, &a->in_addr, &address_str); | |
4386 | if (r < 0) | |
4387 | goto fail; | |
4388 | ||
e7780c8d TG |
4389 | fprintf(f, "%s%s/%u", space ? " " : "", address_str, a->prefixlen); |
4390 | space = true; | |
4391 | } | |
0d536673 | 4392 | fputc('\n', f); |
c1eb9872 | 4393 | |
d5e172d2 ZJS |
4394 | /************************************************************/ |
4395 | ||
0d536673 | 4396 | fputs("ROUTES=", f); |
c1eb9872 TG |
4397 | space = false; |
4398 | SET_FOREACH(route, link->routes, i) { | |
4399 | _cleanup_free_ char *route_str = NULL; | |
4400 | ||
4401 | r = in_addr_to_string(route->family, &route->dst, &route_str); | |
4402 | if (r < 0) | |
4403 | goto fail; | |
4404 | ||
ecc3f340 ZJS |
4405 | fprintf(f, "%s%s/%hhu/%hhu/%"PRIu32"/%"PRIu32"/"USEC_FMT, |
4406 | space ? " " : "", route_str, | |
f833694d | 4407 | route->dst_prefixlen, route->tos, route->priority, route->table, route->lifetime); |
c1eb9872 TG |
4408 | space = true; |
4409 | } | |
4410 | ||
0d536673 | 4411 | fputc('\n', f); |
bcb7a07e | 4412 | } |
7374f9d8 | 4413 | |
b295beea LP |
4414 | print_link_hashmap(f, "CARRIER_BOUND_TO=", link->bound_to_links); |
4415 | print_link_hashmap(f, "CARRIER_BOUND_BY=", link->bound_by_links); | |
0d4ad91d | 4416 | |
8eb9058d | 4417 | if (link->dhcp_lease) { |
bd91b83e | 4418 | r = dhcp_lease_save(link->dhcp_lease, link->lease_file); |
fe8db0c5 | 4419 | if (r < 0) |
c2d6bd61 | 4420 | goto fail; |
fe8db0c5 | 4421 | |
7374f9d8 | 4422 | fprintf(f, |
b0e39c82 TG |
4423 | "DHCP_LEASE=%s\n", |
4424 | link->lease_file); | |
deb2e523 | 4425 | } else |
6990fb6b | 4426 | (void) unlink(link->lease_file); |
fe8db0c5 | 4427 | |
0bc70f1d TG |
4428 | if (link->ipv4ll) { |
4429 | struct in_addr address; | |
4430 | ||
4431 | r = sd_ipv4ll_get_address(link->ipv4ll, &address); | |
4432 | if (r >= 0) { | |
0d536673 | 4433 | fputs("IPV4LL_ADDRESS=", f); |
072320ea | 4434 | serialize_in_addrs(f, &address, 1, false, NULL); |
0d536673 | 4435 | fputc('\n', f); |
0bc70f1d TG |
4436 | } |
4437 | } | |
4438 | ||
7e738e7b SS |
4439 | if (link->dhcp6_client) { |
4440 | _cleanup_free_ char *duid = NULL; | |
4441 | uint32_t iaid; | |
4442 | ||
4443 | r = sd_dhcp6_client_get_iaid(link->dhcp6_client, &iaid); | |
4444 | if (r >= 0) | |
4445 | fprintf(f, "DHCP6_CLIENT_IAID=0x%x\n", iaid); | |
4446 | ||
4447 | r = sd_dhcp6_client_duid_as_string(link->dhcp6_client, &duid); | |
4448 | if (r >= 0) | |
4449 | fprintf(f, "DHCP6_CLIENT_DUID=%s\n", duid); | |
4450 | } | |
18d8a2cf | 4451 | |
c2d6bd61 LP |
4452 | r = fflush_and_check(f); |
4453 | if (r < 0) | |
4454 | goto fail; | |
fe8db0c5 | 4455 | |
c2d6bd61 | 4456 | if (rename(temp_path, link->state_file) < 0) { |
fe8db0c5 | 4457 | r = -errno; |
c2d6bd61 | 4458 | goto fail; |
fe8db0c5 TG |
4459 | } |
4460 | ||
c2d6bd61 | 4461 | return 0; |
dacd6cee | 4462 | |
c2d6bd61 | 4463 | fail: |
6a7a4e4d | 4464 | (void) unlink(link->state_file); |
6a7a4e4d LP |
4465 | if (temp_path) |
4466 | (void) unlink(temp_path); | |
4467 | ||
dacd6cee | 4468 | return log_link_error_errno(link, r, "Failed to save link data to %s: %m", link->state_file); |
fe8db0c5 TG |
4469 | } |
4470 | ||
84de38c5 TG |
4471 | /* The serialized state in /run is no longer up-to-date. */ |
4472 | void link_dirty(Link *link) { | |
4473 | int r; | |
4474 | ||
4475 | assert(link); | |
4476 | ||
0c241a37 SS |
4477 | /* mark manager dirty as link is dirty */ |
4478 | manager_dirty(link->manager); | |
4479 | ||
de7fef4b | 4480 | r = set_ensure_put(&link->manager->dirty_links, NULL, link); |
0c241a37 | 4481 | if (r <= 0) |
de7fef4b | 4482 | /* Ignore allocation errors and don't take another ref if the link was already dirty */ |
84de38c5 | 4483 | return; |
84de38c5 TG |
4484 | link_ref(link); |
4485 | } | |
4486 | ||
4487 | /* The serialized state in /run is up-to-date */ | |
4488 | void link_clean(Link *link) { | |
4489 | assert(link); | |
4490 | assert(link->manager); | |
4491 | ||
1046bf9b | 4492 | link_unref(set_remove(link->manager->dirty_links, link)); |
84de38c5 TG |
4493 | } |
4494 | ||
fe8db0c5 | 4495 | static const char* const link_state_table[_LINK_STATE_MAX] = { |
8434fd5c | 4496 | [LINK_STATE_PENDING] = "pending", |
bd08ce56 | 4497 | [LINK_STATE_INITIALIZED] = "initialized", |
289e6774 | 4498 | [LINK_STATE_CONFIGURING] = "configuring", |
fe8db0c5 | 4499 | [LINK_STATE_CONFIGURED] = "configured", |
57bd6899 | 4500 | [LINK_STATE_UNMANAGED] = "unmanaged", |
fe8db0c5 | 4501 | [LINK_STATE_FAILED] = "failed", |
370e9930 | 4502 | [LINK_STATE_LINGER] = "linger", |
fe8db0c5 TG |
4503 | }; |
4504 | ||
4505 | DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState); | |
5ecb131d YW |
4506 | |
4507 | int log_link_message_full_errno(Link *link, sd_netlink_message *m, int level, int err, const char *msg) { | |
4508 | const char *err_msg = NULL; | |
4509 | ||
4510 | (void) sd_netlink_message_read_string(m, NLMSGERR_ATTR_MSG, &err_msg); | |
ab119e63 YW |
4511 | return log_link_full(link, level, err, |
4512 | "%s: %s%s%s%m", | |
4513 | msg, | |
4514 | strempty(err_msg), | |
4515 | err_msg && !endswith(err_msg, ".") ? "." : "", | |
4516 | err_msg ? " " : ""); | |
5ecb131d | 4517 | } |