1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Tom Gundersen <teg@jklm.no>
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.
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.
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/>.
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>
34 #include <linux/if_tunnel.h>
39 #include "rtnl-types.h"
42 static const NLTypeSystem rtnl_link_type_system;
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) },
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 },
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 },
58 [IFLA_BRIDGE_VLAN_INFO] = { .type = NLA_BINARY,
59 .len = sizeof(struct bridge_vlan_info), },
63 static const NLType rtnl_link_info_data_vlan_types[IFLA_VLAN_MAX + 1] = {
64 [IFLA_VLAN_ID] = { .type = NLA_U16 },
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 },
70 [IFLA_VLAN_PROTOCOL] = { .type = NLA_U16 },
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 },
83 [IFLA_BOND_ARP_IP_TARGET] = { .type = NLA_NESTED },
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 },
100 [IFLA_BOND_AD_INFO] = { .type = NLA_NESTED },
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 },
120 static const NLType rtnl_link_info_data_ipgre_types[IFLA_GRE_MAX + 1] = {
121 [IFLA_GRE_LINK] = { .type = NLA_U32 },
122 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
123 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
124 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
125 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
126 [IFLA_GRE_LOCAL] = { .type = NLA_IN_ADDR },
127 [IFLA_GRE_REMOTE] = { .type = NLA_IN_ADDR },
128 [IFLA_GRE_TTL] = { .type = NLA_U8 },
129 [IFLA_GRE_TOS] = { .type = NLA_U8 },
130 [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 },
133 static const NLType rtnl_link_info_data_ipvti_types[IFLA_VTI_MAX + 1] = {
134 [IFLA_VTI_LINK] = { .type = NLA_U32 },
135 [IFLA_VTI_IKEY] = { .type = NLA_U32 },
136 [IFLA_VTI_OKEY] = { .type = NLA_U32 },
137 [IFLA_VTI_LOCAL] = { .type = NLA_IN_ADDR },
138 [IFLA_VTI_REMOTE] = { .type = NLA_IN_ADDR },
141 typedef enum NLUnionLinkInfoData {
142 NL_UNION_LINK_INFO_DATA_BOND,
143 NL_UNION_LINK_INFO_DATA_BRIDGE,
144 NL_UNION_LINK_INFO_DATA_VLAN,
145 NL_UNION_LINK_INFO_DATA_VETH,
146 NL_UNION_LINK_INFO_DATA_MACVLAN,
147 NL_UNION_LINK_INFO_DATA_IPIP_TUNNEL,
148 NL_UNION_LINK_INFO_DATA_IPGRE_TUNNEL,
149 NL_UNION_LINK_INFO_DATA_SIT_TUNNEL,
150 NL_UNION_LINK_INFO_DATA_VTI_TUNNEL,
151 _NL_UNION_LINK_INFO_DATA_MAX,
152 _NL_UNION_LINK_INFO_DATA_INVALID = -1
153 } NLUnionLinkInfoData;
155 const char *nl_union_link_info_data_to_string(NLUnionLinkInfoData p) _const_;
156 NLUnionLinkInfoData nl_union_link_info_data_from_string(const char *p) _pure_;
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_MACVLAN] = "macvlan",
165 [NL_UNION_LINK_INFO_DATA_IPIP_TUNNEL] = "ipip",
166 [NL_UNION_LINK_INFO_DATA_IPGRE_TUNNEL] = "gre",
167 [NL_UNION_LINK_INFO_DATA_SIT_TUNNEL] = "sit",
168 [NL_UNION_LINK_INFO_DATA_VTI_TUNNEL] = "vti",
171 DEFINE_STRING_TABLE_LOOKUP(nl_union_link_info_data, NLUnionLinkInfoData);
173 static const NLTypeSystem rtnl_link_info_data_type_systems[_NL_UNION_LINK_INFO_DATA_MAX] = {
174 [NL_UNION_LINK_INFO_DATA_BOND] = { .max = ELEMENTSOF(rtnl_link_info_data_bond_types) - 1,
175 .types = rtnl_link_info_data_bond_types },
176 [NL_UNION_LINK_INFO_DATA_BRIDGE] = { .max = ELEMENTSOF(rtnl_link_info_data_bridge_types) - 1,
177 .types = rtnl_link_info_data_bridge_types },
178 [NL_UNION_LINK_INFO_DATA_VLAN] = { .max = ELEMENTSOF(rtnl_link_info_data_vlan_types) - 1,
179 .types = rtnl_link_info_data_vlan_types },
180 [NL_UNION_LINK_INFO_DATA_VETH] = { .max = ELEMENTSOF(rtnl_link_info_data_veth_types) - 1,
181 .types = rtnl_link_info_data_veth_types },
182 [NL_UNION_LINK_INFO_DATA_MACVLAN] = { .max = ELEMENTSOF(rtnl_link_info_data_macvlan_types) - 1,
183 .types = rtnl_link_info_data_macvlan_types },
184 [NL_UNION_LINK_INFO_DATA_IPIP_TUNNEL] = { .max = ELEMENTSOF(rtnl_link_info_data_iptun_types) - 1,
185 .types = rtnl_link_info_data_iptun_types },
186 [NL_UNION_LINK_INFO_DATA_IPGRE_TUNNEL] = { .max = ELEMENTSOF(rtnl_link_info_data_ipgre_types) - 1,
187 .types = rtnl_link_info_data_ipgre_types },
188 [NL_UNION_LINK_INFO_DATA_SIT_TUNNEL] = { .max = ELEMENTSOF(rtnl_link_info_data_iptun_types) - 1,
189 .types = rtnl_link_info_data_iptun_types },
190 [NL_UNION_LINK_INFO_DATA_VTI_TUNNEL] = { .max = ELEMENTSOF(rtnl_link_info_data_ipvti_types) - 1,
191 .types = rtnl_link_info_data_ipvti_types },
194 static const NLTypeSystemUnion rtnl_link_info_data_type_system_union = {
195 .num = _NL_UNION_LINK_INFO_DATA_MAX,
196 .lookup = nl_union_link_info_data_from_string,
197 .type_systems = rtnl_link_info_data_type_systems,
198 .match = IFLA_INFO_KIND,
201 static const NLType rtnl_link_info_types[IFLA_INFO_MAX + 1] = {
202 [IFLA_INFO_KIND] = { .type = NLA_STRING },
203 [IFLA_INFO_DATA] = { .type = NLA_UNION, .type_system_union = &rtnl_link_info_data_type_system_union},
206 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
207 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
211 static const NLTypeSystem rtnl_link_info_type_system = {
212 .max = ELEMENTSOF(rtnl_link_info_types) - 1,
213 .types = rtnl_link_info_types,
216 static const NLType rtnl_link_types[IFLA_MAX + 1] = {
217 [IFLA_ADDRESS] = { .type = NLA_ETHER_ADDR, },
218 [IFLA_BROADCAST] = { .type = NLA_ETHER_ADDR, },
219 [IFLA_IFNAME] = { .type = NLA_STRING, .size = IFNAMSIZ - 1, },
220 [IFLA_MTU] = { .type = NLA_U32 },
221 [IFLA_LINK] = { .type = NLA_U32 },
228 [IFLA_MASTER] = { .type = NLA_U32 },
233 [IFLA_TXQLEN] = { .type = NLA_U32 },
235 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
237 [IFLA_WEIGHT] = { .type = NLA_U32 },
238 [IFLA_OPERSTATE] = { .type = NLA_U8 },
239 [IFLA_LINKMODE] = { .type = NLA_U8 },
240 [IFLA_LINKINFO] = { .type = NLA_NESTED, .type_system = &rtnl_link_info_type_system },
241 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
242 [IFLA_IFALIAS] = { .type = NLA_STRING, .size = IFALIASZ - 1 },
245 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED, },
247 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
248 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
249 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
254 [IFLA_GROUP] = { .type = NLA_U32 },
255 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
256 [IFLA_EXT_MASK] = { .type = NLA_U32 },
257 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
258 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
259 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
260 [IFLA_CARRIER] = { .type = NLA_U8 },
262 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_PORT_ID_LEN },
266 static const NLTypeSystem rtnl_link_type_system = {
267 .max = ELEMENTSOF(rtnl_link_types) - 1,
268 .types = rtnl_link_types,
271 static const NLType rtnl_address_types[IFA_MAX + 1] = {
272 [IFA_ADDRESS] = { .type = NLA_IN_ADDR },
273 [IFA_LOCAL] = { .type = NLA_IN_ADDR },
274 [IFA_LABEL] = { .type = NLA_STRING, .size = IFNAMSIZ - 1 },
275 [IFA_BROADCAST] = { .type = NLA_IN_ADDR }, /* 6? */
276 [IFA_CACHEINFO] = { .type = NLA_CACHE_INFO, .size = sizeof(struct ifa_cacheinfo) },
282 [IFA_FLAGS] = { .type = NLA_U32 },
286 static const NLTypeSystem rtnl_address_type_system = {
287 .max = ELEMENTSOF(rtnl_address_types) - 1,
288 .types = rtnl_address_types,
291 static const NLType rtnl_route_types[RTA_MAX + 1] = {
292 [RTA_DST] = { .type = NLA_IN_ADDR }, /* 6? */
293 [RTA_SRC] = { .type = NLA_IN_ADDR }, /* 6? */
294 [RTA_IIF] = { .type = NLA_U32 },
295 [RTA_OIF] = { .type = NLA_U32 },
296 [RTA_GATEWAY] = { .type = NLA_IN_ADDR },
297 [RTA_PRIORITY] = { .type = NLA_U32 },
298 [RTA_PREFSRC] = { .type = NLA_IN_ADDR }, /* 6? */
300 [RTA_METRICS] = { .type = NLA_NESTED },
301 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
303 [RTA_FLOW] = { .type = NLA_U32 }, /* 6? */
312 static const NLTypeSystem rtnl_route_type_system = {
313 .max = ELEMENTSOF(rtnl_route_types) - 1,
314 .types = rtnl_route_types,
317 static const NLType rtnl_types[RTM_MAX + 1] = {
318 [NLMSG_ERROR] = { .type = NLA_META, .size = sizeof(struct nlmsgerr) },
319 [RTM_NEWLINK] = { .type = NLA_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
320 [RTM_DELLINK] = { .type = NLA_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
321 [RTM_GETLINK] = { .type = NLA_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
322 [RTM_SETLINK] = { .type = NLA_NESTED, .type_system = &rtnl_link_type_system, .size = sizeof(struct ifinfomsg) },
323 [RTM_NEWADDR] = { .type = NLA_NESTED, .type_system = &rtnl_address_type_system, .size = sizeof(struct ifaddrmsg) },
324 [RTM_DELADDR] = { .type = NLA_NESTED, .type_system = &rtnl_address_type_system, .size = sizeof(struct ifaddrmsg) },
325 [RTM_GETADDR] = { .type = NLA_NESTED, .type_system = &rtnl_address_type_system, .size = sizeof(struct ifaddrmsg) },
326 [RTM_NEWROUTE] = { .type = NLA_NESTED, .type_system = &rtnl_route_type_system, .size = sizeof(struct rtmsg) },
327 [RTM_DELROUTE] = { .type = NLA_NESTED, .type_system = &rtnl_route_type_system, .size = sizeof(struct rtmsg) },
328 [RTM_GETROUTE] = { .type = NLA_NESTED, .type_system = &rtnl_route_type_system, .size = sizeof(struct rtmsg) },
331 const NLTypeSystem rtnl_type_system = {
332 .max = ELEMENTSOF(rtnl_types) - 1,
336 int type_system_get_type(const NLTypeSystem *type_system, const NLType **ret, uint16_t type) {
337 const NLType *nl_type;
342 type_system = &rtnl_type_system;
344 assert(type_system->types);
346 if (type > type_system->max)
349 nl_type = &type_system->types[type];
351 if (nl_type->type == NLA_UNSPEC)
359 int type_system_get_type_system(const NLTypeSystem *type_system, const NLTypeSystem **ret, uint16_t type) {
360 const NLType *nl_type;
365 r = type_system_get_type(type_system, &nl_type, type);
369 assert_return(nl_type->type == NLA_NESTED, -EINVAL);
371 assert(nl_type->type_system);
373 *ret = nl_type->type_system;
378 int type_system_get_type_system_union(const NLTypeSystem *type_system, const NLTypeSystemUnion **ret, uint16_t type) {
379 const NLType *nl_type;
384 r = type_system_get_type(type_system, &nl_type, type);
388 assert_return(nl_type->type == NLA_UNION, -EINVAL);
390 assert(nl_type->type_system_union);
392 *ret = nl_type->type_system_union;
397 int type_system_union_get_type_system(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, const char *key) {
400 assert(type_system_union);
401 assert(type_system_union->lookup);
402 assert(type_system_union->type_systems);
406 type = type_system_union->lookup(key);
410 assert(type < type_system_union->num);
412 *ret = &type_system_union->type_systems[type];