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