]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-netlink/netlink-types.c
Merge pull request #11975 from keszybz/fuzzer-fixes-n
[thirdparty/systemd.git] / src / libsystemd / sd-netlink / netlink-types.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <netinet/in.h>
4 #include <stdint.h>
5 #include <sys/socket.h>
6 #include <linux/netlink.h>
7 #include <linux/rtnetlink.h>
8 #include <linux/genetlink.h>
9 #include <linux/ip.h>
10 #include <linux/if.h>
11 #include <linux/can/netlink.h>
12 #include <linux/fib_rules.h>
13 #include <linux/if_addr.h>
14 #include <linux/if_addrlabel.h>
15 #include <linux/if_bridge.h>
16 #include <linux/if_link.h>
17 #include <linux/if_tunnel.h>
18 #include <linux/l2tp.h>
19 #include <linux/veth.h>
20
21 #if HAVE_LINUX_FOU_H
22 #include <linux/fou.h>
23 #endif
24
25 #if HAVE_LINUX_CAN_VXCAN_H
26 #include <linux/can/vxcan.h>
27 #endif
28
29 #include "macro.h"
30 #include "missing.h"
31 #include "netlink-types.h"
32 #include "sd-netlink.h"
33 #include "string-table.h"
34 #include "util.h"
35 #include "wireguard-netlink.h"
36
37 /* Maximum ARP IP target defined in kernel */
38 #define BOND_MAX_ARP_TARGETS 16
39
40 typedef enum {
41 BOND_ARP_TARGETS_0,
42 BOND_ARP_TARGETS_1,
43 BOND_ARP_TARGETS_2,
44 BOND_ARP_TARGETS_3,
45 BOND_ARP_TARGETS_4,
46 BOND_ARP_TARGETS_5,
47 BOND_ARP_TARGETS_6,
48 BOND_ARP_TARGETS_7,
49 BOND_ARP_TARGETS_8,
50 BOND_ARP_TARGETS_9,
51 BOND_ARP_TARGETS_10,
52 BOND_ARP_TARGETS_11,
53 BOND_ARP_TARGETS_12,
54 BOND_ARP_TARGETS_13,
55 BOND_ARP_TARGETS_14,
56 BOND_ARP_TARGETS_MAX = BOND_MAX_ARP_TARGETS,
57 } BondArpTargets;
58
59 struct NLType {
60 uint16_t type;
61 size_t size;
62 const NLTypeSystem *type_system;
63 const NLTypeSystemUnion *type_system_union;
64 };
65
66 struct NLTypeSystem {
67 uint16_t count;
68 const NLType *types;
69 };
70
71 static const NLTypeSystem rtnl_link_type_system;
72
73 static const NLType empty_types[1] = {
74 /* fake array to avoid .types==NULL, which denotes invalid type-systems */
75 };
76
77 static const NLTypeSystem empty_type_system = {
78 .count = 0,
79 .types = empty_types,
80 };
81
82 static const NLType rtnl_link_info_data_veth_types[] = {
83 [VETH_INFO_PEER] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
84 };
85
86 static const NLType rtnl_link_info_data_vxcan_types[] = {
87 [VXCAN_INFO_PEER] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
88 };
89
90 static const NLType rtnl_link_info_data_ipvlan_types[] = {
91 [IFLA_IPVLAN_MODE] = { .type = NETLINK_TYPE_U16 },
92 [IFLA_IPVLAN_FLAGS] = { .type = NETLINK_TYPE_U16 },
93 };
94
95 static const NLType rtnl_link_info_data_macvlan_types[] = {
96 [IFLA_MACVLAN_MODE] = { .type = NETLINK_TYPE_U32 },
97 [IFLA_MACVLAN_FLAGS] = { .type = NETLINK_TYPE_U16 },
98 };
99
100 static const NLType rtnl_link_info_data_bridge_types[] = {
101 [IFLA_BR_FORWARD_DELAY] = { .type = NETLINK_TYPE_U32 },
102 [IFLA_BR_HELLO_TIME] = { .type = NETLINK_TYPE_U32 },
103 [IFLA_BR_MAX_AGE] = { .type = NETLINK_TYPE_U32 },
104 [IFLA_BR_AGEING_TIME] = { .type = NETLINK_TYPE_U32 },
105 [IFLA_BR_STP_STATE] = { .type = NETLINK_TYPE_U32 },
106 [IFLA_BR_PRIORITY] = { .type = NETLINK_TYPE_U16 },
107 [IFLA_BR_VLAN_FILTERING] = { .type = NETLINK_TYPE_U8 },
108 [IFLA_BR_VLAN_PROTOCOL] = { .type = NETLINK_TYPE_U16 },
109 [IFLA_BR_GROUP_FWD_MASK] = { .type = NETLINK_TYPE_U16 },
110 [IFLA_BR_ROOT_PORT] = { .type = NETLINK_TYPE_U16 },
111 [IFLA_BR_ROOT_PATH_COST] = { .type = NETLINK_TYPE_U32 },
112 [IFLA_BR_TOPOLOGY_CHANGE] = { .type = NETLINK_TYPE_U8 },
113 [IFLA_BR_TOPOLOGY_CHANGE_DETECTED] = { .type = NETLINK_TYPE_U8 },
114 [IFLA_BR_HELLO_TIMER] = { .type = NETLINK_TYPE_U16 },
115 [IFLA_BR_TCN_TIMER] = { .type = NETLINK_TYPE_U16 },
116 [IFLA_BR_TOPOLOGY_CHANGE_TIMER] = { .type = NETLINK_TYPE_U16 },
117 [IFLA_BR_GC_TIMER] = { .type = NETLINK_TYPE_U64 },
118 [IFLA_BR_GROUP_ADDR] = { .type = NETLINK_TYPE_U16 },
119 [IFLA_BR_FDB_FLUSH] = { .type = NETLINK_TYPE_U16 },
120 [IFLA_BR_MCAST_ROUTER] = { .type = NETLINK_TYPE_U8 },
121 [IFLA_BR_MCAST_SNOOPING] = { .type = NETLINK_TYPE_U8 },
122 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NETLINK_TYPE_U8 },
123 [IFLA_BR_MCAST_QUERIER] = { .type = NETLINK_TYPE_U8 },
124 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NETLINK_TYPE_U32 },
125 [IFLA_BR_MCAST_HASH_MAX] = { .type = NETLINK_TYPE_U16 },
126 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NETLINK_TYPE_U32 },
127 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NETLINK_TYPE_U16 },
128 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NETLINK_TYPE_U64 },
129 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NETLINK_TYPE_U64 },
130 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NETLINK_TYPE_U64 },
131 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NETLINK_TYPE_U64 },
132 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NETLINK_TYPE_U64 },
133 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NETLINK_TYPE_U64 },
134 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NETLINK_TYPE_U8 },
135 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NETLINK_TYPE_U8 },
136 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NETLINK_TYPE_U8 },
137 [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NETLINK_TYPE_U16 },
138 };
139
140 static const NLType rtnl_link_info_data_vlan_types[] = {
141 [IFLA_VLAN_ID] = { .type = NETLINK_TYPE_U16 },
142 /*
143 [IFLA_VLAN_FLAGS] = { .len = sizeof(struct ifla_vlan_flags) },
144 [IFLA_VLAN_EGRESS_QOS] = { .type = NETLINK_TYPE_NESTED },
145 [IFLA_VLAN_INGRESS_QOS] = { .type = NETLINK_TYPE_NESTED },
146 */
147 [IFLA_VLAN_PROTOCOL] = { .type = NETLINK_TYPE_U16 },
148 };
149
150 static const NLType rtnl_link_info_data_vxlan_types[] = {
151 [IFLA_VXLAN_ID] = { .type = NETLINK_TYPE_U32 },
152 [IFLA_VXLAN_GROUP] = { .type = NETLINK_TYPE_IN_ADDR },
153 [IFLA_VXLAN_LINK] = { .type = NETLINK_TYPE_U32 },
154 [IFLA_VXLAN_LOCAL] = { .type = NETLINK_TYPE_IN_ADDR },
155 [IFLA_VXLAN_TTL] = { .type = NETLINK_TYPE_U8 },
156 [IFLA_VXLAN_TOS] = { .type = NETLINK_TYPE_U8 },
157 [IFLA_VXLAN_LEARNING] = { .type = NETLINK_TYPE_U8 },
158 [IFLA_VXLAN_AGEING] = { .type = NETLINK_TYPE_U32 },
159 [IFLA_VXLAN_LIMIT] = { .type = NETLINK_TYPE_U32 },
160 [IFLA_VXLAN_PORT_RANGE] = { .type = NETLINK_TYPE_U32},
161 [IFLA_VXLAN_PROXY] = { .type = NETLINK_TYPE_U8 },
162 [IFLA_VXLAN_RSC] = { .type = NETLINK_TYPE_U8 },
163 [IFLA_VXLAN_L2MISS] = { .type = NETLINK_TYPE_U8 },
164 [IFLA_VXLAN_L3MISS] = { .type = NETLINK_TYPE_U8 },
165 [IFLA_VXLAN_PORT] = { .type = NETLINK_TYPE_U16 },
166 [IFLA_VXLAN_GROUP6] = { .type = NETLINK_TYPE_IN_ADDR },
167 [IFLA_VXLAN_LOCAL6] = { .type = NETLINK_TYPE_IN_ADDR },
168 [IFLA_VXLAN_UDP_CSUM] = { .type = NETLINK_TYPE_U8 },
169 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NETLINK_TYPE_U8 },
170 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NETLINK_TYPE_U8 },
171 [IFLA_VXLAN_REMCSUM_TX] = { .type = NETLINK_TYPE_U8 },
172 [IFLA_VXLAN_REMCSUM_RX] = { .type = NETLINK_TYPE_U8 },
173 [IFLA_VXLAN_GBP] = { .type = NETLINK_TYPE_FLAG },
174 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NETLINK_TYPE_FLAG },
175 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NETLINK_TYPE_U8 },
176 [IFLA_VXLAN_LABEL] = { .type = NETLINK_TYPE_U32 },
177 [IFLA_VXLAN_GPE] = { .type = NETLINK_TYPE_FLAG },
178 };
179
180 static const NLType rtnl_bond_arp_target_types[] = {
181 [BOND_ARP_TARGETS_0] = { .type = NETLINK_TYPE_U32 },
182 [BOND_ARP_TARGETS_1] = { .type = NETLINK_TYPE_U32 },
183 [BOND_ARP_TARGETS_2] = { .type = NETLINK_TYPE_U32 },
184 [BOND_ARP_TARGETS_3] = { .type = NETLINK_TYPE_U32 },
185 [BOND_ARP_TARGETS_4] = { .type = NETLINK_TYPE_U32 },
186 [BOND_ARP_TARGETS_5] = { .type = NETLINK_TYPE_U32 },
187 [BOND_ARP_TARGETS_6] = { .type = NETLINK_TYPE_U32 },
188 [BOND_ARP_TARGETS_7] = { .type = NETLINK_TYPE_U32 },
189 [BOND_ARP_TARGETS_8] = { .type = NETLINK_TYPE_U32 },
190 [BOND_ARP_TARGETS_9] = { .type = NETLINK_TYPE_U32 },
191 [BOND_ARP_TARGETS_10] = { .type = NETLINK_TYPE_U32 },
192 [BOND_ARP_TARGETS_11] = { .type = NETLINK_TYPE_U32 },
193 [BOND_ARP_TARGETS_12] = { .type = NETLINK_TYPE_U32 },
194 [BOND_ARP_TARGETS_13] = { .type = NETLINK_TYPE_U32 },
195 [BOND_ARP_TARGETS_14] = { .type = NETLINK_TYPE_U32 },
196 [BOND_ARP_TARGETS_MAX] = { .type = NETLINK_TYPE_U32 },
197 };
198
199 static const NLTypeSystem rtnl_bond_arp_type_system = {
200 .count = ELEMENTSOF(rtnl_bond_arp_target_types),
201 .types = rtnl_bond_arp_target_types,
202 };
203
204 static const NLType rtnl_link_info_data_bond_types[] = {
205 [IFLA_BOND_MODE] = { .type = NETLINK_TYPE_U8 },
206 [IFLA_BOND_ACTIVE_SLAVE] = { .type = NETLINK_TYPE_U32 },
207 [IFLA_BOND_MIIMON] = { .type = NETLINK_TYPE_U32 },
208 [IFLA_BOND_UPDELAY] = { .type = NETLINK_TYPE_U32 },
209 [IFLA_BOND_DOWNDELAY] = { .type = NETLINK_TYPE_U32 },
210 [IFLA_BOND_USE_CARRIER] = { .type = NETLINK_TYPE_U8 },
211 [IFLA_BOND_ARP_INTERVAL] = { .type = NETLINK_TYPE_U32 },
212 [IFLA_BOND_ARP_IP_TARGET] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_bond_arp_type_system },
213 [IFLA_BOND_ARP_VALIDATE] = { .type = NETLINK_TYPE_U32 },
214 [IFLA_BOND_ARP_ALL_TARGETS] = { .type = NETLINK_TYPE_U32 },
215 [IFLA_BOND_PRIMARY] = { .type = NETLINK_TYPE_U32 },
216 [IFLA_BOND_PRIMARY_RESELECT] = { .type = NETLINK_TYPE_U8 },
217 [IFLA_BOND_FAIL_OVER_MAC] = { .type = NETLINK_TYPE_U8 },
218 [IFLA_BOND_XMIT_HASH_POLICY] = { .type = NETLINK_TYPE_U8 },
219 [IFLA_BOND_RESEND_IGMP] = { .type = NETLINK_TYPE_U32 },
220 [IFLA_BOND_NUM_PEER_NOTIF] = { .type = NETLINK_TYPE_U8 },
221 [IFLA_BOND_ALL_SLAVES_ACTIVE] = { .type = NETLINK_TYPE_U8 },
222 [IFLA_BOND_MIN_LINKS] = { .type = NETLINK_TYPE_U32 },
223 [IFLA_BOND_LP_INTERVAL] = { .type = NETLINK_TYPE_U32 },
224 [IFLA_BOND_PACKETS_PER_SLAVE] = { .type = NETLINK_TYPE_U32 },
225 [IFLA_BOND_AD_LACP_RATE] = { .type = NETLINK_TYPE_U8 },
226 [IFLA_BOND_AD_SELECT] = { .type = NETLINK_TYPE_U8 },
227 [IFLA_BOND_AD_INFO] = { .type = NETLINK_TYPE_NESTED },
228 [IFLA_BOND_AD_ACTOR_SYS_PRIO] = { .type = NETLINK_TYPE_U16 },
229 [IFLA_BOND_AD_USER_PORT_KEY] = { .type = NETLINK_TYPE_U16 },
230 [IFLA_BOND_AD_ACTOR_SYSTEM] = { .type = NETLINK_TYPE_ETHER_ADDR },
231 [IFLA_BOND_TLB_DYNAMIC_LB] = { .type = NETLINK_TYPE_U8 },
232 };
233
234 static const NLType rtnl_link_info_data_iptun_types[] = {
235 [IFLA_IPTUN_LINK] = { .type = NETLINK_TYPE_U32 },
236 [IFLA_IPTUN_LOCAL] = { .type = NETLINK_TYPE_IN_ADDR },
237 [IFLA_IPTUN_REMOTE] = { .type = NETLINK_TYPE_IN_ADDR },
238 [IFLA_IPTUN_TTL] = { .type = NETLINK_TYPE_U8 },
239 [IFLA_IPTUN_TOS] = { .type = NETLINK_TYPE_U8 },
240 [IFLA_IPTUN_PMTUDISC] = { .type = NETLINK_TYPE_U8 },
241 [IFLA_IPTUN_FLAGS] = { .type = NETLINK_TYPE_U16 },
242 [IFLA_IPTUN_PROTO] = { .type = NETLINK_TYPE_U8 },
243 [IFLA_IPTUN_6RD_PREFIX] = { .type = NETLINK_TYPE_IN_ADDR },
244 [IFLA_IPTUN_6RD_RELAY_PREFIX] = { .type = NETLINK_TYPE_U32 },
245 [IFLA_IPTUN_6RD_PREFIXLEN] = { .type = NETLINK_TYPE_U16 },
246 [IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NETLINK_TYPE_U16 },
247 [IFLA_IPTUN_ENCAP_TYPE] = { .type = NETLINK_TYPE_U16 },
248 [IFLA_IPTUN_ENCAP_FLAGS] = { .type = NETLINK_TYPE_U16 },
249 [IFLA_IPTUN_ENCAP_SPORT] = { .type = NETLINK_TYPE_U16 },
250 [IFLA_IPTUN_ENCAP_DPORT] = { .type = NETLINK_TYPE_U16 },
251 };
252
253 static const NLType rtnl_link_info_data_ipgre_types[] = {
254 [IFLA_GRE_LINK] = { .type = NETLINK_TYPE_U32 },
255 [IFLA_GRE_IFLAGS] = { .type = NETLINK_TYPE_U16 },
256 [IFLA_GRE_OFLAGS] = { .type = NETLINK_TYPE_U16 },
257 [IFLA_GRE_IKEY] = { .type = NETLINK_TYPE_U32 },
258 [IFLA_GRE_OKEY] = { .type = NETLINK_TYPE_U32 },
259 [IFLA_GRE_LOCAL] = { .type = NETLINK_TYPE_IN_ADDR },
260 [IFLA_GRE_REMOTE] = { .type = NETLINK_TYPE_IN_ADDR },
261 [IFLA_GRE_TTL] = { .type = NETLINK_TYPE_U8 },
262 [IFLA_GRE_TOS] = { .type = NETLINK_TYPE_U8 },
263 [IFLA_GRE_PMTUDISC] = { .type = NETLINK_TYPE_U8 },
264 [IFLA_GRE_FLOWINFO] = { .type = NETLINK_TYPE_U32 },
265 [IFLA_GRE_FLAGS] = { .type = NETLINK_TYPE_U32 },
266 [IFLA_GRE_ENCAP_TYPE] = { .type = NETLINK_TYPE_U16 },
267 [IFLA_GRE_ENCAP_FLAGS] = { .type = NETLINK_TYPE_U16 },
268 [IFLA_GRE_ENCAP_SPORT] = { .type = NETLINK_TYPE_U16 },
269 [IFLA_GRE_ENCAP_DPORT] = { .type = NETLINK_TYPE_U16 },
270 [IFLA_GRE_ERSPAN_INDEX] = { .type = NETLINK_TYPE_U32 },
271 };
272
273 static const NLType rtnl_link_info_data_ipvti_types[] = {
274 [IFLA_VTI_LINK] = { .type = NETLINK_TYPE_U32 },
275 [IFLA_VTI_IKEY] = { .type = NETLINK_TYPE_U32 },
276 [IFLA_VTI_OKEY] = { .type = NETLINK_TYPE_U32 },
277 [IFLA_VTI_LOCAL] = { .type = NETLINK_TYPE_IN_ADDR },
278 [IFLA_VTI_REMOTE] = { .type = NETLINK_TYPE_IN_ADDR },
279 };
280
281 static const NLType rtnl_link_info_data_ip6tnl_types[] = {
282 [IFLA_IPTUN_LINK] = { .type = NETLINK_TYPE_U32 },
283 [IFLA_IPTUN_LOCAL] = { .type = NETLINK_TYPE_IN_ADDR },
284 [IFLA_IPTUN_REMOTE] = { .type = NETLINK_TYPE_IN_ADDR },
285 [IFLA_IPTUN_TTL] = { .type = NETLINK_TYPE_U8 },
286 [IFLA_IPTUN_FLAGS] = { .type = NETLINK_TYPE_U32 },
287 [IFLA_IPTUN_PROTO] = { .type = NETLINK_TYPE_U8 },
288 [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NETLINK_TYPE_U8 },
289 [IFLA_IPTUN_FLOWINFO] = { .type = NETLINK_TYPE_U32 },
290 };
291
292 static const NLType rtnl_link_info_data_vrf_types[] = {
293 [IFLA_VRF_TABLE] = { .type = NETLINK_TYPE_U32 },
294 };
295
296 static const NLType rtnl_link_info_data_geneve_types[] = {
297 [IFLA_GENEVE_ID] = { .type = NETLINK_TYPE_U32 },
298 [IFLA_GENEVE_TTL] = { .type = NETLINK_TYPE_U8 },
299 [IFLA_GENEVE_TOS] = { .type = NETLINK_TYPE_U8 },
300 [IFLA_GENEVE_PORT] = { .type = NETLINK_TYPE_U16 },
301 [IFLA_GENEVE_REMOTE] = { .type = NETLINK_TYPE_IN_ADDR },
302 [IFLA_GENEVE_REMOTE6] = { .type = NETLINK_TYPE_IN_ADDR },
303 [IFLA_GENEVE_UDP_CSUM] = { .type = NETLINK_TYPE_U8 },
304 [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NETLINK_TYPE_U8 },
305 [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NETLINK_TYPE_U8 },
306 [IFLA_GENEVE_LABEL] = { .type = NETLINK_TYPE_U32 },
307 };
308
309 static const NLType rtnl_link_info_data_can_types[] = {
310 [IFLA_CAN_BITTIMING] = { .size = sizeof(struct can_bittiming) },
311 [IFLA_CAN_RESTART_MS] = { .type = NETLINK_TYPE_U32 },
312 [IFLA_CAN_CTRLMODE] = { .size = sizeof(struct can_ctrlmode) },
313 };
314
315 /* these strings must match the .kind entries in the kernel */
316 static const char* const nl_union_link_info_data_table[] = {
317 [NL_UNION_LINK_INFO_DATA_BOND] = "bond",
318 [NL_UNION_LINK_INFO_DATA_BRIDGE] = "bridge",
319 [NL_UNION_LINK_INFO_DATA_VLAN] = "vlan",
320 [NL_UNION_LINK_INFO_DATA_VETH] = "veth",
321 [NL_UNION_LINK_INFO_DATA_DUMMY] = "dummy",
322 [NL_UNION_LINK_INFO_DATA_MACVLAN] = "macvlan",
323 [NL_UNION_LINK_INFO_DATA_MACVTAP] = "macvtap",
324 [NL_UNION_LINK_INFO_DATA_IPVLAN] = "ipvlan",
325 [NL_UNION_LINK_INFO_DATA_VXLAN] = "vxlan",
326 [NL_UNION_LINK_INFO_DATA_IPIP_TUNNEL] = "ipip",
327 [NL_UNION_LINK_INFO_DATA_IPGRE_TUNNEL] = "gre",
328 [NL_UNION_LINK_INFO_DATA_ERSPAN] = "erspan",
329 [NL_UNION_LINK_INFO_DATA_IPGRETAP_TUNNEL] = "gretap",
330 [NL_UNION_LINK_INFO_DATA_IP6GRE_TUNNEL] = "ip6gre",
331 [NL_UNION_LINK_INFO_DATA_IP6GRETAP_TUNNEL] = "ip6gretap",
332 [NL_UNION_LINK_INFO_DATA_SIT_TUNNEL] = "sit",
333 [NL_UNION_LINK_INFO_DATA_VTI_TUNNEL] = "vti",
334 [NL_UNION_LINK_INFO_DATA_VTI6_TUNNEL] = "vti6",
335 [NL_UNION_LINK_INFO_DATA_IP6TNL_TUNNEL] = "ip6tnl",
336 [NL_UNION_LINK_INFO_DATA_VRF] = "vrf",
337 [NL_UNION_LINK_INFO_DATA_VCAN] = "vcan",
338 [NL_UNION_LINK_INFO_DATA_GENEVE] = "geneve",
339 [NL_UNION_LINK_INFO_DATA_VXCAN] = "vxcan",
340 [NL_UNION_LINK_INFO_DATA_WIREGUARD] = "wireguard",
341 [NL_UNION_LINK_INFO_DATA_NETDEVSIM] = "netdevsim",
342 [NL_UNION_LINK_INFO_DATA_CAN] = "can",
343 };
344
345 DEFINE_STRING_TABLE_LOOKUP(nl_union_link_info_data, NLUnionLinkInfoData);
346
347 static const NLTypeSystem rtnl_link_info_data_type_systems[] = {
348 [NL_UNION_LINK_INFO_DATA_BOND] = { .count = ELEMENTSOF(rtnl_link_info_data_bond_types),
349 .types = rtnl_link_info_data_bond_types },
350 [NL_UNION_LINK_INFO_DATA_BRIDGE] = { .count = ELEMENTSOF(rtnl_link_info_data_bridge_types),
351 .types = rtnl_link_info_data_bridge_types },
352 [NL_UNION_LINK_INFO_DATA_VLAN] = { .count = ELEMENTSOF(rtnl_link_info_data_vlan_types),
353 .types = rtnl_link_info_data_vlan_types },
354 [NL_UNION_LINK_INFO_DATA_VETH] = { .count = ELEMENTSOF(rtnl_link_info_data_veth_types),
355 .types = rtnl_link_info_data_veth_types },
356 [NL_UNION_LINK_INFO_DATA_MACVLAN] = { .count = ELEMENTSOF(rtnl_link_info_data_macvlan_types),
357 .types = rtnl_link_info_data_macvlan_types },
358 [NL_UNION_LINK_INFO_DATA_MACVTAP] = { .count = ELEMENTSOF(rtnl_link_info_data_macvlan_types),
359 .types = rtnl_link_info_data_macvlan_types },
360 [NL_UNION_LINK_INFO_DATA_IPVLAN] = { .count = ELEMENTSOF(rtnl_link_info_data_ipvlan_types),
361 .types = rtnl_link_info_data_ipvlan_types },
362 [NL_UNION_LINK_INFO_DATA_VXLAN] = { .count = ELEMENTSOF(rtnl_link_info_data_vxlan_types),
363 .types = rtnl_link_info_data_vxlan_types },
364 [NL_UNION_LINK_INFO_DATA_IPIP_TUNNEL] = { .count = ELEMENTSOF(rtnl_link_info_data_iptun_types),
365 .types = rtnl_link_info_data_iptun_types },
366 [NL_UNION_LINK_INFO_DATA_IPGRE_TUNNEL] = { .count = ELEMENTSOF(rtnl_link_info_data_ipgre_types),
367 .types = rtnl_link_info_data_ipgre_types },
368 [NL_UNION_LINK_INFO_DATA_ERSPAN] = { .count = ELEMENTSOF(rtnl_link_info_data_ipgre_types),
369 .types = rtnl_link_info_data_ipgre_types },
370 [NL_UNION_LINK_INFO_DATA_IPGRETAP_TUNNEL] = { .count = ELEMENTSOF(rtnl_link_info_data_ipgre_types),
371 .types = rtnl_link_info_data_ipgre_types },
372 [NL_UNION_LINK_INFO_DATA_IP6GRE_TUNNEL] = { .count = ELEMENTSOF(rtnl_link_info_data_ipgre_types),
373 .types = rtnl_link_info_data_ipgre_types },
374 [NL_UNION_LINK_INFO_DATA_IP6GRETAP_TUNNEL] = { .count = ELEMENTSOF(rtnl_link_info_data_ipgre_types),
375 .types = rtnl_link_info_data_ipgre_types },
376 [NL_UNION_LINK_INFO_DATA_SIT_TUNNEL] = { .count = ELEMENTSOF(rtnl_link_info_data_iptun_types),
377 .types = rtnl_link_info_data_iptun_types },
378 [NL_UNION_LINK_INFO_DATA_VTI_TUNNEL] = { .count = ELEMENTSOF(rtnl_link_info_data_ipvti_types),
379 .types = rtnl_link_info_data_ipvti_types },
380 [NL_UNION_LINK_INFO_DATA_VTI6_TUNNEL] = { .count = ELEMENTSOF(rtnl_link_info_data_ipvti_types),
381 .types = rtnl_link_info_data_ipvti_types },
382 [NL_UNION_LINK_INFO_DATA_IP6TNL_TUNNEL] = { .count = ELEMENTSOF(rtnl_link_info_data_ip6tnl_types),
383 .types = rtnl_link_info_data_ip6tnl_types },
384 [NL_UNION_LINK_INFO_DATA_VRF] = { .count = ELEMENTSOF(rtnl_link_info_data_vrf_types),
385 .types = rtnl_link_info_data_vrf_types },
386 [NL_UNION_LINK_INFO_DATA_GENEVE] = { .count = ELEMENTSOF(rtnl_link_info_data_geneve_types),
387 .types = rtnl_link_info_data_geneve_types },
388 [NL_UNION_LINK_INFO_DATA_VXCAN] = { .count = ELEMENTSOF(rtnl_link_info_data_vxcan_types),
389 .types = rtnl_link_info_data_vxcan_types },
390 [NL_UNION_LINK_INFO_DATA_CAN] = { .count = ELEMENTSOF(rtnl_link_info_data_can_types),
391 .types = rtnl_link_info_data_can_types },
392 };
393
394 static const NLTypeSystemUnion rtnl_link_info_data_type_system_union = {
395 .num = _NL_UNION_LINK_INFO_DATA_MAX,
396 .lookup = nl_union_link_info_data_from_string,
397 .type_systems = rtnl_link_info_data_type_systems,
398 .match_type = NL_MATCH_SIBLING,
399 .match = IFLA_INFO_KIND,
400 };
401
402 static const NLType rtnl_link_info_types[] = {
403 [IFLA_INFO_KIND] = { .type = NETLINK_TYPE_STRING },
404 [IFLA_INFO_DATA] = { .type = NETLINK_TYPE_UNION, .type_system_union = &rtnl_link_info_data_type_system_union},
405 /*
406 [IFLA_INFO_XSTATS],
407 [IFLA_INFO_SLAVE_KIND] = { .type = NETLINK_TYPE_STRING },
408 [IFLA_INFO_SLAVE_DATA] = { .type = NETLINK_TYPE_NESTED },
409 */
410 };
411
412 static const NLTypeSystem rtnl_link_info_type_system = {
413 .count = ELEMENTSOF(rtnl_link_info_types),
414 .types = rtnl_link_info_types,
415 };
416
417 static const struct NLType rtnl_prot_info_bridge_port_types[] = {
418 [IFLA_BRPORT_STATE] = { .type = NETLINK_TYPE_U8 },
419 [IFLA_BRPORT_COST] = { .type = NETLINK_TYPE_U32 },
420 [IFLA_BRPORT_PRIORITY] = { .type = NETLINK_TYPE_U16 },
421 [IFLA_BRPORT_MODE] = { .type = NETLINK_TYPE_U8 },
422 [IFLA_BRPORT_GUARD] = { .type = NETLINK_TYPE_U8 },
423 [IFLA_BRPORT_PROTECT] = { .type = NETLINK_TYPE_U8 },
424 [IFLA_BRPORT_FAST_LEAVE] = { .type = NETLINK_TYPE_U8 },
425 [IFLA_BRPORT_LEARNING] = { .type = NETLINK_TYPE_U8 },
426 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NETLINK_TYPE_U8 },
427 [IFLA_BRPORT_PROXYARP] = { .type = NETLINK_TYPE_U8 },
428 [IFLA_BRPORT_LEARNING_SYNC] = { .type = NETLINK_TYPE_U8 },
429 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NETLINK_TYPE_U8 },
430 [IFLA_BRPORT_ROOT_ID] = { .type = NETLINK_TYPE_U8 },
431 [IFLA_BRPORT_BRIDGE_ID] = { .type = NETLINK_TYPE_U8 },
432 [IFLA_BRPORT_DESIGNATED_PORT] = { .type = NETLINK_TYPE_U16 },
433 [IFLA_BRPORT_DESIGNATED_COST] = { .type = NETLINK_TYPE_U16 },
434 [IFLA_BRPORT_ID] = { .type = NETLINK_TYPE_U16 },
435 [IFLA_BRPORT_NO] = { .type = NETLINK_TYPE_U16 },
436 [IFLA_BRPORT_TOPOLOGY_CHANGE_ACK] = { .type = NETLINK_TYPE_U8 },
437 [IFLA_BRPORT_CONFIG_PENDING] = { .type = NETLINK_TYPE_U8 },
438 [IFLA_BRPORT_MESSAGE_AGE_TIMER] = { .type = NETLINK_TYPE_U64 },
439 [IFLA_BRPORT_FORWARD_DELAY_TIMER] = { .type = NETLINK_TYPE_U64 },
440 [IFLA_BRPORT_HOLD_TIMER] = { .type = NETLINK_TYPE_U64 },
441 [IFLA_BRPORT_FLUSH] = { .type = NETLINK_TYPE_U8 },
442 [IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NETLINK_TYPE_U8 },
443 [IFLA_BRPORT_PAD] = { .type = NETLINK_TYPE_U8 },
444 [IFLA_BRPORT_MCAST_FLOOD] = { .type = NETLINK_TYPE_U8 },
445 [IFLA_BRPORT_MCAST_TO_UCAST] = { .type = NETLINK_TYPE_U8 },
446 [IFLA_BRPORT_VLAN_TUNNEL] = { .type = NETLINK_TYPE_U8 },
447 [IFLA_BRPORT_BCAST_FLOOD] = { .type = NETLINK_TYPE_U8 },
448 [IFLA_BRPORT_GROUP_FWD_MASK] = { .type = NETLINK_TYPE_U16 },
449 [IFLA_BRPORT_NEIGH_SUPPRESS] = { .type = NETLINK_TYPE_U8 },
450 [IFLA_BRPORT_ISOLATED] = { .type = NETLINK_TYPE_U8 },
451 [IFLA_BRPORT_BACKUP_PORT] = { .type = NETLINK_TYPE_U32 },
452 };
453
454 static const NLTypeSystem rtnl_prot_info_type_systems[] = {
455 [AF_BRIDGE] = { .count = ELEMENTSOF(rtnl_prot_info_bridge_port_types),
456 .types = rtnl_prot_info_bridge_port_types },
457 };
458
459 static const NLTypeSystemUnion rtnl_prot_info_type_system_union = {
460 .num = AF_MAX,
461 .type_systems = rtnl_prot_info_type_systems,
462 .match_type = NL_MATCH_PROTOCOL,
463 };
464
465 static const struct NLType rtnl_af_spec_inet6_types[] = {
466 [IFLA_INET6_FLAGS] = { .type = NETLINK_TYPE_U32 },
467 /*
468 IFLA_INET6_CONF,
469 IFLA_INET6_STATS,
470 IFLA_INET6_MCAST,
471 IFLA_INET6_CACHEINFO,
472 IFLA_INET6_ICMP6STATS,
473 */
474 [IFLA_INET6_TOKEN] = { .type = NETLINK_TYPE_IN_ADDR },
475 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NETLINK_TYPE_U8 },
476 };
477
478 static const NLTypeSystem rtnl_af_spec_inet6_type_system = {
479 .count = ELEMENTSOF(rtnl_af_spec_inet6_types),
480 .types = rtnl_af_spec_inet6_types,
481 };
482
483 static const NLType rtnl_af_spec_types[] = {
484 [AF_INET6] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_af_spec_inet6_type_system },
485 };
486
487 static const NLTypeSystem rtnl_af_spec_type_system = {
488 .count = ELEMENTSOF(rtnl_af_spec_types),
489 .types = rtnl_af_spec_types,
490 };
491
492 static const NLType rtnl_link_types[] = {
493 [IFLA_ADDRESS] = { .type = NETLINK_TYPE_ETHER_ADDR },
494 [IFLA_BROADCAST] = { .type = NETLINK_TYPE_ETHER_ADDR },
495 [IFLA_IFNAME] = { .type = NETLINK_TYPE_STRING, .size = IFNAMSIZ - 1 },
496 [IFLA_MTU] = { .type = NETLINK_TYPE_U32 },
497 [IFLA_LINK] = { .type = NETLINK_TYPE_U32 },
498 /*
499 [IFLA_QDISC],
500 [IFLA_STATS],
501 [IFLA_COST],
502 [IFLA_PRIORITY],
503 */
504 [IFLA_MASTER] = { .type = NETLINK_TYPE_U32 },
505 /*
506 [IFLA_WIRELESS],
507 */
508 [IFLA_PROTINFO] = { .type = NETLINK_TYPE_UNION, .type_system_union = &rtnl_prot_info_type_system_union },
509 [IFLA_TXQLEN] = { .type = NETLINK_TYPE_U32 },
510 /*
511 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
512 */
513 [IFLA_WEIGHT] = { .type = NETLINK_TYPE_U32 },
514 [IFLA_OPERSTATE] = { .type = NETLINK_TYPE_U8 },
515 [IFLA_LINKMODE] = { .type = NETLINK_TYPE_U8 },
516 [IFLA_LINKINFO] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_link_info_type_system },
517 [IFLA_NET_NS_PID] = { .type = NETLINK_TYPE_U32 },
518 [IFLA_IFALIAS] = { .type = NETLINK_TYPE_STRING, .size = IFALIASZ - 1 },
519 /*
520 [IFLA_NUM_VF],
521 [IFLA_VFINFO_LIST] = {. type = NETLINK_TYPE_NESTED, },
522 [IFLA_STATS64],
523 [IFLA_VF_PORTS] = { .type = NETLINK_TYPE_NESTED },
524 [IFLA_PORT_SELF] = { .type = NETLINK_TYPE_NESTED },
525 */
526 [IFLA_AF_SPEC] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_af_spec_type_system },
527 /*
528 [IFLA_VF_PORTS],
529 [IFLA_PORT_SELF],
530 [IFLA_AF_SPEC],
531 */
532 [IFLA_GROUP] = { .type = NETLINK_TYPE_U32 },
533 [IFLA_NET_NS_FD] = { .type = NETLINK_TYPE_U32 },
534 [IFLA_EXT_MASK] = { .type = NETLINK_TYPE_U32 },
535 [IFLA_PROMISCUITY] = { .type = NETLINK_TYPE_U32 },
536 [IFLA_NUM_TX_QUEUES] = { .type = NETLINK_TYPE_U32 },
537 [IFLA_NUM_RX_QUEUES] = { .type = NETLINK_TYPE_U32 },
538 [IFLA_CARRIER] = { .type = NETLINK_TYPE_U8 },
539 /*
540 [IFLA_PHYS_PORT_ID] = { .type = NETLINK_TYPE_BINARY, .len = MAX_PHYS_PORT_ID_LEN },
541 */
542 };
543
544 static const NLTypeSystem rtnl_link_type_system = {
545 .count = ELEMENTSOF(rtnl_link_types),
546 .types = rtnl_link_types,
547 };
548
549 /* IFA_FLAGS was defined in kernel 3.14, but we still support older
550 * kernels where IFA_MAX is lower. */
551 static const NLType rtnl_address_types[] = {
552 [IFA_ADDRESS] = { .type = NETLINK_TYPE_IN_ADDR },
553 [IFA_LOCAL] = { .type = NETLINK_TYPE_IN_ADDR },
554 [IFA_LABEL] = { .type = NETLINK_TYPE_STRING, .size = IFNAMSIZ - 1 },
555 [IFA_BROADCAST] = { .type = NETLINK_TYPE_IN_ADDR }, /* 6? */
556 [IFA_CACHEINFO] = { .type = NETLINK_TYPE_CACHE_INFO, .size = sizeof(struct ifa_cacheinfo) },
557 /*
558 [IFA_ANYCAST],
559 [IFA_MULTICAST],
560 */
561 [IFA_FLAGS] = { .type = NETLINK_TYPE_U32 },
562 };
563
564 static const NLTypeSystem rtnl_address_type_system = {
565 .count = ELEMENTSOF(rtnl_address_types),
566 .types = rtnl_address_types,
567 };
568
569 /* RTM_METRICS --- array of struct rtattr with types of RTAX_* */
570
571 static const NLType rtnl_route_metrics_types[] = {
572 [RTAX_MTU] = { .type = NETLINK_TYPE_U32 },
573 [RTAX_WINDOW] = { .type = NETLINK_TYPE_U32 },
574 [RTAX_RTT] = { .type = NETLINK_TYPE_U32 },
575 [RTAX_RTTVAR] = { .type = NETLINK_TYPE_U32 },
576 [RTAX_SSTHRESH] = { .type = NETLINK_TYPE_U32 },
577 [RTAX_CWND] = { .type = NETLINK_TYPE_U32 },
578 [RTAX_ADVMSS] = { .type = NETLINK_TYPE_U32 },
579 [RTAX_REORDERING] = { .type = NETLINK_TYPE_U32 },
580 [RTAX_HOPLIMIT] = { .type = NETLINK_TYPE_U32 },
581 [RTAX_INITCWND] = { .type = NETLINK_TYPE_U32 },
582 [RTAX_FEATURES] = { .type = NETLINK_TYPE_U32 },
583 [RTAX_RTO_MIN] = { .type = NETLINK_TYPE_U32 },
584 [RTAX_INITRWND] = { .type = NETLINK_TYPE_U32 },
585 [RTAX_QUICKACK] = { .type = NETLINK_TYPE_U32 },
586 };
587
588 static const NLTypeSystem rtnl_route_metrics_type_system = {
589 .count = ELEMENTSOF(rtnl_route_metrics_types),
590 .types = rtnl_route_metrics_types,
591 };
592
593 static const NLType rtnl_route_types[] = {
594 [RTA_DST] = { .type = NETLINK_TYPE_IN_ADDR }, /* 6? */
595 [RTA_SRC] = { .type = NETLINK_TYPE_IN_ADDR }, /* 6? */
596 [RTA_IIF] = { .type = NETLINK_TYPE_U32 },
597 [RTA_OIF] = { .type = NETLINK_TYPE_U32 },
598 [RTA_GATEWAY] = { .type = NETLINK_TYPE_IN_ADDR },
599 [RTA_PRIORITY] = { .type = NETLINK_TYPE_U32 },
600 [RTA_PREFSRC] = { .type = NETLINK_TYPE_IN_ADDR }, /* 6? */
601 [RTA_METRICS] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_route_metrics_type_system},
602 /* [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
603 */
604 [RTA_FLOW] = { .type = NETLINK_TYPE_U32 }, /* 6? */
605 /*
606 RTA_CACHEINFO,
607 RTA_TABLE,
608 RTA_MARK,
609 RTA_MFC_STATS,
610 RTA_VIA,
611 RTA_NEWDST,
612 */
613 [RTA_PREF] = { .type = NETLINK_TYPE_U8 },
614 /*
615 RTA_ENCAP_TYPE,
616 RTA_ENCAP,
617 */
618 [RTA_EXPIRES] = { .type = NETLINK_TYPE_U32 },
619 };
620
621 static const NLTypeSystem rtnl_route_type_system = {
622 .count = ELEMENTSOF(rtnl_route_types),
623 .types = rtnl_route_types,
624 };
625
626 static const NLType rtnl_neigh_types[] = {
627 [NDA_DST] = { .type = NETLINK_TYPE_IN_ADDR },
628 [NDA_LLADDR] = { .type = NETLINK_TYPE_ETHER_ADDR },
629 [NDA_CACHEINFO] = { .type = NETLINK_TYPE_CACHE_INFO, .size = sizeof(struct nda_cacheinfo) },
630 [NDA_PROBES] = { .type = NETLINK_TYPE_U32 },
631 [NDA_VLAN] = { .type = NETLINK_TYPE_U16 },
632 [NDA_PORT] = { .type = NETLINK_TYPE_U16 },
633 [NDA_VNI] = { .type = NETLINK_TYPE_U32 },
634 [NDA_IFINDEX] = { .type = NETLINK_TYPE_U32 },
635 };
636
637 static const NLTypeSystem rtnl_neigh_type_system = {
638 .count = ELEMENTSOF(rtnl_neigh_types),
639 .types = rtnl_neigh_types,
640 };
641
642 static const NLType rtnl_addrlabel_types[] = {
643 [IFAL_ADDRESS] = { .type = NETLINK_TYPE_IN_ADDR, .size = sizeof(struct in6_addr) },
644 [IFAL_LABEL] = { .type = NETLINK_TYPE_U32 },
645 };
646
647 static const NLTypeSystem rtnl_addrlabel_type_system = {
648 .count = ELEMENTSOF(rtnl_addrlabel_types),
649 .types = rtnl_addrlabel_types,
650 };
651
652 static const NLType rtnl_routing_policy_rule_types[] = {
653 [FRA_DST] = { .type = NETLINK_TYPE_IN_ADDR },
654 [FRA_SRC] = { .type = NETLINK_TYPE_IN_ADDR },
655 [FRA_IIFNAME] = { .type = NETLINK_TYPE_STRING },
656 [FRA_GOTO] = { .type = NETLINK_TYPE_U32 },
657 [FRA_PRIORITY] = { .type = NETLINK_TYPE_U32 },
658 [FRA_FWMARK] = { .type = NETLINK_TYPE_U32 },
659 [FRA_FLOW] = { .type = NETLINK_TYPE_U32 },
660 [FRA_TUN_ID] = { .type = NETLINK_TYPE_U64 },
661 [FRA_SUPPRESS_IFGROUP] = { .type = NETLINK_TYPE_U32 },
662 [FRA_SUPPRESS_PREFIXLEN] = { .type = NETLINK_TYPE_U32 },
663 [FRA_TABLE] = { .type = NETLINK_TYPE_U32 },
664 [FRA_FWMASK] = { .type = NETLINK_TYPE_U32 },
665 [FRA_OIFNAME] = { .type = NETLINK_TYPE_STRING },
666 [FRA_PAD] = { .type = NETLINK_TYPE_U32 },
667 [FRA_L3MDEV] = { .type = NETLINK_TYPE_U8 },
668 [FRA_UID_RANGE] = { .size = sizeof(struct fib_rule_uid_range) },
669 [FRA_PROTOCOL] = { .type = NETLINK_TYPE_U8 },
670 [FRA_IP_PROTO] = { .type = NETLINK_TYPE_U8 },
671 [FRA_SPORT_RANGE] = { .size = sizeof(struct fib_rule_port_range) },
672 [FRA_DPORT_RANGE] = { .size = sizeof(struct fib_rule_port_range) },
673 };
674
675 static const NLTypeSystem rtnl_routing_policy_rule_type_system = {
676 .count = ELEMENTSOF(rtnl_routing_policy_rule_types),
677 .types = rtnl_routing_policy_rule_types,
678 };
679
680 static const NLType rtnl_types[] = {
681 [NLMSG_DONE] = { .type = NETLINK_TYPE_NESTED, .type_system = &empty_type_system, .size = 0 },
682 [NLMSG_ERROR] = { .type = NETLINK_TYPE_NESTED, .type_system = &empty_type_system, .size = sizeof(struct nlmsgerr) },
683 [RTM_NEWLINK] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
684 [RTM_DELLINK] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
685 [RTM_GETLINK] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
686 [RTM_SETLINK] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
687 [RTM_NEWADDR] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_address_type_system, .size = sizeof(struct ifaddrmsg) },
688 [RTM_DELADDR] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_address_type_system, .size = sizeof(struct ifaddrmsg) },
689 [RTM_GETADDR] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_address_type_system, .size = sizeof(struct ifaddrmsg) },
690 [RTM_NEWROUTE] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_route_type_system, .size = sizeof(struct rtmsg) },
691 [RTM_DELROUTE] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_route_type_system, .size = sizeof(struct rtmsg) },
692 [RTM_GETROUTE] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_route_type_system, .size = sizeof(struct rtmsg) },
693 [RTM_NEWNEIGH] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_neigh_type_system, .size = sizeof(struct ndmsg) },
694 [RTM_DELNEIGH] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_neigh_type_system, .size = sizeof(struct ndmsg) },
695 [RTM_GETNEIGH] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_neigh_type_system, .size = sizeof(struct ndmsg) },
696 [RTM_NEWADDRLABEL] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_addrlabel_type_system, .size = sizeof(struct ifaddrlblmsg) },
697 [RTM_DELADDRLABEL] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_addrlabel_type_system, .size = sizeof(struct ifaddrlblmsg) },
698 [RTM_GETADDRLABEL] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_addrlabel_type_system, .size = sizeof(struct ifaddrlblmsg) },
699 [RTM_NEWRULE] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_routing_policy_rule_type_system, .size = sizeof(struct rtmsg) },
700 [RTM_DELRULE] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_routing_policy_rule_type_system, .size = sizeof(struct rtmsg) },
701 [RTM_GETRULE] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_routing_policy_rule_type_system, .size = sizeof(struct rtmsg) },
702 };
703
704 const NLTypeSystem rtnl_type_system_root = {
705 .count = ELEMENTSOF(rtnl_types),
706 .types = rtnl_types,
707 };
708
709 static const NLType genl_wireguard_allowedip_types[] = {
710 [WGALLOWEDIP_A_FAMILY] = { .type = NETLINK_TYPE_U16 },
711 [WGALLOWEDIP_A_IPADDR] = { .type = NETLINK_TYPE_IN_ADDR },
712 [WGALLOWEDIP_A_CIDR_MASK] = { .type = NETLINK_TYPE_U8 },
713 };
714
715 static const NLTypeSystem genl_wireguard_allowedip_type_system = {
716 .count = ELEMENTSOF(genl_wireguard_allowedip_types),
717 .types = genl_wireguard_allowedip_types,
718 };
719
720 static const NLType genl_wireguard_peer_types[] = {
721 [WGPEER_A_PUBLIC_KEY] = { .size = WG_KEY_LEN },
722 [WGPEER_A_FLAGS] = { .type = NETLINK_TYPE_U32 },
723 [WGPEER_A_PRESHARED_KEY] = { .size = WG_KEY_LEN },
724 [WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL] = { .type = NETLINK_TYPE_U16 },
725 [WGPEER_A_ENDPOINT] = { .type = NETLINK_TYPE_SOCKADDR },
726 [WGPEER_A_ALLOWEDIPS] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_wireguard_allowedip_type_system },
727 };
728
729 static const NLTypeSystem genl_wireguard_peer_type_system = {
730 .count = ELEMENTSOF(genl_wireguard_peer_types),
731 .types = genl_wireguard_peer_types,
732 };
733
734 static const NLType genl_wireguard_set_device_types[] = {
735 [WGDEVICE_A_IFINDEX] = { .type = NETLINK_TYPE_U32 },
736 [WGDEVICE_A_IFNAME] = { .type = NETLINK_TYPE_STRING, .size = IFNAMSIZ-1 },
737 [WGDEVICE_A_FLAGS] = { .type = NETLINK_TYPE_U32 },
738 [WGDEVICE_A_PRIVATE_KEY] = { .size = WG_KEY_LEN },
739 [WGDEVICE_A_LISTEN_PORT] = { .type = NETLINK_TYPE_U16 },
740 [WGDEVICE_A_FWMARK] = { .type = NETLINK_TYPE_U32 },
741 [WGDEVICE_A_PEERS] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_wireguard_peer_type_system },
742 };
743
744 static const NLTypeSystem genl_wireguard_set_device_type_system = {
745 .count = ELEMENTSOF(genl_wireguard_set_device_types),
746 .types = genl_wireguard_set_device_types,
747 };
748
749 static const NLType genl_wireguard_cmds[] = {
750 [WG_CMD_SET_DEVICE] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_wireguard_set_device_type_system },
751 };
752
753 static const NLTypeSystem genl_wireguard_type_system = {
754 .count = ELEMENTSOF(genl_wireguard_cmds),
755 .types = genl_wireguard_cmds,
756 };
757
758 static const NLType genl_get_family_types[] = {
759 [CTRL_ATTR_FAMILY_NAME] = { .type = NETLINK_TYPE_STRING },
760 [CTRL_ATTR_FAMILY_ID] = { .type = NETLINK_TYPE_U16 },
761 };
762
763 static const NLTypeSystem genl_get_family_type_system = {
764 .count = ELEMENTSOF(genl_get_family_types),
765 .types = genl_get_family_types,
766 };
767
768 static const NLType genl_ctrl_id_ctrl_cmds[] = {
769 [CTRL_CMD_GETFAMILY] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_get_family_type_system },
770 };
771
772 static const NLTypeSystem genl_ctrl_id_ctrl_type_system = {
773 .count = ELEMENTSOF(genl_ctrl_id_ctrl_cmds),
774 .types = genl_ctrl_id_ctrl_cmds,
775 };
776
777 static const NLType genl_fou_types[] = {
778 [FOU_ATTR_PORT] = { .type = NETLINK_TYPE_U16 },
779 [FOU_ATTR_AF] = { .type = NETLINK_TYPE_U8 },
780 [FOU_ATTR_IPPROTO] = { .type = NETLINK_TYPE_U8 },
781 [FOU_ATTR_TYPE] = { .type = NETLINK_TYPE_U8 },
782 [FOU_ATTR_REMCSUM_NOPARTIAL] = { .type = NETLINK_TYPE_FLAG },
783 };
784
785 static const NLTypeSystem genl_fou_type_system = {
786 .count = ELEMENTSOF(genl_fou_types),
787 .types = genl_fou_types,
788 };
789
790 static const NLType genl_fou_cmds[] = {
791 [FOU_CMD_ADD] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_fou_type_system },
792 [FOU_CMD_DEL] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_fou_type_system },
793 [FOU_CMD_GET] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_fou_type_system },
794 };
795
796 static const NLTypeSystem genl_fou_cmds_type_system = {
797 .count = ELEMENTSOF(genl_fou_cmds),
798 .types = genl_fou_cmds,
799 };
800
801 static const NLType genl_l2tp_types[] = {
802 [L2TP_ATTR_PW_TYPE] = { .type = NETLINK_TYPE_U16 },
803 [L2TP_ATTR_ENCAP_TYPE] = { .type = NETLINK_TYPE_U16 },
804 [L2TP_ATTR_OFFSET] = { .type = NETLINK_TYPE_U16 },
805 [L2TP_ATTR_DATA_SEQ] = { .type = NETLINK_TYPE_U16 },
806 [L2TP_ATTR_L2SPEC_TYPE] = { .type = NETLINK_TYPE_U8 },
807 [L2TP_ATTR_L2SPEC_LEN] = { .type = NETLINK_TYPE_U8 },
808 [L2TP_ATTR_PROTO_VERSION] = { .type = NETLINK_TYPE_U8 },
809 [L2TP_ATTR_IFNAME] = { .type = NETLINK_TYPE_STRING },
810 [L2TP_ATTR_CONN_ID] = { .type = NETLINK_TYPE_U32 },
811 [L2TP_ATTR_PEER_CONN_ID] = { .type = NETLINK_TYPE_U32 },
812 [L2TP_ATTR_SESSION_ID] = { .type = NETLINK_TYPE_U32 },
813 [L2TP_ATTR_PEER_SESSION_ID] = { .type = NETLINK_TYPE_U32 },
814 [L2TP_ATTR_UDP_CSUM] = { .type = NETLINK_TYPE_U8 },
815 [L2TP_ATTR_VLAN_ID] = { .type = NETLINK_TYPE_U16 },
816 [L2TP_ATTR_RECV_SEQ] = { .type = NETLINK_TYPE_U8 },
817 [L2TP_ATTR_SEND_SEQ] = { .type = NETLINK_TYPE_U8 },
818 [L2TP_ATTR_LNS_MODE] = { .type = NETLINK_TYPE_U8 },
819 [L2TP_ATTR_USING_IPSEC] = { .type = NETLINK_TYPE_U8 },
820 [L2TP_ATTR_FD] = { .type = NETLINK_TYPE_U32 },
821 [L2TP_ATTR_IP_SADDR] = { .type = NETLINK_TYPE_IN_ADDR },
822 [L2TP_ATTR_IP_DADDR] = { .type = NETLINK_TYPE_IN_ADDR },
823 [L2TP_ATTR_UDP_SPORT] = { .type = NETLINK_TYPE_U16 },
824 [L2TP_ATTR_UDP_DPORT] = { .type = NETLINK_TYPE_U16 },
825 [L2TP_ATTR_IP6_SADDR] = { .type = NETLINK_TYPE_IN_ADDR },
826 [L2TP_ATTR_IP6_DADDR] = { .type = NETLINK_TYPE_IN_ADDR },
827 [L2TP_ATTR_UDP_ZERO_CSUM6_TX] = { .type = NETLINK_TYPE_FLAG },
828 [L2TP_ATTR_UDP_ZERO_CSUM6_RX] = { .type = NETLINK_TYPE_FLAG },
829 };
830
831 static const NLTypeSystem genl_l2tp_type_system = {
832 .count = ELEMENTSOF(genl_l2tp_types),
833 .types = genl_l2tp_types,
834 };
835
836 static const NLType genl_l2tp[] = {
837 [L2TP_CMD_TUNNEL_CREATE] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_l2tp_type_system },
838 [L2TP_CMD_TUNNEL_DELETE] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_l2tp_type_system },
839 [L2TP_CMD_TUNNEL_MODIFY] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_l2tp_type_system },
840 [L2TP_CMD_TUNNEL_GET] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_l2tp_type_system },
841 [L2TP_CMD_SESSION_CREATE] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_l2tp_type_system },
842 [L2TP_CMD_SESSION_DELETE] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_l2tp_type_system },
843 [L2TP_CMD_SESSION_MODIFY] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_l2tp_type_system },
844 [L2TP_CMD_SESSION_GET] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_l2tp_type_system },
845 };
846
847 static const NLTypeSystem genl_l2tp_tunnel_session_type_system = {
848 .count = ELEMENTSOF(genl_l2tp),
849 .types = genl_l2tp,
850 };
851
852 static const NLType genl_families[] = {
853 [SD_GENL_ID_CTRL] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_ctrl_id_ctrl_type_system },
854 [SD_GENL_WIREGUARD] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_wireguard_type_system },
855 [SD_GENL_FOU] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_fou_cmds_type_system},
856 [SD_GENL_L2TP] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_l2tp_tunnel_session_type_system },
857 };
858
859 const NLTypeSystem genl_family_type_system_root = {
860 .count = ELEMENTSOF(genl_families),
861 .types = genl_families,
862 };
863
864 static const NLType genl_types[] = {
865 [NLMSG_ERROR] = { .type = NETLINK_TYPE_NESTED, .type_system = &empty_type_system, .size = sizeof(struct nlmsgerr) },
866 [GENL_ID_CTRL] = { .type = NETLINK_TYPE_NESTED, .type_system = &genl_get_family_type_system, .size = sizeof(struct genlmsghdr) },
867 };
868
869 const NLTypeSystem genl_type_system_root = {
870 .count = ELEMENTSOF(genl_types),
871 .types = genl_types,
872 };
873
874 uint16_t type_get_type(const NLType *type) {
875 assert(type);
876 return type->type;
877 }
878
879 size_t type_get_size(const NLType *type) {
880 assert(type);
881 return type->size;
882 }
883
884 void type_get_type_system(const NLType *nl_type, const NLTypeSystem **ret) {
885 assert(nl_type);
886 assert(ret);
887 assert(nl_type->type == NETLINK_TYPE_NESTED);
888 assert(nl_type->type_system);
889
890 *ret = nl_type->type_system;
891 }
892
893 void type_get_type_system_union(const NLType *nl_type, const NLTypeSystemUnion **ret) {
894 assert(nl_type);
895 assert(ret);
896 assert(nl_type->type == NETLINK_TYPE_UNION);
897 assert(nl_type->type_system_union);
898
899 *ret = nl_type->type_system_union;
900 }
901
902 uint16_t type_system_get_count(const NLTypeSystem *type_system) {
903 assert(type_system);
904 return type_system->count;
905 }
906
907 const NLTypeSystem *type_system_get_root(int protocol) {
908 switch (protocol) {
909 case NETLINK_GENERIC:
910 return &genl_type_system_root;
911 default: /* NETLINK_ROUTE: */
912 return &rtnl_type_system_root;
913 }
914 }
915
916 int type_system_get_type(const NLTypeSystem *type_system, const NLType **ret, uint16_t type) {
917 const NLType *nl_type;
918
919 assert(ret);
920 assert(type_system);
921 assert(type_system->types);
922
923 if (type >= type_system->count)
924 return -EOPNOTSUPP;
925
926 nl_type = &type_system->types[type];
927
928 if (nl_type->type == NETLINK_TYPE_UNSPEC)
929 return -EOPNOTSUPP;
930
931 *ret = nl_type;
932
933 return 0;
934 }
935
936 int type_system_get_type_system(const NLTypeSystem *type_system, const NLTypeSystem **ret, uint16_t type) {
937 const NLType *nl_type;
938 int r;
939
940 assert(ret);
941
942 r = type_system_get_type(type_system, &nl_type, type);
943 if (r < 0)
944 return r;
945
946 type_get_type_system(nl_type, ret);
947 return 0;
948 }
949
950 int type_system_get_type_system_union(const NLTypeSystem *type_system, const NLTypeSystemUnion **ret, uint16_t type) {
951 const NLType *nl_type;
952 int r;
953
954 assert(ret);
955
956 r = type_system_get_type(type_system, &nl_type, type);
957 if (r < 0)
958 return r;
959
960 type_get_type_system_union(nl_type, ret);
961 return 0;
962 }
963
964 int type_system_union_get_type_system(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, const char *key) {
965 int type;
966
967 assert(type_system_union);
968 assert(type_system_union->match_type == NL_MATCH_SIBLING);
969 assert(type_system_union->lookup);
970 assert(type_system_union->type_systems);
971 assert(ret);
972 assert(key);
973
974 type = type_system_union->lookup(key);
975 if (type < 0)
976 return -EOPNOTSUPP;
977
978 assert(type < type_system_union->num);
979
980 *ret = &type_system_union->type_systems[type];
981
982 return 0;
983 }
984
985 int type_system_union_protocol_get_type_system(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, uint16_t protocol) {
986 const NLTypeSystem *type_system;
987
988 assert(type_system_union);
989 assert(type_system_union->type_systems);
990 assert(type_system_union->match_type == NL_MATCH_PROTOCOL);
991 assert(ret);
992
993 if (protocol >= type_system_union->num)
994 return -EOPNOTSUPP;
995
996 type_system = &type_system_union->type_systems[protocol];
997 if (!type_system->types)
998 return -EOPNOTSUPP;
999
1000 *ret = type_system;
1001
1002 return 0;
1003 }