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