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