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