xref: /aosp_15_r20/external/libnl/lib/xfrm/template.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
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 
xfrmnl_user_tmpl_free(struct xfrmnl_user_tmpl * utmpl)57 void 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  */
xfrmnl_user_tmpl_alloc()76 struct xfrmnl_user_tmpl* xfrmnl_user_tmpl_alloc()
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  */
xfrmnl_user_tmpl_clone(struct xfrmnl_user_tmpl * utmpl)95 struct xfrmnl_user_tmpl* xfrmnl_user_tmpl_clone(struct xfrmnl_user_tmpl* utmpl)
96 {
97 	struct xfrmnl_user_tmpl* new;
98 
99 	new = xfrmnl_user_tmpl_alloc();
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  */
116 static 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 
xfrmnl_user_tmpl_mode2str(int mode,char * buf,size_t len)124 char* 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 
xfrmnl_user_tmpl_str2mode(const char * name)129 int 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  */
xfrmnl_user_tmpl_cmp(struct xfrmnl_user_tmpl * a,struct xfrmnl_user_tmpl * b)148 int xfrmnl_user_tmpl_cmp(struct xfrmnl_user_tmpl* a, struct xfrmnl_user_tmpl* b)
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 
xfrmnl_user_tmpl_dump(struct xfrmnl_user_tmpl * tmpl,struct nl_dump_params * p)168 void 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  */
xfrmnl_user_tmpl_get_daddr(struct xfrmnl_user_tmpl * utmpl)193 struct nl_addr* xfrmnl_user_tmpl_get_daddr (struct xfrmnl_user_tmpl* utmpl)
194 {
195 	return utmpl->id.daddr;
196 }
197 
xfrmnl_user_tmpl_set_daddr(struct xfrmnl_user_tmpl * utmpl,struct nl_addr * addr)198 int 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 
xfrmnl_user_tmpl_get_spi(struct xfrmnl_user_tmpl * utmpl)209 int xfrmnl_user_tmpl_get_spi (struct xfrmnl_user_tmpl* utmpl)
210 {
211 	return utmpl->id.spi;
212 }
213 
xfrmnl_user_tmpl_set_spi(struct xfrmnl_user_tmpl * utmpl,unsigned int spi)214 int 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 
xfrmnl_user_tmpl_get_proto(struct xfrmnl_user_tmpl * utmpl)221 int xfrmnl_user_tmpl_get_proto (struct xfrmnl_user_tmpl* utmpl)
222 {
223 	return utmpl->id.proto;
224 }
225 
xfrmnl_user_tmpl_set_proto(struct xfrmnl_user_tmpl * utmpl,unsigned int protocol)226 int 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 
xfrmnl_user_tmpl_get_family(struct xfrmnl_user_tmpl * utmpl)233 int xfrmnl_user_tmpl_get_family(struct xfrmnl_user_tmpl *utmpl)
234 {
235 	return utmpl->family;
236 }
237 
xfrmnl_user_tmpl_set_family(struct xfrmnl_user_tmpl * utmpl,unsigned int family)238 int xfrmnl_user_tmpl_set_family(struct xfrmnl_user_tmpl *utmpl, unsigned int family)
239 {
240 	utmpl->family = family;
241 
242 	return 0;
243 }
244 
xfrmnl_user_tmpl_get_saddr(struct xfrmnl_user_tmpl * utmpl)245 struct nl_addr* xfrmnl_user_tmpl_get_saddr (struct xfrmnl_user_tmpl* utmpl)
246 {
247 	return utmpl->saddr;
248 }
249 
xfrmnl_user_tmpl_set_saddr(struct xfrmnl_user_tmpl * utmpl,struct nl_addr * addr)250 int 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 
xfrmnl_user_tmpl_get_reqid(struct xfrmnl_user_tmpl * utmpl)261 int xfrmnl_user_tmpl_get_reqid (struct xfrmnl_user_tmpl* utmpl)
262 {
263 	return utmpl->reqid;
264 }
265 
xfrmnl_user_tmpl_set_reqid(struct xfrmnl_user_tmpl * utmpl,unsigned int reqid)266 int xfrmnl_user_tmpl_set_reqid (struct xfrmnl_user_tmpl* utmpl, unsigned int reqid)
267 {
268 	utmpl->reqid = reqid;
269 
270 	return 0;
271 }
272 
xfrmnl_user_tmpl_get_mode(struct xfrmnl_user_tmpl * utmpl)273 int xfrmnl_user_tmpl_get_mode (struct xfrmnl_user_tmpl* utmpl)
274 {
275 	return utmpl->mode;
276 }
277 
xfrmnl_user_tmpl_set_mode(struct xfrmnl_user_tmpl * utmpl,unsigned int mode)278 int xfrmnl_user_tmpl_set_mode (struct xfrmnl_user_tmpl* utmpl, unsigned int mode)
279 {
280 	utmpl->mode = mode;
281 
282 	return 0;
283 }
284 
xfrmnl_user_tmpl_get_share(struct xfrmnl_user_tmpl * utmpl)285 int xfrmnl_user_tmpl_get_share (struct xfrmnl_user_tmpl* utmpl)
286 {
287 	return utmpl->share;
288 }
289 
xfrmnl_user_tmpl_set_share(struct xfrmnl_user_tmpl * utmpl,unsigned int share)290 int xfrmnl_user_tmpl_set_share (struct xfrmnl_user_tmpl* utmpl, unsigned int share)
291 {
292 	utmpl->share = share;
293 
294 	return 0;
295 }
296 
xfrmnl_user_tmpl_get_optional(struct xfrmnl_user_tmpl * utmpl)297 int xfrmnl_user_tmpl_get_optional (struct xfrmnl_user_tmpl* utmpl)
298 {
299 	return utmpl->optional;
300 }
301 
xfrmnl_user_tmpl_set_optional(struct xfrmnl_user_tmpl * utmpl,unsigned int optional)302 int xfrmnl_user_tmpl_set_optional (struct xfrmnl_user_tmpl* utmpl, unsigned int optional)
303 {
304 	utmpl->optional = optional;
305 
306 	return 0;
307 }
308 
xfrmnl_user_tmpl_get_aalgos(struct xfrmnl_user_tmpl * utmpl)309 int xfrmnl_user_tmpl_get_aalgos (struct xfrmnl_user_tmpl* utmpl)
310 {
311 	return utmpl->aalgos;
312 }
313 
xfrmnl_user_tmpl_set_aalgos(struct xfrmnl_user_tmpl * utmpl,unsigned int aalgos)314 int xfrmnl_user_tmpl_set_aalgos (struct xfrmnl_user_tmpl* utmpl, unsigned int aalgos)
315 {
316 	utmpl->aalgos = aalgos;
317 
318 	return 0;
319 }
320 
xfrmnl_user_tmpl_get_ealgos(struct xfrmnl_user_tmpl * utmpl)321 int xfrmnl_user_tmpl_get_ealgos (struct xfrmnl_user_tmpl* utmpl)
322 {
323 	return utmpl->ealgos;
324 }
325 
xfrmnl_user_tmpl_set_ealgos(struct xfrmnl_user_tmpl * utmpl,unsigned int ealgos)326 int xfrmnl_user_tmpl_set_ealgos (struct xfrmnl_user_tmpl* utmpl, unsigned int ealgos)
327 {
328 	utmpl->ealgos = ealgos;
329 
330 	return 0;
331 }
332 
xfrmnl_user_tmpl_get_calgos(struct xfrmnl_user_tmpl * utmpl)333 int xfrmnl_user_tmpl_get_calgos (struct xfrmnl_user_tmpl* utmpl)
334 {
335 	return utmpl->calgos;
336 }
337 
xfrmnl_user_tmpl_set_calgos(struct xfrmnl_user_tmpl * utmpl,unsigned int calgos)338 int 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 /** @} */
346