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