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