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