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