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