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