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