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