1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include "sd-netlink.h"
5 
6 #include "conf-parser.h"
7 #include "ether-addr-util.h"
8 #include "list.h"
9 #include "log-link.h"
10 #include "networkd-link.h"
11 #include "time-util.h"
12 
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, })
15 
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                     \
19         "-BareUDP\0"                              \
20         "-BatmanAdvanced\0"                       \
21         "-Bond\0"                                 \
22         "-Bridge\0"                               \
23         "-FooOverUDP\0"                           \
24         "-GENEVE\0"                               \
25         "-IPoIB\0"                                \
26         "-IPVLAN\0"                               \
27         "-IPVTAP\0"                               \
28         "-L2TP\0"                                 \
29         "-L2TPSession\0"                          \
30         "-MACsec\0"                               \
31         "-MACsecReceiveAssociation\0"             \
32         "-MACsecReceiveChannel\0"                 \
33         "-MACsecTransmitAssociation\0"            \
34         "-MACVLAN\0"                              \
35         "-MACVTAP\0"                              \
36         "-Peer\0"                                 \
37         "-Tap\0"                                  \
38         "-Tun\0"                                  \
39         "-Tunnel\0"                               \
40         "-VLAN\0"                                 \
41         "-VRF\0"                                  \
42         "-VXCAN\0"                                \
43         "-VXLAN\0"                                \
44         "-WLAN\0"                                 \
45         "-WireGuard\0"                            \
46         "-WireGuardPeer\0"                        \
47         "-Xfrm\0"
48 
49 typedef enum NetDevKind {
50         NETDEV_KIND_BAREUDP,
51         NETDEV_KIND_BATADV,
52         NETDEV_KIND_BOND,
53         NETDEV_KIND_BRIDGE,
54         NETDEV_KIND_DUMMY,
55         NETDEV_KIND_ERSPAN,
56         NETDEV_KIND_FOU,
57         NETDEV_KIND_GENEVE,
58         NETDEV_KIND_GRE,
59         NETDEV_KIND_GRETAP,
60         NETDEV_KIND_IFB,
61         NETDEV_KIND_IP6GRE,
62         NETDEV_KIND_IP6GRETAP,
63         NETDEV_KIND_IP6TNL,
64         NETDEV_KIND_IPIP,
65         NETDEV_KIND_IPOIB,
66         NETDEV_KIND_IPVLAN,
67         NETDEV_KIND_IPVTAP,
68         NETDEV_KIND_L2TP,
69         NETDEV_KIND_MACSEC,
70         NETDEV_KIND_MACVLAN,
71         NETDEV_KIND_MACVTAP,
72         NETDEV_KIND_NETDEVSIM,
73         NETDEV_KIND_NLMON,
74         NETDEV_KIND_SIT,
75         NETDEV_KIND_TAP,
76         NETDEV_KIND_TUN,
77         NETDEV_KIND_VCAN,
78         NETDEV_KIND_VETH,
79         NETDEV_KIND_VLAN,
80         NETDEV_KIND_VRF,
81         NETDEV_KIND_VTI,
82         NETDEV_KIND_VTI6,
83         NETDEV_KIND_VXCAN,
84         NETDEV_KIND_VXLAN,
85         NETDEV_KIND_WIREGUARD,
86         NETDEV_KIND_WLAN,
87         NETDEV_KIND_XFRM,
88         _NETDEV_KIND_MAX,
89         _NETDEV_KIND_TUNNEL, /* Used by config_parse_stacked_netdev() */
90         _NETDEV_KIND_INVALID = -EINVAL,
91 } NetDevKind;
92 
93 typedef enum NetDevState {
94         NETDEV_STATE_LOADING,
95         NETDEV_STATE_FAILED,
96         NETDEV_STATE_CREATING,
97         NETDEV_STATE_READY,
98         NETDEV_STATE_LINGER,
99         _NETDEV_STATE_MAX,
100         _NETDEV_STATE_INVALID = -EINVAL,
101 } NetDevState;
102 
103 typedef enum NetDevCreateType {
104         NETDEV_CREATE_INDEPENDENT,
105         NETDEV_CREATE_STACKED,
106         _NETDEV_CREATE_MAX,
107         _NETDEV_CREATE_INVALID = -EINVAL,
108 } NetDevCreateType;
109 
110 typedef struct Manager Manager;
111 typedef struct Condition Condition;
112 
113 typedef struct NetDev {
114         Manager *manager;
115 
116         unsigned n_ref;
117 
118         char *filename;
119 
120         LIST_HEAD(Condition, conditions);
121 
122         NetDevState state;
123         NetDevKind kind;
124         char *description;
125         char *ifname;
126         struct hw_addr_data hw_addr;
127         uint32_t mtu;
128         int ifindex;
129 } NetDev;
130 
131 typedef struct NetDevVTable {
132         /* How much memory does an object of this unit type need */
133         size_t object_size;
134 
135         /* Config file sections this netdev kind understands, separated
136          * by NUL chars */
137         const char *sections;
138 
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
142          * to be set != 0. */
143         void (*init)(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 createad */
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         /* expected iftype, e.g. ARPHRD_ETHER. */
168         uint16_t iftype;
169 
170         /* Generate MAC address when MACAddress= is not specified. */
171         bool generate_mac;
172 
173         /* When assigning ifindex to the netdev, skip to check if the netdev kind matches. */
174         bool skip_netdev_kind_check;
175 } NetDevVTable;
176 
177 extern const NetDevVTable * const netdev_vtable[_NETDEV_KIND_MAX];
178 
179 #define NETDEV_VTABLE(n) ((n)->kind != _NETDEV_KIND_INVALID ? netdev_vtable[(n)->kind] : NULL)
180 
181 /* For casting a netdev into the various netdev kinds */
182 #define DEFINE_NETDEV_CAST(UPPERCASE, MixedCase)                            \
183         static inline MixedCase* UPPERCASE(NetDev *n) {                     \
184                 if (_unlikely_(!n ||                                        \
185                                n->kind != NETDEV_KIND_##UPPERCASE) ||       \
186                                n->state == _NETDEV_STATE_INVALID)           \
187                         return NULL;                                        \
188                                                                             \
189                 return (MixedCase*) n;                                      \
190         }
191 
192 /* For casting the various netdev kinds into a netdev */
193 #define NETDEV(n) (&(n)->meta)
194 
195 int netdev_load(Manager *manager, bool reload);
196 int netdev_load_one(Manager *manager, const char *filename);
197 void netdev_drop(NetDev *netdev);
198 void netdev_enter_failed(NetDev *netdev);
199 
200 NetDev *netdev_unref(NetDev *netdev);
201 NetDev *netdev_ref(NetDev *netdev);
202 DEFINE_TRIVIAL_DESTRUCTOR(netdev_destroy_callback, NetDev, netdev_unref);
203 DEFINE_TRIVIAL_CLEANUP_FUNC(NetDev*, netdev_unref);
204 
205 bool netdev_is_managed(NetDev *netdev);
206 int netdev_get(Manager *manager, const char *name, NetDev **ret);
207 int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *newlink);
208 int netdev_generate_hw_addr(NetDev *netdev, Link *link, const char *name,
209                             const struct hw_addr_data *hw_addr, struct hw_addr_data *ret);
210 
211 int link_request_stacked_netdev(Link *link, NetDev *netdev);
212 
213 const char *netdev_kind_to_string(NetDevKind d) _const_;
214 NetDevKind netdev_kind_from_string(const char *d) _pure_;
215 
netdev_get_create_type(NetDev * netdev)216 static inline NetDevCreateType netdev_get_create_type(NetDev *netdev) {
217         assert(netdev);
218         assert(NETDEV_VTABLE(netdev));
219 
220         return NETDEV_VTABLE(netdev)->create_type;
221 }
222 
223 CONFIG_PARSER_PROTOTYPE(config_parse_netdev_kind);
224 CONFIG_PARSER_PROTOTYPE(config_parse_netdev_hw_addr);
225 
226 /* gperf */
227 const struct ConfigPerfItem* network_netdev_gperf_lookup(const char *key, GPERF_LEN_TYPE length);
228 
229 /* Macros which append INTERFACE= to the message */
230 
231 #define log_netdev_full_errno_zerook(netdev, level, error, ...)         \
232         ({                                                              \
233                 const NetDev *_n = (netdev);                            \
234                 log_interface_full_errno_zerook(_n ? _n->ifname : NULL, level, error, __VA_ARGS__); \
235         })
236 
237 #define log_netdev_full_errno(netdev, level, error, ...) \
238         ({                                                              \
239                 int _error = (error);                                   \
240                 ASSERT_NON_ZERO(_error);                                \
241                 log_netdev_full_errno_zerook(netdev, level, _error, __VA_ARGS__); \
242         })
243 
244 #define log_netdev_full(netdev, level, ...) (void) log_netdev_full_errno_zerook(netdev, level, 0, __VA_ARGS__)
245 
246 #define log_netdev_debug(netdev, ...)   log_netdev_full(netdev, LOG_DEBUG, __VA_ARGS__)
247 #define log_netdev_info(netdev, ...)    log_netdev_full(netdev, LOG_INFO, __VA_ARGS__)
248 #define log_netdev_notice(netdev, ...)  log_netdev_full(netdev, LOG_NOTICE, __VA_ARGS__)
249 #define log_netdev_warning(netdev, ...) log_netdev_full(netdev, LOG_WARNING,  __VA_ARGS__)
250 #define log_netdev_error(netdev, ...)   log_netdev_full(netdev, LOG_ERR, __VA_ARGS__)
251 
252 #define log_netdev_debug_errno(netdev, error, ...)   log_netdev_full_errno(netdev, LOG_DEBUG, error, __VA_ARGS__)
253 #define log_netdev_info_errno(netdev, error, ...)    log_netdev_full_errno(netdev, LOG_INFO, error, __VA_ARGS__)
254 #define log_netdev_notice_errno(netdev, error, ...)  log_netdev_full_errno(netdev, LOG_NOTICE, error, __VA_ARGS__)
255 #define log_netdev_warning_errno(netdev, error, ...) log_netdev_full_errno(netdev, LOG_WARNING, error, __VA_ARGS__)
256 #define log_netdev_error_errno(netdev, error, ...)   log_netdev_full_errno(netdev, LOG_ERR, error, __VA_ARGS__)
257 
258 #define LOG_NETDEV_MESSAGE(netdev, fmt, ...) "MESSAGE=%s: " fmt, (netdev)->ifname, ##__VA_ARGS__
259 #define LOG_NETDEV_INTERFACE(netdev) "INTERFACE=%s", (netdev)->ifname
260