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