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