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