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