libnl 3.10.0
template.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the
16 * distribution.
17 *
18 * Neither the name of Texas Instruments Incorporated nor the names of
19 * its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35/**
36 * @ingroup xfrmnl
37 * @defgroup XFRM User Template Object
38 *
39 * Abstract data type representing XFRM SA properties
40 *
41 * @{
42 *
43 * Header
44 * ------
45 * ~~~~{.c}
46 * #include <netlink/xfrm/template.h>
47 * ~~~~
48 */
49
50#include "nl-default.h"
51
52#include <netlink/xfrm/template.h>
53
54#include "nl-xfrm.h"
55#include "nl-priv-dynamic-core/nl-core.h"
56
57void xfrmnl_user_tmpl_free(struct xfrmnl_user_tmpl* utmpl)
58{
59 if (!utmpl)
60 return;
61
62 nl_addr_put (utmpl->id.daddr);
63 nl_addr_put (utmpl->saddr);
64 free(utmpl);
65}
66
67/**
68 * @name Creating User Template Object
69 * @{
70 */
71
72/**
73 * Allocate new user template object.
74 * @return Newly allocated user template object or NULL
75 */
77{
78 struct xfrmnl_user_tmpl* utmpl;
79
80 utmpl = calloc(1, sizeof(struct xfrmnl_user_tmpl));
81 if (!utmpl)
82 return NULL;
83
84 nl_init_list_head(&utmpl->utmpl_list);
85
86 return utmpl;
87}
88
89/**
90 * Clone existing user template object.
91 * @arg utmpl Selector object.
92 * @return Newly allocated user template object being a duplicate of the
93 * specified user template object or NULL if a failure occured.
94 */
96{
97 struct xfrmnl_user_tmpl* new;
98
100 if (!new)
101 return NULL;
102
103 memcpy(new, utmpl, sizeof(struct xfrmnl_user_tmpl));
104 new->id.daddr = nl_addr_clone (utmpl->id.daddr);
105 new->saddr = nl_addr_clone (utmpl->saddr);
106
107 return new;
108}
109
110/** @} */
111
112/**
113 * @name XFRM Template Mode Translations
114 * @{
115 */
116static const struct trans_tbl tmpl_modes[] = {
117 __ADD(XFRM_MODE_TRANSPORT, transport),
118 __ADD(XFRM_MODE_TUNNEL, tunnel),
119 __ADD(XFRM_MODE_ROUTEOPTIMIZATION, route optimization),
120 __ADD(XFRM_MODE_IN_TRIGGER, in trigger),
121 __ADD(XFRM_MODE_BEET, beet),
122};
123
124char* xfrmnl_user_tmpl_mode2str(int mode, char *buf, size_t len)
125{
126 return __type2str (mode, buf, len, tmpl_modes, ARRAY_SIZE(tmpl_modes));
127}
128
129int xfrmnl_user_tmpl_str2mode(const char *name)
130{
131 return __str2type (name, tmpl_modes, ARRAY_SIZE(tmpl_modes));
132}
133/** @} */
134
135/**
136 * @name Miscellaneous
137 * @{
138 */
139
140/**
141 * Compares two user template objects.
142 * @arg a A user template object.
143 * @arg b Another user template object.
144 *
145 * @return Non zero if difference is found, 0 otherwise if both
146 * the objects are identical.
147 */
149{
150 /* Check for any differences */
151 if ((nl_addr_cmp_prefix (a->id.daddr, b->id.daddr) != 0) ||
152 (a->id.spi != b->id.spi) ||
153 (a->id.proto && (a->id.proto != b->id.proto)) ||
154 (nl_addr_cmp_prefix (a->saddr, b->saddr) != 0) ||
155 (a->family != b->family) ||
156 (a->reqid != b->reqid) ||
157 (a->mode != b->mode) ||
158 (a->share != b->share) ||
159 (a->aalgos != b->aalgos) ||
160 (a->ealgos != b->ealgos) ||
161 (a->calgos != b->calgos))
162 return 1;
163
164 /* The objects are identical */
165 return 0;
166}
167
168void xfrmnl_user_tmpl_dump(struct xfrmnl_user_tmpl* tmpl, struct nl_dump_params *p)
169{
170 char dst[INET6_ADDRSTRLEN+5], src[INET6_ADDRSTRLEN+5];
171 char buf [128];
172
173 nl_dump_line(p, "\t\tsrc %s dst %s family: %s \n",
174 nl_addr2str(tmpl->saddr, src, sizeof(src)),
175 nl_addr2str (tmpl->id.daddr, dst, sizeof (dst)),
176 nl_af2str (tmpl->family, buf, 128));
177 nl_dump_line (p, "\t\tprotocol: %s spi: 0x%x reqid: %u mode: %s\n",
178 nl_ip_proto2str (tmpl->id.proto, buf, sizeof(buf)),
179 tmpl->id.spi, tmpl->reqid,
180 xfrmnl_user_tmpl_mode2str (tmpl->mode, buf, 128));
181 nl_dump_line (p, "\t\tAuth Algo: 0x%x Crypto Algo: 0x%x Compr Algo: 0x%x\n",
182 tmpl->aalgos, tmpl->ealgos, tmpl->calgos);
183
184 return;
185}
186
187/** @} */
188
189/**
190 * @name Attributes
191 * @{
192 */
193struct nl_addr* xfrmnl_user_tmpl_get_daddr (struct xfrmnl_user_tmpl* utmpl)
194{
195 return utmpl->id.daddr;
196}
197
198int xfrmnl_user_tmpl_set_daddr (struct xfrmnl_user_tmpl* utmpl, struct nl_addr* addr)
199{
200 /* Increment reference counter on this to keep this address
201 * object around while user template in use */
202 nl_addr_get(addr);
203
204 utmpl->id.daddr = addr;
205
206 return 0;
207}
208
209int xfrmnl_user_tmpl_get_spi (struct xfrmnl_user_tmpl* utmpl)
210{
211 return utmpl->id.spi;
212}
213
214int xfrmnl_user_tmpl_set_spi (struct xfrmnl_user_tmpl* utmpl, unsigned int spi)
215{
216 utmpl->id.spi = spi;
217
218 return 0;
219}
220
221int xfrmnl_user_tmpl_get_proto (struct xfrmnl_user_tmpl* utmpl)
222{
223 return utmpl->id.proto;
224}
225
226int xfrmnl_user_tmpl_set_proto (struct xfrmnl_user_tmpl* utmpl, unsigned int protocol)
227{
228 utmpl->id.proto = protocol;
229
230 return 0;
231}
232
233int xfrmnl_user_tmpl_get_family(struct xfrmnl_user_tmpl *utmpl)
234{
235 return utmpl->family;
236}
237
238int xfrmnl_user_tmpl_set_family(struct xfrmnl_user_tmpl *utmpl, unsigned int family)
239{
240 utmpl->family = family;
241
242 return 0;
243}
244
245struct nl_addr* xfrmnl_user_tmpl_get_saddr (struct xfrmnl_user_tmpl* utmpl)
246{
247 return utmpl->saddr;
248}
249
250int xfrmnl_user_tmpl_set_saddr (struct xfrmnl_user_tmpl* utmpl, struct nl_addr* addr)
251{
252 /* Increment reference counter on this to keep this address
253 * object around while user template in use */
254 nl_addr_get(addr);
255
256 utmpl->saddr = addr;
257
258 return 0;
259}
260
261int xfrmnl_user_tmpl_get_reqid (struct xfrmnl_user_tmpl* utmpl)
262{
263 return utmpl->reqid;
264}
265
266int xfrmnl_user_tmpl_set_reqid (struct xfrmnl_user_tmpl* utmpl, unsigned int reqid)
267{
268 utmpl->reqid = reqid;
269
270 return 0;
271}
272
273int xfrmnl_user_tmpl_get_mode (struct xfrmnl_user_tmpl* utmpl)
274{
275 return utmpl->mode;
276}
277
278int xfrmnl_user_tmpl_set_mode (struct xfrmnl_user_tmpl* utmpl, unsigned int mode)
279{
280 utmpl->mode = mode;
281
282 return 0;
283}
284
285int xfrmnl_user_tmpl_get_share (struct xfrmnl_user_tmpl* utmpl)
286{
287 return utmpl->share;
288}
289
290int xfrmnl_user_tmpl_set_share (struct xfrmnl_user_tmpl* utmpl, unsigned int share)
291{
292 utmpl->share = share;
293
294 return 0;
295}
296
297int xfrmnl_user_tmpl_get_optional (struct xfrmnl_user_tmpl* utmpl)
298{
299 return utmpl->optional;
300}
301
302int xfrmnl_user_tmpl_set_optional (struct xfrmnl_user_tmpl* utmpl, unsigned int optional)
303{
304 utmpl->optional = optional;
305
306 return 0;
307}
308
309int xfrmnl_user_tmpl_get_aalgos (struct xfrmnl_user_tmpl* utmpl)
310{
311 return utmpl->aalgos;
312}
313
314int xfrmnl_user_tmpl_set_aalgos (struct xfrmnl_user_tmpl* utmpl, unsigned int aalgos)
315{
316 utmpl->aalgos = aalgos;
317
318 return 0;
319}
320
321int xfrmnl_user_tmpl_get_ealgos (struct xfrmnl_user_tmpl* utmpl)
322{
323 return utmpl->ealgos;
324}
325
326int xfrmnl_user_tmpl_set_ealgos (struct xfrmnl_user_tmpl* utmpl, unsigned int ealgos)
327{
328 utmpl->ealgos = ealgos;
329
330 return 0;
331}
332
333int xfrmnl_user_tmpl_get_calgos (struct xfrmnl_user_tmpl* utmpl)
334{
335 return utmpl->calgos;
336}
337
338int xfrmnl_user_tmpl_set_calgos (struct xfrmnl_user_tmpl* utmpl, unsigned int calgos)
339{
340 utmpl->calgos = calgos;
341
342 return 0;
343}
344
345/** @} */
struct xfrmnl_user_tmpl * xfrmnl_user_tmpl_clone(struct xfrmnl_user_tmpl *utmpl)
Clone existing user template object.
Definition template.c:95
int xfrmnl_user_tmpl_cmp(struct xfrmnl_user_tmpl *a, struct xfrmnl_user_tmpl *b)
Compares two user template objects.
Definition template.c:148
struct xfrmnl_user_tmpl * xfrmnl_user_tmpl_alloc()
Allocate new user template object.
Definition template.c:76
struct nl_addr * nl_addr_get(struct nl_addr *addr)
Increase the reference counter of an abstract address.
Definition addr.c:525
struct nl_addr * nl_addr_clone(const struct nl_addr *addr)
Clone existing abstract address object.
Definition addr.c:495
int nl_addr_cmp_prefix(const struct nl_addr *a, const struct nl_addr *b)
Compare the prefix of two abstract addresses.
Definition addr.c:626
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.
Definition addr.c:1001
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
Definition addr.c:541
Dumping parameters.
Definition types.h:32