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