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