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