1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include "sd-netlink.h"
6 #include "conf-parser.h"
7 #include "ether-addr-util.h"
10 #include "networkd-link.h"
11 #include "time-util.h"
13 /* Special hardware address value to suppress generating persistent hardware address for the netdev. */
14 #define HW_ADDR_NONE ((struct hw_addr_data) { .length = 1, })
16 #define NETDEV_COMMON_SECTIONS "Match\0NetDev\0"
17 /* This is the list of known sections. We need to ignore them in the initial parsing phase. */
18 #define NETDEV_OTHER_SECTIONS \
31 "-MACsecReceiveAssociation\0" \
32 "-MACsecReceiveChannel\0" \
33 "-MACsecTransmitAssociation\0" \
49 typedef enum NetDevKind
{
62 NETDEV_KIND_IP6GRETAP
,
72 NETDEV_KIND_NETDEVSIM
,
85 NETDEV_KIND_WIREGUARD
,
89 _NETDEV_KIND_TUNNEL
, /* Used by config_parse_stacked_netdev() */
90 _NETDEV_KIND_INVALID
= -EINVAL
,
93 typedef enum NetDevState
{
96 NETDEV_STATE_CREATING
,
100 _NETDEV_STATE_INVALID
= -EINVAL
,
103 typedef enum NetDevCreateType
{
104 NETDEV_CREATE_INDEPENDENT
,
105 NETDEV_CREATE_STACKED
,
107 _NETDEV_CREATE_INVALID
= -EINVAL
,
110 typedef struct Manager Manager
;
111 typedef struct Condition Condition
;
113 typedef struct NetDev
{
120 LIST_HEAD(Condition
, conditions
);
126 struct hw_addr_data hw_addr
;
131 typedef struct NetDevVTable
{
132 /* How much memory does an object of this unit type need */
135 /* Config file sections this netdev kind understands, separated
137 const char *sections
;
139 /* This should reset all type-specific variables. This should
140 * not allocate memory, and is called with zero-initialized
141 * data. It should hence only initialize variables that need
143 void (*init
)(NetDev
*n
);
145 /* This is called when the interface is removed. */
146 void (*drop
)(NetDev
*n
);
148 /* This should free all kind-specific variables. It should be
150 void (*done
)(NetDev
*n
);
152 /* fill in message to create netdev */
153 int (*fill_message_create
)(NetDev
*netdev
, Link
*link
, sd_netlink_message
*message
);
155 /* specifies if netdev is independent, or a master device or a stacked device */
156 NetDevCreateType create_type
;
158 /* This is used for stacked netdev. Return true when the underlying link is ready. */
159 int (*is_ready_to_create
)(NetDev
*netdev
, Link
*link
);
161 /* create netdev, if not done via rtnl */
162 int (*create
)(NetDev
*netdev
);
164 /* perform additional configuration after netdev has been createad */
165 int (*post_create
)(NetDev
*netdev
, Link
*link
);
167 /* verify that compulsory configuration options were specified */
168 int (*config_verify
)(NetDev
*netdev
, const char *filename
);
170 /* expected iftype, e.g. ARPHRD_ETHER. */
173 /* Generate MAC address when MACAddress= is not specified. */
176 /* When assigning ifindex to the netdev, skip to check if the netdev kind matches. */
177 bool skip_netdev_kind_check
;
180 extern const NetDevVTable
* const netdev_vtable
[_NETDEV_KIND_MAX
];
182 #define NETDEV_VTABLE(n) ((n)->kind != _NETDEV_KIND_INVALID ? netdev_vtable[(n)->kind] : NULL)
184 /* For casting a netdev into the various netdev kinds */
185 #define DEFINE_NETDEV_CAST(UPPERCASE, MixedCase) \
186 static inline MixedCase* UPPERCASE(NetDev *n) { \
188 assert(n->kind == NETDEV_KIND_##UPPERCASE); \
189 assert(n->state < _NETDEV_STATE_MAX); \
191 return (MixedCase*) n; \
194 /* For casting the various netdev kinds into a netdev */
195 #define NETDEV(n) (&(n)->meta)
197 int netdev_load(Manager
*manager
, bool reload
);
198 int netdev_load_one(Manager
*manager
, const char *filename
);
199 void netdev_drop(NetDev
*netdev
);
200 void netdev_enter_failed(NetDev
*netdev
);
202 NetDev
*netdev_unref(NetDev
*netdev
);
203 NetDev
*netdev_ref(NetDev
*netdev
);
204 DEFINE_TRIVIAL_DESTRUCTOR(netdev_destroy_callback
, NetDev
, netdev_unref
);
205 DEFINE_TRIVIAL_CLEANUP_FUNC(NetDev
*, netdev_unref
);
207 bool netdev_is_managed(NetDev
*netdev
);
208 int netdev_get(Manager
*manager
, const char *name
, NetDev
**ret
);
209 int netdev_set_ifindex(NetDev
*netdev
, sd_netlink_message
*newlink
);
210 int netdev_generate_hw_addr(NetDev
*netdev
, Link
*link
, const char *name
,
211 const struct hw_addr_data
*hw_addr
, struct hw_addr_data
*ret
);
213 int link_request_stacked_netdev(Link
*link
, NetDev
*netdev
);
215 const char *netdev_kind_to_string(NetDevKind d
) _const_
;
216 NetDevKind
netdev_kind_from_string(const char *d
) _pure_
;
218 static inline NetDevCreateType
netdev_get_create_type(NetDev
*netdev
) {
220 assert(NETDEV_VTABLE(netdev
));
222 return NETDEV_VTABLE(netdev
)->create_type
;
225 CONFIG_PARSER_PROTOTYPE(config_parse_netdev_kind
);
226 CONFIG_PARSER_PROTOTYPE(config_parse_netdev_hw_addr
);
229 const struct ConfigPerfItem
* network_netdev_gperf_lookup(const char *key
, GPERF_LEN_TYPE length
);
231 /* Macros which append INTERFACE= to the message */
233 #define log_netdev_full_errno_zerook(netdev, level, error, ...) \
235 const NetDev *_n = (netdev); \
236 log_interface_full_errno_zerook(_n ? _n->ifname : NULL, level, error, __VA_ARGS__); \
239 #define log_netdev_full_errno(netdev, level, error, ...) \
241 int _error = (error); \
242 ASSERT_NON_ZERO(_error); \
243 log_netdev_full_errno_zerook(netdev, level, _error, __VA_ARGS__); \
246 #define log_netdev_full(netdev, level, ...) (void) log_netdev_full_errno_zerook(netdev, level, 0, __VA_ARGS__)
248 #define log_netdev_debug(netdev, ...) log_netdev_full(netdev, LOG_DEBUG, __VA_ARGS__)
249 #define log_netdev_info(netdev, ...) log_netdev_full(netdev, LOG_INFO, __VA_ARGS__)
250 #define log_netdev_notice(netdev, ...) log_netdev_full(netdev, LOG_NOTICE, __VA_ARGS__)
251 #define log_netdev_warning(netdev, ...) log_netdev_full(netdev, LOG_WARNING, __VA_ARGS__)
252 #define log_netdev_error(netdev, ...) log_netdev_full(netdev, LOG_ERR, __VA_ARGS__)
254 #define log_netdev_debug_errno(netdev, error, ...) log_netdev_full_errno(netdev, LOG_DEBUG, error, __VA_ARGS__)
255 #define log_netdev_info_errno(netdev, error, ...) log_netdev_full_errno(netdev, LOG_INFO, error, __VA_ARGS__)
256 #define log_netdev_notice_errno(netdev, error, ...) log_netdev_full_errno(netdev, LOG_NOTICE, error, __VA_ARGS__)
257 #define log_netdev_warning_errno(netdev, error, ...) log_netdev_full_errno(netdev, LOG_WARNING, error, __VA_ARGS__)
258 #define log_netdev_error_errno(netdev, error, ...) log_netdev_full_errno(netdev, LOG_ERR, error, __VA_ARGS__)
260 #define LOG_NETDEV_MESSAGE(netdev, fmt, ...) "MESSAGE=%s: " fmt, (netdev)->ifname, ##__VA_ARGS__
261 #define LOG_NETDEV_INTERFACE(netdev) "INTERFACE=%s", (netdev)->ifname