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