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