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