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