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