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