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