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