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