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