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