chiark / gitweb /
sd-netlink: make NLTypeSystem internal
[elogind.git] / src / libsystemd / sd-netlink / netlink-types.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2014 Tom Gundersen <teg@jklm.no>
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdint.h>
23 #include <sys/socket.h>
24 #include <linux/netlink.h>
25 #include <linux/rtnetlink.h>
26 #include <linux/in6.h>
27 #include <linux/veth.h>
28 #include <linux/if_bridge.h>
29 #include <linux/if_addr.h>
30 #include <linux/if.h>
31
32 #include <linux/ip.h>
33 #include <linux/if_link.h>
34 #include <linux/if_tunnel.h>
35
36 #include "macro.h"
37 #include "util.h"
38
39 #include "netlink-types.h"
40 #include "missing.h"
41
42 struct NLType {
43         uint16_t type;
44         size_t size;
45         const NLTypeSystem *type_system;
46         const NLTypeSystemUnion *type_system_union;
47 };
48
49 struct NLTypeSystem {
50         uint16_t max;
51         const NLType *types;
52 };
53
54 static const NLTypeSystem rtnl_link_type_system;
55
56 static const NLType rtnl_link_info_data_veth_types[VETH_INFO_MAX + 1] = {
57         [VETH_INFO_PEER]  = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
58 };
59
60 static const NLType rtnl_link_info_data_ipvlan_types[IFLA_IPVLAN_MAX + 1] = {
61         [IFLA_IPVLAN_MODE]  = { .type = NETLINK_TYPE_U16 },
62 };
63
64 static const NLType rtnl_link_info_data_macvlan_types[IFLA_MACVLAN_MAX + 1] = {
65         [IFLA_MACVLAN_MODE]  = { .type = NETLINK_TYPE_U32 },
66         [IFLA_MACVLAN_FLAGS] = { .type = NETLINK_TYPE_U16 },
67 };
68
69 static const NLType rtnl_link_info_data_bridge_types[IFLA_BRIDGE_MAX + 1] = {
70         [IFLA_BRIDGE_FLAGS]     = { .type = NETLINK_TYPE_U16 },
71         [IFLA_BRIDGE_MODE]      = { .type = NETLINK_TYPE_U16 },
72 /*
73         [IFLA_BRIDGE_VLAN_INFO] = { .type = NETLINK_TYPE_BINARY,
74                                     .len = sizeof(struct bridge_vlan_info), },
75 */
76 };
77
78 static const NLType rtnl_link_info_data_vlan_types[IFLA_VLAN_MAX + 1] = {
79         [IFLA_VLAN_ID]          = { .type = NETLINK_TYPE_U16 },
80 /*
81         [IFLA_VLAN_FLAGS]       = { .len = sizeof(struct ifla_vlan_flags) },
82         [IFLA_VLAN_EGRESS_QOS]  = { .type = NETLINK_TYPE_NESTED },
83         [IFLA_VLAN_INGRESS_QOS] = { .type = NETLINK_TYPE_NESTED },
84 */
85         [IFLA_VLAN_PROTOCOL]    = { .type = NETLINK_TYPE_U16 },
86 };
87
88 static const NLType rtnl_link_info_data_vxlan_types[IFLA_VXLAN_MAX+1] = {
89         [IFLA_VXLAN_ID]         = { .type = NETLINK_TYPE_U32 },
90         [IFLA_VXLAN_GROUP]      = { .type = NETLINK_TYPE_IN_ADDR },
91         [IFLA_VXLAN_LINK]       = { .type = NETLINK_TYPE_U32 },
92         [IFLA_VXLAN_LOCAL]      = { .type = NETLINK_TYPE_U32},
93         [IFLA_VXLAN_TTL]        = { .type = NETLINK_TYPE_U8 },
94         [IFLA_VXLAN_TOS]        = { .type = NETLINK_TYPE_U8 },
95         [IFLA_VXLAN_LEARNING]   = { .type = NETLINK_TYPE_U8 },
96         [IFLA_VXLAN_AGEING]     = { .type = NETLINK_TYPE_U32 },
97         [IFLA_VXLAN_LIMIT]      = { .type = NETLINK_TYPE_U32 },
98         [IFLA_VXLAN_PORT_RANGE] = { .type = NETLINK_TYPE_U32},
99         [IFLA_VXLAN_PROXY]      = { .type = NETLINK_TYPE_U8 },
100         [IFLA_VXLAN_RSC]        = { .type = NETLINK_TYPE_U8 },
101         [IFLA_VXLAN_L2MISS]     = { .type = NETLINK_TYPE_U8 },
102         [IFLA_VXLAN_L3MISS]     = { .type = NETLINK_TYPE_U8 },
103 };
104
105 static const NLType rtnl_bond_arp_target_types[BOND_ARP_TARGETS_MAX + 1] = {
106         [BOND_ARP_TARGETS_0]        = { .type = NETLINK_TYPE_U32 },
107         [BOND_ARP_TARGETS_1]        = { .type = NETLINK_TYPE_U32 },
108         [BOND_ARP_TARGETS_2]        = { .type = NETLINK_TYPE_U32 },
109         [BOND_ARP_TARGETS_3]        = { .type = NETLINK_TYPE_U32 },
110         [BOND_ARP_TARGETS_4]        = { .type = NETLINK_TYPE_U32 },
111         [BOND_ARP_TARGETS_5]        = { .type = NETLINK_TYPE_U32 },
112         [BOND_ARP_TARGETS_6]        = { .type = NETLINK_TYPE_U32 },
113         [BOND_ARP_TARGETS_7]        = { .type = NETLINK_TYPE_U32 },
114         [BOND_ARP_TARGETS_8]        = { .type = NETLINK_TYPE_U32 },
115         [BOND_ARP_TARGETS_9]        = { .type = NETLINK_TYPE_U32 },
116         [BOND_ARP_TARGETS_10]       = { .type = NETLINK_TYPE_U32 },
117         [BOND_ARP_TARGETS_11]       = { .type = NETLINK_TYPE_U32 },
118         [BOND_ARP_TARGETS_12]       = { .type = NETLINK_TYPE_U32 },
119         [BOND_ARP_TARGETS_13]       = { .type = NETLINK_TYPE_U32 },
120         [BOND_ARP_TARGETS_14]       = { .type = NETLINK_TYPE_U32 },
121         [BOND_ARP_TARGETS_MAX]      = { .type = NETLINK_TYPE_U32 },
122 };
123
124 static const NLTypeSystem rtnl_bond_arp_type_system = {
125         .max = ELEMENTSOF(rtnl_bond_arp_target_types) - 1,
126         .types = rtnl_bond_arp_target_types,
127 };
128
129 static const NLType rtnl_link_info_data_bond_types[IFLA_BOND_MAX + 1] = {
130         [IFLA_BOND_MODE]                = { .type = NETLINK_TYPE_U8 },
131         [IFLA_BOND_ACTIVE_SLAVE]        = { .type = NETLINK_TYPE_U32 },
132         [IFLA_BOND_MIIMON]              = { .type = NETLINK_TYPE_U32 },
133         [IFLA_BOND_UPDELAY]             = { .type = NETLINK_TYPE_U32 },
134         [IFLA_BOND_DOWNDELAY]           = { .type = NETLINK_TYPE_U32 },
135         [IFLA_BOND_USE_CARRIER]         = { .type = NETLINK_TYPE_U8 },
136         [IFLA_BOND_ARP_INTERVAL]        = { .type = NETLINK_TYPE_U32 },
137         [IFLA_BOND_ARP_IP_TARGET]       = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_bond_arp_type_system },
138         [IFLA_BOND_ARP_VALIDATE]        = { .type = NETLINK_TYPE_U32 },
139         [IFLA_BOND_ARP_ALL_TARGETS]     = { .type = NETLINK_TYPE_U32 },
140         [IFLA_BOND_PRIMARY]             = { .type = NETLINK_TYPE_U32 },
141         [IFLA_BOND_PRIMARY_RESELECT]    = { .type = NETLINK_TYPE_U8 },
142         [IFLA_BOND_FAIL_OVER_MAC]       = { .type = NETLINK_TYPE_U8 },
143         [IFLA_BOND_XMIT_HASH_POLICY]    = { .type = NETLINK_TYPE_U8 },
144         [IFLA_BOND_RESEND_IGMP]         = { .type = NETLINK_TYPE_U32 },
145         [IFLA_BOND_NUM_PEER_NOTIF]      = { .type = NETLINK_TYPE_U8 },
146         [IFLA_BOND_ALL_SLAVES_ACTIVE]   = { .type = NETLINK_TYPE_U8 },
147         [IFLA_BOND_MIN_LINKS]           = { .type = NETLINK_TYPE_U32 },
148         [IFLA_BOND_LP_INTERVAL]         = { .type = NETLINK_TYPE_U32 },
149         [IFLA_BOND_PACKETS_PER_SLAVE]   = { .type = NETLINK_TYPE_U32 },
150         [IFLA_BOND_AD_LACP_RATE]        = { .type = NETLINK_TYPE_U8 },
151         [IFLA_BOND_AD_SELECT]           = { .type = NETLINK_TYPE_U8 },
152         [IFLA_BOND_AD_INFO]             = { .type = NETLINK_TYPE_NESTED },
153 };
154
155 static const NLType rtnl_link_info_data_iptun_types[IFLA_IPTUN_MAX + 1] = {
156         [IFLA_IPTUN_LINK]                = { .type = NETLINK_TYPE_U32 },
157         [IFLA_IPTUN_LOCAL]               = { .type = NETLINK_TYPE_IN_ADDR },
158         [IFLA_IPTUN_REMOTE]              = { .type = NETLINK_TYPE_IN_ADDR },
159         [IFLA_IPTUN_TTL]                 = { .type = NETLINK_TYPE_U8 },
160         [IFLA_IPTUN_TOS]                 = { .type = NETLINK_TYPE_U8 },
161         [IFLA_IPTUN_PMTUDISC]            = { .type = NETLINK_TYPE_U8 },
162         [IFLA_IPTUN_FLAGS]               = { .type = NETLINK_TYPE_U16 },
163         [IFLA_IPTUN_PROTO]               = { .type = NETLINK_TYPE_U8 },
164         [IFLA_IPTUN_6RD_PREFIX]          = { .type = NETLINK_TYPE_IN_ADDR },
165         [IFLA_IPTUN_6RD_RELAY_PREFIX]    = { .type = NETLINK_TYPE_U32 },
166         [IFLA_IPTUN_6RD_PREFIXLEN]       = { .type = NETLINK_TYPE_U16 },
167         [IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NETLINK_TYPE_U16 },
168 };
169
170 static  const NLType rtnl_link_info_data_ipgre_types[IFLA_GRE_MAX + 1] = {
171         [IFLA_GRE_LINK]     = { .type = NETLINK_TYPE_U32 },
172         [IFLA_GRE_IFLAGS]   = { .type = NETLINK_TYPE_U16 },
173         [IFLA_GRE_OFLAGS]   = { .type = NETLINK_TYPE_U16 },
174         [IFLA_GRE_IKEY]     = { .type = NETLINK_TYPE_U32 },
175         [IFLA_GRE_OKEY]     = { .type = NETLINK_TYPE_U32 },
176         [IFLA_GRE_LOCAL]    = { .type = NETLINK_TYPE_IN_ADDR },
177         [IFLA_GRE_REMOTE]   = { .type = NETLINK_TYPE_IN_ADDR },
178         [IFLA_GRE_TTL]      = { .type = NETLINK_TYPE_U8 },
179         [IFLA_GRE_TOS]      = { .type = NETLINK_TYPE_U8 },
180         [IFLA_GRE_PMTUDISC] = { .type = NETLINK_TYPE_U8 },
181 };
182
183 static const NLType rtnl_link_info_data_ipvti_types[IFLA_VTI_MAX + 1] = {
184         [IFLA_VTI_LINK]         = { .type = NETLINK_TYPE_U32 },
185         [IFLA_VTI_IKEY]         = { .type = NETLINK_TYPE_U32 },
186         [IFLA_VTI_OKEY]         = { .type = NETLINK_TYPE_U32 },
187         [IFLA_VTI_LOCAL]        = { .type = NETLINK_TYPE_IN_ADDR  },
188         [IFLA_VTI_REMOTE]       = { .type = NETLINK_TYPE_IN_ADDR  },
189 };
190
191 static const NLType rtnl_link_info_data_ip6tnl_types[IFLA_IPTUN_MAX + 1] = {
192         [IFLA_IPTUN_LINK]                = { .type = NETLINK_TYPE_U32 },
193         [IFLA_IPTUN_LOCAL]               = { .type = NETLINK_TYPE_IN_ADDR },
194         [IFLA_IPTUN_REMOTE]              = { .type = NETLINK_TYPE_IN_ADDR },
195         [IFLA_IPTUN_TTL]                 = { .type = NETLINK_TYPE_U8 },
196         [IFLA_IPTUN_FLAGS]               = { .type = NETLINK_TYPE_U32 },
197         [IFLA_IPTUN_PROTO]               = { .type = NETLINK_TYPE_U8 },
198         [IFLA_IPTUN_ENCAP_LIMIT]         = { .type = NETLINK_TYPE_U8 },
199         [IFLA_IPTUN_FLOWINFO]            = { .type = NETLINK_TYPE_U32},
200 };
201
202 /* these strings must match the .kind entries in the kernel */
203 static const char* const nl_union_link_info_data_table[_NL_UNION_LINK_INFO_DATA_MAX] = {
204         [NL_UNION_LINK_INFO_DATA_BOND] = "bond",
205         [NL_UNION_LINK_INFO_DATA_BRIDGE] = "bridge",
206         [NL_UNION_LINK_INFO_DATA_VLAN] = "vlan",
207         [NL_UNION_LINK_INFO_DATA_VETH] = "veth",
208         [NL_UNION_LINK_INFO_DATA_DUMMY] = "dummy",
209         [NL_UNION_LINK_INFO_DATA_MACVLAN] = "macvlan",
210         [NL_UNION_LINK_INFO_DATA_IPVLAN] = "ipvlan",
211         [NL_UNION_LINK_INFO_DATA_VXLAN] = "vxlan",
212         [NL_UNION_LINK_INFO_DATA_IPIP_TUNNEL] = "ipip",
213         [NL_UNION_LINK_INFO_DATA_IPGRE_TUNNEL] = "gre",
214         [NL_UNION_LINK_INFO_DATA_IPGRETAP_TUNNEL] = "gretap",
215         [NL_UNION_LINK_INFO_DATA_IP6GRE_TUNNEL] = "ip6gre",
216         [NL_UNION_LINK_INFO_DATA_IP6GRETAP_TUNNEL] = "ip6gretap",
217         [NL_UNION_LINK_INFO_DATA_SIT_TUNNEL] = "sit",
218         [NL_UNION_LINK_INFO_DATA_VTI_TUNNEL] = "vti",
219         [NL_UNION_LINK_INFO_DATA_VTI6_TUNNEL] = "vti6",
220         [NL_UNION_LINK_INFO_DATA_IP6TNL_TUNNEL] = "ip6tnl",
221 };
222
223 DEFINE_STRING_TABLE_LOOKUP(nl_union_link_info_data, NLUnionLinkInfoData);
224
225 static const NLTypeSystem rtnl_link_info_data_type_systems[_NL_UNION_LINK_INFO_DATA_MAX] = {
226         [NL_UNION_LINK_INFO_DATA_BOND] =        { .max = ELEMENTSOF(rtnl_link_info_data_bond_types) - 1,
227                                                   .types = rtnl_link_info_data_bond_types },
228         [NL_UNION_LINK_INFO_DATA_BRIDGE] =      { .max = ELEMENTSOF(rtnl_link_info_data_bridge_types) - 1,
229                                                   .types = rtnl_link_info_data_bridge_types },
230         [NL_UNION_LINK_INFO_DATA_VLAN] =        { .max = ELEMENTSOF(rtnl_link_info_data_vlan_types) - 1,
231                                                   .types = rtnl_link_info_data_vlan_types },
232         [NL_UNION_LINK_INFO_DATA_VETH] =        { .max = ELEMENTSOF(rtnl_link_info_data_veth_types) - 1,
233                                                   .types = rtnl_link_info_data_veth_types },
234         [NL_UNION_LINK_INFO_DATA_MACVLAN] =     { .max = ELEMENTSOF(rtnl_link_info_data_macvlan_types) - 1,
235                                                   .types = rtnl_link_info_data_macvlan_types },
236         [NL_UNION_LINK_INFO_DATA_IPVLAN] =      { .max = ELEMENTSOF(rtnl_link_info_data_ipvlan_types) - 1,
237                                                   .types = rtnl_link_info_data_ipvlan_types },
238         [NL_UNION_LINK_INFO_DATA_VXLAN] =       { .max = ELEMENTSOF(rtnl_link_info_data_vxlan_types) - 1,
239                                                   .types = rtnl_link_info_data_vxlan_types },
240         [NL_UNION_LINK_INFO_DATA_IPIP_TUNNEL] = { .max = ELEMENTSOF(rtnl_link_info_data_iptun_types) - 1,
241                                                   .types = rtnl_link_info_data_iptun_types },
242         [NL_UNION_LINK_INFO_DATA_IPGRE_TUNNEL] =  { .max = ELEMENTSOF(rtnl_link_info_data_ipgre_types) - 1,
243                                                     .types = rtnl_link_info_data_ipgre_types },
244         [NL_UNION_LINK_INFO_DATA_IPGRETAP_TUNNEL] =  { .max = ELEMENTSOF(rtnl_link_info_data_ipgre_types) - 1,
245                                                     .types = rtnl_link_info_data_ipgre_types },
246         [NL_UNION_LINK_INFO_DATA_IP6GRE_TUNNEL] =  { .max = ELEMENTSOF(rtnl_link_info_data_ipgre_types) - 1,
247                                                     .types = rtnl_link_info_data_ipgre_types },
248         [NL_UNION_LINK_INFO_DATA_IP6GRETAP_TUNNEL] =  { .max = ELEMENTSOF(rtnl_link_info_data_ipgre_types) - 1,
249                                                     .types = rtnl_link_info_data_ipgre_types },
250         [NL_UNION_LINK_INFO_DATA_SIT_TUNNEL] =  { .max = ELEMENTSOF(rtnl_link_info_data_iptun_types) - 1,
251                                                   .types = rtnl_link_info_data_iptun_types },
252         [NL_UNION_LINK_INFO_DATA_VTI_TUNNEL] =  { .max = ELEMENTSOF(rtnl_link_info_data_ipvti_types) - 1,
253                                                   .types = rtnl_link_info_data_ipvti_types },
254         [NL_UNION_LINK_INFO_DATA_VTI6_TUNNEL] =  { .max = ELEMENTSOF(rtnl_link_info_data_ipvti_types) - 1,
255                                                   .types = rtnl_link_info_data_ipvti_types },
256         [NL_UNION_LINK_INFO_DATA_IP6TNL_TUNNEL] =  { .max = ELEMENTSOF(rtnl_link_info_data_ip6tnl_types) - 1,
257                                                      .types = rtnl_link_info_data_ip6tnl_types },
258
259 };
260
261 static const NLTypeSystemUnion rtnl_link_info_data_type_system_union = {
262         .num = _NL_UNION_LINK_INFO_DATA_MAX,
263         .lookup = nl_union_link_info_data_from_string,
264         .type_systems = rtnl_link_info_data_type_systems,
265         .match_type = NL_MATCH_SIBLING,
266         .match = IFLA_INFO_KIND,
267 };
268
269 static const NLType rtnl_link_info_types[IFLA_INFO_MAX + 1] = {
270         [IFLA_INFO_KIND]        = { .type = NETLINK_TYPE_STRING },
271         [IFLA_INFO_DATA]        = { .type = NETLINK_TYPE_UNION, .type_system_union = &rtnl_link_info_data_type_system_union},
272 /*
273         [IFLA_INFO_XSTATS],
274         [IFLA_INFO_SLAVE_KIND]  = { .type = NETLINK_TYPE_STRING },
275         [IFLA_INFO_SLAVE_DATA]  = { .type = NETLINK_TYPE_NESTED },
276 */
277 };
278
279 static const NLTypeSystem rtnl_link_info_type_system = {
280         .max = ELEMENTSOF(rtnl_link_info_types) - 1,
281         .types = rtnl_link_info_types,
282 };
283
284 static const struct NLType rtnl_prot_info_bridge_port_types[IFLA_BRPORT_MAX + 1] = {
285         [IFLA_BRPORT_STATE]             = { .type = NETLINK_TYPE_U8 },
286         [IFLA_BRPORT_COST]              = { .type = NETLINK_TYPE_U32 },
287         [IFLA_BRPORT_PRIORITY]          = { .type = NETLINK_TYPE_U16 },
288         [IFLA_BRPORT_MODE]              = { .type = NETLINK_TYPE_U8 },
289         [IFLA_BRPORT_GUARD]             = { .type = NETLINK_TYPE_U8 },
290         [IFLA_BRPORT_PROTECT]           = { .type = NETLINK_TYPE_U8 },
291         [IFLA_BRPORT_LEARNING]          = { .type = NETLINK_TYPE_U8 },
292         [IFLA_BRPORT_UNICAST_FLOOD]     = { .type = NETLINK_TYPE_U8 },
293 };
294
295 static const NLTypeSystem rtnl_prot_info_type_systems[AF_MAX] = {
296         [AF_BRIDGE] =   { .max = ELEMENTSOF(rtnl_prot_info_bridge_port_types) - 1,
297                           .types = rtnl_prot_info_bridge_port_types },
298 };
299
300 static const NLTypeSystemUnion rtnl_prot_info_type_system_union = {
301         .num = AF_MAX,
302         .type_systems = rtnl_prot_info_type_systems,
303         .match_type = NL_MATCH_PROTOCOL,
304 };
305
306 static const struct NLType rtnl_af_spec_inet6_types[IFLA_INET6_MAX + 1] = {
307         [IFLA_INET6_FLAGS]              = { .type = NETLINK_TYPE_U32 },
308 /*
309         IFLA_INET6_CONF,
310         IFLA_INET6_STATS,
311         IFLA_INET6_MCAST,
312         IFLA_INET6_CACHEINFO,
313         IFLA_INET6_ICMP6STATS,
314 */
315         [IFLA_INET6_TOKEN]              = { .type = NETLINK_TYPE_IN_ADDR },
316         [IFLA_INET6_ADDR_GEN_MODE]      = { .type = NETLINK_TYPE_U8 },
317 };
318
319 static const NLTypeSystem rtnl_af_spec_inet6_type_system = {
320         .max = ELEMENTSOF(rtnl_af_spec_inet6_types) - 1,
321         .types = rtnl_af_spec_inet6_types,
322 };
323
324 static const NLType rtnl_af_spec_types[AF_MAX + 1] = {
325         [AF_INET6] =    { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_af_spec_inet6_type_system },
326 };
327
328 static const NLTypeSystem rtnl_af_spec_type_system = {
329         .max = ELEMENTSOF(rtnl_af_spec_types) - 1,
330         .types = rtnl_af_spec_types,
331 };
332
333 static const NLType rtnl_link_types[IFLA_MAX + 1 ] = {
334         [IFLA_ADDRESS]          = { .type = NETLINK_TYPE_ETHER_ADDR, },
335         [IFLA_BROADCAST]        = { .type = NETLINK_TYPE_ETHER_ADDR, },
336         [IFLA_IFNAME]           = { .type = NETLINK_TYPE_STRING, .size = IFNAMSIZ - 1, },
337         [IFLA_MTU]              = { .type = NETLINK_TYPE_U32 },
338         [IFLA_LINK]             = { .type = NETLINK_TYPE_U32 },
339 /*
340         [IFLA_QDISC],
341         [IFLA_STATS],
342         [IFLA_COST],
343         [IFLA_PRIORITY],
344 */
345         [IFLA_MASTER]           = { .type = NETLINK_TYPE_U32 },
346 /*
347         [IFLA_WIRELESS],
348 */
349         [IFLA_PROTINFO]         = { .type = NETLINK_TYPE_UNION, .type_system_union = &rtnl_prot_info_type_system_union },
350         [IFLA_TXQLEN]           = { .type = NETLINK_TYPE_U32 },
351 /*
352         [IFLA_MAP]              = { .len = sizeof(struct rtnl_link_ifmap) },
353 */
354         [IFLA_WEIGHT]           = { .type = NETLINK_TYPE_U32 },
355         [IFLA_OPERSTATE]        = { .type = NETLINK_TYPE_U8 },
356         [IFLA_LINKMODE]         = { .type = NETLINK_TYPE_U8 },
357         [IFLA_LINKINFO]         = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_link_info_type_system },
358         [IFLA_NET_NS_PID]       = { .type = NETLINK_TYPE_U32 },
359         [IFLA_IFALIAS]          = { .type = NETLINK_TYPE_STRING, .size = IFALIASZ - 1 },
360 /*
361         [IFLA_NUM_VF],
362         [IFLA_VFINFO_LIST]      = {. type = NETLINK_TYPE_NESTED, },
363         [IFLA_STATS64],
364         [IFLA_VF_PORTS]         = { .type = NETLINK_TYPE_NESTED },
365         [IFLA_PORT_SELF]        = { .type = NETLINK_TYPE_NESTED },
366 */
367         [IFLA_AF_SPEC]          = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_af_spec_type_system },
368 /*
369         [IFLA_VF_PORTS],
370         [IFLA_PORT_SELF],
371         [IFLA_AF_SPEC],
372 */
373         [IFLA_GROUP]            = { .type = NETLINK_TYPE_U32 },
374         [IFLA_NET_NS_FD]        = { .type = NETLINK_TYPE_U32 },
375         [IFLA_EXT_MASK]         = { .type = NETLINK_TYPE_U32 },
376         [IFLA_PROMISCUITY]      = { .type = NETLINK_TYPE_U32 },
377         [IFLA_NUM_TX_QUEUES]    = { .type = NETLINK_TYPE_U32 },
378         [IFLA_NUM_RX_QUEUES]    = { .type = NETLINK_TYPE_U32 },
379         [IFLA_CARRIER]          = { .type = NETLINK_TYPE_U8 },
380 /*
381         [IFLA_PHYS_PORT_ID]     = { .type = NETLINK_TYPE_BINARY, .len = MAX_PHYS_PORT_ID_LEN },
382 */
383 };
384
385 static const NLTypeSystem rtnl_link_type_system = {
386         .max = ELEMENTSOF(rtnl_link_types) - 1,
387         .types = rtnl_link_types,
388 };
389
390 /* IFA_FLAGS was defined in kernel 3.14, but we still support older
391  * kernels where IFA_MAX is lower. */
392 static const NLType rtnl_address_types[CONST_MAX(IFA_MAX, IFA_FLAGS) + 1] = {
393         [IFA_ADDRESS]           = { .type = NETLINK_TYPE_IN_ADDR },
394         [IFA_LOCAL]             = { .type = NETLINK_TYPE_IN_ADDR },
395         [IFA_LABEL]             = { .type = NETLINK_TYPE_STRING, .size = IFNAMSIZ - 1 },
396         [IFA_BROADCAST]         = { .type = NETLINK_TYPE_IN_ADDR }, /* 6? */
397         [IFA_CACHEINFO]         = { .type = NETLINK_TYPE_CACHE_INFO, .size = sizeof(struct ifa_cacheinfo) },
398 /*
399         [IFA_ANYCAST],
400         [IFA_MULTICAST],
401 */
402         [IFA_FLAGS]             = { .type = NETLINK_TYPE_U32 },
403 };
404
405 static const NLTypeSystem rtnl_address_type_system = {
406         .max = ELEMENTSOF(rtnl_address_types) - 1,
407         .types = rtnl_address_types,
408 };
409
410 static const NLType rtnl_route_types[RTA_MAX + 1] = {
411         [RTA_DST]               = { .type = NETLINK_TYPE_IN_ADDR }, /* 6? */
412         [RTA_SRC]               = { .type = NETLINK_TYPE_IN_ADDR }, /* 6? */
413         [RTA_IIF]               = { .type = NETLINK_TYPE_U32 },
414         [RTA_OIF]               = { .type = NETLINK_TYPE_U32 },
415         [RTA_GATEWAY]           = { .type = NETLINK_TYPE_IN_ADDR },
416         [RTA_PRIORITY]          = { .type = NETLINK_TYPE_U32 },
417         [RTA_PREFSRC]           = { .type = NETLINK_TYPE_IN_ADDR }, /* 6? */
418 /*
419         [RTA_METRICS]           = { .type = NETLINK_TYPE_NESTED },
420         [RTA_MULTIPATH]         = { .len = sizeof(struct rtnexthop) },
421 */
422         [RTA_FLOW]              = { .type = NETLINK_TYPE_U32 }, /* 6? */
423 /*
424         RTA_CACHEINFO,
425         RTA_TABLE,
426         RTA_MARK,
427         RTA_MFC_STATS,
428 */
429 };
430
431 static const NLTypeSystem rtnl_route_type_system = {
432         .max = ELEMENTSOF(rtnl_route_types) - 1,
433         .types = rtnl_route_types,
434 };
435
436 static const NLType rtnl_neigh_types[NDA_MAX + 1] = {
437         [NDA_DST]               = { .type = NETLINK_TYPE_IN_ADDR },
438         [NDA_LLADDR]            = { .type = NETLINK_TYPE_ETHER_ADDR },
439         [NDA_CACHEINFO]         = { .type = NETLINK_TYPE_CACHE_INFO, .size = sizeof(struct nda_cacheinfo) },
440         [NDA_PROBES]            = { .type = NETLINK_TYPE_U32 },
441         [NDA_VLAN]              = { .type = NETLINK_TYPE_U16 },
442         [NDA_PORT]              = { .type = NETLINK_TYPE_U16 },
443         [NDA_VNI]               = { .type = NETLINK_TYPE_U32 },
444         [NDA_IFINDEX]           = { .type = NETLINK_TYPE_U32 },
445 };
446
447 static const NLTypeSystem rtnl_neigh_type_system = {
448         .max = ELEMENTSOF(rtnl_neigh_types) - 1,
449         .types = rtnl_neigh_types,
450 };
451
452 static const NLType rtnl_types[RTM_MAX + 1] = {
453         [NLMSG_DONE]   = { .type = NETLINK_TYPE_META, .size = 0 },
454         [NLMSG_ERROR]  = { .type = NETLINK_TYPE_META, .size = sizeof(struct nlmsgerr) },
455         [RTM_NEWLINK]  = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
456         [RTM_DELLINK]  = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
457         [RTM_GETLINK]  = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
458         [RTM_SETLINK]  = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
459         [RTM_NEWADDR]  = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_address_type_system, .size = sizeof(struct ifaddrmsg) },
460         [RTM_DELADDR]  = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_address_type_system, .size = sizeof(struct ifaddrmsg) },
461         [RTM_GETADDR]  = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_address_type_system, .size = sizeof(struct ifaddrmsg) },
462         [RTM_NEWROUTE] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_route_type_system, .size = sizeof(struct rtmsg) },
463         [RTM_DELROUTE] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_route_type_system, .size = sizeof(struct rtmsg) },
464         [RTM_GETROUTE] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_route_type_system, .size = sizeof(struct rtmsg) },
465         [RTM_NEWNEIGH] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_neigh_type_system, .size = sizeof(struct ndmsg) },
466         [RTM_DELNEIGH] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_neigh_type_system, .size = sizeof(struct ndmsg) },
467         [RTM_GETNEIGH] = { .type = NETLINK_TYPE_NESTED, .type_system = &rtnl_neigh_type_system, .size = sizeof(struct ndmsg) },
468 };
469
470 const NLTypeSystem rtnl_type_system = {
471         .max = ELEMENTSOF(rtnl_types) - 1,
472         .types = rtnl_types,
473 };
474
475 uint16_t type_get_type(const NLType *type) {
476         assert(type);
477         return type->type;
478 }
479
480 size_t type_get_size(const NLType *type) {
481         assert(type);
482         return type->size;
483 }
484
485 void type_get_type_system(const NLType *nl_type, const NLTypeSystem **ret) {
486         assert(nl_type);
487         assert(ret);
488         assert(nl_type->type == NETLINK_TYPE_NESTED);
489         assert(nl_type->type_system);
490
491         *ret = nl_type->type_system;
492 }
493
494 void type_get_type_system_union(const NLType *nl_type, const NLTypeSystemUnion **ret) {
495         assert(nl_type);
496         assert(ret);
497         assert(nl_type->type == NETLINK_TYPE_UNION);
498         assert(nl_type->type_system_union);
499
500         *ret = nl_type->type_system_union;
501 }
502
503 uint16_t type_system_get_max(const NLTypeSystem *type_system) {
504         assert(type_system);
505         return type_system->max;
506 }
507
508 int type_system_get_type(const NLTypeSystem *type_system, const NLType **ret, uint16_t type) {
509         const NLType *nl_type;
510
511         assert(ret);
512
513         if (!type_system)
514                 type_system = &rtnl_type_system;
515
516         assert(type_system->types);
517
518         if (type > type_system->max)
519                 return -EOPNOTSUPP;
520
521         nl_type = &type_system->types[type];
522
523         if (nl_type->type == NETLINK_TYPE_UNSPEC)
524                 return -EOPNOTSUPP;
525
526         *ret = nl_type;
527
528         return 0;
529 }
530
531 int type_system_get_type_system(const NLTypeSystem *type_system, const NLTypeSystem **ret, uint16_t type) {
532         const NLType *nl_type;
533         int r;
534
535         assert(ret);
536
537         r = type_system_get_type(type_system, &nl_type, type);
538         if (r < 0)
539                 return r;
540
541         type_get_type_system(nl_type, ret);
542         return 0;
543 }
544
545 int type_system_get_type_system_union(const NLTypeSystem *type_system, const NLTypeSystemUnion **ret, uint16_t type) {
546         const NLType *nl_type;
547         int r;
548
549         assert(ret);
550
551         r = type_system_get_type(type_system, &nl_type, type);
552         if (r < 0)
553                 return r;
554
555         type_get_type_system_union(nl_type, ret);
556         return 0;
557 }
558
559 int type_system_union_get_type_system(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, const char *key) {
560         int type;
561
562         assert(type_system_union);
563         assert(type_system_union->match_type == NL_MATCH_SIBLING);
564         assert(type_system_union->lookup);
565         assert(type_system_union->type_systems);
566         assert(ret);
567         assert(key);
568
569         type = type_system_union->lookup(key);
570         if (type < 0)
571                 return -EOPNOTSUPP;
572
573         assert(type < type_system_union->num);
574
575         *ret = &type_system_union->type_systems[type];
576
577         return 0;
578 }
579
580 int type_system_union_protocol_get_type_system(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, uint16_t protocol) {
581         const NLTypeSystem *type_system;
582
583         assert(type_system_union);
584         assert(type_system_union->type_systems);
585         assert(type_system_union->match_type == NL_MATCH_PROTOCOL);
586         assert(ret);
587
588         if (protocol >= type_system_union->num)
589                 return -EOPNOTSUPP;
590
591         type_system = &type_system_union->type_systems[protocol];
592         if (type_system->max == 0)
593                 return -EOPNOTSUPP;
594
595         *ret = type_system;
596
597         return 0;
598 }