]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-link.c
sd-dhcp-client: minor simplification
[thirdparty/systemd.git] / src / network / networkd-link.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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-address.h"
25 #include "networkd-can.h"
26 #include "networkd-dhcp-server.h"
27 #include "networkd-dhcp4.h"
28 #include "networkd-dhcp6.h"
29 #include "networkd-fdb.h"
30 #include "networkd-ipv4ll.h"
31 #include "networkd-ipv6-proxy-ndp.h"
32 #include "networkd-link-bus.h"
33 #include "networkd-link.h"
34 #include "networkd-lldp-tx.h"
35 #include "networkd-manager.h"
36 #include "networkd-mdb.h"
37 #include "networkd-ndisc.h"
38 #include "networkd-neighbor.h"
39 #include "networkd-nexthop.h"
40 #include "networkd-sriov.h"
41 #include "networkd-sysctl.h"
42 #include "networkd-radv.h"
43 #include "networkd-routing-policy-rule.h"
44 #include "networkd-wifi.h"
45 #include "set.h"
46 #include "socket-util.h"
47 #include "stat-util.h"
48 #include "stdio-util.h"
49 #include "string-table.h"
50 #include "strv.h"
51 #include "sysctl-util.h"
52 #include "tc.h"
53 #include "tmpfile-util.h"
54 #include "udev-util.h"
55 #include "util.h"
56 #include "vrf.h"
57
58 bool link_ipv4ll_enabled(Link *link, AddressFamily mask) {
59 assert(link);
60 assert((mask & ~(ADDRESS_FAMILY_IPV4 | ADDRESS_FAMILY_FALLBACK_IPV4)) == 0);
61
62 if (link->flags & IFF_LOOPBACK)
63 return false;
64
65 if (!link->network)
66 return false;
67
68 if (link->iftype == ARPHRD_CAN)
69 return false;
70
71 if (STRPTR_IN_SET(link->kind,
72 "vrf", "wireguard", "ipip", "gre", "ip6gre","ip6tnl", "sit", "vti",
73 "vti6", "nlmon", "xfrm", "bareudp"))
74 return false;
75
76 /* L3 or L3S mode do not support ARP. */
77 if (IN_SET(link_get_ipvlan_mode(link), NETDEV_IPVLAN_MODE_L3, NETDEV_IPVLAN_MODE_L3S))
78 return false;
79
80 if (link->network->bond)
81 return false;
82
83 return link->network->link_local & mask;
84 }
85
86 bool link_ipv6ll_enabled(Link *link) {
87 assert(link);
88
89 if (!socket_ipv6_is_supported())
90 return false;
91
92 if (link->flags & IFF_LOOPBACK)
93 return false;
94
95 if (!link->network)
96 return false;
97
98 if (link->iftype == ARPHRD_CAN)
99 return false;
100
101 if (STRPTR_IN_SET(link->kind, "vrf", "wireguard", "ipip", "gre", "sit", "vti", "nlmon"))
102 return false;
103
104 if (link->network->bond)
105 return false;
106
107 return link->network->link_local & ADDRESS_FAMILY_IPV6;
108 }
109
110 bool link_ipv6_enabled(Link *link) {
111 assert(link);
112
113 if (!socket_ipv6_is_supported())
114 return false;
115
116 if (link->network->bond)
117 return false;
118
119 if (link->iftype == ARPHRD_CAN)
120 return false;
121
122 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
123 if (link_ipv6ll_enabled(link))
124 return true;
125
126 if (network_has_static_ipv6_configurations(link->network))
127 return true;
128
129 return false;
130 }
131
132 static bool link_is_enslaved(Link *link) {
133 if (link->flags & IFF_SLAVE)
134 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
135 return true;
136
137 if (!link->network)
138 return false;
139
140 if (link->master_ifindex > 0 && link->network->bridge)
141 return true;
142
143 /* TODO: add conditions for other netdevs. */
144
145 return false;
146 }
147
148 static void link_update_master_operstate(Link *link, NetDev *netdev) {
149 Link *master;
150
151 if (!netdev)
152 return;
153
154 if (netdev->ifindex <= 0)
155 return;
156
157 if (link_get(link->manager, netdev->ifindex, &master) < 0)
158 return;
159
160 link_update_operstate(master, true);
161 }
162
163 void link_update_operstate(Link *link, bool also_update_master) {
164 LinkOperationalState operstate;
165 LinkCarrierState carrier_state;
166 LinkAddressState address_state;
167 _cleanup_strv_free_ char **p = NULL;
168 uint8_t scope = RT_SCOPE_NOWHERE;
169 bool changed = false;
170 Address *address;
171
172 assert(link);
173
174 if (link->kernel_operstate == IF_OPER_DORMANT)
175 carrier_state = LINK_CARRIER_STATE_DORMANT;
176 else if (link_has_carrier(link)) {
177 if (link_is_enslaved(link))
178 carrier_state = LINK_CARRIER_STATE_ENSLAVED;
179 else
180 carrier_state = LINK_CARRIER_STATE_CARRIER;
181 } else if (link->flags & IFF_UP)
182 carrier_state = LINK_CARRIER_STATE_NO_CARRIER;
183 else
184 carrier_state = LINK_CARRIER_STATE_OFF;
185
186 if (carrier_state >= LINK_CARRIER_STATE_CARRIER) {
187 Link *slave;
188
189 SET_FOREACH(slave, link->slaves) {
190 link_update_operstate(slave, false);
191
192 if (slave->carrier_state < LINK_CARRIER_STATE_CARRIER)
193 carrier_state = LINK_CARRIER_STATE_DEGRADED_CARRIER;
194 }
195 }
196
197 SET_FOREACH(address, link->addresses) {
198 if (!address_is_ready(address))
199 continue;
200
201 if (address->scope < scope)
202 scope = address->scope;
203 }
204
205 /* for operstate we also take foreign addresses into account */
206 SET_FOREACH(address, link->addresses_foreign) {
207 if (!address_is_ready(address))
208 continue;
209
210 if (address->scope < scope)
211 scope = address->scope;
212 }
213
214 if (scope < RT_SCOPE_SITE)
215 /* universally accessible addresses found */
216 address_state = LINK_ADDRESS_STATE_ROUTABLE;
217 else if (scope < RT_SCOPE_HOST)
218 /* only link or site local addresses found */
219 address_state = LINK_ADDRESS_STATE_DEGRADED;
220 else
221 /* no useful addresses found */
222 address_state = LINK_ADDRESS_STATE_OFF;
223
224 /* Mapping of address and carrier state vs operational state
225 * carrier state
226 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
227 * ------------------------------------------------------------------------------
228 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
229 * address_state degraded | off | no-carrier | dormant | degraded-carrier | degraded | enslaved
230 * routable | off | no-carrier | dormant | degraded-carrier | routable | routable
231 */
232
233 if (carrier_state < LINK_CARRIER_STATE_CARRIER || address_state == LINK_ADDRESS_STATE_OFF)
234 operstate = (LinkOperationalState) carrier_state;
235 else if (address_state == LINK_ADDRESS_STATE_ROUTABLE)
236 operstate = LINK_OPERSTATE_ROUTABLE;
237 else if (carrier_state == LINK_CARRIER_STATE_CARRIER)
238 operstate = LINK_OPERSTATE_DEGRADED;
239 else
240 operstate = LINK_OPERSTATE_ENSLAVED;
241
242 if (link->carrier_state != carrier_state) {
243 link->carrier_state = carrier_state;
244 changed = true;
245 if (strv_extend(&p, "CarrierState") < 0)
246 log_oom();
247 }
248
249 if (link->address_state != address_state) {
250 link->address_state = address_state;
251 changed = true;
252 if (strv_extend(&p, "AddressState") < 0)
253 log_oom();
254 }
255
256 if (link->operstate != operstate) {
257 link->operstate = operstate;
258 changed = true;
259 if (strv_extend(&p, "OperationalState") < 0)
260 log_oom();
261 }
262
263 if (p)
264 link_send_changed_strv(link, p);
265 if (changed)
266 link_dirty(link);
267
268 if (also_update_master && link->network) {
269 link_update_master_operstate(link, link->network->bond);
270 link_update_master_operstate(link, link->network->bridge);
271 }
272 }
273
274 #define FLAG_STRING(string, flag, old, new) \
275 (((old ^ new) & flag) \
276 ? ((old & flag) ? (" -" string) : (" +" string)) \
277 : "")
278
279 static int link_update_flags(Link *link, sd_netlink_message *m, bool force_update_operstate) {
280 unsigned flags, unknown_flags_added, unknown_flags_removed, unknown_flags;
281 uint8_t operstate;
282 int r;
283
284 assert(link);
285
286 r = sd_rtnl_message_link_get_flags(m, &flags);
287 if (r < 0)
288 return log_link_warning_errno(link, r, "Could not get link flags: %m");
289
290 r = sd_netlink_message_read_u8(m, IFLA_OPERSTATE, &operstate);
291 if (r < 0)
292 /* if we got a message without operstate, take it to mean
293 the state was unchanged */
294 operstate = link->kernel_operstate;
295
296 if (!force_update_operstate && (link->flags == flags) && (link->kernel_operstate == operstate))
297 return 0;
298
299 if (link->flags != flags) {
300 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",
301 FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
302 FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
303 FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
304 FLAG_STRING("UP", IFF_UP, link->flags, flags),
305 FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
306 FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
307 FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
308 FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
309 FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
310 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
311 FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
312 FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
313 FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
314 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
315 FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
316 FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
317 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
318 FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
319 FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
320
321 unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
322 IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
323 IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
324 IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
325 IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
326 IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
327 unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
328 unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
329
330 /* link flags are currently at most 18 bits, let's align to
331 * printing 20 */
332 if (unknown_flags_added)
333 log_link_debug(link,
334 "Unknown link flags gained: %#.5x (ignoring)",
335 unknown_flags_added);
336
337 if (unknown_flags_removed)
338 log_link_debug(link,
339 "Unknown link flags lost: %#.5x (ignoring)",
340 unknown_flags_removed);
341 }
342
343 link->flags = flags;
344 link->kernel_operstate = operstate;
345
346 link_update_operstate(link, true);
347
348 return 0;
349 }
350
351 static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
352 _cleanup_(link_unrefp) Link *link = NULL;
353 const char *ifname, *kind = NULL;
354 unsigned short iftype;
355 int r, ifindex;
356 uint16_t type;
357
358 assert(manager);
359 assert(message);
360 assert(ret);
361
362 /* check for link kind */
363 r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
364 if (r == 0) {
365 (void) sd_netlink_message_read_string(message, IFLA_INFO_KIND, &kind);
366 r = sd_netlink_message_exit_container(message);
367 if (r < 0)
368 return r;
369 }
370
371 r = sd_netlink_message_get_type(message, &type);
372 if (r < 0)
373 return r;
374 else if (type != RTM_NEWLINK)
375 return -EINVAL;
376
377 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
378 if (r < 0)
379 return r;
380 else if (ifindex <= 0)
381 return -EINVAL;
382
383 r = sd_rtnl_message_link_get_type(message, &iftype);
384 if (r < 0)
385 return r;
386
387 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname);
388 if (r < 0)
389 return r;
390
391 link = new(Link, 1);
392 if (!link)
393 return -ENOMEM;
394
395 *link = (Link) {
396 .n_ref = 1,
397 .manager = manager,
398 .state = LINK_STATE_PENDING,
399 .ifindex = ifindex,
400 .iftype = iftype,
401
402 .n_dns = (unsigned) -1,
403 .dns_default_route = -1,
404 .llmnr = _RESOLVE_SUPPORT_INVALID,
405 .mdns = _RESOLVE_SUPPORT_INVALID,
406 .dnssec_mode = _DNSSEC_MODE_INVALID,
407 .dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID,
408 };
409
410 link->ifname = strdup(ifname);
411 if (!link->ifname)
412 return -ENOMEM;
413
414 if (kind) {
415 link->kind = strdup(kind);
416 if (!link->kind)
417 return -ENOMEM;
418 }
419
420 r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t *)&link->master_ifindex);
421 if (r < 0)
422 log_link_debug_errno(link, r, "New device has no master, continuing without");
423
424 r = netlink_message_read_hw_addr(message, IFLA_ADDRESS, &link->hw_addr);
425 if (r < 0)
426 log_link_debug_errno(link, r, "Hardware address not found for new device, continuing without");
427
428 r = netlink_message_read_hw_addr(message, IFLA_BROADCAST, &link->bcast_addr);
429 if (r < 0)
430 log_link_debug_errno(link, r, "Broadcast address not found for new device, continuing without");
431
432 r = ethtool_get_permanent_macaddr(&manager->ethtool_fd, link->ifname, &link->permanent_mac);
433 if (r < 0)
434 log_link_debug_errno(link, r, "Permanent MAC address not found for new device, continuing without: %m");
435
436 r = ethtool_get_driver(&manager->ethtool_fd, link->ifname, &link->driver);
437 if (r < 0)
438 log_link_debug_errno(link, r, "Failed to get driver, continuing without: %m");
439
440 r = sd_netlink_message_read_strv(message, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &link->alternative_names);
441 if (r < 0 && r != -ENODATA)
442 return r;
443
444 if (asprintf(&link->state_file, "/run/systemd/netif/links/%d", link->ifindex) < 0)
445 return -ENOMEM;
446
447 if (asprintf(&link->lease_file, "/run/systemd/netif/leases/%d", link->ifindex) < 0)
448 return -ENOMEM;
449
450 if (asprintf(&link->lldp_file, "/run/systemd/netif/lldp/%d", link->ifindex) < 0)
451 return -ENOMEM;
452
453 r = hashmap_ensure_allocated(&manager->links, NULL);
454 if (r < 0)
455 return r;
456
457 r = hashmap_put(manager->links, INT_TO_PTR(link->ifindex), link);
458 if (r < 0)
459 return r;
460
461 r = link_update_flags(link, message, false);
462 if (r < 0)
463 return r;
464
465 *ret = TAKE_PTR(link);
466
467 return 0;
468 }
469
470 void link_ntp_settings_clear(Link *link) {
471 link->ntp = strv_free(link->ntp);
472 }
473
474 void link_dns_settings_clear(Link *link) {
475 if (link->n_dns != (unsigned) -1)
476 for (unsigned i = 0; i < link->n_dns; i++)
477 in_addr_full_free(link->dns[i]);
478 link->dns = mfree(link->dns);
479 link->n_dns = (unsigned) -1;
480
481 link->search_domains = ordered_set_free_free(link->search_domains);
482 link->route_domains = ordered_set_free_free(link->route_domains);
483
484 link->dns_default_route = -1;
485 link->llmnr = _RESOLVE_SUPPORT_INVALID;
486 link->mdns = _RESOLVE_SUPPORT_INVALID;
487 link->dnssec_mode = _DNSSEC_MODE_INVALID;
488 link->dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID;
489
490 link->dnssec_negative_trust_anchors = set_free_free(link->dnssec_negative_trust_anchors);
491 }
492
493 static void link_free_engines(Link *link) {
494 if (!link)
495 return;
496
497 link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server);
498 link->dhcp_client = sd_dhcp_client_unref(link->dhcp_client);
499 link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
500 link->dhcp_acd = sd_ipv4acd_unref(link->dhcp_acd);
501
502 link->lldp = sd_lldp_unref(link->lldp);
503 link_lldp_emit_stop(link);
504
505 ndisc_flush(link);
506
507 link->ipv4ll = sd_ipv4ll_unref(link->ipv4ll);
508 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
509 link->dhcp6_lease = sd_dhcp6_lease_unref(link->dhcp6_lease);
510 link->ndisc = sd_ndisc_unref(link->ndisc);
511 link->radv = sd_radv_unref(link->radv);
512
513 ipv4_dad_unref(link);
514 }
515
516 static Link *link_free(Link *link) {
517 assert(link);
518
519 link_ntp_settings_clear(link);
520 link_dns_settings_clear(link);
521
522 link->routes = set_free(link->routes);
523 link->routes_foreign = set_free(link->routes_foreign);
524 link->dhcp_routes = set_free(link->dhcp_routes);
525 link->dhcp_routes_old = set_free(link->dhcp_routes_old);
526 link->dhcp6_routes = set_free(link->dhcp6_routes);
527 link->dhcp6_routes_old = set_free(link->dhcp6_routes_old);
528 link->dhcp6_pd_routes = set_free(link->dhcp6_pd_routes);
529 link->dhcp6_pd_routes_old = set_free(link->dhcp6_pd_routes_old);
530 link->ndisc_routes = set_free(link->ndisc_routes);
531
532 link->nexthops = set_free(link->nexthops);
533 link->nexthops_foreign = set_free(link->nexthops_foreign);
534
535 link->neighbors = set_free(link->neighbors);
536 link->neighbors_foreign = set_free(link->neighbors_foreign);
537
538 link->addresses = set_free(link->addresses);
539 link->addresses_foreign = set_free(link->addresses_foreign);
540 link->pool_addresses = set_free(link->pool_addresses);
541 link->static_addresses = set_free(link->static_addresses);
542 link->dhcp6_addresses = set_free(link->dhcp6_addresses);
543 link->dhcp6_addresses_old = set_free(link->dhcp6_addresses_old);
544 link->dhcp6_pd_addresses = set_free(link->dhcp6_pd_addresses);
545 link->dhcp6_pd_addresses_old = set_free(link->dhcp6_pd_addresses_old);
546 link->ndisc_addresses = set_free(link->ndisc_addresses);
547
548 link_free_engines(link);
549 free(link->lease_file);
550 free(link->lldp_file);
551
552 free(link->ifname);
553 strv_free(link->alternative_names);
554 free(link->kind);
555 free(link->ssid);
556 free(link->driver);
557
558 (void) unlink(link->state_file);
559 free(link->state_file);
560
561 sd_device_unref(link->sd_device);
562
563 hashmap_free(link->bound_to_links);
564 hashmap_free(link->bound_by_links);
565
566 set_free_with_destructor(link->slaves, link_unref);
567
568 network_unref(link->network);
569
570 return mfree(link);
571 }
572
573 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free);
574
575 int link_get(Manager *m, int ifindex, Link **ret) {
576 Link *link;
577
578 assert(m);
579 assert(ifindex > 0);
580 assert(ret);
581
582 link = hashmap_get(m->links, INT_TO_PTR(ifindex));
583 if (!link)
584 return -ENODEV;
585
586 *ret = link;
587
588 return 0;
589 }
590
591 void link_set_state(Link *link, LinkState state) {
592 assert(link);
593
594 if (link->state == state)
595 return;
596
597 log_link_debug(link, "State changed: %s -> %s",
598 link_state_to_string(link->state),
599 link_state_to_string(state));
600
601 link->state = state;
602
603 link_send_changed(link, "AdministrativeState", NULL);
604 }
605
606 static void link_enter_unmanaged(Link *link) {
607 assert(link);
608
609 link_set_state(link, LINK_STATE_UNMANAGED);
610
611 link_dirty(link);
612 }
613
614 int link_stop_engines(Link *link, bool may_keep_dhcp) {
615 int r = 0, k;
616
617 assert(link);
618 assert(link->manager);
619 assert(link->manager->event);
620
621 bool keep_dhcp = may_keep_dhcp &&
622 link->network &&
623 (link->manager->restarting ||
624 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP_ON_STOP));
625
626 if (!keep_dhcp) {
627 k = sd_dhcp_client_stop(link->dhcp_client);
628 if (k < 0)
629 r = log_link_warning_errno(link, k, "Could not stop DHCPv4 client: %m");
630 }
631
632 k = sd_ipv4acd_stop(link->dhcp_acd);
633 if (k < 0)
634 r = log_link_warning_errno(link, k, "Could not stop IPv4 ACD client for DHCPv4: %m");
635
636 k = sd_dhcp_server_stop(link->dhcp_server);
637 if (k < 0)
638 r = log_link_warning_errno(link, k, "Could not stop DHCPv4 server: %m");
639
640 k = sd_lldp_stop(link->lldp);
641 if (k < 0)
642 r = log_link_warning_errno(link, k, "Could not stop LLDP: %m");
643
644 k = sd_ipv4ll_stop(link->ipv4ll);
645 if (k < 0)
646 r = log_link_warning_errno(link, k, "Could not stop IPv4 link-local: %m");
647
648 k = ipv4_dad_stop(link);
649 if (k < 0)
650 r = log_link_warning_errno(link, k, "Could not stop IPv4 ACD client: %m");
651
652 k = sd_dhcp6_client_stop(link->dhcp6_client);
653 if (k < 0)
654 r = log_link_warning_errno(link, k, "Could not stop DHCPv6 client: %m");
655
656 k = dhcp6_pd_remove(link);
657 if (k < 0)
658 r = log_link_warning_errno(link, k, "Could not remove DHCPv6 PD addresses and routes: %m");
659
660 k = sd_ndisc_stop(link->ndisc);
661 if (k < 0)
662 r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Discovery: %m");
663
664 k = sd_radv_stop(link->radv);
665 if (k < 0)
666 r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Advertisement: %m");
667
668 link_lldp_emit_stop(link);
669 return r;
670 }
671
672 void link_enter_failed(Link *link) {
673 assert(link);
674
675 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
676 return;
677
678 log_link_warning(link, "Failed");
679
680 link_set_state(link, LINK_STATE_FAILED);
681
682 (void) link_stop_engines(link, false);
683
684 link_dirty(link);
685 }
686
687 static int link_join_netdevs_after_configured(Link *link) {
688 NetDev *netdev;
689 int r;
690
691 HASHMAP_FOREACH(netdev, link->network->stacked_netdevs) {
692 if (netdev->ifindex > 0)
693 /* Assume already enslaved. */
694 continue;
695
696 if (netdev_get_create_type(netdev) != NETDEV_CREATE_AFTER_CONFIGURED)
697 continue;
698
699 log_struct(LOG_DEBUG,
700 LOG_LINK_INTERFACE(link),
701 LOG_NETDEV_INTERFACE(netdev),
702 LOG_LINK_MESSAGE(link, "Enslaving by '%s'", netdev->ifname));
703
704 r = netdev_join(netdev, link, NULL);
705 if (r < 0)
706 return log_struct_errno(LOG_WARNING, r,
707 LOG_LINK_INTERFACE(link),
708 LOG_NETDEV_INTERFACE(netdev),
709 LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", netdev->ifname));
710 }
711
712 return 0;
713 }
714
715 static void link_enter_configured(Link *link) {
716 assert(link);
717 assert(link->network);
718
719 if (link->state != LINK_STATE_CONFIGURING)
720 return;
721
722 link_set_state(link, LINK_STATE_CONFIGURED);
723
724 (void) link_join_netdevs_after_configured(link);
725
726 link_dirty(link);
727 }
728
729 void link_check_ready(Link *link) {
730 Address *a;
731
732 assert(link);
733
734 if (link->state == LINK_STATE_CONFIGURED)
735 return;
736
737 if (link->state != LINK_STATE_CONFIGURING) {
738 log_link_debug(link, "%s(): link is in %s state.", __func__, link_state_to_string(link->state));
739 return;
740 }
741
742 if (!link->network)
743 return;
744
745 if (!link->addresses_configured) {
746 log_link_debug(link, "%s(): static addresses are not configured.", __func__);
747 return;
748 }
749
750 if (!link->neighbors_configured) {
751 log_link_debug(link, "%s(): static neighbors are not configured.", __func__);
752 return;
753 }
754
755 SET_FOREACH(a, link->addresses)
756 if (!address_is_ready(a)) {
757 _cleanup_free_ char *str = NULL;
758
759 (void) in_addr_to_string(a->family, &a->in_addr, &str);
760 log_link_debug(link, "%s(): an address %s/%d is not ready.", __func__, strnull(str), a->prefixlen);
761 return;
762 }
763
764 if (!link->static_routes_configured) {
765 log_link_debug(link, "%s(): static routes are not configured.", __func__);
766 return;
767 }
768
769 if (!link->static_nexthops_configured) {
770 log_link_debug(link, "%s(): static nexthops are not configured.", __func__);
771 return;
772 }
773
774 if (!link->routing_policy_rules_configured) {
775 log_link_debug(link, "%s(): static routing policy rules are not configured.", __func__);
776 return;
777 }
778
779 if (!link->tc_configured) {
780 log_link_debug(link, "%s(): traffic controls are not configured.", __func__);
781 return;
782 }
783
784 if (!link->sr_iov_configured) {
785 log_link_debug(link, "%s(): SR-IOV is not configured.", __func__);
786 return;
787 }
788
789 if (!link->bridge_mdb_configured) {
790 log_link_debug(link, "%s(): Bridge MDB is not configured.", __func__);
791 return;
792 }
793
794 if (link_has_carrier(link) || !link->network->configure_without_carrier) {
795 bool has_ndisc_address = false;
796 NDiscAddress *n;
797
798 if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_IPV4) && !link->ipv4ll_address_configured) {
799 log_link_debug(link, "%s(): IPv4LL is not configured.", __func__);
800 return;
801 }
802
803 if (link_ipv6ll_enabled(link) &&
804 in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address)) {
805 log_link_debug(link, "%s(): IPv6LL is not configured.", __func__);
806 return;
807 }
808
809 SET_FOREACH(n, link->ndisc_addresses)
810 if (!n->marked) {
811 has_ndisc_address = true;
812 break;
813 }
814
815 if ((link_dhcp4_enabled(link) || link_dhcp6_enabled(link)) &&
816 !link->dhcp_address && set_isempty(link->dhcp6_addresses) && !has_ndisc_address &&
817 !(link_ipv4ll_enabled(link, ADDRESS_FAMILY_FALLBACK_IPV4) && link->ipv4ll_address_configured)) {
818 log_link_debug(link, "%s(): DHCP4 or DHCP6 is enabled but no dynamic address is assigned yet.", __func__);
819 return;
820 }
821
822 if (link_dhcp4_enabled(link) || link_dhcp6_enabled(link) || link_dhcp6_pd_is_enabled(link) || link_ipv6_accept_ra_enabled(link)) {
823 if (!link->dhcp4_configured &&
824 !(link->dhcp6_address_configured && link->dhcp6_route_configured) &&
825 !(link->dhcp6_pd_address_configured && link->dhcp6_pd_route_configured) &&
826 !(link->ndisc_addresses_configured && link->ndisc_routes_configured) &&
827 !(link_ipv4ll_enabled(link, ADDRESS_FAMILY_FALLBACK_IPV4) && link->ipv4ll_address_configured)) {
828 /* When DHCP or RA is enabled, at least one protocol must provide an address, or
829 * an IPv4ll fallback address must be configured. */
830 log_link_debug(link, "%s(): dynamic addresses or routes are not configured.", __func__);
831 return;
832 }
833
834 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",
835 __func__,
836 yes_no(link->dhcp4_configured),
837 yes_no(link->dhcp6_address_configured),
838 yes_no(link->dhcp6_route_configured),
839 yes_no(link->dhcp6_pd_address_configured),
840 yes_no(link->dhcp6_pd_route_configured),
841 yes_no(link->ndisc_addresses_configured),
842 yes_no(link->ndisc_routes_configured));
843 }
844 }
845
846 link_enter_configured(link);
847
848 return;
849 }
850
851 static int link_set_static_configs(Link *link) {
852 int r;
853
854 assert(link);
855 assert(link->network);
856 assert(link->state != _LINK_STATE_INVALID);
857
858 /* Reset all *_configured flags we are configuring. */
859 link->request_static_addresses = false;
860 link->addresses_configured = false;
861 link->addresses_ready = false;
862 link->neighbors_configured = false;
863 link->static_routes_configured = false;
864 link->static_nexthops_configured = false;
865 link->routing_policy_rules_configured = false;
866
867 r = link_set_bridge_fdb(link);
868 if (r < 0)
869 return r;
870
871 r = link_set_bridge_mdb(link);
872 if (r < 0)
873 return r;
874
875 r = link_set_neighbors(link);
876 if (r < 0)
877 return r;
878
879 r = link_set_addresses(link);
880 if (r < 0)
881 return r;
882
883 r = link_set_address_labels(link);
884 if (r < 0)
885 return r;
886
887 /* now that we can figure out a default address for the dhcp server, start it */
888 r = dhcp4_server_configure(link);
889 if (r < 0)
890 return r;
891
892 return 0;
893 }
894
895 static int link_configure_continue(Link *link);
896
897 static int link_mac_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
898 int r;
899
900 assert(link);
901
902 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
903 return 1;
904
905 r = sd_netlink_message_get_errno(m);
906 if (r < 0)
907 log_link_message_warning_errno(link, m, r, "Could not set MAC address, ignoring");
908 else
909 log_link_debug(link, "Setting MAC address done.");
910
911 return 1;
912 }
913
914 static int link_set_mac(Link *link) {
915 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
916 int r;
917
918 assert(link);
919 assert(link->network);
920 assert(link->manager);
921 assert(link->manager->rtnl);
922
923 if (!link->network->mac)
924 return 0;
925
926 log_link_debug(link, "Setting MAC address");
927
928 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
929 if (r < 0)
930 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
931
932 r = sd_netlink_message_append_ether_addr(req, IFLA_ADDRESS, link->network->mac);
933 if (r < 0)
934 return log_link_error_errno(link, r, "Could not set MAC address: %m");
935
936 r = netlink_call_async(link->manager->rtnl, NULL, req, link_mac_handler,
937 link_netlink_destroy_callback, link);
938 if (r < 0)
939 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
940
941 link_ref(link);
942
943 return 0;
944 }
945
946 static int link_nomaster_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
947 int r;
948
949 assert(link);
950
951 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
952 return 1;
953
954 r = sd_netlink_message_get_errno(m);
955 if (r < 0)
956 log_link_message_warning_errno(link, m, r, "Could not set nomaster, ignoring");
957 else
958 log_link_debug(link, "Setting nomaster done.");
959
960 return 1;
961 }
962
963 static int link_set_nomaster(Link *link) {
964 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
965 int r;
966
967 assert(link);
968 assert(link->network);
969 assert(link->manager);
970 assert(link->manager->rtnl);
971
972 /* set it free if not enslaved with networkd */
973 if (link->network->bridge || link->network->bond || link->network->vrf)
974 return 0;
975
976 log_link_debug(link, "Setting nomaster");
977
978 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
979 if (r < 0)
980 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
981
982 r = sd_netlink_message_append_u32(req, IFLA_MASTER, 0);
983 if (r < 0)
984 return log_link_error_errno(link, r, "Could not append IFLA_MASTER attribute: %m");
985
986 r = netlink_call_async(link->manager->rtnl, NULL, req, link_nomaster_handler,
987 link_netlink_destroy_callback, link);
988 if (r < 0)
989 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
990
991 link_ref(link);
992
993 return 0;
994 }
995
996 static int set_mtu_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
997 int r;
998
999 assert(m);
1000 assert(link);
1001 assert(link->ifname);
1002
1003 link->setting_mtu = false;
1004
1005 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1006 return 1;
1007
1008 r = sd_netlink_message_get_errno(m);
1009 if (r < 0)
1010 log_link_message_warning_errno(link, m, r, "Could not set MTU, ignoring");
1011 else
1012 log_link_debug(link, "Setting MTU done.");
1013
1014 if (link->state == LINK_STATE_INITIALIZED) {
1015 r = link_configure_continue(link);
1016 if (r < 0)
1017 link_enter_failed(link);
1018 }
1019
1020 return 1;
1021 }
1022
1023 int link_set_mtu(Link *link, uint32_t mtu) {
1024 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1025 int r;
1026
1027 assert(link);
1028 assert(link->manager);
1029 assert(link->manager->rtnl);
1030
1031 if (mtu == 0 || link->setting_mtu)
1032 return 0;
1033
1034 if (link->mtu == mtu)
1035 return 0;
1036
1037 log_link_debug(link, "Setting MTU: %" PRIu32, mtu);
1038
1039 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
1040 if (r < 0)
1041 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
1042
1043 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1044 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1045 if (link_ipv6_enabled(link) && mtu < IPV6_MIN_MTU) {
1046
1047 log_link_warning(link, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU) ", as "
1048 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU) " bytes");
1049
1050 mtu = IPV6_MIN_MTU;
1051 }
1052
1053 r = sd_netlink_message_append_u32(req, IFLA_MTU, mtu);
1054 if (r < 0)
1055 return log_link_error_errno(link, r, "Could not append MTU: %m");
1056
1057 r = netlink_call_async(link->manager->rtnl, NULL, req, set_mtu_handler,
1058 link_netlink_destroy_callback, link);
1059 if (r < 0)
1060 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
1061
1062 link_ref(link);
1063 link->setting_mtu = true;
1064
1065 return 0;
1066 }
1067
1068 static bool link_reduces_vlan_mtu(Link *link) {
1069 /* See netif_reduces_vlan_mtu() in kernel. */
1070 return streq_ptr(link->kind, "macsec");
1071 }
1072
1073 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link *link) {
1074 uint32_t mtu = 0;
1075 NetDev *dev;
1076
1077 HASHMAP_FOREACH(dev, link->network->stacked_netdevs)
1078 if (dev->kind == NETDEV_KIND_VLAN && dev->mtu > 0)
1079 /* See vlan_dev_change_mtu() in kernel. */
1080 mtu = MAX(mtu, link_reduces_vlan_mtu(link) ? dev->mtu + 4 : dev->mtu);
1081
1082 else if (dev->kind == NETDEV_KIND_MACVLAN && dev->mtu > mtu)
1083 /* See macvlan_change_mtu() in kernel. */
1084 mtu = dev->mtu;
1085
1086 return mtu;
1087 }
1088
1089 static int link_configure_mtu(Link *link) {
1090 uint32_t mtu;
1091
1092 assert(link);
1093 assert(link->network);
1094
1095 if (link->network->mtu > 0)
1096 return link_set_mtu(link, link->network->mtu);
1097
1098 mtu = link_get_requested_mtu_by_stacked_netdevs(link);
1099 if (link->mtu >= mtu)
1100 return 0;
1101
1102 log_link_notice(link, "Bumping MTU bytes from %"PRIu32" to %"PRIu32" because of stacked device. "
1103 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1104 link->mtu, mtu);
1105
1106 return link_set_mtu(link, mtu);
1107 }
1108
1109 static int set_flags_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1110 int r;
1111
1112 assert(m);
1113 assert(link);
1114 assert(link->ifname);
1115
1116 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1117 return 1;
1118
1119 r = sd_netlink_message_get_errno(m);
1120 if (r < 0)
1121 log_link_message_warning_errno(link, m, r, "Could not set link flags, ignoring");
1122
1123 return 1;
1124 }
1125
1126 static int link_set_flags(Link *link) {
1127 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1128 unsigned ifi_change = 0;
1129 unsigned ifi_flags = 0;
1130 int r;
1131
1132 assert(link);
1133 assert(link->manager);
1134 assert(link->manager->rtnl);
1135
1136 if (link->flags & IFF_LOOPBACK)
1137 return 0;
1138
1139 if (!link->network)
1140 return 0;
1141
1142 if (link->network->arp < 0 && link->network->multicast < 0 && link->network->allmulticast < 0)
1143 return 0;
1144
1145 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
1146 if (r < 0)
1147 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
1148
1149 if (link->network->arp >= 0) {
1150 ifi_change |= IFF_NOARP;
1151 SET_FLAG(ifi_flags, IFF_NOARP, link->network->arp == 0);
1152 }
1153
1154 if (link->network->multicast >= 0) {
1155 ifi_change |= IFF_MULTICAST;
1156 SET_FLAG(ifi_flags, IFF_MULTICAST, link->network->multicast);
1157 }
1158
1159 if (link->network->allmulticast >= 0) {
1160 ifi_change |= IFF_ALLMULTI;
1161 SET_FLAG(ifi_flags, IFF_ALLMULTI, link->network->allmulticast);
1162 }
1163
1164 r = sd_rtnl_message_link_set_flags(req, ifi_flags, ifi_change);
1165 if (r < 0)
1166 return log_link_error_errno(link, r, "Could not set link flags: %m");
1167
1168 r = netlink_call_async(link->manager->rtnl, NULL, req, set_flags_handler,
1169 link_netlink_destroy_callback, link);
1170 if (r < 0)
1171 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
1172
1173 link_ref(link);
1174
1175 return 0;
1176 }
1177
1178 static int link_acquire_ipv6_conf(Link *link) {
1179 int r;
1180
1181 assert(link);
1182
1183 if (link->ndisc) {
1184 log_link_debug(link, "Discovering IPv6 routers");
1185
1186 r = sd_ndisc_start(link->ndisc);
1187 if (r < 0 && r != -EBUSY)
1188 return log_link_warning_errno(link, r, "Could not start IPv6 Router Discovery: %m");
1189 }
1190
1191 if (link->radv) {
1192 assert(link->radv);
1193 assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0);
1194
1195 log_link_debug(link, "Starting IPv6 Router Advertisements");
1196
1197 r = radv_emit_dns(link);
1198 if (r < 0)
1199 return log_link_warning_errno(link, r, "Failed to configure DNS or Domains in IPv6 Router Advertisement: %m");
1200
1201 r = sd_radv_start(link->radv);
1202 if (r < 0 && r != -EBUSY)
1203 return log_link_warning_errno(link, r, "Could not start IPv6 Router Advertisement: %m");
1204 }
1205
1206 if (link_dhcp6_enabled(link) && IN_SET(link->network->dhcp6_without_ra,
1207 DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST,
1208 DHCP6_CLIENT_START_MODE_SOLICIT)) {
1209 assert(link->dhcp6_client);
1210 assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0);
1211
1212 r = dhcp6_request_address(link, link->network->dhcp6_without_ra == DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST);
1213 if (r < 0 && r != -EBUSY)
1214 return log_link_warning_errno(link, r, "Could not acquire DHCPv6 lease: %m");
1215 else
1216 log_link_debug(link, "Acquiring DHCPv6 lease");
1217 }
1218
1219 r = dhcp6_request_prefix_delegation(link);
1220 if (r < 0)
1221 return log_link_warning_errno(link, r, "Failed to request DHCPv6 prefix delegation: %m");
1222
1223 return 0;
1224 }
1225
1226 static int link_acquire_ipv4_conf(Link *link) {
1227 int r;
1228
1229 assert(link);
1230 assert(link->manager);
1231 assert(link->manager->event);
1232
1233 if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_IPV4)) {
1234 assert(link->ipv4ll);
1235
1236 log_link_debug(link, "Acquiring IPv4 link-local address");
1237
1238 r = sd_ipv4ll_start(link->ipv4ll);
1239 if (r < 0)
1240 return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
1241 }
1242
1243 if (link->dhcp_client) {
1244 log_link_debug(link, "Acquiring DHCPv4 lease");
1245
1246 r = sd_dhcp_client_start(link->dhcp_client);
1247 if (r < 0)
1248 return log_link_warning_errno(link, r, "Could not acquire DHCPv4 lease: %m");
1249 }
1250
1251 return 0;
1252 }
1253
1254 static int link_acquire_conf(Link *link) {
1255 int r;
1256
1257 assert(link);
1258
1259 r = link_acquire_ipv4_conf(link);
1260 if (r < 0)
1261 return r;
1262
1263 if (!in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address)) {
1264 r = link_acquire_ipv6_conf(link);
1265 if (r < 0)
1266 return r;
1267 }
1268
1269 r = link_lldp_emit_start(link);
1270 if (r < 0)
1271 return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m");
1272
1273 return 0;
1274 }
1275
1276 bool link_has_carrier(Link *link) {
1277 /* see Documentation/networking/operstates.txt in the kernel sources */
1278
1279 if (link->kernel_operstate == IF_OPER_UP)
1280 return true;
1281
1282 if (link->kernel_operstate == IF_OPER_UNKNOWN)
1283 /* operstate may not be implemented, so fall back to flags */
1284 if (FLAGS_SET(link->flags, IFF_LOWER_UP | IFF_RUNNING) &&
1285 !FLAGS_SET(link->flags, IFF_DORMANT))
1286 return true;
1287
1288 return false;
1289 }
1290
1291 static int link_address_genmode_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1292 int r;
1293
1294 assert(link);
1295
1296 link->setting_genmode = false;
1297
1298 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1299 return 1;
1300
1301 r = sd_netlink_message_get_errno(m);
1302 if (r < 0)
1303 log_link_message_warning_errno(link, m, r, "Could not set address genmode for interface, ignoring");
1304 else
1305 log_link_debug(link, "Setting address genmode done.");
1306
1307 if (link->state == LINK_STATE_INITIALIZED) {
1308 r = link_configure_continue(link);
1309 if (r < 0)
1310 link_enter_failed(link);
1311 }
1312
1313 return 1;
1314 }
1315
1316 static int link_configure_addrgen_mode(Link *link) {
1317 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1318 uint8_t ipv6ll_mode;
1319 int r;
1320
1321 assert(link);
1322 assert(link->network);
1323 assert(link->manager);
1324 assert(link->manager->rtnl);
1325
1326 if (!socket_ipv6_is_supported() || link->setting_genmode)
1327 return 0;
1328
1329 log_link_debug(link, "Setting address genmode for link");
1330
1331 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
1332 if (r < 0)
1333 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
1334
1335 r = sd_netlink_message_open_container(req, IFLA_AF_SPEC);
1336 if (r < 0)
1337 return log_link_error_errno(link, r, "Could not open IFLA_AF_SPEC container: %m");
1338
1339 r = sd_netlink_message_open_container(req, AF_INET6);
1340 if (r < 0)
1341 return log_link_error_errno(link, r, "Could not open AF_INET6 container: %m");
1342
1343 if (!link_ipv6ll_enabled(link))
1344 ipv6ll_mode = IN6_ADDR_GEN_MODE_NONE;
1345 else if (link->network->ipv6ll_address_gen_mode < 0) {
1346 r = sysctl_read_ip_property(AF_INET6, link->ifname, "stable_secret", NULL);
1347 if (r < 0) {
1348 /* The file may not exist. And even if it exists, when stable_secret is unset,
1349 * reading the file fails with EIO. */
1350 log_link_debug_errno(link, r, "Failed to read sysctl property stable_secret: %m");
1351
1352 ipv6ll_mode = IN6_ADDR_GEN_MODE_EUI64;
1353 } else
1354 ipv6ll_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
1355 } else
1356 ipv6ll_mode = link->network->ipv6ll_address_gen_mode;
1357
1358 r = sd_netlink_message_append_u8(req, IFLA_INET6_ADDR_GEN_MODE, ipv6ll_mode);
1359 if (r < 0)
1360 return log_link_error_errno(link, r, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1361
1362 r = sd_netlink_message_close_container(req);
1363 if (r < 0)
1364 return log_link_error_errno(link, r, "Could not close AF_INET6 container: %m");
1365
1366 r = sd_netlink_message_close_container(req);
1367 if (r < 0)
1368 return log_link_error_errno(link, r, "Could not close IFLA_AF_SPEC container: %m");
1369
1370 r = netlink_call_async(link->manager->rtnl, NULL, req, link_address_genmode_handler,
1371 link_netlink_destroy_callback, link);
1372 if (r < 0)
1373 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
1374
1375 link_ref(link);
1376 link->setting_genmode = true;
1377
1378 return 0;
1379 }
1380
1381 static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1382 int r;
1383
1384 assert(link);
1385
1386 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1387 return 1;
1388
1389 r = sd_netlink_message_get_errno(m);
1390 if (r < 0)
1391 /* we warn but don't fail the link, as it may be brought up later */
1392 log_link_message_warning_errno(link, m, r, "Could not bring up interface");
1393
1394 return 1;
1395 }
1396
1397 static int link_up(Link *link) {
1398 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1399 int r;
1400
1401 assert(link);
1402 assert(link->network);
1403 assert(link->manager);
1404 assert(link->manager->rtnl);
1405
1406 log_link_debug(link, "Bringing link up");
1407
1408 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
1409 if (r < 0)
1410 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
1411
1412 r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
1413 if (r < 0)
1414 return log_link_error_errno(link, r, "Could not set link flags: %m");
1415
1416 r = netlink_call_async(link->manager->rtnl, NULL, req, link_up_handler,
1417 link_netlink_destroy_callback, link);
1418 if (r < 0)
1419 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
1420
1421 link_ref(link);
1422
1423 return 0;
1424 }
1425
1426 static int link_down_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1427 int r;
1428
1429 assert(link);
1430
1431 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1432 return 1;
1433
1434 r = sd_netlink_message_get_errno(m);
1435 if (r < 0)
1436 log_link_message_warning_errno(link, m, r, "Could not bring down interface");
1437
1438 return 1;
1439 }
1440
1441 int link_down(Link *link, link_netlink_message_handler_t callback) {
1442 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1443 int r;
1444
1445 assert(link);
1446 assert(link->manager);
1447 assert(link->manager->rtnl);
1448
1449 log_link_debug(link, "Bringing link down");
1450
1451 r = sd_rtnl_message_new_link(link->manager->rtnl, &req,
1452 RTM_SETLINK, link->ifindex);
1453 if (r < 0)
1454 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
1455
1456 r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP);
1457 if (r < 0)
1458 return log_link_error_errno(link, r, "Could not set link flags: %m");
1459
1460 r = netlink_call_async(link->manager->rtnl, NULL, req,
1461 callback ?: link_down_handler,
1462 link_netlink_destroy_callback, link);
1463 if (r < 0)
1464 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
1465
1466 link_ref(link);
1467
1468 return 0;
1469 }
1470
1471 static int link_group_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1472 int r;
1473
1474 assert(link);
1475
1476 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1477 return 1;
1478
1479 r = sd_netlink_message_get_errno(m);
1480 if (r < 0)
1481 log_link_message_warning_errno(link, m, r, "Could not set group for the interface");
1482
1483 return 1;
1484 }
1485
1486 static int link_set_group(Link *link) {
1487 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1488 int r;
1489
1490 assert(link);
1491 assert(link->network);
1492 assert(link->manager);
1493 assert(link->manager->rtnl);
1494
1495 if (link->network->group <= 0)
1496 return 0;
1497
1498 log_link_debug(link, "Setting group");
1499
1500 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
1501 if (r < 0)
1502 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
1503
1504 r = sd_netlink_message_append_u32(req, IFLA_GROUP, link->network->group);
1505 if (r < 0)
1506 return log_link_error_errno(link, r, "Could not set link group: %m");
1507
1508 r = netlink_call_async(link->manager->rtnl, NULL, req, link_group_handler,
1509 link_netlink_destroy_callback, link);
1510 if (r < 0)
1511 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
1512
1513 link_ref(link);
1514
1515 return 0;
1516 }
1517
1518 static int link_handle_bound_to_list(Link *link) {
1519 Link *l;
1520 int r;
1521 bool required_up = false;
1522 bool link_is_up = false;
1523
1524 assert(link);
1525
1526 if (hashmap_isempty(link->bound_to_links))
1527 return 0;
1528
1529 if (link->flags & IFF_UP)
1530 link_is_up = true;
1531
1532 HASHMAP_FOREACH (l, link->bound_to_links)
1533 if (link_has_carrier(l)) {
1534 required_up = true;
1535 break;
1536 }
1537
1538 if (!required_up && link_is_up) {
1539 r = link_down(link, NULL);
1540 if (r < 0)
1541 return r;
1542 } else if (required_up && !link_is_up) {
1543 r = link_up(link);
1544 if (r < 0)
1545 return r;
1546 }
1547
1548 return 0;
1549 }
1550
1551 static int link_handle_bound_by_list(Link *link) {
1552 Link *l;
1553 int r;
1554
1555 assert(link);
1556
1557 if (hashmap_isempty(link->bound_by_links))
1558 return 0;
1559
1560 HASHMAP_FOREACH (l, link->bound_by_links) {
1561 r = link_handle_bound_to_list(l);
1562 if (r < 0)
1563 return r;
1564 }
1565
1566 return 0;
1567 }
1568
1569 static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) {
1570 int r;
1571
1572 assert(link);
1573 assert(carrier);
1574
1575 if (link == carrier)
1576 return 0;
1577
1578 if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex)))
1579 return 0;
1580
1581 r = hashmap_ensure_allocated(h, NULL);
1582 if (r < 0)
1583 return r;
1584
1585 r = hashmap_put(*h, INT_TO_PTR(carrier->ifindex), carrier);
1586 if (r < 0)
1587 return r;
1588
1589 return 0;
1590 }
1591
1592 static int link_new_bound_by_list(Link *link) {
1593 Manager *m;
1594 Link *carrier;
1595 int r;
1596 bool list_updated = false;
1597
1598 assert(link);
1599 assert(link->manager);
1600
1601 m = link->manager;
1602
1603 HASHMAP_FOREACH(carrier, m->links) {
1604 if (!carrier->network)
1605 continue;
1606
1607 if (strv_isempty(carrier->network->bind_carrier))
1608 continue;
1609
1610 if (strv_fnmatch(carrier->network->bind_carrier, link->ifname)) {
1611 r = link_put_carrier(link, carrier, &link->bound_by_links);
1612 if (r < 0)
1613 return r;
1614
1615 list_updated = true;
1616 }
1617 }
1618
1619 if (list_updated)
1620 link_dirty(link);
1621
1622 HASHMAP_FOREACH(carrier, link->bound_by_links) {
1623 r = link_put_carrier(carrier, link, &carrier->bound_to_links);
1624 if (r < 0)
1625 return r;
1626
1627 link_dirty(carrier);
1628 }
1629
1630 return 0;
1631 }
1632
1633 static int link_new_bound_to_list(Link *link) {
1634 Manager *m;
1635 Link *carrier;
1636 int r;
1637 bool list_updated = false;
1638
1639 assert(link);
1640 assert(link->manager);
1641
1642 if (!link->network)
1643 return 0;
1644
1645 if (strv_isempty(link->network->bind_carrier))
1646 return 0;
1647
1648 m = link->manager;
1649
1650 HASHMAP_FOREACH (carrier, m->links) {
1651 if (strv_fnmatch(link->network->bind_carrier, carrier->ifname)) {
1652 r = link_put_carrier(link, carrier, &link->bound_to_links);
1653 if (r < 0)
1654 return r;
1655
1656 list_updated = true;
1657 }
1658 }
1659
1660 if (list_updated)
1661 link_dirty(link);
1662
1663 HASHMAP_FOREACH (carrier, link->bound_to_links) {
1664 r = link_put_carrier(carrier, link, &carrier->bound_by_links);
1665 if (r < 0)
1666 return r;
1667
1668 link_dirty(carrier);
1669 }
1670
1671 return 0;
1672 }
1673
1674 static int link_new_carrier_maps(Link *link) {
1675 int r;
1676
1677 r = link_new_bound_by_list(link);
1678 if (r < 0)
1679 return r;
1680
1681 r = link_handle_bound_by_list(link);
1682 if (r < 0)
1683 return r;
1684
1685 r = link_new_bound_to_list(link);
1686 if (r < 0)
1687 return r;
1688
1689 r = link_handle_bound_to_list(link);
1690 if (r < 0)
1691 return r;
1692
1693 return 0;
1694 }
1695
1696 static void link_free_bound_to_list(Link *link) {
1697 Link *bound_to;
1698
1699 HASHMAP_FOREACH (bound_to, link->bound_to_links) {
1700 hashmap_remove(link->bound_to_links, INT_TO_PTR(bound_to->ifindex));
1701
1702 if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex)))
1703 link_dirty(bound_to);
1704 }
1705
1706 return;
1707 }
1708
1709 static void link_free_bound_by_list(Link *link) {
1710 Link *bound_by;
1711
1712 HASHMAP_FOREACH (bound_by, link->bound_by_links) {
1713 hashmap_remove(link->bound_by_links, INT_TO_PTR(bound_by->ifindex));
1714
1715 if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) {
1716 link_dirty(bound_by);
1717 link_handle_bound_to_list(bound_by);
1718 }
1719 }
1720
1721 return;
1722 }
1723
1724 static void link_free_carrier_maps(Link *link) {
1725 bool list_updated = false;
1726
1727 assert(link);
1728
1729 if (!hashmap_isempty(link->bound_to_links)) {
1730 link_free_bound_to_list(link);
1731 list_updated = true;
1732 }
1733
1734 if (!hashmap_isempty(link->bound_by_links)) {
1735 link_free_bound_by_list(link);
1736 list_updated = true;
1737 }
1738
1739 if (list_updated)
1740 link_dirty(link);
1741
1742 return;
1743 }
1744
1745 static int link_append_to_master(Link *link, NetDev *netdev) {
1746 Link *master;
1747 int r;
1748
1749 assert(link);
1750 assert(netdev);
1751
1752 r = link_get(link->manager, netdev->ifindex, &master);
1753 if (r < 0)
1754 return r;
1755
1756 r = set_ensure_put(&master->slaves, NULL, link);
1757 if (r <= 0)
1758 return r;
1759
1760 link_ref(link);
1761 return 0;
1762 }
1763
1764 static void link_drop_from_master(Link *link, NetDev *netdev) {
1765 Link *master;
1766
1767 assert(link);
1768
1769 if (!link->manager || !netdev)
1770 return;
1771
1772 if (link_get(link->manager, netdev->ifindex, &master) < 0)
1773 return;
1774
1775 link_unref(set_remove(master->slaves, link));
1776 }
1777
1778 static void link_detach_from_manager(Link *link) {
1779 if (!link || !link->manager)
1780 return;
1781
1782 link_unref(set_remove(link->manager->links_requesting_uuid, link));
1783 link_clean(link);
1784
1785 /* The following must be called at last. */
1786 assert_se(hashmap_remove(link->manager->links, INT_TO_PTR(link->ifindex)) == link);
1787 link_unref(link);
1788 }
1789
1790 void link_drop(Link *link) {
1791 if (!link || link->state == LINK_STATE_LINGER)
1792 return;
1793
1794 link_set_state(link, LINK_STATE_LINGER);
1795
1796 link_free_carrier_maps(link);
1797
1798 if (link->network) {
1799 link_drop_from_master(link, link->network->bridge);
1800 link_drop_from_master(link, link->network->bond);
1801 }
1802
1803 log_link_debug(link, "Link removed");
1804
1805 (void) unlink(link->state_file);
1806 link_detach_from_manager(link);
1807 }
1808
1809 static int link_joined(Link *link) {
1810 int r;
1811
1812 assert(link);
1813 assert(link->network);
1814
1815 if (!hashmap_isempty(link->bound_to_links)) {
1816 r = link_handle_bound_to_list(link);
1817 if (r < 0)
1818 return r;
1819 } else if (!(link->flags & IFF_UP)) {
1820 r = link_up(link);
1821 if (r < 0) {
1822 link_enter_failed(link);
1823 return r;
1824 }
1825 }
1826
1827 if (link->network->bridge) {
1828 r = link_set_bridge(link);
1829 if (r < 0)
1830 log_link_error_errno(link, r, "Could not set bridge message: %m");
1831
1832 r = link_append_to_master(link, link->network->bridge);
1833 if (r < 0)
1834 log_link_error_errno(link, r, "Failed to add to bridge master's slave list: %m");
1835 }
1836
1837 if (link->network->bond) {
1838 r = link_set_bond(link);
1839 if (r < 0)
1840 log_link_error_errno(link, r, "Could not set bond message: %m");
1841
1842 r = link_append_to_master(link, link->network->bond);
1843 if (r < 0)
1844 log_link_error_errno(link, r, "Failed to add to bond master's slave list: %m");
1845 }
1846
1847 r = link_set_bridge_vlan(link);
1848 if (r < 0)
1849 log_link_error_errno(link, r, "Could not set bridge vlan: %m");
1850
1851 /* Skip setting up addresses until it gets carrier,
1852 or it would try to set addresses twice,
1853 which is bad for non-idempotent steps. */
1854 if (!link_has_carrier(link) && !link->network->configure_without_carrier)
1855 return 0;
1856
1857 link_set_state(link, LINK_STATE_CONFIGURING);
1858
1859 r = link_acquire_conf(link);
1860 if (r < 0)
1861 return r;
1862
1863 return link_set_static_configs(link);
1864 }
1865
1866 static int netdev_join_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1867 int r;
1868
1869 assert(link);
1870 assert(link->network);
1871 assert(link->enslaving > 0);
1872
1873 link->enslaving--;
1874
1875 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1876 return 1;
1877
1878 r = sd_netlink_message_get_errno(m);
1879 if (r < 0 && r != -EEXIST) {
1880 log_link_message_warning_errno(link, m, r, "Could not join netdev");
1881 link_enter_failed(link);
1882 return 1;
1883 }
1884
1885 log_link_debug(link, "Joined netdev");
1886
1887 if (link->enslaving == 0) {
1888 r = link_joined(link);
1889 if (r < 0)
1890 link_enter_failed(link);
1891 }
1892
1893 return 1;
1894 }
1895
1896 static int link_enter_join_netdev(Link *link) {
1897 NetDev *netdev;
1898 int r;
1899
1900 assert(link);
1901 assert(link->network);
1902 assert(link->state == LINK_STATE_INITIALIZED);
1903
1904 link_set_state(link, LINK_STATE_CONFIGURING);
1905
1906 link_dirty(link);
1907 link->enslaving = 0;
1908
1909 if (link->network->bond) {
1910 if (link->network->bond->state == NETDEV_STATE_READY &&
1911 link->network->bond->ifindex == link->master_ifindex)
1912 return link_joined(link);
1913
1914 log_struct(LOG_DEBUG,
1915 LOG_LINK_INTERFACE(link),
1916 LOG_NETDEV_INTERFACE(link->network->bond),
1917 LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bond->ifname));
1918
1919 link->enslaving++;
1920
1921 r = netdev_join(link->network->bond, link, netdev_join_handler);
1922 if (r < 0) {
1923 log_struct_errno(LOG_WARNING, r,
1924 LOG_LINK_INTERFACE(link),
1925 LOG_NETDEV_INTERFACE(link->network->bond),
1926 LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bond->ifname));
1927 link_enter_failed(link);
1928 return r;
1929 }
1930 }
1931
1932 if (link->network->bridge) {
1933 log_struct(LOG_DEBUG,
1934 LOG_LINK_INTERFACE(link),
1935 LOG_NETDEV_INTERFACE(link->network->bridge),
1936 LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bridge->ifname));
1937
1938 link->enslaving++;
1939
1940 r = netdev_join(link->network->bridge, link, netdev_join_handler);
1941 if (r < 0) {
1942 log_struct_errno(LOG_WARNING, r,
1943 LOG_LINK_INTERFACE(link),
1944 LOG_NETDEV_INTERFACE(link->network->bridge),
1945 LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bridge->ifname));
1946 link_enter_failed(link);
1947 return r;
1948 }
1949 }
1950
1951 if (link->network->vrf) {
1952 log_struct(LOG_DEBUG,
1953 LOG_LINK_INTERFACE(link),
1954 LOG_NETDEV_INTERFACE(link->network->vrf),
1955 LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->vrf->ifname));
1956
1957 link->enslaving++;
1958
1959 r = netdev_join(link->network->vrf, link, netdev_join_handler);
1960 if (r < 0) {
1961 log_struct_errno(LOG_WARNING, r,
1962 LOG_LINK_INTERFACE(link),
1963 LOG_NETDEV_INTERFACE(link->network->vrf),
1964 LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->vrf->ifname));
1965 link_enter_failed(link);
1966 return r;
1967 }
1968 }
1969
1970 HASHMAP_FOREACH(netdev, link->network->stacked_netdevs) {
1971
1972 if (netdev->ifindex > 0)
1973 /* Assume already enslaved. */
1974 continue;
1975
1976 if (netdev_get_create_type(netdev) != NETDEV_CREATE_STACKED)
1977 continue;
1978
1979 log_struct(LOG_DEBUG,
1980 LOG_LINK_INTERFACE(link),
1981 LOG_NETDEV_INTERFACE(netdev),
1982 LOG_LINK_MESSAGE(link, "Enslaving by '%s'", netdev->ifname));
1983
1984 link->enslaving++;
1985
1986 r = netdev_join(netdev, link, netdev_join_handler);
1987 if (r < 0) {
1988 log_struct_errno(LOG_WARNING, r,
1989 LOG_LINK_INTERFACE(link),
1990 LOG_NETDEV_INTERFACE(netdev),
1991 LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", netdev->ifname));
1992 link_enter_failed(link);
1993 return r;
1994 }
1995 }
1996
1997 if (link->enslaving == 0)
1998 return link_joined(link);
1999
2000 return 0;
2001 }
2002
2003 static int link_drop_foreign_config(Link *link) {
2004 int r;
2005
2006 r = link_drop_foreign_addresses(link);
2007 if (r < 0)
2008 return r;
2009
2010 r = link_drop_foreign_neighbors(link);
2011 if (r < 0)
2012 return r;
2013
2014 return link_drop_foreign_routes(link);
2015 }
2016
2017 static int link_drop_config(Link *link) {
2018 int r;
2019
2020 r = link_drop_addresses(link);
2021 if (r < 0)
2022 return r;
2023
2024 r = link_drop_neighbors(link);
2025 if (r < 0)
2026 return r;
2027
2028 r = link_drop_routes(link);
2029 if (r < 0)
2030 return r;
2031
2032 ndisc_flush(link);
2033
2034 return 0;
2035 }
2036
2037 int link_configure(Link *link) {
2038 int r;
2039
2040 assert(link);
2041 assert(link->network);
2042 assert(link->state == LINK_STATE_INITIALIZED);
2043
2044 r = link_configure_traffic_control(link);
2045 if (r < 0)
2046 return r;
2047
2048 r = link_configure_sr_iov(link);
2049 if (r < 0)
2050 return r;
2051
2052 if (link->iftype == ARPHRD_CAN)
2053 return link_configure_can(link);
2054
2055 r = link_set_sysctl(link);
2056 if (r < 0)
2057 return r;
2058
2059 r = link_set_ipv6_proxy_ndp_addresses(link);
2060 if (r < 0)
2061 return r;
2062
2063 r = link_set_mac(link);
2064 if (r < 0)
2065 return r;
2066
2067 r = link_set_nomaster(link);
2068 if (r < 0)
2069 return r;
2070
2071 r = link_set_flags(link);
2072 if (r < 0)
2073 return r;
2074
2075 r = link_set_group(link);
2076 if (r < 0)
2077 return r;
2078
2079 r = ipv4ll_configure(link);
2080 if (r < 0)
2081 return r;
2082
2083 r = dhcp4_configure(link);
2084 if (r < 0)
2085 return r;
2086
2087 r = dhcp6_configure(link);
2088 if (r < 0)
2089 return r;
2090
2091 r = ndisc_configure(link);
2092 if (r < 0)
2093 return r;
2094
2095 r = radv_configure(link);
2096 if (r < 0)
2097 return r;
2098
2099 r = link_lldp_rx_configure(link);
2100 if (r < 0)
2101 return r;
2102
2103 r = link_configure_mtu(link);
2104 if (r < 0)
2105 return r;
2106
2107 r = link_configure_addrgen_mode(link);
2108 if (r < 0)
2109 return r;
2110
2111 return link_configure_continue(link);
2112 }
2113
2114 /* The configuration continues in this separate function, instead of
2115 * including this in the above link_configure() function, for two
2116 * reasons:
2117 * 1) some devices reset the link when the mtu is set, which caused
2118 * an infinite loop here in networkd; see:
2119 * https://github.com/systemd/systemd/issues/6593
2120 * https://github.com/systemd/systemd/issues/9831
2121 * 2) if ipv6ll is disabled, then bringing the interface up must be
2122 * delayed until after we get confirmation from the kernel that
2123 * the addr_gen_mode parameter has been set (via netlink), see:
2124 * https://github.com/systemd/systemd/issues/13882
2125 */
2126 static int link_configure_continue(Link *link) {
2127 int r;
2128
2129 assert(link);
2130 assert(link->network);
2131 assert(link->state == LINK_STATE_INITIALIZED);
2132
2133 if (link->setting_mtu || link->setting_genmode)
2134 return 0;
2135
2136 /* Drop foreign config, but ignore loopback or critical devices.
2137 * We do not want to remove loopback address or addresses used for root NFS. */
2138 if (!(link->flags & IFF_LOOPBACK) &&
2139 link->network->keep_configuration != KEEP_CONFIGURATION_YES) {
2140 r = link_drop_foreign_config(link);
2141 if (r < 0)
2142 return r;
2143 }
2144
2145 /* The kernel resets ipv6 mtu after changing device mtu;
2146 * we must set this here, after we've set device mtu */
2147 r = link_set_ipv6_mtu(link);
2148 if (r < 0)
2149 log_link_warning_errno(link, r, "Cannot set IPv6 MTU for interface, ignoring: %m");
2150
2151 return link_enter_join_netdev(link);
2152 }
2153
2154 static int link_reconfigure_internal(Link *link, sd_netlink_message *m, bool force) {
2155 Network *network;
2156 int r;
2157
2158 if (m) {
2159 _cleanup_strv_free_ char **s = NULL;
2160
2161 r = sd_netlink_message_get_errno(m);
2162 if (r < 0)
2163 return r;
2164
2165 r = sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &s);
2166 if (r < 0 && r != -ENODATA)
2167 return r;
2168
2169 strv_free_and_replace(link->alternative_names, s);
2170 }
2171
2172 r = network_get(link->manager, link->iftype, link->sd_device,
2173 link->ifname, link->alternative_names, link->driver,
2174 &link->hw_addr.addr.ether, &link->permanent_mac,
2175 link->wlan_iftype, link->ssid, &link->bssid, &network);
2176 if (r == -ENOENT) {
2177 link_enter_unmanaged(link);
2178 return 0;
2179 } else if (r == 0 && network->unmanaged) {
2180 link_enter_unmanaged(link);
2181 return 0;
2182 } else if (r < 0)
2183 return r;
2184
2185 if (link->network == network && !force)
2186 return 0;
2187
2188 log_link_info(link, "Re-configuring with %s", network->filename);
2189
2190 /* Dropping old .network file */
2191 r = link_stop_engines(link, false);
2192 if (r < 0)
2193 return r;
2194
2195 r = link_drop_config(link);
2196 if (r < 0)
2197 return r;
2198
2199 if (!IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING, LINK_STATE_INITIALIZED)) {
2200 log_link_debug(link, "State is %s, dropping config", link_state_to_string(link->state));
2201 r = link_drop_foreign_config(link);
2202 if (r < 0)
2203 return r;
2204 }
2205
2206 link_free_carrier_maps(link);
2207 link_free_engines(link);
2208 link->network = network_unref(link->network);
2209
2210 /* Then, apply new .network file */
2211 r = network_apply(network, link);
2212 if (r < 0)
2213 return r;
2214
2215 r = link_new_carrier_maps(link);
2216 if (r < 0)
2217 return r;
2218
2219 link_set_state(link, LINK_STATE_INITIALIZED);
2220 link_dirty(link);
2221
2222 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2223 * link_configure() is called later asynchronously. */
2224 r = link_configure_duid(link);
2225 if (r <= 0)
2226 return r;
2227
2228 r = link_configure(link);
2229 if (r < 0)
2230 return r;
2231
2232 return 0;
2233 }
2234
2235 static int link_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
2236 int r;
2237
2238 r = link_reconfigure_internal(link, m, false);
2239 if (r < 0)
2240 link_enter_failed(link);
2241
2242 return 1;
2243 }
2244
2245 static int link_force_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
2246 int r;
2247
2248 r = link_reconfigure_internal(link, m, true);
2249 if (r < 0)
2250 link_enter_failed(link);
2251
2252 return 1;
2253 }
2254
2255 int link_reconfigure(Link *link, bool force) {
2256 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
2257 int r;
2258
2259 if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_LINGER))
2260 return 0;
2261
2262 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK,
2263 link->ifindex);
2264 if (r < 0)
2265 return r;
2266
2267 r = netlink_call_async(link->manager->rtnl, NULL, req,
2268 force ? link_force_reconfigure_handler : link_reconfigure_handler,
2269 link_netlink_destroy_callback, link);
2270 if (r < 0)
2271 return r;
2272
2273 link_ref(link);
2274
2275 return 0;
2276 }
2277
2278 static int link_initialized_and_synced(Link *link) {
2279 Network *network;
2280 int r;
2281
2282 assert(link);
2283 assert(link->ifname);
2284 assert(link->manager);
2285
2286 /* We may get called either from the asynchronous netlink callback,
2287 * or directly for link_add() if running in a container. See link_add(). */
2288 if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED))
2289 return 0;
2290
2291 log_link_debug(link, "Link state is up-to-date");
2292 link_set_state(link, LINK_STATE_INITIALIZED);
2293
2294 r = link_new_bound_by_list(link);
2295 if (r < 0)
2296 return r;
2297
2298 r = link_handle_bound_by_list(link);
2299 if (r < 0)
2300 return r;
2301
2302 if (!link->network) {
2303 r = wifi_get_info(link);
2304 if (r < 0)
2305 return r;
2306
2307 r = network_get(link->manager, link->iftype, link->sd_device,
2308 link->ifname, link->alternative_names, link->driver,
2309 &link->hw_addr.addr.ether, &link->permanent_mac,
2310 link->wlan_iftype, link->ssid, &link->bssid, &network);
2311 if (r == -ENOENT) {
2312 link_enter_unmanaged(link);
2313 return 0;
2314 } else if (r == 0 && network->unmanaged) {
2315 link_enter_unmanaged(link);
2316 return 0;
2317 } else if (r < 0)
2318 return r;
2319
2320 if (link->flags & IFF_LOOPBACK) {
2321 if (network->link_local != ADDRESS_FAMILY_NO)
2322 log_link_debug(link, "Ignoring link-local autoconfiguration for loopback link");
2323
2324 if (network->dhcp != ADDRESS_FAMILY_NO)
2325 log_link_debug(link, "Ignoring DHCP clients for loopback link");
2326
2327 if (network->dhcp_server)
2328 log_link_debug(link, "Ignoring DHCP server for loopback link");
2329 }
2330
2331 r = network_apply(network, link);
2332 if (r < 0)
2333 return r;
2334 }
2335
2336 r = link_new_bound_to_list(link);
2337 if (r < 0)
2338 return r;
2339
2340 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2341 * link_configure() is called later asynchronously. */
2342 r = link_configure_duid(link);
2343 if (r <= 0)
2344 return r;
2345
2346 r = link_configure(link);
2347 if (r < 0)
2348 return r;
2349
2350 return 0;
2351 }
2352
2353 static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
2354 _cleanup_strv_free_ char **s = NULL;
2355 int r;
2356
2357 r = sd_netlink_message_get_errno(m);
2358 if (r < 0) {
2359 log_link_warning_errno(link, r, "Failed to wait for the interface to be initialized: %m");
2360 link_enter_failed(link);
2361 return 0;
2362 }
2363
2364 r = sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &s);
2365 if (r < 0 && r != -ENODATA) {
2366 link_enter_failed(link);
2367 return 0;
2368 }
2369
2370 strv_free_and_replace(link->alternative_names, s);
2371
2372 r = link_initialized_and_synced(link);
2373 if (r < 0)
2374 link_enter_failed(link);
2375 return 1;
2376 }
2377
2378 int link_initialized(Link *link, sd_device *device) {
2379 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
2380 int r;
2381
2382 assert(link);
2383 assert(link->manager);
2384 assert(link->manager->rtnl);
2385 assert(device);
2386
2387 if (link->state != LINK_STATE_PENDING)
2388 return 0;
2389
2390 if (link->sd_device)
2391 return 0;
2392
2393 log_link_debug(link, "udev initialized link");
2394 link_set_state(link, LINK_STATE_INITIALIZED);
2395
2396 link->sd_device = sd_device_ref(device);
2397
2398 /* udev has initialized the link, but we don't know if we have yet
2399 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2400 * when it returns we know that the pending NEWLINKs have already been
2401 * processed and that we are up-to-date */
2402
2403 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK,
2404 link->ifindex);
2405 if (r < 0)
2406 return r;
2407
2408 r = netlink_call_async(link->manager->rtnl, NULL, req, link_initialized_handler,
2409 link_netlink_destroy_callback, link);
2410 if (r < 0)
2411 return r;
2412
2413 link_ref(link);
2414
2415 return 0;
2416 }
2417
2418 int link_add(Manager *m, sd_netlink_message *message, Link **ret) {
2419 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
2420 char ifindex_str[2 + DECIMAL_STR_MAX(int)];
2421 Link *link;
2422 int r;
2423
2424 assert(m);
2425 assert(m->rtnl);
2426 assert(message);
2427 assert(ret);
2428
2429 r = link_new(m, message, ret);
2430 if (r < 0)
2431 return r;
2432
2433 link = *ret;
2434
2435 log_link_debug(link, "Link %d added", link->ifindex);
2436
2437 if (path_is_read_only_fs("/sys") <= 0) {
2438 /* udev should be around */
2439 sprintf(ifindex_str, "n%d", link->ifindex);
2440 r = sd_device_new_from_device_id(&device, ifindex_str);
2441 if (r < 0) {
2442 log_link_warning_errno(link, r, "Could not find device, waiting for device initialization: %m");
2443 return 0;
2444 }
2445
2446 r = sd_device_get_is_initialized(device);
2447 if (r < 0) {
2448 log_link_warning_errno(link, r, "Could not determine whether the device is initialized: %m");
2449 goto failed;
2450 }
2451 if (r == 0) {
2452 /* not yet ready */
2453 log_link_debug(link, "link pending udev initialization...");
2454 return 0;
2455 }
2456
2457 r = device_is_renaming(device);
2458 if (r < 0) {
2459 log_link_warning_errno(link, r, "Failed to determine the device is being renamed: %m");
2460 goto failed;
2461 }
2462 if (r > 0) {
2463 log_link_debug(link, "Interface is being renamed, pending initialization.");
2464 return 0;
2465 }
2466
2467 r = link_initialized(link, device);
2468 if (r < 0)
2469 goto failed;
2470 } else {
2471 r = link_initialized_and_synced(link);
2472 if (r < 0)
2473 goto failed;
2474 }
2475
2476 return 0;
2477 failed:
2478 link_enter_failed(link);
2479 return r;
2480 }
2481
2482 int link_ipv6ll_gained(Link *link, const struct in6_addr *address) {
2483 int r;
2484
2485 assert(link);
2486
2487 log_link_info(link, "Gained IPv6LL");
2488
2489 link->ipv6ll_address = *address;
2490 link_check_ready(link);
2491
2492 if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) {
2493 r = link_acquire_ipv6_conf(link);
2494 if (r < 0) {
2495 link_enter_failed(link);
2496 return r;
2497 }
2498 }
2499
2500 return 0;
2501 }
2502
2503 static int link_carrier_gained(Link *link) {
2504 int r;
2505
2506 assert(link);
2507
2508 r = wifi_get_info(link);
2509 if (r < 0)
2510 return r;
2511 if (r > 0) {
2512 r = link_reconfigure_internal(link, NULL, false);
2513 if (r < 0) {
2514 link_enter_failed(link);
2515 return r;
2516 }
2517 }
2518
2519 if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) {
2520 r = link_acquire_conf(link);
2521 if (r < 0) {
2522 link_enter_failed(link);
2523 return r;
2524 }
2525
2526 link_set_state(link, LINK_STATE_CONFIGURING);
2527 r = link_set_static_configs(link);
2528 if (r < 0)
2529 return r;
2530 }
2531
2532 r = link_handle_bound_by_list(link);
2533 if (r < 0)
2534 return r;
2535
2536 if (!link->bridge_mdb_configured) {
2537 r = link_set_bridge_mdb(link);
2538 if (r < 0)
2539 return r;
2540 }
2541
2542 if (streq_ptr(link->kind, "bridge")) {
2543 Link *slave;
2544
2545 SET_FOREACH(slave, link->slaves) {
2546 if (slave->bridge_mdb_configured)
2547 continue;
2548
2549 r = link_set_bridge_mdb(slave);
2550 if (r < 0)
2551 link_enter_failed(slave);
2552 }
2553 }
2554
2555 return 0;
2556 }
2557
2558 static int link_carrier_lost(Link *link) {
2559 int r;
2560
2561 assert(link);
2562
2563 if (link->network && link->network->ignore_carrier_loss)
2564 return 0;
2565
2566 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
2567 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
2568 * configuration and stop the clients as well. */
2569 if (link->setting_mtu)
2570 return 0;
2571
2572 r = link_stop_engines(link, false);
2573 if (r < 0) {
2574 link_enter_failed(link);
2575 return r;
2576 }
2577
2578 r = link_drop_config(link);
2579 if (r < 0)
2580 return r;
2581
2582 if (!IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING, LINK_STATE_INITIALIZED)) {
2583 log_link_debug(link, "State is %s, dropping config", link_state_to_string(link->state));
2584 r = link_drop_foreign_config(link);
2585 if (r < 0)
2586 return r;
2587 }
2588
2589 r = link_handle_bound_by_list(link);
2590 if (r < 0)
2591 return r;
2592
2593 return 0;
2594 }
2595
2596 int link_carrier_reset(Link *link) {
2597 int r;
2598
2599 assert(link);
2600
2601 if (link_has_carrier(link)) {
2602 r = link_carrier_lost(link);
2603 if (r < 0)
2604 return r;
2605
2606 r = link_carrier_gained(link);
2607 if (r < 0)
2608 return r;
2609
2610 log_link_info(link, "Reset carrier");
2611 }
2612
2613 return 0;
2614 }
2615
2616 /* This is called every time an interface admin state changes to up;
2617 * specifically, when IFF_UP flag changes from unset to set */
2618 static int link_admin_state_up(Link *link) {
2619 int r;
2620
2621 /* We set the ipv6 mtu after the device mtu, but the kernel resets
2622 * ipv6 mtu on NETDEV_UP, so we need to reset it. The check for
2623 * ipv6_mtu_set prevents this from trying to set it too early before
2624 * the link->network has been setup; we only need to reset it
2625 * here if we've already set it during normal initialization. */
2626 if (link->ipv6_mtu_set) {
2627 r = link_set_ipv6_mtu(link);
2628 if (r < 0)
2629 return r;
2630 }
2631
2632 return 0;
2633 }
2634
2635 int link_update(Link *link, sd_netlink_message *m) {
2636 _cleanup_strv_free_ char **s = NULL;
2637 hw_addr_data hw_addr;
2638 const char *ifname;
2639 uint32_t mtu;
2640 bool had_carrier, carrier_gained, carrier_lost, link_was_admin_up;
2641 int old_master, r;
2642
2643 assert(link);
2644 assert(link->ifname);
2645 assert(m);
2646
2647 if (link->state == LINK_STATE_LINGER) {
2648 log_link_info(link, "Link re-added");
2649 link_set_state(link, LINK_STATE_CONFIGURING);
2650
2651 r = link_new_carrier_maps(link);
2652 if (r < 0)
2653 return r;
2654 }
2655
2656 r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname);
2657 if (r >= 0 && !streq(ifname, link->ifname)) {
2658 Manager *manager = link->manager;
2659
2660 log_link_info(link, "Interface name change detected, %s has been renamed to %s.", link->ifname, ifname);
2661
2662 link_drop(link);
2663 r = link_add(manager, m, &link);
2664 if (r < 0)
2665 return r;
2666 }
2667
2668 r = sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &s);
2669 if (r >= 0)
2670 strv_free_and_replace(link->alternative_names, s);
2671
2672 r = sd_netlink_message_read_u32(m, IFLA_MTU, &mtu);
2673 if (r >= 0 && mtu > 0) {
2674 link->mtu = mtu;
2675 if (link->original_mtu == 0) {
2676 link->original_mtu = mtu;
2677 log_link_debug(link, "Saved original MTU: %" PRIu32, link->original_mtu);
2678 }
2679
2680 if (link->dhcp_client) {
2681 r = sd_dhcp_client_set_mtu(link->dhcp_client,
2682 link->mtu);
2683 if (r < 0)
2684 return log_link_warning_errno(link, r, "Could not update MTU in DHCP client: %m");
2685 }
2686
2687 if (link->radv) {
2688 r = sd_radv_set_mtu(link->radv, link->mtu);
2689 if (r < 0)
2690 return log_link_warning_errno(link, r, "Could not set MTU for Router Advertisement: %m");
2691 }
2692 }
2693
2694 /* The kernel may broadcast NEWLINK messages without the MAC address
2695 set, simply ignore them. */
2696 r = netlink_message_read_hw_addr(m, IFLA_ADDRESS, &hw_addr);
2697 if (r >= 0 && (link->hw_addr.length != hw_addr.length ||
2698 memcmp(link->hw_addr.addr.bytes, hw_addr.addr.bytes, hw_addr.length) != 0)) {
2699
2700 memcpy(link->hw_addr.addr.bytes, hw_addr.addr.bytes, hw_addr.length);
2701
2702 log_link_debug(link, "Gained new hardware address: %s", HW_ADDR_TO_STR(&hw_addr));
2703
2704 r = ipv4ll_update_mac(link);
2705 if (r < 0)
2706 return log_link_warning_errno(link, r, "Could not update MAC address in IPv4LL client: %m");
2707
2708 r = dhcp4_update_mac(link);
2709 if (r < 0)
2710 return log_link_warning_errno(link, r, "Could not update MAC address in DHCP client: %m");
2711
2712 r = dhcp6_update_mac(link);
2713 if (r < 0)
2714 return log_link_warning_errno(link, r, "Could not update MAC address in DHCPv6 client: %m");
2715
2716 r = radv_update_mac(link);
2717 if (r < 0)
2718 return log_link_warning_errno(link, r, "Could not update MAC address for Router Advertisement: %m");
2719
2720 if (link->ndisc) {
2721 r = sd_ndisc_set_mac(link->ndisc, &link->hw_addr.addr.ether);
2722 if (r < 0)
2723 return log_link_warning_errno(link, r, "Could not update MAC for NDisc: %m");
2724 }
2725
2726 r = ipv4_dad_update_mac(link);
2727 if (r < 0)
2728 return log_link_warning_errno(link, r, "Could not update MAC address in IPv4 ACD client: %m");
2729 }
2730
2731 old_master = link->master_ifindex;
2732 (void) sd_netlink_message_read_u32(m, IFLA_MASTER, (uint32_t *) &link->master_ifindex);
2733
2734 link_was_admin_up = link->flags & IFF_UP;
2735 had_carrier = link_has_carrier(link);
2736
2737 r = link_update_flags(link, m, old_master != link->master_ifindex);
2738 if (r < 0)
2739 return r;
2740
2741 if (!link_was_admin_up && (link->flags & IFF_UP)) {
2742 log_link_info(link, "Link UP");
2743
2744 r = link_admin_state_up(link);
2745 if (r < 0)
2746 return r;
2747 } else if (link_was_admin_up && !(link->flags & IFF_UP))
2748 log_link_info(link, "Link DOWN");
2749
2750 r = link_update_lldp(link);
2751 if (r < 0)
2752 return r;
2753
2754 carrier_gained = !had_carrier && link_has_carrier(link);
2755 carrier_lost = had_carrier && !link_has_carrier(link);
2756
2757 if (carrier_gained) {
2758 log_link_info(link, "Gained carrier");
2759
2760 r = link_carrier_gained(link);
2761 if (r < 0)
2762 return r;
2763 } else if (carrier_lost) {
2764 log_link_info(link, "Lost carrier");
2765
2766 r = link_carrier_lost(link);
2767 if (r < 0)
2768 return r;
2769 }
2770
2771 return 0;
2772 }
2773
2774 static void print_link_hashmap(FILE *f, const char *prefix, Hashmap* h) {
2775 bool space = false;
2776 Link *link;
2777
2778 assert(f);
2779 assert(prefix);
2780
2781 if (hashmap_isempty(h))
2782 return;
2783
2784 fputs(prefix, f);
2785 HASHMAP_FOREACH(link, h) {
2786 if (space)
2787 fputc(' ', f);
2788
2789 fprintf(f, "%i", link->ifindex);
2790 space = true;
2791 }
2792
2793 fputc('\n', f);
2794 }
2795
2796 static void link_save_dns(Link *link, FILE *f, struct in_addr_full **dns, unsigned n_dns, bool *space) {
2797 for (unsigned j = 0; j < n_dns; j++) {
2798 const char *str;
2799
2800 if (dns[j]->ifindex != 0 && dns[j]->ifindex != link->ifindex)
2801 continue;
2802
2803 str = in_addr_full_to_string(dns[j]);
2804 if (!str)
2805 continue;
2806
2807 if (*space)
2808 fputc(' ', f);
2809 fputs(str, f);
2810 *space = true;
2811 }
2812 }
2813
2814 static void serialize_addresses(
2815 FILE *f,
2816 const char *lvalue,
2817 bool *space,
2818 char **addresses,
2819 sd_dhcp_lease *lease,
2820 bool conditional,
2821 sd_dhcp_lease_server_type what,
2822 sd_dhcp6_lease *lease6,
2823 bool conditional6,
2824 int (*lease6_get_addr)(sd_dhcp6_lease*, const struct in6_addr**),
2825 int (*lease6_get_fqdn)(sd_dhcp6_lease*, char ***)) {
2826 int r;
2827
2828 bool _space = false;
2829 if (!space)
2830 space = &_space;
2831
2832 if (lvalue)
2833 fprintf(f, "%s=", lvalue);
2834 fputstrv(f, addresses, NULL, space);
2835
2836 if (lease && conditional) {
2837 const struct in_addr *lease_addresses;
2838
2839 r = sd_dhcp_lease_get_servers(lease, what, &lease_addresses);
2840 if (r > 0)
2841 serialize_in_addrs(f, lease_addresses, r, space, in4_addr_is_non_local);
2842 }
2843
2844 if (lease6 && conditional6 && lease6_get_addr) {
2845 const struct in6_addr *in6_addrs;
2846
2847 r = lease6_get_addr(lease6, &in6_addrs);
2848 if (r > 0)
2849 serialize_in6_addrs(f, in6_addrs, r, space);
2850 }
2851
2852 if (lease6 && conditional6 && lease6_get_fqdn) {
2853 char **in6_hosts;
2854
2855 r = lease6_get_fqdn(lease6, &in6_hosts);
2856 if (r > 0)
2857 fputstrv(f, in6_hosts, NULL, space);
2858 }
2859
2860 if (lvalue)
2861 fputc('\n', f);
2862 }
2863
2864 int link_save(Link *link) {
2865 const char *admin_state, *oper_state, *carrier_state, *address_state;
2866 _cleanup_free_ char *temp_path = NULL;
2867 _cleanup_fclose_ FILE *f = NULL;
2868 int r;
2869
2870 assert(link);
2871 assert(link->state_file);
2872 assert(link->lease_file);
2873 assert(link->manager);
2874
2875 if (link->state == LINK_STATE_LINGER) {
2876 (void) unlink(link->state_file);
2877 return 0;
2878 }
2879
2880 link_lldp_save(link);
2881
2882 admin_state = link_state_to_string(link->state);
2883 assert(admin_state);
2884
2885 oper_state = link_operstate_to_string(link->operstate);
2886 assert(oper_state);
2887
2888 carrier_state = link_carrier_state_to_string(link->carrier_state);
2889 assert(carrier_state);
2890
2891 address_state = link_address_state_to_string(link->address_state);
2892 assert(address_state);
2893
2894 r = fopen_temporary(link->state_file, &f, &temp_path);
2895 if (r < 0)
2896 goto fail;
2897
2898 (void) fchmod(fileno(f), 0644);
2899
2900 fprintf(f,
2901 "# This is private data. Do not parse.\n"
2902 "ADMIN_STATE=%s\n"
2903 "OPER_STATE=%s\n"
2904 "CARRIER_STATE=%s\n"
2905 "ADDRESS_STATE=%s\n",
2906 admin_state, oper_state, carrier_state, address_state);
2907
2908 if (link->network) {
2909 char **dhcp6_domains = NULL, **dhcp_domains = NULL;
2910 const char *dhcp_domainname = NULL, *p;
2911 bool space;
2912
2913 fprintf(f, "REQUIRED_FOR_ONLINE=%s\n",
2914 yes_no(link->network->required_for_online));
2915
2916 LinkOperationalStateRange st = link->network->required_operstate_for_online;
2917 fprintf(f, "REQUIRED_OPER_STATE_FOR_ONLINE=%s%s%s\n",
2918 strempty(link_operstate_to_string(st.min)),
2919 st.max != LINK_OPERSTATE_RANGE_DEFAULT.max ? ":" : "",
2920 st.max != LINK_OPERSTATE_RANGE_DEFAULT.max ? strempty(link_operstate_to_string(st.max)) : "");
2921
2922 fprintf(f, "NETWORK_FILE=%s\n", link->network->filename);
2923
2924 /************************************************************/
2925
2926 fputs("DNS=", f);
2927 space = false;
2928 if (link->n_dns != (unsigned) -1)
2929 link_save_dns(link, f, link->dns, link->n_dns, &space);
2930 else
2931 link_save_dns(link, f, link->network->dns, link->network->n_dns, &space);
2932
2933 serialize_addresses(f, NULL, &space,
2934 NULL,
2935 link->dhcp_lease,
2936 link->network->dhcp_use_dns,
2937 SD_DHCP_LEASE_DNS,
2938 link->dhcp6_lease,
2939 link->network->dhcp6_use_dns,
2940 sd_dhcp6_lease_get_dns,
2941 NULL);
2942
2943 /* Make sure to flush out old entries before we use the NDisc data */
2944 ndisc_vacuum(link);
2945
2946 if (link->network->ipv6_accept_ra_use_dns && link->ndisc_rdnss) {
2947 NDiscRDNSS *dd;
2948
2949 SET_FOREACH(dd, link->ndisc_rdnss)
2950 serialize_in6_addrs(f, &dd->address, 1, &space);
2951 }
2952
2953 fputc('\n', f);
2954
2955 /************************************************************/
2956
2957 serialize_addresses(f, "NTP", NULL,
2958 link->ntp ?: link->network->ntp,
2959 link->dhcp_lease,
2960 link->network->dhcp_use_ntp,
2961 SD_DHCP_LEASE_NTP,
2962 link->dhcp6_lease,
2963 link->network->dhcp6_use_ntp,
2964 sd_dhcp6_lease_get_ntp_addrs,
2965 sd_dhcp6_lease_get_ntp_fqdn);
2966
2967 serialize_addresses(f, "SIP", NULL,
2968 NULL,
2969 link->dhcp_lease,
2970 link->network->dhcp_use_sip,
2971 SD_DHCP_LEASE_SIP,
2972 NULL, false, NULL, NULL);
2973
2974 /************************************************************/
2975
2976 if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) {
2977 if (link->dhcp_lease) {
2978 (void) sd_dhcp_lease_get_domainname(link->dhcp_lease, &dhcp_domainname);
2979 (void) sd_dhcp_lease_get_search_domains(link->dhcp_lease, &dhcp_domains);
2980 }
2981 if (link->dhcp6_lease)
2982 (void) sd_dhcp6_lease_get_domains(link->dhcp6_lease, &dhcp6_domains);
2983 }
2984
2985 fputs("DOMAINS=", f);
2986 space = false;
2987 ORDERED_SET_FOREACH(p, link->search_domains ?: link->network->search_domains)
2988 fputs_with_space(f, p, NULL, &space);
2989
2990 if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) {
2991 if (dhcp_domainname)
2992 fputs_with_space(f, dhcp_domainname, NULL, &space);
2993 if (dhcp_domains)
2994 fputstrv(f, dhcp_domains, NULL, &space);
2995 if (dhcp6_domains)
2996 fputstrv(f, dhcp6_domains, NULL, &space);
2997 }
2998
2999 if (link->network->ipv6_accept_ra_use_domains == DHCP_USE_DOMAINS_YES) {
3000 NDiscDNSSL *dd;
3001
3002 SET_FOREACH(dd, link->ndisc_dnssl)
3003 fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space);
3004 }
3005
3006 fputc('\n', f);
3007
3008 /************************************************************/
3009
3010 fputs("ROUTE_DOMAINS=", f);
3011 space = false;
3012 ORDERED_SET_FOREACH(p, link->route_domains ?: link->network->route_domains)
3013 fputs_with_space(f, p, NULL, &space);
3014
3015 if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_ROUTE) {
3016 if (dhcp_domainname)
3017 fputs_with_space(f, dhcp_domainname, NULL, &space);
3018 if (dhcp_domains)
3019 fputstrv(f, dhcp_domains, NULL, &space);
3020 if (dhcp6_domains)
3021 fputstrv(f, dhcp6_domains, NULL, &space);
3022 }
3023
3024 if (link->network->ipv6_accept_ra_use_domains == DHCP_USE_DOMAINS_ROUTE) {
3025 NDiscDNSSL *dd;
3026
3027 SET_FOREACH(dd, link->ndisc_dnssl)
3028 fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space);
3029 }
3030
3031 fputc('\n', f);
3032
3033 /************************************************************/
3034
3035 fprintf(f, "LLMNR=%s\n",
3036 resolve_support_to_string(link->llmnr >= 0 ? link->llmnr : link->network->llmnr));
3037
3038 /************************************************************/
3039
3040 fprintf(f, "MDNS=%s\n",
3041 resolve_support_to_string(link->mdns >= 0 ? link->mdns : link->network->mdns));
3042
3043 /************************************************************/
3044
3045 int dns_default_route =
3046 link->dns_default_route >= 0 ? link->dns_default_route :
3047 link->network->dns_default_route;
3048 if (dns_default_route >= 0)
3049 fprintf(f, "DNS_DEFAULT_ROUTE=%s\n", yes_no(dns_default_route));
3050
3051 /************************************************************/
3052
3053 DnsOverTlsMode dns_over_tls_mode =
3054 link->dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID ? link->dns_over_tls_mode :
3055 link->network->dns_over_tls_mode;
3056 if (dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID)
3057 fprintf(f, "DNS_OVER_TLS=%s\n", dns_over_tls_mode_to_string(dns_over_tls_mode));
3058
3059 /************************************************************/
3060
3061 DnssecMode dnssec_mode =
3062 link->dnssec_mode != _DNSSEC_MODE_INVALID ? link->dnssec_mode :
3063 link->network->dnssec_mode;
3064 if (dnssec_mode != _DNSSEC_MODE_INVALID)
3065 fprintf(f, "DNSSEC=%s\n", dnssec_mode_to_string(dnssec_mode));
3066
3067 /************************************************************/
3068
3069 Set *nta_anchors = link->dnssec_negative_trust_anchors;
3070 if (set_isempty(nta_anchors))
3071 nta_anchors = link->network->dnssec_negative_trust_anchors;
3072
3073 if (!set_isempty(nta_anchors)) {
3074 const char *n;
3075
3076 fputs("DNSSEC_NTA=", f);
3077 space = false;
3078 SET_FOREACH(n, nta_anchors)
3079 fputs_with_space(f, n, NULL, &space);
3080 fputc('\n', f);
3081 }
3082 }
3083
3084 print_link_hashmap(f, "CARRIER_BOUND_TO=", link->bound_to_links);
3085 print_link_hashmap(f, "CARRIER_BOUND_BY=", link->bound_by_links);
3086
3087 if (link->dhcp_lease) {
3088 r = dhcp_lease_save(link->dhcp_lease, link->lease_file);
3089 if (r < 0)
3090 goto fail;
3091
3092 fprintf(f,
3093 "DHCP_LEASE=%s\n",
3094 link->lease_file);
3095 } else
3096 (void) unlink(link->lease_file);
3097
3098 r = link_serialize_dhcp6_client(link, f);
3099 if (r < 0)
3100 goto fail;
3101
3102 r = fflush_and_check(f);
3103 if (r < 0)
3104 goto fail;
3105
3106 if (rename(temp_path, link->state_file) < 0) {
3107 r = -errno;
3108 goto fail;
3109 }
3110
3111 return 0;
3112
3113 fail:
3114 (void) unlink(link->state_file);
3115 if (temp_path)
3116 (void) unlink(temp_path);
3117
3118 return log_link_error_errno(link, r, "Failed to save link data to %s: %m", link->state_file);
3119 }
3120
3121 /* The serialized state in /run is no longer up-to-date. */
3122 void link_dirty(Link *link) {
3123 int r;
3124
3125 assert(link);
3126
3127 /* mark manager dirty as link is dirty */
3128 manager_dirty(link->manager);
3129
3130 r = set_ensure_put(&link->manager->dirty_links, NULL, link);
3131 if (r <= 0)
3132 /* Ignore allocation errors and don't take another ref if the link was already dirty */
3133 return;
3134 link_ref(link);
3135 }
3136
3137 /* The serialized state in /run is up-to-date */
3138 void link_clean(Link *link) {
3139 assert(link);
3140 assert(link->manager);
3141
3142 link_unref(set_remove(link->manager->dirty_links, link));
3143 }
3144
3145 int link_save_and_clean(Link *link) {
3146 int r;
3147
3148 r = link_save(link);
3149 if (r < 0)
3150 return r;
3151
3152 link_clean(link);
3153 return 0;
3154 }
3155
3156 static const char* const link_state_table[_LINK_STATE_MAX] = {
3157 [LINK_STATE_PENDING] = "pending",
3158 [LINK_STATE_INITIALIZED] = "initialized",
3159 [LINK_STATE_CONFIGURING] = "configuring",
3160 [LINK_STATE_CONFIGURED] = "configured",
3161 [LINK_STATE_UNMANAGED] = "unmanaged",
3162 [LINK_STATE_FAILED] = "failed",
3163 [LINK_STATE_LINGER] = "linger",
3164 };
3165
3166 DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
3167
3168 int log_link_message_full_errno(Link *link, sd_netlink_message *m, int level, int err, const char *msg) {
3169 const char *err_msg = NULL;
3170
3171 (void) sd_netlink_message_read_string(m, NLMSGERR_ATTR_MSG, &err_msg);
3172 return log_link_full_errno(link, level, err,
3173 "%s: %s%s%s%m",
3174 msg,
3175 strempty(err_msg),
3176 err_msg && !endswith(err_msg, ".") ? "." : "",
3177 err_msg ? " " : "");
3178 }