]>
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 | ||
e6ebebbe | 1360 | if (link->network->arp < 0 && link->network->multicast < 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 | ||
1377 | r = sd_rtnl_message_link_set_flags(req, ifi_flags, ifi_change); | |
1378 | if (r < 0) | |
1379 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
1380 | ||
1381 | r = sd_netlink_call_async(link->manager->rtnl, req, set_flags_handler, link, 0, NULL); | |
1382 | if (r < 0) | |
1383 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1384 | ||
1385 | link_ref(link); | |
1386 | ||
1387 | return 0; | |
1388 | } | |
1389 | ||
e1853b00 | 1390 | static int link_set_bridge(Link *link) { |
4afd3348 | 1391 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
e1853b00 SS |
1392 | int r; |
1393 | ||
1394 | assert(link); | |
1395 | assert(link->network); | |
1396 | ||
6a7a4e4d LP |
1397 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1398 | if (r < 0) | |
1399 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
e1853b00 SS |
1400 | |
1401 | r = sd_rtnl_message_link_set_family(req, PF_BRIDGE); | |
6a7a4e4d LP |
1402 | if (r < 0) |
1403 | return log_link_error_errno(link, r, "Could not set message family: %m"); | |
e1853b00 | 1404 | |
1c4baffc | 1405 | r = sd_netlink_message_open_container(req, IFLA_PROTINFO); |
6a7a4e4d LP |
1406 | if (r < 0) |
1407 | return log_link_error_errno(link, r, "Could not append IFLA_PROTINFO attribute: %m"); | |
e1853b00 | 1408 | |
7f9915f0 SS |
1409 | if (link->network->use_bpdu >= 0) { |
1410 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_GUARD, link->network->use_bpdu); | |
1411 | if (r < 0) | |
1412 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_GUARD attribute: %m"); | |
1413 | } | |
eb7ff4dd | 1414 | |
7f9915f0 SS |
1415 | if (link->network->hairpin >= 0) { |
1416 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_MODE, link->network->hairpin); | |
1417 | if (r < 0) | |
1418 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_MODE attribute: %m"); | |
1419 | } | |
eb7ff4dd | 1420 | |
7f9915f0 SS |
1421 | if (link->network->fast_leave >= 0) { |
1422 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_FAST_LEAVE, link->network->fast_leave); | |
1423 | if (r < 0) | |
1424 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m"); | |
1425 | } | |
eb7ff4dd | 1426 | |
7f9915f0 SS |
1427 | if (link->network->allow_port_to_be_root >= 0) { |
1428 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_PROTECT, link->network->allow_port_to_be_root); | |
1429 | if (r < 0) | |
1430 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_PROTECT attribute: %m"); | |
eb7ff4dd | 1431 | |
7f9915f0 SS |
1432 | } |
1433 | ||
1434 | if (link->network->unicast_flood >= 0) { | |
1435 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_UNICAST_FLOOD, link->network->unicast_flood); | |
1436 | if (r < 0) | |
1437 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m"); | |
1438 | ||
1439 | } | |
eb7ff4dd | 1440 | |
34437b4f | 1441 | if (link->network->cost != 0) { |
1c4baffc | 1442 | r = sd_netlink_message_append_u32(req, IFLA_BRPORT_COST, link->network->cost); |
6a7a4e4d LP |
1443 | if (r < 0) |
1444 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_COST attribute: %m"); | |
e1853b00 | 1445 | } |
b56be296 DJL |
1446 | if (link->network->priority != LINK_BRIDGE_PORT_PRIORITY_INVALID) { |
1447 | r = sd_netlink_message_append_u16(req, IFLA_BRPORT_PRIORITY, link->network->priority); | |
1448 | if (r < 0) | |
1449 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_PRIORITY attribute: %m"); | |
1450 | } | |
e1853b00 | 1451 | |
1c4baffc | 1452 | r = sd_netlink_message_close_container(req); |
6a7a4e4d LP |
1453 | if (r < 0) |
1454 | return log_link_error_errno(link, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
e1853b00 | 1455 | |
1c4baffc | 1456 | r = sd_netlink_call_async(link->manager->rtnl, req, link_set_handler, link, 0, NULL); |
6a7a4e4d LP |
1457 | if (r < 0) |
1458 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
e1853b00 SS |
1459 | |
1460 | link_ref(link); | |
1461 | ||
1462 | return r; | |
1463 | } | |
1464 | ||
eb64b435 SS |
1465 | static int link_bond_set(Link *link) { |
1466 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1467 | int r; | |
1468 | ||
1469 | assert(link); | |
1470 | assert(link->network); | |
1471 | ||
1472 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_NEWLINK, link->network->bond->ifindex); | |
1473 | if (r < 0) | |
1474 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
1475 | ||
1476 | r = sd_netlink_message_set_flags(req, NLM_F_REQUEST | NLM_F_ACK); | |
1477 | if (r < 0) | |
1478 | return log_link_error_errno(link, r, "Could not set netlink flags: %m"); | |
1479 | ||
1480 | r = sd_netlink_message_open_container(req, IFLA_LINKINFO); | |
1481 | if (r < 0) | |
1482 | return log_link_error_errno(link, r, "Could not append IFLA_PROTINFO attribute: %m"); | |
1483 | ||
1484 | r = sd_netlink_message_open_container_union(req, IFLA_INFO_DATA, "bond"); | |
1485 | if (r < 0) | |
1486 | return log_link_error_errno(link, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
1487 | ||
1488 | if (link->network->active_slave) { | |
1489 | r = sd_netlink_message_append_u32(req, IFLA_BOND_ACTIVE_SLAVE, link->ifindex); | |
1490 | if (r < 0) | |
1491 | return log_link_error_errno(link, r, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m"); | |
1492 | } | |
1493 | ||
1494 | if (link->network->primary_slave) { | |
1495 | r = sd_netlink_message_append_u32(req, IFLA_BOND_PRIMARY, link->ifindex); | |
1496 | if (r < 0) | |
1497 | return log_link_error_errno(link, r, "Could not append IFLA_BOND_PRIMARY attribute: %m"); | |
1498 | } | |
1499 | ||
1500 | r = sd_netlink_message_close_container(req); | |
1501 | if (r < 0) | |
1502 | return log_link_error_errno(link, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
1503 | ||
1504 | r = sd_netlink_message_close_container(req); | |
1505 | if (r < 0) | |
1506 | return log_link_error_errno(link, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
1507 | ||
1508 | r = sd_netlink_call_async(link->manager->rtnl, req, set_flags_handler, link, 0, NULL); | |
1509 | if (r < 0) | |
1510 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1511 | ||
1512 | link_ref(link); | |
1513 | ||
1514 | return r; | |
1515 | } | |
1516 | ||
34437b4f LP |
1517 | static int link_lldp_save(Link *link) { |
1518 | _cleanup_free_ char *temp_path = NULL; | |
1519 | _cleanup_fclose_ FILE *f = NULL; | |
1520 | sd_lldp_neighbor **l = NULL; | |
1521 | int n = 0, r, i; | |
49699bac SS |
1522 | |
1523 | assert(link); | |
34437b4f LP |
1524 | assert(link->lldp_file); |
1525 | ||
1526 | if (!link->lldp) { | |
1527 | (void) unlink(link->lldp_file); | |
1528 | return 0; | |
1529 | } | |
1530 | ||
1531 | r = sd_lldp_get_neighbors(link->lldp, &l); | |
1532 | if (r < 0) | |
1533 | goto finish; | |
1534 | if (r == 0) { | |
1535 | (void) unlink(link->lldp_file); | |
1536 | goto finish; | |
1537 | } | |
1538 | ||
1539 | n = r; | |
1540 | ||
1541 | r = fopen_temporary(link->lldp_file, &f, &temp_path); | |
1542 | if (r < 0) | |
1543 | goto finish; | |
1544 | ||
1545 | fchmod(fileno(f), 0644); | |
49699bac | 1546 | |
34437b4f LP |
1547 | for (i = 0; i < n; i++) { |
1548 | const void *p; | |
1549 | le64_t u; | |
1550 | size_t sz; | |
1551 | ||
1552 | r = sd_lldp_neighbor_get_raw(l[i], &p, &sz); | |
9ef61f2e | 1553 | if (r < 0) |
34437b4f LP |
1554 | goto finish; |
1555 | ||
1556 | u = htole64(sz); | |
1557 | (void) fwrite(&u, 1, sizeof(u), f); | |
1558 | (void) fwrite(p, 1, sz, f); | |
1559 | } | |
49699bac | 1560 | |
34437b4f LP |
1561 | r = fflush_and_check(f); |
1562 | if (r < 0) | |
1563 | goto finish; | |
1564 | ||
1565 | if (rename(temp_path, link->lldp_file) < 0) { | |
1566 | r = -errno; | |
1567 | goto finish; | |
9ef61f2e | 1568 | } |
34437b4f LP |
1569 | |
1570 | finish: | |
1571 | if (r < 0) { | |
1572 | (void) unlink(link->lldp_file); | |
1573 | if (temp_path) | |
1574 | (void) unlink(temp_path); | |
1575 | ||
1576 | log_link_error_errno(link, r, "Failed to save LLDP data to %s: %m", link->lldp_file); | |
1577 | } | |
1578 | ||
1579 | if (l) { | |
1580 | for (i = 0; i < n; i++) | |
1581 | sd_lldp_neighbor_unref(l[i]); | |
1582 | free(l); | |
1583 | } | |
1584 | ||
1585 | return r; | |
1586 | } | |
1587 | ||
90dffb22 | 1588 | static void lldp_handler(sd_lldp *lldp, sd_lldp_event event, sd_lldp_neighbor *n, void *userdata) { |
34437b4f | 1589 | Link *link = userdata; |
90dffb22 | 1590 | int r; |
34437b4f LP |
1591 | |
1592 | assert(link); | |
1593 | ||
1594 | (void) link_lldp_save(link); | |
90dffb22 | 1595 | |
7272b25e | 1596 | if (link_lldp_emit_enabled(link) && event == SD_LLDP_EVENT_ADDED) { |
90dffb22 LP |
1597 | /* If we received information about a new neighbor, restart the LLDP "fast" logic */ |
1598 | ||
1599 | log_link_debug(link, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission."); | |
1600 | ||
7272b25e | 1601 | r = link_lldp_emit_start(link); |
90dffb22 LP |
1602 | if (r < 0) |
1603 | log_link_warning_errno(link, r, "Failed to restart LLDP transmission: %m"); | |
1604 | } | |
49699bac SS |
1605 | } |
1606 | ||
e7ab854c TG |
1607 | static int link_acquire_ipv6_conf(Link *link) { |
1608 | int r; | |
1609 | ||
1610 | assert(link); | |
1611 | ||
1612 | if (link_dhcp6_enabled(link)) { | |
1613 | assert(link->dhcp6_client); | |
c601ebf7 | 1614 | assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0); |
e7ab854c | 1615 | |
720bec40 TY |
1616 | /* start DHCPv6 client in stateless mode */ |
1617 | r = dhcp6_request_address(link, true); | |
63348d13 | 1618 | if (r < 0 && r != -EBUSY) |
e7ab854c | 1619 | return log_link_warning_errno(link, r, "Could not acquire DHCPv6 lease: %m"); |
720bec40 TY |
1620 | else |
1621 | log_link_debug(link, "Acquiring DHCPv6 lease"); | |
e7ab854c TG |
1622 | } |
1623 | ||
1624 | if (link_ipv6_accept_ra_enabled(link)) { | |
1e7a0e21 | 1625 | assert(link->ndisc); |
e7ab854c TG |
1626 | |
1627 | log_link_debug(link, "Discovering IPv6 routers"); | |
1628 | ||
1e7a0e21 | 1629 | r = sd_ndisc_start(link->ndisc); |
63348d13 | 1630 | if (r < 0 && r != -EBUSY) |
e7ab854c TG |
1631 | return log_link_warning_errno(link, r, "Could not start IPv6 Router Discovery: %m"); |
1632 | } | |
1633 | ||
7465dd22 PF |
1634 | if (link_radv_enabled(link)) { |
1635 | assert(link->radv); | |
1636 | assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0); | |
1637 | ||
1638 | log_link_debug(link, "Starting IPv6 Router Advertisements"); | |
1639 | ||
1640 | r = sd_radv_start(link->radv); | |
1641 | if (r < 0 && r != -EBUSY) | |
1642 | return log_link_warning_errno(link, r, "Could not start IPv6 Router Advertisement: %m"); | |
1643 | } | |
1644 | ||
e7ab854c TG |
1645 | return 0; |
1646 | } | |
1647 | ||
6fc25497 | 1648 | static int link_acquire_ipv4_conf(Link *link) { |
ff254138 TG |
1649 | int r; |
1650 | ||
1651 | assert(link); | |
1652 | assert(link->network); | |
ff254138 TG |
1653 | assert(link->manager); |
1654 | assert(link->manager->event); | |
1655 | ||
78c958f8 | 1656 | if (link_ipv4ll_enabled(link)) { |
eb34d4af | 1657 | assert(link->ipv4ll); |
ff254138 | 1658 | |
6a7a4e4d | 1659 | log_link_debug(link, "Acquiring IPv4 link-local address"); |
5c1d3fc9 UTL |
1660 | |
1661 | r = sd_ipv4ll_start(link->ipv4ll); | |
6a7a4e4d LP |
1662 | if (r < 0) |
1663 | return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m"); | |
5c1d3fc9 UTL |
1664 | } |
1665 | ||
78c958f8 | 1666 | if (link_dhcp4_enabled(link)) { |
eb34d4af | 1667 | assert(link->dhcp_client); |
ff254138 | 1668 | |
6a7a4e4d | 1669 | log_link_debug(link, "Acquiring DHCPv4 lease"); |
ab47d620 | 1670 | |
5c1d3fc9 | 1671 | r = sd_dhcp_client_start(link->dhcp_client); |
6a7a4e4d LP |
1672 | if (r < 0) |
1673 | return log_link_warning_errno(link, r, "Could not acquire DHCPv4 lease: %m"); | |
5c1d3fc9 | 1674 | } |
ff254138 | 1675 | |
6fc25497 SS |
1676 | return 0; |
1677 | } | |
1678 | ||
1679 | static int link_acquire_conf(Link *link) { | |
1680 | int r; | |
1681 | ||
1682 | assert(link); | |
1683 | ||
97e7fb39 SS |
1684 | if (link->setting_mtu) { |
1685 | link->setting_mtu = false; | |
1686 | return 0; | |
1687 | } | |
1688 | ||
6fc25497 SS |
1689 | r = link_acquire_ipv4_conf(link); |
1690 | if (r < 0) | |
1691 | return r; | |
1692 | ||
1693 | if (in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address) == 0) { | |
1694 | r = link_acquire_ipv6_conf(link); | |
1695 | if (r < 0) | |
1696 | return r; | |
1697 | } | |
1698 | ||
7272b25e LP |
1699 | if (link_lldp_emit_enabled(link)) { |
1700 | r = link_lldp_emit_start(link); | |
8e1ad1ea LP |
1701 | if (r < 0) |
1702 | return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m"); | |
1703 | } | |
1704 | ||
ff254138 TG |
1705 | return 0; |
1706 | } | |
1707 | ||
a61bb41c | 1708 | bool link_has_carrier(Link *link) { |
deb2e523 TG |
1709 | /* see Documentation/networking/operstates.txt in the kernel sources */ |
1710 | ||
a61bb41c | 1711 | if (link->kernel_operstate == IF_OPER_UP) |
deb2e523 TG |
1712 | return true; |
1713 | ||
a61bb41c | 1714 | if (link->kernel_operstate == IF_OPER_UNKNOWN) |
deb2e523 | 1715 | /* operstate may not be implemented, so fall back to flags */ |
a61bb41c | 1716 | if ((link->flags & IFF_LOWER_UP) && !(link->flags & IFF_DORMANT)) |
deb2e523 TG |
1717 | return true; |
1718 | ||
1719 | return false; | |
1720 | } | |
1721 | ||
1c4baffc | 1722 | static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
8e766630 | 1723 | _cleanup_(link_unrefp) Link *link = userdata; |
dd3efc09 TG |
1724 | int r; |
1725 | ||
1746cf2a TG |
1726 | assert(link); |
1727 | ||
5da8149f | 1728 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
1746cf2a TG |
1729 | return 1; |
1730 | ||
1c4baffc | 1731 | r = sd_netlink_message_get_errno(m); |
6a7a4e4d LP |
1732 | if (r < 0) |
1733 | /* we warn but don't fail the link, as it may be | |
1734 | brought up later */ | |
a2fae7bb | 1735 | log_link_warning_errno(link, r, "Could not bring up interface: %m"); |
45ad2c13 | 1736 | |
f882c247 TG |
1737 | return 1; |
1738 | } | |
1739 | ||
14b6bb77 | 1740 | int link_up(Link *link) { |
4afd3348 | 1741 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
5c3072ea | 1742 | uint8_t ipv6ll_mode; |
f579559b TG |
1743 | int r; |
1744 | ||
f882c247 | 1745 | assert(link); |
c106cc36 | 1746 | assert(link->network); |
f882c247 TG |
1747 | assert(link->manager); |
1748 | assert(link->manager->rtnl); | |
1749 | ||
6a7a4e4d | 1750 | log_link_debug(link, "Bringing link up"); |
449f7554 | 1751 | |
6a7a4e4d LP |
1752 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1753 | if (r < 0) | |
1754 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
f579559b | 1755 | |
2b2d8603 | 1756 | /* set it free if not enslaved with networkd */ |
6cb955c6 | 1757 | if (!link->network->bridge && !link->network->bond && !link->network->vrf) { |
2b2d8603 TY |
1758 | r = sd_netlink_message_append_u32(req, IFLA_MASTER, 0); |
1759 | if (r < 0) | |
1760 | return log_link_error_errno(link, r, "Could not append IFLA_MASTER attribute: %m"); | |
1761 | } | |
1762 | ||
5d4795f3 | 1763 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); |
6a7a4e4d LP |
1764 | if (r < 0) |
1765 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
fc25d7f8 | 1766 | |
c106cc36 | 1767 | if (link->network->mac) { |
1c4baffc | 1768 | r = sd_netlink_message_append_ether_addr(req, IFLA_ADDRESS, link->network->mac); |
6a7a4e4d LP |
1769 | if (r < 0) |
1770 | return log_link_error_errno(link, r, "Could not set MAC address: %m"); | |
c106cc36 TG |
1771 | } |
1772 | ||
4cef7fe3 | 1773 | /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled) |
2b00a4e0 | 1774 | for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */ |
439689c6 SS |
1775 | (void) link_enable_ipv6(link); |
1776 | ||
4e964aa0 | 1777 | if (link->network->mtu != 0) { |
439689c6 SS |
1778 | /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes |
1779 | on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */ | |
1780 | if (link_ipv6_enabled(link) && link->network->mtu < IPV6_MIN_MTU) { | |
1781 | ||
1782 | log_link_warning(link, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU) ", as " | |
1783 | "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU) " bytes: %m"); | |
1784 | ||
1785 | link->network->mtu = IPV6_MIN_MTU; | |
1786 | } | |
1787 | ||
1c4baffc | 1788 | r = sd_netlink_message_append_u32(req, IFLA_MTU, link->network->mtu); |
6a7a4e4d LP |
1789 | if (r < 0) |
1790 | return log_link_error_errno(link, r, "Could not set MTU: %m"); | |
c106cc36 TG |
1791 | } |
1792 | ||
1c4baffc | 1793 | r = sd_netlink_message_open_container(req, IFLA_AF_SPEC); |
6a7a4e4d LP |
1794 | if (r < 0) |
1795 | return log_link_error_errno(link, r, "Could not open IFLA_AF_SPEC container: %m"); | |
d0d6a4cd | 1796 | |
439689c6 | 1797 | if (link_ipv6_enabled(link)) { |
01d28f81 | 1798 | /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */ |
1c4baffc | 1799 | r = sd_netlink_message_open_container(req, AF_INET6); |
01d28f81 TG |
1800 | if (r < 0) |
1801 | return log_link_error_errno(link, r, "Could not open AF_INET6 container: %m"); | |
d0d6a4cd | 1802 | |
fbc38f23 TY |
1803 | if (!link_ipv6ll_enabled(link)) |
1804 | ipv6ll_mode = IN6_ADDR_GEN_MODE_NONE; | |
1805 | else { | |
1806 | const char *p = NULL; | |
1807 | _cleanup_free_ char *stable_secret = NULL; | |
1808 | ||
1809 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/stable_secret"); | |
1810 | r = read_one_line_file(p, &stable_secret); | |
1811 | ||
1812 | if (r < 0) | |
1813 | ipv6ll_mode = IN6_ADDR_GEN_MODE_EUI64; | |
1814 | else | |
1815 | ipv6ll_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; | |
1816 | } | |
1c4baffc | 1817 | r = sd_netlink_message_append_u8(req, IFLA_INET6_ADDR_GEN_MODE, ipv6ll_mode); |
01d28f81 TG |
1818 | if (r < 0) |
1819 | return log_link_error_errno(link, r, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m"); | |
d0d6a4cd | 1820 | |
01d28f81 | 1821 | if (!in_addr_is_null(AF_INET6, &link->network->ipv6_token)) { |
1c4baffc | 1822 | r = sd_netlink_message_append_in6_addr(req, IFLA_INET6_TOKEN, &link->network->ipv6_token.in6); |
01d28f81 TG |
1823 | if (r < 0) |
1824 | return log_link_error_errno(link, r, "Could not append IFLA_INET6_TOKEN: %m"); | |
1825 | } | |
1826 | ||
1c4baffc | 1827 | r = sd_netlink_message_close_container(req); |
6a7a4e4d | 1828 | if (r < 0) |
01d28f81 | 1829 | return log_link_error_errno(link, r, "Could not close AF_INET6 container: %m"); |
7f77697a | 1830 | } |
d0d6a4cd | 1831 | |
1c4baffc | 1832 | r = sd_netlink_message_close_container(req); |
6a7a4e4d LP |
1833 | if (r < 0) |
1834 | return log_link_error_errno(link, r, "Could not close IFLA_AF_SPEC container: %m"); | |
d0d6a4cd | 1835 | |
1c4baffc | 1836 | r = sd_netlink_call_async(link->manager->rtnl, req, link_up_handler, link, 0, NULL); |
6a7a4e4d LP |
1837 | if (r < 0) |
1838 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
f579559b | 1839 | |
b226d99b TG |
1840 | link_ref(link); |
1841 | ||
f882c247 TG |
1842 | return 0; |
1843 | } | |
1844 | ||
1c4baffc | 1845 | static int link_down_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
8e766630 | 1846 | _cleanup_(link_unrefp) Link *link = userdata; |
0d4ad91d AR |
1847 | int r; |
1848 | ||
1849 | assert(link); | |
1850 | ||
1851 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1852 | return 1; | |
1853 | ||
1c4baffc | 1854 | r = sd_netlink_message_get_errno(m); |
0d4ad91d | 1855 | if (r < 0) |
a2fae7bb | 1856 | log_link_warning_errno(link, r, "Could not bring down interface: %m"); |
0d4ad91d AR |
1857 | |
1858 | return 1; | |
1859 | } | |
1860 | ||
14b6bb77 | 1861 | int link_down(Link *link) { |
4afd3348 | 1862 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
0d4ad91d AR |
1863 | int r; |
1864 | ||
1865 | assert(link); | |
1866 | assert(link->manager); | |
1867 | assert(link->manager->rtnl); | |
1868 | ||
6a7a4e4d | 1869 | log_link_debug(link, "Bringing link down"); |
0d4ad91d AR |
1870 | |
1871 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, | |
1872 | RTM_SETLINK, link->ifindex); | |
6a7a4e4d LP |
1873 | if (r < 0) |
1874 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
0d4ad91d AR |
1875 | |
1876 | r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP); | |
6a7a4e4d LP |
1877 | if (r < 0) |
1878 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
0d4ad91d | 1879 | |
1c4baffc | 1880 | r = sd_netlink_call_async(link->manager->rtnl, req, link_down_handler, link, 0, NULL); |
6a7a4e4d LP |
1881 | if (r < 0) |
1882 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
0d4ad91d AR |
1883 | |
1884 | link_ref(link); | |
1885 | ||
1886 | return 0; | |
1887 | } | |
1888 | ||
92c918b0 SS |
1889 | static int link_up_can(Link *link) { |
1890 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1891 | int r; | |
1892 | ||
1893 | assert(link); | |
1894 | ||
1895 | log_link_debug(link, "Bringing CAN link up"); | |
1896 | ||
1897 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); | |
1898 | if (r < 0) | |
1899 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
1900 | ||
1901 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); | |
1902 | if (r < 0) | |
1903 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
1904 | ||
1905 | r = sd_netlink_call_async(link->manager->rtnl, req, link_up_handler, link, 0, NULL); | |
1906 | if (r < 0) | |
1907 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1908 | ||
1909 | link_ref(link); | |
1910 | ||
1911 | return 0; | |
1912 | } | |
1913 | ||
0d4ad91d AR |
1914 | static int link_handle_bound_to_list(Link *link) { |
1915 | Link *l; | |
1916 | Iterator i; | |
1917 | int r; | |
1918 | bool required_up = false; | |
1919 | bool link_is_up = false; | |
1920 | ||
1921 | assert(link); | |
1922 | ||
1923 | if (hashmap_isempty(link->bound_to_links)) | |
1924 | return 0; | |
1925 | ||
1926 | if (link->flags & IFF_UP) | |
1927 | link_is_up = true; | |
1928 | ||
1929 | HASHMAP_FOREACH (l, link->bound_to_links, i) | |
1930 | if (link_has_carrier(l)) { | |
1931 | required_up = true; | |
1932 | break; | |
1933 | } | |
1934 | ||
1935 | if (!required_up && link_is_up) { | |
1936 | r = link_down(link); | |
1937 | if (r < 0) | |
1938 | return r; | |
1939 | } else if (required_up && !link_is_up) { | |
1940 | r = link_up(link); | |
1941 | if (r < 0) | |
1942 | return r; | |
1943 | } | |
1944 | ||
1945 | return 0; | |
1946 | } | |
1947 | ||
1948 | static int link_handle_bound_by_list(Link *link) { | |
1949 | Iterator i; | |
1950 | Link *l; | |
1951 | int r; | |
1952 | ||
1953 | assert(link); | |
1954 | ||
1955 | if (hashmap_isempty(link->bound_by_links)) | |
1956 | return 0; | |
1957 | ||
1958 | HASHMAP_FOREACH (l, link->bound_by_links, i) { | |
1959 | r = link_handle_bound_to_list(l); | |
1960 | if (r < 0) | |
1961 | return r; | |
1962 | } | |
1963 | ||
1964 | return 0; | |
1965 | } | |
1966 | ||
1967 | static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) { | |
1968 | int r; | |
1969 | ||
1970 | assert(link); | |
1971 | assert(carrier); | |
1972 | ||
1973 | if (link == carrier) | |
1974 | return 0; | |
1975 | ||
1976 | if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex))) | |
1977 | return 0; | |
1978 | ||
1979 | r = hashmap_ensure_allocated(h, NULL); | |
1980 | if (r < 0) | |
1981 | return r; | |
1982 | ||
1983 | r = hashmap_put(*h, INT_TO_PTR(carrier->ifindex), carrier); | |
1984 | if (r < 0) | |
1985 | return r; | |
1986 | ||
1987 | return 0; | |
1988 | } | |
1989 | ||
1990 | static int link_new_bound_by_list(Link *link) { | |
1991 | Manager *m; | |
1992 | Link *carrier; | |
1993 | Iterator i; | |
1994 | int r; | |
1995 | bool list_updated = false; | |
1996 | ||
1997 | assert(link); | |
1998 | assert(link->manager); | |
1999 | ||
2000 | m = link->manager; | |
2001 | ||
b295beea | 2002 | HASHMAP_FOREACH(carrier, m->links, i) { |
0d4ad91d AR |
2003 | if (!carrier->network) |
2004 | continue; | |
2005 | ||
2006 | if (strv_isempty(carrier->network->bind_carrier)) | |
2007 | continue; | |
2008 | ||
2009 | if (strv_fnmatch(carrier->network->bind_carrier, link->ifname, 0)) { | |
2010 | r = link_put_carrier(link, carrier, &link->bound_by_links); | |
2011 | if (r < 0) | |
2012 | return r; | |
2013 | ||
2014 | list_updated = true; | |
2015 | } | |
2016 | } | |
2017 | ||
2018 | if (list_updated) | |
84de38c5 | 2019 | link_dirty(link); |
0d4ad91d | 2020 | |
b295beea | 2021 | HASHMAP_FOREACH(carrier, link->bound_by_links, i) { |
0d4ad91d AR |
2022 | r = link_put_carrier(carrier, link, &carrier->bound_to_links); |
2023 | if (r < 0) | |
2024 | return r; | |
2025 | ||
84de38c5 | 2026 | link_dirty(carrier); |
0d4ad91d AR |
2027 | } |
2028 | ||
2029 | return 0; | |
2030 | } | |
2031 | ||
2032 | static int link_new_bound_to_list(Link *link) { | |
2033 | Manager *m; | |
2034 | Link *carrier; | |
2035 | Iterator i; | |
2036 | int r; | |
2037 | bool list_updated = false; | |
2038 | ||
2039 | assert(link); | |
2040 | assert(link->manager); | |
2041 | ||
2042 | if (!link->network) | |
2043 | return 0; | |
2044 | ||
2045 | if (strv_isempty(link->network->bind_carrier)) | |
2046 | return 0; | |
2047 | ||
2048 | m = link->manager; | |
2049 | ||
2050 | HASHMAP_FOREACH (carrier, m->links, i) { | |
2051 | if (strv_fnmatch(link->network->bind_carrier, carrier->ifname, 0)) { | |
2052 | r = link_put_carrier(link, carrier, &link->bound_to_links); | |
2053 | if (r < 0) | |
2054 | return r; | |
2055 | ||
2056 | list_updated = true; | |
2057 | } | |
2058 | } | |
2059 | ||
2060 | if (list_updated) | |
84de38c5 | 2061 | link_dirty(link); |
0d4ad91d AR |
2062 | |
2063 | HASHMAP_FOREACH (carrier, link->bound_to_links, i) { | |
2064 | r = link_put_carrier(carrier, link, &carrier->bound_by_links); | |
2065 | if (r < 0) | |
2066 | return r; | |
2067 | ||
84de38c5 | 2068 | link_dirty(carrier); |
0d4ad91d AR |
2069 | } |
2070 | ||
2071 | return 0; | |
2072 | } | |
2073 | ||
2074 | static int link_new_carrier_maps(Link *link) { | |
2075 | int r; | |
2076 | ||
2077 | r = link_new_bound_by_list(link); | |
2078 | if (r < 0) | |
2079 | return r; | |
2080 | ||
2081 | r = link_handle_bound_by_list(link); | |
2082 | if (r < 0) | |
2083 | return r; | |
2084 | ||
2085 | r = link_new_bound_to_list(link); | |
2086 | if (r < 0) | |
2087 | return r; | |
2088 | ||
2089 | r = link_handle_bound_to_list(link); | |
2090 | if (r < 0) | |
2091 | return r; | |
2092 | ||
2093 | return 0; | |
2094 | } | |
2095 | ||
2096 | static void link_free_bound_to_list(Link *link) { | |
2097 | Link *bound_to; | |
2098 | Iterator i; | |
2099 | ||
2100 | HASHMAP_FOREACH (bound_to, link->bound_to_links, i) { | |
2101 | hashmap_remove(link->bound_to_links, INT_TO_PTR(bound_to->ifindex)); | |
2102 | ||
2103 | if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex))) | |
84de38c5 | 2104 | link_dirty(bound_to); |
0d4ad91d AR |
2105 | } |
2106 | ||
2107 | return; | |
2108 | } | |
2109 | ||
2110 | static void link_free_bound_by_list(Link *link) { | |
2111 | Link *bound_by; | |
2112 | Iterator i; | |
2113 | ||
2114 | HASHMAP_FOREACH (bound_by, link->bound_by_links, i) { | |
2115 | hashmap_remove(link->bound_by_links, INT_TO_PTR(bound_by->ifindex)); | |
2116 | ||
2117 | if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) { | |
84de38c5 | 2118 | link_dirty(bound_by); |
0d4ad91d AR |
2119 | link_handle_bound_to_list(bound_by); |
2120 | } | |
2121 | } | |
2122 | ||
2123 | return; | |
2124 | } | |
2125 | ||
2126 | static void link_free_carrier_maps(Link *link) { | |
2127 | bool list_updated = false; | |
2128 | ||
2129 | assert(link); | |
2130 | ||
2131 | if (!hashmap_isempty(link->bound_to_links)) { | |
2132 | link_free_bound_to_list(link); | |
2133 | list_updated = true; | |
2134 | } | |
2135 | ||
2136 | if (!hashmap_isempty(link->bound_by_links)) { | |
2137 | link_free_bound_by_list(link); | |
2138 | list_updated = true; | |
2139 | } | |
2140 | ||
2141 | if (list_updated) | |
84de38c5 | 2142 | link_dirty(link); |
0d4ad91d AR |
2143 | |
2144 | return; | |
2145 | } | |
2146 | ||
2147 | void link_drop(Link *link) { | |
2148 | if (!link || link->state == LINK_STATE_LINGER) | |
2149 | return; | |
2150 | ||
2151 | link_set_state(link, LINK_STATE_LINGER); | |
2152 | ||
2153 | link_free_carrier_maps(link); | |
2154 | ||
6a7a4e4d | 2155 | log_link_debug(link, "Link removed"); |
0d4ad91d | 2156 | |
84de38c5 | 2157 | (void)unlink(link->state_file); |
0d4ad91d AR |
2158 | link_unref(link); |
2159 | ||
2160 | return; | |
2161 | } | |
2162 | ||
3f265037 | 2163 | static int link_joined(Link *link) { |
f882c247 TG |
2164 | int r; |
2165 | ||
ef1ba606 | 2166 | assert(link); |
f5be5601 | 2167 | assert(link->network); |
dd3efc09 | 2168 | |
0d4ad91d AR |
2169 | if (!hashmap_isempty(link->bound_to_links)) { |
2170 | r = link_handle_bound_to_list(link); | |
2171 | if (r < 0) | |
2172 | return r; | |
2173 | } else if (!(link->flags & IFF_UP)) { | |
505f8da7 TG |
2174 | r = link_up(link); |
2175 | if (r < 0) { | |
2176 | link_enter_failed(link); | |
2177 | return r; | |
2178 | } | |
ef1ba606 | 2179 | } |
f882c247 | 2180 | |
9ed794a3 | 2181 | if (link->network->bridge) { |
e1853b00 | 2182 | r = link_set_bridge(link); |
6a7a4e4d LP |
2183 | if (r < 0) |
2184 | log_link_error_errno(link, r, "Could not set bridge message: %m"); | |
e1853b00 SS |
2185 | } |
2186 | ||
eb64b435 SS |
2187 | if (link->network->bond) { |
2188 | r = link_bond_set(link); | |
2189 | if (r < 0) | |
2190 | log_link_error_errno(link, r, "Could not set bond message: %m"); | |
2191 | } | |
2192 | ||
ffff9abe TJ |
2193 | if (link->network->use_br_vlan && |
2194 | (link->network->bridge || streq_ptr("bridge", link->kind))) { | |
13b498f9 TJ |
2195 | r = link_set_bridge_vlan(link); |
2196 | if (r < 0) | |
2197 | log_link_error_errno(link, r, "Could not set bridge vlan: %m"); | |
2198 | } | |
2199 | ||
c1835a42 RM |
2200 | /* Skip setting up addresses until it gets carrier, |
2201 | or it would try to set addresses twice, | |
2202 | which is bad for non-idempotent steps. */ | |
dad2d78e | 2203 | if (!link_has_carrier(link) && !link->network->configure_without_carrier) |
c1835a42 RM |
2204 | return 0; |
2205 | ||
fb6730c4 | 2206 | return link_enter_set_addresses(link); |
02b59d57 TG |
2207 | } |
2208 | ||
62e2d5bb | 2209 | static int netdev_join_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
8e766630 | 2210 | _cleanup_(link_unrefp) Link *link = userdata; |
02b59d57 TG |
2211 | int r; |
2212 | ||
1746cf2a | 2213 | assert(link); |
ef1ba606 | 2214 | assert(link->network); |
02b59d57 | 2215 | |
313cefa1 | 2216 | link->enslaving--; |
52433f6b | 2217 | |
5da8149f | 2218 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
02b59d57 TG |
2219 | return 1; |
2220 | ||
1c4baffc | 2221 | r = sd_netlink_message_get_errno(m); |
856f962c | 2222 | if (r < 0 && r != -EEXIST) { |
a2fae7bb | 2223 | log_link_error_errno(link, r, "Could not join netdev: %m"); |
ef1ba606 TG |
2224 | link_enter_failed(link); |
2225 | return 1; | |
ba179154 | 2226 | } else |
6a7a4e4d | 2227 | log_link_debug(link, "Joined netdev"); |
02b59d57 | 2228 | |
856f962c | 2229 | if (link->enslaving <= 0) |
3f265037 | 2230 | link_joined(link); |
02b59d57 TG |
2231 | |
2232 | return 1; | |
2233 | } | |
2234 | ||
3f265037 | 2235 | static int link_enter_join_netdev(Link *link) { |
6a0a2f86 | 2236 | NetDev *netdev; |
672682a6 | 2237 | Iterator i; |
02b59d57 TG |
2238 | int r; |
2239 | ||
2240 | assert(link); | |
2241 | assert(link->network); | |
8434fd5c | 2242 | assert(link->state == LINK_STATE_PENDING); |
02b59d57 | 2243 | |
e331e246 | 2244 | link_set_state(link, LINK_STATE_ENSLAVING); |
02b59d57 | 2245 | |
84de38c5 | 2246 | link_dirty(link); |
fe8db0c5 | 2247 | |
7951dea2 SS |
2248 | if (!link->network->bridge && |
2249 | !link->network->bond && | |
6cb955c6 | 2250 | !link->network->vrf && |
6a0a2f86 | 2251 | hashmap_isempty(link->network->stacked_netdevs)) |
3f265037 | 2252 | return link_joined(link); |
02b59d57 | 2253 | |
d9c67ea1 | 2254 | if (link->network->bond) { |
f2341e0a LP |
2255 | log_struct(LOG_DEBUG, |
2256 | LOG_LINK_INTERFACE(link), | |
2257 | LOG_NETDEV_INTERFACE(link->network->bond), | |
2258 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bond->ifname), | |
2259 | NULL); | |
2260 | ||
2261 | r = netdev_join(link->network->bond, link, netdev_join_handler); | |
52433f6b | 2262 | if (r < 0) { |
f2341e0a LP |
2263 | log_struct_errno(LOG_WARNING, r, |
2264 | LOG_LINK_INTERFACE(link), | |
2265 | LOG_NETDEV_INTERFACE(link->network->bond), | |
2266 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bond->ifname), | |
2267 | NULL); | |
2268 | ||
52433f6b TG |
2269 | link_enter_failed(link); |
2270 | return r; | |
2271 | } | |
2272 | ||
313cefa1 | 2273 | link->enslaving++; |
0ad6148e MO |
2274 | } |
2275 | ||
d9c67ea1 | 2276 | if (link->network->bridge) { |
f2341e0a LP |
2277 | log_struct(LOG_DEBUG, |
2278 | LOG_LINK_INTERFACE(link), | |
2279 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
2280 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bridge->ifname), | |
2281 | NULL); | |
2282 | ||
2283 | r = netdev_join(link->network->bridge, link, netdev_join_handler); | |
0ad6148e | 2284 | if (r < 0) { |
f2341e0a LP |
2285 | log_struct_errno(LOG_WARNING, r, |
2286 | LOG_LINK_INTERFACE(link), | |
2287 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
2288 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bridge->ifname), | |
2289 | NULL), | |
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), | |
2301 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->vrf->ifname), | |
2302 | NULL); | |
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), | |
2308 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->vrf->ifname), | |
2309 | NULL); | |
2310 | link_enter_failed(link); | |
2311 | return r; | |
2312 | } | |
2313 | ||
2314 | link->enslaving++; | |
2315 | } | |
2316 | ||
6a0a2f86 | 2317 | HASHMAP_FOREACH(netdev, link->network->stacked_netdevs, i) { |
7951dea2 | 2318 | |
a63e5daa SS |
2319 | if (netdev->ifindex > 0) { |
2320 | link_joined(link); | |
2321 | continue; | |
2322 | } | |
2323 | ||
f2341e0a LP |
2324 | log_struct(LOG_DEBUG, |
2325 | LOG_LINK_INTERFACE(link), | |
2326 | LOG_NETDEV_INTERFACE(netdev), | |
2327 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", netdev->ifname), | |
2328 | NULL); | |
2329 | ||
2330 | r = netdev_join(netdev, link, netdev_join_handler); | |
7951dea2 | 2331 | if (r < 0) { |
f2341e0a LP |
2332 | log_struct_errno(LOG_WARNING, r, |
2333 | LOG_LINK_INTERFACE(link), | |
2334 | LOG_NETDEV_INTERFACE(netdev), | |
2335 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", netdev->ifname), | |
2336 | NULL); | |
326cb406 SS |
2337 | link_enter_failed(link); |
2338 | return r; | |
2339 | } | |
2340 | ||
313cefa1 | 2341 | link->enslaving++; |
326cb406 SS |
2342 | } |
2343 | ||
ef1ba606 TG |
2344 | return 0; |
2345 | } | |
2346 | ||
769d324c | 2347 | static int link_set_ipv4_forward(Link *link) { |
5a8bcb67 LP |
2348 | int r; |
2349 | ||
765afd5c | 2350 | if (!link_ipv4_forward_enabled(link)) |
15dee3f0 LP |
2351 | return 0; |
2352 | ||
765afd5c LP |
2353 | /* We propagate the forwarding flag from one interface to the |
2354 | * global setting one way. This means: as long as at least one | |
2355 | * interface was configured at any time that had IP forwarding | |
2356 | * enabled the setting will stay on for good. We do this | |
2357 | * primarily to keep IPv4 and IPv6 packet forwarding behaviour | |
2358 | * somewhat in sync (see below). */ | |
15dee3f0 | 2359 | |
765afd5c | 2360 | r = write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
eb3da901 | 2361 | if (r < 0) |
765afd5c | 2362 | log_link_warning_errno(link, r, "Cannot turn on IPv4 packet forwarding, ignoring: %m"); |
43c6d5ab | 2363 | |
769d324c LP |
2364 | return 0; |
2365 | } | |
2366 | ||
2367 | static int link_set_ipv6_forward(Link *link) { | |
769d324c LP |
2368 | int r; |
2369 | ||
765afd5c | 2370 | if (!link_ipv6_forward_enabled(link)) |
8add5f79 NO |
2371 | return 0; |
2372 | ||
61233823 | 2373 | /* On Linux, the IPv6 stack does not know a per-interface |
765afd5c LP |
2374 | * packet forwarding setting: either packet forwarding is on |
2375 | * for all, or off for all. We hence don't bother with a | |
2376 | * per-interface setting, but simply propagate the interface | |
2377 | * flag, if it is set, to the global flag, one-way. Note that | |
2378 | * while IPv4 would allow a per-interface flag, we expose the | |
2379 | * same behaviour there and also propagate the setting from | |
2380 | * one to all, to keep things simple (see above). */ | |
15dee3f0 | 2381 | |
765afd5c | 2382 | r = write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
eb3da901 | 2383 | if (r < 0) |
765afd5c | 2384 | log_link_warning_errno(link, r, "Cannot configure IPv6 packet forwarding, ignoring: %m"); |
5a8bcb67 LP |
2385 | |
2386 | return 0; | |
2387 | } | |
2388 | ||
49092e22 | 2389 | static int link_set_ipv6_privacy_extensions(Link *link) { |
1f0d9695 LP |
2390 | char buf[DECIMAL_STR_MAX(unsigned) + 1]; |
2391 | IPv6PrivacyExtensions s; | |
49092e22 SS |
2392 | const char *p = NULL; |
2393 | int r; | |
2394 | ||
1f0d9695 | 2395 | s = link_ipv6_privacy_extensions(link); |
66a6bd68 | 2396 | if (s < 0) |
49092e22 SS |
2397 | return 0; |
2398 | ||
2399 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/use_tempaddr"); | |
66a6bd68 | 2400 | xsprintf(buf, "%u", (unsigned) link->network->ipv6_privacy_extensions); |
49092e22 | 2401 | |
eb3da901 LP |
2402 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
2403 | if (r < 0) | |
49092e22 SS |
2404 | log_link_warning_errno(link, r, "Cannot configure IPv6 privacy extension for interface: %m"); |
2405 | ||
2406 | return 0; | |
2407 | } | |
2408 | ||
4f2e437a | 2409 | static int link_set_ipv6_accept_ra(Link *link) { |
3b015d40 | 2410 | const char *p = NULL; |
4f2e437a SS |
2411 | int r; |
2412 | ||
2413 | /* Make this a NOP if IPv6 is not available */ | |
2414 | if (!socket_ipv6_is_supported()) | |
2415 | return 0; | |
2416 | ||
2417 | if (link->flags & IFF_LOOPBACK) | |
2418 | return 0; | |
2419 | ||
d68e2e59 LP |
2420 | if (!link->network) |
2421 | return 0; | |
2422 | ||
4f2e437a | 2423 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/accept_ra"); |
4f2e437a | 2424 | |
0053598f | 2425 | /* We handle router advertisements ourselves, tell the kernel to GTFO */ |
fe307276 | 2426 | r = write_string_file(p, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
eb3da901 | 2427 | if (r < 0) |
fe307276 | 2428 | log_link_warning_errno(link, r, "Cannot disable kernel IPv6 accept_ra for interface: %m"); |
4f2e437a SS |
2429 | |
2430 | return 0; | |
2431 | } | |
2432 | ||
8749cbcd | 2433 | static int link_set_ipv6_dad_transmits(Link *link) { |
66a6bd68 | 2434 | char buf[DECIMAL_STR_MAX(int) + 1]; |
8749cbcd SS |
2435 | const char *p = NULL; |
2436 | int r; | |
2437 | ||
2438 | /* Make this a NOP if IPv6 is not available */ | |
2439 | if (!socket_ipv6_is_supported()) | |
2440 | return 0; | |
2441 | ||
2442 | if (link->flags & IFF_LOOPBACK) | |
2443 | return 0; | |
2444 | ||
d68e2e59 LP |
2445 | if (!link->network) |
2446 | return 0; | |
2447 | ||
8749cbcd SS |
2448 | if (link->network->ipv6_dad_transmits < 0) |
2449 | return 0; | |
2450 | ||
2451 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/dad_transmits"); | |
66a6bd68 | 2452 | xsprintf(buf, "%i", link->network->ipv6_dad_transmits); |
8749cbcd | 2453 | |
eb3da901 LP |
2454 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
2455 | if (r < 0) | |
8749cbcd | 2456 | log_link_warning_errno(link, r, "Cannot set IPv6 dad transmits for interface: %m"); |
8749cbcd SS |
2457 | |
2458 | return 0; | |
2459 | } | |
2460 | ||
b69c3180 | 2461 | static int link_set_ipv6_hop_limit(Link *link) { |
66a6bd68 | 2462 | char buf[DECIMAL_STR_MAX(int) + 1]; |
b69c3180 SS |
2463 | const char *p = NULL; |
2464 | int r; | |
2465 | ||
2466 | /* Make this a NOP if IPv6 is not available */ | |
2467 | if (!socket_ipv6_is_supported()) | |
2468 | return 0; | |
2469 | ||
2470 | if (link->flags & IFF_LOOPBACK) | |
2471 | return 0; | |
2472 | ||
d68e2e59 LP |
2473 | if (!link->network) |
2474 | return 0; | |
2475 | ||
b69c3180 SS |
2476 | if (link->network->ipv6_hop_limit < 0) |
2477 | return 0; | |
2478 | ||
2479 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/hop_limit"); | |
66a6bd68 | 2480 | xsprintf(buf, "%i", link->network->ipv6_hop_limit); |
b69c3180 | 2481 | |
eb3da901 LP |
2482 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
2483 | if (r < 0) | |
b69c3180 | 2484 | log_link_warning_errno(link, r, "Cannot set IPv6 hop limit for interface: %m"); |
b69c3180 SS |
2485 | |
2486 | return 0; | |
2487 | } | |
2488 | ||
11102cba SS |
2489 | static int link_set_ipv6_mtu(Link *link) { |
2490 | char buf[DECIMAL_STR_MAX(unsigned) + 1]; | |
2491 | const char *p = NULL; | |
2492 | int r; | |
2493 | ||
2494 | /* Make this a NOP if IPv6 is not available */ | |
2495 | if (!socket_ipv6_is_supported()) | |
2496 | return 0; | |
2497 | ||
2498 | if (link->flags & IFF_LOOPBACK) | |
2499 | return 0; | |
2500 | ||
2501 | if (link->network->ipv6_mtu == 0) | |
2502 | return 0; | |
2503 | ||
2504 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/mtu"); | |
2505 | ||
4e964aa0 | 2506 | xsprintf(buf, "%" PRIu32, link->network->ipv6_mtu); |
11102cba SS |
2507 | |
2508 | r = write_string_file(p, buf, 0); | |
2509 | if (r < 0) | |
2510 | log_link_warning_errno(link, r, "Cannot set IPv6 MTU for interface: %m"); | |
2511 | ||
2512 | return 0; | |
2513 | } | |
2514 | ||
5e5b137a TG |
2515 | static int link_drop_foreign_config(Link *link) { |
2516 | Address *address; | |
2517 | Route *route; | |
2518 | Iterator i; | |
2519 | int r; | |
2520 | ||
2521 | SET_FOREACH(address, link->addresses_foreign, i) { | |
fe307276 | 2522 | /* we consider IPv6LL addresses to be managed by the kernel */ |
5e5b137a TG |
2523 | if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1) |
2524 | continue; | |
2525 | ||
2526 | r = address_remove(address, link, link_address_remove_handler); | |
2527 | if (r < 0) | |
2528 | return r; | |
2529 | } | |
2530 | ||
2531 | SET_FOREACH(route, link->routes_foreign, i) { | |
fe307276 | 2532 | /* do not touch routes managed by the kernel */ |
5e5b137a TG |
2533 | if (route->protocol == RTPROT_KERNEL) |
2534 | continue; | |
2535 | ||
3fb1ac5d | 2536 | r = route_remove(route, link, link_route_remove_handler); |
5e5b137a TG |
2537 | if (r < 0) |
2538 | return r; | |
2539 | } | |
2540 | ||
2541 | return 0; | |
2542 | } | |
2543 | ||
3104883d | 2544 | static int link_drop_config(Link *link) { |
410a7f15 | 2545 | Address *address, *pool_address; |
3104883d SS |
2546 | Route *route; |
2547 | Iterator i; | |
2548 | int r; | |
2549 | ||
2550 | SET_FOREACH(address, link->addresses, i) { | |
2551 | /* we consider IPv6LL addresses to be managed by the kernel */ | |
2552 | if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1) | |
2553 | continue; | |
2554 | ||
2555 | r = address_remove(address, link, link_address_remove_handler); | |
2556 | if (r < 0) | |
2557 | return r; | |
410a7f15 RM |
2558 | |
2559 | /* If this address came from an address pool, clean up the pool */ | |
2560 | LIST_FOREACH(addresses, pool_address, link->pool_addresses) { | |
2561 | if (address_equal(address, pool_address)) { | |
2562 | LIST_REMOVE(addresses, link->pool_addresses, pool_address); | |
2563 | address_free(pool_address); | |
2564 | break; | |
2565 | } | |
2566 | } | |
3104883d SS |
2567 | } |
2568 | ||
2569 | SET_FOREACH(route, link->routes, i) { | |
2570 | /* do not touch routes managed by the kernel */ | |
2571 | if (route->protocol == RTPROT_KERNEL) | |
2572 | continue; | |
2573 | ||
2574 | r = route_remove(route, link, link_route_remove_handler); | |
2575 | if (r < 0) | |
2576 | return r; | |
2577 | } | |
2578 | ||
c69305ff LP |
2579 | ndisc_flush(link); |
2580 | ||
3104883d SS |
2581 | return 0; |
2582 | } | |
2583 | ||
273eec24 LP |
2584 | static int link_update_lldp(Link *link) { |
2585 | int r; | |
2586 | ||
2587 | assert(link); | |
2588 | ||
2589 | if (!link->lldp) | |
2590 | return 0; | |
2591 | ||
2592 | if (link->flags & IFF_UP) { | |
2593 | r = sd_lldp_start(link->lldp); | |
2594 | if (r > 0) | |
2595 | log_link_debug(link, "Started LLDP."); | |
2596 | } else { | |
2597 | r = sd_lldp_stop(link->lldp); | |
2598 | if (r > 0) | |
2599 | log_link_debug(link, "Stopped LLDP."); | |
2600 | } | |
2601 | ||
2602 | return r; | |
2603 | } | |
2604 | ||
a748b692 | 2605 | static int link_configure(Link *link) { |
02b59d57 TG |
2606 | int r; |
2607 | ||
ef1ba606 | 2608 | assert(link); |
b22d8a00 | 2609 | assert(link->network); |
8434fd5c | 2610 | assert(link->state == LINK_STATE_PENDING); |
a748b692 | 2611 | |
92c918b0 SS |
2612 | if (streq_ptr(link->kind, "vcan")) { |
2613 | ||
2614 | if (!(link->flags & IFF_UP)) { | |
2615 | r = link_up_can(link); | |
2616 | if (r < 0) { | |
2617 | link_enter_failed(link); | |
2618 | return r; | |
2619 | } | |
2620 | } | |
2621 | ||
2622 | return 0; | |
2623 | } | |
2624 | ||
02e28621 ДГ |
2625 | /* Drop foreign config, but ignore loopback or critical devices. |
2626 | * We do not want to remove loopback address or addresses used for root NFS. */ | |
2627 | if (!(link->flags & IFF_LOOPBACK) && !(link->network->dhcp_critical)) { | |
e5d44b34 CH |
2628 | r = link_drop_foreign_config(link); |
2629 | if (r < 0) | |
2630 | return r; | |
2631 | } | |
5e5b137a | 2632 | |
23d8b221 SS |
2633 | r = link_set_proxy_arp(link); |
2634 | if (r < 0) | |
2635 | return r; | |
2636 | ||
a0e5c15d FK |
2637 | r = ipv6_proxy_ndp_addresses_configure(link); |
2638 | if (r < 0) | |
2639 | return r; | |
2640 | ||
769d324c LP |
2641 | r = link_set_ipv4_forward(link); |
2642 | if (r < 0) | |
2643 | return r; | |
2644 | ||
2645 | r = link_set_ipv6_forward(link); | |
5a8bcb67 LP |
2646 | if (r < 0) |
2647 | return r; | |
2648 | ||
49092e22 SS |
2649 | r = link_set_ipv6_privacy_extensions(link); |
2650 | if (r < 0) | |
2651 | return r; | |
2652 | ||
4f2e437a SS |
2653 | r = link_set_ipv6_accept_ra(link); |
2654 | if (r < 0) | |
2655 | return r; | |
2656 | ||
8749cbcd SS |
2657 | r = link_set_ipv6_dad_transmits(link); |
2658 | if (r < 0) | |
2659 | return r; | |
b69c3180 SS |
2660 | |
2661 | r = link_set_ipv6_hop_limit(link); | |
2662 | if (r < 0) | |
2663 | return r; | |
8749cbcd | 2664 | |
99d2baa2 SS |
2665 | r = link_set_flags(link); |
2666 | if (r < 0) | |
2667 | return r; | |
2668 | ||
11102cba SS |
2669 | r = link_set_ipv6_mtu(link); |
2670 | if (r < 0) | |
2671 | return r; | |
2672 | ||
78c958f8 | 2673 | if (link_ipv4ll_enabled(link)) { |
b22d8a00 | 2674 | r = ipv4ll_configure(link); |
eb34d4af TG |
2675 | if (r < 0) |
2676 | return r; | |
2677 | } | |
2678 | ||
78c958f8 | 2679 | if (link_dhcp4_enabled(link)) { |
64b21ece MV |
2680 | r = dhcp4_set_promote_secondaries(link); |
2681 | if (r < 0) | |
2682 | return r; | |
2683 | ||
3c9b8860 | 2684 | r = dhcp4_configure(link); |
eb34d4af TG |
2685 | if (r < 0) |
2686 | return r; | |
eb34d4af TG |
2687 | } |
2688 | ||
78c958f8 | 2689 | if (link_dhcp4_server_enabled(link)) { |
dd43110f TG |
2690 | r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex); |
2691 | if (r < 0) | |
2692 | return r; | |
2693 | ||
2694 | r = sd_dhcp_server_attach_event(link->dhcp_server, NULL, 0); | |
2695 | if (r < 0) | |
2696 | return r; | |
dd43110f TG |
2697 | } |
2698 | ||
62379e88 TG |
2699 | if (link_dhcp6_enabled(link) || |
2700 | link_ipv6_accept_ra_enabled(link)) { | |
f5a8c43f TG |
2701 | r = dhcp6_configure(link); |
2702 | if (r < 0) | |
2703 | return r; | |
2704 | } | |
2705 | ||
2706 | if (link_ipv6_accept_ra_enabled(link)) { | |
de1e9928 | 2707 | r = ndisc_configure(link); |
4138fb2c PF |
2708 | if (r < 0) |
2709 | return r; | |
2710 | } | |
2711 | ||
7465dd22 PF |
2712 | if (link_radv_enabled(link)) { |
2713 | r = radv_configure(link); | |
2714 | if (r < 0) | |
2715 | return r; | |
2716 | } | |
2717 | ||
8e1ad1ea | 2718 | if (link_lldp_rx_enabled(link)) { |
fc6a313b LP |
2719 | r = sd_lldp_new(&link->lldp); |
2720 | if (r < 0) | |
2721 | return r; | |
2722 | ||
2723 | r = sd_lldp_set_ifindex(link->lldp, link->ifindex); | |
34437b4f LP |
2724 | if (r < 0) |
2725 | return r; | |
2726 | ||
2727 | r = sd_lldp_match_capabilities(link->lldp, | |
2728 | link->network->lldp_mode == LLDP_MODE_ROUTERS_ONLY ? | |
6afa6767 BG |
2729 | SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS : |
2730 | SD_LLDP_SYSTEM_CAPABILITIES_ALL); | |
ce43e484 SS |
2731 | if (r < 0) |
2732 | return r; | |
2733 | ||
b553a6b1 LP |
2734 | r = sd_lldp_set_filter_address(link->lldp, &link->mac); |
2735 | if (r < 0) | |
2736 | return r; | |
2737 | ||
ce43e484 SS |
2738 | r = sd_lldp_attach_event(link->lldp, NULL, 0); |
2739 | if (r < 0) | |
2740 | return r; | |
49699bac | 2741 | |
032b27f5 | 2742 | r = sd_lldp_set_callback(link->lldp, lldp_handler, link); |
49699bac SS |
2743 | if (r < 0) |
2744 | return r; | |
273eec24 LP |
2745 | |
2746 | r = link_update_lldp(link); | |
2747 | if (r < 0) | |
2748 | return r; | |
ce43e484 SS |
2749 | } |
2750 | ||
dad2d78e | 2751 | if (link_has_carrier(link) || link->network->configure_without_carrier) { |
1e9be60b TG |
2752 | r = link_acquire_conf(link); |
2753 | if (r < 0) | |
2754 | return r; | |
cc544d5f | 2755 | } |
1e9be60b | 2756 | |
3f265037 | 2757 | return link_enter_join_netdev(link); |
505f8da7 TG |
2758 | } |
2759 | ||
1c4baffc | 2760 | static int link_initialized_and_synced(sd_netlink *rtnl, sd_netlink_message *m, |
3c9b8860 | 2761 | void *userdata) { |
8e766630 | 2762 | _cleanup_(link_unrefp) Link *link = userdata; |
505f8da7 | 2763 | Network *network; |
505f8da7 TG |
2764 | int r; |
2765 | ||
2766 | assert(link); | |
2767 | assert(link->ifname); | |
2768 | assert(link->manager); | |
2769 | ||
8434fd5c | 2770 | if (link->state != LINK_STATE_PENDING) |
5da8149f | 2771 | return 1; |
505f8da7 | 2772 | |
6a7a4e4d | 2773 | log_link_debug(link, "Link state is up-to-date"); |
505f8da7 | 2774 | |
0d4ad91d AR |
2775 | r = link_new_bound_by_list(link); |
2776 | if (r < 0) | |
2777 | return r; | |
2778 | ||
2779 | r = link_handle_bound_by_list(link); | |
2780 | if (r < 0) | |
2781 | return r; | |
2782 | ||
c4a03a56 TG |
2783 | if (!link->network) { |
2784 | r = network_get(link->manager, link->udev_device, link->ifname, | |
2785 | &link->mac, &network); | |
2786 | if (r == -ENOENT) { | |
2787 | link_enter_unmanaged(link); | |
2788 | return 1; | |
a09dc546 DM |
2789 | } else if (r == 0 && network->unmanaged) { |
2790 | link_enter_unmanaged(link); | |
2791 | return 0; | |
c4a03a56 TG |
2792 | } else if (r < 0) |
2793 | return r; | |
505f8da7 | 2794 | |
c4a03a56 TG |
2795 | if (link->flags & IFF_LOOPBACK) { |
2796 | if (network->link_local != ADDRESS_FAMILY_NO) | |
2797 | log_link_debug(link, "Ignoring link-local autoconfiguration for loopback link"); | |
78c958f8 | 2798 | |
c4a03a56 TG |
2799 | if (network->dhcp != ADDRESS_FAMILY_NO) |
2800 | log_link_debug(link, "Ignoring DHCP clients for loopback link"); | |
78c958f8 | 2801 | |
c4a03a56 TG |
2802 | if (network->dhcp_server) |
2803 | log_link_debug(link, "Ignoring DHCP server for loopback link"); | |
2804 | } | |
bd2efe92 | 2805 | |
7d342c03 | 2806 | r = network_apply(network, link); |
c4a03a56 TG |
2807 | if (r < 0) |
2808 | return r; | |
2809 | } | |
505f8da7 | 2810 | |
0d4ad91d AR |
2811 | r = link_new_bound_to_list(link); |
2812 | if (r < 0) | |
2813 | return r; | |
2814 | ||
a748b692 TG |
2815 | r = link_configure(link); |
2816 | if (r < 0) | |
2817 | return r; | |
2818 | ||
5da8149f | 2819 | return 1; |
505f8da7 TG |
2820 | } |
2821 | ||
4f561e8e | 2822 | int link_initialized(Link *link, struct udev_device *device) { |
4afd3348 | 2823 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
4f561e8e TG |
2824 | int r; |
2825 | ||
2826 | assert(link); | |
2827 | assert(link->manager); | |
2828 | assert(link->manager->rtnl); | |
2829 | assert(device); | |
2830 | ||
8434fd5c | 2831 | if (link->state != LINK_STATE_PENDING) |
4f561e8e TG |
2832 | return 0; |
2833 | ||
679b3605 TG |
2834 | if (link->udev_device) |
2835 | return 0; | |
2836 | ||
79008bdd | 2837 | log_link_debug(link, "udev initialized link"); |
4f561e8e TG |
2838 | |
2839 | link->udev_device = udev_device_ref(device); | |
2840 | ||
3c9b8860 TG |
2841 | /* udev has initialized the link, but we don't know if we have yet |
2842 | * processed the NEWLINK messages with the latest state. Do a GETLINK, | |
2843 | * when it returns we know that the pending NEWLINKs have already been | |
2844 | * processed and that we are up-to-date */ | |
4f561e8e | 2845 | |
3c9b8860 TG |
2846 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, |
2847 | link->ifindex); | |
4f561e8e TG |
2848 | if (r < 0) |
2849 | return r; | |
2850 | ||
1c4baffc | 2851 | r = sd_netlink_call_async(link->manager->rtnl, req, |
7b872092 | 2852 | link_initialized_and_synced, link, 0, NULL); |
4f561e8e TG |
2853 | if (r < 0) |
2854 | return r; | |
2855 | ||
5da8149f TG |
2856 | link_ref(link); |
2857 | ||
4f561e8e TG |
2858 | return 0; |
2859 | } | |
2860 | ||
c4a03a56 | 2861 | static int link_load(Link *link) { |
0bc70f1d TG |
2862 | _cleanup_free_ char *network_file = NULL, |
2863 | *addresses = NULL, | |
f703cc2c | 2864 | *routes = NULL, |
0bc70f1d TG |
2865 | *dhcp4_address = NULL, |
2866 | *ipv4ll_address = NULL; | |
2867 | union in_addr_union address; | |
f703cc2c | 2868 | union in_addr_union route_dst; |
c598ac76 | 2869 | const char *p; |
c4a03a56 TG |
2870 | int r; |
2871 | ||
2872 | assert(link); | |
2873 | ||
1a5a177e | 2874 | r = parse_env_file(NULL, link->state_file, NEWLINE, |
c4a03a56 TG |
2875 | "NETWORK_FILE", &network_file, |
2876 | "ADDRESSES", &addresses, | |
f703cc2c | 2877 | "ROUTES", &routes, |
0bc70f1d TG |
2878 | "DHCP4_ADDRESS", &dhcp4_address, |
2879 | "IPV4LL_ADDRESS", &ipv4ll_address, | |
c4a03a56 TG |
2880 | NULL); |
2881 | if (r < 0 && r != -ENOENT) | |
2882 | return log_link_error_errno(link, r, "Failed to read %s: %m", link->state_file); | |
2883 | ||
2884 | if (network_file) { | |
2885 | Network *network; | |
2886 | char *suffix; | |
2887 | ||
2888 | /* drop suffix */ | |
2889 | suffix = strrchr(network_file, '.'); | |
2890 | if (!suffix) { | |
2891 | log_link_debug(link, "Failed to get network name from %s", network_file); | |
2892 | goto network_file_fail; | |
2893 | } | |
2894 | *suffix = '\0'; | |
2895 | ||
2896 | r = network_get_by_name(link->manager, basename(network_file), &network); | |
2897 | if (r < 0) { | |
2898 | log_link_debug_errno(link, r, "Failed to get network %s: %m", basename(network_file)); | |
2899 | goto network_file_fail; | |
2900 | } | |
2901 | ||
7d342c03 | 2902 | r = network_apply(network, link); |
c4a03a56 TG |
2903 | if (r < 0) |
2904 | return log_link_error_errno(link, r, "Failed to apply network %s: %m", basename(network_file)); | |
2905 | } | |
2906 | ||
2907 | network_file_fail: | |
2908 | ||
2909 | if (addresses) { | |
c598ac76 | 2910 | p = addresses; |
c4a03a56 | 2911 | |
c598ac76 TG |
2912 | for (;;) { |
2913 | _cleanup_free_ char *address_str = NULL; | |
c4a03a56 TG |
2914 | char *prefixlen_str; |
2915 | int family; | |
2916 | unsigned char prefixlen; | |
c4a03a56 | 2917 | |
c598ac76 TG |
2918 | r = extract_first_word(&p, &address_str, NULL, 0); |
2919 | if (r < 0) { | |
2920 | log_link_debug_errno(link, r, "Failed to extract next address string: %m"); | |
2921 | continue; | |
928bc597 TA |
2922 | } |
2923 | if (r == 0) | |
c598ac76 TG |
2924 | break; |
2925 | ||
2926 | prefixlen_str = strchr(address_str, '/'); | |
c4a03a56 | 2927 | if (!prefixlen_str) { |
c598ac76 | 2928 | log_link_debug(link, "Failed to parse address and prefix length %s", address_str); |
c4a03a56 TG |
2929 | continue; |
2930 | } | |
2931 | ||
313cefa1 | 2932 | *prefixlen_str++ = '\0'; |
c4a03a56 TG |
2933 | |
2934 | r = sscanf(prefixlen_str, "%hhu", &prefixlen); | |
2935 | if (r != 1) { | |
2936 | log_link_error(link, "Failed to parse prefixlen %s", prefixlen_str); | |
2937 | continue; | |
2938 | } | |
2939 | ||
c598ac76 | 2940 | r = in_addr_from_string_auto(address_str, &family, &address); |
c4a03a56 | 2941 | if (r < 0) { |
c598ac76 | 2942 | log_link_debug_errno(link, r, "Failed to parse address %s: %m", address_str); |
c4a03a56 TG |
2943 | continue; |
2944 | } | |
2945 | ||
2946 | r = address_add(link, family, &address, prefixlen, NULL); | |
2947 | if (r < 0) | |
2948 | return log_link_error_errno(link, r, "Failed to add address: %m"); | |
2949 | } | |
2950 | } | |
2951 | ||
f703cc2c | 2952 | if (routes) { |
74544b4e TA |
2953 | p = routes; |
2954 | ||
c598ac76 | 2955 | for (;;) { |
f833694d | 2956 | Route *route; |
c598ac76 | 2957 | _cleanup_free_ char *route_str = NULL; |
4afd3348 | 2958 | _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL; |
f833694d | 2959 | usec_t lifetime; |
f703cc2c TG |
2960 | char *prefixlen_str; |
2961 | int family; | |
2962 | unsigned char prefixlen, tos, table; | |
2963 | uint32_t priority; | |
2964 | ||
c598ac76 TG |
2965 | r = extract_first_word(&p, &route_str, NULL, 0); |
2966 | if (r < 0) { | |
2967 | log_link_debug_errno(link, r, "Failed to extract next route string: %m"); | |
2968 | continue; | |
928bc597 TA |
2969 | } |
2970 | if (r == 0) | |
c598ac76 TG |
2971 | break; |
2972 | ||
2973 | prefixlen_str = strchr(route_str, '/'); | |
f703cc2c | 2974 | if (!prefixlen_str) { |
c598ac76 | 2975 | log_link_debug(link, "Failed to parse route %s", route_str); |
f703cc2c TG |
2976 | continue; |
2977 | } | |
2978 | ||
313cefa1 | 2979 | *prefixlen_str++ = '\0'; |
f703cc2c | 2980 | |
f833694d TG |
2981 | r = sscanf(prefixlen_str, "%hhu/%hhu/%"SCNu32"/%hhu/"USEC_FMT, &prefixlen, &tos, &priority, &table, &lifetime); |
2982 | if (r != 5) { | |
2983 | log_link_debug(link, | |
2984 | "Failed to parse destination prefix length, tos, priority, table or expiration %s", | |
2985 | prefixlen_str); | |
f703cc2c TG |
2986 | continue; |
2987 | } | |
2988 | ||
c598ac76 | 2989 | r = in_addr_from_string_auto(route_str, &family, &route_dst); |
f703cc2c | 2990 | if (r < 0) { |
c598ac76 | 2991 | log_link_debug_errno(link, r, "Failed to parse route destination %s: %m", route_str); |
f703cc2c TG |
2992 | continue; |
2993 | } | |
2994 | ||
f833694d | 2995 | r = route_add(link, family, &route_dst, prefixlen, tos, priority, table, &route); |
f703cc2c TG |
2996 | if (r < 0) |
2997 | return log_link_error_errno(link, r, "Failed to add route: %m"); | |
f833694d | 2998 | |
f02ba163 | 2999 | if (lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) { |
f833694d TG |
3000 | r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(), lifetime, |
3001 | 0, route_expire_handler, route); | |
3002 | if (r < 0) | |
3003 | log_link_warning_errno(link, r, "Could not arm route expiration handler: %m"); | |
3004 | } | |
3005 | ||
3006 | route->lifetime = lifetime; | |
3007 | sd_event_source_unref(route->expire); | |
1cc6c93a | 3008 | route->expire = TAKE_PTR(expire); |
f703cc2c TG |
3009 | } |
3010 | } | |
3011 | ||
0bc70f1d TG |
3012 | if (dhcp4_address) { |
3013 | r = in_addr_from_string(AF_INET, dhcp4_address, &address); | |
3014 | if (r < 0) { | |
b68d26b8 | 3015 | log_link_debug_errno(link, r, "Failed to parse DHCPv4 address %s: %m", dhcp4_address); |
0bc70f1d TG |
3016 | goto dhcp4_address_fail; |
3017 | } | |
3018 | ||
b1c626f6 | 3019 | r = sd_dhcp_client_new(&link->dhcp_client, link->network ? link->network->dhcp_anonymize : 0); |
0bc70f1d | 3020 | if (r < 0) |
b68d26b8 | 3021 | return log_link_error_errno(link, r, "Failed to create DHCPv4 client: %m"); |
0bc70f1d TG |
3022 | |
3023 | r = sd_dhcp_client_set_request_address(link->dhcp_client, &address.in); | |
3024 | if (r < 0) | |
b68d26b8 | 3025 | return log_link_error_errno(link, r, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address); |
0bc70f1d TG |
3026 | } |
3027 | ||
3028 | dhcp4_address_fail: | |
3029 | ||
3030 | if (ipv4ll_address) { | |
3031 | r = in_addr_from_string(AF_INET, ipv4ll_address, &address); | |
3032 | if (r < 0) { | |
b68d26b8 | 3033 | log_link_debug_errno(link, r, "Failed to parse IPv4LL address %s: %m", ipv4ll_address); |
0bc70f1d TG |
3034 | goto ipv4ll_address_fail; |
3035 | } | |
3036 | ||
3037 | r = sd_ipv4ll_new(&link->ipv4ll); | |
3038 | if (r < 0) | |
b68d26b8 | 3039 | return log_link_error_errno(link, r, "Failed to create IPv4LL client: %m"); |
0bc70f1d TG |
3040 | |
3041 | r = sd_ipv4ll_set_address(link->ipv4ll, &address.in); | |
3042 | if (r < 0) | |
b68d26b8 | 3043 | return log_link_error_errno(link, r, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address); |
0bc70f1d TG |
3044 | } |
3045 | ||
3046 | ipv4ll_address_fail: | |
3047 | ||
c4a03a56 TG |
3048 | return 0; |
3049 | } | |
3050 | ||
1c4baffc | 3051 | int link_add(Manager *m, sd_netlink_message *message, Link **ret) { |
505f8da7 | 3052 | Link *link; |
8e766630 | 3053 | _cleanup_(udev_device_unrefp) struct udev_device *device = NULL; |
505f8da7 TG |
3054 | char ifindex_str[2 + DECIMAL_STR_MAX(int)]; |
3055 | int r; | |
3056 | ||
3057 | assert(m); | |
fbbeb65a | 3058 | assert(m->rtnl); |
505f8da7 TG |
3059 | assert(message); |
3060 | assert(ret); | |
3061 | ||
3062 | r = link_new(m, message, ret); | |
3063 | if (r < 0) | |
3064 | return r; | |
3065 | ||
3066 | link = *ret; | |
3067 | ||
6a7a4e4d | 3068 | log_link_debug(link, "Link %d added", link->ifindex); |
505f8da7 | 3069 | |
c4a03a56 TG |
3070 | r = link_load(link); |
3071 | if (r < 0) | |
3072 | return r; | |
3073 | ||
75f86906 | 3074 | if (detect_container() <= 0) { |
505f8da7 | 3075 | /* not in a container, udev will be around */ |
ae06ab10 | 3076 | sprintf(ifindex_str, "n%d", link->ifindex); |
505f8da7 | 3077 | device = udev_device_new_from_device_id(m->udev, ifindex_str); |
5c416fc4 TG |
3078 | if (!device) { |
3079 | r = log_link_warning_errno(link, errno, "Could not find udev device: %m"); | |
3080 | goto failed; | |
3081 | } | |
505f8da7 | 3082 | |
3c4cb064 | 3083 | if (udev_device_get_is_initialized(device) <= 0) { |
505f8da7 | 3084 | /* not yet ready */ |
79008bdd | 3085 | log_link_debug(link, "link pending udev initialization..."); |
505f8da7 | 3086 | return 0; |
3c4cb064 | 3087 | } |
505f8da7 | 3088 | |
4f561e8e TG |
3089 | r = link_initialized(link, device); |
3090 | if (r < 0) | |
5c416fc4 | 3091 | goto failed; |
4f561e8e | 3092 | } else { |
5da8149f TG |
3093 | /* we are calling a callback directly, so must take a ref */ |
3094 | link_ref(link); | |
3095 | ||
4f561e8e TG |
3096 | r = link_initialized_and_synced(m->rtnl, NULL, link); |
3097 | if (r < 0) | |
5c416fc4 | 3098 | goto failed; |
4f561e8e | 3099 | } |
505f8da7 | 3100 | |
a748b692 | 3101 | return 0; |
5c416fc4 TG |
3102 | failed: |
3103 | link_enter_failed(link); | |
3104 | return r; | |
a748b692 TG |
3105 | } |
3106 | ||
c601ebf7 | 3107 | int link_ipv6ll_gained(Link *link, const struct in6_addr *address) { |
e7ab854c TG |
3108 | int r; |
3109 | ||
3110 | assert(link); | |
3111 | ||
3112 | log_link_info(link, "Gained IPv6LL"); | |
3113 | ||
c601ebf7 | 3114 | link->ipv6ll_address = *address; |
e7ab854c TG |
3115 | link_check_ready(link); |
3116 | ||
201f0c91 | 3117 | if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_UNMANAGED, LINK_STATE_FAILED)) { |
e7ab854c TG |
3118 | r = link_acquire_ipv6_conf(link); |
3119 | if (r < 0) { | |
3120 | link_enter_failed(link); | |
3121 | return r; | |
3122 | } | |
3123 | } | |
3124 | ||
3125 | return 0; | |
3126 | } | |
3127 | ||
9c0a72f9 TG |
3128 | static int link_carrier_gained(Link *link) { |
3129 | int r; | |
3130 | ||
3131 | assert(link); | |
3132 | ||
201f0c91 | 3133 | if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_UNMANAGED, LINK_STATE_FAILED)) { |
9c0a72f9 TG |
3134 | r = link_acquire_conf(link); |
3135 | if (r < 0) { | |
3136 | link_enter_failed(link); | |
3137 | return r; | |
3138 | } | |
6fc25497 SS |
3139 | |
3140 | r = link_enter_set_addresses(link); | |
3141 | if (r < 0) | |
3142 | return r; | |
9c0a72f9 TG |
3143 | } |
3144 | ||
0d4ad91d AR |
3145 | r = link_handle_bound_by_list(link); |
3146 | if (r < 0) | |
3147 | return r; | |
3148 | ||
9c0a72f9 TG |
3149 | return 0; |
3150 | } | |
3151 | ||
3152 | static int link_carrier_lost(Link *link) { | |
3153 | int r; | |
3154 | ||
3155 | assert(link); | |
3156 | ||
fbdb6605 SS |
3157 | /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop. |
3158 | setting_mtu keep track whether the device got reset because of setting MTU and does not drop the | |
3159 | configuration and stop the clients as well. */ | |
97e7fb39 SS |
3160 | if (link->setting_mtu) |
3161 | return 0; | |
3162 | ||
9c0a72f9 TG |
3163 | r = link_stop_clients(link); |
3164 | if (r < 0) { | |
3165 | link_enter_failed(link); | |
3166 | return r; | |
3167 | } | |
3168 | ||
28464ae0 SS |
3169 | if (link_dhcp4_server_enabled(link)) |
3170 | (void) sd_dhcp_server_stop(link->dhcp_server); | |
45a9eac9 | 3171 | |
3104883d SS |
3172 | r = link_drop_config(link); |
3173 | if (r < 0) | |
3174 | return r; | |
3175 | ||
f258e948 MP |
3176 | if (!IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING)) { |
3177 | log_link_debug(link, "State is %s, dropping config", link_state_to_string(link->state)); | |
c436d553 MM |
3178 | r = link_drop_foreign_config(link); |
3179 | if (r < 0) | |
3180 | return r; | |
3181 | } | |
3104883d | 3182 | |
0d4ad91d AR |
3183 | r = link_handle_bound_by_list(link); |
3184 | if (r < 0) | |
3185 | return r; | |
3186 | ||
9c0a72f9 TG |
3187 | return 0; |
3188 | } | |
3189 | ||
3190 | int link_carrier_reset(Link *link) { | |
3191 | int r; | |
3192 | ||
3193 | assert(link); | |
3194 | ||
3195 | if (link_has_carrier(link)) { | |
3196 | r = link_carrier_lost(link); | |
3197 | if (r < 0) | |
3198 | return r; | |
3199 | ||
3200 | r = link_carrier_gained(link); | |
3201 | if (r < 0) | |
3202 | return r; | |
3203 | ||
6a7a4e4d | 3204 | log_link_info(link, "Reset carrier"); |
9c0a72f9 TG |
3205 | } |
3206 | ||
3207 | return 0; | |
3208 | } | |
3209 | ||
1c4baffc | 3210 | int link_update(Link *link, sd_netlink_message *m) { |
c49b33ac | 3211 | struct ether_addr mac; |
ca4e095a | 3212 | const char *ifname; |
afe7fd56 | 3213 | uint32_t mtu; |
a61bb41c | 3214 | bool had_carrier, carrier_gained, carrier_lost; |
22936833 TG |
3215 | int r; |
3216 | ||
dd3efc09 | 3217 | assert(link); |
b8941f74 | 3218 | assert(link->ifname); |
22936833 TG |
3219 | assert(m); |
3220 | ||
7619683b TG |
3221 | if (link->state == LINK_STATE_LINGER) { |
3222 | link_ref(link); | |
6a7a4e4d | 3223 | log_link_info(link, "Link readded"); |
e331e246 | 3224 | link_set_state(link, LINK_STATE_ENSLAVING); |
0d4ad91d AR |
3225 | |
3226 | r = link_new_carrier_maps(link); | |
3227 | if (r < 0) | |
3228 | return r; | |
7619683b TG |
3229 | } |
3230 | ||
1c4baffc | 3231 | r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname); |
b8941f74 | 3232 | if (r >= 0 && !streq(ifname, link->ifname)) { |
aa2b10da | 3233 | log_link_info(link, "Interface name change detected, %s has been renamed to %s.", link->ifname, ifname); |
b8941f74 | 3234 | |
587c4586 YW |
3235 | if (link->state == LINK_STATE_PENDING) { |
3236 | r = free_and_strdup(&link->ifname, ifname); | |
3237 | if (r < 0) | |
3238 | return r; | |
3239 | } else { | |
3240 | Manager *manager = link->manager; | |
0d4ad91d | 3241 | |
587c4586 YW |
3242 | link_drop(link); |
3243 | r = link_add(manager, m, &link); | |
3244 | if (r < 0) | |
3245 | return r; | |
3246 | } | |
b8941f74 TG |
3247 | } |
3248 | ||
1c4baffc | 3249 | r = sd_netlink_message_read_u32(m, IFLA_MTU, &mtu); |
afe7fd56 TG |
3250 | if (r >= 0 && mtu > 0) { |
3251 | link->mtu = mtu; | |
4e964aa0 | 3252 | if (link->original_mtu == 0) { |
afe7fd56 | 3253 | link->original_mtu = mtu; |
6a7a4e4d | 3254 | log_link_debug(link, "Saved original MTU: %" PRIu32, link->original_mtu); |
afe7fd56 TG |
3255 | } |
3256 | ||
3257 | if (link->dhcp_client) { | |
3c9b8860 TG |
3258 | r = sd_dhcp_client_set_mtu(link->dhcp_client, |
3259 | link->mtu); | |
afe7fd56 | 3260 | if (r < 0) { |
6a7a4e4d | 3261 | log_link_warning_errno(link, r, "Could not update MTU in DHCP client: %m"); |
afe7fd56 TG |
3262 | return r; |
3263 | } | |
3264 | } | |
7465dd22 PF |
3265 | |
3266 | if (link->radv) { | |
3267 | r = sd_radv_set_mtu(link->radv, link->mtu); | |
3268 | if (r < 0) | |
3269 | return log_link_warning_errno(link, r, "Could not set MTU for Router Advertisement: %m"); | |
3270 | } | |
9842de0d | 3271 | } |
69629de9 | 3272 | |
e9189a1f TG |
3273 | /* The kernel may broadcast NEWLINK messages without the MAC address |
3274 | set, simply ignore them. */ | |
1c4baffc | 3275 | r = sd_netlink_message_read_ether_addr(m, IFLA_ADDRESS, &mac); |
e9189a1f | 3276 | if (r >= 0) { |
3c9b8860 TG |
3277 | if (memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet, |
3278 | ETH_ALEN)) { | |
c49b33ac | 3279 | |
3c9b8860 TG |
3280 | memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet, |
3281 | ETH_ALEN); | |
c49b33ac | 3282 | |
79008bdd | 3283 | log_link_debug(link, "MAC address: " |
20861203 TG |
3284 | "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", |
3285 | mac.ether_addr_octet[0], | |
3286 | mac.ether_addr_octet[1], | |
3287 | mac.ether_addr_octet[2], | |
3288 | mac.ether_addr_octet[3], | |
3289 | mac.ether_addr_octet[4], | |
3290 | mac.ether_addr_octet[5]); | |
c49b33ac | 3291 | |
20861203 TG |
3292 | if (link->ipv4ll) { |
3293 | r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac); | |
6a7a4e4d LP |
3294 | if (r < 0) |
3295 | return log_link_warning_errno(link, r, "Could not update MAC address in IPv4LL client: %m"); | |
c49b33ac | 3296 | } |
c49b33ac | 3297 | |
20861203 | 3298 | if (link->dhcp_client) { |
3c9b8860 | 3299 | r = sd_dhcp_client_set_mac(link->dhcp_client, |
76253e73 DW |
3300 | (const uint8_t *) &link->mac, |
3301 | sizeof (link->mac), | |
3302 | ARPHRD_ETHER); | |
6a7a4e4d LP |
3303 | if (r < 0) |
3304 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCP client: %m"); | |
413708d1 | 3305 | |
e4f05508 JR |
3306 | switch (link->network->dhcp_client_identifier) { |
3307 | case DHCP_CLIENT_ID_DUID: { | |
3308 | const DUID *duid = link_duid(link); | |
3309 | ||
3310 | r = sd_dhcp_client_set_iaid_duid(link->dhcp_client, | |
3311 | link->network->iaid, | |
3312 | duid->type, | |
3313 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
3314 | duid->raw_data_len); | |
3315 | if (r < 0) | |
3316 | return log_link_warning_errno(link, r, "Could not update DUID/IAID in DHCP client: %m"); | |
3317 | break; | |
3318 | } | |
dace710c YW |
3319 | case DHCP_CLIENT_ID_DUID_ONLY: { |
3320 | const DUID *duid = link_duid(link); | |
3321 | ||
3322 | r = sd_dhcp_client_set_duid(link->dhcp_client, | |
3323 | duid->type, | |
3324 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
3325 | duid->raw_data_len); | |
3326 | if (r < 0) | |
3327 | return log_link_warning_errno(link, r, "Could not update DUID in DHCP client: %m"); | |
3328 | break; | |
3329 | } | |
e4f05508 JR |
3330 | case DHCP_CLIENT_ID_MAC: |
3331 | r = sd_dhcp_client_set_client_id(link->dhcp_client, | |
3332 | ARPHRD_ETHER, | |
3333 | (const uint8_t *)&link->mac, | |
3334 | sizeof(link->mac)); | |
508f63b4 | 3335 | if (r < 0) |
e4f05508 JR |
3336 | return log_link_warning_errno(link, r, "Could not update MAC client id in DHCP client: %m"); |
3337 | break; | |
3338 | default: | |
3339 | assert_not_reached("Unknown client identifier type."); | |
3340 | } | |
c49b33ac | 3341 | } |
4138fb2c PF |
3342 | |
3343 | if (link->dhcp6_client) { | |
8341a5c3 ZJS |
3344 | const DUID* duid = link_duid(link); |
3345 | ||
4138fb2c | 3346 | r = sd_dhcp6_client_set_mac(link->dhcp6_client, |
76253e73 DW |
3347 | (const uint8_t *) &link->mac, |
3348 | sizeof (link->mac), | |
3349 | ARPHRD_ETHER); | |
6a7a4e4d LP |
3350 | if (r < 0) |
3351 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCPv6 client: %m"); | |
413708d1 VK |
3352 | |
3353 | r = sd_dhcp6_client_set_iaid(link->dhcp6_client, | |
3354 | link->network->iaid); | |
3355 | if (r < 0) | |
3356 | return log_link_warning_errno(link, r, "Could not update DHCPv6 IAID: %m"); | |
3357 | ||
8341a5c3 ZJS |
3358 | r = sd_dhcp6_client_set_duid(link->dhcp6_client, |
3359 | duid->type, | |
3360 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
3361 | duid->raw_data_len); | |
413708d1 VK |
3362 | if (r < 0) |
3363 | return log_link_warning_errno(link, r, "Could not update DHCPv6 DUID: %m"); | |
4138fb2c | 3364 | } |
7465dd22 PF |
3365 | |
3366 | if (link->radv) { | |
3367 | r = sd_radv_set_mac(link->radv, &link->mac); | |
3368 | if (r < 0) | |
3369 | return log_link_warning_errno(link, r, "Could not update MAC for Router Advertisement: %m"); | |
3370 | } | |
420d2058 SS |
3371 | |
3372 | if (link->ndisc) { | |
3373 | r = sd_ndisc_set_mac(link->ndisc, &link->mac); | |
3374 | if (r < 0) | |
3375 | return log_link_warning_errno(link, r, "Could not update MAC for ndisc: %m"); | |
3376 | } | |
c49b33ac | 3377 | } |
4f882b2a TG |
3378 | } |
3379 | ||
a61bb41c TG |
3380 | had_carrier = link_has_carrier(link); |
3381 | ||
3382 | r = link_update_flags(link, m); | |
3383 | if (r < 0) | |
3384 | return r; | |
3385 | ||
273eec24 LP |
3386 | r = link_update_lldp(link); |
3387 | if (r < 0) | |
3388 | return r; | |
3389 | ||
a61bb41c TG |
3390 | carrier_gained = !had_carrier && link_has_carrier(link); |
3391 | carrier_lost = had_carrier && !link_has_carrier(link); | |
3392 | ||
3393 | if (carrier_gained) { | |
6a7a4e4d | 3394 | log_link_info(link, "Gained carrier"); |
a61bb41c | 3395 | |
9c0a72f9 TG |
3396 | r = link_carrier_gained(link); |
3397 | if (r < 0) | |
3398 | return r; | |
a61bb41c | 3399 | } else if (carrier_lost) { |
6a7a4e4d | 3400 | log_link_info(link, "Lost carrier"); |
a61bb41c | 3401 | |
9c0a72f9 TG |
3402 | r = link_carrier_lost(link); |
3403 | if (r < 0) | |
a61bb41c | 3404 | return r; |
a61bb41c TG |
3405 | } |
3406 | ||
3407 | return 0; | |
dd3efc09 | 3408 | } |
fe8db0c5 | 3409 | |
b295beea LP |
3410 | static void print_link_hashmap(FILE *f, const char *prefix, Hashmap* h) { |
3411 | bool space = false; | |
3412 | Iterator i; | |
3413 | Link *link; | |
3414 | ||
3415 | assert(f); | |
3416 | assert(prefix); | |
3417 | ||
3418 | if (hashmap_isempty(h)) | |
3419 | return; | |
3420 | ||
0d536673 | 3421 | fputs(prefix, f); |
b295beea LP |
3422 | HASHMAP_FOREACH(link, h, i) { |
3423 | if (space) | |
0d536673 | 3424 | fputc(' ', f); |
b295beea LP |
3425 | |
3426 | fprintf(f, "%i", link->ifindex); | |
3427 | space = true; | |
3428 | } | |
3429 | ||
0d536673 | 3430 | fputc('\n', f); |
b295beea LP |
3431 | } |
3432 | ||
fe8db0c5 | 3433 | int link_save(Link *link) { |
68a8723c | 3434 | _cleanup_free_ char *temp_path = NULL; |
fe8db0c5 | 3435 | _cleanup_fclose_ FILE *f = NULL; |
e375dcde | 3436 | const char *admin_state, *oper_state; |
e7780c8d | 3437 | Address *a; |
c1eb9872 | 3438 | Route *route; |
e7780c8d | 3439 | Iterator i; |
fe8db0c5 TG |
3440 | int r; |
3441 | ||
3442 | assert(link); | |
3443 | assert(link->state_file); | |
68a8723c | 3444 | assert(link->lease_file); |
bbf7c048 TG |
3445 | assert(link->manager); |
3446 | ||
370e9930 TG |
3447 | if (link->state == LINK_STATE_LINGER) { |
3448 | unlink(link->state_file); | |
3449 | return 0; | |
3450 | } | |
3451 | ||
34437b4f LP |
3452 | link_lldp_save(link); |
3453 | ||
deb2e523 TG |
3454 | admin_state = link_state_to_string(link->state); |
3455 | assert(admin_state); | |
3456 | ||
e375dcde TG |
3457 | oper_state = link_operstate_to_string(link->operstate); |
3458 | assert(oper_state); | |
deb2e523 | 3459 | |
fe8db0c5 TG |
3460 | r = fopen_temporary(link->state_file, &f, &temp_path); |
3461 | if (r < 0) | |
6a7a4e4d | 3462 | goto fail; |
fe8db0c5 | 3463 | |
0d536673 | 3464 | (void) __fsetlocking(f, FSETLOCKING_BYCALLER); |
5512a963 | 3465 | (void) fchmod(fileno(f), 0644); |
fe8db0c5 TG |
3466 | |
3467 | fprintf(f, | |
3468 | "# This is private data. Do not parse.\n" | |
deb2e523 | 3469 | "ADMIN_STATE=%s\n" |
6dcaa6f5 TG |
3470 | "OPER_STATE=%s\n", |
3471 | admin_state, oper_state); | |
fe8db0c5 | 3472 | |
bcb7a07e | 3473 | if (link->network) { |
ea352b40 | 3474 | bool space; |
07bdc70d | 3475 | sd_dhcp6_lease *dhcp6_lease = NULL; |
b2a81c0b LP |
3476 | const char *dhcp_domainname = NULL; |
3477 | char **dhcp6_domains = NULL; | |
b85bc551 | 3478 | char **dhcp_domains = NULL; |
5512a963 | 3479 | unsigned j; |
07bdc70d | 3480 | |
c1a38904 MTL |
3481 | fprintf(f, "REQUIRED_FOR_ONLINE=%s\n", |
3482 | yes_no(link->network->required_for_online)); | |
3483 | ||
07bdc70d | 3484 | if (link->dhcp6_client) { |
4058d339 TG |
3485 | r = sd_dhcp6_client_get_lease(link->dhcp6_client, &dhcp6_lease); |
3486 | if (r < 0 && r != -ENOMSG) | |
07bdc70d PF |
3487 | log_link_debug(link, "No DHCPv6 lease"); |
3488 | } | |
b0e39c82 | 3489 | |
adc5b2e2 TG |
3490 | fprintf(f, "NETWORK_FILE=%s\n", link->network->filename); |
3491 | ||
0d536673 | 3492 | fputs("DNS=", f); |
ea352b40 | 3493 | space = false; |
5512a963 LP |
3494 | |
3495 | for (j = 0; j < link->network->n_dns; j++) { | |
3496 | _cleanup_free_ char *b = NULL; | |
3497 | ||
3498 | r = in_addr_to_string(link->network->dns[j].family, | |
3499 | &link->network->dns[j].address, &b); | |
3500 | if (r < 0) { | |
3501 | log_debug_errno(r, "Failed to format address, ignoring: %m"); | |
3502 | continue; | |
3503 | } | |
3504 | ||
3505 | if (space) | |
0d536673 LP |
3506 | fputc(' ', f); |
3507 | fputs(b, f); | |
5512a963 LP |
3508 | space = true; |
3509 | } | |
d5314fff | 3510 | |
27cb34f5 | 3511 | if (link->network->dhcp_use_dns && |
b0e39c82 TG |
3512 | link->dhcp_lease) { |
3513 | const struct in_addr *addresses; | |
3514 | ||
3515 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses); | |
3516 | if (r > 0) { | |
ea352b40 | 3517 | if (space) |
0d536673 | 3518 | fputc(' ', f); |
b0e39c82 | 3519 | serialize_in_addrs(f, addresses, r); |
07bdc70d PF |
3520 | space = true; |
3521 | } | |
3522 | } | |
3523 | ||
27cb34f5 | 3524 | if (link->network->dhcp_use_dns && dhcp6_lease) { |
07bdc70d PF |
3525 | struct in6_addr *in6_addrs; |
3526 | ||
3527 | r = sd_dhcp6_lease_get_dns(dhcp6_lease, &in6_addrs); | |
3528 | if (r > 0) { | |
3529 | if (space) | |
0d536673 | 3530 | fputc(' ', f); |
07bdc70d | 3531 | serialize_in6_addrs(f, in6_addrs, r); |
1e7a0e21 LP |
3532 | space = true; |
3533 | } | |
3534 | } | |
3535 | ||
3536 | /* Make sure to flush out old entries before we use the NDISC data */ | |
3537 | ndisc_vacuum(link); | |
3538 | ||
b296797f | 3539 | if (link->network->ipv6_accept_ra_use_dns && link->ndisc_rdnss) { |
1e7a0e21 LP |
3540 | NDiscRDNSS *dd; |
3541 | ||
3542 | SET_FOREACH(dd, link->ndisc_rdnss, i) { | |
3543 | if (space) | |
0d536673 | 3544 | fputc(' ', f); |
1e7a0e21 LP |
3545 | |
3546 | serialize_in6_addrs(f, &dd->address, 1); | |
3547 | space = true; | |
b0e39c82 TG |
3548 | } |
3549 | } | |
3550 | ||
0d536673 | 3551 | fputc('\n', f); |
b0e39c82 | 3552 | |
0d536673 | 3553 | fputs("NTP=", f); |
ea352b40 | 3554 | space = false; |
3df9bec5 | 3555 | fputstrv(f, link->network->ntp, NULL, &space); |
d5314fff | 3556 | |
27cb34f5 | 3557 | if (link->network->dhcp_use_ntp && |
b0e39c82 TG |
3558 | link->dhcp_lease) { |
3559 | const struct in_addr *addresses; | |
3560 | ||
3561 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses); | |
3562 | if (r > 0) { | |
ea352b40 | 3563 | if (space) |
0d536673 | 3564 | fputc(' ', f); |
b0e39c82 | 3565 | serialize_in_addrs(f, addresses, r); |
07bdc70d PF |
3566 | space = true; |
3567 | } | |
3568 | } | |
3569 | ||
27cb34f5 | 3570 | if (link->network->dhcp_use_ntp && dhcp6_lease) { |
07bdc70d PF |
3571 | struct in6_addr *in6_addrs; |
3572 | char **hosts; | |
07bdc70d PF |
3573 | |
3574 | r = sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease, | |
3575 | &in6_addrs); | |
3576 | if (r > 0) { | |
3577 | if (space) | |
0d536673 | 3578 | fputc(' ', f); |
07bdc70d PF |
3579 | serialize_in6_addrs(f, in6_addrs, r); |
3580 | space = true; | |
3581 | } | |
3582 | ||
3583 | r = sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease, &hosts); | |
3df9bec5 LP |
3584 | if (r > 0) |
3585 | fputstrv(f, hosts, NULL, &space); | |
b0e39c82 TG |
3586 | } |
3587 | ||
0d536673 | 3588 | fputc('\n', f); |
bd8f6538 | 3589 | |
b2a81c0b | 3590 | if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) { |
b85bc551 | 3591 | if (link->dhcp_lease) { |
b2a81c0b | 3592 | (void) sd_dhcp_lease_get_domainname(link->dhcp_lease, &dhcp_domainname); |
b85bc551 DW |
3593 | (void) sd_dhcp_lease_get_search_domains(link->dhcp_lease, &dhcp_domains); |
3594 | } | |
b2a81c0b LP |
3595 | if (dhcp6_lease) |
3596 | (void) sd_dhcp6_lease_get_domains(dhcp6_lease, &dhcp6_domains); | |
07bdc70d PF |
3597 | } |
3598 | ||
0d536673 | 3599 | fputs("DOMAINS=", f); |
b85bc551 | 3600 | space = false; |
b2a81c0b | 3601 | fputstrv(f, link->network->search_domains, NULL, &space); |
07bdc70d | 3602 | |
1e7a0e21 LP |
3603 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) { |
3604 | NDiscDNSSL *dd; | |
9b4d1882 | 3605 | |
1e7a0e21 LP |
3606 | if (dhcp_domainname) |
3607 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
b85bc551 DW |
3608 | if (dhcp_domains) |
3609 | fputstrv(f, dhcp_domains, NULL, &space); | |
1e7a0e21 LP |
3610 | if (dhcp6_domains) |
3611 | fputstrv(f, dhcp6_domains, NULL, &space); | |
3612 | ||
3613 | SET_FOREACH(dd, link->ndisc_dnssl, i) | |
3614 | fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space); | |
3615 | } | |
b2a81c0b | 3616 | |
0d536673 | 3617 | fputc('\n', f); |
6192b846 | 3618 | |
0d536673 | 3619 | fputs("ROUTE_DOMAINS=", f); |
b85bc551 DW |
3620 | space = false; |
3621 | fputstrv(f, link->network->route_domains, NULL, &space); | |
b2a81c0b | 3622 | |
1e7a0e21 LP |
3623 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_ROUTE) { |
3624 | NDiscDNSSL *dd; | |
b2a81c0b | 3625 | |
1e7a0e21 LP |
3626 | if (dhcp_domainname) |
3627 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
b85bc551 DW |
3628 | if (dhcp_domains) |
3629 | fputstrv(f, dhcp_domains, NULL, &space); | |
1e7a0e21 LP |
3630 | if (dhcp6_domains) |
3631 | fputstrv(f, dhcp6_domains, NULL, &space); | |
3632 | ||
3633 | SET_FOREACH(dd, link->ndisc_dnssl, i) | |
3634 | fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space); | |
3635 | } | |
b2a81c0b | 3636 | |
0d536673 | 3637 | fputc('\n', f); |
67272d15 | 3638 | |
3c9b8860 | 3639 | fprintf(f, "LLMNR=%s\n", |
a7e5da6e | 3640 | resolve_support_to_string(link->network->llmnr)); |
aaa297d4 LP |
3641 | fprintf(f, "MDNS=%s\n", |
3642 | resolve_support_to_string(link->network->mdns)); | |
e7780c8d | 3643 | |
ad6c0475 LP |
3644 | if (link->network->dnssec_mode != _DNSSEC_MODE_INVALID) |
3645 | fprintf(f, "DNSSEC=%s\n", | |
3646 | dnssec_mode_to_string(link->network->dnssec_mode)); | |
3647 | ||
8a516214 LP |
3648 | if (!set_isempty(link->network->dnssec_negative_trust_anchors)) { |
3649 | const char *n; | |
3650 | ||
0d536673 | 3651 | fputs("DNSSEC_NTA=", f); |
8a516214 | 3652 | space = false; |
d390f8ef LP |
3653 | SET_FOREACH(n, link->network->dnssec_negative_trust_anchors, i) |
3654 | fputs_with_space(f, n, NULL, &space); | |
0d536673 | 3655 | fputc('\n', f); |
8a516214 LP |
3656 | } |
3657 | ||
0d536673 | 3658 | fputs("ADDRESSES=", f); |
e7780c8d TG |
3659 | space = false; |
3660 | SET_FOREACH(a, link->addresses, i) { | |
3661 | _cleanup_free_ char *address_str = NULL; | |
3662 | ||
3663 | r = in_addr_to_string(a->family, &a->in_addr, &address_str); | |
3664 | if (r < 0) | |
3665 | goto fail; | |
3666 | ||
e7780c8d TG |
3667 | fprintf(f, "%s%s/%u", space ? " " : "", address_str, a->prefixlen); |
3668 | space = true; | |
3669 | } | |
0d536673 | 3670 | fputc('\n', f); |
c1eb9872 | 3671 | |
0d536673 | 3672 | fputs("ROUTES=", f); |
c1eb9872 TG |
3673 | space = false; |
3674 | SET_FOREACH(route, link->routes, i) { | |
3675 | _cleanup_free_ char *route_str = NULL; | |
3676 | ||
3677 | r = in_addr_to_string(route->family, &route->dst, &route_str); | |
3678 | if (r < 0) | |
3679 | goto fail; | |
3680 | ||
ecc3f340 ZJS |
3681 | fprintf(f, "%s%s/%hhu/%hhu/%"PRIu32"/%"PRIu32"/"USEC_FMT, |
3682 | space ? " " : "", route_str, | |
f833694d | 3683 | route->dst_prefixlen, route->tos, route->priority, route->table, route->lifetime); |
c1eb9872 TG |
3684 | space = true; |
3685 | } | |
3686 | ||
0d536673 | 3687 | fputc('\n', f); |
bcb7a07e | 3688 | } |
7374f9d8 | 3689 | |
b295beea LP |
3690 | print_link_hashmap(f, "CARRIER_BOUND_TO=", link->bound_to_links); |
3691 | print_link_hashmap(f, "CARRIER_BOUND_BY=", link->bound_by_links); | |
0d4ad91d | 3692 | |
8eb9058d | 3693 | if (link->dhcp_lease) { |
0bc70f1d | 3694 | struct in_addr address; |
8eb9058d LP |
3695 | const char *tz = NULL; |
3696 | ||
0bc70f1d TG |
3697 | assert(link->network); |
3698 | ||
8eb9058d LP |
3699 | r = sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz); |
3700 | if (r >= 0) | |
3701 | fprintf(f, "TIMEZONE=%s\n", tz); | |
8eb9058d | 3702 | |
0bc70f1d TG |
3703 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
3704 | if (r >= 0) { | |
0d536673 | 3705 | fputs("DHCP4_ADDRESS=", f); |
0bc70f1d | 3706 | serialize_in_addrs(f, &address, 1); |
0d536673 | 3707 | fputc('\n', f); |
0bc70f1d | 3708 | } |
d9876a52 | 3709 | |
bd91b83e | 3710 | r = dhcp_lease_save(link->dhcp_lease, link->lease_file); |
fe8db0c5 | 3711 | if (r < 0) |
c2d6bd61 | 3712 | goto fail; |
fe8db0c5 | 3713 | |
7374f9d8 | 3714 | fprintf(f, |
b0e39c82 TG |
3715 | "DHCP_LEASE=%s\n", |
3716 | link->lease_file); | |
deb2e523 | 3717 | } else |
68a8723c | 3718 | unlink(link->lease_file); |
fe8db0c5 | 3719 | |
0bc70f1d TG |
3720 | if (link->ipv4ll) { |
3721 | struct in_addr address; | |
3722 | ||
3723 | r = sd_ipv4ll_get_address(link->ipv4ll, &address); | |
3724 | if (r >= 0) { | |
0d536673 | 3725 | fputs("IPV4LL_ADDRESS=", f); |
0bc70f1d | 3726 | serialize_in_addrs(f, &address, 1); |
0d536673 | 3727 | fputc('\n', f); |
0bc70f1d TG |
3728 | } |
3729 | } | |
3730 | ||
c2d6bd61 LP |
3731 | r = fflush_and_check(f); |
3732 | if (r < 0) | |
3733 | goto fail; | |
fe8db0c5 | 3734 | |
c2d6bd61 | 3735 | if (rename(temp_path, link->state_file) < 0) { |
fe8db0c5 | 3736 | r = -errno; |
c2d6bd61 | 3737 | goto fail; |
fe8db0c5 TG |
3738 | } |
3739 | ||
c2d6bd61 | 3740 | return 0; |
dacd6cee | 3741 | |
c2d6bd61 | 3742 | fail: |
6a7a4e4d | 3743 | (void) unlink(link->state_file); |
6a7a4e4d LP |
3744 | if (temp_path) |
3745 | (void) unlink(temp_path); | |
3746 | ||
dacd6cee | 3747 | return log_link_error_errno(link, r, "Failed to save link data to %s: %m", link->state_file); |
fe8db0c5 TG |
3748 | } |
3749 | ||
84de38c5 TG |
3750 | /* The serialized state in /run is no longer up-to-date. */ |
3751 | void link_dirty(Link *link) { | |
3752 | int r; | |
3753 | ||
3754 | assert(link); | |
3755 | ||
0c241a37 SS |
3756 | /* mark manager dirty as link is dirty */ |
3757 | manager_dirty(link->manager); | |
3758 | ||
84de38c5 TG |
3759 | r = set_ensure_allocated(&link->manager->dirty_links, NULL); |
3760 | if (r < 0) | |
3761 | /* allocation errors are ignored */ | |
3762 | return; | |
3763 | ||
3764 | r = set_put(link->manager->dirty_links, link); | |
0c241a37 SS |
3765 | if (r <= 0) |
3766 | /* don't take another ref if the link was already dirty */ | |
84de38c5 TG |
3767 | return; |
3768 | ||
3769 | link_ref(link); | |
3770 | } | |
3771 | ||
3772 | /* The serialized state in /run is up-to-date */ | |
3773 | void link_clean(Link *link) { | |
3774 | assert(link); | |
3775 | assert(link->manager); | |
3776 | ||
3777 | set_remove(link->manager->dirty_links, link); | |
3778 | link_unref(link); | |
3779 | } | |
3780 | ||
fe8db0c5 | 3781 | static const char* const link_state_table[_LINK_STATE_MAX] = { |
8434fd5c | 3782 | [LINK_STATE_PENDING] = "pending", |
fe8db0c5 TG |
3783 | [LINK_STATE_ENSLAVING] = "configuring", |
3784 | [LINK_STATE_SETTING_ADDRESSES] = "configuring", | |
3785 | [LINK_STATE_SETTING_ROUTES] = "configuring", | |
3786 | [LINK_STATE_CONFIGURED] = "configured", | |
57bd6899 | 3787 | [LINK_STATE_UNMANAGED] = "unmanaged", |
fe8db0c5 | 3788 | [LINK_STATE_FAILED] = "failed", |
370e9930 | 3789 | [LINK_STATE_LINGER] = "linger", |
fe8db0c5 TG |
3790 | }; |
3791 | ||
3792 | DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState); | |
e375dcde TG |
3793 | |
3794 | static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = { | |
d3df0e39 TG |
3795 | [LINK_OPERSTATE_OFF] = "off", |
3796 | [LINK_OPERSTATE_NO_CARRIER] = "no-carrier", | |
e375dcde TG |
3797 | [LINK_OPERSTATE_DORMANT] = "dormant", |
3798 | [LINK_OPERSTATE_CARRIER] = "carrier", | |
3799 | [LINK_OPERSTATE_DEGRADED] = "degraded", | |
3800 | [LINK_OPERSTATE_ROUTABLE] = "routable", | |
3801 | }; | |
3802 | ||
3803 | DEFINE_STRING_TABLE_LOOKUP(link_operstate, LinkOperationalState); |