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