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