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