chiark / gitweb /
test-tables: add new entries
[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 <stddef.h>
23 #include <stdint.h>
24 #include <sys/socket.h>
25 #include <linux/netlink.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/in6.h>
28 #include <linux/veth.h>
29 #include <linux/if_bridge.h>
30 #include <linux/if_addr.h>
31 #include <linux/if.h>
32
33 #include <linux/ip.h>
34 #include <linux/if_tunnel.h>
35
36 #include "macro.h"
37 #include "util.h"
38
39 #include "rtnl-types.h"
40 #include "missing.h"
41
42 static const NLTypeSystem rtnl_link_type_system;
43
44 static const NLType rtnl_link_info_data_veth_types[VETH_INFO_MAX + 1] = {
45         [VETH_INFO_PEER]  = { .type = NLA_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
46 };
47
48
49 static const NLType rtnl_link_info_data_macvlan_types[IFLA_MACVLAN_MAX + 1] = {
50         [IFLA_MACVLAN_MODE]  = { .type = NLA_U32 },
51         [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
52 };
53
54 static const NLType rtnl_link_info_data_bridge_types[IFLA_BRIDGE_MAX + 1] = {
55         [IFLA_BRIDGE_FLAGS]     = { .type = NLA_U16 },
56         [IFLA_BRIDGE_MODE]      = { .type = NLA_U16 },
57 /*
58         [IFLA_BRIDGE_VLAN_INFO] = { .type = NLA_BINARY,
59                                     .len = sizeof(struct bridge_vlan_info), },
60 */
61 };
62
63 static const NLType rtnl_link_info_data_vlan_types[IFLA_VLAN_MAX + 1] = {
64         [IFLA_VLAN_ID]          = { .type = NLA_U16 },
65 /*
66         [IFLA_VLAN_FLAGS]       = { .len = sizeof(struct ifla_vlan_flags) },
67         [IFLA_VLAN_EGRESS_QOS]  = { .type = NLA_NESTED },
68         [IFLA_VLAN_INGRESS_QOS] = { .type = NLA_NESTED },
69 */
70         [IFLA_VLAN_PROTOCOL]    = { .type = NLA_U16 },
71 };
72
73 static const NLType rtnl_link_info_data_vxlan_types[IFLA_VXLAN_MAX+1] = {
74         [IFLA_VXLAN_ID] = { .type = NLA_U32 },
75         [IFLA_VXLAN_GROUP] = {.type = NLA_IN_ADDR },
76         [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
77         [IFLA_VXLAN_LOCAL] = { .type = NLA_U32},
78         [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
79         [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
80         [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
81         [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
82         [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
83         [IFLA_VXLAN_PORT_RANGE] = { .type = NLA_U32},
84         [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
85         [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
86         [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
87         [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
88 };
89
90 static const NLType rtnl_link_info_data_bond_types[IFLA_BOND_MAX + 1] = {
91         [IFLA_BOND_MODE]                = { .type = NLA_U8 },
92         [IFLA_BOND_ACTIVE_SLAVE]        = { .type = NLA_U32 },
93 #ifdef IFLA_BOND_MIIMON
94         [IFLA_BOND_MIIMON]              = { .type = NLA_U32 },
95         [IFLA_BOND_UPDELAY]             = { .type = NLA_U32 },
96         [IFLA_BOND_DOWNDELAY]           = { .type = NLA_U32 },
97         [IFLA_BOND_USE_CARRIER]         = { .type = NLA_U8 },
98         [IFLA_BOND_ARP_INTERVAL]        = { .type = NLA_U32 },
99 /*
100         [IFLA_BOND_ARP_IP_TARGET]       = { .type = NLA_NESTED },
101 */
102         [IFLA_BOND_ARP_VALIDATE]        = { .type = NLA_U32 },
103         [IFLA_BOND_ARP_ALL_TARGETS]     = { .type = NLA_U32 },
104         [IFLA_BOND_PRIMARY]             = { .type = NLA_U32 },
105         [IFLA_BOND_PRIMARY_RESELECT]    = { .type = NLA_U8 },
106         [IFLA_BOND_FAIL_OVER_MAC]       = { .type = NLA_U8 },
107         [IFLA_BOND_XMIT_HASH_POLICY]    = { .type = NLA_U8 },
108         [IFLA_BOND_RESEND_IGMP]         = { .type = NLA_U32 },
109         [IFLA_BOND_NUM_PEER_NOTIF]      = { .type = NLA_U8 },
110         [IFLA_BOND_ALL_SLAVES_ACTIVE]   = { .type = NLA_U8 },
111         [IFLA_BOND_MIN_LINKS]           = { .type = NLA_U32 },
112         [IFLA_BOND_LP_INTERVAL]         = { .type = NLA_U32 },
113         [IFLA_BOND_PACKETS_PER_SLAVE]   = { .type = NLA_U32 },
114         [IFLA_BOND_AD_LACP_RATE]        = { .type = NLA_U8 },
115         [IFLA_BOND_AD_SELECT]           = { .type = NLA_U8 },
116 /*
117         [IFLA_BOND_AD_INFO]             = { .type = NLA_NESTED },
118 */
119 #endif
120 };
121
122 static const NLType rtnl_link_info_data_iptun_types[IFLA_IPTUN_MAX + 1] = {
123         [IFLA_IPTUN_LINK]                = { .type = NLA_U32 },
124         [IFLA_IPTUN_LOCAL]               = { .type = NLA_IN_ADDR },
125         [IFLA_IPTUN_REMOTE]              = { .type = NLA_IN_ADDR },
126         [IFLA_IPTUN_TTL]                 = { .type = NLA_U8 },
127         [IFLA_IPTUN_TOS]                 = { .type = NLA_U8 },
128         [IFLA_IPTUN_PMTUDISC]            = { .type = NLA_U8 },
129         [IFLA_IPTUN_FLAGS]               = { .type = NLA_U16 },
130         [IFLA_IPTUN_PROTO]               = { .type = NLA_U8 },
131         [IFLA_IPTUN_6RD_PREFIX]          = { .type = NLA_IN_ADDR },
132         [IFLA_IPTUN_6RD_RELAY_PREFIX]    = { .type = NLA_U32 },
133         [IFLA_IPTUN_6RD_PREFIXLEN]       = { .type = NLA_U16 },
134         [IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NLA_U16 },
135 };
136
137 static  const NLType rtnl_link_info_data_ipgre_types[IFLA_GRE_MAX + 1] = {
138         [IFLA_GRE_LINK]     = { .type = NLA_U32 },
139         [IFLA_GRE_IFLAGS]   = { .type = NLA_U16 },
140         [IFLA_GRE_OFLAGS]   = { .type = NLA_U16 },
141         [IFLA_GRE_IKEY]     = { .type = NLA_U32 },
142         [IFLA_GRE_OKEY]     = { .type = NLA_U32 },
143         [IFLA_GRE_LOCAL]    = { .type = NLA_IN_ADDR },
144         [IFLA_GRE_REMOTE]   = { .type = NLA_IN_ADDR },
145         [IFLA_GRE_TTL]      = { .type = NLA_U8 },
146         [IFLA_GRE_TOS]      = { .type = NLA_U8 },
147         [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 },
148 };
149
150 static const NLType rtnl_link_info_data_ipvti_types[IFLA_VTI_MAX + 1] = {
151         [IFLA_VTI_LINK]         = { .type = NLA_U32 },
152         [IFLA_VTI_IKEY]         = { .type = NLA_U32 },
153         [IFLA_VTI_OKEY]         = { .type = NLA_U32 },
154         [IFLA_VTI_LOCAL]        = { .type = NLA_IN_ADDR  },
155         [IFLA_VTI_REMOTE]       = { .type = NLA_IN_ADDR  },
156 };
157
158 /* these strings must match the .kind entries in the kernel */
159 static const char* const nl_union_link_info_data_table[_NL_UNION_LINK_INFO_DATA_MAX] = {
160         [NL_UNION_LINK_INFO_DATA_BOND] = "bond",
161         [NL_UNION_LINK_INFO_DATA_BRIDGE] = "bridge",
162         [NL_UNION_LINK_INFO_DATA_VLAN] = "vlan",
163         [NL_UNION_LINK_INFO_DATA_VETH] = "veth",
164         [NL_UNION_LINK_INFO_DATA_DUMMY] = "dummy",
165         [NL_UNION_LINK_INFO_DATA_MACVLAN] = "macvlan",
166         [NL_UNION_LINK_INFO_DATA_VXLAN] = "vxlan",
167         [NL_UNION_LINK_INFO_DATA_IPIP_TUNNEL] = "ipip",
168         [NL_UNION_LINK_INFO_DATA_IPGRE_TUNNEL] = "gre",
169         [NL_UNION_LINK_INFO_DATA_SIT_TUNNEL] = "sit",
170         [NL_UNION_LINK_INFO_DATA_VTI_TUNNEL] = "vti",
171 };
172
173 DEFINE_STRING_TABLE_LOOKUP(nl_union_link_info_data, NLUnionLinkInfoData);
174
175 static const NLTypeSystem rtnl_link_info_data_type_systems[_NL_UNION_LINK_INFO_DATA_MAX] = {
176         [NL_UNION_LINK_INFO_DATA_BOND] =        { .max = ELEMENTSOF(rtnl_link_info_data_bond_types) - 1,
177                                                   .types = rtnl_link_info_data_bond_types },
178         [NL_UNION_LINK_INFO_DATA_BRIDGE] =      { .max = ELEMENTSOF(rtnl_link_info_data_bridge_types) - 1,
179                                                   .types = rtnl_link_info_data_bridge_types },
180         [NL_UNION_LINK_INFO_DATA_VLAN] =        { .max = ELEMENTSOF(rtnl_link_info_data_vlan_types) - 1,
181                                                   .types = rtnl_link_info_data_vlan_types },
182         [NL_UNION_LINK_INFO_DATA_VETH] =        { .max = ELEMENTSOF(rtnl_link_info_data_veth_types) - 1,
183                                                   .types = rtnl_link_info_data_veth_types },
184         [NL_UNION_LINK_INFO_DATA_MACVLAN] =     { .max = ELEMENTSOF(rtnl_link_info_data_macvlan_types) - 1,
185                                                   .types = rtnl_link_info_data_macvlan_types },
186         [NL_UNION_LINK_INFO_DATA_VXLAN] =       { .max = ELEMENTSOF(rtnl_link_info_data_vxlan_types) - 1,
187                                                   .types = rtnl_link_info_data_vxlan_types },
188         [NL_UNION_LINK_INFO_DATA_IPIP_TUNNEL] = { .max = ELEMENTSOF(rtnl_link_info_data_iptun_types) - 1,
189                                                   .types = rtnl_link_info_data_iptun_types },
190         [NL_UNION_LINK_INFO_DATA_IPGRE_TUNNEL] =  { .max = ELEMENTSOF(rtnl_link_info_data_ipgre_types) - 1,
191                                                     .types = rtnl_link_info_data_ipgre_types },
192         [NL_UNION_LINK_INFO_DATA_SIT_TUNNEL] =  { .max = ELEMENTSOF(rtnl_link_info_data_iptun_types) - 1,
193                                                   .types = rtnl_link_info_data_iptun_types },
194         [NL_UNION_LINK_INFO_DATA_VTI_TUNNEL] =  { .max = ELEMENTSOF(rtnl_link_info_data_ipvti_types) - 1,
195                                                   .types = rtnl_link_info_data_ipvti_types },
196 };
197
198 static const NLTypeSystemUnion rtnl_link_info_data_type_system_union = {
199         .num = _NL_UNION_LINK_INFO_DATA_MAX,
200         .lookup = nl_union_link_info_data_from_string,
201         .type_systems = rtnl_link_info_data_type_systems,
202         .match = IFLA_INFO_KIND,
203 };
204
205 static const NLType rtnl_link_info_types[IFLA_INFO_MAX + 1] = {
206         [IFLA_INFO_KIND]        = { .type = NLA_STRING },
207         [IFLA_INFO_DATA]        = { .type = NLA_UNION, .type_system_union = &rtnl_link_info_data_type_system_union},
208 /*
209         [IFLA_INFO_XSTATS],
210         [IFLA_INFO_SLAVE_KIND]  = { .type = NLA_STRING },
211         [IFLA_INFO_SLAVE_DATA]  = { .type = NLA_NESTED },
212 */
213 };
214
215 static const NLTypeSystem rtnl_link_info_type_system = {
216         .max = ELEMENTSOF(rtnl_link_info_types) - 1,
217         .types = rtnl_link_info_types,
218 };
219
220 static const NLType rtnl_link_types[IFLA_MAX + 1] = {
221         [IFLA_ADDRESS]          = { .type = NLA_ETHER_ADDR, },
222         [IFLA_BROADCAST]        = { .type = NLA_ETHER_ADDR, },
223         [IFLA_IFNAME]           = { .type = NLA_STRING, .size = IFNAMSIZ - 1, },
224         [IFLA_MTU]              = { .type = NLA_U32 },
225         [IFLA_LINK]             = { .type = NLA_U32 },
226 /*
227         [IFLA_QDISC],
228         [IFLA_STATS],
229         [IFLA_COST],
230         [IFLA_PRIORITY],
231 */
232         [IFLA_MASTER]           = { .type = NLA_U32 },
233 /*
234         [IFLA_WIRELESS],
235         [IFLA_PROTINFO],
236 */
237         [IFLA_TXQLEN]           = { .type = NLA_U32 },
238 /*
239         [IFLA_MAP]              = { .len = sizeof(struct rtnl_link_ifmap) },
240 */
241         [IFLA_WEIGHT]           = { .type = NLA_U32 },
242         [IFLA_OPERSTATE]        = { .type = NLA_U8 },
243         [IFLA_LINKMODE]         = { .type = NLA_U8 },
244         [IFLA_LINKINFO]         = { .type = NLA_NESTED, .type_system = &rtnl_link_info_type_system },
245         [IFLA_NET_NS_PID]       = { .type = NLA_U32 },
246         [IFLA_IFALIAS]          = { .type = NLA_STRING, .size = IFALIASZ - 1 },
247 /*
248         [IFLA_NUM_VF],
249         [IFLA_VFINFO_LIST]      = {. type = NLA_NESTED, },
250         [IFLA_STATS64],
251         [IFLA_VF_PORTS]         = { .type = NLA_NESTED },
252         [IFLA_PORT_SELF]        = { .type = NLA_NESTED },
253         [IFLA_AF_SPEC]          = { .type = NLA_NESTED },
254         [IFLA_VF_PORTS],
255         [IFLA_PORT_SELF],
256         [IFLA_AF_SPEC],
257 */
258         [IFLA_GROUP]            = { .type = NLA_U32 },
259         [IFLA_NET_NS_FD]        = { .type = NLA_U32 },
260         [IFLA_EXT_MASK]         = { .type = NLA_U32 },
261         [IFLA_PROMISCUITY]      = { .type = NLA_U32 },
262         [IFLA_NUM_TX_QUEUES]    = { .type = NLA_U32 },
263         [IFLA_NUM_RX_QUEUES]    = { .type = NLA_U32 },
264         [IFLA_CARRIER]          = { .type = NLA_U8 },
265 /*
266         [IFLA_PHYS_PORT_ID]     = { .type = NLA_BINARY, .len = MAX_PHYS_PORT_ID_LEN },
267 */
268 };
269
270 static const NLTypeSystem rtnl_link_type_system = {
271         .max = ELEMENTSOF(rtnl_link_types) - 1,
272         .types = rtnl_link_types,
273 };
274
275 static const NLType rtnl_address_types[IFA_MAX + 1] = {
276         [IFA_ADDRESS]           = { .type = NLA_IN_ADDR },
277         [IFA_LOCAL]             = { .type = NLA_IN_ADDR },
278         [IFA_LABEL]             = { .type = NLA_STRING, .size = IFNAMSIZ - 1 },
279         [IFA_BROADCAST]         = { .type = NLA_IN_ADDR }, /* 6? */
280         [IFA_CACHEINFO]         = { .type = NLA_CACHE_INFO, .size = sizeof(struct ifa_cacheinfo) },
281 /*
282         [IFA_ANYCAST],
283         [IFA_MULTICAST],
284 */
285 #ifdef IFA_FLAGS
286         [IFA_FLAGS]             = { .type = NLA_U32 },
287 #endif
288 };
289
290 static const NLTypeSystem rtnl_address_type_system = {
291         .max = ELEMENTSOF(rtnl_address_types) - 1,
292         .types = rtnl_address_types,
293 };
294
295 static const NLType rtnl_route_types[RTA_MAX + 1] = {
296         [RTA_DST]               = { .type = NLA_IN_ADDR }, /* 6? */
297         [RTA_SRC]               = { .type = NLA_IN_ADDR }, /* 6? */
298         [RTA_IIF]               = { .type = NLA_U32 },
299         [RTA_OIF]               = { .type = NLA_U32 },
300         [RTA_GATEWAY]           = { .type = NLA_IN_ADDR },
301         [RTA_PRIORITY]          = { .type = NLA_U32 },
302         [RTA_PREFSRC]           = { .type = NLA_IN_ADDR }, /* 6? */
303 /*
304         [RTA_METRICS]           = { .type = NLA_NESTED },
305         [RTA_MULTIPATH]         = { .len = sizeof(struct rtnexthop) },
306 */
307         [RTA_FLOW]              = { .type = NLA_U32 }, /* 6? */
308 /*
309         RTA_CACHEINFO,
310         RTA_TABLE,
311         RTA_MARK,
312         RTA_MFC_STATS,
313 */
314 };
315
316 static const NLTypeSystem rtnl_route_type_system = {
317         .max = ELEMENTSOF(rtnl_route_types) - 1,
318         .types = rtnl_route_types,
319 };
320
321 static const NLType rtnl_types[RTM_MAX + 1] = {
322         [NLMSG_ERROR]  = { .type = NLA_META, .size = sizeof(struct nlmsgerr) },
323         [RTM_NEWLINK]  = { .type = NLA_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
324         [RTM_DELLINK]  = { .type = NLA_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
325         [RTM_GETLINK]  = { .type = NLA_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
326         [RTM_SETLINK]  = { .type = NLA_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
327         [RTM_NEWADDR]  = { .type = NLA_NESTED, .type_system = &rtnl_address_type_system, .size = sizeof(struct ifaddrmsg) },
328         [RTM_DELADDR]  = { .type = NLA_NESTED, .type_system = &rtnl_address_type_system, .size = sizeof(struct ifaddrmsg) },
329         [RTM_GETADDR]  = { .type = NLA_NESTED, .type_system = &rtnl_address_type_system, .size = sizeof(struct ifaddrmsg) },
330         [RTM_NEWROUTE] = { .type = NLA_NESTED, .type_system = &rtnl_route_type_system, .size = sizeof(struct rtmsg) },
331         [RTM_DELROUTE] = { .type = NLA_NESTED, .type_system = &rtnl_route_type_system, .size = sizeof(struct rtmsg) },
332         [RTM_GETROUTE] = { .type = NLA_NESTED, .type_system = &rtnl_route_type_system, .size = sizeof(struct rtmsg) },
333 };
334
335 const NLTypeSystem rtnl_type_system = {
336         .max = ELEMENTSOF(rtnl_types) - 1,
337         .types = rtnl_types,
338 };
339
340 int type_system_get_type(const NLTypeSystem *type_system, const NLType **ret, uint16_t type) {
341         const NLType *nl_type;
342
343         assert(ret);
344
345         if (!type_system)
346                 type_system = &rtnl_type_system;
347
348         assert(type_system->types);
349
350         if (type > type_system->max)
351                 return -ENOTSUP;
352
353         nl_type = &type_system->types[type];
354
355         if (nl_type->type == NLA_UNSPEC)
356                 return -ENOTSUP;
357
358         *ret = nl_type;
359
360         return 0;
361 }
362
363 int type_system_get_type_system(const NLTypeSystem *type_system, const NLTypeSystem **ret, uint16_t type) {
364         const NLType *nl_type;
365         int r;
366
367         assert(ret);
368
369         r = type_system_get_type(type_system, &nl_type, type);
370         if (r < 0)
371                 return r;
372
373         assert_return(nl_type->type == NLA_NESTED, -EINVAL);
374
375         assert(nl_type->type_system);
376
377         *ret = nl_type->type_system;
378
379         return 0;
380 }
381
382 int type_system_get_type_system_union(const NLTypeSystem *type_system, const NLTypeSystemUnion **ret, uint16_t type) {
383         const NLType *nl_type;
384         int r;
385
386         assert(ret);
387
388         r = type_system_get_type(type_system, &nl_type, type);
389         if (r < 0)
390                 return r;
391
392         assert_return(nl_type->type == NLA_UNION, -EINVAL);
393
394         assert(nl_type->type_system_union);
395
396         *ret = nl_type->type_system_union;
397
398         return 0;
399 }
400
401 int type_system_union_get_type_system(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, const char *key) {
402         int type;
403
404         assert(type_system_union);
405         assert(type_system_union->lookup);
406         assert(type_system_union->type_systems);
407         assert(ret);
408         assert(key);
409
410         type = type_system_union->lookup(key);
411         if (type < 0)
412                 return -ENOTSUP;
413
414         assert(type < type_system_union->num);
415
416         *ret = &type_system_union->type_systems[type];
417
418         return 0;
419 }