chiark / gitweb /
sd-rtnl: rework rtnl type system
[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 "macro.h"
34 #include "util.h"
35
36 #include "rtnl-types.h"
37
38 static const NLTypeSystem rtnl_link_type_system;
39
40 static const NLType rtnl_link_info_data_veth_types[VETH_INFO_MAX + 1] = {
41         [VETH_INFO_PEER]  = { .type = NLA_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
42 };
43
44
45 static const NLType rtnl_link_info_data_macvlan_types[IFLA_MACVLAN_MAX + 1] = {
46         [IFLA_MACVLAN_MODE]  = { .type = NLA_U32 },
47         [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
48 };
49
50 static const NLType rtnl_link_info_data_bridge_types[IFLA_BRIDGE_MAX + 1] = {
51         [IFLA_BRIDGE_FLAGS]     = { .type = NLA_U16 },
52         [IFLA_BRIDGE_MODE]      = { .type = NLA_U16 },
53 /*
54         [IFLA_BRIDGE_VLAN_INFO] = { .type = NLA_BINARY,
55                                     .len = sizeof(struct bridge_vlan_info), },
56 */
57 };
58
59 static const NLType rtnl_link_info_data_vlan_types[IFLA_VLAN_MAX + 1] = {
60         [IFLA_VLAN_ID]          = { .type = NLA_U16 },
61 /*
62         [IFLA_VLAN_FLAGS]       = { .len = sizeof(struct ifla_vlan_flags) },
63         [IFLA_VLAN_EGRESS_QOS]  = { .type = NLA_NESTED },
64         [IFLA_VLAN_INGRESS_QOS] = { .type = NLA_NESTED },
65 */
66         [IFLA_VLAN_PROTOCOL]    = { .type = NLA_U16 },
67 };
68
69 static const NLType rtnl_link_info_data_bond_types[IFLA_BOND_MAX + 1] = {
70         [IFLA_BOND_MODE]                = { .type = NLA_U8 },
71         [IFLA_BOND_ACTIVE_SLAVE]        = { .type = NLA_U32 },
72 #ifdef IFLA_BOND_MIIMON
73         [IFLA_BOND_MIIMON]              = { .type = NLA_U32 },
74         [IFLA_BOND_UPDELAY]             = { .type = NLA_U32 },
75         [IFLA_BOND_DOWNDELAY]           = { .type = NLA_U32 },
76         [IFLA_BOND_USE_CARRIER]         = { .type = NLA_U8 },
77         [IFLA_BOND_ARP_INTERVAL]        = { .type = NLA_U32 },
78 /*
79         [IFLA_BOND_ARP_IP_TARGET]       = { .type = NLA_NESTED },
80 */
81         [IFLA_BOND_ARP_VALIDATE]        = { .type = NLA_U32 },
82         [IFLA_BOND_ARP_ALL_TARGETS]     = { .type = NLA_U32 },
83         [IFLA_BOND_PRIMARY]             = { .type = NLA_U32 },
84         [IFLA_BOND_PRIMARY_RESELECT]    = { .type = NLA_U8 },
85         [IFLA_BOND_FAIL_OVER_MAC]       = { .type = NLA_U8 },
86         [IFLA_BOND_XMIT_HASH_POLICY]    = { .type = NLA_U8 },
87         [IFLA_BOND_RESEND_IGMP]         = { .type = NLA_U32 },
88         [IFLA_BOND_NUM_PEER_NOTIF]      = { .type = NLA_U8 },
89         [IFLA_BOND_ALL_SLAVES_ACTIVE]   = { .type = NLA_U8 },
90         [IFLA_BOND_MIN_LINKS]           = { .type = NLA_U32 },
91         [IFLA_BOND_LP_INTERVAL]         = { .type = NLA_U32 },
92         [IFLA_BOND_PACKETS_PER_SLAVE]   = { .type = NLA_U32 },
93         [IFLA_BOND_AD_LACP_RATE]        = { .type = NLA_U8 },
94         [IFLA_BOND_AD_SELECT]           = { .type = NLA_U8 },
95 /*
96         [IFLA_BOND_AD_INFO]             = { .type = NLA_NESTED },
97 */
98 #endif
99 };
100
101 typedef enum NLUnionLinkInfoData {
102         NL_UNION_LINK_INFO_DATA_BOND,
103         NL_UNION_LINK_INFO_DATA_BRIDGE,
104         NL_UNION_LINK_INFO_DATA_VLAN,
105         NL_UNION_LINK_INFO_DATA_VETH,
106         NL_UNION_LINK_INFO_DATA_MACVLAN,
107         _NL_UNION_LINK_INFO_DATA_MAX,
108         _NL_UNION_LINK_INFO_DATA_INVALID = -1
109 } NLUnionLinkInfoData;
110
111 const char *nl_union_link_info_data_to_string(NLUnionLinkInfoData p) _const_;
112 NLUnionLinkInfoData nl_union_link_info_data_from_string(const char *p) _pure_;
113
114 static const char* const nl_union_link_info_data_table[_NL_UNION_LINK_INFO_DATA_MAX] = {
115         [NL_UNION_LINK_INFO_DATA_BOND] = "bond",
116         [NL_UNION_LINK_INFO_DATA_BRIDGE] = "bridge",
117         [NL_UNION_LINK_INFO_DATA_VLAN] = "vlan",
118         [NL_UNION_LINK_INFO_DATA_VETH] = "veth",
119         [NL_UNION_LINK_INFO_DATA_MACVLAN] = "macvlan",
120 };
121
122 DEFINE_STRING_TABLE_LOOKUP(nl_union_link_info_data, NLUnionLinkInfoData);
123
124 static const NLTypeSystem rtnl_link_info_data_type_systems[_NL_UNION_LINK_INFO_DATA_MAX] = {
125         [NL_UNION_LINK_INFO_DATA_BRIDGE] =  { .max = ELEMENTSOF(rtnl_link_info_data_bridge_types) - 1,
126                                               .types = rtnl_link_info_data_bridge_types },
127         [NL_UNION_LINK_INFO_DATA_VLAN] =    { .max = ELEMENTSOF(rtnl_link_info_data_vlan_types) - 1,
128                                               .types = rtnl_link_info_data_vlan_types },
129         [NL_UNION_LINK_INFO_DATA_VETH] =    { .max = ELEMENTSOF(rtnl_link_info_data_veth_types) - 1,
130                                               .types = rtnl_link_info_data_veth_types },
131         [NL_UNION_LINK_INFO_DATA_MACVLAN] = { .max = ELEMENTSOF(rtnl_link_info_data_macvlan_types) - 1,
132                                               .types = rtnl_link_info_data_macvlan_types },
133 };
134
135 static const NLTypeSystemUnion rtnl_link_info_data_type_system_union = {
136         .num = _NL_UNION_LINK_INFO_DATA_MAX,
137         .lookup = nl_union_link_info_data_from_string,
138         .type_systems = rtnl_link_info_data_type_systems,
139         .match = IFLA_INFO_KIND,
140 };
141
142 static const NLType rtnl_link_info_types[IFLA_INFO_MAX + 1] = {
143         [IFLA_INFO_KIND]        = { .type = NLA_STRING },
144         [IFLA_INFO_DATA]        = { .type = NLA_UNION, .type_system_union = &rtnl_link_info_data_type_system_union},
145 /*
146         [IFLA_INFO_XSTATS],
147         [IFLA_INFO_SLAVE_KIND]  = { .type = NLA_STRING },
148         [IFLA_INFO_SLAVE_DATA]  = { .type = NLA_NESTED },
149 */
150 };
151
152 static const NLTypeSystem rtnl_link_info_type_system = {
153         .max = ELEMENTSOF(rtnl_link_info_types) - 1,
154         .types = rtnl_link_info_types,
155 };
156
157 static const NLType rtnl_link_types[IFLA_MAX + 1] = {
158         [IFLA_ADDRESS]          = { .type = NLA_ETHER_ADDR, },
159         [IFLA_BROADCAST]        = { .type = NLA_ETHER_ADDR, },
160         [IFLA_IFNAME]           = { .type = NLA_STRING, .size = IFNAMSIZ - 1, },
161         [IFLA_MTU]              = { .type = NLA_U32 },
162         [IFLA_LINK]             = { .type = NLA_U32 },
163 /*
164         [IFLA_QDISC],
165         [IFLA_STATS],
166         [IFLA_COST],
167         [IFLA_PRIORITY],
168 */
169         [IFLA_MASTER]           = { .type = NLA_U32 },
170 /*
171         [IFLA_WIRELESS],
172         [IFLA_PROTINFO],
173 */
174         [IFLA_TXQLEN]           = { .type = NLA_U32 },
175 /*
176         [IFLA_MAP]              = { .len = sizeof(struct rtnl_link_ifmap) },
177 */
178         [IFLA_WEIGHT]           = { .type = NLA_U32 },
179         [IFLA_OPERSTATE]        = { .type = NLA_U8 },
180         [IFLA_LINKMODE]         = { .type = NLA_U8 },
181         [IFLA_LINKINFO]         = { .type = NLA_NESTED, .type_system = &rtnl_link_info_type_system },
182         [IFLA_NET_NS_PID]       = { .type = NLA_U32 },
183         [IFLA_IFALIAS]          = { .type = NLA_STRING, .size = IFALIASZ - 1 },
184 /*
185         [IFLA_NUM_VF],
186         [IFLA_VFINFO_LIST]      = {. type = NLA_NESTED, },
187         [IFLA_STATS64],
188         [IFLA_VF_PORTS]         = { .type = NLA_NESTED },
189         [IFLA_PORT_SELF]        = { .type = NLA_NESTED },
190         [IFLA_AF_SPEC]          = { .type = NLA_NESTED },
191         [IFLA_VF_PORTS],
192         [IFLA_PORT_SELF],
193         [IFLA_AF_SPEC],
194 */
195         [IFLA_GROUP]            = { .type = NLA_U32 },
196         [IFLA_NET_NS_FD]        = { .type = NLA_U32 },
197         [IFLA_EXT_MASK]         = { .type = NLA_U32 },
198         [IFLA_PROMISCUITY]      = { .type = NLA_U32 },
199         [IFLA_NUM_TX_QUEUES]    = { .type = NLA_U32 },
200         [IFLA_NUM_RX_QUEUES]    = { .type = NLA_U32 },
201         [IFLA_CARRIER]          = { .type = NLA_U8 },
202 /*
203         [IFLA_PHYS_PORT_ID]     = { .type = NLA_BINARY, .len = MAX_PHYS_PORT_ID_LEN },
204 */
205 };
206
207 static const NLTypeSystem rtnl_link_type_system = {
208         .max = ELEMENTSOF(rtnl_link_types) - 1,
209         .types = rtnl_link_types,
210 };
211
212 static const NLType rtnl_address_types[IFA_MAX + 1] = {
213         [IFA_ADDRESS]           = { .type = NLA_IN_ADDR },
214         [IFA_LOCAL]             = { .type = NLA_IN_ADDR },
215         [IFA_LABEL]             = { .type = NLA_STRING, .size = IFNAMSIZ - 1 },
216         [IFA_BROADCAST]         = { .type = NLA_IN_ADDR }, /* 6? */
217 /*
218         [IFA_ANYCAST],
219         [IFA_CACHEINFO]         = { .len = sizeof(struct ifa_cacheinfo) },
220         [IFA_MULTICAST],
221 */
222 #ifdef IFA_FLAGS
223         [IFA_FLAGS]             = { .type = NLA_U32 },
224 #endif
225 };
226
227 static const NLTypeSystem rtnl_address_type_system = {
228         .max = ELEMENTSOF(rtnl_address_types) - 1,
229         .types = rtnl_address_types,
230 };
231
232 static const NLType rtnl_route_types[RTA_MAX + 1] = {
233         [RTA_DST]               = { .type = NLA_IN_ADDR }, /* 6? */
234         [RTA_SRC]               = { .type = NLA_IN_ADDR }, /* 6? */
235         [RTA_IIF]               = { .type = NLA_U32 },
236         [RTA_OIF]               = { .type = NLA_U32 },
237         [RTA_GATEWAY]           = { .type = NLA_IN_ADDR },
238         [RTA_PRIORITY]          = { .type = NLA_U32 },
239         [RTA_PREFSRC]           = { .type = NLA_IN_ADDR }, /* 6? */
240 /*
241         [RTA_METRICS]           = { .type = NLA_NESTED },
242         [RTA_MULTIPATH]         = { .len = sizeof(struct rtnexthop) },
243 */
244         [RTA_FLOW]              = { .type = NLA_U32 }, /* 6? */
245 /*
246         RTA_CACHEINFO,
247         RTA_TABLE,
248         RTA_MARK,
249         RTA_MFC_STATS,
250 */
251 };
252
253 static const NLTypeSystem rtnl_route_type_system = {
254         .max = ELEMENTSOF(rtnl_route_types) - 1,
255         .types = rtnl_route_types,
256 };
257
258 static const NLType rtnl_types[RTM_MAX + 1] = {
259         [NLMSG_ERROR]  = { .type = NLA_META, .size = sizeof(struct nlmsgerr) },
260         [RTM_NEWLINK]  = { .type = NLA_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
261         [RTM_DELLINK]  = { .type = NLA_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
262         [RTM_GETLINK]  = { .type = NLA_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
263         [RTM_SETLINK]  = { .type = NLA_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
264         [RTM_NEWADDR]  = { .type = NLA_NESTED, .type_system = &rtnl_address_type_system, .size = sizeof(struct ifaddrmsg) },
265         [RTM_DELADDR]  = { .type = NLA_NESTED, .type_system = &rtnl_address_type_system, .size = sizeof(struct ifaddrmsg) },
266         [RTM_GETADDR]  = { .type = NLA_NESTED, .type_system = &rtnl_address_type_system, .size = sizeof(struct ifaddrmsg) },
267         [RTM_NEWROUTE] = { .type = NLA_NESTED, .type_system = &rtnl_route_type_system, .size = sizeof(struct rtmsg) },
268         [RTM_DELROUTE] = { .type = NLA_NESTED, .type_system = &rtnl_route_type_system, .size = sizeof(struct rtmsg) },
269         [RTM_GETROUTE] = { .type = NLA_NESTED, .type_system = &rtnl_route_type_system, .size = sizeof(struct rtmsg) },
270 };
271
272 const NLTypeSystem rtnl_type_system = {
273         .max = ELEMENTSOF(rtnl_types) - 1,
274         .types = rtnl_types,
275 };
276
277 int type_system_get_type(const NLTypeSystem *type_system, const NLType **ret, uint16_t type) {
278         const NLType *nl_type;
279
280         assert(ret);
281
282         if (!type_system)
283                 type_system = &rtnl_type_system;
284
285         assert(type_system->types);
286
287         if (type > type_system->max)
288                 return -ENOTSUP;
289
290         nl_type = &type_system->types[type];
291
292         if (nl_type->type == NLA_UNSPEC)
293                 return -ENOTSUP;
294
295         *ret = nl_type;
296
297         return 0;
298 }
299
300 int type_system_get_type_system(const NLTypeSystem *type_system, const NLTypeSystem **ret, uint16_t type) {
301         const NLType *nl_type;
302         int r;
303
304         assert(ret);
305
306         r = type_system_get_type(type_system, &nl_type, type);
307         if (r < 0)
308                 return r;
309
310         assert_return(nl_type->type == NLA_NESTED, -EINVAL);
311
312         assert(nl_type->type_system);
313
314         *ret = nl_type->type_system;
315
316         return 0;
317 }
318
319 int type_system_get_type_system_union(const NLTypeSystem *type_system, const NLTypeSystemUnion **ret, uint16_t type) {
320         const NLType *nl_type;
321         int r;
322
323         assert(ret);
324
325         r = type_system_get_type(type_system, &nl_type, type);
326         if (r < 0)
327                 return r;
328
329         assert_return(nl_type->type == NLA_UNION, -EINVAL);
330
331         assert(nl_type->type_system_union);
332
333         *ret = nl_type->type_system_union;
334
335         return 0;
336 }
337
338 int type_system_union_get_type_system(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, const char *key) {
339         int type;
340
341         assert(type_system_union);
342         assert(type_system_union->lookup);
343         assert(type_system_union->type_systems);
344         assert(ret);
345         assert(key);
346
347         type = type_system_union->lookup(key);
348         if (type < 0)
349                 return -ENOTSUP;
350
351         assert(type < type_system_union->num);
352
353         *ret = &type_system_union->type_systems[type];
354
355         return 0;
356 }