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