| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "ether-addr-util.h" |
| 5 |
#include "list.h" |
| 6 |
#include "log-link.h" |
| 7 |
#include "netdev-util.h" |
| 8 |
#include "networkd-forward.h" |
| 9 |
|
| 10 |
/* Special hardware address value to suppress generating persistent hardware address for the netdev. */ |
| 11 |
#define HW_ADDR_NONE ((struct hw_addr_data) { .length = 1, }) |
| 12 |
|
| 13 |
#define NETDEV_COMMON_SECTIONS "Match\0NetDev\0" |
| 14 |
/* This is the list of known sections. We need to ignore them in the initial parsing phase. */ |
| 15 |
#define NETDEV_OTHER_SECTIONS \ |
| 16 |
"-BareUDP\0" \ |
| 17 |
"-BatmanAdvanced\0" \ |
| 18 |
"-Bond\0" \ |
| 19 |
"-Bridge\0" \ |
| 20 |
"-FooOverUDP\0" \ |
| 21 |
"-GENEVE\0" \ |
| 22 |
"-HSR\0" \ |
| 23 |
"-IPoIB\0" \ |
| 24 |
"-IPVLAN\0" \ |
| 25 |
"-IPVTAP\0" \ |
| 26 |
"-L2TP\0" \ |
| 27 |
"-L2TPSession\0" \ |
| 28 |
"-MACsec\0" \ |
| 29 |
"-MACsecReceiveAssociation\0" \ |
| 30 |
"-MACsecReceiveChannel\0" \ |
| 31 |
"-MACsecTransmitAssociation\0" \ |
| 32 |
"-MACVLAN\0" \ |
| 33 |
"-MACVTAP\0" \ |
| 34 |
"-Peer\0" \ |
| 35 |
"-Tap\0" \ |
| 36 |
"-Tun\0" \ |
| 37 |
"-Tunnel\0" \ |
| 38 |
"-VLAN\0" \ |
| 39 |
"-VRF\0" \ |
| 40 |
"-VXCAN\0" \ |
| 41 |
"-VXLAN\0" \ |
| 42 |
"-WLAN\0" \ |
| 43 |
"-WireGuard\0" \ |
| 44 |
"-WireGuardPeer\0" \ |
| 45 |
"-Xfrm\0" |
| 46 |
|
| 47 |
typedef enum NetDevKind { |
| 48 |
NETDEV_KIND_BAREUDP, |
| 49 |
NETDEV_KIND_BATADV, |
| 50 |
NETDEV_KIND_BOND, |
| 51 |
NETDEV_KIND_BRIDGE, |
| 52 |
NETDEV_KIND_DUMMY, |
| 53 |
NETDEV_KIND_ERSPAN, |
| 54 |
NETDEV_KIND_FOU, |
| 55 |
NETDEV_KIND_GENEVE, |
| 56 |
NETDEV_KIND_GRE, |
| 57 |
NETDEV_KIND_GRETAP, |
| 58 |
NETDEV_KIND_HSR, |
| 59 |
NETDEV_KIND_IFB, |
| 60 |
NETDEV_KIND_IP6GRE, |
| 61 |
NETDEV_KIND_IP6GRETAP, |
| 62 |
NETDEV_KIND_IP6TNL, |
| 63 |
NETDEV_KIND_IPIP, |
| 64 |
NETDEV_KIND_IPOIB, |
| 65 |
NETDEV_KIND_IPVLAN, |
| 66 |
NETDEV_KIND_IPVTAP, |
| 67 |
NETDEV_KIND_L2TP, |
| 68 |
NETDEV_KIND_MACSEC, |
| 69 |
NETDEV_KIND_MACVLAN, |
| 70 |
NETDEV_KIND_MACVTAP, |
| 71 |
NETDEV_KIND_NLMON, |
| 72 |
NETDEV_KIND_SIT, |
| 73 |
NETDEV_KIND_TAP, |
| 74 |
NETDEV_KIND_TUN, |
| 75 |
NETDEV_KIND_VCAN, |
| 76 |
NETDEV_KIND_VETH, |
| 77 |
NETDEV_KIND_VLAN, |
| 78 |
NETDEV_KIND_VRF, |
| 79 |
NETDEV_KIND_VTI, |
| 80 |
NETDEV_KIND_VTI6, |
| 81 |
NETDEV_KIND_VXCAN, |
| 82 |
NETDEV_KIND_VXLAN, |
| 83 |
NETDEV_KIND_WIREGUARD, |
| 84 |
NETDEV_KIND_WLAN, |
| 85 |
NETDEV_KIND_XFRM, |
| 86 |
_NETDEV_KIND_MAX, |
| 87 |
_NETDEV_KIND_TUNNEL, /* Used by config_parse_stacked_netdev() */ |
| 88 |
_NETDEV_KIND_INVALID = -EINVAL, |
| 89 |
} NetDevKind; |
| 90 |
|
| 91 |
typedef enum NetDevState { |
| 92 |
NETDEV_STATE_LOADING, |
| 93 |
NETDEV_STATE_FAILED, |
| 94 |
NETDEV_STATE_CREATING, |
| 95 |
NETDEV_STATE_READY, |
| 96 |
NETDEV_STATE_LINGER, |
| 97 |
_NETDEV_STATE_MAX, |
| 98 |
_NETDEV_STATE_INVALID = -EINVAL, |
| 99 |
} NetDevState; |
| 100 |
|
| 101 |
typedef enum NetDevCreateType { |
| 102 |
NETDEV_CREATE_INDEPENDENT, |
| 103 |
NETDEV_CREATE_STACKED, |
| 104 |
_NETDEV_CREATE_MAX, |
| 105 |
_NETDEV_CREATE_INVALID = -EINVAL, |
| 106 |
} NetDevCreateType; |
| 107 |
|
| 108 |
typedef struct NetDev { |
| 109 |
Manager *manager; |
| 110 |
|
| 111 |
unsigned n_ref; |
| 112 |
|
| 113 |
char *filename; |
| 114 |
char **dropins; |
| 115 |
Hashmap *stats_by_path; |
| 116 |
|
| 117 |
LIST_HEAD(Condition, conditions); |
| 118 |
|
| 119 |
NetDevState state; |
| 120 |
NetDevKind kind; |
| 121 |
char *description; |
| 122 |
char *ifname; |
| 123 |
struct hw_addr_data hw_addr; |
| 124 |
uint32_t mtu; |
| 125 |
int ifindex; |
| 126 |
} NetDev; |
| 127 |
|
| 128 |
typedef struct NetDevVTable { |
| 129 |
/* How much memory does an object of this unit type need */ |
| 130 |
size_t object_size; |
| 131 |
|
| 132 |
/* Config file sections this netdev kind understands, separated |
| 133 |
* by NUL chars */ |
| 134 |
const char *sections; |
| 135 |
|
| 136 |
/* This should reset all type-specific variables. This should |
| 137 |
* not allocate memory, and is called with zero-initialized |
| 138 |
* data. It should hence only initialize variables that need |
| 139 |
* to be set != 0. */ |
| 140 |
void (*init)(NetDev *n); |
| 141 |
|
| 142 |
/* This is called when the interface is removed. */ |
| 143 |
void (*drop)(NetDev *n); |
| 144 |
|
| 145 |
/* This should free all kind-specific variables. It should be |
| 146 |
* idempotent. */ |
| 147 |
void (*done)(NetDev *n); |
| 148 |
|
| 149 |
/* fill in message to create netdev */ |
| 150 |
int (*fill_message_create)(NetDev *netdev, Link *link, sd_netlink_message *message); |
| 151 |
|
| 152 |
/* specifies if netdev is independent, or a master device or a stacked device */ |
| 153 |
NetDevCreateType create_type; |
| 154 |
|
| 155 |
/* This is used for stacked netdev. Return true when the underlying link is ready. */ |
| 156 |
int (*is_ready_to_create)(NetDev *netdev, Link *link); |
| 157 |
|
| 158 |
/* create netdev, if not done via rtnl */ |
| 159 |
int (*create)(NetDev *netdev); |
| 160 |
|
| 161 |
/* perform additional configuration after netdev has been created */ |
| 162 |
int (*post_create)(NetDev *netdev, Link *link); |
| 163 |
|
| 164 |
/* verify that compulsory configuration options were specified */ |
| 165 |
int (*config_verify)(NetDev *netdev, const char *filename); |
| 166 |
|
| 167 |
/* attach/detach additional interfaces, e.g. veth peer or L2TP sessions. */ |
| 168 |
int (*attach)(NetDev *netdev); |
| 169 |
void (*detach)(NetDev *netdev); |
| 170 |
|
| 171 |
/* set ifindex of the created interface. */ |
| 172 |
int (*set_ifindex)(NetDev *netdev, const char *name, int ifindex); |
| 173 |
|
| 174 |
/* get ifindex of the netdev. */ |
| 175 |
int (*get_ifindex)(NetDev *netdev, const char *name); |
| 176 |
|
| 177 |
/* provides if MAC address can be set. If this is not set, assumed to be yes. */ |
| 178 |
bool (*can_set_mac)(NetDev *netdev, const struct hw_addr_data *hw_addr); |
| 179 |
|
| 180 |
/* provides if MTU can be set. If this is not set, assumed to be yes. */ |
| 181 |
bool (*can_set_mtu)(NetDev *netdev, uint32_t mtu); |
| 182 |
|
| 183 |
/* provides if the netdev needs to be reconfigured when a specified type of address on the underlying |
| 184 |
* interface is updated. */ |
| 185 |
bool (*needs_reconfigure)(NetDev *netdev, NetDevLocalAddressType type); |
| 186 |
|
| 187 |
/* expected iftype, e.g. ARPHRD_ETHER. */ |
| 188 |
uint16_t iftype; |
| 189 |
|
| 190 |
/* Generate MAC address when MACAddress= is not specified. */ |
| 191 |
bool generate_mac; |
| 192 |
|
| 193 |
/* When assigning ifindex to the netdev, skip to check if the netdev kind matches. */ |
| 194 |
bool skip_netdev_kind_check; |
| 195 |
|
| 196 |
/* Provides if the netdev can be updated, that is, whether RTM_NEWLINK with existing ifindex is supported or not. |
| 197 |
* If this is true, the netdev does not support updating. */ |
| 198 |
bool keep_existing; |
| 199 |
} NetDevVTable; |
| 200 |
|
| 201 |
extern const NetDevVTable * const netdev_vtable[_NETDEV_KIND_MAX]; |
| 202 |
|
| 203 |
#define NETDEV_VTABLE(n) ((n)->kind != _NETDEV_KIND_INVALID ? netdev_vtable[(n)->kind] : NULL) |
| 204 |
|
| 205 |
/* For casting a netdev into the various netdev kinds */ |
| 206 |
#define DEFINE_NETDEV_CAST(UPPERCASE, MixedCase) \ |
| 207 |
static inline MixedCase* UPPERCASE(NetDev *n) { \ |
| 208 |
assert(n); \ |
| 209 |
assert(n->kind == NETDEV_KIND_##UPPERCASE); \ |
| 210 |
assert(n->state < _NETDEV_STATE_MAX); \ |
| 211 |
\ |
| 212 |
return (MixedCase*) n; \ |
| 213 |
} |
| 214 |
|
| 215 |
/* For casting the various netdev kinds into a netdev */ |
| 216 |
#define NETDEV(n) (&(n)->meta) |
| 217 |
|
| 218 |
int netdev_attach_name(NetDev *netdev, const char *name); |
| 219 |
NetDev* netdev_detach_name(NetDev *netdev, const char *name); |
| 220 |
void netdev_detach(NetDev *netdev); |
| 221 |
int netdev_set_ifindex_internal(NetDev *netdev, int ifindex); |
| 222 |
|
| 223 |
int netdev_load(Manager *manager); |
| 224 |
int netdev_reload(Manager *manager); |
| 225 |
int netdev_load_one(Manager *manager, const char *filename, NetDev **ret); |
| 226 |
void netdev_drop(NetDev *netdev); |
| 227 |
void netdev_enter_failed(NetDev *netdev); |
| 228 |
int netdev_enter_ready(NetDev *netdev); |
| 229 |
|
| 230 |
DECLARE_TRIVIAL_REF_UNREF_FUNC(NetDev, netdev); |
| 231 |
DEFINE_TRIVIAL_DESTRUCTOR(netdev_destroy_callback, NetDev, netdev_unref); |
| 232 |
DEFINE_TRIVIAL_CLEANUP_FUNC(NetDev*, netdev_unref); |
| 233 |
|
| 234 |
bool netdev_is_managed(NetDev *netdev); |
| 235 |
int netdev_get(Manager *manager, const char *name, NetDev **ret); |
| 236 |
void link_assign_netdev(Link *link); |
| 237 |
int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *message); |
| 238 |
int netdev_generate_hw_addr(NetDev *netdev, Link *parent, const char *name, |
| 239 |
const struct hw_addr_data *hw_addr, struct hw_addr_data *ret); |
| 240 |
|
| 241 |
bool netdev_needs_reconfigure(NetDev *netdev, NetDevLocalAddressType type); |
| 242 |
int link_request_stacked_netdev(Link *link, NetDev *netdev); |
| 243 |
|
| 244 |
DECLARE_STRING_TABLE_LOOKUP(netdev_kind, NetDevKind); |
| 245 |
|
| 246 |
static inline NetDevCreateType netdev_get_create_type(NetDev *netdev) { |
| 247 |
assert(netdev); |
| 248 |
assert(NETDEV_VTABLE(netdev)); |
| 249 |
|
| 250 |
return NETDEV_VTABLE(netdev)->create_type; |
| 251 |
} |
| 252 |
|
| 253 |
CONFIG_PARSER_PROTOTYPE(config_parse_netdev_kind); |
| 254 |
CONFIG_PARSER_PROTOTYPE(config_parse_netdev_hw_addr); |
| 255 |
|
| 256 |
/* gperf */ |
| 257 |
const struct ConfigPerfItem* network_netdev_gperf_lookup(const char *str, GPERF_LEN_TYPE length); |
| 258 |
|
| 259 |
/* Macros which append INTERFACE= to the message */ |
| 260 |
|
| 261 |
#define log_netdev_syntax(netdev, level, message_id, fmt, ...) \ |
| 262 |
({ \ |
| 263 |
const NetDev *_n = (netdev); \ |
| 264 |
const char *_ifname = _n ? _n->ifname : NULL; \ |
| 265 |
log_struct(level, \ |
| 266 |
LOG_MESSAGE(fmt, __VA_ARGS__), \ |
| 267 |
LOG_MESSAGE_ID(message_id), \ |
| 268 |
LOG_ITEM("INTERFACE=%s", strempty(_ifname))); \ |
| 269 |
}) |
| 270 |
|
| 271 |
#define log_netdev_full_errno_zerook(netdev, level, error, ...) \ |
| 272 |
({ \ |
| 273 |
const NetDev *_n = (netdev); \ |
| 274 |
log_interface_full_errno_zerook(_n ? _n->ifname : NULL, level, error, __VA_ARGS__); \ |
| 275 |
}) |
| 276 |
|
| 277 |
#define log_netdev_full_errno(netdev, level, error, ...) \ |
| 278 |
({ \ |
| 279 |
int _error = (error); \ |
| 280 |
ASSERT_NON_ZERO(_error); \ |
| 281 |
log_netdev_full_errno_zerook(netdev, level, _error, __VA_ARGS__); \ |
| 282 |
}) |
| 283 |
|
| 284 |
#define log_netdev_full(netdev, level, ...) (void) log_netdev_full_errno_zerook(netdev, level, 0, __VA_ARGS__) |
| 285 |
|
| 286 |
#define log_netdev_debug(netdev, ...) log_netdev_full(netdev, LOG_DEBUG, __VA_ARGS__) |
| 287 |
#define log_netdev_info(netdev, ...) log_netdev_full(netdev, LOG_INFO, __VA_ARGS__) |
| 288 |
#define log_netdev_notice(netdev, ...) log_netdev_full(netdev, LOG_NOTICE, __VA_ARGS__) |
| 289 |
#define log_netdev_warning(netdev, ...) log_netdev_full(netdev, LOG_WARNING, __VA_ARGS__) |
| 290 |
#define log_netdev_error(netdev, ...) log_netdev_full(netdev, LOG_ERR, __VA_ARGS__) |
| 291 |
|
| 292 |
#define log_netdev_debug_errno(netdev, error, ...) log_netdev_full_errno(netdev, LOG_DEBUG, error, __VA_ARGS__) |
| 293 |
#define log_netdev_info_errno(netdev, error, ...) log_netdev_full_errno(netdev, LOG_INFO, error, __VA_ARGS__) |
| 294 |
#define log_netdev_notice_errno(netdev, error, ...) log_netdev_full_errno(netdev, LOG_NOTICE, error, __VA_ARGS__) |
| 295 |
#define log_netdev_warning_errno(netdev, error, ...) log_netdev_full_errno(netdev, LOG_WARNING, error, __VA_ARGS__) |
| 296 |
#define log_netdev_error_errno(netdev, error, ...) log_netdev_full_errno(netdev, LOG_ERR, error, __VA_ARGS__) |
| 297 |
|
| 298 |
#define LOG_NETDEV_MESSAGE(netdev, fmt, ...) "MESSAGE=%s: " fmt, (netdev)->ifname, ##__VA_ARGS__ |
| 299 |
#define LOG_NETDEV_INTERFACE(netdev) "INTERFACE=%s", (netdev)->ifname |