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