libnl: /data/src/libnl3/lib/route/link/vrf.c Source File

libnl 3.7.0

vrf.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2015 Cumulus Networks. All rights reserved.
4 * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
5 */
6
7 /**
8 * @ingroup link
9 * @defgroup vrf VRF
10 * Virtual Routing and Forwarding link module
11 *
12 * @details
13 * \b Link Type Name: "vrf"
14 *
15 * @route_doc{link_vrf, VRF Documentation}
16 *
17 * @{
18 */
19
20 #include <netlink-private/netlink.h>
21 #include <netlink/netlink.h>
22 #include <netlink/attr.h>
23 #include <netlink/utils.h>
24 #include <netlink/object.h>
25 #include <netlink/route/rtnl.h>
26 #include <netlink-private/route/link/api.h>
27 #include <netlink/route/link/vrf.h>
28
29 #include <linux/if_link.h>
30 #include <linux-private/linux/rtnetlink.h>
31
32 #define VRF_TABLE_ID_MAX RT_TABLE_MAX
33
34 /** @cond SKIP */
35 #define VRF_HAS_TABLE_ID (1<<0)
36
37 struct vrf_info {
38 uint32_t table_id;
39 uint32_t vi_mask;
40 };
41
42 /** @endcond */
43
44 static struct nla_policy vrf_policy[IFLA_VRF_MAX + 1] = {
45 [IFLA_VRF_TABLE] = { .type = NLA_U32 },
46 };
47
48 static int vrf_alloc(struct rtnl_link *link)
49 {
50 struct vrf_info *vi;
51
52 if (link->l_info) {
53 memset(link->l_info, 0, sizeof (*vi));
54 return 0;
55 }
56
57 if ((vi = calloc(1, sizeof(*vi))) == NULL)
58 return -NLE_NOMEM;
59
60 link->l_info = vi;
61
62 return 0;
63 }
64
65 static int vrf_parse(struct rtnl_link *link, struct nlattr *data,
66 struct nlattr *xstats)
67 {
68 struct nlattr *tb[IFLA_VRF_MAX+1];
69 struct vrf_info *vi;
70 int err;
71
72 NL_DBG(3, "Parsing VRF link info");
73
74 if ((err = nla_parse_nested(tb, IFLA_VRF_MAX, data, vrf_policy)) < 0)
75 goto errout;
76
77 if ((err = vrf_alloc(link)) < 0)
78 goto errout;
79
80 vi = link->l_info;
81
82 if (tb[IFLA_VRF_TABLE]) {
83 vi->table_id = nla_get_u32(tb[IFLA_VRF_TABLE]);
84 vi->vi_mask |= VRF_HAS_TABLE_ID;
85 }
86
87 err = 0;
88
89 errout:
90 return err;
91 }
92
93 static void vrf_free(struct rtnl_link *link)
94 {
95 free(link->l_info);
96 link->l_info = NULL;
97 }
98
99 static int vrf_clone(struct rtnl_link *dst, struct rtnl_link *src)
100 {
101 struct vrf_info *vdst, *vsrc = src->l_info;
102 int err;
103
104 dst->l_info = NULL;
105 if ((err = rtnl_link_set_type(dst, "vrf")) < 0)
106 return err;
107 vdst = dst->l_info;
108
109 BUG_ON(!vdst || !vsrc);
110
111 memcpy(vdst, vsrc, sizeof(struct vrf_info));
112
113 return 0;
114 }
115
116 static int vrf_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
117 {
118 struct vrf_info *vi = link->l_info;
119 struct nlattr *data;
120
121 if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
122 return -NLE_NOMEM;
123
124 if (vi->vi_mask & VRF_HAS_TABLE_ID) {
125 NLA_PUT_U32(msg, IFLA_VRF_TABLE, vi->table_id);
126 }
127
128 nla_nest_end(msg, data);
129
130 nla_put_failure:
131
132 return 0;
133 }
134
135 static void vrf_dump(struct rtnl_link *link, struct nl_dump_params *p)
136 {
137 struct vrf_info *vi = link->l_info;
138
139 if (vi->vi_mask & VRF_HAS_TABLE_ID) {
140 nl_dump(p, "table-id %u", vi->table_id);
141 }
142 }
143
144 static struct rtnl_link_info_ops vrf_info_ops = {
145 .io_name = "vrf",
146 .io_alloc = vrf_alloc,
147 .io_parse = vrf_parse,
148 .io_dump = {
149 [NL_DUMP_LINE] = vrf_dump,
150 [NL_DUMP_DETAILS] = vrf_dump,
151 },
152 .io_clone = vrf_clone,
153 .io_put_attrs = vrf_put_attrs,
154 .io_free = vrf_free,
155 };
156
157 /** @cond SKIP */
158 #define IS_VRF_LINK_ASSERT(link) \
159 if ((link)->l_info_ops != &vrf_info_ops) { \
160 APPBUG("Link is not a VRF link. set type \"vrf\" first."); \
161 return -NLE_OPNOTSUPP; \
162 }
163 /** @endcond */
164
165 /**
166 * @name VRF Object
167 * @{
168 */
169
170 /**
171 * Allocate link object of type VRF
172 *
173 * @return Allocated link object or NULL.
174 */
175 struct rtnl_link *rtnl_link_vrf_alloc(void)
176 {
177 struct rtnl_link *link;
178
179 if (!(link = rtnl_link_alloc()))
180 return NULL;
181
182 if (rtnl_link_set_type(link, "vrf") < 0) {
183 rtnl_link_put(link);
184 return NULL;
185 }
186
187 return link;
188 }
189
190 /**
191 * Check if link is a VRF link
192 * @arg link Link object
193 *
194 * @return True if link is a VRF link, otherwise false is returned.
195 */
196 int rtnl_link_is_vrf(struct rtnl_link *link)
197 {
198 return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "vrf");
199 }
200
201 /**
202 * Get VRF table id
203 * @arg link Link object
204 * @arg id Pointer to store table identifier
205 *
206 * @return 0 on success or a negative error code
207 */
208 int rtnl_link_vrf_get_tableid(struct rtnl_link *link, uint32_t *id)
209 {
210 struct vrf_info *vi = link->l_info;
211
212 IS_VRF_LINK_ASSERT(link);
213 if(!id)
214 return -NLE_INVAL;
215
216 if (vi->vi_mask & VRF_HAS_TABLE_ID)
217 *id = vi->table_id;
218 else
219 return -NLE_AGAIN;
220
221 return 0;
222 }
223
224 /**
225 * Set VRF table id
226 * @arg link Link object
227 * @arg id Table identifier associated with VRF link
228 *
229 * @return 0 on success or a negative error code
230 */
231 int rtnl_link_vrf_set_tableid(struct rtnl_link *link, uint32_t id)
232 {
233 struct vrf_info *vi = link->l_info;
234
235 IS_VRF_LINK_ASSERT(link);
236
237 _NL_STATIC_ASSERT(VRF_TABLE_ID_MAX == UINT32_MAX);
238
239 vi->table_id = id;
240 vi->vi_mask |= VRF_HAS_TABLE_ID;
241
242 return 0;
243 }
244
245 /** @} */
246
247 static void __init vrf_init(void)
248 {
249 rtnl_link_register_info(&vrf_info_ops);
250 }
251
252 static void __exit vrf_exit(void)
253 {
254 rtnl_link_unregister_info(&vrf_info_ops);
255 }
256
257 /** @} */
nla_get_u32
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
Definition: attr.c:699
NLA_PUT_U32
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
Definition: attr.h:230
nla_nest_start
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
Definition: attr.c:895
nla_parse_nested
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
Definition: attr.c:1013
nla_nest_end
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
Definition: attr.c:958
NLA_U32
@ NLA_U32
32 bit integer
Definition: attr.h:37
rtnl_link_register_info
int rtnl_link_register_info(struct rtnl_link_info_ops *ops)
Register operations for a link info type.
Definition: api.c:124
rtnl_link_unregister_info
int rtnl_link_unregister_info(struct rtnl_link_info_ops *ops)
Unregister operations for a link info type.
Definition: api.c:159
rtnl_link_put
void rtnl_link_put(struct rtnl_link *link)
Return a link object reference.
Definition: link.c:1919
rtnl_link_set_type
int rtnl_link_set_type(struct rtnl_link *link, const char *type)
Set type of link object.
Definition: link.c:2548
rtnl_link_alloc
struct rtnl_link * rtnl_link_alloc(void)
Allocate link object.
Definition: link.c:1910
nl_dump
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition: utils.c:955
NL_DUMP_LINE
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:16
NL_DUMP_DETAILS
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:17
rtnl_link_is_vrf
int rtnl_link_is_vrf(struct rtnl_link *link)
Check if link is a VRF link.
Definition: vrf.c:196
rtnl_link_vrf_set_tableid
int rtnl_link_vrf_set_tableid(struct rtnl_link *link, uint32_t id)
Set VRF table id.
Definition: vrf.c:231
rtnl_link_vrf_get_tableid
int rtnl_link_vrf_get_tableid(struct rtnl_link *link, uint32_t *id)
Get VRF table id.
Definition: vrf.c:208
rtnl_link_vrf_alloc
struct rtnl_link * rtnl_link_vrf_alloc(void)
Allocate link object of type VRF.
Definition: vrf.c:175
nl_dump_params
Dumping parameters.
Definition: types.h:28
nla_policy
Attribute validation policy.
Definition: attr.h:63
nla_policy::type
uint16_t type
Type of attribute or NLA_UNSPEC.
Definition: attr.h:65
rtnl_link
Link object.