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