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