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