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