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