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