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