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