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