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