1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "conf-files.h"
7 #include "conf-parser.h"
10 #include "netdev/bond.h"
11 #include "netdev/bridge.h"
12 #include "netdev/dummy.h"
13 #include "netdev/fou-tunnel.h"
14 #include "netdev/geneve.h"
15 #include "netdev/ipvlan.h"
16 #include "netdev/l2tp-tunnel.h"
17 #include "netdev/macvlan.h"
18 #include "netdev/netdev.h"
19 #include "netdev/netdevsim.h"
20 #include "netdev/tunnel.h"
21 #include "netdev/tuntap.h"
22 #include "netdev/vcan.h"
23 #include "netdev/veth.h"
24 #include "netdev/vlan.h"
25 #include "netdev/vrf.h"
26 #include "netdev/vxcan.h"
27 #include "netdev/vxlan.h"
28 #include "netdev/wireguard.h"
29 #include "netlink-util.h"
30 #include "network-internal.h"
31 #include "networkd-link.h"
32 #include "networkd-manager.h"
33 #include "siphash24.h"
34 #include "stat-util.h"
35 #include "string-table.h"
36 #include "string-util.h"
39 const NetDevVTable
* const netdev_vtable
[_NETDEV_KIND_MAX
] = {
40 [NETDEV_KIND_BRIDGE
] = &bridge_vtable
,
41 [NETDEV_KIND_BOND
] = &bond_vtable
,
42 [NETDEV_KIND_VLAN
] = &vlan_vtable
,
43 [NETDEV_KIND_MACVLAN
] = &macvlan_vtable
,
44 [NETDEV_KIND_MACVTAP
] = &macvtap_vtable
,
45 [NETDEV_KIND_IPVLAN
] = &ipvlan_vtable
,
46 [NETDEV_KIND_VXLAN
] = &vxlan_vtable
,
47 [NETDEV_KIND_IPIP
] = &ipip_vtable
,
48 [NETDEV_KIND_GRE
] = &gre_vtable
,
49 [NETDEV_KIND_GRETAP
] = &gretap_vtable
,
50 [NETDEV_KIND_IP6GRE
] = &ip6gre_vtable
,
51 [NETDEV_KIND_IP6GRETAP
] = &ip6gretap_vtable
,
52 [NETDEV_KIND_SIT
] = &sit_vtable
,
53 [NETDEV_KIND_VTI
] = &vti_vtable
,
54 [NETDEV_KIND_VTI6
] = &vti6_vtable
,
55 [NETDEV_KIND_VETH
] = &veth_vtable
,
56 [NETDEV_KIND_DUMMY
] = &dummy_vtable
,
57 [NETDEV_KIND_TUN
] = &tun_vtable
,
58 [NETDEV_KIND_TAP
] = &tap_vtable
,
59 [NETDEV_KIND_IP6TNL
] = &ip6tnl_vtable
,
60 [NETDEV_KIND_VRF
] = &vrf_vtable
,
61 [NETDEV_KIND_VCAN
] = &vcan_vtable
,
62 [NETDEV_KIND_GENEVE
] = &geneve_vtable
,
63 [NETDEV_KIND_VXCAN
] = &vxcan_vtable
,
64 [NETDEV_KIND_WIREGUARD
] = &wireguard_vtable
,
65 [NETDEV_KIND_NETDEVSIM
] = &netdevsim_vtable
,
66 [NETDEV_KIND_FOU
] = &foutnl_vtable
,
67 [NETDEV_KIND_ERSPAN
] = &erspan_vtable
,
68 [NETDEV_KIND_L2TP
] = &l2tptnl_vtable
,
71 static const char* const netdev_kind_table
[_NETDEV_KIND_MAX
] = {
72 [NETDEV_KIND_BRIDGE
] = "bridge",
73 [NETDEV_KIND_BOND
] = "bond",
74 [NETDEV_KIND_VLAN
] = "vlan",
75 [NETDEV_KIND_MACVLAN
] = "macvlan",
76 [NETDEV_KIND_MACVTAP
] = "macvtap",
77 [NETDEV_KIND_IPVLAN
] = "ipvlan",
78 [NETDEV_KIND_VXLAN
] = "vxlan",
79 [NETDEV_KIND_IPIP
] = "ipip",
80 [NETDEV_KIND_GRE
] = "gre",
81 [NETDEV_KIND_GRETAP
] = "gretap",
82 [NETDEV_KIND_IP6GRE
] = "ip6gre",
83 [NETDEV_KIND_IP6GRETAP
] = "ip6gretap",
84 [NETDEV_KIND_SIT
] = "sit",
85 [NETDEV_KIND_VETH
] = "veth",
86 [NETDEV_KIND_VTI
] = "vti",
87 [NETDEV_KIND_VTI6
] = "vti6",
88 [NETDEV_KIND_DUMMY
] = "dummy",
89 [NETDEV_KIND_TUN
] = "tun",
90 [NETDEV_KIND_TAP
] = "tap",
91 [NETDEV_KIND_IP6TNL
] = "ip6tnl",
92 [NETDEV_KIND_VRF
] = "vrf",
93 [NETDEV_KIND_VCAN
] = "vcan",
94 [NETDEV_KIND_GENEVE
] = "geneve",
95 [NETDEV_KIND_VXCAN
] = "vxcan",
96 [NETDEV_KIND_WIREGUARD
] = "wireguard",
97 [NETDEV_KIND_NETDEVSIM
] = "netdevsim",
98 [NETDEV_KIND_FOU
] = "fou",
99 [NETDEV_KIND_ERSPAN
] = "erspan",
100 [NETDEV_KIND_L2TP
] = "l2tp",
103 DEFINE_STRING_TABLE_LOOKUP(netdev_kind
, NetDevKind
);
105 int config_parse_netdev_kind(
107 const char *filename
,
110 unsigned section_line
,
117 NetDevKind k
, *kind
= data
;
122 k
= netdev_kind_from_string(rvalue
);
124 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse netdev kind, ignoring assignment: %s", rvalue
);
128 if (*kind
!= _NETDEV_KIND_INVALID
&& *kind
!= k
) {
129 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
130 "Specified netdev kind is different from the previous value '%s', ignoring assignment: %s",
131 netdev_kind_to_string(*kind
), rvalue
);
140 static void netdev_callbacks_clear(NetDev
*netdev
) {
141 netdev_join_callback
*callback
;
146 while ((callback
= netdev
->callbacks
)) {
147 LIST_REMOVE(callbacks
, netdev
->callbacks
, callback
);
148 link_unref(callback
->link
);
153 bool netdev_is_managed(NetDev
*netdev
) {
154 if (!netdev
|| !netdev
->manager
|| !netdev
->ifname
)
157 return hashmap_get(netdev
->manager
->netdevs
, netdev
->ifname
) == netdev
;
160 static void netdev_detach_from_manager(NetDev
*netdev
) {
161 if (netdev
->ifname
&& netdev
->manager
)
162 hashmap_remove(netdev
->manager
->netdevs
, netdev
->ifname
);
165 static NetDev
*netdev_free(NetDev
*netdev
) {
168 netdev_callbacks_clear(netdev
);
170 netdev_detach_from_manager(netdev
);
172 free(netdev
->filename
);
174 free(netdev
->description
);
175 free(netdev
->ifname
);
177 condition_free_list(netdev
->conditions
);
179 /* Invoke the per-kind done() destructor, but only if the state field is initialized. We conditionalize that
180 * because we parse .netdev files twice: once to determine the kind (with a short, minimal NetDev structure
181 * allocation, with no room for per-kind fields), and once to read the kind's properties (with a full,
182 * comprehensive NetDev structure allocation with enough space for whatever the specific kind needs). Now, in
183 * the first case we shouldn't try to destruct the per-kind NetDev fields on destruction, in the second case we
184 * should. We use the state field to discern the two cases: it's _NETDEV_STATE_INVALID on the first "raw"
186 if (netdev
->state
!= _NETDEV_STATE_INVALID
&&
187 NETDEV_VTABLE(netdev
) &&
188 NETDEV_VTABLE(netdev
)->done
)
189 NETDEV_VTABLE(netdev
)->done(netdev
);
191 return mfree(netdev
);
194 DEFINE_TRIVIAL_REF_UNREF_FUNC(NetDev
, netdev
, netdev_free
);
196 void netdev_drop(NetDev
*netdev
) {
197 if (!netdev
|| netdev
->state
== NETDEV_STATE_LINGER
)
200 netdev
->state
= NETDEV_STATE_LINGER
;
202 log_netdev_debug(netdev
, "netdev removed");
204 netdev_callbacks_clear(netdev
);
206 netdev_detach_from_manager(netdev
);
208 netdev_unref(netdev
);
213 int netdev_get(Manager
*manager
, const char *name
, NetDev
**ret
) {
220 netdev
= hashmap_get(manager
->netdevs
, name
);
231 static int netdev_enter_failed(NetDev
*netdev
) {
232 netdev
->state
= NETDEV_STATE_FAILED
;
234 netdev_callbacks_clear(netdev
);
239 static int netdev_enslave_ready(NetDev
*netdev
, Link
* link
, link_netlink_message_handler_t callback
) {
240 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
244 assert(netdev
->state
== NETDEV_STATE_READY
);
245 assert(netdev
->manager
);
246 assert(netdev
->manager
->rtnl
);
247 assert(IN_SET(netdev
->kind
, NETDEV_KIND_BRIDGE
, NETDEV_KIND_BOND
, NETDEV_KIND_VRF
));
251 if (link
->flags
& IFF_UP
&& netdev
->kind
== NETDEV_KIND_BOND
) {
252 log_netdev_debug(netdev
, "Link '%s' was up when attempting to enslave it. Bringing link down.", link
->ifname
);
255 return log_netdev_error_errno(netdev
, r
, "Could not bring link down: %m");
258 r
= sd_rtnl_message_new_link(netdev
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
260 return log_netdev_error_errno(netdev
, r
, "Could not allocate RTM_SETLINK message: %m");
262 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, netdev
->ifindex
);
264 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_MASTER attribute: %m");
266 r
= netlink_call_async(netdev
->manager
->rtnl
, NULL
, req
, callback
,
267 link_netlink_destroy_callback
, link
);
269 return log_netdev_error_errno(netdev
, r
, "Could not send rtnetlink message: %m");
273 log_netdev_debug(netdev
, "Enslaving link '%s'", link
->ifname
);
278 static int netdev_enter_ready(NetDev
*netdev
) {
279 netdev_join_callback
*callback
, *callback_next
;
283 assert(netdev
->ifname
);
285 if (netdev
->state
!= NETDEV_STATE_CREATING
)
288 netdev
->state
= NETDEV_STATE_READY
;
290 log_netdev_info(netdev
, "netdev ready");
292 LIST_FOREACH_SAFE(callbacks
, callback
, callback_next
, netdev
->callbacks
) {
293 /* enslave the links that were attempted to be enslaved before the
295 r
= netdev_enslave_ready(netdev
, callback
->link
, callback
->callback
);
299 LIST_REMOVE(callbacks
, netdev
->callbacks
, callback
);
300 link_unref(callback
->link
);
304 if (NETDEV_VTABLE(netdev
)->post_create
)
305 NETDEV_VTABLE(netdev
)->post_create(netdev
, NULL
, NULL
);
310 /* callback for netdev's created without a backing Link */
311 static int netdev_create_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, NetDev
*netdev
) {
315 assert(netdev
->state
!= _NETDEV_STATE_INVALID
);
317 r
= sd_netlink_message_get_errno(m
);
319 log_netdev_info(netdev
, "netdev exists, using existing without changing its parameters");
321 log_netdev_warning_errno(netdev
, r
, "netdev could not be created: %m");
327 log_netdev_debug(netdev
, "Created");
332 static int netdev_enslave(NetDev
*netdev
, Link
*link
, link_netlink_message_handler_t callback
) {
336 assert(netdev
->manager
);
337 assert(netdev
->manager
->rtnl
);
338 assert(IN_SET(netdev
->kind
, NETDEV_KIND_BRIDGE
, NETDEV_KIND_BOND
, NETDEV_KIND_VRF
));
340 if (netdev
->state
== NETDEV_STATE_READY
) {
341 r
= netdev_enslave_ready(netdev
, link
, callback
);
344 } else if (IN_SET(netdev
->state
, NETDEV_STATE_LINGER
, NETDEV_STATE_FAILED
)) {
345 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
347 r
= rtnl_message_new_synthetic_error(netdev
->manager
->rtnl
, -ENODEV
, 0, &m
);
349 callback(netdev
->manager
->rtnl
, m
, link
);
351 /* the netdev is not yet read, save this request for when it is */
352 netdev_join_callback
*cb
;
354 cb
= new(netdev_join_callback
, 1);
358 *cb
= (netdev_join_callback
) {
359 .callback
= callback
,
360 .link
= link_ref(link
),
363 LIST_PREPEND(callbacks
, netdev
->callbacks
, cb
);
365 log_netdev_debug(netdev
, "Will enslave '%s', when ready", link
->ifname
);
371 int netdev_set_ifindex(NetDev
*netdev
, sd_netlink_message
*message
) {
374 const char *received_kind
;
375 const char *received_name
;
381 r
= sd_netlink_message_get_type(message
, &type
);
383 return log_netdev_error_errno(netdev
, r
, "Could not get rtnl message type: %m");
385 if (type
!= RTM_NEWLINK
) {
386 log_netdev_error(netdev
, "Cannot set ifindex from unexpected rtnl message type.");
390 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
392 log_netdev_error_errno(netdev
, r
, "Could not get ifindex: %m");
393 netdev_enter_failed(netdev
);
395 } else if (ifindex
<= 0) {
396 log_netdev_error(netdev
, "Got invalid ifindex: %d", ifindex
);
397 netdev_enter_failed(netdev
);
401 if (netdev
->ifindex
> 0) {
402 if (netdev
->ifindex
!= ifindex
) {
403 log_netdev_error(netdev
, "Could not set ifindex to %d, already set to %d",
404 ifindex
, netdev
->ifindex
);
405 netdev_enter_failed(netdev
);
408 /* ifindex already set to the same for this netdev */
412 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &received_name
);
414 return log_netdev_error_errno(netdev
, r
, "Could not get IFNAME: %m");
416 if (!streq(netdev
->ifname
, received_name
)) {
417 log_netdev_error(netdev
, "Received newlink with wrong IFNAME %s", received_name
);
418 netdev_enter_failed(netdev
);
422 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
424 return log_netdev_error_errno(netdev
, r
, "Could not get LINKINFO: %m");
426 r
= sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &received_kind
);
428 return log_netdev_error_errno(netdev
, r
, "Could not get KIND: %m");
430 r
= sd_netlink_message_exit_container(message
);
432 return log_netdev_error_errno(netdev
, r
, "Could not exit container: %m");
434 if (netdev
->kind
== NETDEV_KIND_TAP
)
435 /* the kernel does not distinguish between tun and tap */
438 kind
= netdev_kind_to_string(netdev
->kind
);
440 log_netdev_error(netdev
, "Could not get kind");
441 netdev_enter_failed(netdev
);
446 if (!streq(kind
, received_kind
)) {
447 log_netdev_error(netdev
,
448 "Received newlink with wrong KIND %s, "
449 "expected %s", received_kind
, kind
);
450 netdev_enter_failed(netdev
);
454 netdev
->ifindex
= ifindex
;
456 log_netdev_debug(netdev
, "netdev has index %d", netdev
->ifindex
);
458 netdev_enter_ready(netdev
);
463 #define HASH_KEY SD_ID128_MAKE(52,e1,45,bd,00,6f,29,96,21,c6,30,6d,83,71,04,48)
465 int netdev_get_mac(const char *ifname
, struct ether_addr
**ret
) {
466 _cleanup_free_
struct ether_addr
*mac
= NULL
;
475 mac
= new0(struct ether_addr
, 1);
480 sz
= sizeof(sd_id128_t
) + l
;
481 v
= newa(uint8_t, sz
);
483 /* fetch some persistent data unique to the machine */
484 r
= sd_id128_get_machine((sd_id128_t
*) v
);
488 /* combine with some data unique (on this machine) to this
490 memcpy(v
+ sizeof(sd_id128_t
), ifname
, l
);
492 /* Let's hash the host machine ID plus the container name. We
493 * use a fixed, but originally randomly created hash key here. */
494 result
= siphash24(v
, sz
, HASH_KEY
.bytes
);
496 assert_cc(ETH_ALEN
<= sizeof(result
));
497 memcpy(mac
->ether_addr_octet
, &result
, ETH_ALEN
);
499 /* see eth_random_addr in the kernel */
500 mac
->ether_addr_octet
[0] &= 0xfe; /* clear multicast bit */
501 mac
->ether_addr_octet
[0] |= 0x02; /* set local assignment bit (IEEE802) */
503 *ret
= TAKE_PTR(mac
);
508 static int netdev_create(NetDev
*netdev
, Link
*link
, link_netlink_message_handler_t callback
) {
512 assert(!link
|| callback
);
515 if (NETDEV_VTABLE(netdev
)->create
) {
518 r
= NETDEV_VTABLE(netdev
)->create(netdev
);
522 log_netdev_debug(netdev
, "Created");
524 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
526 r
= sd_rtnl_message_new_link(netdev
->manager
->rtnl
, &m
, RTM_NEWLINK
, 0);
528 return log_netdev_error_errno(netdev
, r
, "Could not allocate RTM_NEWLINK message: %m");
530 r
= sd_netlink_message_append_string(m
, IFLA_IFNAME
, netdev
->ifname
);
532 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IFNAME, attribute: %m");
535 r
= sd_netlink_message_append_ether_addr(m
, IFLA_ADDRESS
, netdev
->mac
);
537 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_ADDRESS attribute: %m");
541 r
= sd_netlink_message_append_u32(m
, IFLA_MTU
, netdev
->mtu
);
543 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_MTU attribute: %m");
547 r
= sd_netlink_message_append_u32(m
, IFLA_LINK
, link
->ifindex
);
549 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_LINK attribute: %m");
552 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
554 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_LINKINFO attribute: %m");
556 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, netdev_kind_to_string(netdev
->kind
));
558 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
560 if (NETDEV_VTABLE(netdev
)->fill_message_create
) {
561 r
= NETDEV_VTABLE(netdev
)->fill_message_create(netdev
, link
, m
);
566 r
= sd_netlink_message_close_container(m
);
568 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
570 r
= sd_netlink_message_close_container(m
);
572 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_LINKINFO attribute: %m");
575 r
= netlink_call_async(netdev
->manager
->rtnl
, NULL
, m
, callback
,
576 link_netlink_destroy_callback
, link
);
578 return log_netdev_error_errno(netdev
, r
, "Could not send rtnetlink message: %m");
582 r
= netlink_call_async(netdev
->manager
->rtnl
, NULL
, m
, netdev_create_handler
,
583 netdev_destroy_callback
, netdev
);
585 return log_netdev_error_errno(netdev
, r
, "Could not send rtnetlink message: %m");
590 netdev
->state
= NETDEV_STATE_CREATING
;
592 log_netdev_debug(netdev
, "Creating");
598 static int netdev_create_after_configured(NetDev
*netdev
, Link
*link
) {
601 assert(NETDEV_VTABLE(netdev
)->create_after_configured
);
603 return NETDEV_VTABLE(netdev
)->create_after_configured(netdev
, link
);
606 /* the callback must be called, possibly after a timeout, as otherwise the Link will hang */
607 int netdev_join(NetDev
*netdev
, Link
*link
, link_netlink_message_handler_t callback
) {
611 assert(netdev
->manager
);
612 assert(netdev
->manager
->rtnl
);
614 switch (netdev_get_create_type(netdev
)) {
615 case NETDEV_CREATE_MASTER
:
616 r
= netdev_enslave(netdev
, link
, callback
);
621 case NETDEV_CREATE_STACKED
:
622 r
= netdev_create(netdev
, link
, callback
);
627 case NETDEV_CREATE_AFTER_CONFIGURED
:
628 r
= netdev_create_after_configured(netdev
, link
);
633 assert_not_reached("Can not join independent netdev");
639 int netdev_load_one(Manager
*manager
, const char *filename
) {
640 _cleanup_(netdev_unrefp
) NetDev
*netdev_raw
= NULL
, *netdev
= NULL
;
641 _cleanup_fclose_
FILE *file
= NULL
;
642 const char *dropin_dirname
;
643 bool independent
= false;
649 file
= fopen(filename
, "re");
657 if (null_or_empty_fd(fileno(file
))) {
658 log_debug("Skipping empty file: %s", filename
);
662 netdev_raw
= new(NetDev
, 1);
666 *netdev_raw
= (NetDev
) {
668 .kind
= _NETDEV_KIND_INVALID
,
669 .state
= _NETDEV_STATE_INVALID
, /* an invalid state means done() of the implementation won't be called on destruction */
672 dropin_dirname
= strjoina(basename(filename
), ".d");
673 r
= config_parse_many(filename
, NETWORK_DIRS
, dropin_dirname
,
675 config_item_perf_lookup
, network_netdev_gperf_lookup
,
676 CONFIG_PARSE_WARN
|CONFIG_PARSE_RELAXED
, netdev_raw
);
680 /* skip out early if configuration does not match the environment */
681 if (!condition_test_list(netdev_raw
->conditions
, NULL
, NULL
, NULL
)) {
682 log_debug("%s: Conditions in the file do not match the system environment, skipping.", filename
);
686 if (netdev_raw
->kind
== _NETDEV_KIND_INVALID
) {
687 log_warning("NetDev has no Kind= configured in %s. Ignoring", filename
);
691 if (!netdev_raw
->ifname
) {
692 log_warning("NetDev without Name= configured in %s. Ignoring", filename
);
696 r
= fseek(file
, 0, SEEK_SET
);
700 netdev
= malloc0(NETDEV_VTABLE(netdev_raw
)->object_size
);
705 netdev
->manager
= manager
;
706 netdev
->kind
= netdev_raw
->kind
;
707 netdev
->state
= NETDEV_STATE_LOADING
; /* we initialize the state here for the first time,
708 so that done() will be called on destruction */
710 if (NETDEV_VTABLE(netdev
)->init
)
711 NETDEV_VTABLE(netdev
)->init(netdev
);
713 r
= config_parse_many(filename
, NETWORK_DIRS
, dropin_dirname
,
714 NETDEV_VTABLE(netdev
)->sections
,
715 config_item_perf_lookup
, network_netdev_gperf_lookup
,
716 CONFIG_PARSE_WARN
, netdev
);
720 /* verify configuration */
721 if (NETDEV_VTABLE(netdev
)->config_verify
) {
722 r
= NETDEV_VTABLE(netdev
)->config_verify(netdev
, filename
);
727 netdev
->filename
= strdup(filename
);
728 if (!netdev
->filename
)
731 if (!netdev
->mac
&& netdev
->kind
!= NETDEV_KIND_VLAN
) {
732 r
= netdev_get_mac(netdev
->ifname
, &netdev
->mac
);
734 return log_netdev_error_errno(netdev
, r
,
735 "Failed to generate predictable MAC address for %s: %m",
739 r
= hashmap_ensure_allocated(&netdev
->manager
->netdevs
, &string_hash_ops
);
743 r
= hashmap_put(netdev
->manager
->netdevs
, netdev
->ifname
, netdev
);
745 NetDev
*n
= hashmap_get(netdev
->manager
->netdevs
, netdev
->ifname
);
748 log_netdev_warning_errno(netdev
, r
,
749 "The setting Name=%s in %s conflicts with the one in %s, ignoring",
750 netdev
->ifname
, netdev
->filename
, n
->filename
);
752 /* Clear ifname before netdev_free() is called. Otherwise, the NetDev object 'n' is
753 * removed from the hashmap 'manager->netdevs'. */
754 netdev
->ifname
= mfree(netdev
->ifname
);
760 LIST_HEAD_INIT(netdev
->callbacks
);
762 log_netdev_debug(netdev
, "loaded %s", netdev_kind_to_string(netdev
->kind
));
764 if (IN_SET(netdev_get_create_type(netdev
), NETDEV_CREATE_MASTER
, NETDEV_CREATE_INDEPENDENT
)) {
765 r
= netdev_create(netdev
, NULL
, NULL
);
770 switch (netdev
->kind
) {
771 case NETDEV_KIND_IPIP
:
772 independent
= IPIP(netdev
)->independent
;
774 case NETDEV_KIND_GRE
:
775 independent
= GRE(netdev
)->independent
;
777 case NETDEV_KIND_GRETAP
:
778 independent
= GRETAP(netdev
)->independent
;
780 case NETDEV_KIND_IP6GRE
:
781 independent
= IP6GRE(netdev
)->independent
;
783 case NETDEV_KIND_IP6GRETAP
:
784 independent
= IP6GRETAP(netdev
)->independent
;
786 case NETDEV_KIND_SIT
:
787 independent
= SIT(netdev
)->independent
;
789 case NETDEV_KIND_VTI
:
790 independent
= VTI(netdev
)->independent
;
792 case NETDEV_KIND_VTI6
:
793 independent
= VTI6(netdev
)->independent
;
795 case NETDEV_KIND_IP6TNL
:
796 independent
= IP6TNL(netdev
)->independent
;
798 case NETDEV_KIND_ERSPAN
:
799 independent
= ERSPAN(netdev
)->independent
;
806 r
= netdev_create(netdev
, NULL
, NULL
);
816 int netdev_load(Manager
*manager
) {
817 _cleanup_strv_free_
char **files
= NULL
;
823 hashmap_clear_with_destructor(manager
->netdevs
, netdev_unref
);
825 r
= conf_files_list_strv(&files
, ".netdev", NULL
, 0, NETWORK_DIRS
);
827 return log_error_errno(r
, "Failed to enumerate netdev files: %m");
829 STRV_FOREACH(f
, files
) {
830 r
= netdev_load_one(manager
, *f
);