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