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