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