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