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