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