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