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