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