1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <netinet/in.h>
6 #include "alloc-util.h"
7 #include "conf-files.h"
8 #include "conf-parser.h"
11 #include "netdev/bond.h"
12 #include "netdev/bridge.h"
13 #include "netdev/dummy.h"
14 #include "netdev/fou-tunnel.h"
15 #include "netdev/geneve.h"
16 #include "netdev/ipvlan.h"
17 #include "netdev/l2tp-tunnel.h"
18 #include "netdev/macsec.h"
19 #include "netdev/macvlan.h"
20 #include "netdev/netdev.h"
21 #include "netdev/netdevsim.h"
22 #include "netdev/tunnel.h"
23 #include "netdev/tuntap.h"
24 #include "netdev/vcan.h"
25 #include "netdev/veth.h"
26 #include "netdev/vlan.h"
27 #include "netdev/vrf.h"
28 #include "netdev/vxcan.h"
29 #include "netdev/vxlan.h"
30 #include "netdev/wireguard.h"
31 #include "netlink-util.h"
32 #include "network-internal.h"
33 #include "networkd-link.h"
34 #include "networkd-manager.h"
35 #include "siphash24.h"
36 #include "stat-util.h"
37 #include "string-table.h"
38 #include "string-util.h"
41 const NetDevVTable
* const netdev_vtable
[_NETDEV_KIND_MAX
] = {
42 [NETDEV_KIND_BRIDGE
] = &bridge_vtable
,
43 [NETDEV_KIND_BOND
] = &bond_vtable
,
44 [NETDEV_KIND_VLAN
] = &vlan_vtable
,
45 [NETDEV_KIND_MACVLAN
] = &macvlan_vtable
,
46 [NETDEV_KIND_MACVTAP
] = &macvtap_vtable
,
47 [NETDEV_KIND_IPVLAN
] = &ipvlan_vtable
,
48 [NETDEV_KIND_VXLAN
] = &vxlan_vtable
,
49 [NETDEV_KIND_IPIP
] = &ipip_vtable
,
50 [NETDEV_KIND_GRE
] = &gre_vtable
,
51 [NETDEV_KIND_GRETAP
] = &gretap_vtable
,
52 [NETDEV_KIND_IP6GRE
] = &ip6gre_vtable
,
53 [NETDEV_KIND_IP6GRETAP
] = &ip6gretap_vtable
,
54 [NETDEV_KIND_SIT
] = &sit_vtable
,
55 [NETDEV_KIND_VTI
] = &vti_vtable
,
56 [NETDEV_KIND_VTI6
] = &vti6_vtable
,
57 [NETDEV_KIND_VETH
] = &veth_vtable
,
58 [NETDEV_KIND_DUMMY
] = &dummy_vtable
,
59 [NETDEV_KIND_TUN
] = &tun_vtable
,
60 [NETDEV_KIND_TAP
] = &tap_vtable
,
61 [NETDEV_KIND_IP6TNL
] = &ip6tnl_vtable
,
62 [NETDEV_KIND_VRF
] = &vrf_vtable
,
63 [NETDEV_KIND_VCAN
] = &vcan_vtable
,
64 [NETDEV_KIND_GENEVE
] = &geneve_vtable
,
65 [NETDEV_KIND_VXCAN
] = &vxcan_vtable
,
66 [NETDEV_KIND_WIREGUARD
] = &wireguard_vtable
,
67 [NETDEV_KIND_NETDEVSIM
] = &netdevsim_vtable
,
68 [NETDEV_KIND_FOU
] = &foutnl_vtable
,
69 [NETDEV_KIND_ERSPAN
] = &erspan_vtable
,
70 [NETDEV_KIND_L2TP
] = &l2tptnl_vtable
,
71 [NETDEV_KIND_MACSEC
] = &macsec_vtable
,
74 static const char* const netdev_kind_table
[_NETDEV_KIND_MAX
] = {
75 [NETDEV_KIND_BRIDGE
] = "bridge",
76 [NETDEV_KIND_BOND
] = "bond",
77 [NETDEV_KIND_VLAN
] = "vlan",
78 [NETDEV_KIND_MACVLAN
] = "macvlan",
79 [NETDEV_KIND_MACVTAP
] = "macvtap",
80 [NETDEV_KIND_IPVLAN
] = "ipvlan",
81 [NETDEV_KIND_VXLAN
] = "vxlan",
82 [NETDEV_KIND_IPIP
] = "ipip",
83 [NETDEV_KIND_GRE
] = "gre",
84 [NETDEV_KIND_GRETAP
] = "gretap",
85 [NETDEV_KIND_IP6GRE
] = "ip6gre",
86 [NETDEV_KIND_IP6GRETAP
] = "ip6gretap",
87 [NETDEV_KIND_SIT
] = "sit",
88 [NETDEV_KIND_VETH
] = "veth",
89 [NETDEV_KIND_VTI
] = "vti",
90 [NETDEV_KIND_VTI6
] = "vti6",
91 [NETDEV_KIND_DUMMY
] = "dummy",
92 [NETDEV_KIND_TUN
] = "tun",
93 [NETDEV_KIND_TAP
] = "tap",
94 [NETDEV_KIND_IP6TNL
] = "ip6tnl",
95 [NETDEV_KIND_VRF
] = "vrf",
96 [NETDEV_KIND_VCAN
] = "vcan",
97 [NETDEV_KIND_GENEVE
] = "geneve",
98 [NETDEV_KIND_VXCAN
] = "vxcan",
99 [NETDEV_KIND_WIREGUARD
] = "wireguard",
100 [NETDEV_KIND_NETDEVSIM
] = "netdevsim",
101 [NETDEV_KIND_FOU
] = "fou",
102 [NETDEV_KIND_ERSPAN
] = "erspan",
103 [NETDEV_KIND_L2TP
] = "l2tp",
104 [NETDEV_KIND_MACSEC
] = "macsec",
107 DEFINE_STRING_TABLE_LOOKUP(netdev_kind
, NetDevKind
);
109 int config_parse_netdev_kind(
111 const char *filename
,
114 unsigned section_line
,
121 NetDevKind k
, *kind
= data
;
126 k
= netdev_kind_from_string(rvalue
);
128 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse netdev kind, ignoring assignment: %s", rvalue
);
132 if (*kind
!= _NETDEV_KIND_INVALID
&& *kind
!= k
) {
133 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
134 "Specified netdev kind is different from the previous value '%s', ignoring assignment: %s",
135 netdev_kind_to_string(*kind
), rvalue
);
144 static void netdev_callbacks_clear(NetDev
*netdev
) {
145 netdev_join_callback
*callback
;
150 while ((callback
= netdev
->callbacks
)) {
151 LIST_REMOVE(callbacks
, netdev
->callbacks
, callback
);
152 link_unref(callback
->link
);
157 bool netdev_is_managed(NetDev
*netdev
) {
158 if (!netdev
|| !netdev
->manager
|| !netdev
->ifname
)
161 return hashmap_get(netdev
->manager
->netdevs
, netdev
->ifname
) == netdev
;
164 static void netdev_detach_from_manager(NetDev
*netdev
) {
165 if (netdev
->ifname
&& netdev
->manager
)
166 hashmap_remove(netdev
->manager
->netdevs
, netdev
->ifname
);
169 static NetDev
*netdev_free(NetDev
*netdev
) {
172 netdev_callbacks_clear(netdev
);
174 netdev_detach_from_manager(netdev
);
176 free(netdev
->filename
);
178 free(netdev
->description
);
179 free(netdev
->ifname
);
181 condition_free_list(netdev
->conditions
);
183 /* Invoke the per-kind done() destructor, but only if the state field is initialized. We conditionalize that
184 * because we parse .netdev files twice: once to determine the kind (with a short, minimal NetDev structure
185 * allocation, with no room for per-kind fields), and once to read the kind's properties (with a full,
186 * comprehensive NetDev structure allocation with enough space for whatever the specific kind needs). Now, in
187 * the first case we shouldn't try to destruct the per-kind NetDev fields on destruction, in the second case we
188 * should. We use the state field to discern the two cases: it's _NETDEV_STATE_INVALID on the first "raw"
190 if (netdev
->state
!= _NETDEV_STATE_INVALID
&&
191 NETDEV_VTABLE(netdev
) &&
192 NETDEV_VTABLE(netdev
)->done
)
193 NETDEV_VTABLE(netdev
)->done(netdev
);
195 return mfree(netdev
);
198 DEFINE_TRIVIAL_REF_UNREF_FUNC(NetDev
, netdev
, netdev_free
);
200 void netdev_drop(NetDev
*netdev
) {
201 if (!netdev
|| netdev
->state
== NETDEV_STATE_LINGER
)
204 netdev
->state
= NETDEV_STATE_LINGER
;
206 log_netdev_debug(netdev
, "netdev removed");
208 netdev_callbacks_clear(netdev
);
210 netdev_detach_from_manager(netdev
);
212 netdev_unref(netdev
);
217 int netdev_get(Manager
*manager
, const char *name
, NetDev
**ret
) {
224 netdev
= hashmap_get(manager
->netdevs
, name
);
235 static int netdev_enter_failed(NetDev
*netdev
) {
236 netdev
->state
= NETDEV_STATE_FAILED
;
238 netdev_callbacks_clear(netdev
);
243 static int netdev_enslave_ready(NetDev
*netdev
, Link
* link
, link_netlink_message_handler_t callback
) {
244 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
248 assert(netdev
->state
== NETDEV_STATE_READY
);
249 assert(netdev
->manager
);
250 assert(netdev
->manager
->rtnl
);
251 assert(IN_SET(netdev
->kind
, NETDEV_KIND_BRIDGE
, NETDEV_KIND_BOND
, NETDEV_KIND_VRF
));
255 if (link
->flags
& IFF_UP
&& netdev
->kind
== NETDEV_KIND_BOND
) {
256 log_netdev_debug(netdev
, "Link '%s' was up when attempting to enslave it. Bringing link down.", link
->ifname
);
259 return log_netdev_error_errno(netdev
, r
, "Could not bring link down: %m");
262 r
= sd_rtnl_message_new_link(netdev
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
264 return log_netdev_error_errno(netdev
, r
, "Could not allocate RTM_SETLINK message: %m");
266 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, netdev
->ifindex
);
268 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_MASTER attribute: %m");
270 r
= netlink_call_async(netdev
->manager
->rtnl
, NULL
, req
, callback
,
271 link_netlink_destroy_callback
, link
);
273 return log_netdev_error_errno(netdev
, r
, "Could not send rtnetlink message: %m");
277 log_netdev_debug(netdev
, "Enslaving link '%s'", link
->ifname
);
282 static int netdev_enter_ready(NetDev
*netdev
) {
283 netdev_join_callback
*callback
, *callback_next
;
287 assert(netdev
->ifname
);
289 if (netdev
->state
!= NETDEV_STATE_CREATING
)
292 netdev
->state
= NETDEV_STATE_READY
;
294 log_netdev_info(netdev
, "netdev ready");
296 LIST_FOREACH_SAFE(callbacks
, callback
, callback_next
, netdev
->callbacks
) {
297 /* enslave the links that were attempted to be enslaved before the
299 r
= netdev_enslave_ready(netdev
, callback
->link
, callback
->callback
);
303 LIST_REMOVE(callbacks
, netdev
->callbacks
, callback
);
304 link_unref(callback
->link
);
308 if (NETDEV_VTABLE(netdev
)->post_create
)
309 NETDEV_VTABLE(netdev
)->post_create(netdev
, NULL
, NULL
);
314 /* callback for netdev's created without a backing Link */
315 static int netdev_create_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, NetDev
*netdev
) {
319 assert(netdev
->state
!= _NETDEV_STATE_INVALID
);
321 r
= sd_netlink_message_get_errno(m
);
323 log_netdev_info(netdev
, "netdev exists, using existing without changing its parameters");
325 log_netdev_warning_errno(netdev
, r
, "netdev could not be created: %m");
331 log_netdev_debug(netdev
, "Created");
336 static int netdev_enslave(NetDev
*netdev
, Link
*link
, link_netlink_message_handler_t callback
) {
340 assert(netdev
->manager
);
341 assert(netdev
->manager
->rtnl
);
342 assert(IN_SET(netdev
->kind
, NETDEV_KIND_BRIDGE
, NETDEV_KIND_BOND
, NETDEV_KIND_VRF
));
344 if (netdev
->state
== NETDEV_STATE_READY
) {
345 r
= netdev_enslave_ready(netdev
, link
, callback
);
348 } else if (IN_SET(netdev
->state
, NETDEV_STATE_LINGER
, NETDEV_STATE_FAILED
)) {
349 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
351 r
= rtnl_message_new_synthetic_error(netdev
->manager
->rtnl
, -ENODEV
, 0, &m
);
353 callback(netdev
->manager
->rtnl
, m
, link
);
355 /* the netdev is not yet read, save this request for when it is */
356 netdev_join_callback
*cb
;
358 cb
= new(netdev_join_callback
, 1);
362 *cb
= (netdev_join_callback
) {
363 .callback
= callback
,
364 .link
= link_ref(link
),
367 LIST_PREPEND(callbacks
, netdev
->callbacks
, cb
);
369 log_netdev_debug(netdev
, "Will enslave '%s', when ready", link
->ifname
);
375 int netdev_set_ifindex(NetDev
*netdev
, sd_netlink_message
*message
) {
378 const char *received_kind
;
379 const char *received_name
;
385 r
= sd_netlink_message_get_type(message
, &type
);
387 return log_netdev_error_errno(netdev
, r
, "Could not get rtnl message type: %m");
389 if (type
!= RTM_NEWLINK
) {
390 log_netdev_error(netdev
, "Cannot set ifindex from unexpected rtnl message type.");
394 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
396 log_netdev_error_errno(netdev
, r
, "Could not get ifindex: %m");
397 netdev_enter_failed(netdev
);
399 } else if (ifindex
<= 0) {
400 log_netdev_error(netdev
, "Got invalid ifindex: %d", ifindex
);
401 netdev_enter_failed(netdev
);
405 if (netdev
->ifindex
> 0) {
406 if (netdev
->ifindex
!= ifindex
) {
407 log_netdev_error(netdev
, "Could not set ifindex to %d, already set to %d",
408 ifindex
, netdev
->ifindex
);
409 netdev_enter_failed(netdev
);
412 /* ifindex already set to the same for this netdev */
416 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &received_name
);
418 return log_netdev_error_errno(netdev
, r
, "Could not get IFNAME: %m");
420 if (!streq(netdev
->ifname
, received_name
)) {
421 log_netdev_error(netdev
, "Received newlink with wrong IFNAME %s", received_name
);
422 netdev_enter_failed(netdev
);
426 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
428 return log_netdev_error_errno(netdev
, r
, "Could not get LINKINFO: %m");
430 r
= sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &received_kind
);
432 return log_netdev_error_errno(netdev
, r
, "Could not get KIND: %m");
434 r
= sd_netlink_message_exit_container(message
);
436 return log_netdev_error_errno(netdev
, r
, "Could not exit container: %m");
438 if (netdev
->kind
== NETDEV_KIND_TAP
)
439 /* the kernel does not distinguish between tun and tap */
442 kind
= netdev_kind_to_string(netdev
->kind
);
444 log_netdev_error(netdev
, "Could not get kind");
445 netdev_enter_failed(netdev
);
450 if (!streq(kind
, received_kind
)) {
451 log_netdev_error(netdev
,
452 "Received newlink with wrong KIND %s, "
453 "expected %s", received_kind
, kind
);
454 netdev_enter_failed(netdev
);
458 netdev
->ifindex
= ifindex
;
460 log_netdev_debug(netdev
, "netdev has index %d", netdev
->ifindex
);
462 netdev_enter_ready(netdev
);
467 #define HASH_KEY SD_ID128_MAKE(52,e1,45,bd,00,6f,29,96,21,c6,30,6d,83,71,04,48)
469 int netdev_get_mac(const char *ifname
, struct ether_addr
**ret
) {
470 _cleanup_free_
struct ether_addr
*mac
= NULL
;
479 mac
= new0(struct ether_addr
, 1);
484 sz
= sizeof(sd_id128_t
) + l
;
485 v
= newa(uint8_t, sz
);
487 /* fetch some persistent data unique to the machine */
488 r
= sd_id128_get_machine((sd_id128_t
*) v
);
492 /* combine with some data unique (on this machine) to this
494 memcpy(v
+ sizeof(sd_id128_t
), ifname
, l
);
496 /* Let's hash the host machine ID plus the container name. We
497 * use a fixed, but originally randomly created hash key here. */
498 result
= siphash24(v
, sz
, HASH_KEY
.bytes
);
500 assert_cc(ETH_ALEN
<= sizeof(result
));
501 memcpy(mac
->ether_addr_octet
, &result
, ETH_ALEN
);
503 /* see eth_random_addr in the kernel */
504 mac
->ether_addr_octet
[0] &= 0xfe; /* clear multicast bit */
505 mac
->ether_addr_octet
[0] |= 0x02; /* set local assignment bit (IEEE802) */
507 *ret
= TAKE_PTR(mac
);
512 static int netdev_create(NetDev
*netdev
, Link
*link
, link_netlink_message_handler_t callback
) {
516 assert(!link
|| callback
);
519 if (NETDEV_VTABLE(netdev
)->create
) {
522 r
= NETDEV_VTABLE(netdev
)->create(netdev
);
526 log_netdev_debug(netdev
, "Created");
528 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
530 r
= sd_rtnl_message_new_link(netdev
->manager
->rtnl
, &m
, RTM_NEWLINK
, 0);
532 return log_netdev_error_errno(netdev
, r
, "Could not allocate RTM_NEWLINK message: %m");
534 r
= sd_netlink_message_append_string(m
, IFLA_IFNAME
, netdev
->ifname
);
536 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IFNAME, attribute: %m");
539 r
= sd_netlink_message_append_ether_addr(m
, IFLA_ADDRESS
, netdev
->mac
);
541 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_ADDRESS attribute: %m");
545 r
= sd_netlink_message_append_u32(m
, IFLA_MTU
, netdev
->mtu
);
547 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_MTU attribute: %m");
551 r
= sd_netlink_message_append_u32(m
, IFLA_LINK
, link
->ifindex
);
553 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_LINK attribute: %m");
556 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
558 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_LINKINFO attribute: %m");
560 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, netdev_kind_to_string(netdev
->kind
));
562 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
564 if (NETDEV_VTABLE(netdev
)->fill_message_create
) {
565 r
= NETDEV_VTABLE(netdev
)->fill_message_create(netdev
, link
, m
);
570 r
= sd_netlink_message_close_container(m
);
572 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
574 r
= sd_netlink_message_close_container(m
);
576 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_LINKINFO attribute: %m");
579 r
= netlink_call_async(netdev
->manager
->rtnl
, NULL
, m
, callback
,
580 link_netlink_destroy_callback
, link
);
582 return log_netdev_error_errno(netdev
, r
, "Could not send rtnetlink message: %m");
586 r
= netlink_call_async(netdev
->manager
->rtnl
, NULL
, m
, netdev_create_handler
,
587 netdev_destroy_callback
, netdev
);
589 return log_netdev_error_errno(netdev
, r
, "Could not send rtnetlink message: %m");
594 netdev
->state
= NETDEV_STATE_CREATING
;
596 log_netdev_debug(netdev
, "Creating");
602 static int netdev_create_after_configured(NetDev
*netdev
, Link
*link
) {
605 assert(NETDEV_VTABLE(netdev
)->create_after_configured
);
607 return NETDEV_VTABLE(netdev
)->create_after_configured(netdev
, link
);
610 /* the callback must be called, possibly after a timeout, as otherwise the Link will hang */
611 int netdev_join(NetDev
*netdev
, Link
*link
, link_netlink_message_handler_t callback
) {
615 assert(netdev
->manager
);
616 assert(netdev
->manager
->rtnl
);
618 switch (netdev_get_create_type(netdev
)) {
619 case NETDEV_CREATE_MASTER
:
620 r
= netdev_enslave(netdev
, link
, callback
);
625 case NETDEV_CREATE_STACKED
:
626 r
= netdev_create(netdev
, link
, callback
);
631 case NETDEV_CREATE_AFTER_CONFIGURED
:
632 r
= netdev_create_after_configured(netdev
, link
);
637 assert_not_reached("Can not join independent netdev");
643 int netdev_load_one(Manager
*manager
, const char *filename
) {
644 _cleanup_(netdev_unrefp
) NetDev
*netdev_raw
= NULL
, *netdev
= NULL
;
645 _cleanup_fclose_
FILE *file
= NULL
;
646 const char *dropin_dirname
;
647 bool independent
= false;
653 file
= fopen(filename
, "re");
661 if (null_or_empty_fd(fileno(file
))) {
662 log_debug("Skipping empty file: %s", filename
);
666 netdev_raw
= new(NetDev
, 1);
670 *netdev_raw
= (NetDev
) {
672 .kind
= _NETDEV_KIND_INVALID
,
673 .state
= _NETDEV_STATE_INVALID
, /* an invalid state means done() of the implementation won't be called on destruction */
676 dropin_dirname
= strjoina(basename(filename
), ".d");
677 r
= config_parse_many(filename
, NETWORK_DIRS
, dropin_dirname
,
679 config_item_perf_lookup
, network_netdev_gperf_lookup
,
680 CONFIG_PARSE_WARN
|CONFIG_PARSE_RELAXED
, netdev_raw
);
684 /* skip out early if configuration does not match the environment */
685 if (!condition_test_list(netdev_raw
->conditions
, NULL
, NULL
, NULL
)) {
686 log_debug("%s: Conditions in the file do not match the system environment, skipping.", filename
);
690 if (netdev_raw
->kind
== _NETDEV_KIND_INVALID
) {
691 log_warning("NetDev has no Kind= configured in %s. Ignoring", filename
);
695 if (!netdev_raw
->ifname
) {
696 log_warning("NetDev without Name= configured in %s. Ignoring", filename
);
700 r
= fseek(file
, 0, SEEK_SET
);
704 netdev
= malloc0(NETDEV_VTABLE(netdev_raw
)->object_size
);
709 netdev
->manager
= manager
;
710 netdev
->kind
= netdev_raw
->kind
;
711 netdev
->state
= NETDEV_STATE_LOADING
; /* we initialize the state here for the first time,
712 so that done() will be called on destruction */
714 if (NETDEV_VTABLE(netdev
)->init
)
715 NETDEV_VTABLE(netdev
)->init(netdev
);
717 r
= config_parse_many(filename
, NETWORK_DIRS
, dropin_dirname
,
718 NETDEV_VTABLE(netdev
)->sections
,
719 config_item_perf_lookup
, network_netdev_gperf_lookup
,
720 CONFIG_PARSE_WARN
, netdev
);
724 /* verify configuration */
725 if (NETDEV_VTABLE(netdev
)->config_verify
) {
726 r
= NETDEV_VTABLE(netdev
)->config_verify(netdev
, filename
);
731 netdev
->filename
= strdup(filename
);
732 if (!netdev
->filename
)
735 if (!netdev
->mac
&& netdev
->kind
!= NETDEV_KIND_VLAN
) {
736 r
= netdev_get_mac(netdev
->ifname
, &netdev
->mac
);
738 return log_netdev_error_errno(netdev
, r
,
739 "Failed to generate predictable MAC address for %s: %m",
743 r
= hashmap_ensure_allocated(&netdev
->manager
->netdevs
, &string_hash_ops
);
747 r
= hashmap_put(netdev
->manager
->netdevs
, netdev
->ifname
, netdev
);
749 NetDev
*n
= hashmap_get(netdev
->manager
->netdevs
, netdev
->ifname
);
752 log_netdev_warning_errno(netdev
, r
,
753 "The setting Name=%s in %s conflicts with the one in %s, ignoring",
754 netdev
->ifname
, netdev
->filename
, n
->filename
);
756 /* Clear ifname before netdev_free() is called. Otherwise, the NetDev object 'n' is
757 * removed from the hashmap 'manager->netdevs'. */
758 netdev
->ifname
= mfree(netdev
->ifname
);
764 LIST_HEAD_INIT(netdev
->callbacks
);
766 log_netdev_debug(netdev
, "loaded %s", netdev_kind_to_string(netdev
->kind
));
768 if (IN_SET(netdev_get_create_type(netdev
), NETDEV_CREATE_MASTER
, NETDEV_CREATE_INDEPENDENT
)) {
769 r
= netdev_create(netdev
, NULL
, NULL
);
774 switch (netdev
->kind
) {
775 case NETDEV_KIND_IPIP
:
776 independent
= IPIP(netdev
)->independent
;
778 case NETDEV_KIND_GRE
:
779 independent
= GRE(netdev
)->independent
;
781 case NETDEV_KIND_GRETAP
:
782 independent
= GRETAP(netdev
)->independent
;
784 case NETDEV_KIND_IP6GRE
:
785 independent
= IP6GRE(netdev
)->independent
;
787 case NETDEV_KIND_IP6GRETAP
:
788 independent
= IP6GRETAP(netdev
)->independent
;
790 case NETDEV_KIND_SIT
:
791 independent
= SIT(netdev
)->independent
;
793 case NETDEV_KIND_VTI
:
794 independent
= VTI(netdev
)->independent
;
796 case NETDEV_KIND_VTI6
:
797 independent
= VTI6(netdev
)->independent
;
799 case NETDEV_KIND_IP6TNL
:
800 independent
= IP6TNL(netdev
)->independent
;
802 case NETDEV_KIND_ERSPAN
:
803 independent
= ERSPAN(netdev
)->independent
;
810 r
= netdev_create(netdev
, NULL
, NULL
);
820 int netdev_load(Manager
*manager
) {
821 _cleanup_strv_free_
char **files
= NULL
;
827 hashmap_clear_with_destructor(manager
->netdevs
, netdev_unref
);
829 r
= conf_files_list_strv(&files
, ".netdev", NULL
, 0, NETWORK_DIRS
);
831 return log_error_errno(r
, "Failed to enumerate netdev files: %m");
833 STRV_FOREACH(f
, files
) {
834 r
= netdev_load_one(manager
, *f
);