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