2 This file is part of systemd.
4 Copyright 2013 Tom Gundersen <teg@jklm.no>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <netinet/ether.h>
24 #include "alloc-util.h"
26 #include "dhcp-lease-internal.h"
29 #include "netlink-util.h"
30 #include "network-internal.h"
31 #include "networkd-ipv6-proxy-ndp.h"
32 #include "networkd-lldp-tx.h"
33 #include "networkd-manager.h"
34 #include "networkd-ndisc.h"
35 #include "networkd-radv.h"
36 #include "networkd-routing-policy-rule.h"
38 #include "socket-util.h"
39 #include "stdio-util.h"
40 #include "string-table.h"
41 #include "udev-util.h"
45 static bool link_dhcp6_enabled(Link
*link
) {
48 if (!socket_ipv6_is_supported())
51 if (link
->flags
& IFF_LOOPBACK
)
57 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
60 static bool link_dhcp4_enabled(Link
*link
) {
63 if (link
->flags
& IFF_LOOPBACK
)
69 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
72 static bool link_dhcp4_server_enabled(Link
*link
) {
75 if (link
->flags
& IFF_LOOPBACK
)
81 return link
->network
->dhcp_server
;
84 static bool link_ipv4ll_enabled(Link
*link
) {
87 if (link
->flags
& IFF_LOOPBACK
)
93 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
96 static bool link_ipv6ll_enabled(Link
*link
) {
99 if (!socket_ipv6_is_supported())
102 if (link
->flags
& IFF_LOOPBACK
)
108 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
111 static bool link_ipv6_enabled(Link
*link
) {
114 if (!socket_ipv6_is_supported())
117 if (link
->network
->bridge
)
120 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
121 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
124 static bool link_radv_enabled(Link
*link
) {
127 if (!link_ipv6ll_enabled(link
))
130 return link
->network
->router_prefix_delegation
;
133 static bool link_lldp_rx_enabled(Link
*link
) {
136 if (link
->flags
& IFF_LOOPBACK
)
139 if (link
->iftype
!= ARPHRD_ETHER
)
145 /* LLDP should be handled on bridge slaves as those have a direct
146 * connection to their peers not on the bridge master. Linux doesn't
147 * even (by default) forward lldp packets to the bridge master.*/
148 if (streq_ptr("bridge", link
->kind
))
151 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
154 static bool link_lldp_emit_enabled(Link
*link
) {
157 if (link
->flags
& IFF_LOOPBACK
)
160 if (link
->iftype
!= ARPHRD_ETHER
)
166 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
169 static bool link_ipv4_forward_enabled(Link
*link
) {
172 if (link
->flags
& IFF_LOOPBACK
)
178 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
181 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
184 static bool link_ipv6_forward_enabled(Link
*link
) {
187 if (!socket_ipv6_is_supported())
190 if (link
->flags
& IFF_LOOPBACK
)
196 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
199 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
202 static bool link_proxy_arp_enabled(Link
*link
) {
205 if (link
->flags
& IFF_LOOPBACK
)
211 if (link
->network
->proxy_arp
< 0)
217 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
220 if (!socket_ipv6_is_supported())
223 if (link
->flags
& IFF_LOOPBACK
)
229 if (!link_ipv6ll_enabled(link
))
232 /* If unset use system default (enabled if local forwarding is disabled.
233 * disabled if local forwarding is enabled).
234 * If set, ignore or enforce RA independent of local forwarding state.
236 if (link
->network
->ipv6_accept_ra
< 0)
237 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
238 return !link_ipv6_forward_enabled(link
);
239 else if (link
->network
->ipv6_accept_ra
> 0)
240 /* accept RA even if ip_forward is enabled */
247 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
250 if (!socket_ipv6_is_supported())
251 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
253 if (link
->flags
& IFF_LOOPBACK
)
254 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
257 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
259 return link
->network
->ipv6_privacy_extensions
;
262 static int link_enable_ipv6(Link
*link
) {
263 const char *p
= NULL
;
267 if (link
->flags
& IFF_LOOPBACK
)
270 disabled
= !link_ipv6_enabled(link
);
272 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
274 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
276 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
277 enable_disable(!disabled
), link
->ifname
);
279 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
284 void link_update_operstate(Link
*link
) {
285 LinkOperationalState operstate
;
288 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
289 operstate
= LINK_OPERSTATE_DORMANT
;
290 else if (link_has_carrier(link
)) {
292 uint8_t scope
= RT_SCOPE_NOWHERE
;
295 /* if we have carrier, check what addresses we have */
296 SET_FOREACH(address
, link
->addresses
, i
) {
297 if (!address_is_ready(address
))
300 if (address
->scope
< scope
)
301 scope
= address
->scope
;
304 /* for operstate we also take foreign addresses into account */
305 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
306 if (!address_is_ready(address
))
309 if (address
->scope
< scope
)
310 scope
= address
->scope
;
313 if (scope
< RT_SCOPE_SITE
)
314 /* universally accessible addresses found */
315 operstate
= LINK_OPERSTATE_ROUTABLE
;
316 else if (scope
< RT_SCOPE_HOST
)
317 /* only link or site local addresses found */
318 operstate
= LINK_OPERSTATE_DEGRADED
;
320 /* no useful addresses found */
321 operstate
= LINK_OPERSTATE_CARRIER
;
322 } else if (link
->flags
& IFF_UP
)
323 operstate
= LINK_OPERSTATE_NO_CARRIER
;
325 operstate
= LINK_OPERSTATE_OFF
;
327 if (link
->operstate
!= operstate
) {
328 link
->operstate
= operstate
;
329 link_send_changed(link
, "OperationalState", NULL
);
334 #define FLAG_STRING(string, flag, old, new) \
335 (((old ^ new) & flag) \
336 ? ((old & flag) ? (" -" string) : (" +" string)) \
339 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
340 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
346 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
348 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
350 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
352 /* if we got a message without operstate, take it to mean
353 the state was unchanged */
354 operstate
= link
->kernel_operstate
;
356 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
359 if (link
->flags
!= flags
) {
360 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",
361 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
362 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
363 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
364 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
365 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
366 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
367 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
368 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
369 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
370 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
371 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
372 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
373 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
374 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
375 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
376 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
377 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
378 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
379 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
381 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
382 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
383 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
384 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
385 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
386 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
387 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
388 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
390 /* link flags are currently at most 18 bits, let's align to
392 if (unknown_flags_added
)
394 "Unknown link flags gained: %#.5x (ignoring)",
395 unknown_flags_added
);
397 if (unknown_flags_removed
)
399 "Unknown link flags lost: %#.5x (ignoring)",
400 unknown_flags_removed
);
404 link
->kernel_operstate
= operstate
;
406 link_update_operstate(link
);
411 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
412 _cleanup_link_unref_ Link
*link
= NULL
;
414 const char *ifname
, *kind
= NULL
;
416 unsigned short iftype
;
422 /* check for link kind */
423 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
425 (void)sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
426 r
= sd_netlink_message_exit_container(message
);
431 r
= sd_netlink_message_get_type(message
, &type
);
434 else if (type
!= RTM_NEWLINK
)
437 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
440 else if (ifindex
<= 0)
443 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
447 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
451 link
= new0(Link
, 1);
456 link
->manager
= manager
;
457 link
->state
= LINK_STATE_PENDING
;
458 link
->rtnl_extended_attrs
= true;
459 link
->ifindex
= ifindex
;
460 link
->iftype
= iftype
;
461 link
->ifname
= strdup(ifname
);
466 link
->kind
= strdup(kind
);
471 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
473 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
475 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
478 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
481 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
484 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
488 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
492 r
= link_update_flags(link
, message
);
502 static void link_free(Link
*link
) {
510 while (!set_isempty(link
->addresses
))
511 address_free(set_first(link
->addresses
));
513 while (!set_isempty(link
->addresses_foreign
))
514 address_free(set_first(link
->addresses_foreign
));
516 link
->addresses
= set_free(link
->addresses
);
518 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
520 while ((address
= link
->pool_addresses
)) {
521 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
522 address_free(address
);
525 sd_dhcp_server_unref(link
->dhcp_server
);
526 sd_dhcp_client_unref(link
->dhcp_client
);
527 sd_dhcp_lease_unref(link
->dhcp_lease
);
529 link_lldp_emit_stop(link
);
531 free(link
->lease_file
);
533 sd_lldp_unref(link
->lldp
);
534 free(link
->lldp_file
);
538 sd_ipv4ll_unref(link
->ipv4ll
);
539 sd_dhcp6_client_unref(link
->dhcp6_client
);
540 sd_ndisc_unref(link
->ndisc
);
541 sd_radv_unref(link
->radv
);
544 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
550 (void)unlink(link
->state_file
);
551 free(link
->state_file
);
553 udev_device_unref(link
->udev_device
);
555 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
556 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
557 hashmap_free(link
->bound_to_links
);
559 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
560 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
561 hashmap_free(link
->bound_by_links
);
566 Link
*link_unref(Link
*link
) {
570 assert(link
->n_ref
> 0);
582 Link
*link_ref(Link
*link
) {
586 assert(link
->n_ref
> 0);
593 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
600 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
609 static void link_set_state(Link
*link
, LinkState state
) {
612 if (link
->state
== state
)
617 link_send_changed(link
, "AdministrativeState", NULL
);
620 static void link_enter_unmanaged(Link
*link
) {
623 log_link_debug(link
, "Unmanaged");
625 link_set_state(link
, LINK_STATE_UNMANAGED
);
630 static int link_stop_clients(Link
*link
) {
634 assert(link
->manager
);
635 assert(link
->manager
->event
);
637 if (link
->dhcp_client
) {
638 k
= sd_dhcp_client_stop(link
->dhcp_client
);
640 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
644 k
= sd_ipv4ll_stop(link
->ipv4ll
);
646 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
649 if (link
->dhcp6_client
) {
650 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
652 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
656 k
= sd_ndisc_stop(link
->ndisc
);
658 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
662 k
= sd_radv_stop(link
->radv
);
664 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
667 link_lldp_emit_stop(link
);
671 void link_enter_failed(Link
*link
) {
674 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
677 log_link_warning(link
, "Failed");
679 link_set_state(link
, LINK_STATE_FAILED
);
681 link_stop_clients(link
);
686 static Address
* link_find_dhcp_server_address(Link
*link
) {
690 assert(link
->network
);
692 /* The first statically configured address if there is any */
693 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
695 if (address
->family
!= AF_INET
)
698 if (in_addr_is_null(address
->family
, &address
->in_addr
))
704 /* If that didn't work, find a suitable address we got from the pool */
705 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
706 if (address
->family
!= AF_INET
)
715 static void link_enter_configured(Link
*link
) {
717 assert(link
->network
);
719 if (link
->state
!= LINK_STATE_SETTING_ROUTES
)
722 log_link_info(link
, "Configured");
724 link_set_state(link
, LINK_STATE_CONFIGURED
);
729 void link_check_ready(Link
*link
) {
735 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
741 if (!link
->static_configured
)
744 if (link_ipv4ll_enabled(link
))
745 if (!link
->ipv4ll_address
||
749 if (link_ipv6ll_enabled(link
))
750 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
753 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
754 !link
->dhcp4_configured
) ||
755 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
756 !link
->dhcp6_configured
) ||
757 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
758 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
761 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
764 SET_FOREACH(a
, link
->addresses
, i
)
765 if (!address_is_ready(a
))
768 if (link
->state
!= LINK_STATE_CONFIGURED
)
769 link_enter_configured(link
);
774 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
775 _cleanup_link_unref_ Link
*link
= userdata
;
778 assert(link
->link_messages
> 0);
779 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
780 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
783 link
->link_messages
--;
785 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
788 r
= sd_netlink_message_get_errno(m
);
789 if (r
< 0 && r
!= -EEXIST
)
790 log_link_warning_errno(link
, r
, "Could not set route: %m");
792 if (link
->link_messages
== 0) {
793 log_link_debug(link
, "Routes set");
794 link
->static_configured
= true;
795 link_check_ready(link
);
801 static int link_enter_set_routes(Link
*link
) {
806 assert(link
->network
);
807 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
809 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
811 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
812 r
= route_configure(rt
, link
, route_handler
);
814 log_link_warning_errno(link
, r
, "Could not set routes: %m");
815 link_enter_failed(link
);
819 link
->link_messages
++;
822 if (link
->link_messages
== 0) {
823 link
->static_configured
= true;
824 link_check_ready(link
);
826 log_link_debug(link
, "Setting routes");
831 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
832 _cleanup_link_unref_ Link
*link
= userdata
;
837 assert(link
->ifname
);
839 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
842 r
= sd_netlink_message_get_errno(m
);
843 if (r
< 0 && r
!= -ESRCH
)
844 log_link_warning_errno(link
, r
, "Could not drop route: %m");
849 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
850 _cleanup_link_unref_ Link
*link
= userdata
;
856 assert(link
->ifname
);
857 assert(link
->link_messages
> 0);
858 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
859 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
861 link
->link_messages
--;
863 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
866 r
= sd_netlink_message_get_errno(m
);
867 if (r
< 0 && r
!= -EEXIST
)
868 log_link_warning_errno(link
, r
, "could not set address: %m");
870 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
872 if (link
->link_messages
== 0) {
873 log_link_debug(link
, "Addresses set");
874 link_enter_set_routes(link
);
880 static int address_label_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
881 _cleanup_link_unref_ Link
*link
= userdata
;
887 assert(link
->ifname
);
888 assert(link
->link_messages
> 0);
890 link
->link_messages
--;
892 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
895 r
= sd_netlink_message_get_errno(m
);
896 if (r
< 0 && r
!= -EEXIST
)
897 log_link_warning_errno(link
, r
, "could not set address label: %m");
899 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
901 if (link
->link_messages
== 0) {
902 log_link_debug(link
, "Addresses label set");
903 link_enter_set_routes(link
);
909 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
910 _cleanup_free_
struct in_addr
*addresses
= NULL
;
911 size_t n_addresses
= 0, n_allocated
= 0;
914 log_debug("Copying DNS server information from %s", link
->ifname
);
919 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
922 /* Only look for IPv4 addresses */
923 if (link
->network
->dns
[i
].family
!= AF_INET
)
926 ia
= link
->network
->dns
[i
].address
.in
;
928 /* Never propagate obviously borked data */
929 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
932 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
935 addresses
[n_addresses
++] = ia
;
938 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
939 const struct in_addr
*da
= NULL
;
942 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
945 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
948 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
953 if (n_addresses
<= 0)
956 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
959 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
960 _cleanup_free_
struct in_addr
*addresses
= NULL
;
961 size_t n_addresses
= 0, n_allocated
= 0;
967 log_debug("Copying NTP server information from %s", link
->ifname
);
969 STRV_FOREACH(a
, link
->network
->ntp
) {
972 /* Only look for IPv4 addresses */
973 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
976 /* Never propagate obviously borked data */
977 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
980 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
983 addresses
[n_addresses
++] = ia
;
986 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
987 const struct in_addr
*da
= NULL
;
990 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
993 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
996 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1001 if (n_addresses
<= 0)
1004 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1007 static int link_set_bridge_fdb(Link
*link
) {
1008 FdbEntry
*fdb_entry
;
1011 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1012 r
= fdb_entry_configure(link
, fdb_entry
);
1014 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1020 static int link_enter_set_addresses(Link
*link
) {
1021 RoutingPolicyRule
*rule
, *rrule
= NULL
;
1022 AddressLabel
*label
;
1027 assert(link
->network
);
1028 assert(link
->state
!= _LINK_STATE_INVALID
);
1030 r
= link_set_bridge_fdb(link
);
1034 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
1036 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1037 r
= address_configure(ad
, link
, address_handler
, false);
1039 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1040 link_enter_failed(link
);
1044 link
->link_messages
++;
1047 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1048 r
= address_label_configure(label
, link
, address_label_handler
, false);
1050 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1051 link_enter_failed(link
);
1055 link
->link_messages
++;
1058 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
1059 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
1060 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, &rrule
);
1062 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
1066 r
= routing_policy_rule_configure(rule
, link
, link_routing_policy_rule_handler
, false);
1068 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
1069 link_enter_failed(link
);
1073 link
->link_messages
++;
1076 routing_policy_rule_purge(link
->manager
, link
);
1078 /* now that we can figure out a default address for the dhcp server,
1080 if (link_dhcp4_server_enabled(link
)) {
1082 Link
*uplink
= NULL
;
1083 bool acquired_uplink
= false;
1085 address
= link_find_dhcp_server_address(link
);
1087 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1088 link_enter_failed(link
);
1092 /* use the server address' subnet as the pool */
1093 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1094 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1099 r = sd_dhcp_server_set_router(link->dhcp_server,
1100 &main_address->in_addr.in);
1105 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1106 r
= sd_dhcp_server_set_max_lease_time(
1108 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1113 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1114 r
= sd_dhcp_server_set_default_lease_time(
1116 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1121 if (link
->network
->dhcp_server_emit_dns
) {
1123 if (link
->network
->n_dhcp_server_dns
> 0)
1124 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1126 uplink
= manager_find_uplink(link
->manager
, link
);
1127 acquired_uplink
= true;
1130 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1133 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1136 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1140 if (link
->network
->dhcp_server_emit_ntp
) {
1142 if (link
->network
->n_dhcp_server_ntp
> 0)
1143 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1145 if (!acquired_uplink
)
1146 uplink
= manager_find_uplink(link
->manager
, link
);
1149 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1152 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1156 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1159 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1161 log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1165 if (link
->network
->dhcp_server_emit_timezone
) {
1166 _cleanup_free_
char *buffer
= NULL
;
1167 const char *tz
= NULL
;
1169 if (link
->network
->dhcp_server_timezone
)
1170 tz
= link
->network
->dhcp_server_timezone
;
1172 r
= get_timezone(&buffer
);
1174 log_warning_errno(r
, "Failed to determine timezone: %m");
1180 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1186 r
= sd_dhcp_server_start(link
->dhcp_server
);
1188 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1190 link_enter_failed(link
);
1195 log_link_debug(link
, "Offering DHCPv4 leases");
1198 if (link
->link_messages
== 0)
1199 link_enter_set_routes(link
);
1201 log_link_debug(link
, "Setting addresses");
1206 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1207 _cleanup_link_unref_ Link
*link
= userdata
;
1212 assert(link
->ifname
);
1214 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1217 r
= sd_netlink_message_get_errno(m
);
1218 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1219 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1224 static int link_set_bridge_vlan(Link
*link
) {
1227 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1229 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1234 static int link_set_proxy_arp(Link
*link
) {
1235 const char *p
= NULL
;
1238 if (!link_proxy_arp_enabled(link
))
1241 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1243 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1245 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1250 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1251 _cleanup_link_unref_ Link
*link
= userdata
;
1254 log_link_debug(link
, "Set link");
1256 r
= sd_netlink_message_get_errno(m
);
1257 if (r
< 0 && r
!= -EEXIST
) {
1258 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1259 link_enter_failed(link
);
1266 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1267 _cleanup_link_unref_ Link
*link
= userdata
;
1272 assert(link
->ifname
);
1274 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1277 r
= sd_netlink_message_get_errno(m
);
1279 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1284 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1285 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1289 assert(link
->manager
);
1290 assert(link
->manager
->rtnl
);
1292 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1294 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1296 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1298 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1300 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1302 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1304 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1311 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1312 _cleanup_link_unref_ Link
*link
= userdata
;
1317 assert(link
->ifname
);
1319 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1322 r
= sd_netlink_message_get_errno(m
);
1324 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1329 static int link_set_flags(Link
*link
) {
1330 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1331 unsigned ifi_change
= 0;
1332 unsigned ifi_flags
= 0;
1336 assert(link
->manager
);
1337 assert(link
->manager
->rtnl
);
1339 if (link
->flags
& IFF_LOOPBACK
)
1345 if (link
->network
->arp
< 0)
1348 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1350 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1352 if (link
->network
->arp
>= 0) {
1353 ifi_change
|= IFF_NOARP
;
1354 ifi_flags
|= link
->network
->arp
? 0 : IFF_NOARP
;
1357 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1359 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1361 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1363 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1370 static int link_set_bridge(Link
*link
) {
1371 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1375 assert(link
->network
);
1377 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1379 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1381 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1383 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1385 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1387 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1389 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1391 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1393 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1395 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1397 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1399 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1401 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1403 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1405 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1407 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1409 if (link
->network
->cost
!= 0) {
1410 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1412 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1414 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1415 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1417 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1420 r
= sd_netlink_message_close_container(req
);
1422 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1424 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1426 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1433 static int link_bond_set(Link
*link
) {
1434 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1438 assert(link
->network
);
1440 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1442 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1444 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1446 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1448 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1450 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1452 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1454 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1456 if (link
->network
->active_slave
) {
1457 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1459 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1462 if (link
->network
->primary_slave
) {
1463 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1465 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1468 r
= sd_netlink_message_close_container(req
);
1470 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1472 r
= sd_netlink_message_close_container(req
);
1474 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1476 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1478 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1485 static int link_lldp_save(Link
*link
) {
1486 _cleanup_free_
char *temp_path
= NULL
;
1487 _cleanup_fclose_
FILE *f
= NULL
;
1488 sd_lldp_neighbor
**l
= NULL
;
1492 assert(link
->lldp_file
);
1495 (void) unlink(link
->lldp_file
);
1499 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1503 (void) unlink(link
->lldp_file
);
1509 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1513 fchmod(fileno(f
), 0644);
1515 for (i
= 0; i
< n
; i
++) {
1520 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1525 (void) fwrite(&u
, 1, sizeof(u
), f
);
1526 (void) fwrite(p
, 1, sz
, f
);
1529 r
= fflush_and_check(f
);
1533 if (rename(temp_path
, link
->lldp_file
) < 0) {
1540 (void) unlink(link
->lldp_file
);
1542 (void) unlink(temp_path
);
1544 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1548 for (i
= 0; i
< n
; i
++)
1549 sd_lldp_neighbor_unref(l
[i
]);
1556 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1557 Link
*link
= userdata
;
1562 (void) link_lldp_save(link
);
1564 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1565 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1567 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1569 r
= link_lldp_emit_start(link
);
1571 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1575 static int link_acquire_ipv6_conf(Link
*link
) {
1580 if (link_dhcp6_enabled(link
)) {
1581 assert(link
->dhcp6_client
);
1582 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1584 /* start DHCPv6 client in stateless mode */
1585 r
= dhcp6_request_address(link
, true);
1586 if (r
< 0 && r
!= -EBUSY
)
1587 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1589 log_link_debug(link
, "Acquiring DHCPv6 lease");
1592 if (link_ipv6_accept_ra_enabled(link
)) {
1593 assert(link
->ndisc
);
1595 log_link_debug(link
, "Discovering IPv6 routers");
1597 r
= sd_ndisc_start(link
->ndisc
);
1598 if (r
< 0 && r
!= -EBUSY
)
1599 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1602 if (link_radv_enabled(link
)) {
1604 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1606 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1608 r
= sd_radv_start(link
->radv
);
1609 if (r
< 0 && r
!= -EBUSY
)
1610 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1616 static int link_acquire_ipv4_conf(Link
*link
) {
1620 assert(link
->network
);
1621 assert(link
->manager
);
1622 assert(link
->manager
->event
);
1624 if (link_ipv4ll_enabled(link
)) {
1625 assert(link
->ipv4ll
);
1627 log_link_debug(link
, "Acquiring IPv4 link-local address");
1629 r
= sd_ipv4ll_start(link
->ipv4ll
);
1631 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1634 if (link_dhcp4_enabled(link
)) {
1635 assert(link
->dhcp_client
);
1637 log_link_debug(link
, "Acquiring DHCPv4 lease");
1639 r
= sd_dhcp_client_start(link
->dhcp_client
);
1641 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1647 static int link_acquire_conf(Link
*link
) {
1652 r
= link_acquire_ipv4_conf(link
);
1656 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1657 r
= link_acquire_ipv6_conf(link
);
1662 if (link_lldp_emit_enabled(link
)) {
1663 r
= link_lldp_emit_start(link
);
1665 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1671 bool link_has_carrier(Link
*link
) {
1672 /* see Documentation/networking/operstates.txt in the kernel sources */
1674 if (link
->kernel_operstate
== IF_OPER_UP
)
1677 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1678 /* operstate may not be implemented, so fall back to flags */
1679 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1685 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1686 _cleanup_link_unref_ Link
*link
= userdata
;
1691 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1694 r
= sd_netlink_message_get_errno(m
);
1696 /* we warn but don't fail the link, as it may be
1698 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1703 int link_up(Link
*link
) {
1704 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1705 uint8_t ipv6ll_mode
;
1709 assert(link
->network
);
1710 assert(link
->manager
);
1711 assert(link
->manager
->rtnl
);
1713 log_link_debug(link
, "Bringing link up");
1715 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1717 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1719 /* set it free if not enslaved with networkd */
1720 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1721 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1723 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1726 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1728 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1730 if (link
->network
->mac
) {
1731 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1733 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1736 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1737 for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1738 (void) link_enable_ipv6(link
);
1740 if (link
->network
->mtu
) {
1741 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1742 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1743 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1745 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1746 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1748 link
->network
->mtu
= IPV6_MIN_MTU
;
1751 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1753 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1756 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1758 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1760 if (link_ipv6_enabled(link
)) {
1761 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1762 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1764 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1766 if (!link_ipv6ll_enabled(link
))
1767 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1769 const char *p
= NULL
;
1770 _cleanup_free_
char *stable_secret
= NULL
;
1772 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1773 r
= read_one_line_file(p
, &stable_secret
);
1776 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1778 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1780 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1782 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1784 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1785 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1787 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1790 r
= sd_netlink_message_close_container(req
);
1792 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1795 r
= sd_netlink_message_close_container(req
);
1797 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1799 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1801 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1808 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1809 _cleanup_link_unref_ Link
*link
= userdata
;
1814 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1817 r
= sd_netlink_message_get_errno(m
);
1819 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1824 int link_down(Link
*link
) {
1825 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1829 assert(link
->manager
);
1830 assert(link
->manager
->rtnl
);
1832 log_link_debug(link
, "Bringing link down");
1834 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1835 RTM_SETLINK
, link
->ifindex
);
1837 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1839 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1841 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1843 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1845 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1852 static int link_up_can(Link
*link
) {
1853 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1858 log_link_debug(link
, "Bringing CAN link up");
1860 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1862 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1864 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1866 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1868 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1870 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1877 static int link_handle_bound_to_list(Link
*link
) {
1881 bool required_up
= false;
1882 bool link_is_up
= false;
1886 if (hashmap_isempty(link
->bound_to_links
))
1889 if (link
->flags
& IFF_UP
)
1892 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1893 if (link_has_carrier(l
)) {
1898 if (!required_up
&& link_is_up
) {
1899 r
= link_down(link
);
1902 } else if (required_up
&& !link_is_up
) {
1911 static int link_handle_bound_by_list(Link
*link
) {
1918 if (hashmap_isempty(link
->bound_by_links
))
1921 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1922 r
= link_handle_bound_to_list(l
);
1930 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1936 if (link
== carrier
)
1939 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1942 r
= hashmap_ensure_allocated(h
, NULL
);
1946 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1953 static int link_new_bound_by_list(Link
*link
) {
1958 bool list_updated
= false;
1961 assert(link
->manager
);
1965 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1966 if (!carrier
->network
)
1969 if (strv_isempty(carrier
->network
->bind_carrier
))
1972 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1973 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1977 list_updated
= true;
1984 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1985 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1989 link_dirty(carrier
);
1995 static int link_new_bound_to_list(Link
*link
) {
2000 bool list_updated
= false;
2003 assert(link
->manager
);
2008 if (strv_isempty(link
->network
->bind_carrier
))
2013 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2014 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2015 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2019 list_updated
= true;
2026 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2027 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2031 link_dirty(carrier
);
2037 static int link_new_carrier_maps(Link
*link
) {
2040 r
= link_new_bound_by_list(link
);
2044 r
= link_handle_bound_by_list(link
);
2048 r
= link_new_bound_to_list(link
);
2052 r
= link_handle_bound_to_list(link
);
2059 static void link_free_bound_to_list(Link
*link
) {
2063 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2064 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2066 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2067 link_dirty(bound_to
);
2073 static void link_free_bound_by_list(Link
*link
) {
2077 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2078 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2080 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2081 link_dirty(bound_by
);
2082 link_handle_bound_to_list(bound_by
);
2089 static void link_free_carrier_maps(Link
*link
) {
2090 bool list_updated
= false;
2094 if (!hashmap_isempty(link
->bound_to_links
)) {
2095 link_free_bound_to_list(link
);
2096 list_updated
= true;
2099 if (!hashmap_isempty(link
->bound_by_links
)) {
2100 link_free_bound_by_list(link
);
2101 list_updated
= true;
2110 void link_drop(Link
*link
) {
2111 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2114 link_set_state(link
, LINK_STATE_LINGER
);
2116 link_free_carrier_maps(link
);
2118 log_link_debug(link
, "Link removed");
2120 (void)unlink(link
->state_file
);
2126 static int link_joined(Link
*link
) {
2130 assert(link
->network
);
2132 if (!hashmap_isempty(link
->bound_to_links
)) {
2133 r
= link_handle_bound_to_list(link
);
2136 } else if (!(link
->flags
& IFF_UP
)) {
2139 link_enter_failed(link
);
2144 if (link
->network
->bridge
) {
2145 r
= link_set_bridge(link
);
2147 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2150 if (link
->network
->bond
) {
2151 r
= link_bond_set(link
);
2153 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2156 if (link
->network
->use_br_vlan
&&
2157 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2158 r
= link_set_bridge_vlan(link
);
2160 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2163 /* Skip setting up addresses until it gets carrier,
2164 or it would try to set addresses twice,
2165 which is bad for non-idempotent steps. */
2166 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2169 return link_enter_set_addresses(link
);
2172 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2173 _cleanup_link_unref_ Link
*link
= userdata
;
2177 assert(link
->network
);
2181 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2184 r
= sd_netlink_message_get_errno(m
);
2185 if (r
< 0 && r
!= -EEXIST
) {
2186 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2187 link_enter_failed(link
);
2190 log_link_debug(link
, "Joined netdev");
2192 if (link
->enslaving
<= 0)
2198 static int link_enter_join_netdev(Link
*link
) {
2204 assert(link
->network
);
2205 assert(link
->state
== LINK_STATE_PENDING
);
2207 link_set_state(link
, LINK_STATE_ENSLAVING
);
2211 if (!link
->network
->bridge
&&
2212 !link
->network
->bond
&&
2213 !link
->network
->vrf
&&
2214 hashmap_isempty(link
->network
->stacked_netdevs
))
2215 return link_joined(link
);
2217 if (link
->network
->bond
) {
2218 log_struct(LOG_DEBUG
,
2219 LOG_LINK_INTERFACE(link
),
2220 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2221 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
2224 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2226 log_struct_errno(LOG_WARNING
, r
,
2227 LOG_LINK_INTERFACE(link
),
2228 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2229 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
2232 link_enter_failed(link
);
2239 if (link
->network
->bridge
) {
2240 log_struct(LOG_DEBUG
,
2241 LOG_LINK_INTERFACE(link
),
2242 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2243 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
2246 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2248 log_struct_errno(LOG_WARNING
, r
,
2249 LOG_LINK_INTERFACE(link
),
2250 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2251 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
2253 link_enter_failed(link
);
2260 if (link
->network
->vrf
) {
2261 log_struct(LOG_DEBUG
,
2262 LOG_LINK_INTERFACE(link
),
2263 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2264 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
),
2266 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2268 log_struct_errno(LOG_WARNING
, r
,
2269 LOG_LINK_INTERFACE(link
),
2270 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2271 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
),
2273 link_enter_failed(link
);
2280 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2282 log_struct(LOG_DEBUG
,
2283 LOG_LINK_INTERFACE(link
),
2284 LOG_NETDEV_INTERFACE(netdev
),
2285 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
2288 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2290 log_struct_errno(LOG_WARNING
, r
,
2291 LOG_LINK_INTERFACE(link
),
2292 LOG_NETDEV_INTERFACE(netdev
),
2293 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
2295 link_enter_failed(link
);
2305 static int link_set_ipv4_forward(Link
*link
) {
2308 if (!link_ipv4_forward_enabled(link
))
2311 /* We propagate the forwarding flag from one interface to the
2312 * global setting one way. This means: as long as at least one
2313 * interface was configured at any time that had IP forwarding
2314 * enabled the setting will stay on for good. We do this
2315 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2316 * somewhat in sync (see below). */
2318 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2320 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2325 static int link_set_ipv6_forward(Link
*link
) {
2328 if (!link_ipv6_forward_enabled(link
))
2331 /* On Linux, the IPv6 stack does not know a per-interface
2332 * packet forwarding setting: either packet forwarding is on
2333 * for all, or off for all. We hence don't bother with a
2334 * per-interface setting, but simply propagate the interface
2335 * flag, if it is set, to the global flag, one-way. Note that
2336 * while IPv4 would allow a per-interface flag, we expose the
2337 * same behaviour there and also propagate the setting from
2338 * one to all, to keep things simple (see above). */
2340 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2342 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2347 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2348 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2349 IPv6PrivacyExtensions s
;
2350 const char *p
= NULL
;
2353 s
= link_ipv6_privacy_extensions(link
);
2357 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2358 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2360 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2362 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2367 static int link_set_ipv6_accept_ra(Link
*link
) {
2368 const char *p
= NULL
;
2371 /* Make this a NOP if IPv6 is not available */
2372 if (!socket_ipv6_is_supported())
2375 if (link
->flags
& IFF_LOOPBACK
)
2381 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2383 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2384 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2386 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2391 static int link_set_ipv6_dad_transmits(Link
*link
) {
2392 char buf
[DECIMAL_STR_MAX(int) + 1];
2393 const char *p
= NULL
;
2396 /* Make this a NOP if IPv6 is not available */
2397 if (!socket_ipv6_is_supported())
2400 if (link
->flags
& IFF_LOOPBACK
)
2406 if (link
->network
->ipv6_dad_transmits
< 0)
2409 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2410 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2412 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2414 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2419 static int link_set_ipv6_hop_limit(Link
*link
) {
2420 char buf
[DECIMAL_STR_MAX(int) + 1];
2421 const char *p
= NULL
;
2424 /* Make this a NOP if IPv6 is not available */
2425 if (!socket_ipv6_is_supported())
2428 if (link
->flags
& IFF_LOOPBACK
)
2434 if (link
->network
->ipv6_hop_limit
< 0)
2437 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2438 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2440 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2442 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2447 static int link_drop_foreign_config(Link
*link
) {
2453 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2454 /* we consider IPv6LL addresses to be managed by the kernel */
2455 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2458 r
= address_remove(address
, link
, link_address_remove_handler
);
2463 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2464 /* do not touch routes managed by the kernel */
2465 if (route
->protocol
== RTPROT_KERNEL
)
2468 r
= route_remove(route
, link
, link_route_remove_handler
);
2476 static int link_drop_config(Link
*link
) {
2477 Address
*address
, *pool_address
;
2482 SET_FOREACH(address
, link
->addresses
, i
) {
2483 /* we consider IPv6LL addresses to be managed by the kernel */
2484 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2487 r
= address_remove(address
, link
, link_address_remove_handler
);
2491 /* If this address came from an address pool, clean up the pool */
2492 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2493 if (address_equal(address
, pool_address
)) {
2494 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2495 address_free(pool_address
);
2501 SET_FOREACH(route
, link
->routes
, i
) {
2502 /* do not touch routes managed by the kernel */
2503 if (route
->protocol
== RTPROT_KERNEL
)
2506 r
= route_remove(route
, link
, link_route_remove_handler
);
2516 static int link_update_lldp(Link
*link
) {
2524 if (link
->flags
& IFF_UP
) {
2525 r
= sd_lldp_start(link
->lldp
);
2527 log_link_debug(link
, "Started LLDP.");
2529 r
= sd_lldp_stop(link
->lldp
);
2531 log_link_debug(link
, "Stopped LLDP.");
2537 static int link_configure(Link
*link
) {
2541 assert(link
->network
);
2542 assert(link
->state
== LINK_STATE_PENDING
);
2544 if (streq_ptr(link
->kind
, "vcan")) {
2546 if (!(link
->flags
& IFF_UP
)) {
2547 r
= link_up_can(link
);
2549 link_enter_failed(link
);
2557 /* Drop foreign config, but ignore loopback or critical devices.
2558 * We do not want to remove loopback address or addresses used for root NFS. */
2559 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2560 r
= link_drop_foreign_config(link
);
2565 r
= link_set_proxy_arp(link
);
2569 r
= ipv6_proxy_ndp_addresses_configure(link
);
2573 r
= link_set_ipv4_forward(link
);
2577 r
= link_set_ipv6_forward(link
);
2581 r
= link_set_ipv6_privacy_extensions(link
);
2585 r
= link_set_ipv6_accept_ra(link
);
2589 r
= link_set_ipv6_dad_transmits(link
);
2593 r
= link_set_ipv6_hop_limit(link
);
2597 r
= link_set_flags(link
);
2601 if (link_ipv4ll_enabled(link
)) {
2602 r
= ipv4ll_configure(link
);
2607 if (link_dhcp4_enabled(link
)) {
2608 r
= dhcp4_configure(link
);
2613 if (link_dhcp4_server_enabled(link
)) {
2614 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2618 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2623 if (link_dhcp6_enabled(link
) ||
2624 link_ipv6_accept_ra_enabled(link
)) {
2625 r
= dhcp6_configure(link
);
2630 if (link_ipv6_accept_ra_enabled(link
)) {
2631 r
= ndisc_configure(link
);
2636 if (link_radv_enabled(link
)) {
2637 r
= radv_configure(link
);
2642 if (link_lldp_rx_enabled(link
)) {
2643 r
= sd_lldp_new(&link
->lldp
);
2647 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2651 r
= sd_lldp_match_capabilities(link
->lldp
,
2652 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2653 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2654 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2658 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2662 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2666 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2670 r
= link_update_lldp(link
);
2675 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2676 r
= link_acquire_conf(link
);
2681 return link_enter_join_netdev(link
);
2684 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2686 _cleanup_link_unref_ Link
*link
= userdata
;
2691 assert(link
->ifname
);
2692 assert(link
->manager
);
2694 if (link
->state
!= LINK_STATE_PENDING
)
2697 log_link_debug(link
, "Link state is up-to-date");
2699 r
= link_new_bound_by_list(link
);
2703 r
= link_handle_bound_by_list(link
);
2707 if (!link
->network
) {
2708 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2709 &link
->mac
, &network
);
2711 link_enter_unmanaged(link
);
2713 } else if (r
== 0 && network
->unmanaged
) {
2714 link_enter_unmanaged(link
);
2719 if (link
->flags
& IFF_LOOPBACK
) {
2720 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2721 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2723 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2724 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2726 if (network
->dhcp_server
)
2727 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2730 r
= network_apply(network
, link
);
2735 r
= link_new_bound_to_list(link
);
2739 r
= link_configure(link
);
2746 int link_initialized(Link
*link
, struct udev_device
*device
) {
2747 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2751 assert(link
->manager
);
2752 assert(link
->manager
->rtnl
);
2755 if (link
->state
!= LINK_STATE_PENDING
)
2758 if (link
->udev_device
)
2761 log_link_debug(link
, "udev initialized link");
2763 link
->udev_device
= udev_device_ref(device
);
2765 /* udev has initialized the link, but we don't know if we have yet
2766 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2767 * when it returns we know that the pending NEWLINKs have already been
2768 * processed and that we are up-to-date */
2770 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2775 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2776 link_initialized_and_synced
, link
, 0, NULL
);
2785 static int link_load(Link
*link
) {
2786 _cleanup_free_
char *network_file
= NULL
,
2789 *dhcp4_address
= NULL
,
2790 *ipv4ll_address
= NULL
;
2791 union in_addr_union address
;
2792 union in_addr_union route_dst
;
2798 r
= parse_env_file(link
->state_file
, NEWLINE
,
2799 "NETWORK_FILE", &network_file
,
2800 "ADDRESSES", &addresses
,
2802 "DHCP4_ADDRESS", &dhcp4_address
,
2803 "IPV4LL_ADDRESS", &ipv4ll_address
,
2805 if (r
< 0 && r
!= -ENOENT
)
2806 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2813 suffix
= strrchr(network_file
, '.');
2815 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2816 goto network_file_fail
;
2820 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2822 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2823 goto network_file_fail
;
2826 r
= network_apply(network
, link
);
2828 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2837 _cleanup_free_
char *address_str
= NULL
;
2838 char *prefixlen_str
;
2840 unsigned char prefixlen
;
2842 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2844 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2850 prefixlen_str
= strchr(address_str
, '/');
2851 if (!prefixlen_str
) {
2852 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2856 *prefixlen_str
++ = '\0';
2858 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2860 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2864 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2866 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2870 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2872 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2881 _cleanup_free_
char *route_str
= NULL
;
2882 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2884 char *prefixlen_str
;
2886 unsigned char prefixlen
, tos
, table
;
2889 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2891 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2897 prefixlen_str
= strchr(route_str
, '/');
2898 if (!prefixlen_str
) {
2899 log_link_debug(link
, "Failed to parse route %s", route_str
);
2903 *prefixlen_str
++ = '\0';
2905 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2907 log_link_debug(link
,
2908 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2913 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2915 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2919 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2921 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2923 if (lifetime
!= USEC_INFINITY
) {
2924 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2925 0, route_expire_handler
, route
);
2927 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2930 route
->lifetime
= lifetime
;
2931 sd_event_source_unref(route
->expire
);
2932 route
->expire
= expire
;
2937 if (dhcp4_address
) {
2938 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2940 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
2941 goto dhcp4_address_fail
;
2944 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
->dhcp_anonymize
);
2946 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
2948 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2950 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
2955 if (ipv4ll_address
) {
2956 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2958 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
2959 goto ipv4ll_address_fail
;
2962 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2964 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
2966 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2968 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
2971 ipv4ll_address_fail
:
2976 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2978 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2979 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2987 r
= link_new(m
, message
, ret
);
2993 log_link_debug(link
, "Link %d added", link
->ifindex
);
2995 r
= link_load(link
);
2999 if (detect_container() <= 0) {
3000 /* not in a container, udev will be around */
3001 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3002 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
3004 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
3008 if (udev_device_get_is_initialized(device
) <= 0) {
3010 log_link_debug(link
, "link pending udev initialization...");
3014 r
= link_initialized(link
, device
);
3018 /* we are calling a callback directly, so must take a ref */
3021 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
3028 link_enter_failed(link
);
3032 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3037 log_link_info(link
, "Gained IPv6LL");
3039 link
->ipv6ll_address
= *address
;
3040 link_check_ready(link
);
3042 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3043 r
= link_acquire_ipv6_conf(link
);
3045 link_enter_failed(link
);
3053 static int link_carrier_gained(Link
*link
) {
3058 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3059 r
= link_acquire_conf(link
);
3061 link_enter_failed(link
);
3065 r
= link_enter_set_addresses(link
);
3070 r
= link_handle_bound_by_list(link
);
3077 static int link_carrier_lost(Link
*link
) {
3082 r
= link_stop_clients(link
);
3084 link_enter_failed(link
);
3088 if (link_dhcp4_server_enabled(link
))
3089 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3091 r
= link_drop_config(link
);
3095 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3096 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3097 r
= link_drop_foreign_config(link
);
3102 r
= link_handle_bound_by_list(link
);
3109 int link_carrier_reset(Link
*link
) {
3114 if (link_has_carrier(link
)) {
3115 r
= link_carrier_lost(link
);
3119 r
= link_carrier_gained(link
);
3123 log_link_info(link
, "Reset carrier");
3129 int link_update(Link
*link
, sd_netlink_message
*m
) {
3130 struct ether_addr mac
;
3133 bool had_carrier
, carrier_gained
, carrier_lost
;
3137 assert(link
->ifname
);
3140 if (link
->state
== LINK_STATE_LINGER
) {
3142 log_link_info(link
, "Link readded");
3143 link_set_state(link
, LINK_STATE_ENSLAVING
);
3145 r
= link_new_carrier_maps(link
);
3150 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3151 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3152 log_link_info(link
, "Renamed to %s", ifname
);
3154 link_free_carrier_maps(link
);
3156 r
= free_and_strdup(&link
->ifname
, ifname
);
3160 r
= link_new_carrier_maps(link
);
3165 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3166 if (r
>= 0 && mtu
> 0) {
3168 if (!link
->original_mtu
) {
3169 link
->original_mtu
= mtu
;
3170 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3173 if (link
->dhcp_client
) {
3174 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3177 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3183 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3185 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3189 /* The kernel may broadcast NEWLINK messages without the MAC address
3190 set, simply ignore them. */
3191 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3193 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3196 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3199 log_link_debug(link
, "MAC address: "
3200 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3201 mac
.ether_addr_octet
[0],
3202 mac
.ether_addr_octet
[1],
3203 mac
.ether_addr_octet
[2],
3204 mac
.ether_addr_octet
[3],
3205 mac
.ether_addr_octet
[4],
3206 mac
.ether_addr_octet
[5]);
3209 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3211 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3214 if (link
->dhcp_client
) {
3215 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3216 (const uint8_t *) &link
->mac
,
3220 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3222 switch (link
->network
->dhcp_client_identifier
) {
3223 case DHCP_CLIENT_ID_DUID
: {
3224 const DUID
*duid
= link_duid(link
);
3226 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
3227 link
->network
->iaid
,
3229 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3230 duid
->raw_data_len
);
3232 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
3235 case DHCP_CLIENT_ID_MAC
:
3236 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
3238 (const uint8_t *)&link
->mac
,
3241 return log_link_warning_errno(link
, r
, "Could not update MAC client id in DHCP client: %m");
3244 assert_not_reached("Unknown client identifier type.");
3248 if (link
->dhcp6_client
) {
3249 const DUID
* duid
= link_duid(link
);
3251 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3252 (const uint8_t *) &link
->mac
,
3256 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3258 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3259 link
->network
->iaid
);
3261 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3263 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3265 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3266 duid
->raw_data_len
);
3268 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3272 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3274 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3279 had_carrier
= link_has_carrier(link
);
3281 r
= link_update_flags(link
, m
);
3285 r
= link_update_lldp(link
);
3289 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3290 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3292 if (carrier_gained
) {
3293 log_link_info(link
, "Gained carrier");
3295 r
= link_carrier_gained(link
);
3298 } else if (carrier_lost
) {
3299 log_link_info(link
, "Lost carrier");
3301 r
= link_carrier_lost(link
);
3309 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3317 if (hashmap_isempty(h
))
3320 fputs_unlocked(prefix
, f
);
3321 HASHMAP_FOREACH(link
, h
, i
) {
3323 fputc_unlocked(' ', f
);
3325 fprintf(f
, "%i", link
->ifindex
);
3329 fputc_unlocked('\n', f
);
3332 int link_save(Link
*link
) {
3333 _cleanup_free_
char *temp_path
= NULL
;
3334 _cleanup_fclose_
FILE *f
= NULL
;
3335 const char *admin_state
, *oper_state
;
3342 assert(link
->state_file
);
3343 assert(link
->lease_file
);
3344 assert(link
->manager
);
3346 if (link
->state
== LINK_STATE_LINGER
) {
3347 unlink(link
->state_file
);
3351 link_lldp_save(link
);
3353 admin_state
= link_state_to_string(link
->state
);
3354 assert(admin_state
);
3356 oper_state
= link_operstate_to_string(link
->operstate
);
3359 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3363 (void) fchmod(fileno(f
), 0644);
3366 "# This is private data. Do not parse.\n"
3369 admin_state
, oper_state
);
3371 if (link
->network
) {
3373 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3374 const char *dhcp_domainname
= NULL
;
3375 char **dhcp6_domains
= NULL
;
3376 char **dhcp_domains
= NULL
;
3379 if (link
->dhcp6_client
) {
3380 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3381 if (r
< 0 && r
!= -ENOMSG
)
3382 log_link_debug(link
, "No DHCPv6 lease");
3385 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3387 fputs_unlocked("DNS=", f
);
3390 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3391 _cleanup_free_
char *b
= NULL
;
3393 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3394 &link
->network
->dns
[j
].address
, &b
);
3396 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3401 fputc_unlocked(' ', f
);
3402 fputs_unlocked(b
, f
);
3406 if (link
->network
->dhcp_use_dns
&&
3408 const struct in_addr
*addresses
;
3410 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3413 fputc_unlocked(' ', f
);
3414 serialize_in_addrs(f
, addresses
, r
);
3419 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3420 struct in6_addr
*in6_addrs
;
3422 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3425 fputc_unlocked(' ', f
);
3426 serialize_in6_addrs(f
, in6_addrs
, r
);
3431 /* Make sure to flush out old entries before we use the NDISC data */
3434 if (link
->network
->dhcp_use_dns
&& link
->ndisc_rdnss
) {
3437 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3439 fputc_unlocked(' ', f
);
3441 serialize_in6_addrs(f
, &dd
->address
, 1);
3446 fputc_unlocked('\n', f
);
3448 fputs_unlocked("NTP=", f
);
3450 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3452 if (link
->network
->dhcp_use_ntp
&&
3454 const struct in_addr
*addresses
;
3456 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3459 fputc_unlocked(' ', f
);
3460 serialize_in_addrs(f
, addresses
, r
);
3465 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3466 struct in6_addr
*in6_addrs
;
3469 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3473 fputc_unlocked(' ', f
);
3474 serialize_in6_addrs(f
, in6_addrs
, r
);
3478 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3480 fputstrv(f
, hosts
, NULL
, &space
);
3483 fputc_unlocked('\n', f
);
3485 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3486 if (link
->dhcp_lease
) {
3487 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3488 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3491 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3494 fputs_unlocked("DOMAINS=", f
);
3496 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3498 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3501 if (dhcp_domainname
)
3502 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3504 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3506 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3508 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3509 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3512 fputc_unlocked('\n', f
);
3514 fputs_unlocked("ROUTE_DOMAINS=", f
);
3516 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
3518 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3521 if (dhcp_domainname
)
3522 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3524 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3526 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3528 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3529 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3532 fputc_unlocked('\n', f
);
3534 fprintf(f
, "LLMNR=%s\n",
3535 resolve_support_to_string(link
->network
->llmnr
));
3536 fprintf(f
, "MDNS=%s\n",
3537 resolve_support_to_string(link
->network
->mdns
));
3539 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3540 fprintf(f
, "DNSSEC=%s\n",
3541 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3543 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3546 fputs_unlocked("DNSSEC_NTA=", f
);
3548 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3549 fputs_with_space(f
, n
, NULL
, &space
);
3550 fputc_unlocked('\n', f
);
3553 fputs_unlocked("ADDRESSES=", f
);
3555 SET_FOREACH(a
, link
->addresses
, i
) {
3556 _cleanup_free_
char *address_str
= NULL
;
3558 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3562 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3565 fputc_unlocked('\n', f
);
3567 fputs_unlocked("ROUTES=", f
);
3569 SET_FOREACH(route
, link
->routes
, i
) {
3570 _cleanup_free_
char *route_str
= NULL
;
3572 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3576 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
3577 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3581 fputc_unlocked('\n', f
);
3584 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3585 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3587 if (link
->dhcp_lease
) {
3588 struct in_addr address
;
3589 const char *tz
= NULL
;
3591 assert(link
->network
);
3593 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3595 fprintf(f
, "TIMEZONE=%s\n", tz
);
3597 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3599 fputs_unlocked("DHCP4_ADDRESS=", f
);
3600 serialize_in_addrs(f
, &address
, 1);
3601 fputc_unlocked('\n', f
);
3604 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3612 unlink(link
->lease_file
);
3615 struct in_addr address
;
3617 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3619 fputs_unlocked("IPV4LL_ADDRESS=", f
);
3620 serialize_in_addrs(f
, &address
, 1);
3621 fputc_unlocked('\n', f
);
3625 r
= fflush_and_check(f
);
3629 if (rename(temp_path
, link
->state_file
) < 0) {
3637 (void) unlink(link
->state_file
);
3639 (void) unlink(temp_path
);
3641 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3644 /* The serialized state in /run is no longer up-to-date. */
3645 void link_dirty(Link
*link
) {
3650 /* mark manager dirty as link is dirty */
3651 manager_dirty(link
->manager
);
3653 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3655 /* allocation errors are ignored */
3658 r
= set_put(link
->manager
->dirty_links
, link
);
3660 /* don't take another ref if the link was already dirty */
3666 /* The serialized state in /run is up-to-date */
3667 void link_clean(Link
*link
) {
3669 assert(link
->manager
);
3671 set_remove(link
->manager
->dirty_links
, link
);
3675 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3676 [LINK_STATE_PENDING
] = "pending",
3677 [LINK_STATE_ENSLAVING
] = "configuring",
3678 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3679 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3680 [LINK_STATE_CONFIGURED
] = "configured",
3681 [LINK_STATE_UNMANAGED
] = "unmanaged",
3682 [LINK_STATE_FAILED
] = "failed",
3683 [LINK_STATE_LINGER
] = "linger",
3686 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3688 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3689 [LINK_OPERSTATE_OFF
] = "off",
3690 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3691 [LINK_OPERSTATE_DORMANT
] = "dormant",
3692 [LINK_OPERSTATE_CARRIER
] = "carrier",
3693 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3694 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3697 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);