libnl 3.5.0

xfrmi.c
1 /*
2 * lib/route/link/xfrmi.c XFRMI Link Info
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
7 * of the License.
8 *
9 * Copyright (c) 2019 Eyal Birger <eyal.birger@gmail.com>
10 *
11 * Based on lib/route/link/ipvti.c
12 */
13
14 /**
15 * @ingroup link
16 * @defgroup xfrmi XFRMI
17 * xfrmi link module
18 *
19 * @details
20 * \b Link Type Name: "xfrmi"
21 *
22 * @route_doc{link_xfrmi, XFRMI Documentation}
23 *
24 * @{
25 */
26
27 #include <netlink-private/netlink.h>
28 #include <netlink/netlink.h>
29 #include <netlink/attr.h>
30 #include <netlink/utils.h>
31 #include <netlink/object.h>
32 #include <netlink/route/rtnl.h>
33 #include <netlink/route/link/xfrmi.h>
34 #include <netlink-private/route/link/api.h>
35
36 #define XFRMI_ATTR_LINK (1 << 0)
37 #define XFRMI_ATTR_IF_ID (1 << 1)
38
39 #define XFRMI_LINK_TYPE_NAME "xfrm"
40
41 struct xfrmi_info {
42 uint32_t link;
43 uint32_t if_id;
44 uint32_t xfrmi_mask;
45 };
46
47 static struct nla_policy xfrmi_policy[IFLA_XFRM_MAX + 1] = {
48 [IFLA_XFRM_LINK] = { .type = NLA_U32 },
49 [IFLA_XFRM_IF_ID] = { .type = NLA_U32 },
50 };
51
52 static int xfrmi_alloc(struct rtnl_link *link)
53 {
54 struct xfrmi_info *xfrmi;
55
56 if (link->l_info)
57 memset(link->l_info, 0, sizeof(*xfrmi));
58 else {
59 xfrmi = calloc(1, sizeof(*xfrmi));
60 if (!xfrmi)
61 return -NLE_NOMEM;
62
63 link->l_info = xfrmi;
64 }
65
66 return 0;
67 }
68
69 static int xfrmi_parse(struct rtnl_link *link, struct nlattr *data,
70 struct nlattr *xstats)
71 {
72 struct nlattr *tb[IFLA_XFRM_MAX + 1];
73 struct xfrmi_info *xfrmi;
74 int err;
75
76 NL_DBG(3, "Parsing XFRMI link info\n");
77
78 err = nla_parse_nested(tb, IFLA_XFRM_MAX, data, xfrmi_policy);
79 if (err < 0)
80 return err;
81
82 err = xfrmi_alloc(link);
83 if (err < 0)
84 return err;
85
86 xfrmi = link->l_info;
87
88 if (tb[IFLA_XFRM_LINK]) {
89 xfrmi->link = nla_get_u32(tb[IFLA_XFRM_LINK]);
90 xfrmi->xfrmi_mask |= XFRMI_ATTR_LINK;
91 }
92
93 if (tb[IFLA_XFRM_IF_ID]) {
94 xfrmi->if_id = nla_get_u32(tb[IFLA_XFRM_IF_ID]);
95 xfrmi->xfrmi_mask |= XFRMI_ATTR_IF_ID;
96 }
97
98 return 0;
99 }
100
101 static int xfrmi_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
102 {
103 struct xfrmi_info *xfrmi = link->l_info;
104 struct nlattr *data;
105
106 data = nla_nest_start(msg, IFLA_INFO_DATA);
107 if (!data)
108 return -NLE_MSGSIZE;
109
110 if (xfrmi->xfrmi_mask & XFRMI_ATTR_LINK)
111 NLA_PUT_U32(msg, IFLA_XFRM_LINK, xfrmi->link);
112
113 if (xfrmi->xfrmi_mask & XFRMI_ATTR_IF_ID)
114 NLA_PUT_U32(msg, IFLA_XFRM_IF_ID, xfrmi->if_id);
115
116 nla_nest_end(msg, data);
117
118 nla_put_failure:
119 return 0;
120 }
121
122 static void xfrmi_free(struct rtnl_link *link)
123 {
124 struct xfrmi_info *xfrmi = link->l_info;
125
126 free(xfrmi);
127 link->l_info = NULL;
128 }
129
130 static void xfrmi_dump_line(struct rtnl_link *link, struct nl_dump_params *p)
131 {
132 nl_dump(p, "xfrmi : %s", link->l_name);
133 }
134
135 static void xfrmi_dump_details(struct rtnl_link *link, struct nl_dump_params *p)
136 {
137 struct xfrmi_info *xfrmi = link->l_info;
138
139 if (xfrmi->xfrmi_mask & XFRMI_ATTR_LINK) {
140 struct rtnl_link *parent;
141 char *name;
142
143 nl_dump(p, " link ");
144
145 name = NULL;
146 parent = link_lookup(link->ce_cache, xfrmi->link);
147 if (parent)
148 name = rtnl_link_get_name(parent);
149
150 if (name)
151 nl_dump_line(p, "%s\n", name);
152 else
153 nl_dump_line(p, "%u\n", xfrmi->link);
154 }
155
156 if (xfrmi->xfrmi_mask & XFRMI_ATTR_IF_ID) {
157 nl_dump(p, " if_id ");
158 nl_dump_line(p, "%x\n", xfrmi->if_id);
159 }
160 }
161
162 static int xfrmi_clone(struct rtnl_link *dst, struct rtnl_link *src)
163 {
164 struct xfrmi_info *xfrmi_dst, *xfrmi_src = src->l_info;
165 int err;
166
167 dst->l_info = NULL;
168
169 err = rtnl_link_set_type(dst, XFRMI_LINK_TYPE_NAME);
170 if (err < 0)
171 return err;
172
173 xfrmi_dst = dst->l_info;
174
175 if (!xfrmi_dst || !xfrmi_src)
176 BUG();
177
178 memcpy(xfrmi_dst, xfrmi_src, sizeof(struct xfrmi_info));
179
180 return 0;
181 }
182
183 static struct rtnl_link_info_ops xfrmi_info_ops = {
184 .io_name = XFRMI_LINK_TYPE_NAME,
185 .io_alloc = xfrmi_alloc,
186 .io_parse = xfrmi_parse,
187 .io_dump = {
188 [NL_DUMP_LINE] = xfrmi_dump_line,
189 [NL_DUMP_DETAILS] = xfrmi_dump_details,
190 },
191 .io_clone = xfrmi_clone,
192 .io_put_attrs = xfrmi_put_attrs,
193 .io_free = xfrmi_free,
194 };
195
196 #define IS_XFRMI_LINK_ASSERT(link) do { \
197 if ((link)->l_info_ops != &xfrmi_info_ops) { \
198 APPBUG("Link is not a xfrmi link. set type \"xfrmi\" first."); \
199 return -NLE_OPNOTSUPP; \
200 } \
201 } while(0)
202
203 struct rtnl_link *rtnl_link_xfrmi_alloc(void)
204 {
205 struct rtnl_link *link;
206 int err;
207
208 link = rtnl_link_alloc();
209 if (!link)
210 return NULL;
211
212 err = rtnl_link_set_type(link, XFRMI_LINK_TYPE_NAME);
213 if (err < 0) {
214 rtnl_link_put(link);
215 return NULL;
216 }
217
218 return link;
219 }
220
221 /**
222 * Check if link is a XFRMI link
223 * @arg link Link object
224 *
225 * @return True if link is a IXFRMI link, otherwise 0 is returned.
226 */
227 int rtnl_link_is_xfrmi(struct rtnl_link *link)
228 {
229 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
230 XFRMI_LINK_TYPE_NAME);
231 }
232
233 /**
234 * Set XFRMI link interface index
235 * @arg link Link object
236 * @arg index interface index
237 *
238 * @return 0 on success or a negative error code
239 */
240 int rtnl_link_xfrmi_set_link(struct rtnl_link *link, uint32_t index)
241 {
242 struct xfrmi_info *xfrmi = link->l_info;
243
244 IS_XFRMI_LINK_ASSERT(link);
245
246 xfrmi->link = index;
247 xfrmi->xfrmi_mask |= XFRMI_ATTR_LINK;
248
249 return 0;
250 }
251
252 /**
253 * Get XFRMI link interface index
254 * @arg link Link object
255 * @arg out_link The output value on success
256 *
257 * @return 0 on sucess or a negative error code
258 */
259 int rtnl_link_xfrmi_get_link(struct rtnl_link *link, uint32_t *out_link)
260 {
261 struct xfrmi_info *xfrmi = link->l_info;
262
263 IS_XFRMI_LINK_ASSERT(link);
264
265 if (!(xfrmi->xfrmi_mask & XFRMI_ATTR_LINK))
266 return -NLE_NOATTR;
267
268 *out_link = xfrmi->link;
269 return 0;
270 }
271
272 /**
273 * Set XFRMI if_id
274 * @arg link Link object
275 * @arg if_id xfrm if_id
276 *
277 * @return 0 on success or a negative error code
278 */
279 int rtnl_link_xfrmi_set_if_id(struct rtnl_link *link, uint32_t if_id)
280 {
281 struct xfrmi_info *xfrmi = link->l_info;
282
283 IS_XFRMI_LINK_ASSERT(link);
284
285 xfrmi->if_id = if_id;
286 xfrmi->xfrmi_mask |= XFRMI_ATTR_IF_ID;
287
288 return 0;
289 }
290
291 /**
292 * Get XFRMI if_id
293 * @arg link Link object
294 * @arg out_if_id The output value on success
295 *
296 * @return 0 on sucess or a negative error code
297 */
298 int rtnl_link_xfrmi_get_if_id(struct rtnl_link *link, uint32_t *out_if_id)
299 {
300 struct xfrmi_info *xfrmi = link->l_info;
301
302 IS_XFRMI_LINK_ASSERT(link);
303
304 if (!(xfrmi->xfrmi_mask & XFRMI_ATTR_IF_ID))
305 return -NLE_NOATTR;
306
307 *out_if_id = xfrmi->if_id;
308 return 0;
309 }
310
311 static void __init xfrmi_init(void)
312 {
313 rtnl_link_register_info(&xfrmi_info_ops);
314 }
315
316 static void __exit xfrmi_exit(void)
317 {
318 rtnl_link_unregister_info(&xfrmi_info_ops);
319 }
NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:22
rtnl_link_is_xfrmi
int rtnl_link_is_xfrmi(struct rtnl_link *link)
Check if link is a XFRMI link.
Definition: xfrmi.c:227
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:111
nla_policy
Attribute validation policy.
Definition: attr.h:69
rtnl_link_xfrmi_set_link
int rtnl_link_xfrmi_set_link(struct rtnl_link *link, uint32_t index)
Set XFRMI link interface index.
Definition: xfrmi.c:240
rtnl_link_alloc
struct rtnl_link * rtnl_link_alloc(void)
Allocate link object.
Definition: link.c:1931
nla_get_u32
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
Definition: attr.c:707
rtnl_link
Link object.
NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:23
rtnl_link_get_name
char * rtnl_link_get_name(struct rtnl_link *link)
Return name of link object.
Definition: link.c:1973
nla_nest_end
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
Definition: attr.c:966
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:1021
rtnl_link_xfrmi_set_if_id
int rtnl_link_xfrmi_set_if_id(struct rtnl_link *link, uint32_t if_id)
Set XFRMI if_id.
Definition: xfrmi.c:279
rtnl_link_set_type
int rtnl_link_set_type(struct rtnl_link *link, const char *type)
Set type of link object.
Definition: link.c:2568
NLA_PUT_U32
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
Definition: attr.h:236
xfrmi_info
Definition: xfrmi.c:41
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:146
nla_policy::type
uint16_t type
Type of attribute or NLA_UNSPEC.
Definition: attr.h:71
NLA_U32
32 bit integer
Definition: attr.h:43
nl_dump_params
Dumping parameters.
Definition: types.h:33
nl_dump
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition: utils.c:962
rtnl_link_put
void rtnl_link_put(struct rtnl_link *link)
Return a link object reference.
Definition: link.c:1940
rtnl_link_xfrmi_get_if_id
int rtnl_link_xfrmi_get_if_id(struct rtnl_link *link, uint32_t *out_if_id)
Get XFRMI if_id.
Definition: xfrmi.c:298
nla_nest_start
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
Definition: attr.c:903
rtnl_link_xfrmi_get_link
int rtnl_link_xfrmi_get_link(struct rtnl_link *link, uint32_t *out_link)
Get XFRMI link interface index.
Definition: xfrmi.c:259