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