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