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