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