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