]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-link.c
network: DHCP4 client ID save in state file
[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) || link_ipv6_accept_ra_enabled(link)) &&
1137 !link->dhcp4_configured &&
1138 !link->dhcp6_configured &&
1139 !link->ndisc_configured &&
1140 !(link_ipv4ll_enabled(link, ADDRESS_FAMILY_FALLBACK_IPV4) && link->ipv4ll_address))
1141 /* When DHCP or RA is enabled, at least one protocol must provide an address, or
1142 * an IPv4ll fallback address must be configured. */
1143 return;
1144 }
1145
1146 if (link->state != LINK_STATE_CONFIGURED)
1147 link_enter_configured(link);
1148
1149 return;
1150 }
1151
1152 static int link_request_set_neighbors(Link *link) {
1153 Neighbor *neighbor;
1154 int r;
1155
1156 assert(link);
1157 assert(link->network);
1158 assert(link->state != _LINK_STATE_INVALID);
1159
1160 link->neighbors_configured = false;
1161
1162 LIST_FOREACH(neighbors, neighbor, link->network->neighbors) {
1163 r = neighbor_configure(neighbor, link, NULL);
1164 if (r < 0)
1165 return log_link_warning_errno(link, r, "Could not set neighbor: %m");
1166 }
1167
1168 if (link->neighbor_messages == 0) {
1169 link->neighbors_configured = true;
1170 link_check_ready(link);
1171 } else {
1172 log_link_debug(link, "Setting neighbors");
1173 link_set_state(link, LINK_STATE_CONFIGURING);
1174 }
1175
1176 return 0;
1177 }
1178
1179 int address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1180 int r;
1181
1182 assert(rtnl);
1183 assert(m);
1184 assert(link);
1185 assert(link->ifname);
1186 assert(link->address_messages > 0);
1187 assert(IN_SET(link->state, LINK_STATE_CONFIGURING,
1188 LINK_STATE_FAILED, LINK_STATE_LINGER));
1189
1190 link->address_messages--;
1191
1192 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1193 return 1;
1194
1195 r = sd_netlink_message_get_errno(m);
1196 if (r < 0 && r != -EEXIST) {
1197 log_link_message_warning_errno(link, m, r, "Could not set address");
1198 link_enter_failed(link);
1199 return 1;
1200 } else if (r >= 0)
1201 (void) manager_rtnl_process_address(rtnl, m, link->manager);
1202
1203 if (link->address_messages == 0) {
1204 log_link_debug(link, "Addresses set");
1205 link->addresses_configured = true;
1206 link_check_ready(link);
1207 }
1208
1209 return 1;
1210 }
1211
1212 static int link_set_bridge_fdb(Link *link) {
1213 FdbEntry *fdb_entry;
1214 int r;
1215
1216 LIST_FOREACH(static_fdb_entries, fdb_entry, link->network->static_fdb_entries) {
1217 r = fdb_entry_configure(link, fdb_entry);
1218 if (r < 0)
1219 return log_link_error_errno(link, r, "Failed to add MAC entry to static MAC table: %m");
1220 }
1221
1222 return 0;
1223 }
1224
1225 static int link_request_set_addresses(Link *link) {
1226 AddressLabel *label;
1227 Address *ad;
1228 Prefix *p;
1229 int r;
1230
1231 assert(link);
1232 assert(link->network);
1233 assert(link->state != _LINK_STATE_INVALID);
1234
1235 /* Reset all *_configured flags we are configuring. */
1236 link->addresses_configured = false;
1237 link->addresses_ready = false;
1238 link->neighbors_configured = false;
1239 link->static_routes_configured = false;
1240 link->static_routes_ready = false;
1241 link->static_nexthops_configured = false;
1242 link->routing_policy_rules_configured = false;
1243
1244 r = link_set_bridge_fdb(link);
1245 if (r < 0)
1246 return r;
1247
1248 r = link_request_set_neighbors(link);
1249 if (r < 0)
1250 return r;
1251
1252 LIST_FOREACH(addresses, ad, link->network->static_addresses) {
1253 bool update;
1254
1255 update = address_get(link, ad->family, &ad->in_addr, ad->prefixlen, NULL) > 0;
1256
1257 r = address_configure(ad, link, address_handler, update);
1258 if (r < 0)
1259 return log_link_warning_errno(link, r, "Could not set addresses: %m");
1260 if (r > 0)
1261 link->address_messages++;
1262 }
1263
1264 if (IN_SET(link->network->router_prefix_delegation,
1265 RADV_PREFIX_DELEGATION_STATIC,
1266 RADV_PREFIX_DELEGATION_BOTH))
1267 LIST_FOREACH(prefixes, p, link->network->static_prefixes) {
1268 _cleanup_(address_freep) Address *address = NULL;
1269
1270 if (!p->assign)
1271 continue;
1272
1273 r = address_new(&address);
1274 if (r < 0)
1275 return log_link_error_errno(link, r, "Could not allocate address: %m");
1276
1277 r = sd_radv_prefix_get_prefix(p->radv_prefix, &address->in_addr.in6, &address->prefixlen);
1278 if (r < 0)
1279 return r;
1280
1281 r = generate_ipv6_eui_64_address(link, &address->in_addr.in6);
1282 if (r < 0)
1283 return r;
1284
1285 address->family = AF_INET6;
1286 r = address_configure(address, link, address_handler, true);
1287 if (r < 0)
1288 return log_link_warning_errno(link, r, "Could not set addresses: %m");
1289 if (r > 0)
1290 link->address_messages++;
1291 }
1292
1293 LIST_FOREACH(labels, label, link->network->address_labels) {
1294 r = address_label_configure(label, link, NULL, false);
1295 if (r < 0)
1296 return log_link_warning_errno(link, r, "Could not set address label: %m");
1297
1298 link->address_label_messages++;
1299 }
1300
1301 /* now that we can figure out a default address for the dhcp server,
1302 start it */
1303 if (link_dhcp4_server_enabled(link) && (link->flags & IFF_UP)) {
1304 r = dhcp4_server_configure(link);
1305 if (r < 0)
1306 return r;
1307 log_link_debug(link, "Offering DHCPv4 leases");
1308 }
1309
1310 if (link->address_messages == 0) {
1311 link->addresses_configured = true;
1312 link_check_ready(link);
1313 } else {
1314 log_link_debug(link, "Setting addresses");
1315 link_set_state(link, LINK_STATE_CONFIGURING);
1316 }
1317
1318 return 0;
1319 }
1320
1321 static int link_set_bridge_vlan(Link *link) {
1322 int r;
1323
1324 r = br_vlan_configure(link, link->network->pvid, link->network->br_vid_bitmap, link->network->br_untagged_bitmap);
1325 if (r < 0)
1326 log_link_error_errno(link, r, "Failed to assign VLANs to bridge port: %m");
1327
1328 return r;
1329 }
1330
1331 static int link_set_proxy_arp(Link *link) {
1332 int r;
1333
1334 if (!link_proxy_arp_enabled(link))
1335 return 0;
1336
1337 r = sysctl_write_ip_property_boolean(AF_INET, link->ifname, "proxy_arp", link->network->proxy_arp > 0);
1338 if (r < 0)
1339 log_link_warning_errno(link, r, "Cannot configure proxy ARP for interface: %m");
1340
1341 return 0;
1342 }
1343
1344 static int link_configure_continue(Link *link);
1345
1346 static int set_mtu_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1347 int r;
1348
1349 assert(m);
1350 assert(link);
1351 assert(link->ifname);
1352
1353 link->setting_mtu = false;
1354
1355 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1356 return 1;
1357
1358 r = sd_netlink_message_get_errno(m);
1359 if (r < 0)
1360 log_link_message_warning_errno(link, m, r, "Could not set MTU, ignoring");
1361 else
1362 log_link_debug(link, "Setting MTU done.");
1363
1364 if (link->state == LINK_STATE_INITIALIZED) {
1365 r = link_configure_continue(link);
1366 if (r < 0)
1367 link_enter_failed(link);
1368 }
1369
1370 return 1;
1371 }
1372
1373 int link_set_mtu(Link *link, uint32_t mtu) {
1374 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1375 int r;
1376
1377 assert(link);
1378 assert(link->manager);
1379 assert(link->manager->rtnl);
1380
1381 if (mtu == 0 || link->setting_mtu)
1382 return 0;
1383
1384 if (link->mtu == mtu)
1385 return 0;
1386
1387 log_link_debug(link, "Setting MTU: %" PRIu32, mtu);
1388
1389 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
1390 if (r < 0)
1391 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
1392
1393 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1394 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1395 if (link_ipv6_enabled(link) && mtu < IPV6_MIN_MTU) {
1396
1397 log_link_warning(link, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU) ", as "
1398 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU) " bytes");
1399
1400 mtu = IPV6_MIN_MTU;
1401 }
1402
1403 r = sd_netlink_message_append_u32(req, IFLA_MTU, mtu);
1404 if (r < 0)
1405 return log_link_error_errno(link, r, "Could not append MTU: %m");
1406
1407 r = netlink_call_async(link->manager->rtnl, NULL, req, set_mtu_handler,
1408 link_netlink_destroy_callback, link);
1409 if (r < 0)
1410 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
1411
1412 link_ref(link);
1413 link->setting_mtu = true;
1414
1415 return 0;
1416 }
1417
1418 static bool link_reduces_vlan_mtu(Link *link) {
1419 /* See netif_reduces_vlan_mtu() in kernel. */
1420 return streq_ptr(link->kind, "macsec");
1421 }
1422
1423 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link *link) {
1424 uint32_t mtu = 0;
1425 NetDev *dev;
1426 Iterator i;
1427
1428 HASHMAP_FOREACH(dev, link->network->stacked_netdevs, i)
1429 if (dev->kind == NETDEV_KIND_VLAN && dev->mtu > 0)
1430 /* See vlan_dev_change_mtu() in kernel. */
1431 mtu = MAX(mtu, link_reduces_vlan_mtu(link) ? dev->mtu + 4 : dev->mtu);
1432
1433 else if (dev->kind == NETDEV_KIND_MACVLAN && dev->mtu > mtu)
1434 /* See macvlan_change_mtu() in kernel. */
1435 mtu = dev->mtu;
1436
1437 return mtu;
1438 }
1439
1440 static int link_configure_mtu(Link *link) {
1441 uint32_t mtu;
1442
1443 assert(link);
1444 assert(link->network);
1445
1446 if (link->network->mtu > 0)
1447 return link_set_mtu(link, link->network->mtu);
1448
1449 mtu = link_get_requested_mtu_by_stacked_netdevs(link);
1450 if (link->mtu >= mtu)
1451 return 0;
1452
1453 log_link_notice(link, "Bumping MTU bytes from %"PRIu32" to %"PRIu32" because of stacked device. "
1454 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1455 link->mtu, mtu);
1456
1457 return link_set_mtu(link, mtu);
1458 }
1459
1460 static int set_flags_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1461 int r;
1462
1463 assert(m);
1464 assert(link);
1465 assert(link->ifname);
1466
1467 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1468 return 1;
1469
1470 r = sd_netlink_message_get_errno(m);
1471 if (r < 0)
1472 log_link_message_warning_errno(link, m, r, "Could not set link flags, ignoring");
1473
1474 return 1;
1475 }
1476
1477 static int link_set_flags(Link *link) {
1478 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1479 unsigned ifi_change = 0;
1480 unsigned ifi_flags = 0;
1481 int r;
1482
1483 assert(link);
1484 assert(link->manager);
1485 assert(link->manager->rtnl);
1486
1487 if (link->flags & IFF_LOOPBACK)
1488 return 0;
1489
1490 if (!link->network)
1491 return 0;
1492
1493 if (link->network->arp < 0 && link->network->multicast < 0 && link->network->allmulticast < 0)
1494 return 0;
1495
1496 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
1497 if (r < 0)
1498 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
1499
1500 if (link->network->arp >= 0) {
1501 ifi_change |= IFF_NOARP;
1502 SET_FLAG(ifi_flags, IFF_NOARP, link->network->arp == 0);
1503 }
1504
1505 if (link->network->multicast >= 0) {
1506 ifi_change |= IFF_MULTICAST;
1507 SET_FLAG(ifi_flags, IFF_MULTICAST, link->network->multicast);
1508 }
1509
1510 if (link->network->allmulticast >= 0) {
1511 ifi_change |= IFF_ALLMULTI;
1512 SET_FLAG(ifi_flags, IFF_ALLMULTI, link->network->allmulticast);
1513 }
1514
1515 r = sd_rtnl_message_link_set_flags(req, ifi_flags, ifi_change);
1516 if (r < 0)
1517 return log_link_error_errno(link, r, "Could not set link flags: %m");
1518
1519 r = netlink_call_async(link->manager->rtnl, NULL, req, set_flags_handler,
1520 link_netlink_destroy_callback, link);
1521 if (r < 0)
1522 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
1523
1524 link_ref(link);
1525
1526 return 0;
1527 }
1528
1529 static int link_acquire_ipv6_conf(Link *link) {
1530 int r;
1531
1532 assert(link);
1533
1534 if (link_ipv6_accept_ra_enabled(link)) {
1535 assert(link->ndisc);
1536
1537 log_link_debug(link, "Discovering IPv6 routers");
1538
1539 r = sd_ndisc_start(link->ndisc);
1540 if (r < 0 && r != -EBUSY)
1541 return log_link_warning_errno(link, r, "Could not start IPv6 Router Discovery: %m");
1542 }
1543
1544 if (link_radv_enabled(link)) {
1545 assert(link->radv);
1546 assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0);
1547
1548 log_link_debug(link, "Starting IPv6 Router Advertisements");
1549
1550 r = radv_emit_dns(link);
1551 if (r < 0)
1552 return log_link_warning_errno(link, r, "Failed to configure DNS or Domains in IPv6 Router Advertisement: %m");
1553
1554 r = sd_radv_start(link->radv);
1555 if (r < 0 && r != -EBUSY)
1556 return log_link_warning_errno(link, r, "Could not start IPv6 Router Advertisement: %m");
1557 }
1558
1559 if (link_dhcp6_enabled(link) && link->network->dhcp6_without_ra) {
1560 assert(link->dhcp6_client);
1561 assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0);
1562
1563 r = dhcp6_request_address(link, true);
1564 if (r < 0 && r != -EBUSY)
1565 return log_link_warning_errno(link, r, "Could not acquire DHCPv6 lease: %m");
1566 else
1567 log_link_debug(link, "Acquiring DHCPv6 lease");
1568 }
1569
1570 (void) dhcp6_request_prefix_delegation(link);
1571
1572 return 0;
1573 }
1574
1575 static int link_acquire_ipv4_conf(Link *link) {
1576 int r;
1577
1578 assert(link);
1579 assert(link->manager);
1580 assert(link->manager->event);
1581
1582 if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_IPV4)) {
1583 assert(link->ipv4ll);
1584
1585 log_link_debug(link, "Acquiring IPv4 link-local address");
1586
1587 r = sd_ipv4ll_start(link->ipv4ll);
1588 if (r < 0)
1589 return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
1590 }
1591
1592 if (link_dhcp4_enabled(link)) {
1593 assert(link->dhcp_client);
1594
1595 log_link_debug(link, "Acquiring DHCPv4 lease");
1596
1597 r = sd_dhcp_client_start(link->dhcp_client);
1598 if (r < 0)
1599 return log_link_warning_errno(link, r, "Could not acquire DHCPv4 lease: %m");
1600 }
1601
1602 return 0;
1603 }
1604
1605 static int link_acquire_conf(Link *link) {
1606 int r;
1607
1608 assert(link);
1609
1610 r = link_acquire_ipv4_conf(link);
1611 if (r < 0)
1612 return r;
1613
1614 if (!in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address)) {
1615 r = link_acquire_ipv6_conf(link);
1616 if (r < 0)
1617 return r;
1618 }
1619
1620 if (link_lldp_emit_enabled(link)) {
1621 r = link_lldp_emit_start(link);
1622 if (r < 0)
1623 return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m");
1624 }
1625
1626 return 0;
1627 }
1628
1629 bool link_has_carrier(Link *link) {
1630 /* see Documentation/networking/operstates.txt in the kernel sources */
1631
1632 if (link->kernel_operstate == IF_OPER_UP)
1633 return true;
1634
1635 if (link->kernel_operstate == IF_OPER_UNKNOWN)
1636 /* operstate may not be implemented, so fall back to flags */
1637 if (FLAGS_SET(link->flags, IFF_LOWER_UP | IFF_RUNNING) &&
1638 !FLAGS_SET(link->flags, IFF_DORMANT))
1639 return true;
1640
1641 return false;
1642 }
1643
1644 static int link_address_genmode_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1645 int r;
1646
1647 assert(link);
1648
1649 link->setting_genmode = false;
1650
1651 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1652 return 1;
1653
1654 r = sd_netlink_message_get_errno(m);
1655 if (r < 0)
1656 log_link_message_warning_errno(link, m, r, "Could not set address genmode for interface, ignoring");
1657 else
1658 log_link_debug(link, "Setting address genmode done.");
1659
1660 if (link->state == LINK_STATE_INITIALIZED) {
1661 r = link_configure_continue(link);
1662 if (r < 0)
1663 link_enter_failed(link);
1664 }
1665
1666 return 1;
1667 }
1668
1669 static int link_configure_addrgen_mode(Link *link) {
1670 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1671 uint8_t ipv6ll_mode;
1672 int r;
1673
1674 assert(link);
1675 assert(link->network);
1676 assert(link->manager);
1677 assert(link->manager->rtnl);
1678
1679 if (!socket_ipv6_is_supported() || link->setting_genmode)
1680 return 0;
1681
1682 log_link_debug(link, "Setting address genmode for link");
1683
1684 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
1685 if (r < 0)
1686 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
1687
1688 r = sd_netlink_message_open_container(req, IFLA_AF_SPEC);
1689 if (r < 0)
1690 return log_link_error_errno(link, r, "Could not open IFLA_AF_SPEC container: %m");
1691
1692 r = sd_netlink_message_open_container(req, AF_INET6);
1693 if (r < 0)
1694 return log_link_error_errno(link, r, "Could not open AF_INET6 container: %m");
1695
1696 if (!link_ipv6ll_enabled(link))
1697 ipv6ll_mode = IN6_ADDR_GEN_MODE_NONE;
1698 else if (link->network->ipv6_address_gen_mode < 0) {
1699 r = sysctl_read_ip_property(AF_INET6, link->ifname, "stable_secret", NULL);
1700 if (r < 0) {
1701 /* The file may not exist. And even if it exists, when stable_secret is unset,
1702 * reading the file fails with EIO. */
1703 log_link_debug_errno(link, r, "Failed to read sysctl property stable_secret: %m");
1704
1705 ipv6ll_mode = IN6_ADDR_GEN_MODE_EUI64;
1706 } else
1707 ipv6ll_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
1708 } else
1709 ipv6ll_mode = link->network->ipv6_address_gen_mode;
1710
1711 r = sd_netlink_message_append_u8(req, IFLA_INET6_ADDR_GEN_MODE, ipv6ll_mode);
1712 if (r < 0)
1713 return log_link_error_errno(link, r, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1714
1715 r = sd_netlink_message_close_container(req);
1716 if (r < 0)
1717 return log_link_error_errno(link, r, "Could not close AF_INET6 container: %m");
1718
1719 r = sd_netlink_message_close_container(req);
1720 if (r < 0)
1721 return log_link_error_errno(link, r, "Could not close IFLA_AF_SPEC container: %m");
1722
1723 r = netlink_call_async(link->manager->rtnl, NULL, req, link_address_genmode_handler,
1724 link_netlink_destroy_callback, link);
1725 if (r < 0)
1726 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
1727
1728 link_ref(link);
1729 link->setting_genmode = true;
1730
1731 return 0;
1732 }
1733
1734 static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1735 int r;
1736
1737 assert(link);
1738
1739 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1740 return 1;
1741
1742 r = sd_netlink_message_get_errno(m);
1743 if (r < 0)
1744 /* we warn but don't fail the link, as it may be brought up later */
1745 log_link_message_warning_errno(link, m, r, "Could not bring up interface");
1746
1747 return 1;
1748 }
1749
1750 static int link_up(Link *link) {
1751 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1752 int r;
1753
1754 assert(link);
1755 assert(link->network);
1756 assert(link->manager);
1757 assert(link->manager->rtnl);
1758
1759 log_link_debug(link, "Bringing link up");
1760
1761 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
1762 if (r < 0)
1763 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
1764
1765 /* set it free if not enslaved with networkd */
1766 if (!link->network->bridge && !link->network->bond && !link->network->vrf) {
1767 r = sd_netlink_message_append_u32(req, IFLA_MASTER, 0);
1768 if (r < 0)
1769 return log_link_error_errno(link, r, "Could not append IFLA_MASTER attribute: %m");
1770 }
1771
1772 r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
1773 if (r < 0)
1774 return log_link_error_errno(link, r, "Could not set link flags: %m");
1775
1776 if (link->network->mac) {
1777 r = sd_netlink_message_append_ether_addr(req, IFLA_ADDRESS, link->network->mac);
1778 if (r < 0)
1779 return log_link_error_errno(link, r, "Could not set MAC address: %m");
1780 }
1781
1782 r = netlink_call_async(link->manager->rtnl, NULL, req, link_up_handler,
1783 link_netlink_destroy_callback, link);
1784 if (r < 0)
1785 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
1786
1787 link_ref(link);
1788
1789 return 0;
1790 }
1791
1792 static int link_down_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1793 int r;
1794
1795 assert(link);
1796
1797 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1798 return 1;
1799
1800 r = sd_netlink_message_get_errno(m);
1801 if (r < 0)
1802 log_link_message_warning_errno(link, m, r, "Could not bring down interface");
1803
1804 return 1;
1805 }
1806
1807 int link_down(Link *link, link_netlink_message_handler_t callback) {
1808 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1809 int r;
1810
1811 assert(link);
1812 assert(link->manager);
1813 assert(link->manager->rtnl);
1814
1815 log_link_debug(link, "Bringing link down");
1816
1817 r = sd_rtnl_message_new_link(link->manager->rtnl, &req,
1818 RTM_SETLINK, link->ifindex);
1819 if (r < 0)
1820 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
1821
1822 r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP);
1823 if (r < 0)
1824 return log_link_error_errno(link, r, "Could not set link flags: %m");
1825
1826 r = netlink_call_async(link->manager->rtnl, NULL, req,
1827 callback ?: link_down_handler,
1828 link_netlink_destroy_callback, link);
1829 if (r < 0)
1830 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
1831
1832 link_ref(link);
1833
1834 return 0;
1835 }
1836
1837 static int link_group_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1838 int r;
1839
1840 assert(link);
1841
1842 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1843 return 1;
1844
1845 r = sd_netlink_message_get_errno(m);
1846 if (r < 0)
1847 log_link_message_warning_errno(link, m, r, "Could not set group for the interface");
1848
1849 return 1;
1850 }
1851
1852 static int link_set_group(Link *link) {
1853 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1854 int r;
1855
1856 assert(link);
1857 assert(link->network);
1858 assert(link->manager);
1859 assert(link->manager->rtnl);
1860
1861 if (link->network->group <= 0)
1862 return 0;
1863
1864 log_link_debug(link, "Setting group");
1865
1866 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
1867 if (r < 0)
1868 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
1869
1870 r = sd_netlink_message_append_u32(req, IFLA_GROUP, link->network->group);
1871 if (r < 0)
1872 return log_link_error_errno(link, r, "Could not set link group: %m");
1873
1874 r = netlink_call_async(link->manager->rtnl, NULL, req, link_group_handler,
1875 link_netlink_destroy_callback, link);
1876 if (r < 0)
1877 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
1878
1879 link_ref(link);
1880
1881 return 0;
1882 }
1883
1884 static int link_handle_bound_to_list(Link *link) {
1885 Link *l;
1886 Iterator i;
1887 int r;
1888 bool required_up = false;
1889 bool link_is_up = false;
1890
1891 assert(link);
1892
1893 if (hashmap_isempty(link->bound_to_links))
1894 return 0;
1895
1896 if (link->flags & IFF_UP)
1897 link_is_up = true;
1898
1899 HASHMAP_FOREACH (l, link->bound_to_links, i)
1900 if (link_has_carrier(l)) {
1901 required_up = true;
1902 break;
1903 }
1904
1905 if (!required_up && link_is_up) {
1906 r = link_down(link, NULL);
1907 if (r < 0)
1908 return r;
1909 } else if (required_up && !link_is_up) {
1910 r = link_up(link);
1911 if (r < 0)
1912 return r;
1913 }
1914
1915 return 0;
1916 }
1917
1918 static int link_handle_bound_by_list(Link *link) {
1919 Iterator i;
1920 Link *l;
1921 int r;
1922
1923 assert(link);
1924
1925 if (hashmap_isempty(link->bound_by_links))
1926 return 0;
1927
1928 HASHMAP_FOREACH (l, link->bound_by_links, i) {
1929 r = link_handle_bound_to_list(l);
1930 if (r < 0)
1931 return r;
1932 }
1933
1934 return 0;
1935 }
1936
1937 static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) {
1938 int r;
1939
1940 assert(link);
1941 assert(carrier);
1942
1943 if (link == carrier)
1944 return 0;
1945
1946 if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex)))
1947 return 0;
1948
1949 r = hashmap_ensure_allocated(h, NULL);
1950 if (r < 0)
1951 return r;
1952
1953 r = hashmap_put(*h, INT_TO_PTR(carrier->ifindex), carrier);
1954 if (r < 0)
1955 return r;
1956
1957 return 0;
1958 }
1959
1960 static int link_new_bound_by_list(Link *link) {
1961 Manager *m;
1962 Link *carrier;
1963 Iterator i;
1964 int r;
1965 bool list_updated = false;
1966
1967 assert(link);
1968 assert(link->manager);
1969
1970 m = link->manager;
1971
1972 HASHMAP_FOREACH(carrier, m->links, i) {
1973 if (!carrier->network)
1974 continue;
1975
1976 if (strv_isempty(carrier->network->bind_carrier))
1977 continue;
1978
1979 if (strv_fnmatch(carrier->network->bind_carrier, link->ifname)) {
1980 r = link_put_carrier(link, carrier, &link->bound_by_links);
1981 if (r < 0)
1982 return r;
1983
1984 list_updated = true;
1985 }
1986 }
1987
1988 if (list_updated)
1989 link_dirty(link);
1990
1991 HASHMAP_FOREACH(carrier, link->bound_by_links, i) {
1992 r = link_put_carrier(carrier, link, &carrier->bound_to_links);
1993 if (r < 0)
1994 return r;
1995
1996 link_dirty(carrier);
1997 }
1998
1999 return 0;
2000 }
2001
2002 static int link_new_bound_to_list(Link *link) {
2003 Manager *m;
2004 Link *carrier;
2005 Iterator i;
2006 int r;
2007 bool list_updated = false;
2008
2009 assert(link);
2010 assert(link->manager);
2011
2012 if (!link->network)
2013 return 0;
2014
2015 if (strv_isempty(link->network->bind_carrier))
2016 return 0;
2017
2018 m = link->manager;
2019
2020 HASHMAP_FOREACH (carrier, m->links, i) {
2021 if (strv_fnmatch(link->network->bind_carrier, carrier->ifname)) {
2022 r = link_put_carrier(link, carrier, &link->bound_to_links);
2023 if (r < 0)
2024 return r;
2025
2026 list_updated = true;
2027 }
2028 }
2029
2030 if (list_updated)
2031 link_dirty(link);
2032
2033 HASHMAP_FOREACH (carrier, link->bound_to_links, i) {
2034 r = link_put_carrier(carrier, link, &carrier->bound_by_links);
2035 if (r < 0)
2036 return r;
2037
2038 link_dirty(carrier);
2039 }
2040
2041 return 0;
2042 }
2043
2044 static int link_new_carrier_maps(Link *link) {
2045 int r;
2046
2047 r = link_new_bound_by_list(link);
2048 if (r < 0)
2049 return r;
2050
2051 r = link_handle_bound_by_list(link);
2052 if (r < 0)
2053 return r;
2054
2055 r = link_new_bound_to_list(link);
2056 if (r < 0)
2057 return r;
2058
2059 r = link_handle_bound_to_list(link);
2060 if (r < 0)
2061 return r;
2062
2063 return 0;
2064 }
2065
2066 static void link_free_bound_to_list(Link *link) {
2067 Link *bound_to;
2068 Iterator i;
2069
2070 HASHMAP_FOREACH (bound_to, link->bound_to_links, i) {
2071 hashmap_remove(link->bound_to_links, INT_TO_PTR(bound_to->ifindex));
2072
2073 if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex)))
2074 link_dirty(bound_to);
2075 }
2076
2077 return;
2078 }
2079
2080 static void link_free_bound_by_list(Link *link) {
2081 Link *bound_by;
2082 Iterator i;
2083
2084 HASHMAP_FOREACH (bound_by, link->bound_by_links, i) {
2085 hashmap_remove(link->bound_by_links, INT_TO_PTR(bound_by->ifindex));
2086
2087 if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) {
2088 link_dirty(bound_by);
2089 link_handle_bound_to_list(bound_by);
2090 }
2091 }
2092
2093 return;
2094 }
2095
2096 static void link_free_carrier_maps(Link *link) {
2097 bool list_updated = false;
2098
2099 assert(link);
2100
2101 if (!hashmap_isempty(link->bound_to_links)) {
2102 link_free_bound_to_list(link);
2103 list_updated = true;
2104 }
2105
2106 if (!hashmap_isempty(link->bound_by_links)) {
2107 link_free_bound_by_list(link);
2108 list_updated = true;
2109 }
2110
2111 if (list_updated)
2112 link_dirty(link);
2113
2114 return;
2115 }
2116
2117 static int link_append_to_master(Link *link, NetDev *netdev) {
2118 Link *master;
2119 int r;
2120
2121 assert(link);
2122 assert(netdev);
2123
2124 r = link_get(link->manager, netdev->ifindex, &master);
2125 if (r < 0)
2126 return r;
2127
2128 r = set_ensure_allocated(&master->slaves, NULL);
2129 if (r < 0)
2130 return r;
2131
2132 r = set_put(master->slaves, link);
2133 if (r <= 0)
2134 return r;
2135
2136 link_ref(link);
2137 return 0;
2138 }
2139
2140 static void link_drop_from_master(Link *link, NetDev *netdev) {
2141 Link *master;
2142
2143 assert(link);
2144
2145 if (!link->manager || !netdev)
2146 return;
2147
2148 if (link_get(link->manager, netdev->ifindex, &master) < 0)
2149 return;
2150
2151 link_unref(set_remove(master->slaves, link));
2152 }
2153
2154 static void link_detach_from_manager(Link *link) {
2155 if (!link || !link->manager)
2156 return;
2157
2158 link_unref(set_remove(link->manager->links_requesting_uuid, link));
2159 link_clean(link);
2160
2161 /* The following must be called at last. */
2162 assert_se(hashmap_remove(link->manager->links, INT_TO_PTR(link->ifindex)) == link);
2163 link_unref(link);
2164 }
2165
2166 void link_drop(Link *link) {
2167 if (!link || link->state == LINK_STATE_LINGER)
2168 return;
2169
2170 link_set_state(link, LINK_STATE_LINGER);
2171
2172 link_free_carrier_maps(link);
2173
2174 if (link->network) {
2175 link_drop_from_master(link, link->network->bridge);
2176 link_drop_from_master(link, link->network->bond);
2177 }
2178
2179 log_link_debug(link, "Link removed");
2180
2181 (void) unlink(link->state_file);
2182 link_detach_from_manager(link);
2183 }
2184
2185 static int link_joined(Link *link) {
2186 int r;
2187
2188 assert(link);
2189 assert(link->network);
2190
2191 if (!hashmap_isempty(link->bound_to_links)) {
2192 r = link_handle_bound_to_list(link);
2193 if (r < 0)
2194 return r;
2195 } else if (!(link->flags & IFF_UP)) {
2196 r = link_up(link);
2197 if (r < 0) {
2198 link_enter_failed(link);
2199 return r;
2200 }
2201 }
2202
2203 if (link->network->bridge) {
2204 r = link_set_bridge(link);
2205 if (r < 0)
2206 log_link_error_errno(link, r, "Could not set bridge message: %m");
2207
2208 r = link_append_to_master(link, link->network->bridge);
2209 if (r < 0)
2210 log_link_error_errno(link, r, "Failed to add to bridge master's slave list: %m");
2211 }
2212
2213 if (link->network->bond) {
2214 r = link_set_bond(link);
2215 if (r < 0)
2216 log_link_error_errno(link, r, "Could not set bond message: %m");
2217
2218 r = link_append_to_master(link, link->network->bond);
2219 if (r < 0)
2220 log_link_error_errno(link, r, "Failed to add to bond master's slave list: %m");
2221 }
2222
2223 if (link->network->use_br_vlan &&
2224 (link->network->bridge || streq_ptr("bridge", link->kind))) {
2225 r = link_set_bridge_vlan(link);
2226 if (r < 0)
2227 log_link_error_errno(link, r, "Could not set bridge vlan: %m");
2228 }
2229
2230 /* Skip setting up addresses until it gets carrier,
2231 or it would try to set addresses twice,
2232 which is bad for non-idempotent steps. */
2233 if (!link_has_carrier(link) && !link->network->configure_without_carrier)
2234 return 0;
2235
2236 link_set_state(link, LINK_STATE_CONFIGURING);
2237 return link_request_set_addresses(link);
2238 }
2239
2240 static int netdev_join_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
2241 int r;
2242
2243 assert(link);
2244 assert(link->network);
2245 assert(link->enslaving > 0);
2246
2247 link->enslaving--;
2248
2249 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
2250 return 1;
2251
2252 r = sd_netlink_message_get_errno(m);
2253 if (r < 0 && r != -EEXIST) {
2254 log_link_message_warning_errno(link, m, r, "Could not join netdev");
2255 link_enter_failed(link);
2256 return 1;
2257 }
2258
2259 log_link_debug(link, "Joined netdev");
2260
2261 if (link->enslaving == 0) {
2262 r = link_joined(link);
2263 if (r < 0)
2264 link_enter_failed(link);
2265 }
2266
2267 return 1;
2268 }
2269
2270 static int link_enter_join_netdev(Link *link) {
2271 NetDev *netdev;
2272 Iterator i;
2273 int r;
2274
2275 assert(link);
2276 assert(link->network);
2277 assert(link->state == LINK_STATE_INITIALIZED);
2278
2279 link_set_state(link, LINK_STATE_CONFIGURING);
2280
2281 link_dirty(link);
2282 link->enslaving = 0;
2283
2284 if (link->network->bond) {
2285 if (link->network->bond->state == NETDEV_STATE_READY &&
2286 link->network->bond->ifindex == link->master_ifindex)
2287 return link_joined(link);
2288
2289 log_struct(LOG_DEBUG,
2290 LOG_LINK_INTERFACE(link),
2291 LOG_NETDEV_INTERFACE(link->network->bond),
2292 LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bond->ifname));
2293
2294 link->enslaving++;
2295
2296 r = netdev_join(link->network->bond, link, netdev_join_handler);
2297 if (r < 0) {
2298 log_struct_errno(LOG_WARNING, r,
2299 LOG_LINK_INTERFACE(link),
2300 LOG_NETDEV_INTERFACE(link->network->bond),
2301 LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bond->ifname));
2302 link_enter_failed(link);
2303 return r;
2304 }
2305 }
2306
2307 if (link->network->bridge) {
2308 log_struct(LOG_DEBUG,
2309 LOG_LINK_INTERFACE(link),
2310 LOG_NETDEV_INTERFACE(link->network->bridge),
2311 LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bridge->ifname));
2312
2313 link->enslaving++;
2314
2315 r = netdev_join(link->network->bridge, link, netdev_join_handler);
2316 if (r < 0) {
2317 log_struct_errno(LOG_WARNING, r,
2318 LOG_LINK_INTERFACE(link),
2319 LOG_NETDEV_INTERFACE(link->network->bridge),
2320 LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bridge->ifname));
2321 link_enter_failed(link);
2322 return r;
2323 }
2324 }
2325
2326 if (link->network->vrf) {
2327 log_struct(LOG_DEBUG,
2328 LOG_LINK_INTERFACE(link),
2329 LOG_NETDEV_INTERFACE(link->network->vrf),
2330 LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->vrf->ifname));
2331
2332 link->enslaving++;
2333
2334 r = netdev_join(link->network->vrf, link, netdev_join_handler);
2335 if (r < 0) {
2336 log_struct_errno(LOG_WARNING, r,
2337 LOG_LINK_INTERFACE(link),
2338 LOG_NETDEV_INTERFACE(link->network->vrf),
2339 LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->vrf->ifname));
2340 link_enter_failed(link);
2341 return r;
2342 }
2343 }
2344
2345 HASHMAP_FOREACH(netdev, link->network->stacked_netdevs, i) {
2346
2347 if (netdev->ifindex > 0)
2348 /* Assume already enslaved. */
2349 continue;
2350
2351 if (netdev_get_create_type(netdev) != NETDEV_CREATE_STACKED)
2352 continue;
2353
2354 log_struct(LOG_DEBUG,
2355 LOG_LINK_INTERFACE(link),
2356 LOG_NETDEV_INTERFACE(netdev),
2357 LOG_LINK_MESSAGE(link, "Enslaving by '%s'", netdev->ifname));
2358
2359 link->enslaving++;
2360
2361 r = netdev_join(netdev, link, netdev_join_handler);
2362 if (r < 0) {
2363 log_struct_errno(LOG_WARNING, r,
2364 LOG_LINK_INTERFACE(link),
2365 LOG_NETDEV_INTERFACE(netdev),
2366 LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", netdev->ifname));
2367 link_enter_failed(link);
2368 return r;
2369 }
2370 }
2371
2372 if (link->enslaving == 0)
2373 return link_joined(link);
2374
2375 return 0;
2376 }
2377
2378 static int link_set_ipv4_forward(Link *link) {
2379 int r;
2380
2381 if (!link_ipv4_forward_enabled(link))
2382 return 0;
2383
2384 /* We propagate the forwarding flag from one interface to the
2385 * global setting one way. This means: as long as at least one
2386 * interface was configured at any time that had IP forwarding
2387 * enabled the setting will stay on for good. We do this
2388 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2389 * somewhat in sync (see below). */
2390
2391 r = sysctl_write_ip_property(AF_INET, NULL, "ip_forward", "1");
2392 if (r < 0)
2393 log_link_warning_errno(link, r, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2394
2395 return 0;
2396 }
2397
2398 static int link_set_ipv6_forward(Link *link) {
2399 int r;
2400
2401 if (!link_ipv6_forward_enabled(link))
2402 return 0;
2403
2404 /* On Linux, the IPv6 stack does not know a per-interface
2405 * packet forwarding setting: either packet forwarding is on
2406 * for all, or off for all. We hence don't bother with a
2407 * per-interface setting, but simply propagate the interface
2408 * flag, if it is set, to the global flag, one-way. Note that
2409 * while IPv4 would allow a per-interface flag, we expose the
2410 * same behaviour there and also propagate the setting from
2411 * one to all, to keep things simple (see above). */
2412
2413 r = sysctl_write_ip_property(AF_INET6, "all", "forwarding", "1");
2414 if (r < 0)
2415 log_link_warning_errno(link, r, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2416
2417 return 0;
2418 }
2419
2420 static int link_set_ipv6_privacy_extensions(Link *link) {
2421 IPv6PrivacyExtensions s;
2422 int r;
2423
2424 s = link_ipv6_privacy_extensions(link);
2425 if (s < 0)
2426 return 0;
2427
2428 r = sysctl_write_ip_property_int(AF_INET6, link->ifname, "use_tempaddr", (int) link->network->ipv6_privacy_extensions);
2429 if (r < 0)
2430 log_link_warning_errno(link, r, "Cannot configure IPv6 privacy extension for interface: %m");
2431
2432 return 0;
2433 }
2434
2435 static int link_set_ipv6_accept_ra(Link *link) {
2436 int r;
2437
2438 /* Make this a NOP if IPv6 is not available */
2439 if (!socket_ipv6_is_supported())
2440 return 0;
2441
2442 if (link->flags & IFF_LOOPBACK)
2443 return 0;
2444
2445 if (!link->network)
2446 return 0;
2447
2448 r = sysctl_write_ip_property(AF_INET6, link->ifname, "accept_ra", "0");
2449 if (r < 0)
2450 log_link_warning_errno(link, r, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2451
2452 return 0;
2453 }
2454
2455 static int link_set_ipv6_dad_transmits(Link *link) {
2456 int r;
2457
2458 /* Make this a NOP if IPv6 is not available */
2459 if (!socket_ipv6_is_supported())
2460 return 0;
2461
2462 if (link->flags & IFF_LOOPBACK)
2463 return 0;
2464
2465 if (!link->network)
2466 return 0;
2467
2468 if (link->network->ipv6_dad_transmits < 0)
2469 return 0;
2470
2471 r = sysctl_write_ip_property_int(AF_INET6, link->ifname, "dad_transmits", link->network->ipv6_dad_transmits);
2472 if (r < 0)
2473 log_link_warning_errno(link, r, "Cannot set IPv6 dad transmits for interface: %m");
2474
2475 return 0;
2476 }
2477
2478 static int link_set_ipv6_hop_limit(Link *link) {
2479 int r;
2480
2481 /* Make this a NOP if IPv6 is not available */
2482 if (!socket_ipv6_is_supported())
2483 return 0;
2484
2485 if (link->flags & IFF_LOOPBACK)
2486 return 0;
2487
2488 if (!link->network)
2489 return 0;
2490
2491 if (link->network->ipv6_hop_limit < 0)
2492 return 0;
2493
2494 r = sysctl_write_ip_property_int(AF_INET6, link->ifname, "hop_limit", link->network->ipv6_hop_limit);
2495 if (r < 0)
2496 log_link_warning_errno(link, r, "Cannot set IPv6 hop limit for interface: %m");
2497
2498 return 0;
2499 }
2500
2501 static int link_set_ipv6_mtu(Link *link) {
2502 int r;
2503
2504 /* Make this a NOP if IPv6 is not available */
2505 if (!socket_ipv6_is_supported())
2506 return 0;
2507
2508 if (link->flags & IFF_LOOPBACK)
2509 return 0;
2510
2511 if (link->network->ipv6_mtu == 0)
2512 return 0;
2513
2514 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
2515 * on the interface. Bump up IPv6 MTU bytes to IPV6_MTU_MIN. */
2516 if (link->network->ipv6_mtu < IPV6_MIN_MTU) {
2517 log_link_notice(link, "Bumping IPv6 MTU to "STRINGIFY(IPV6_MIN_MTU)" byte minimum required");
2518 link->network->ipv6_mtu = IPV6_MIN_MTU;
2519 }
2520
2521 r = sysctl_write_ip_property_uint32(AF_INET6, link->ifname, "mtu", link->network->ipv6_mtu);
2522 if (r < 0) {
2523 if (link->mtu < link->network->ipv6_mtu)
2524 log_link_warning(link, "Cannot set IPv6 MTU %"PRIu32" higher than device MTU %"PRIu32,
2525 link->network->ipv6_mtu, link->mtu);
2526 else
2527 log_link_warning_errno(link, r, "Cannot set IPv6 MTU for interface: %m");
2528 }
2529
2530 link->ipv6_mtu_set = true;
2531
2532 return 0;
2533 }
2534
2535 static bool link_is_static_address_configured(Link *link, Address *address) {
2536 Address *net_address;
2537
2538 assert(link);
2539 assert(address);
2540
2541 if (!link->network)
2542 return false;
2543
2544 LIST_FOREACH(addresses, net_address, link->network->static_addresses)
2545 if (address_equal(net_address, address))
2546 return true;
2547
2548 return false;
2549 }
2550
2551 static bool link_is_neighbor_configured(Link *link, Neighbor *neighbor) {
2552 Neighbor *net_neighbor;
2553
2554 assert(link);
2555 assert(neighbor);
2556
2557 if (!link->network)
2558 return false;
2559
2560 LIST_FOREACH(neighbors, net_neighbor, link->network->neighbors)
2561 if (neighbor_equal(net_neighbor, neighbor))
2562 return true;
2563
2564 return false;
2565 }
2566
2567 static bool link_is_static_route_configured(Link *link, Route *route) {
2568 Route *net_route;
2569
2570 assert(link);
2571 assert(route);
2572
2573 if (!link->network)
2574 return false;
2575
2576 LIST_FOREACH(routes, net_route, link->network->static_routes)
2577 if (route_equal(net_route, route))
2578 return true;
2579
2580 return false;
2581 }
2582
2583 static bool link_address_is_dynamic(Link *link, Address *address) {
2584 Route *route;
2585 Iterator i;
2586
2587 assert(link);
2588 assert(address);
2589
2590 if (address->cinfo.ifa_prefered != CACHE_INFO_INFINITY_LIFE_TIME)
2591 return true;
2592
2593 /* Even when the address is leased from a DHCP server, networkd assign the address
2594 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
2595 * corresponding routes with RTPROT_DHCP. */
2596 SET_FOREACH(route, link->routes_foreign, i) {
2597 if (route->protocol != RTPROT_DHCP)
2598 continue;
2599
2600 if (address->family != route->family)
2601 continue;
2602
2603 if (in_addr_equal(address->family, &address->in_addr, &route->prefsrc))
2604 return true;
2605 }
2606
2607 return false;
2608 }
2609
2610 static int link_enumerate_ipv6_tentative_addresses(Link *link) {
2611 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
2612 sd_netlink_message *addr;
2613 int r;
2614
2615 assert(link);
2616 assert(link->manager);
2617 assert(link->manager->rtnl);
2618
2619 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_GETADDR, 0, AF_INET6);
2620 if (r < 0)
2621 return r;
2622
2623 r = sd_netlink_call(link->manager->rtnl, req, 0, &reply);
2624 if (r < 0)
2625 return r;
2626
2627 for (addr = reply; addr; addr = sd_netlink_message_next(addr)) {
2628 unsigned char flags;
2629 int ifindex;
2630
2631 r = sd_rtnl_message_addr_get_ifindex(addr, &ifindex);
2632 if (r < 0) {
2633 log_link_warning_errno(link, r, "rtnl: invalid ifindex, ignoring: %m");
2634 continue;
2635 } else if (link->ifindex != ifindex)
2636 continue;
2637
2638 r = sd_rtnl_message_addr_get_flags(addr, &flags);
2639 if (r < 0) {
2640 log_link_warning_errno(link, r, "rtnl: received address message with invalid flags, ignoring: %m");
2641 continue;
2642 } else if (!(flags & IFA_F_TENTATIVE))
2643 continue;
2644
2645 log_link_debug(link, "Found tentative ipv6 link-local address");
2646 (void) manager_rtnl_process_address(link->manager->rtnl, addr, link->manager);
2647 }
2648
2649 return 0;
2650 }
2651
2652 static int link_drop_foreign_config(Link *link) {
2653 Address *address;
2654 Neighbor *neighbor;
2655 Route *route;
2656 Iterator i;
2657 int r;
2658
2659 /* The kernel doesn't notify us about tentative addresses;
2660 * so if ipv6ll is disabled, we need to enumerate them now so we can drop them below */
2661 if (!link_ipv6ll_enabled(link)) {
2662 r = link_enumerate_ipv6_tentative_addresses(link);
2663 if (r < 0)
2664 return r;
2665 }
2666
2667 SET_FOREACH(address, link->addresses_foreign, i) {
2668 /* we consider IPv6LL addresses to be managed by the kernel */
2669 if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1 && link_ipv6ll_enabled(link))
2670 continue;
2671
2672 if (link_address_is_dynamic(link, address)) {
2673 if (link->network && FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP))
2674 continue;
2675 } else if (link->network && FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC))
2676 continue;
2677
2678 if (link_is_static_address_configured(link, address)) {
2679 r = address_add(link, address->family, &address->in_addr, address->prefixlen, NULL);
2680 if (r < 0)
2681 return log_link_error_errno(link, r, "Failed to add address: %m");
2682 } else {
2683 r = address_remove(address, link, NULL);
2684 if (r < 0)
2685 return r;
2686 }
2687 }
2688
2689 SET_FOREACH(neighbor, link->neighbors_foreign, i) {
2690 if (link_is_neighbor_configured(link, neighbor)) {
2691 r = neighbor_add(link, neighbor->family, &neighbor->in_addr, &neighbor->lladdr, neighbor->lladdr_size, NULL);
2692 if (r < 0)
2693 return r;
2694 } else {
2695 r = neighbor_remove(neighbor, link, NULL);
2696 if (r < 0)
2697 return r;
2698 }
2699 }
2700
2701 SET_FOREACH(route, link->routes_foreign, i) {
2702 /* do not touch routes managed by the kernel */
2703 if (route->protocol == RTPROT_KERNEL)
2704 continue;
2705
2706 /* do not touch multicast route added by kernel */
2707 /* FIXME: Why the kernel adds this route with protocol RTPROT_BOOT??? We need to investigate that.
2708 * https://tools.ietf.org/html/rfc4862#section-5.4 may explain why. */
2709 if (route->protocol == RTPROT_BOOT &&
2710 route->family == AF_INET6 &&
2711 route->dst_prefixlen == 8 &&
2712 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 }}} }))
2713 continue;
2714
2715 if (route->protocol == RTPROT_STATIC && link->network &&
2716 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC))
2717 continue;
2718
2719 if (route->protocol == RTPROT_DHCP && link->network &&
2720 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP))
2721 continue;
2722
2723 if (link_is_static_route_configured(link, route)) {
2724 r = route_add(link, route, NULL);
2725 if (r < 0)
2726 return r;
2727 } else {
2728 r = route_remove(route, link, NULL);
2729 if (r < 0)
2730 return r;
2731 }
2732 }
2733
2734 return 0;
2735 }
2736
2737 static int link_drop_config(Link *link) {
2738 Address *address, *pool_address;
2739 Neighbor *neighbor;
2740 Route *route;
2741 Iterator i;
2742 int r;
2743
2744 SET_FOREACH(address, link->addresses, i) {
2745 /* we consider IPv6LL addresses to be managed by the kernel */
2746 if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1 && link_ipv6ll_enabled(link))
2747 continue;
2748
2749 r = address_remove(address, link, NULL);
2750 if (r < 0)
2751 return r;
2752
2753 /* If this address came from an address pool, clean up the pool */
2754 LIST_FOREACH(addresses, pool_address, link->pool_addresses) {
2755 if (address_equal(address, pool_address)) {
2756 LIST_REMOVE(addresses, link->pool_addresses, pool_address);
2757 address_free(pool_address);
2758 break;
2759 }
2760 }
2761 }
2762
2763 SET_FOREACH(neighbor, link->neighbors, i) {
2764 r = neighbor_remove(neighbor, link, NULL);
2765 if (r < 0)
2766 return r;
2767 }
2768
2769 SET_FOREACH(route, link->routes, i) {
2770 /* do not touch routes managed by the kernel */
2771 if (route->protocol == RTPROT_KERNEL)
2772 continue;
2773
2774 r = route_remove(route, link, NULL);
2775 if (r < 0)
2776 return r;
2777 }
2778
2779 ndisc_flush(link);
2780
2781 return 0;
2782 }
2783
2784 static int link_configure_ipv4_dad(Link *link) {
2785 Address *address;
2786 int r;
2787
2788 assert(link);
2789 assert(link->network);
2790
2791 LIST_FOREACH(addresses, address, link->network->static_addresses)
2792 if (address->family == AF_INET &&
2793 FLAGS_SET(address->duplicate_address_detection, ADDRESS_FAMILY_IPV4)) {
2794 r = configure_ipv4_duplicate_address_detection(link, address);
2795 if (r < 0)
2796 return log_link_error_errno(link, r, "Failed to configure IPv4ACD: %m");
2797 }
2798
2799 return 0;
2800 }
2801
2802 static int link_configure_traffic_control(Link *link) {
2803 TrafficControl *tc;
2804 Iterator i;
2805 int r;
2806
2807 link->tc_configured = false;
2808 link->tc_messages = 0;
2809
2810 ORDERED_HASHMAP_FOREACH(tc, link->network->tc_by_section, i) {
2811 r = traffic_control_configure(link, tc);
2812 if (r < 0)
2813 return r;
2814 }
2815
2816 if (link->tc_messages == 0)
2817 link->tc_configured = true;
2818 else
2819 log_link_debug(link, "Configuring traffic control");
2820
2821 return 0;
2822 }
2823
2824 static int link_configure(Link *link) {
2825 int r;
2826
2827 assert(link);
2828 assert(link->network);
2829 assert(link->state == LINK_STATE_INITIALIZED);
2830
2831 r = link_configure_traffic_control(link);
2832 if (r < 0)
2833 return r;
2834
2835 if (link->iftype == ARPHRD_CAN)
2836 return link_configure_can(link);
2837
2838 /* If IPv6 configured that is static IPv6 address and IPv6LL autoconfiguration is enabled
2839 * for this interface, then enable IPv6 */
2840 (void) link_update_ipv6_sysctl(link);
2841
2842 r = link_set_proxy_arp(link);
2843 if (r < 0)
2844 return r;
2845
2846 r = ipv6_proxy_ndp_addresses_configure(link);
2847 if (r < 0)
2848 return r;
2849
2850 r = link_set_ipv4_forward(link);
2851 if (r < 0)
2852 return r;
2853
2854 r = link_set_ipv6_forward(link);
2855 if (r < 0)
2856 return r;
2857
2858 r = link_set_ipv6_privacy_extensions(link);
2859 if (r < 0)
2860 return r;
2861
2862 r = link_set_ipv6_accept_ra(link);
2863 if (r < 0)
2864 return r;
2865
2866 r = link_set_ipv6_dad_transmits(link);
2867 if (r < 0)
2868 return r;
2869
2870 r = link_set_ipv6_hop_limit(link);
2871 if (r < 0)
2872 return r;
2873
2874 r = link_set_flags(link);
2875 if (r < 0)
2876 return r;
2877
2878 r = link_set_group(link);
2879 if (r < 0)
2880 return r;
2881
2882 if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_IPV4 | ADDRESS_FAMILY_FALLBACK_IPV4)) {
2883 r = ipv4ll_configure(link);
2884 if (r < 0)
2885 return r;
2886 }
2887
2888 if (link_dhcp4_enabled(link)) {
2889 r = dhcp4_set_promote_secondaries(link);
2890 if (r < 0)
2891 return r;
2892
2893 r = dhcp4_configure(link);
2894 if (r < 0)
2895 return r;
2896 }
2897
2898 if (link_dhcp4_server_enabled(link)) {
2899 r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex);
2900 if (r < 0)
2901 return r;
2902
2903 r = sd_dhcp_server_attach_event(link->dhcp_server, NULL, 0);
2904 if (r < 0)
2905 return r;
2906 }
2907
2908 if (link_dhcp6_enabled(link) ||
2909 link_ipv6_accept_ra_enabled(link)) {
2910 r = dhcp6_configure(link);
2911 if (r < 0)
2912 return r;
2913 }
2914
2915 if (link_ipv6_accept_ra_enabled(link)) {
2916 r = ndisc_configure(link);
2917 if (r < 0)
2918 return r;
2919 }
2920
2921 if (link_radv_enabled(link)) {
2922 r = radv_configure(link);
2923 if (r < 0)
2924 return r;
2925 }
2926
2927 if (link_lldp_rx_enabled(link)) {
2928 r = link_lldp_rx_configure(link);
2929 if (r < 0)
2930 return r;
2931 }
2932
2933 r = link_configure_mtu(link);
2934 if (r < 0)
2935 return r;
2936
2937 r = link_configure_addrgen_mode(link);
2938 if (r < 0)
2939 return r;
2940
2941 r = link_configure_ipv4_dad(link);
2942 if (r < 0)
2943 return r;
2944
2945 return link_configure_continue(link);
2946 }
2947
2948 /* The configuration continues in this separate function, instead of
2949 * including this in the above link_configure() function, for two
2950 * reasons:
2951 * 1) some devices reset the link when the mtu is set, which caused
2952 * an infinite loop here in networkd; see:
2953 * https://github.com/systemd/systemd/issues/6593
2954 * https://github.com/systemd/systemd/issues/9831
2955 * 2) if ipv6ll is disabled, then bringing the interface up must be
2956 * delayed until after we get confirmation from the kernel that
2957 * the addr_gen_mode parameter has been set (via netlink), see:
2958 * https://github.com/systemd/systemd/issues/13882
2959 */
2960 static int link_configure_continue(Link *link) {
2961 int r;
2962
2963 assert(link);
2964 assert(link->network);
2965 assert(link->state == LINK_STATE_INITIALIZED);
2966
2967 if (link->setting_mtu || link->setting_genmode)
2968 return 0;
2969
2970 /* Drop foreign config, but ignore loopback or critical devices.
2971 * We do not want to remove loopback address or addresses used for root NFS. */
2972 if (!(link->flags & IFF_LOOPBACK) &&
2973 link->network->keep_configuration != KEEP_CONFIGURATION_YES) {
2974 r = link_drop_foreign_config(link);
2975 if (r < 0)
2976 return r;
2977 }
2978
2979 /* The kernel resets ipv6 mtu after changing device mtu;
2980 * we must set this here, after we've set device mtu */
2981 r = link_set_ipv6_mtu(link);
2982 if (r < 0)
2983 return r;
2984
2985 if (link_has_carrier(link) || link->network->configure_without_carrier) {
2986 r = link_acquire_conf(link);
2987 if (r < 0)
2988 return r;
2989 }
2990
2991 return link_enter_join_netdev(link);
2992 }
2993
2994 static int duid_set_uuid(DUID *duid, sd_id128_t uuid) {
2995 assert(duid);
2996
2997 if (duid->raw_data_len > 0)
2998 return 0;
2999
3000 if (duid->type != DUID_TYPE_UUID)
3001 return -EINVAL;
3002
3003 memcpy(&duid->raw_data, &uuid, sizeof(sd_id128_t));
3004 duid->raw_data_len = sizeof(sd_id128_t);
3005
3006 return 1;
3007 }
3008
3009 int get_product_uuid_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
3010 Manager *manager = userdata;
3011 const sd_bus_error *e;
3012 const void *a;
3013 size_t sz;
3014 DUID *duid;
3015 Link *link;
3016 int r;
3017
3018 assert(m);
3019 assert(manager);
3020
3021 e = sd_bus_message_get_error(m);
3022 if (e) {
3023 log_error_errno(sd_bus_error_get_errno(e),
3024 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
3025 e->message);
3026 goto configure;
3027 }
3028
3029 r = sd_bus_message_read_array(m, 'y', &a, &sz);
3030 if (r < 0)
3031 goto configure;
3032
3033 if (sz != sizeof(sd_id128_t)) {
3034 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3035 goto configure;
3036 }
3037
3038 memcpy(&manager->product_uuid, a, sz);
3039 while ((duid = set_steal_first(manager->duids_requesting_uuid)))
3040 (void) duid_set_uuid(duid, manager->product_uuid);
3041
3042 manager->duids_requesting_uuid = set_free(manager->duids_requesting_uuid);
3043
3044 configure:
3045 while ((link = set_steal_first(manager->links_requesting_uuid))) {
3046 link_unref(link);
3047
3048 r = link_configure(link);
3049 if (r < 0)
3050 link_enter_failed(link);
3051 }
3052
3053 manager->links_requesting_uuid = set_free(manager->links_requesting_uuid);
3054
3055 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3056 * even if the method fails. */
3057 manager->has_product_uuid = true;
3058
3059 return 1;
3060 }
3061
3062 static bool link_requires_uuid(Link *link) {
3063 const DUID *duid;
3064
3065 assert(link);
3066 assert(link->manager);
3067 assert(link->network);
3068
3069 duid = link_get_duid(link);
3070 if (duid->type != DUID_TYPE_UUID || duid->raw_data_len != 0)
3071 return false;
3072
3073 if (link_dhcp4_enabled(link) && IN_SET(link->network->dhcp_client_identifier, DHCP_CLIENT_ID_DUID, DHCP_CLIENT_ID_DUID_ONLY))
3074 return true;
3075
3076 if (link_dhcp6_enabled(link) || link_ipv6_accept_ra_enabled(link))
3077 return true;
3078
3079 return false;
3080 }
3081
3082 static int link_configure_duid(Link *link) {
3083 Manager *m;
3084 DUID *duid;
3085 int r;
3086
3087 assert(link);
3088 assert(link->manager);
3089 assert(link->network);
3090
3091 m = link->manager;
3092 duid = link_get_duid(link);
3093
3094 if (!link_requires_uuid(link))
3095 return 1;
3096
3097 if (m->has_product_uuid) {
3098 (void) duid_set_uuid(duid, m->product_uuid);
3099 return 1;
3100 }
3101
3102 if (!m->links_requesting_uuid) {
3103 r = manager_request_product_uuid(m, link);
3104 if (r < 0) {
3105 if (r == -ENOMEM)
3106 return r;
3107
3108 log_link_warning_errno(link, r,
3109 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3110 return 1;
3111 }
3112 } else {
3113 r = set_put(m->links_requesting_uuid, link);
3114 if (r < 0)
3115 return log_oom();
3116
3117 r = set_put(m->duids_requesting_uuid, duid);
3118 if (r < 0)
3119 return log_oom();
3120
3121 link_ref(link);
3122 }
3123
3124 return 0;
3125 }
3126
3127 static int link_reconfigure_internal(Link *link, sd_netlink_message *m, bool force) {
3128 Network *network;
3129 int r;
3130
3131 if (m) {
3132 _cleanup_strv_free_ char **s = NULL;
3133
3134 r = sd_netlink_message_get_errno(m);
3135 if (r < 0)
3136 return r;
3137
3138 r = sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &s);
3139 if (r < 0 && r != -ENODATA)
3140 return r;
3141
3142 strv_free_and_replace(link->alternative_names, s);
3143 }
3144
3145 r = network_get(link->manager, link->iftype, link->sd_device,
3146 link->ifname, link->alternative_names, link->driver,
3147 &link->mac, &link->permanent_mac,
3148 link->wlan_iftype, link->ssid, &link->bssid, &network);
3149 if (r == -ENOENT) {
3150 link_enter_unmanaged(link);
3151 return 0;
3152 } else if (r == 0 && network->unmanaged) {
3153 link_enter_unmanaged(link);
3154 return 0;
3155 } else if (r < 0)
3156 return r;
3157
3158 if (link->network == network && !force)
3159 return 0;
3160
3161 log_link_info(link, "Re-configuring with %s", network->filename);
3162
3163 /* Dropping old .network file */
3164 r = link_stop_clients(link, false);
3165 if (r < 0)
3166 return r;
3167
3168 if (link_dhcp4_server_enabled(link))
3169 (void) sd_dhcp_server_stop(link->dhcp_server);
3170
3171 r = link_drop_config(link);
3172 if (r < 0)
3173 return r;
3174
3175 if (!IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING, LINK_STATE_INITIALIZED)) {
3176 log_link_debug(link, "State is %s, dropping config", link_state_to_string(link->state));
3177 r = link_drop_foreign_config(link);
3178 if (r < 0)
3179 return r;
3180 }
3181
3182 link_free_carrier_maps(link);
3183 link_free_engines(link);
3184 link->network = network_unref(link->network);
3185
3186 /* Then, apply new .network file */
3187 r = network_apply(network, link);
3188 if (r < 0)
3189 return r;
3190
3191 r = link_new_carrier_maps(link);
3192 if (r < 0)
3193 return r;
3194
3195 link_set_state(link, LINK_STATE_INITIALIZED);
3196 link_dirty(link);
3197
3198 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3199 * link_configure() is called later asynchronously. */
3200 r = link_configure_duid(link);
3201 if (r <= 0)
3202 return r;
3203
3204 r = link_configure(link);
3205 if (r < 0)
3206 return r;
3207
3208 return 0;
3209 }
3210
3211 static int link_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
3212 int r;
3213
3214 r = link_reconfigure_internal(link, m, false);
3215 if (r < 0)
3216 link_enter_failed(link);
3217
3218 return 1;
3219 }
3220
3221 static int link_force_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
3222 int r;
3223
3224 r = link_reconfigure_internal(link, m, true);
3225 if (r < 0)
3226 link_enter_failed(link);
3227
3228 return 1;
3229 }
3230
3231 int link_reconfigure(Link *link, bool force) {
3232 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
3233 int r;
3234
3235 if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_LINGER))
3236 return 0;
3237
3238 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK,
3239 link->ifindex);
3240 if (r < 0)
3241 return r;
3242
3243 r = netlink_call_async(link->manager->rtnl, NULL, req,
3244 force ? link_force_reconfigure_handler : link_reconfigure_handler,
3245 link_netlink_destroy_callback, link);
3246 if (r < 0)
3247 return r;
3248
3249 link_ref(link);
3250
3251 return 0;
3252 }
3253
3254 static int link_initialized_and_synced(Link *link) {
3255 Network *network;
3256 int r;
3257
3258 assert(link);
3259 assert(link->ifname);
3260 assert(link->manager);
3261
3262 /* We may get called either from the asynchronous netlink callback,
3263 * or directly for link_add() if running in a container. See link_add(). */
3264 if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED))
3265 return 0;
3266
3267 log_link_debug(link, "Link state is up-to-date");
3268 link_set_state(link, LINK_STATE_INITIALIZED);
3269
3270 r = link_new_bound_by_list(link);
3271 if (r < 0)
3272 return r;
3273
3274 r = link_handle_bound_by_list(link);
3275 if (r < 0)
3276 return r;
3277
3278 if (!link->network) {
3279 r = wifi_get_info(link);
3280 if (r < 0)
3281 return r;
3282
3283 r = network_get(link->manager, link->iftype, link->sd_device,
3284 link->ifname, link->alternative_names, link->driver,
3285 &link->mac, &link->permanent_mac,
3286 link->wlan_iftype, link->ssid, &link->bssid, &network);
3287 if (r == -ENOENT) {
3288 link_enter_unmanaged(link);
3289 return 0;
3290 } else if (r == 0 && network->unmanaged) {
3291 link_enter_unmanaged(link);
3292 return 0;
3293 } else if (r < 0)
3294 return r;
3295
3296 if (link->flags & IFF_LOOPBACK) {
3297 if (network->link_local != ADDRESS_FAMILY_NO)
3298 log_link_debug(link, "Ignoring link-local autoconfiguration for loopback link");
3299
3300 if (network->dhcp != ADDRESS_FAMILY_NO)
3301 log_link_debug(link, "Ignoring DHCP clients for loopback link");
3302
3303 if (network->dhcp_server)
3304 log_link_debug(link, "Ignoring DHCP server for loopback link");
3305 }
3306
3307 r = network_apply(network, link);
3308 if (r < 0)
3309 return r;
3310 }
3311
3312 r = link_new_bound_to_list(link);
3313 if (r < 0)
3314 return r;
3315
3316 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3317 * link_configure() is called later asynchronously. */
3318 r = link_configure_duid(link);
3319 if (r <= 0)
3320 return r;
3321
3322 r = link_configure(link);
3323 if (r < 0)
3324 return r;
3325
3326 return 0;
3327 }
3328
3329 static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
3330 _cleanup_strv_free_ char **s = NULL;
3331 int r;
3332
3333 r = sd_netlink_message_get_errno(m);
3334 if (r < 0) {
3335 log_link_warning_errno(link, r, "Failed to wait for the interface to be initialized: %m");
3336 link_enter_failed(link);
3337 return 0;
3338 }
3339
3340 r = sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &s);
3341 if (r < 0 && r != -ENODATA) {
3342 link_enter_failed(link);
3343 return 0;
3344 }
3345
3346 strv_free_and_replace(link->alternative_names, s);
3347
3348 r = link_initialized_and_synced(link);
3349 if (r < 0)
3350 link_enter_failed(link);
3351 return 1;
3352 }
3353
3354 int link_initialized(Link *link, sd_device *device) {
3355 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
3356 int r;
3357
3358 assert(link);
3359 assert(link->manager);
3360 assert(link->manager->rtnl);
3361 assert(device);
3362
3363 if (link->state != LINK_STATE_PENDING)
3364 return 0;
3365
3366 if (link->sd_device)
3367 return 0;
3368
3369 log_link_debug(link, "udev initialized link");
3370 link_set_state(link, LINK_STATE_INITIALIZED);
3371
3372 link->sd_device = sd_device_ref(device);
3373
3374 /* udev has initialized the link, but we don't know if we have yet
3375 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3376 * when it returns we know that the pending NEWLINKs have already been
3377 * processed and that we are up-to-date */
3378
3379 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK,
3380 link->ifindex);
3381 if (r < 0)
3382 return r;
3383
3384 r = netlink_call_async(link->manager->rtnl, NULL, req, link_initialized_handler,
3385 link_netlink_destroy_callback, link);
3386 if (r < 0)
3387 return r;
3388
3389 link_ref(link);
3390
3391 return 0;
3392 }
3393
3394 static int link_load(Link *link) {
3395 _cleanup_free_ char *network_file = NULL,
3396 *addresses = NULL,
3397 *routes = NULL,
3398 *dhcp4_address = NULL,
3399 *ipv4ll_address = NULL;
3400 union in_addr_union address;
3401 const char *p;
3402 int r;
3403
3404 assert(link);
3405
3406 r = parse_env_file(NULL, link->state_file,
3407 "NETWORK_FILE", &network_file,
3408 "ADDRESSES", &addresses,
3409 "ROUTES", &routes,
3410 "DHCP4_ADDRESS", &dhcp4_address,
3411 "IPV4LL_ADDRESS", &ipv4ll_address);
3412 if (r < 0 && r != -ENOENT)
3413 return log_link_error_errno(link, r, "Failed to read %s: %m", link->state_file);
3414
3415 if (network_file) {
3416 Network *network;
3417 char *suffix;
3418
3419 /* drop suffix */
3420 suffix = strrchr(network_file, '.');
3421 if (!suffix) {
3422 log_link_debug(link, "Failed to get network name from %s", network_file);
3423 goto network_file_fail;
3424 }
3425 *suffix = '\0';
3426
3427 r = network_get_by_name(link->manager, basename(network_file), &network);
3428 if (r < 0) {
3429 log_link_debug_errno(link, r, "Failed to get network %s: %m", basename(network_file));
3430 goto network_file_fail;
3431 }
3432
3433 r = network_apply(network, link);
3434 if (r < 0)
3435 return log_link_error_errno(link, r, "Failed to apply network %s: %m", basename(network_file));
3436 }
3437
3438 network_file_fail:
3439
3440 if (addresses) {
3441 p = addresses;
3442
3443 for (;;) {
3444 _cleanup_free_ char *address_str = NULL;
3445 char *prefixlen_str;
3446 int family;
3447 unsigned char prefixlen;
3448
3449 r = extract_first_word(&p, &address_str, NULL, 0);
3450 if (r < 0) {
3451 log_link_debug_errno(link, r, "Failed to extract next address string: %m");
3452 continue;
3453 }
3454 if (r == 0)
3455 break;
3456
3457 prefixlen_str = strchr(address_str, '/');
3458 if (!prefixlen_str) {
3459 log_link_debug(link, "Failed to parse address and prefix length %s", address_str);
3460 continue;
3461 }
3462
3463 *prefixlen_str++ = '\0';
3464
3465 r = sscanf(prefixlen_str, "%hhu", &prefixlen);
3466 if (r != 1) {
3467 log_link_error(link, "Failed to parse prefixlen %s", prefixlen_str);
3468 continue;
3469 }
3470
3471 r = in_addr_from_string_auto(address_str, &family, &address);
3472 if (r < 0) {
3473 log_link_debug_errno(link, r, "Failed to parse address %s: %m", address_str);
3474 continue;
3475 }
3476
3477 r = address_add(link, family, &address, prefixlen, NULL);
3478 if (r < 0)
3479 return log_link_error_errno(link, r, "Failed to add address: %m");
3480 }
3481 }
3482
3483 if (routes) {
3484 p = routes;
3485
3486 for (;;) {
3487 _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL;
3488 _cleanup_(route_freep) Route *tmp = NULL;
3489 _cleanup_free_ char *route_str = NULL;
3490 char *prefixlen_str;
3491 Route *route;
3492
3493 r = extract_first_word(&p, &route_str, NULL, 0);
3494 if (r < 0) {
3495 log_link_debug_errno(link, r, "Failed to extract next route string: %m");
3496 continue;
3497 }
3498 if (r == 0)
3499 break;
3500
3501 prefixlen_str = strchr(route_str, '/');
3502 if (!prefixlen_str) {
3503 log_link_debug(link, "Failed to parse route %s", route_str);
3504 continue;
3505 }
3506
3507 *prefixlen_str++ = '\0';
3508
3509 r = route_new(&tmp);
3510 if (r < 0)
3511 return log_oom();
3512
3513 r = sscanf(prefixlen_str, "%hhu/%hhu/%"SCNu32"/%"PRIu32"/"USEC_FMT, &tmp->dst_prefixlen, &tmp->tos, &tmp->priority, &tmp->table, &tmp->lifetime);
3514 if (r != 5) {
3515 log_link_debug(link,
3516 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3517 prefixlen_str);
3518 continue;
3519 }
3520
3521 r = in_addr_from_string_auto(route_str, &tmp->family, &tmp->dst);
3522 if (r < 0) {
3523 log_link_debug_errno(link, r, "Failed to parse route destination %s: %m", route_str);
3524 continue;
3525 }
3526
3527 r = route_add(link, tmp, &route);
3528 if (r < 0)
3529 return log_link_error_errno(link, r, "Failed to add route: %m");
3530
3531 if (route->lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) {
3532 r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(), route->lifetime,
3533 0, route_expire_handler, route);
3534 if (r < 0)
3535 log_link_warning_errno(link, r, "Could not arm route expiration handler: %m");
3536 }
3537
3538 sd_event_source_unref(route->expire);
3539 route->expire = TAKE_PTR(expire);
3540 }
3541 }
3542
3543 if (dhcp4_address) {
3544 r = in_addr_from_string(AF_INET, dhcp4_address, &address);
3545 if (r < 0) {
3546 log_link_debug_errno(link, r, "Failed to parse DHCPv4 address %s: %m", dhcp4_address);
3547 goto dhcp4_address_fail;
3548 }
3549
3550 r = sd_dhcp_client_new(&link->dhcp_client, link->network ? link->network->dhcp_anonymize : 0);
3551 if (r < 0)
3552 return log_link_error_errno(link, r, "Failed to create DHCPv4 client: %m");
3553
3554 r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0);
3555 if (r < 0)
3556 return log_link_error_errno(link, r, "Failed to attach DHCPv4 event: %m");
3557
3558 r = sd_dhcp_client_set_request_address(link->dhcp_client, &address.in);
3559 if (r < 0)
3560 return log_link_error_errno(link, r, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address);
3561 }
3562
3563 dhcp4_address_fail:
3564
3565 if (ipv4ll_address) {
3566 r = in_addr_from_string(AF_INET, ipv4ll_address, &address);
3567 if (r < 0) {
3568 log_link_debug_errno(link, r, "Failed to parse IPv4LL address %s: %m", ipv4ll_address);
3569 goto ipv4ll_address_fail;
3570 }
3571
3572 r = sd_ipv4ll_new(&link->ipv4ll);
3573 if (r < 0)
3574 return log_link_error_errno(link, r, "Failed to create IPv4LL client: %m");
3575
3576 r = sd_ipv4ll_attach_event(link->ipv4ll, NULL, 0);
3577 if (r < 0)
3578 return log_link_error_errno(link, r, "Failed to attach IPv4LL event: %m");
3579
3580 r = sd_ipv4ll_set_address(link->ipv4ll, &address.in);
3581 if (r < 0)
3582 return log_link_error_errno(link, r, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address);
3583 }
3584
3585 ipv4ll_address_fail:
3586
3587 return 0;
3588 }
3589
3590 int link_add(Manager *m, sd_netlink_message *message, Link **ret) {
3591 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
3592 char ifindex_str[2 + DECIMAL_STR_MAX(int)];
3593 Link *link;
3594 int r;
3595
3596 assert(m);
3597 assert(m->rtnl);
3598 assert(message);
3599 assert(ret);
3600
3601 r = link_new(m, message, ret);
3602 if (r < 0)
3603 return r;
3604
3605 link = *ret;
3606
3607 log_link_debug(link, "Link %d added", link->ifindex);
3608
3609 r = link_load(link);
3610 if (r < 0)
3611 return r;
3612
3613 if (path_is_read_only_fs("/sys") <= 0) {
3614 /* udev should be around */
3615 sprintf(ifindex_str, "n%d", link->ifindex);
3616 r = sd_device_new_from_device_id(&device, ifindex_str);
3617 if (r < 0) {
3618 log_link_warning_errno(link, r, "Could not find device, waiting for device initialization: %m");
3619 return 0;
3620 }
3621
3622 r = sd_device_get_is_initialized(device);
3623 if (r < 0) {
3624 log_link_warning_errno(link, r, "Could not determine whether the device is initialized: %m");
3625 goto failed;
3626 }
3627 if (r == 0) {
3628 /* not yet ready */
3629 log_link_debug(link, "link pending udev initialization...");
3630 return 0;
3631 }
3632
3633 r = device_is_renaming(device);
3634 if (r < 0) {
3635 log_link_warning_errno(link, r, "Failed to determine the device is being renamed: %m");
3636 goto failed;
3637 }
3638 if (r > 0) {
3639 log_link_debug(link, "Interface is being renamed, pending initialization.");
3640 return 0;
3641 }
3642
3643 r = link_initialized(link, device);
3644 if (r < 0)
3645 goto failed;
3646 } else {
3647 r = link_initialized_and_synced(link);
3648 if (r < 0)
3649 goto failed;
3650 }
3651
3652 return 0;
3653 failed:
3654 link_enter_failed(link);
3655 return r;
3656 }
3657
3658 int link_ipv6ll_gained(Link *link, const struct in6_addr *address) {
3659 int r;
3660
3661 assert(link);
3662
3663 log_link_info(link, "Gained IPv6LL");
3664
3665 link->ipv6ll_address = *address;
3666 link_check_ready(link);
3667
3668 if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) {
3669 r = link_acquire_ipv6_conf(link);
3670 if (r < 0) {
3671 link_enter_failed(link);
3672 return r;
3673 }
3674 }
3675
3676 return 0;
3677 }
3678
3679 static int link_carrier_gained(Link *link) {
3680 int r;
3681
3682 assert(link);
3683
3684 r = wifi_get_info(link);
3685 if (r < 0)
3686 return r;
3687 if (r > 0) {
3688 r = link_reconfigure_internal(link, NULL, false);
3689 if (r < 0) {
3690 link_enter_failed(link);
3691 return r;
3692 }
3693 }
3694
3695 if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) {
3696 r = link_acquire_conf(link);
3697 if (r < 0) {
3698 link_enter_failed(link);
3699 return r;
3700 }
3701
3702 link_set_state(link, LINK_STATE_CONFIGURING);
3703 r = link_request_set_addresses(link);
3704 if (r < 0)
3705 return r;
3706 }
3707
3708 r = link_handle_bound_by_list(link);
3709 if (r < 0)
3710 return r;
3711
3712 return 0;
3713 }
3714
3715 static int link_carrier_lost(Link *link) {
3716 int r;
3717
3718 assert(link);
3719
3720 if (link->network && link->network->ignore_carrier_loss)
3721 return 0;
3722
3723 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3724 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3725 * configuration and stop the clients as well. */
3726 if (link->setting_mtu)
3727 return 0;
3728
3729 r = link_stop_clients(link, false);
3730 if (r < 0) {
3731 link_enter_failed(link);
3732 return r;
3733 }
3734
3735 if (link_dhcp4_server_enabled(link))
3736 (void) sd_dhcp_server_stop(link->dhcp_server);
3737
3738 r = link_drop_config(link);
3739 if (r < 0)
3740 return r;
3741
3742 if (!IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING, LINK_STATE_INITIALIZED)) {
3743 log_link_debug(link, "State is %s, dropping config", link_state_to_string(link->state));
3744 r = link_drop_foreign_config(link);
3745 if (r < 0)
3746 return r;
3747 }
3748
3749 r = link_handle_bound_by_list(link);
3750 if (r < 0)
3751 return r;
3752
3753 return 0;
3754 }
3755
3756 int link_carrier_reset(Link *link) {
3757 int r;
3758
3759 assert(link);
3760
3761 if (link_has_carrier(link)) {
3762 r = link_carrier_lost(link);
3763 if (r < 0)
3764 return r;
3765
3766 r = link_carrier_gained(link);
3767 if (r < 0)
3768 return r;
3769
3770 log_link_info(link, "Reset carrier");
3771 }
3772
3773 return 0;
3774 }
3775
3776 /* This is called every time an interface admin state changes to up;
3777 * specifically, when IFF_UP flag changes from unset to set */
3778 static int link_admin_state_up(Link *link) {
3779 int r;
3780
3781 /* We set the ipv6 mtu after the device mtu, but the kernel resets
3782 * ipv6 mtu on NETDEV_UP, so we need to reset it. The check for
3783 * ipv6_mtu_set prevents this from trying to set it too early before
3784 * the link->network has been setup; we only need to reset it
3785 * here if we've already set it during normal initialization. */
3786 if (link->ipv6_mtu_set) {
3787 r = link_set_ipv6_mtu(link);
3788 if (r < 0)
3789 return r;
3790 }
3791
3792 return 0;
3793 }
3794
3795 int link_update(Link *link, sd_netlink_message *m) {
3796 _cleanup_strv_free_ char **s = NULL;
3797 struct ether_addr mac;
3798 const char *ifname;
3799 uint32_t mtu;
3800 bool had_carrier, carrier_gained, carrier_lost, link_was_admin_up;
3801 int old_master, r;
3802
3803 assert(link);
3804 assert(link->ifname);
3805 assert(m);
3806
3807 if (link->state == LINK_STATE_LINGER) {
3808 log_link_info(link, "Link re-added");
3809 link_set_state(link, LINK_STATE_CONFIGURING);
3810
3811 r = link_new_carrier_maps(link);
3812 if (r < 0)
3813 return r;
3814 }
3815
3816 r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname);
3817 if (r >= 0 && !streq(ifname, link->ifname)) {
3818 Manager *manager = link->manager;
3819
3820 log_link_info(link, "Interface name change detected, %s has been renamed to %s.", link->ifname, ifname);
3821
3822 link_drop(link);
3823 r = link_add(manager, m, &link);
3824 if (r < 0)
3825 return r;
3826 }
3827
3828 r = sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &s);
3829 if (r >= 0)
3830 strv_free_and_replace(link->alternative_names, s);
3831
3832 r = sd_netlink_message_read_u32(m, IFLA_MTU, &mtu);
3833 if (r >= 0 && mtu > 0) {
3834 link->mtu = mtu;
3835 if (link->original_mtu == 0) {
3836 link->original_mtu = mtu;
3837 log_link_debug(link, "Saved original MTU: %" PRIu32, link->original_mtu);
3838 }
3839
3840 if (link->dhcp_client) {
3841 r = sd_dhcp_client_set_mtu(link->dhcp_client,
3842 link->mtu);
3843 if (r < 0)
3844 return log_link_warning_errno(link, r, "Could not update MTU in DHCP client: %m");
3845 }
3846
3847 if (link->radv) {
3848 r = sd_radv_set_mtu(link->radv, link->mtu);
3849 if (r < 0)
3850 return log_link_warning_errno(link, r, "Could not set MTU for Router Advertisement: %m");
3851 }
3852 }
3853
3854 /* The kernel may broadcast NEWLINK messages without the MAC address
3855 set, simply ignore them. */
3856 r = sd_netlink_message_read_ether_addr(m, IFLA_ADDRESS, &mac);
3857 if (r >= 0) {
3858 if (memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet,
3859 ETH_ALEN)) {
3860
3861 memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet,
3862 ETH_ALEN);
3863
3864 log_link_debug(link, "MAC address: "
3865 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3866 mac.ether_addr_octet[0],
3867 mac.ether_addr_octet[1],
3868 mac.ether_addr_octet[2],
3869 mac.ether_addr_octet[3],
3870 mac.ether_addr_octet[4],
3871 mac.ether_addr_octet[5]);
3872
3873 if (link->ipv4ll) {
3874 r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac);
3875 if (r < 0)
3876 return log_link_warning_errno(link, r, "Could not update MAC address in IPv4LL client: %m");
3877 }
3878
3879 if (link->dhcp_client) {
3880 r = sd_dhcp_client_set_mac(link->dhcp_client,
3881 (const uint8_t *) &link->mac,
3882 sizeof (link->mac),
3883 ARPHRD_ETHER);
3884 if (r < 0)
3885 return log_link_warning_errno(link, r, "Could not update MAC address in DHCP client: %m");
3886
3887 r = dhcp4_set_client_identifier(link);
3888 if (r < 0)
3889 return r;
3890 }
3891
3892 if (link->dhcp6_client) {
3893 const DUID* duid = link_get_duid(link);
3894
3895 r = sd_dhcp6_client_set_mac(link->dhcp6_client,
3896 (const uint8_t *) &link->mac,
3897 sizeof (link->mac),
3898 ARPHRD_ETHER);
3899 if (r < 0)
3900 return log_link_warning_errno(link, r, "Could not update MAC address in DHCPv6 client: %m");
3901
3902 if (link->network->iaid_set) {
3903 r = sd_dhcp6_client_set_iaid(link->dhcp6_client,
3904 link->network->iaid);
3905 if (r < 0)
3906 return log_link_warning_errno(link, r, "Could not update DHCPv6 IAID: %m");
3907 }
3908
3909 r = sd_dhcp6_client_set_duid(link->dhcp6_client,
3910 duid->type,
3911 duid->raw_data_len > 0 ? duid->raw_data : NULL,
3912 duid->raw_data_len);
3913 if (r < 0)
3914 return log_link_warning_errno(link, r, "Could not update DHCPv6 DUID: %m");
3915 }
3916
3917 if (link->radv) {
3918 r = sd_radv_set_mac(link->radv, &link->mac);
3919 if (r < 0)
3920 return log_link_warning_errno(link, r, "Could not update MAC for Router Advertisement: %m");
3921 }
3922
3923 if (link->ndisc) {
3924 r = sd_ndisc_set_mac(link->ndisc, &link->mac);
3925 if (r < 0)
3926 return log_link_warning_errno(link, r, "Could not update MAC for ndisc: %m");
3927 }
3928 }
3929 }
3930
3931 old_master = link->master_ifindex;
3932 (void) sd_netlink_message_read_u32(m, IFLA_MASTER, (uint32_t *) &link->master_ifindex);
3933
3934 link_was_admin_up = link->flags & IFF_UP;
3935 had_carrier = link_has_carrier(link);
3936
3937 r = link_update_flags(link, m, old_master != link->master_ifindex);
3938 if (r < 0)
3939 return r;
3940
3941 if (!link_was_admin_up && (link->flags & IFF_UP)) {
3942 log_link_info(link, "Link UP");
3943
3944 r = link_admin_state_up(link);
3945 if (r < 0)
3946 return r;
3947 } else if (link_was_admin_up && !(link->flags & IFF_UP))
3948 log_link_info(link, "Link DOWN");
3949
3950 r = link_update_lldp(link);
3951 if (r < 0)
3952 return r;
3953
3954 carrier_gained = !had_carrier && link_has_carrier(link);
3955 carrier_lost = had_carrier && !link_has_carrier(link);
3956
3957 if (carrier_gained) {
3958 log_link_info(link, "Gained carrier");
3959
3960 r = link_carrier_gained(link);
3961 if (r < 0)
3962 return r;
3963 } else if (carrier_lost) {
3964 log_link_info(link, "Lost carrier");
3965
3966 r = link_carrier_lost(link);
3967 if (r < 0)
3968 return r;
3969 }
3970
3971 return 0;
3972 }
3973
3974 static void print_link_hashmap(FILE *f, const char *prefix, Hashmap* h) {
3975 bool space = false;
3976 Iterator i;
3977 Link *link;
3978
3979 assert(f);
3980 assert(prefix);
3981
3982 if (hashmap_isempty(h))
3983 return;
3984
3985 fputs(prefix, f);
3986 HASHMAP_FOREACH(link, h, i) {
3987 if (space)
3988 fputc(' ', f);
3989
3990 fprintf(f, "%i", link->ifindex);
3991 space = true;
3992 }
3993
3994 fputc('\n', f);
3995 }
3996
3997 static void link_save_dns(FILE *f, struct in_addr_data *dns, unsigned n_dns, bool *space) {
3998 for (unsigned j = 0; j < n_dns; j++) {
3999 _cleanup_free_ char *b = NULL;
4000 int r;
4001
4002 r = in_addr_to_string(dns[j].family, &dns[j].address, &b);
4003 if (r < 0) {
4004 log_debug_errno(r, "Failed to format address, ignoring: %m");
4005 continue;
4006 }
4007
4008 if (*space)
4009 fputc(' ', f);
4010 fputs(b, f);
4011 *space = true;
4012 }
4013 }
4014
4015 static void serialize_addresses(
4016 FILE *f,
4017 const char *lvalue,
4018 bool *space,
4019 char **addresses,
4020 sd_dhcp_lease *lease,
4021 bool conditional,
4022 sd_dhcp_lease_server_type what,
4023 sd_dhcp6_lease *lease6,
4024 bool conditional6,
4025 int (*lease6_get_addr)(sd_dhcp6_lease*, const struct in6_addr**),
4026 int (*lease6_get_fqdn)(sd_dhcp6_lease*, char ***)) {
4027 int r;
4028
4029 bool _space = false;
4030 if (!space)
4031 space = &_space;
4032
4033 if (lvalue)
4034 fprintf(f, "%s=", lvalue);
4035 fputstrv(f, addresses, NULL, space);
4036
4037 if (lease && conditional) {
4038 const struct in_addr *lease_addresses;
4039
4040 r = sd_dhcp_lease_get_servers(lease, what, &lease_addresses);
4041 if (r > 0)
4042 serialize_in_addrs(f, lease_addresses, r, space, in4_addr_is_non_local);
4043 }
4044
4045 if (lease6 && conditional6 && lease6_get_addr) {
4046 const struct in6_addr *in6_addrs;
4047
4048 r = lease6_get_addr(lease6, &in6_addrs);
4049 if (r > 0)
4050 serialize_in6_addrs(f, in6_addrs, r, space);
4051 }
4052
4053 if (lease6 && conditional6 && lease6_get_fqdn) {
4054 char **in6_hosts;
4055
4056 r = lease6_get_fqdn(lease6, &in6_hosts);
4057 if (r > 0)
4058 fputstrv(f, in6_hosts, NULL, space);
4059 }
4060
4061 if (lvalue)
4062 fputc('\n', f);
4063 }
4064
4065 int link_save(Link *link) {
4066 const char *admin_state, *oper_state, *carrier_state, *address_state;
4067 _cleanup_free_ char *temp_path = NULL;
4068 _cleanup_fclose_ FILE *f = NULL;
4069 Address *a;
4070 Route *route;
4071 Iterator i;
4072 int r;
4073
4074 assert(link);
4075 assert(link->state_file);
4076 assert(link->lease_file);
4077 assert(link->manager);
4078
4079 if (link->state == LINK_STATE_LINGER) {
4080 (void) unlink(link->state_file);
4081 return 0;
4082 }
4083
4084 link_lldp_save(link);
4085
4086 admin_state = link_state_to_string(link->state);
4087 assert(admin_state);
4088
4089 oper_state = link_operstate_to_string(link->operstate);
4090 assert(oper_state);
4091
4092 carrier_state = link_carrier_state_to_string(link->carrier_state);
4093 assert(carrier_state);
4094
4095 address_state = link_address_state_to_string(link->address_state);
4096 assert(address_state);
4097
4098 r = fopen_temporary(link->state_file, &f, &temp_path);
4099 if (r < 0)
4100 goto fail;
4101
4102 (void) fchmod(fileno(f), 0644);
4103
4104 fprintf(f,
4105 "# This is private data. Do not parse.\n"
4106 "ADMIN_STATE=%s\n"
4107 "OPER_STATE=%s\n"
4108 "CARRIER_STATE=%s\n"
4109 "ADDRESS_STATE=%s\n",
4110 admin_state, oper_state, carrier_state, address_state);
4111
4112 if (link->network) {
4113 char **dhcp6_domains = NULL, **dhcp_domains = NULL;
4114 const char *dhcp_domainname = NULL, *p;
4115 sd_dhcp6_lease *dhcp6_lease = NULL;
4116 bool space;
4117
4118 fprintf(f, "REQUIRED_FOR_ONLINE=%s\n",
4119 yes_no(link->network->required_for_online));
4120
4121 LinkOperationalStateRange st = link->network->required_operstate_for_online;
4122 fprintf(f, "REQUIRED_OPER_STATE_FOR_ONLINE=%s%s%s\n",
4123 strempty(link_operstate_to_string(st.min)),
4124 st.max != LINK_OPERSTATE_RANGE_DEFAULT.max ? ":" : "",
4125 st.max != LINK_OPERSTATE_RANGE_DEFAULT.max ? strempty(link_operstate_to_string(st.max)) : "");
4126
4127 if (link->dhcp6_client) {
4128 r = sd_dhcp6_client_get_lease(link->dhcp6_client, &dhcp6_lease);
4129 if (r < 0 && r != -ENOMSG)
4130 log_link_debug_errno(link, r, "Failed to get DHCPv6 lease: %m");
4131 }
4132
4133 fprintf(f, "NETWORK_FILE=%s\n", link->network->filename);
4134
4135 /************************************************************/
4136
4137 fputs("DNS=", f);
4138 space = false;
4139 if (link->n_dns != (unsigned) -1)
4140 link_save_dns(f, link->dns, link->n_dns, &space);
4141 else
4142 link_save_dns(f, link->network->dns, link->network->n_dns, &space);
4143
4144 serialize_addresses(f, NULL, &space,
4145 NULL,
4146 link->dhcp_lease,
4147 link->network->dhcp_use_dns,
4148 SD_DHCP_LEASE_DNS,
4149 dhcp6_lease,
4150 link->network->dhcp6_use_dns,
4151 sd_dhcp6_lease_get_dns,
4152 NULL);
4153
4154 /* Make sure to flush out old entries before we use the NDISC data */
4155 ndisc_vacuum(link);
4156
4157 if (link->network->ipv6_accept_ra_use_dns && link->ndisc_rdnss) {
4158 NDiscRDNSS *dd;
4159
4160 SET_FOREACH(dd, link->ndisc_rdnss, i)
4161 serialize_in6_addrs(f, &dd->address, 1, &space);
4162 }
4163
4164 fputc('\n', f);
4165
4166 /************************************************************/
4167
4168 serialize_addresses(f, "NTP", NULL,
4169 link->ntp ?: link->network->ntp,
4170 link->dhcp_lease,
4171 link->network->dhcp_use_ntp,
4172 SD_DHCP_LEASE_NTP,
4173 dhcp6_lease,
4174 link->network->dhcp6_use_ntp,
4175 sd_dhcp6_lease_get_ntp_addrs,
4176 sd_dhcp6_lease_get_ntp_fqdn);
4177
4178 serialize_addresses(f, "SIP", NULL,
4179 link->network->sip,
4180 link->dhcp_lease,
4181 link->network->dhcp_use_sip,
4182 SD_DHCP_LEASE_SIP,
4183 false, NULL, NULL, NULL);
4184
4185 serialize_addresses(f, "POP3", NULL,
4186 link->network->pop3,
4187 link->dhcp_lease,
4188 true,
4189 SD_DHCP_LEASE_POP3,
4190 false, NULL, NULL, NULL);
4191
4192 serialize_addresses(f, "SMTP", NULL,
4193 link->network->smtp,
4194 link->dhcp_lease,
4195 true,
4196 SD_DHCP_LEASE_SMTP,
4197 false, NULL, NULL, NULL);
4198
4199 serialize_addresses(f, "LPR", NULL,
4200 link->network->lpr,
4201 link->dhcp_lease,
4202 true,
4203 SD_DHCP_LEASE_LPR,
4204 false, NULL, NULL, NULL);
4205
4206 /************************************************************/
4207
4208 if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) {
4209 if (link->dhcp_lease) {
4210 (void) sd_dhcp_lease_get_domainname(link->dhcp_lease, &dhcp_domainname);
4211 (void) sd_dhcp_lease_get_search_domains(link->dhcp_lease, &dhcp_domains);
4212 }
4213 if (dhcp6_lease)
4214 (void) sd_dhcp6_lease_get_domains(dhcp6_lease, &dhcp6_domains);
4215 }
4216
4217 fputs("DOMAINS=", f);
4218 space = false;
4219 ORDERED_SET_FOREACH(p, link->search_domains ?: link->network->search_domains, i)
4220 fputs_with_space(f, p, NULL, &space);
4221
4222 if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) {
4223 if (dhcp_domainname)
4224 fputs_with_space(f, dhcp_domainname, NULL, &space);
4225 if (dhcp_domains)
4226 fputstrv(f, dhcp_domains, NULL, &space);
4227 if (dhcp6_domains)
4228 fputstrv(f, dhcp6_domains, NULL, &space);
4229 }
4230
4231 if (link->network->ipv6_accept_ra_use_domains == DHCP_USE_DOMAINS_YES) {
4232 NDiscDNSSL *dd;
4233
4234 SET_FOREACH(dd, link->ndisc_dnssl, i)
4235 fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space);
4236 }
4237
4238 fputc('\n', f);
4239
4240 /************************************************************/
4241
4242 fputs("ROUTE_DOMAINS=", f);
4243 space = false;
4244 ORDERED_SET_FOREACH(p, link->route_domains ?: link->network->route_domains, i)
4245 fputs_with_space(f, p, NULL, &space);
4246
4247 if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_ROUTE) {
4248 if (dhcp_domainname)
4249 fputs_with_space(f, dhcp_domainname, NULL, &space);
4250 if (dhcp_domains)
4251 fputstrv(f, dhcp_domains, NULL, &space);
4252 if (dhcp6_domains)
4253 fputstrv(f, dhcp6_domains, NULL, &space);
4254 }
4255
4256 if (link->network->ipv6_accept_ra_use_domains == DHCP_USE_DOMAINS_ROUTE) {
4257 NDiscDNSSL *dd;
4258
4259 SET_FOREACH(dd, link->ndisc_dnssl, i)
4260 fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space);
4261 }
4262
4263 fputc('\n', f);
4264
4265 /************************************************************/
4266
4267 fprintf(f, "LLMNR=%s\n",
4268 resolve_support_to_string(link->llmnr >= 0 ? link->llmnr : link->network->llmnr));
4269
4270 /************************************************************/
4271
4272 fprintf(f, "MDNS=%s\n",
4273 resolve_support_to_string(link->mdns >= 0 ? link->mdns : link->network->mdns));
4274
4275 /************************************************************/
4276
4277 int dns_default_route =
4278 link->dns_default_route >= 0 ? link->dns_default_route :
4279 link->network->dns_default_route;
4280 if (dns_default_route >= 0)
4281 fprintf(f, "DNS_DEFAULT_ROUTE=%s\n", yes_no(dns_default_route));
4282
4283 /************************************************************/
4284
4285 DnsOverTlsMode dns_over_tls_mode =
4286 link->dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID ? link->dns_over_tls_mode :
4287 link->network->dns_over_tls_mode;
4288 if (dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID)
4289 fprintf(f, "DNS_OVER_TLS=%s\n", dns_over_tls_mode_to_string(dns_over_tls_mode));
4290
4291 /************************************************************/
4292
4293 DnssecMode dnssec_mode =
4294 link->dnssec_mode != _DNSSEC_MODE_INVALID ? link->dnssec_mode :
4295 link->network->dnssec_mode;
4296 if (dnssec_mode != _DNSSEC_MODE_INVALID)
4297 fprintf(f, "DNSSEC=%s\n", dnssec_mode_to_string(dnssec_mode));
4298
4299 /************************************************************/
4300
4301 Set *nta_anchors = link->dnssec_negative_trust_anchors;
4302 if (set_isempty(nta_anchors))
4303 nta_anchors = link->network->dnssec_negative_trust_anchors;
4304
4305 if (!set_isempty(nta_anchors)) {
4306 const char *n;
4307
4308 fputs("DNSSEC_NTA=", f);
4309 space = false;
4310 SET_FOREACH(n, nta_anchors, i)
4311 fputs_with_space(f, n, NULL, &space);
4312 fputc('\n', f);
4313 }
4314
4315 /************************************************************/
4316
4317 fputs("ADDRESSES=", f);
4318 space = false;
4319 SET_FOREACH(a, link->addresses, i) {
4320 _cleanup_free_ char *address_str = NULL;
4321
4322 r = in_addr_to_string(a->family, &a->in_addr, &address_str);
4323 if (r < 0)
4324 goto fail;
4325
4326 fprintf(f, "%s%s/%u", space ? " " : "", address_str, a->prefixlen);
4327 space = true;
4328 }
4329 fputc('\n', f);
4330
4331 /************************************************************/
4332
4333 fputs("ROUTES=", f);
4334 space = false;
4335 SET_FOREACH(route, link->routes, i) {
4336 _cleanup_free_ char *route_str = NULL;
4337
4338 r = in_addr_to_string(route->family, &route->dst, &route_str);
4339 if (r < 0)
4340 goto fail;
4341
4342 fprintf(f, "%s%s/%hhu/%hhu/%"PRIu32"/%"PRIu32"/"USEC_FMT,
4343 space ? " " : "", route_str,
4344 route->dst_prefixlen, route->tos, route->priority, route->table, route->lifetime);
4345 space = true;
4346 }
4347
4348 fputc('\n', f);
4349 }
4350
4351 print_link_hashmap(f, "CARRIER_BOUND_TO=", link->bound_to_links);
4352 print_link_hashmap(f, "CARRIER_BOUND_BY=", link->bound_by_links);
4353
4354 if (link->dhcp_lease) {
4355 struct in_addr address;
4356 const char *tz = NULL;
4357 size_t client_id_len;
4358 const void *client_id;
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 = sd_dhcp_lease_get_client_id(link->dhcp_lease, &client_id, &client_id_len);
4374 if (r >= 0) {
4375 _cleanup_free_ char *id = NULL;
4376
4377 r = sd_dhcp_client_id_to_string(client_id, client_id_len, &id);
4378 if (r >= 0)
4379 fprintf(f, "DHCP4_CLIENT_ID=%s\n", id);
4380 }
4381
4382 r = dhcp_lease_save(link->dhcp_lease, link->lease_file);
4383 if (r < 0)
4384 goto fail;
4385
4386 fprintf(f,
4387 "DHCP_LEASE=%s\n",
4388 link->lease_file);
4389 } else
4390 (void) unlink(link->lease_file);
4391
4392 if (link->ipv4ll) {
4393 struct in_addr address;
4394
4395 r = sd_ipv4ll_get_address(link->ipv4ll, &address);
4396 if (r >= 0) {
4397 fputs("IPV4LL_ADDRESS=", f);
4398 serialize_in_addrs(f, &address, 1, false, NULL);
4399 fputc('\n', f);
4400 }
4401 }
4402
4403 r = fflush_and_check(f);
4404 if (r < 0)
4405 goto fail;
4406
4407 if (rename(temp_path, link->state_file) < 0) {
4408 r = -errno;
4409 goto fail;
4410 }
4411
4412 return 0;
4413
4414 fail:
4415 (void) unlink(link->state_file);
4416 if (temp_path)
4417 (void) unlink(temp_path);
4418
4419 return log_link_error_errno(link, r, "Failed to save link data to %s: %m", link->state_file);
4420 }
4421
4422 /* The serialized state in /run is no longer up-to-date. */
4423 void link_dirty(Link *link) {
4424 int r;
4425
4426 assert(link);
4427
4428 /* mark manager dirty as link is dirty */
4429 manager_dirty(link->manager);
4430
4431 r = set_ensure_allocated(&link->manager->dirty_links, NULL);
4432 if (r < 0)
4433 /* allocation errors are ignored */
4434 return;
4435
4436 r = set_put(link->manager->dirty_links, link);
4437 if (r <= 0)
4438 /* don't take another ref if the link was already dirty */
4439 return;
4440
4441 link_ref(link);
4442 }
4443
4444 /* The serialized state in /run is up-to-date */
4445 void link_clean(Link *link) {
4446 assert(link);
4447 assert(link->manager);
4448
4449 link_unref(set_remove(link->manager->dirty_links, link));
4450 }
4451
4452 static const char* const link_ipv6_address_gen_mode_table[_LINK_IPV6_ADDRESS_GEN_MODE_MAX] = {
4453 [LINK_IPV6_ADDRESSS_GEN_MODE_EUI64] = "eui64",
4454 [LINK_IPV6_ADDRESSS_GEN_MODE_NONE] = "none",
4455 [LINK_IPV6_ADDRESSS_GEN_MODE_STABLE_PRIVACY] = "stable-privacy",
4456 [LINK_IPV6_ADDRESSS_GEN_MODE_RANDOM] = "random",
4457 };
4458
4459 DEFINE_STRING_TABLE_LOOKUP(link_ipv6_address_gen_mode, LinkIPv6AddressGenMode);
4460 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");
4461
4462 static const char* const link_state_table[_LINK_STATE_MAX] = {
4463 [LINK_STATE_PENDING] = "pending",
4464 [LINK_STATE_INITIALIZED] = "initialized",
4465 [LINK_STATE_CONFIGURING] = "configuring",
4466 [LINK_STATE_CONFIGURED] = "configured",
4467 [LINK_STATE_UNMANAGED] = "unmanaged",
4468 [LINK_STATE_FAILED] = "failed",
4469 [LINK_STATE_LINGER] = "linger",
4470 };
4471
4472 DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
4473
4474 int log_link_message_full_errno(Link *link, sd_netlink_message *m, int level, int err, const char *msg) {
4475 const char *err_msg = NULL;
4476
4477 (void) sd_netlink_message_read_string(m, NLMSGERR_ATTR_MSG, &err_msg);
4478 return log_link_full(link, level, err,
4479 "%s: %s%s%s%m",
4480 msg,
4481 strempty(err_msg),
4482 err_msg && !endswith(err_msg, ".") ? "." : "",
4483 err_msg ? " " : "");
4484 }