1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <netinet/in.h>
7 #include "alloc-util.h"
10 #include "conf-files.h"
11 #include "conf-parser.h"
14 #include "fou-tunnel.h"
18 #include "l2tp-tunnel.h"
23 #include "netdevsim.h"
24 #include "netlink-util.h"
25 #include "network-internal.h"
26 #include "networkd-manager.h"
28 #include "path-lookup.h"
29 #include "siphash24.h"
30 #include "stat-util.h"
31 #include "string-table.h"
32 #include "string-util.h"
42 #include "wireguard.h"
45 const NetDevVTable
* const netdev_vtable
[_NETDEV_KIND_MAX
] = {
46 [NETDEV_KIND_BRIDGE
] = &bridge_vtable
,
47 [NETDEV_KIND_BOND
] = &bond_vtable
,
48 [NETDEV_KIND_VLAN
] = &vlan_vtable
,
49 [NETDEV_KIND_MACVLAN
] = &macvlan_vtable
,
50 [NETDEV_KIND_MACVTAP
] = &macvtap_vtable
,
51 [NETDEV_KIND_IPVLAN
] = &ipvlan_vtable
,
52 [NETDEV_KIND_IPVTAP
] = &ipvtap_vtable
,
53 [NETDEV_KIND_VXLAN
] = &vxlan_vtable
,
54 [NETDEV_KIND_IPIP
] = &ipip_vtable
,
55 [NETDEV_KIND_GRE
] = &gre_vtable
,
56 [NETDEV_KIND_GRETAP
] = &gretap_vtable
,
57 [NETDEV_KIND_IP6GRE
] = &ip6gre_vtable
,
58 [NETDEV_KIND_IP6GRETAP
] = &ip6gretap_vtable
,
59 [NETDEV_KIND_SIT
] = &sit_vtable
,
60 [NETDEV_KIND_VTI
] = &vti_vtable
,
61 [NETDEV_KIND_VTI6
] = &vti6_vtable
,
62 [NETDEV_KIND_VETH
] = &veth_vtable
,
63 [NETDEV_KIND_DUMMY
] = &dummy_vtable
,
64 [NETDEV_KIND_TUN
] = &tun_vtable
,
65 [NETDEV_KIND_TAP
] = &tap_vtable
,
66 [NETDEV_KIND_IP6TNL
] = &ip6tnl_vtable
,
67 [NETDEV_KIND_VRF
] = &vrf_vtable
,
68 [NETDEV_KIND_VCAN
] = &vcan_vtable
,
69 [NETDEV_KIND_GENEVE
] = &geneve_vtable
,
70 [NETDEV_KIND_VXCAN
] = &vxcan_vtable
,
71 [NETDEV_KIND_WIREGUARD
] = &wireguard_vtable
,
72 [NETDEV_KIND_NETDEVSIM
] = &netdevsim_vtable
,
73 [NETDEV_KIND_FOU
] = &foutnl_vtable
,
74 [NETDEV_KIND_ERSPAN
] = &erspan_vtable
,
75 [NETDEV_KIND_L2TP
] = &l2tptnl_vtable
,
76 [NETDEV_KIND_MACSEC
] = &macsec_vtable
,
77 [NETDEV_KIND_NLMON
] = &nlmon_vtable
,
78 [NETDEV_KIND_XFRM
] = &xfrm_vtable
,
79 [NETDEV_KIND_IFB
] = &ifb_vtable
,
82 static const char* const netdev_kind_table
[_NETDEV_KIND_MAX
] = {
83 [NETDEV_KIND_BRIDGE
] = "bridge",
84 [NETDEV_KIND_BOND
] = "bond",
85 [NETDEV_KIND_VLAN
] = "vlan",
86 [NETDEV_KIND_MACVLAN
] = "macvlan",
87 [NETDEV_KIND_MACVTAP
] = "macvtap",
88 [NETDEV_KIND_IPVLAN
] = "ipvlan",
89 [NETDEV_KIND_IPVTAP
] = "ipvtap",
90 [NETDEV_KIND_VXLAN
] = "vxlan",
91 [NETDEV_KIND_IPIP
] = "ipip",
92 [NETDEV_KIND_GRE
] = "gre",
93 [NETDEV_KIND_GRETAP
] = "gretap",
94 [NETDEV_KIND_IP6GRE
] = "ip6gre",
95 [NETDEV_KIND_IP6GRETAP
] = "ip6gretap",
96 [NETDEV_KIND_SIT
] = "sit",
97 [NETDEV_KIND_VETH
] = "veth",
98 [NETDEV_KIND_VTI
] = "vti",
99 [NETDEV_KIND_VTI6
] = "vti6",
100 [NETDEV_KIND_DUMMY
] = "dummy",
101 [NETDEV_KIND_TUN
] = "tun",
102 [NETDEV_KIND_TAP
] = "tap",
103 [NETDEV_KIND_IP6TNL
] = "ip6tnl",
104 [NETDEV_KIND_VRF
] = "vrf",
105 [NETDEV_KIND_VCAN
] = "vcan",
106 [NETDEV_KIND_GENEVE
] = "geneve",
107 [NETDEV_KIND_VXCAN
] = "vxcan",
108 [NETDEV_KIND_WIREGUARD
] = "wireguard",
109 [NETDEV_KIND_NETDEVSIM
] = "netdevsim",
110 [NETDEV_KIND_FOU
] = "fou",
111 [NETDEV_KIND_ERSPAN
] = "erspan",
112 [NETDEV_KIND_L2TP
] = "l2tp",
113 [NETDEV_KIND_MACSEC
] = "macsec",
114 [NETDEV_KIND_NLMON
] = "nlmon",
115 [NETDEV_KIND_XFRM
] = "xfrm",
116 [NETDEV_KIND_IFB
] = "ifb",
119 DEFINE_STRING_TABLE_LOOKUP(netdev_kind
, NetDevKind
);
121 int config_parse_netdev_kind(
123 const char *filename
,
126 unsigned section_line
,
133 NetDevKind k
, *kind
= data
;
138 k
= netdev_kind_from_string(rvalue
);
140 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Failed to parse netdev kind, ignoring assignment: %s", rvalue
);
144 if (*kind
!= _NETDEV_KIND_INVALID
&& *kind
!= k
) {
145 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
146 "Specified netdev kind is different from the previous value '%s', ignoring assignment: %s",
147 netdev_kind_to_string(*kind
), rvalue
);
156 static void netdev_callbacks_clear(NetDev
*netdev
) {
157 netdev_join_callback
*callback
;
162 while ((callback
= netdev
->callbacks
)) {
163 LIST_REMOVE(callbacks
, netdev
->callbacks
, callback
);
164 link_unref(callback
->link
);
169 bool netdev_is_managed(NetDev
*netdev
) {
170 if (!netdev
|| !netdev
->manager
|| !netdev
->ifname
)
173 return hashmap_get(netdev
->manager
->netdevs
, netdev
->ifname
) == netdev
;
176 static void netdev_detach_from_manager(NetDev
*netdev
) {
177 if (netdev
->ifname
&& netdev
->manager
)
178 hashmap_remove(netdev
->manager
->netdevs
, netdev
->ifname
);
181 static NetDev
*netdev_free(NetDev
*netdev
) {
184 netdev_callbacks_clear(netdev
);
186 netdev_detach_from_manager(netdev
);
188 free(netdev
->filename
);
190 free(netdev
->description
);
191 free(netdev
->ifname
);
193 condition_free_list(netdev
->conditions
);
195 /* Invoke the per-kind done() destructor, but only if the state field is initialized. We conditionalize that
196 * because we parse .netdev files twice: once to determine the kind (with a short, minimal NetDev structure
197 * allocation, with no room for per-kind fields), and once to read the kind's properties (with a full,
198 * comprehensive NetDev structure allocation with enough space for whatever the specific kind needs). Now, in
199 * the first case we shouldn't try to destruct the per-kind NetDev fields on destruction, in the second case we
200 * should. We use the state field to discern the two cases: it's _NETDEV_STATE_INVALID on the first "raw"
202 if (netdev
->state
!= _NETDEV_STATE_INVALID
&&
203 NETDEV_VTABLE(netdev
) &&
204 NETDEV_VTABLE(netdev
)->done
)
205 NETDEV_VTABLE(netdev
)->done(netdev
);
207 return mfree(netdev
);
210 DEFINE_TRIVIAL_REF_UNREF_FUNC(NetDev
, netdev
, netdev_free
);
212 void netdev_drop(NetDev
*netdev
) {
213 if (!netdev
|| netdev
->state
== NETDEV_STATE_LINGER
)
216 netdev
->state
= NETDEV_STATE_LINGER
;
218 log_netdev_debug(netdev
, "netdev removed");
220 netdev_callbacks_clear(netdev
);
222 netdev_detach_from_manager(netdev
);
224 netdev_unref(netdev
);
229 int netdev_get(Manager
*manager
, const char *name
, NetDev
**ret
) {
236 netdev
= hashmap_get(manager
->netdevs
, name
);
247 static int netdev_enter_failed(NetDev
*netdev
) {
248 netdev
->state
= NETDEV_STATE_FAILED
;
250 netdev_callbacks_clear(netdev
);
255 static int netdev_enslave_ready(NetDev
*netdev
, Link
* link
, link_netlink_message_handler_t callback
) {
256 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
260 assert(netdev
->state
== NETDEV_STATE_READY
);
261 assert(netdev
->manager
);
262 assert(netdev
->manager
->rtnl
);
263 assert(IN_SET(netdev
->kind
, NETDEV_KIND_BRIDGE
, NETDEV_KIND_BOND
, NETDEV_KIND_VRF
));
267 if (link
->flags
& IFF_UP
&& netdev
->kind
== NETDEV_KIND_BOND
) {
268 log_netdev_debug(netdev
, "Link '%s' was up when attempting to enslave it. Bringing link down.", link
->ifname
);
269 r
= link_down(link
, NULL
);
271 return log_netdev_error_errno(netdev
, r
, "Could not bring link down: %m");
274 r
= sd_rtnl_message_new_link(netdev
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
276 return log_netdev_error_errno(netdev
, r
, "Could not allocate RTM_SETLINK message: %m");
278 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, netdev
->ifindex
);
280 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_MASTER attribute: %m");
282 r
= netlink_call_async(netdev
->manager
->rtnl
, NULL
, req
, callback
,
283 link_netlink_destroy_callback
, link
);
285 return log_netdev_error_errno(netdev
, r
, "Could not send rtnetlink message: %m");
289 log_netdev_debug(netdev
, "Enslaving link '%s'", link
->ifname
);
294 static int netdev_enter_ready(NetDev
*netdev
) {
295 netdev_join_callback
*callback
, *callback_next
;
299 assert(netdev
->ifname
);
301 if (netdev
->state
!= NETDEV_STATE_CREATING
)
304 netdev
->state
= NETDEV_STATE_READY
;
306 log_netdev_info(netdev
, "netdev ready");
308 LIST_FOREACH_SAFE(callbacks
, callback
, callback_next
, netdev
->callbacks
) {
309 /* enslave the links that were attempted to be enslaved before the
311 r
= netdev_enslave_ready(netdev
, callback
->link
, callback
->callback
);
315 LIST_REMOVE(callbacks
, netdev
->callbacks
, callback
);
316 link_unref(callback
->link
);
320 if (NETDEV_VTABLE(netdev
)->post_create
)
321 NETDEV_VTABLE(netdev
)->post_create(netdev
, NULL
, NULL
);
326 /* callback for netdev's created without a backing Link */
327 static int netdev_create_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, NetDev
*netdev
) {
331 assert(netdev
->state
!= _NETDEV_STATE_INVALID
);
333 r
= sd_netlink_message_get_errno(m
);
335 log_netdev_info(netdev
, "netdev exists, using existing without changing its parameters");
337 log_netdev_warning_errno(netdev
, r
, "netdev could not be created: %m");
343 log_netdev_debug(netdev
, "Created");
348 static int netdev_enslave(NetDev
*netdev
, Link
*link
, link_netlink_message_handler_t callback
) {
352 assert(netdev
->manager
);
353 assert(netdev
->manager
->rtnl
);
354 assert(IN_SET(netdev
->kind
, NETDEV_KIND_BRIDGE
, NETDEV_KIND_BOND
, NETDEV_KIND_VRF
));
356 if (netdev
->state
== NETDEV_STATE_READY
) {
357 r
= netdev_enslave_ready(netdev
, link
, callback
);
360 } else if (IN_SET(netdev
->state
, NETDEV_STATE_LINGER
, NETDEV_STATE_FAILED
)) {
361 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
363 r
= rtnl_message_new_synthetic_error(netdev
->manager
->rtnl
, -ENODEV
, 0, &m
);
365 callback(netdev
->manager
->rtnl
, m
, link
);
367 /* the netdev is not yet ready, save this request for when it is */
368 netdev_join_callback
*cb
;
370 cb
= new(netdev_join_callback
, 1);
374 *cb
= (netdev_join_callback
) {
375 .callback
= callback
,
376 .link
= link_ref(link
),
379 LIST_PREPEND(callbacks
, netdev
->callbacks
, cb
);
381 log_netdev_debug(netdev
, "Will enslave '%s', when ready", link
->ifname
);
387 int netdev_set_ifindex(NetDev
*netdev
, sd_netlink_message
*message
) {
390 const char *received_kind
;
391 const char *received_name
;
397 r
= sd_netlink_message_get_type(message
, &type
);
399 return log_netdev_error_errno(netdev
, r
, "Could not get rtnl message type: %m");
401 if (type
!= RTM_NEWLINK
) {
402 log_netdev_error(netdev
, "Cannot set ifindex from unexpected rtnl message type.");
406 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
408 log_netdev_error_errno(netdev
, r
, "Could not get ifindex: %m");
409 netdev_enter_failed(netdev
);
411 } else if (ifindex
<= 0) {
412 log_netdev_error(netdev
, "Got invalid ifindex: %d", ifindex
);
413 netdev_enter_failed(netdev
);
417 if (netdev
->ifindex
> 0) {
418 if (netdev
->ifindex
!= ifindex
) {
419 log_netdev_error(netdev
, "Could not set ifindex to %d, already set to %d",
420 ifindex
, netdev
->ifindex
);
421 netdev_enter_failed(netdev
);
424 /* ifindex already set to the same for this netdev */
428 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &received_name
);
430 return log_netdev_error_errno(netdev
, r
, "Could not get IFNAME: %m");
432 if (!streq(netdev
->ifname
, received_name
)) {
433 log_netdev_error(netdev
, "Received newlink with wrong IFNAME %s", received_name
);
434 netdev_enter_failed(netdev
);
438 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
440 return log_netdev_error_errno(netdev
, r
, "Could not get LINKINFO: %m");
442 r
= sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &received_kind
);
444 return log_netdev_error_errno(netdev
, r
, "Could not get KIND: %m");
446 r
= sd_netlink_message_exit_container(message
);
448 return log_netdev_error_errno(netdev
, r
, "Could not exit container: %m");
450 if (netdev
->kind
== NETDEV_KIND_TAP
)
451 /* the kernel does not distinguish between tun and tap */
454 kind
= netdev_kind_to_string(netdev
->kind
);
456 log_netdev_error(netdev
, "Could not get kind");
457 netdev_enter_failed(netdev
);
462 if (!streq(kind
, received_kind
)) {
463 log_netdev_error(netdev
,
464 "Received newlink with wrong KIND %s, "
465 "expected %s", received_kind
, kind
);
466 netdev_enter_failed(netdev
);
470 netdev
->ifindex
= ifindex
;
472 log_netdev_debug(netdev
, "netdev has index %d", netdev
->ifindex
);
474 netdev_enter_ready(netdev
);
479 #define HASH_KEY SD_ID128_MAKE(52,e1,45,bd,00,6f,29,96,21,c6,30,6d,83,71,04,48)
481 int netdev_get_mac(const char *ifname
, struct ether_addr
**ret
) {
482 _cleanup_free_
struct ether_addr
*mac
= NULL
;
491 mac
= new0(struct ether_addr
, 1);
496 sz
= sizeof(sd_id128_t
) + l
;
497 v
= newa(uint8_t, sz
);
499 /* fetch some persistent data unique to the machine */
500 r
= sd_id128_get_machine((sd_id128_t
*) v
);
504 /* combine with some data unique (on this machine) to this
506 memcpy(v
+ sizeof(sd_id128_t
), ifname
, l
);
508 /* Let's hash the host machine ID plus the container name. We
509 * use a fixed, but originally randomly created hash key here. */
510 result
= siphash24(v
, sz
, HASH_KEY
.bytes
);
512 assert_cc(ETH_ALEN
<= sizeof(result
));
513 memcpy(mac
->ether_addr_octet
, &result
, ETH_ALEN
);
515 /* see eth_random_addr in the kernel */
516 mac
->ether_addr_octet
[0] &= 0xfe; /* clear multicast bit */
517 mac
->ether_addr_octet
[0] |= 0x02; /* set local assignment bit (IEEE802) */
519 *ret
= TAKE_PTR(mac
);
524 static int netdev_create(NetDev
*netdev
, Link
*link
, link_netlink_message_handler_t callback
) {
528 assert(!link
|| callback
);
531 if (NETDEV_VTABLE(netdev
)->create
) {
534 r
= NETDEV_VTABLE(netdev
)->create(netdev
);
538 log_netdev_debug(netdev
, "Created");
540 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
542 r
= sd_rtnl_message_new_link(netdev
->manager
->rtnl
, &m
, RTM_NEWLINK
, 0);
544 return log_netdev_error_errno(netdev
, r
, "Could not allocate RTM_NEWLINK message: %m");
546 r
= sd_netlink_message_append_string(m
, IFLA_IFNAME
, netdev
->ifname
);
548 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IFNAME, attribute: %m");
551 r
= sd_netlink_message_append_ether_addr(m
, IFLA_ADDRESS
, netdev
->mac
);
553 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_ADDRESS attribute: %m");
557 r
= sd_netlink_message_append_u32(m
, IFLA_MTU
, netdev
->mtu
);
559 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_MTU attribute: %m");
563 r
= sd_netlink_message_append_u32(m
, IFLA_LINK
, link
->ifindex
);
565 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_LINK attribute: %m");
568 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
570 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_LINKINFO attribute: %m");
572 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, netdev_kind_to_string(netdev
->kind
));
574 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
576 if (NETDEV_VTABLE(netdev
)->fill_message_create
) {
577 r
= NETDEV_VTABLE(netdev
)->fill_message_create(netdev
, link
, m
);
582 r
= sd_netlink_message_close_container(m
);
584 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
586 r
= sd_netlink_message_close_container(m
);
588 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_LINKINFO attribute: %m");
591 r
= netlink_call_async(netdev
->manager
->rtnl
, NULL
, m
, callback
,
592 link_netlink_destroy_callback
, link
);
594 return log_netdev_error_errno(netdev
, r
, "Could not send rtnetlink message: %m");
598 r
= netlink_call_async(netdev
->manager
->rtnl
, NULL
, m
, netdev_create_handler
,
599 netdev_destroy_callback
, netdev
);
601 return log_netdev_error_errno(netdev
, r
, "Could not send rtnetlink message: %m");
606 netdev
->state
= NETDEV_STATE_CREATING
;
608 log_netdev_debug(netdev
, "Creating");
614 static int netdev_create_after_configured(NetDev
*netdev
, Link
*link
) {
617 assert(NETDEV_VTABLE(netdev
)->create_after_configured
);
619 return NETDEV_VTABLE(netdev
)->create_after_configured(netdev
, link
);
622 /* the callback must be called, possibly after a timeout, as otherwise the Link will hang */
623 int netdev_join(NetDev
*netdev
, Link
*link
, link_netlink_message_handler_t callback
) {
627 assert(netdev
->manager
);
628 assert(netdev
->manager
->rtnl
);
630 switch (netdev_get_create_type(netdev
)) {
631 case NETDEV_CREATE_MASTER
:
632 r
= netdev_enslave(netdev
, link
, callback
);
637 case NETDEV_CREATE_STACKED
:
638 r
= netdev_create(netdev
, link
, callback
);
643 case NETDEV_CREATE_AFTER_CONFIGURED
:
644 r
= netdev_create_after_configured(netdev
, link
);
649 assert_not_reached("Can not join independent netdev");
655 int netdev_load_one(Manager
*manager
, const char *filename
) {
656 _cleanup_(netdev_unrefp
) NetDev
*netdev_raw
= NULL
, *netdev
= NULL
;
657 _cleanup_fclose_
FILE *file
= NULL
;
658 const char *dropin_dirname
;
659 bool independent
= false;
665 file
= fopen(filename
, "re");
673 if (null_or_empty_fd(fileno(file
))) {
674 log_debug("Skipping empty file: %s", filename
);
678 netdev_raw
= new(NetDev
, 1);
682 *netdev_raw
= (NetDev
) {
684 .kind
= _NETDEV_KIND_INVALID
,
685 .state
= _NETDEV_STATE_INVALID
, /* an invalid state means done() of the implementation won't be called on destruction */
688 dropin_dirname
= strjoina(basename(filename
), ".d");
689 r
= config_parse_many(
690 filename
, NETWORK_DIRS
, dropin_dirname
,
691 NETDEV_COMMON_SECTIONS NETDEV_OTHER_SECTIONS
,
692 config_item_perf_lookup
, network_netdev_gperf_lookup
,
699 /* skip out early if configuration does not match the environment */
700 if (!condition_test_list(netdev_raw
->conditions
, environ
, NULL
, NULL
, NULL
)) {
701 log_debug("%s: Conditions in the file do not match the system environment, skipping.", filename
);
705 if (netdev_raw
->kind
== _NETDEV_KIND_INVALID
) {
706 log_warning("NetDev has no Kind= configured in %s. Ignoring", filename
);
710 if (!netdev_raw
->ifname
) {
711 log_warning("NetDev without Name= configured in %s. Ignoring", filename
);
715 r
= fseek(file
, 0, SEEK_SET
);
719 netdev
= malloc0(NETDEV_VTABLE(netdev_raw
)->object_size
);
724 netdev
->manager
= manager
;
725 netdev
->kind
= netdev_raw
->kind
;
726 netdev
->state
= NETDEV_STATE_LOADING
; /* we initialize the state here for the first time,
727 so that done() will be called on destruction */
729 if (NETDEV_VTABLE(netdev
)->init
)
730 NETDEV_VTABLE(netdev
)->init(netdev
);
732 r
= config_parse_many(
733 filename
, NETWORK_DIRS
, dropin_dirname
,
734 NETDEV_VTABLE(netdev
)->sections
,
735 config_item_perf_lookup
, network_netdev_gperf_lookup
,
741 /* verify configuration */
742 if (NETDEV_VTABLE(netdev
)->config_verify
) {
743 r
= NETDEV_VTABLE(netdev
)->config_verify(netdev
, filename
);
748 netdev
->filename
= strdup(filename
);
749 if (!netdev
->filename
)
752 if (!netdev
->mac
&& NETDEV_VTABLE(netdev
)->generate_mac
) {
753 r
= netdev_get_mac(netdev
->ifname
, &netdev
->mac
);
755 return log_netdev_error_errno(netdev
, r
,
756 "Failed to generate predictable MAC address for %s: %m",
760 r
= hashmap_ensure_allocated(&netdev
->manager
->netdevs
, &string_hash_ops
);
764 r
= hashmap_put(netdev
->manager
->netdevs
, netdev
->ifname
, netdev
);
766 NetDev
*n
= hashmap_get(netdev
->manager
->netdevs
, netdev
->ifname
);
769 if (!streq(netdev
->filename
, n
->filename
))
770 log_netdev_warning_errno(netdev
, r
,
771 "The setting Name=%s in %s conflicts with the one in %s, ignoring",
772 netdev
->ifname
, netdev
->filename
, n
->filename
);
774 /* Clear ifname before netdev_free() is called. Otherwise, the NetDev object 'n' is
775 * removed from the hashmap 'manager->netdevs'. */
776 netdev
->ifname
= mfree(netdev
->ifname
);
782 LIST_HEAD_INIT(netdev
->callbacks
);
784 log_netdev_debug(netdev
, "loaded %s", netdev_kind_to_string(netdev
->kind
));
786 if (IN_SET(netdev_get_create_type(netdev
), NETDEV_CREATE_MASTER
, NETDEV_CREATE_INDEPENDENT
)) {
787 r
= netdev_create(netdev
, NULL
, NULL
);
792 switch (netdev
->kind
) {
793 case NETDEV_KIND_IPIP
:
794 independent
= IPIP(netdev
)->independent
;
796 case NETDEV_KIND_GRE
:
797 independent
= GRE(netdev
)->independent
;
799 case NETDEV_KIND_GRETAP
:
800 independent
= GRETAP(netdev
)->independent
;
802 case NETDEV_KIND_IP6GRE
:
803 independent
= IP6GRE(netdev
)->independent
;
805 case NETDEV_KIND_IP6GRETAP
:
806 independent
= IP6GRETAP(netdev
)->independent
;
808 case NETDEV_KIND_SIT
:
809 independent
= SIT(netdev
)->independent
;
811 case NETDEV_KIND_VTI
:
812 independent
= VTI(netdev
)->independent
;
814 case NETDEV_KIND_VTI6
:
815 independent
= VTI6(netdev
)->independent
;
817 case NETDEV_KIND_IP6TNL
:
818 independent
= IP6TNL(netdev
)->independent
;
820 case NETDEV_KIND_ERSPAN
:
821 independent
= ERSPAN(netdev
)->independent
;
823 case NETDEV_KIND_XFRM
:
824 independent
= XFRM(netdev
)->independent
;
831 r
= netdev_create(netdev
, NULL
, NULL
);
841 int netdev_load(Manager
*manager
, bool reload
) {
842 _cleanup_strv_free_
char **files
= NULL
;
849 hashmap_clear_with_destructor(manager
->netdevs
, netdev_unref
);
851 r
= conf_files_list_strv(&files
, ".netdev", NULL
, 0, NETWORK_DIRS
);
853 return log_error_errno(r
, "Failed to enumerate netdev files: %m");
855 STRV_FOREACH(f
, files
) {
856 r
= netdev_load_one(manager
, *f
);
858 log_error_errno(r
, "Failed to load %s, ignoring: %m", *f
);