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 <netlink/xfrm/template.h>
51 #include <netlink-private/netlink.h>
52 
xfrmnl_user_tmpl_free(struct xfrmnl_user_tmpl * utmpl)53 void xfrmnl_user_tmpl_free(struct xfrmnl_user_tmpl* utmpl)
54 {
55 	if (!utmpl)
56 		return;
57 
58 	nl_addr_put (utmpl->id.daddr);
59 	nl_addr_put (utmpl->saddr);
60 	free(utmpl);
61 }
62 
63 /**
64  * @name Creating User Template Object
65  * @{
66  */
67 
68 /**
69  * Allocate new user template object.
70  * @return Newly allocated user template object or NULL
71  */
xfrmnl_user_tmpl_alloc()72 struct xfrmnl_user_tmpl* xfrmnl_user_tmpl_alloc()
73 {
74 	struct xfrmnl_user_tmpl* utmpl;
75 
76 	utmpl = calloc(1, sizeof(struct xfrmnl_user_tmpl));
77 	if (!utmpl)
78 		return NULL;
79 
80 	nl_init_list_head(&utmpl->utmpl_list);
81 
82 	return utmpl;
83 }
84 
85 /**
86  * Clone existing user template object.
87  * @arg utmpl		Selector object.
88  * @return Newly allocated user template object being a duplicate of the
89  *         specified user template object or NULL if a failure occured.
90  */
xfrmnl_user_tmpl_clone(struct xfrmnl_user_tmpl * utmpl)91 struct xfrmnl_user_tmpl* xfrmnl_user_tmpl_clone(struct xfrmnl_user_tmpl* utmpl)
92 {
93 	struct xfrmnl_user_tmpl* new;
94 
95 	new = xfrmnl_user_tmpl_alloc();
96 	if (!new)
97 		return NULL;
98 
99 	memcpy(new, utmpl, sizeof(struct xfrmnl_user_tmpl));
100 	new->id.daddr = nl_addr_clone (utmpl->id.daddr);
101 	new->saddr    = nl_addr_clone (utmpl->saddr);
102 
103 	return new;
104 }
105 
106 /** @} */
107 
108 /**
109  * @name XFRM Template Mode Translations
110  * @{
111  */
112 static const struct trans_tbl tmpl_modes[] = {
113 	__ADD(XFRM_MODE_TRANSPORT, transport),
114 	__ADD(XFRM_MODE_TUNNEL, tunnel),
115 	__ADD(XFRM_MODE_ROUTEOPTIMIZATION, route optimization),
116 	__ADD(XFRM_MODE_IN_TRIGGER, in trigger),
117 	__ADD(XFRM_MODE_BEET, beet),
118 };
119 
xfrmnl_user_tmpl_mode2str(int mode,char * buf,size_t len)120 char* xfrmnl_user_tmpl_mode2str(int mode, char *buf, size_t len)
121 {
122 	return __type2str (mode, buf, len, tmpl_modes, ARRAY_SIZE(tmpl_modes));
123 }
124 
xfrmnl_user_tmpl_str2mode(const char * name)125 int xfrmnl_user_tmpl_str2mode(const char *name)
126 {
127 	return __str2type (name, tmpl_modes, ARRAY_SIZE(tmpl_modes));
128 }
129 /** @} */
130 
131 /**
132  * @name Miscellaneous
133  * @{
134  */
135 
136 /**
137  * Compares two user template objects.
138  * @arg a		A user template object.
139  * @arg b		Another user template object.
140  *
141  * @return Non zero if difference is found, 0 otherwise if both
142  * the objects are identical.
143  */
xfrmnl_user_tmpl_cmp(struct xfrmnl_user_tmpl * a,struct xfrmnl_user_tmpl * b)144 int xfrmnl_user_tmpl_cmp(struct xfrmnl_user_tmpl* a, struct xfrmnl_user_tmpl* b)
145 {
146 	/* Check for any differences */
147 	if ((nl_addr_cmp_prefix (a->id.daddr, b->id.daddr) != 0) ||
148 	    (a->id.spi != b->id.spi) ||
149 	    (a->id.proto && (a->id.proto != b->id.proto)) ||
150 	    (nl_addr_cmp_prefix (a->saddr, b->saddr) != 0) ||
151 	    (a->family != b->family) ||
152 	    (a->reqid != b->reqid) ||
153 	    (a->mode != b->mode) ||
154 	    (a->share != b->share) ||
155 	    (a->aalgos != b->aalgos) ||
156 	    (a->ealgos != b->ealgos) ||
157 	    (a->calgos != b->calgos))
158 		return 1;
159 
160 	/* The objects are identical */
161 	return 0;
162 }
163 
xfrmnl_user_tmpl_dump(struct xfrmnl_user_tmpl * tmpl,struct nl_dump_params * p)164 void xfrmnl_user_tmpl_dump(struct xfrmnl_user_tmpl* tmpl, struct nl_dump_params *p)
165 {
166 	char    dst[INET6_ADDRSTRLEN+5], src[INET6_ADDRSTRLEN+5];
167 	char    buf [128];
168 
169 	nl_dump_line(p, "\t\tsrc %s dst %s family: %s \n",
170 	             nl_addr2str(tmpl->saddr, src, sizeof(src)),
171 	             nl_addr2str (tmpl->id.daddr, dst, sizeof (dst)),
172 	             nl_af2str (tmpl->family, buf, 128));
173 	nl_dump_line (p, "\t\tprotocol: %s spi: 0x%x reqid: %u mode: %s\n",
174 	              nl_ip_proto2str (tmpl->id.proto, buf, sizeof(buf)),
175 	              tmpl->id.spi, tmpl->reqid,
176 	              xfrmnl_user_tmpl_mode2str (tmpl->mode, buf, 128));
177 	nl_dump_line (p, "\t\tAuth Algo: 0x%x Crypto Algo: 0x%x Compr Algo: 0x%x\n",
178 	              tmpl->aalgos, tmpl->ealgos, tmpl->calgos);
179 
180 	return;
181 }
182 
183 /** @} */
184 
185 /**
186  * @name Attributes
187  * @{
188  */
xfrmnl_user_tmpl_get_daddr(struct xfrmnl_user_tmpl * utmpl)189 struct nl_addr* xfrmnl_user_tmpl_get_daddr (struct xfrmnl_user_tmpl* utmpl)
190 {
191 	return utmpl->id.daddr;
192 }
193 
xfrmnl_user_tmpl_set_daddr(struct xfrmnl_user_tmpl * utmpl,struct nl_addr * addr)194 int xfrmnl_user_tmpl_set_daddr (struct xfrmnl_user_tmpl* utmpl, struct nl_addr* addr)
195 {
196 	/* Increment reference counter on this to keep this address
197 	 * object around while user template in use */
198 	nl_addr_get(addr);
199 
200 	utmpl->id.daddr = addr;
201 
202 	return 0;
203 }
204 
xfrmnl_user_tmpl_get_spi(struct xfrmnl_user_tmpl * utmpl)205 int xfrmnl_user_tmpl_get_spi (struct xfrmnl_user_tmpl* utmpl)
206 {
207 	return utmpl->id.spi;
208 }
209 
xfrmnl_user_tmpl_set_spi(struct xfrmnl_user_tmpl * utmpl,unsigned int spi)210 int xfrmnl_user_tmpl_set_spi (struct xfrmnl_user_tmpl* utmpl, unsigned int spi)
211 {
212 	utmpl->id.spi = spi;
213 
214 	return 0;
215 }
216 
xfrmnl_user_tmpl_get_proto(struct xfrmnl_user_tmpl * utmpl)217 int xfrmnl_user_tmpl_get_proto (struct xfrmnl_user_tmpl* utmpl)
218 {
219 	return utmpl->id.proto;
220 }
221 
xfrmnl_user_tmpl_set_proto(struct xfrmnl_user_tmpl * utmpl,unsigned int protocol)222 int xfrmnl_user_tmpl_set_proto (struct xfrmnl_user_tmpl* utmpl, unsigned int protocol)
223 {
224 	utmpl->id.proto = protocol;
225 
226 	return 0;
227 }
228 
xfrmnl_user_tmpl_get_family(struct xfrmnl_user_tmpl * utmpl)229 int xfrmnl_user_tmpl_get_family(struct xfrmnl_user_tmpl *utmpl)
230 {
231 	return utmpl->family;
232 }
233 
xfrmnl_user_tmpl_set_family(struct xfrmnl_user_tmpl * utmpl,unsigned int family)234 int xfrmnl_user_tmpl_set_family(struct xfrmnl_user_tmpl *utmpl, unsigned int family)
235 {
236 	utmpl->family = family;
237 
238 	return 0;
239 }
240 
xfrmnl_user_tmpl_get_saddr(struct xfrmnl_user_tmpl * utmpl)241 struct nl_addr* xfrmnl_user_tmpl_get_saddr (struct xfrmnl_user_tmpl* utmpl)
242 {
243 	return utmpl->saddr;
244 }
245 
xfrmnl_user_tmpl_set_saddr(struct xfrmnl_user_tmpl * utmpl,struct nl_addr * addr)246 int xfrmnl_user_tmpl_set_saddr (struct xfrmnl_user_tmpl* utmpl, struct nl_addr* addr)
247 {
248 	/* Increment reference counter on this to keep this address
249 	 * object around while user template in use */
250 	nl_addr_get(addr);
251 
252 	utmpl->saddr = addr;
253 
254 	return 0;
255 }
256 
xfrmnl_user_tmpl_get_reqid(struct xfrmnl_user_tmpl * utmpl)257 int xfrmnl_user_tmpl_get_reqid (struct xfrmnl_user_tmpl* utmpl)
258 {
259 	return utmpl->reqid;
260 }
261 
xfrmnl_user_tmpl_set_reqid(struct xfrmnl_user_tmpl * utmpl,unsigned int reqid)262 int xfrmnl_user_tmpl_set_reqid (struct xfrmnl_user_tmpl* utmpl, unsigned int reqid)
263 {
264 	utmpl->reqid = reqid;
265 
266 	return 0;
267 }
268 
xfrmnl_user_tmpl_get_mode(struct xfrmnl_user_tmpl * utmpl)269 int xfrmnl_user_tmpl_get_mode (struct xfrmnl_user_tmpl* utmpl)
270 {
271 	return utmpl->mode;
272 }
273 
xfrmnl_user_tmpl_set_mode(struct xfrmnl_user_tmpl * utmpl,unsigned int mode)274 int xfrmnl_user_tmpl_set_mode (struct xfrmnl_user_tmpl* utmpl, unsigned int mode)
275 {
276 	utmpl->mode = mode;
277 
278 	return 0;
279 }
280 
xfrmnl_user_tmpl_get_share(struct xfrmnl_user_tmpl * utmpl)281 int xfrmnl_user_tmpl_get_share (struct xfrmnl_user_tmpl* utmpl)
282 {
283 	return utmpl->share;
284 }
285 
xfrmnl_user_tmpl_set_share(struct xfrmnl_user_tmpl * utmpl,unsigned int share)286 int xfrmnl_user_tmpl_set_share (struct xfrmnl_user_tmpl* utmpl, unsigned int share)
287 {
288 	utmpl->share = share;
289 
290 	return 0;
291 }
292 
xfrmnl_user_tmpl_get_optional(struct xfrmnl_user_tmpl * utmpl)293 int xfrmnl_user_tmpl_get_optional (struct xfrmnl_user_tmpl* utmpl)
294 {
295 	return utmpl->optional;
296 }
297 
xfrmnl_user_tmpl_set_optional(struct xfrmnl_user_tmpl * utmpl,unsigned int optional)298 int xfrmnl_user_tmpl_set_optional (struct xfrmnl_user_tmpl* utmpl, unsigned int optional)
299 {
300 	utmpl->optional = optional;
301 
302 	return 0;
303 }
304 
xfrmnl_user_tmpl_get_aalgos(struct xfrmnl_user_tmpl * utmpl)305 int xfrmnl_user_tmpl_get_aalgos (struct xfrmnl_user_tmpl* utmpl)
306 {
307 	return utmpl->aalgos;
308 }
309 
xfrmnl_user_tmpl_set_aalgos(struct xfrmnl_user_tmpl * utmpl,unsigned int aalgos)310 int xfrmnl_user_tmpl_set_aalgos (struct xfrmnl_user_tmpl* utmpl, unsigned int aalgos)
311 {
312 	utmpl->aalgos = aalgos;
313 
314 	return 0;
315 }
316 
xfrmnl_user_tmpl_get_ealgos(struct xfrmnl_user_tmpl * utmpl)317 int xfrmnl_user_tmpl_get_ealgos (struct xfrmnl_user_tmpl* utmpl)
318 {
319 	return utmpl->ealgos;
320 }
321 
xfrmnl_user_tmpl_set_ealgos(struct xfrmnl_user_tmpl * utmpl,unsigned int ealgos)322 int xfrmnl_user_tmpl_set_ealgos (struct xfrmnl_user_tmpl* utmpl, unsigned int ealgos)
323 {
324 	utmpl->ealgos = ealgos;
325 
326 	return 0;
327 }
328 
xfrmnl_user_tmpl_get_calgos(struct xfrmnl_user_tmpl * utmpl)329 int xfrmnl_user_tmpl_get_calgos (struct xfrmnl_user_tmpl* utmpl)
330 {
331 	return utmpl->calgos;
332 }
333 
xfrmnl_user_tmpl_set_calgos(struct xfrmnl_user_tmpl * utmpl,unsigned int calgos)334 int xfrmnl_user_tmpl_set_calgos (struct xfrmnl_user_tmpl* utmpl, unsigned int calgos)
335 {
336 	utmpl->calgos = calgos;
337 
338 	return 0;
339 }
340 
341 /** @} */
342