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