Debian Codegraph @

lib/xfrm/sp.c

raw
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 /**
37 * @ingroup xfrmnl
38 * @defgroup sp Security Policy
39 * @brief
40 */
41
42 #include "nl-default.h"
43
44 #include <time.h>
45 #include <netlink/netlink.h>
46 #include <netlink/cache.h>
47 #include <netlink/object.h>
48 #include <netlink/xfrm/selector.h>
49 #include <netlink/xfrm/lifetime.h>
50 #include <netlink/xfrm/template.h>
51 #include <netlink/xfrm/sp.h>
52
53 #include "nl-xfrm.h"
54 #include "nl-priv-dynamic-core/object-api.h"
55 #include "nl-priv-dynamic-core/nl-core.h"
56 #include "nl-priv-dynamic-core/cache-api.h"
57 #include "nl-aux-core/nl-core.h"
58 #include "nl-aux-xfrm/nl-xfrm.h"
59
60 struct xfrmnl_userpolicy_type {
61 uint8_t type;
62 uint16_t reserved1;
63 uint16_t reserved2;
64 };
65
66 struct xfrmnl_sp {
67 NLHDR_COMMON
68
69 struct xfrmnl_sel* sel;
70 struct xfrmnl_ltime_cfg* lft;
71 struct xfrmnl_lifetime_cur curlft;
72 uint32_t priority;
73 uint32_t index;
74 uint8_t dir;
75 uint8_t action;
76 uint8_t flags;
77 uint8_t share;
78 struct xfrmnl_user_sec_ctx* sec_ctx;
79 struct xfrmnl_userpolicy_type uptype;
80 uint32_t nr_user_tmpl;
81 struct nl_list_head usertmpl_list;
82 struct xfrmnl_mark mark;
83 uint32_t if_id;
84 };
85
86 /** @cond SKIP */
87 #define XFRM_SP_ATTR_SEL 0x01
88 #define XFRM_SP_ATTR_LTIME_CFG 0x02
89 #define XFRM_SP_ATTR_LTIME_CUR 0x04
90 #define XFRM_SP_ATTR_PRIO 0x08
91 #define XFRM_SP_ATTR_INDEX 0x10
92 #define XFRM_SP_ATTR_DIR 0x20
93 #define XFRM_SP_ATTR_ACTION 0x40
94 #define XFRM_SP_ATTR_FLAGS 0x80
95 #define XFRM_SP_ATTR_SHARE 0x100
96 #define XFRM_SP_ATTR_POLTYPE 0x200
97 #define XFRM_SP_ATTR_SECCTX 0x400
98 #define XFRM_SP_ATTR_TMPL 0x800
99 #define XFRM_SP_ATTR_MARK 0x1000
100 #define XFRM_SP_ATTR_IF_ID 0x2000
101
102 static struct nl_cache_ops xfrmnl_sp_ops;
103 static struct nl_object_ops xfrm_sp_obj_ops;
104 /** @endcond */
105
106 static void xfrm_sp_alloc_data(struct nl_object *c)
107 {
108 struct xfrmnl_sp* sp = nl_object_priv (c);
109
110 if ((sp->sel = xfrmnl_sel_alloc ()) == NULL)
111 return;
112
113 if ((sp->lft = xfrmnl_ltime_cfg_alloc ()) == NULL)
114 return;
115
116 nl_init_list_head(&sp->usertmpl_list);
117
118 return;
119 }
120
121 static void xfrm_sp_free_data(struct nl_object *c)
122 {
123 struct xfrmnl_sp* sp = nl_object_priv (c);
124 struct xfrmnl_user_tmpl *utmpl, *tmp;
125
126 if (sp == NULL)
127 return;
128
129 xfrmnl_sel_put (sp->sel);
130 xfrmnl_ltime_cfg_put (sp->lft);
131
132 if (sp->sec_ctx) {
133 free(sp->sec_ctx);
134 }
135
136 nl_list_for_each_entry_safe(utmpl, tmp, &sp->usertmpl_list, utmpl_list) {
137 xfrmnl_sp_remove_usertemplate (sp, utmpl);
138 xfrmnl_user_tmpl_free (utmpl);
139 }
140 }
141
142 static int xfrm_sp_clone(struct nl_object *_dst, struct nl_object *_src)
143 {
144 struct xfrmnl_sp* dst = nl_object_priv(_dst);
145 struct xfrmnl_sp* src = nl_object_priv(_src);
146 struct xfrmnl_user_tmpl *utmpl;
147 struct xfrmnl_user_tmpl *new;
148
149 dst->sel = NULL;
150 dst->lft = NULL;
151 dst->sec_ctx = NULL;
152 nl_init_list_head(&dst->usertmpl_list);
153
154 if (src->sel) {
155 if ((dst->sel = xfrmnl_sel_clone (src->sel)) == NULL)
156 return -NLE_NOMEM;
157 }
158
159 if (src->lft) {
160 if ((dst->lft = xfrmnl_ltime_cfg_clone (src->lft)) == NULL)
161 return -NLE_NOMEM;
162 }
163
164 if (src->sec_ctx) {
165 uint32_t len = sizeof (struct xfrmnl_user_sec_ctx) + src->sec_ctx->ctx_len;
166
167 if ((dst->sec_ctx = malloc (len)) == NULL)
168 return -NLE_NOMEM;
169 memcpy(dst->sec_ctx, src->sec_ctx, len);
170 }
171
172 nl_list_for_each_entry(utmpl, &src->usertmpl_list, utmpl_list) {
173 new = xfrmnl_user_tmpl_clone (utmpl);
174 if (!new)
175 return -NLE_NOMEM;
176 xfrmnl_sp_add_usertemplate(dst, new);
177 }
178
179 return 0;
180 }
181
182 static uint64_t xfrm_sp_compare(struct nl_object *_a, struct nl_object *_b,
183 uint64_t attrs, int flags)
184 {
185 struct xfrmnl_sp* a = (struct xfrmnl_sp *) _a;
186 struct xfrmnl_sp* b = (struct xfrmnl_sp *) _b;
187 struct xfrmnl_user_tmpl *tmpl_a, *tmpl_b;
188 uint64_t diff = 0;
189
190 #define _DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ATTR, a, b, EXPR)
191 diff |= _DIFF(XFRM_SP_ATTR_SEL, xfrmnl_sel_cmp(a->sel, b->sel));
192 diff |= _DIFF(XFRM_SP_ATTR_LTIME_CFG,
193 xfrmnl_ltime_cfg_cmp(a->lft, b->lft));
194 diff |= _DIFF(XFRM_SP_ATTR_PRIO, a->priority != b->priority);
195 diff |= _DIFF(XFRM_SP_ATTR_INDEX, a->index != b->index);
196 diff |= _DIFF(XFRM_SP_ATTR_DIR, a->dir != b->dir);
197 diff |= _DIFF(XFRM_SP_ATTR_ACTION, a->action != b->action);
198 diff |= _DIFF(XFRM_SP_ATTR_FLAGS, a->flags != b->flags);
199 diff |= _DIFF(XFRM_SP_ATTR_SHARE, a->share != b->share);
200 diff |= _DIFF(XFRM_SP_ATTR_SECCTX,
201 ((a->sec_ctx->len != b->sec_ctx->len) ||
202 (a->sec_ctx->exttype != b->sec_ctx->exttype) ||
203 (a->sec_ctx->ctx_alg != b->sec_ctx->ctx_alg) ||
204 (a->sec_ctx->ctx_doi != b->sec_ctx->ctx_doi) ||
205 (a->sec_ctx->ctx_len != b->sec_ctx->ctx_len) ||
206 strcmp(a->sec_ctx->ctx, b->sec_ctx->ctx)));
207 diff |= _DIFF(XFRM_SP_ATTR_POLTYPE, (a->uptype.type != b->uptype.type));
208 diff |= _DIFF(XFRM_SP_ATTR_TMPL, (a->nr_user_tmpl != b->nr_user_tmpl));
209 diff |= _DIFF(XFRM_SP_ATTR_MARK,
210 (a->mark.m != b->mark.m) || (a->mark.v != b->mark.v));
211 diff |= _DIFF(XFRM_SP_ATTR_IF_ID, a->if_id != b->if_id);
212
213 /* Compare the templates */
214 nl_list_for_each_entry(tmpl_b, &b->usertmpl_list, utmpl_list)
215 nl_list_for_each_entry(tmpl_a, &a->usertmpl_list, utmpl_list)
216 diff |= xfrmnl_user_tmpl_cmp (tmpl_a, tmpl_b);
217 #undef _DIFF
218
219 return diff;
220 }
221
222 /**
223 * @name XFRM SP Attribute Translations
224 * @{
225 */
226 static const struct trans_tbl sp_attrs[] = {
227 __ADD(XFRM_SP_ATTR_SEL, selector),
228 __ADD(XFRM_SP_ATTR_LTIME_CFG, lifetime_cfg),
229 __ADD(XFRM_SP_ATTR_LTIME_CUR, lifetime_cur),
230 __ADD(XFRM_SP_ATTR_PRIO, priority),
231 __ADD(XFRM_SP_ATTR_INDEX, index),
232 __ADD(XFRM_SP_ATTR_DIR, direction),
233 __ADD(XFRM_SP_ATTR_ACTION, action),
234 __ADD(XFRM_SP_ATTR_FLAGS, flags),
235 __ADD(XFRM_SP_ATTR_SHARE, share),
236 __ADD(XFRM_SP_ATTR_POLTYPE, policy_type),
237 __ADD(XFRM_SP_ATTR_SECCTX, security_context),
238 __ADD(XFRM_SP_ATTR_TMPL, user_template),
239 __ADD(XFRM_SP_ATTR_MARK, mark),
240 __ADD(XFRM_SP_ATTR_IF_ID, if_id),
241 };
242
243 static char* xfrm_sp_attrs2str(int attrs, char *buf, size_t len)
244 {
245 return __flags2str (attrs, buf, len, sp_attrs, ARRAY_SIZE(sp_attrs));
246 }
247 /** @} */
248
249 /**
250 * @name XFRM SP Action Translations
251 * @{
252 */
253 static const struct trans_tbl sa_actions[] = {
254 __ADD(XFRM_POLICY_ALLOW, allow),
255 __ADD(XFRM_POLICY_BLOCK, block),
256 };
257
258 char* xfrmnl_sp_action2str(int action, char *buf, size_t len)
259 {
260 return __type2str (action, buf, len, sa_actions, ARRAY_SIZE(sa_actions));
261 }
262
263 int xfrmnl_sp_str2action(const char *name)
264 {
265 return __str2type (name, sa_actions, ARRAY_SIZE(sa_actions));
266 }
267 /** @} */
268
269 /**
270 * @name XFRM SP Flags Translations
271 * @{
272 */
273 static const struct trans_tbl sp_flags[] = {
274 __ADD(XFRM_POLICY_LOCALOK, allow policy override by user),
275 __ADD(XFRM_POLICY_ICMP, auto include ICMP in policy),
276 };
277
278 char* xfrmnl_sp_flags2str(int flags, char *buf, size_t len)
279 {
280 return __flags2str (flags, buf, len, sp_flags, ARRAY_SIZE(sp_flags));
281 }
282
283 int xfrmnl_sp_str2flag(const char *name)
284 {
285 return __str2flags(name, sp_flags, ARRAY_SIZE(sp_flags));
286 }
287 /** @} */
288
289 /**
290 * @name XFRM SP Type Translations
291 * @{
292 */
293 static const struct trans_tbl sp_types[] = {
294 __ADD(XFRM_POLICY_TYPE_MAIN, main),
295 __ADD(XFRM_POLICY_TYPE_SUB, sub),
296 __ADD(XFRM_POLICY_TYPE_MAX, max),
297 __ADD(XFRM_POLICY_TYPE_ANY, any),
298 };
299
300 char* xfrmnl_sp_type2str(int type, char *buf, size_t len)
301 {
302 return __type2str(type, buf, len, sp_types, ARRAY_SIZE(sp_types));
303 }
304
305 int xfrmnl_sp_str2type(const char *name)
306 {
307 return __str2type(name, sp_types, ARRAY_SIZE(sp_types));
308 }
309 /** @} */
310
311 /**
312 * @name XFRM SP Direction Translations
313 * @{
314 */
315 static const struct trans_tbl sp_dir[] = {
316 __ADD(XFRM_POLICY_IN, in),
317 __ADD(XFRM_POLICY_OUT, out),
318 __ADD(XFRM_POLICY_FWD, fwd),
319 __ADD(XFRM_POLICY_MASK, mask),
320 };
321
322 char* xfrmnl_sp_dir2str(int dir, char *buf, size_t len)
323 {
324 return __type2str (dir, buf, len, sp_dir, ARRAY_SIZE(sp_dir));
325 }
326
327 int xfrmnl_sp_str2dir(const char *name)
328 {
329 return __str2type (name, sp_dir, ARRAY_SIZE(sp_dir));
330 }
331
332 int xfrmnl_sp_index2dir (unsigned int index)
333 {
334 return index & 0x7;
335 }
336 /** @} */
337
338 /**
339 * @name XFRM SP Share Translations
340 * @{
341 */
342 static const struct trans_tbl sp_share[] = {
343 __ADD(XFRM_SHARE_ANY, any),
344 __ADD(XFRM_SHARE_SESSION, session),
345 __ADD(XFRM_SHARE_USER, user),
346 __ADD(XFRM_SHARE_UNIQUE, unique),
347 };
348
349 char* xfrmnl_sp_share2str(int share, char *buf, size_t len)
350 {
351 return __type2str (share, buf, len, sp_share, ARRAY_SIZE(sp_share));
352 }
353
354 int xfrmnl_sp_str2share(const char *name)
355 {
356 return __str2type (name, sp_share, ARRAY_SIZE(sp_share));
357 }
358 /** @} */
359
360 static void xfrm_sp_dump_line(struct nl_object *a, struct nl_dump_params *p)
361 {
362 struct xfrmnl_sp* sp = (struct xfrmnl_sp *) a;
363 char dir[32], action[32], share[32], flags[32];
364 char dst[INET6_ADDRSTRLEN+5], src[INET6_ADDRSTRLEN+5];
365 time_t add_time, use_time;
366 struct tm *add_time_tm, *use_time_tm;
367 struct tm tm_buf;
368
369 nl_addr2str(xfrmnl_sel_get_saddr (sp->sel), src, sizeof(src));
370 nl_addr2str (xfrmnl_sel_get_daddr (sp->sel), dst, sizeof (dst));
371 nl_af2str (xfrmnl_sel_get_family (sp->sel), dir, 32);
372 nl_dump_line(p, "src %s dst %s family: %s\n", src, dst, dir);
373 nl_dump_line (p, "src port/mask: %d/%d dst port/mask: %d/%d\n",
374 xfrmnl_sel_get_dport (sp->sel), xfrmnl_sel_get_dportmask (sp->sel),
375 xfrmnl_sel_get_sport (sp->sel), xfrmnl_sel_get_sportmask (sp->sel));
376 nl_dump_line (p, "protocol: %s ifindex: %u uid: %u\n",
377 nl_ip_proto2str (xfrmnl_sel_get_proto (sp->sel), dir, sizeof(dir)),
378 xfrmnl_sel_get_ifindex (sp->sel),
379 xfrmnl_sel_get_userid (sp->sel));
380
381 xfrmnl_sp_dir2str (sp->dir, dir, 32);
382 xfrmnl_sp_action2str (sp->action, action, 32);
383 xfrmnl_sp_share2str (sp->share, share, 32);
384 xfrmnl_sp_flags2str (sp->flags, flags, 32);
385 nl_dump_line(p, "\tdir: %s action: %s index: %u priority: %u share: %s flags: %s(0x%x) \n",
386 dir, action, sp->index, sp->priority, share, flags, sp->flags);
387
388 nl_dump_line(p, "\tlifetime configuration: \n");
389 if (sp->lft->soft_byte_limit == XFRM_INF)
390 sprintf (dir, "INF");
391 else
392 sprintf (dir, "%" PRIu64, sp->lft->soft_byte_limit);
393 if (sp->lft->soft_packet_limit == XFRM_INF)
394 sprintf (action, "INF");
395 else
396 sprintf (action, "%" PRIu64, sp->lft->soft_packet_limit);
397 if (sp->lft->hard_byte_limit == XFRM_INF)
398 sprintf (flags, "INF");
399 else
400 sprintf (flags, "%" PRIu64, sp->lft->hard_byte_limit);
401 if (sp->lft->hard_packet_limit == XFRM_INF)
402 sprintf (share, "INF");
403 else
404 sprintf (share, "%" PRIu64, sp->lft->hard_packet_limit);
405 nl_dump_line(p, "\t\tsoft limit: %s (bytes), %s (packets) \n", dir,
406 action);
407 nl_dump_line(p, "\t\thard limit: %s (bytes), %s (packets) \n", flags,
408 share);
409 nl_dump_line(
410 p,
411 "\t\tsoft add_time: %llu (seconds), soft use_time: %llu (seconds) \n",
412 (long long unsigned)sp->lft->soft_add_expires_seconds,
413 (long long unsigned)sp->lft->soft_use_expires_seconds);
414 nl_dump_line(
415 p,
416 "\t\thard add_time: %llu (seconds), hard use_time: %llu (seconds) \n",
417 (long long unsigned)sp->lft->hard_add_expires_seconds,
418 (long long unsigned)sp->lft->hard_use_expires_seconds);
419
420 nl_dump_line(p, "\tlifetime current: \n");
421 nl_dump_line(p, "\t\t%llu bytes, %llu packets\n",
422 (long long unsigned)sp->curlft.bytes,
423 (long long unsigned)sp->curlft.packets);
424
425 if (sp->curlft.add_time != 0)
426 {
427 add_time = sp->curlft.add_time;
428 add_time_tm = gmtime_r (&add_time, &tm_buf);
429 strftime (dst, INET6_ADDRSTRLEN+5, "%Y-%m-%d %H-%M-%S", add_time_tm);
430 }
431 else
432 {
433 sprintf (dst, "%s", "-");
434 }
435
436 if (sp->curlft.use_time != 0)
437 {
438 use_time = sp->curlft.use_time;
439 use_time_tm = gmtime_r (&use_time, &tm_buf);
440 strftime (src, INET6_ADDRSTRLEN+5, "%Y-%m-%d %H-%M-%S", use_time_tm);
441 }
442 else
443 {
444 sprintf (src, "%s", "-");
445 }
446 nl_dump_line(p, "\t\tadd_time: %s, use_time: %s\n", dst, src);
447
448 if (sp->ce_mask & XFRM_SP_ATTR_SECCTX)
449 {
450 nl_dump_line(p, "\tUser security context: \n");
451 nl_dump_line(p, "\t\tlen: %d exttype: %d Algo: %d DOI: %d ctxlen: %d\n",
452 sp->sec_ctx->len, sp->sec_ctx->exttype,
453 sp->sec_ctx->ctx_alg, sp->sec_ctx->ctx_doi, sp->sec_ctx->ctx_len);
454 nl_dump_line (p, "\t\tctx: %s \n", sp->sec_ctx->ctx);
455 }
456
457 xfrmnl_sp_type2str (sp->uptype.type, flags, 32);
458 if (sp->ce_mask & XFRM_SP_ATTR_POLTYPE)
459 nl_dump_line(p, "\tUser policy type: %s\n", flags);
460
461 if (sp->ce_mask & XFRM_SP_ATTR_TMPL)
462 {
463 struct xfrmnl_user_tmpl* utmpl;
464
465 nl_dump_line(p, "\tUser template: \n");
466
467 nl_list_for_each_entry(utmpl, &sp->usertmpl_list, utmpl_list)
468 xfrmnl_user_tmpl_dump (utmpl, p);
469 }
470
471 if (sp->ce_mask & XFRM_SP_ATTR_MARK)
472 nl_dump_line(p, "\tMark mask: 0x%x Mark value: 0x%x\n", sp->mark.m, sp->mark.v);
473
474
475 if (sp->ce_mask & XFRM_SP_ATTR_IF_ID)
476 nl_dump_line(p, "\tXFRM interface ID: 0x%x\n", sp->if_id);
477
478 nl_dump(p, "\n");
479 }
480
481 static void xfrm_sp_dump_details(struct nl_object *a, struct nl_dump_params *p)
482 {
483 xfrm_sp_dump_line(a, p);
484 }
485
486 static void xfrm_sp_dump_stats(struct nl_object *a, struct nl_dump_params *p)
487 {
488 xfrm_sp_dump_details(a, p);
489
490 return;
491 }
492
493 /**
494 * @name XFRM SP Object Allocation/Freeage
495 * @{
496 */
497
498 struct xfrmnl_sp* xfrmnl_sp_alloc(void)
499 {
500 return (struct xfrmnl_sp*) nl_object_alloc(&xfrm_sp_obj_ops);
501 }
502
503 void xfrmnl_sp_put(struct xfrmnl_sp* sp)
504 {
505 nl_object_put((struct nl_object *) sp);
506 }
507
508 /** @} */
509
510 /**
511 * @name SP Cache Managament
512 * @{
513 */
514
515 /**
516 * Build a SP cache including all SPs currently configured in the kernel.
517 * @arg sock Netlink socket.
518 * @arg result Pointer to store resulting cache.
519 *
520 * Allocates a new SP cache, initializes it properly and updates it
521 * to include all SPs currently configured in the kernel.
522 *
523 * @return 0 on success or a negative error code.
524 */
525 int xfrmnl_sp_alloc_cache(struct nl_sock *sock, struct nl_cache **result)
526 {
527 return nl_cache_alloc_and_fill(&xfrmnl_sp_ops, sock, result);
528 }
529
530 /**
531 * Look up a SP by policy id and direction
532 * @arg cache SP cache
533 * @arg index Policy Id
534 * @arg dir direction
535 * @return sp handle or NULL if no match was found.
536 */
537 struct xfrmnl_sp* xfrmnl_sp_get(struct nl_cache* cache, unsigned int index, unsigned int dir)
538 {
539 struct xfrmnl_sp *sp;
540
541 //nl_list_for_each_entry(sp, &cache->c_items, ce_list) {
542 for (sp = (struct xfrmnl_sp*)nl_cache_get_first (cache);
543 sp != NULL;
544 sp = (struct xfrmnl_sp*)nl_cache_get_next ((struct nl_object*)sp))
545 {
546 if (sp->index == index && sp->dir == dir)
547 {
548 nl_object_get((struct nl_object *) sp);
549 return sp;
550 }
551 }
552
553 return NULL;
554 }
555
556
557 /** @} */
558
559
560 static struct nla_policy xfrm_sp_policy[XFRMA_MAX+1] = {
561 [XFRMA_POLICY] = { .minlen = sizeof(struct xfrm_userpolicy_info)},
562 [XFRMA_SEC_CTX] = { .minlen = sizeof(struct xfrm_sec_ctx) },
563 [XFRMA_TMPL] = { .minlen = sizeof(struct xfrm_user_tmpl) },
564 [XFRMA_POLICY_TYPE] = { .minlen = sizeof(struct xfrm_userpolicy_type)},
565 [XFRMA_MARK] = { .minlen = sizeof(struct xfrm_mark) },
566 [XFRMA_IF_ID] = { .type = NLA_U32 },
567 };
568
569 static int xfrm_sp_request_update(struct nl_cache *c, struct nl_sock *h)
570 {
571 return nl_send_simple (h, XFRM_MSG_GETPOLICY, NLM_F_DUMP, NULL, 0);
572 }
573
574 int xfrmnl_sp_parse(struct nlmsghdr *n, struct xfrmnl_sp **result)
575 {
576 _nl_auto_nl_addr struct nl_addr *addr1 = NULL;
577 _nl_auto_nl_addr struct nl_addr *addr2 = NULL;
578 _nl_auto_xfrmnl_sp struct xfrmnl_sp *sp = NULL;
579 struct nlattr *tb[XFRMA_MAX + 1];
580 struct xfrm_userpolicy_info *sp_info;
581 int len, err;
582
583 sp = xfrmnl_sp_alloc();
584 if (!sp)
585 return -NLE_NOMEM;
586
587 sp->ce_msgtype = n->nlmsg_type;
588 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
589 sp_info = (struct xfrm_userpolicy_info*)((char *)nlmsg_data(n) + sizeof (struct xfrm_userpolicy_id) + NLA_HDRLEN);
590 else
591 sp_info = nlmsg_data(n);
592
593 err = nlmsg_parse(n, sizeof(struct xfrm_userpolicy_info), tb, XFRMA_MAX, xfrm_sp_policy);
594 if (err < 0)
595 return err;
596
597 if (!(addr1 = _nl_addr_build(sp_info->sel.family, &sp_info->sel.daddr)))
598 return -NLE_NOMEM;
599 nl_addr_set_prefixlen (addr1, sp_info->sel.prefixlen_d);
600 xfrmnl_sel_set_daddr (sp->sel, addr1);
601 xfrmnl_sel_set_prefixlen_d (sp->sel, sp_info->sel.prefixlen_d);
602
603 if (!(addr2 = _nl_addr_build(sp_info->sel.family, &sp_info->sel.saddr)))
604 return -NLE_NOMEM;
605 nl_addr_set_prefixlen (addr2, sp_info->sel.prefixlen_s);
606 xfrmnl_sel_set_saddr (sp->sel, addr2);
607 xfrmnl_sel_set_prefixlen_s (sp->sel, sp_info->sel.prefixlen_s);
608
609 xfrmnl_sel_set_dport (sp->sel, ntohs (sp_info->sel.dport));
610 xfrmnl_sel_set_dportmask (sp->sel, ntohs (sp_info->sel.dport_mask));
611 xfrmnl_sel_set_sport (sp->sel, ntohs (sp_info->sel.sport));
612 xfrmnl_sel_set_sportmask (sp->sel, ntohs (sp_info->sel.sport_mask));
613 xfrmnl_sel_set_family (sp->sel, sp_info->sel.family);
614 xfrmnl_sel_set_proto (sp->sel, sp_info->sel.proto);
615 xfrmnl_sel_set_ifindex (sp->sel, sp_info->sel.ifindex);
616 xfrmnl_sel_set_userid (sp->sel, sp_info->sel.user);
617 sp->ce_mask |= XFRM_SP_ATTR_SEL;
618
619 sp->lft->soft_byte_limit = sp_info->lft.soft_byte_limit;
620 sp->lft->hard_byte_limit = sp_info->lft.hard_byte_limit;
621 sp->lft->soft_packet_limit = sp_info->lft.soft_packet_limit;
622 sp->lft->hard_packet_limit = sp_info->lft.hard_packet_limit;
623 sp->lft->soft_add_expires_seconds = sp_info->lft.soft_add_expires_seconds;
624 sp->lft->hard_add_expires_seconds = sp_info->lft.hard_add_expires_seconds;
625 sp->lft->soft_use_expires_seconds = sp_info->lft.soft_use_expires_seconds;
626 sp->lft->hard_use_expires_seconds = sp_info->lft.hard_use_expires_seconds;
627 sp->ce_mask |= XFRM_SP_ATTR_LTIME_CFG;
628
629 sp->curlft.bytes = sp_info->curlft.bytes;
630 sp->curlft.packets = sp_info->curlft.packets;
631 sp->curlft.add_time = sp_info->curlft.add_time;
632 sp->curlft.use_time = sp_info->curlft.use_time;
633 sp->ce_mask |= XFRM_SP_ATTR_LTIME_CUR;
634
635 sp->priority = sp_info->priority;
636 sp->index = sp_info->index;
637 sp->dir = sp_info->dir;
638 sp->action = sp_info->action;
639 sp->flags = sp_info->flags;
640 sp->share = sp_info->share;
641 sp->ce_mask |= (XFRM_SP_ATTR_PRIO | XFRM_SP_ATTR_INDEX |
642 XFRM_SP_ATTR_DIR | XFRM_SP_ATTR_ACTION |
643 XFRM_SP_ATTR_FLAGS | XFRM_SP_ATTR_SHARE);
644
645 if (tb[XFRMA_SEC_CTX]) {
646 struct xfrm_user_sec_ctx* ctx = nla_data(tb[XFRMA_SEC_CTX]);
647
648 len = sizeof (struct xfrmnl_user_sec_ctx) + ctx->ctx_len;
649 if ((sp->sec_ctx = calloc (1, len)) == NULL)
650 return -NLE_NOMEM;
651 memcpy ((void *)sp->sec_ctx, (void *)ctx, len);
652 sp->ce_mask |= XFRM_SP_ATTR_SECCTX;
653 }
654
655 if (tb[XFRMA_POLICY_TYPE]) {
656 struct xfrm_userpolicy_type* up = nla_data(tb[XFRMA_POLICY_TYPE]);
657
658 memcpy ((void *)&sp->uptype, (void *)up, sizeof (struct xfrm_userpolicy_type));
659 sp->ce_mask |= XFRM_SP_ATTR_POLTYPE;
660 }
661
662 if (tb[XFRMA_TMPL]) {
663 struct xfrm_user_tmpl* tmpl = nla_data(tb[XFRMA_TMPL]);
664 uint32_t i;
665 uint32_t num_tmpls = nla_len(tb[XFRMA_TMPL]) / sizeof (*tmpl);
666
667 for (i = 0; (i < num_tmpls) && (tmpl); i ++, tmpl++)
668 {
669 _nl_auto_xfrmnl_user_tmpl struct xfrmnl_user_tmpl *sputmpl = NULL;
670 _nl_auto_nl_addr struct nl_addr *addr1 = NULL;
671 _nl_auto_nl_addr struct nl_addr *addr2 = NULL;
672
673 if ((sputmpl = xfrmnl_user_tmpl_alloc ()) == NULL)
674 return -NLE_NOMEM;
675
676 if (!(addr1 = _nl_addr_build(tmpl->family, &tmpl->id.daddr)))
677 return -NLE_NOMEM;
678 xfrmnl_user_tmpl_set_daddr (sputmpl, addr1);
679 xfrmnl_user_tmpl_set_spi (sputmpl, ntohl(tmpl->id.spi));
680 xfrmnl_user_tmpl_set_proto (sputmpl, tmpl->id.proto);
681 xfrmnl_user_tmpl_set_family (sputmpl, tmpl->family);
682
683 if (!(addr2 = _nl_addr_build(tmpl->family, &tmpl->saddr)))
684 return -NLE_NOMEM;
685 xfrmnl_user_tmpl_set_saddr (sputmpl, addr2);
686
687 xfrmnl_user_tmpl_set_reqid (sputmpl, tmpl->reqid);
688 xfrmnl_user_tmpl_set_mode (sputmpl, tmpl->mode);
689 xfrmnl_user_tmpl_set_share (sputmpl, tmpl->share);
690 xfrmnl_user_tmpl_set_optional (sputmpl, tmpl->optional);
691 xfrmnl_user_tmpl_set_aalgos (sputmpl, tmpl->aalgos);
692 xfrmnl_user_tmpl_set_ealgos (sputmpl, tmpl->ealgos);
693 xfrmnl_user_tmpl_set_calgos (sputmpl, tmpl->calgos);
694 xfrmnl_sp_add_usertemplate (sp, _nl_steal_pointer(&sputmpl));
695
696 sp->ce_mask |= XFRM_SP_ATTR_TMPL;
697 }
698 }
699
700 if (tb[XFRMA_MARK]) {
701 struct xfrm_mark* m = nla_data(tb[XFRMA_MARK]);
702 sp->mark.m = m->m;
703 sp->mark.v = m->v;
704 sp->ce_mask |= XFRM_SP_ATTR_MARK;
705 }
706
707 if (tb[XFRMA_IF_ID]) {
708 sp->if_id = nla_get_u32(tb[XFRMA_IF_ID]);
709 sp->ce_mask |= XFRM_SP_ATTR_IF_ID;
710 }
711
712 *result = _nl_steal_pointer(&sp);
713 return 0;
714 }
715
716 static int xfrm_sp_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
717 struct nlmsghdr *n, struct nl_parser_param *pp)
718 {
719 struct xfrmnl_sp* sp;
720 int err;
721
722 if ((err = xfrmnl_sp_parse(n, &sp)) < 0)
723 {
724 printf ("received error: %d \n", err);
725 return err;
726 }
727
728 err = pp->pp_cb((struct nl_object *) sp, pp);
729
730 xfrmnl_sp_put(sp);
731 return err;
732 }
733
734 /**
735 * @name XFRM SP Get
736 * @{
737 */
738
739 int xfrmnl_sp_build_get_request(unsigned int index, unsigned int dir, unsigned int mark_v, unsigned int mark_m, struct nl_msg **result)
740 {
741 struct nl_msg *msg;
742 struct xfrm_userpolicy_id spid;
743 struct xfrm_mark mark;
744
745 memset(&spid, 0, sizeof(spid));
746 spid.index = index;
747 spid.dir = dir;
748
749 if (!(msg = nlmsg_alloc_simple(XFRM_MSG_GETPOLICY, 0)))
750 return -NLE_NOMEM;
751
752 if (nlmsg_append(msg, &spid, sizeof(spid), NLMSG_ALIGNTO) < 0)
753 goto nla_put_failure;
754
755 if ((mark_m & mark_v) != 0)
756 {
757 memset(&mark, 0, sizeof(struct xfrm_mark));
758 mark.m = mark_m;
759 mark.v = mark_v;
760
761 NLA_PUT (msg, XFRMA_MARK, sizeof (struct xfrm_mark), &mark);
762 }
763
764 *result = msg;
765 return 0;
766
767 nla_put_failure:
768 nlmsg_free(msg);
769 return -NLE_MSGSIZE;
770 }
771
772 int xfrmnl_sp_get_kernel(struct nl_sock* sock, unsigned int index, unsigned int dir, unsigned int mark_v, unsigned int mark_m, struct xfrmnl_sp** result)
773 {
774 struct nl_msg *msg = NULL;
775 struct nl_object *obj;
776 int err;
777
778 if ((err = xfrmnl_sp_build_get_request(index, dir, mark_m, mark_v, &msg)) < 0)
779 return err;
780
781 err = nl_send_auto(sock, msg);
782 nlmsg_free(msg);
783 if (err < 0)
784 return err;
785
786 if ((err = nl_pickup(sock, &xfrm_sp_msg_parser, &obj)) < 0)
787 return err;
788
789 /* We have used xfrm_sp_msg_parser(), object is definitely a xfrm ae */
790 *result = (struct xfrmnl_sp *) obj;
791
792 /* If an object has been returned, we also need to wait for the ACK */
793 if (err == 0 && obj)
794 nl_wait_for_ack(sock);
795
796 return 0;
797 }
798
799 /** @} */
800
801 static int build_xfrm_sp_message(struct xfrmnl_sp *tmpl, int cmd, int flags, struct nl_msg **result)
802 {
803 struct nl_msg* msg;
804 struct xfrm_userpolicy_info sp_info;
805 uint32_t len;
806 struct nl_addr* addr;
807
808 if (!(tmpl->ce_mask & XFRM_SP_ATTR_DIR) ||
809 (!(tmpl->ce_mask & XFRM_SP_ATTR_INDEX) &&
810 !(tmpl->ce_mask & XFRM_SP_ATTR_SEL)))
811 return -NLE_MISSING_ATTR;
812
813 memset ((void*)&sp_info, 0, sizeof (sp_info));
814 if (tmpl->ce_mask & XFRM_SP_ATTR_SEL)
815 {
816 addr = xfrmnl_sel_get_daddr (tmpl->sel);
817 memcpy ((void*)&sp_info.sel.daddr, (void*)nl_addr_get_binary_addr (addr), sizeof (uint8_t) * nl_addr_get_len (addr));
818 addr = xfrmnl_sel_get_saddr (tmpl->sel);
819 memcpy ((void*)&sp_info.sel.saddr, (void*)nl_addr_get_binary_addr (addr), sizeof (uint8_t) * nl_addr_get_len (addr));
820 sp_info.sel.dport = htons (xfrmnl_sel_get_dport (tmpl->sel));
821 sp_info.sel.dport_mask = htons (xfrmnl_sel_get_dportmask (tmpl->sel));
822 sp_info.sel.sport = htons (xfrmnl_sel_get_sport (tmpl->sel));
823 sp_info.sel.sport_mask = htons (xfrmnl_sel_get_sportmask (tmpl->sel));
824 sp_info.sel.family = xfrmnl_sel_get_family (tmpl->sel);
825 sp_info.sel.prefixlen_d = xfrmnl_sel_get_prefixlen_d (tmpl->sel);
826 sp_info.sel.prefixlen_s = xfrmnl_sel_get_prefixlen_s (tmpl->sel);
827 sp_info.sel.proto = xfrmnl_sel_get_proto (tmpl->sel);
828 sp_info.sel.ifindex = xfrmnl_sel_get_ifindex (tmpl->sel);
829 sp_info.sel.user = xfrmnl_sel_get_userid (tmpl->sel);
830 }
831
832 if (tmpl->ce_mask & XFRM_SP_ATTR_LTIME_CFG)
833 {
834 sp_info.lft.soft_byte_limit = xfrmnl_ltime_cfg_get_soft_bytelimit (tmpl->lft);
835 sp_info.lft.hard_byte_limit = xfrmnl_ltime_cfg_get_hard_bytelimit (tmpl->lft);
836 sp_info.lft.soft_packet_limit = xfrmnl_ltime_cfg_get_soft_packetlimit (tmpl->lft);
837 sp_info.lft.hard_packet_limit = xfrmnl_ltime_cfg_get_hard_packetlimit (tmpl->lft);
838 sp_info.lft.soft_add_expires_seconds = xfrmnl_ltime_cfg_get_soft_addexpires (tmpl->lft);
839 sp_info.lft.hard_add_expires_seconds = xfrmnl_ltime_cfg_get_hard_addexpires (tmpl->lft);
840 sp_info.lft.soft_use_expires_seconds = xfrmnl_ltime_cfg_get_soft_useexpires (tmpl->lft);
841 sp_info.lft.hard_use_expires_seconds = xfrmnl_ltime_cfg_get_hard_useexpires (tmpl->lft);
842 }
843
844 //Skip current lifetime: cur lifetime can be updated only via AE
845
846 if (tmpl->ce_mask & XFRM_SP_ATTR_PRIO)
847 sp_info.priority = tmpl->priority;
848
849 if (tmpl->ce_mask & XFRM_SP_ATTR_INDEX)
850 sp_info.index = tmpl->index;
851
852 if (tmpl->ce_mask & XFRM_SP_ATTR_DIR)
853 sp_info.dir = tmpl->dir;
854
855 if (tmpl->ce_mask & XFRM_SP_ATTR_ACTION)
856 sp_info.action = tmpl->action;
857
858 if (tmpl->ce_mask & XFRM_SP_ATTR_FLAGS)
859 sp_info.flags = tmpl->flags;
860
861 if (tmpl->ce_mask & XFRM_SP_ATTR_SHARE)
862 sp_info.share = tmpl->share;
863
864 msg = nlmsg_alloc_simple(cmd, flags);
865 if (!msg)
866 return -NLE_NOMEM;
867
868 if (nlmsg_append(msg, &sp_info, sizeof(sp_info), NLMSG_ALIGNTO) < 0)
869 goto nla_put_failure;
870
871 if (tmpl->ce_mask & XFRM_SP_ATTR_SECCTX) {
872 len = (sizeof (struct xfrm_user_sec_ctx)) + tmpl->sec_ctx->ctx_len;
873 NLA_PUT (msg, XFRMA_SEC_CTX, len, tmpl->sec_ctx);
874 }
875
876 if (tmpl->ce_mask & XFRM_SP_ATTR_POLTYPE) {
877 len = sizeof (struct xfrm_userpolicy_type);
878 NLA_PUT (msg, XFRMA_POLICY_TYPE, len, &tmpl->uptype);
879 }
880
881 if (tmpl->ce_mask & XFRM_SP_ATTR_TMPL) {
882 struct nlattr* tmpls;
883 struct xfrmnl_user_tmpl* utmpl;
884 struct nl_addr* addr;
885
886 if (!(tmpls = nla_nest_start(msg, XFRMA_TMPL)))
887 goto nla_put_failure;
888
889 nl_list_for_each_entry(utmpl, &tmpl->usertmpl_list, utmpl_list) {
890 struct xfrm_user_tmpl* tmpl;
891
892 tmpl = nlmsg_reserve(msg, sizeof(*tmpl), NLMSG_ALIGNTO);
893 if (!tmpl)
894 goto nla_put_failure;
895 addr = xfrmnl_user_tmpl_get_daddr (utmpl);
896 memcpy ((void *)&tmpl->id.daddr, nl_addr_get_binary_addr (addr),
897 nl_addr_get_len (addr));
898 tmpl->id.spi = htonl(xfrmnl_user_tmpl_get_spi (utmpl));
899 tmpl->id.proto = xfrmnl_user_tmpl_get_proto (utmpl);
900 tmpl->family = xfrmnl_user_tmpl_get_family (utmpl);
901 addr = xfrmnl_user_tmpl_get_saddr (utmpl);
902 memcpy ((void *)&tmpl->saddr, nl_addr_get_binary_addr (addr),
903 nl_addr_get_len (addr));
904 tmpl->reqid = xfrmnl_user_tmpl_get_reqid (utmpl);
905 tmpl->mode = xfrmnl_user_tmpl_get_mode (utmpl);
906 tmpl->share = xfrmnl_user_tmpl_get_share (utmpl);
907 tmpl->optional = xfrmnl_user_tmpl_get_optional (utmpl);
908 tmpl->aalgos = xfrmnl_user_tmpl_get_aalgos (utmpl);
909 tmpl->ealgos = xfrmnl_user_tmpl_get_ealgos (utmpl);
910 tmpl->calgos = xfrmnl_user_tmpl_get_calgos (utmpl);
911 }
912 nla_nest_end(msg, tmpls);
913 }
914
915 if (tmpl->ce_mask & XFRM_SP_ATTR_MARK) {
916 NLA_PUT (msg, XFRMA_MARK, sizeof (struct xfrm_mark), &tmpl->mark);
917 }
918
919 if (tmpl->ce_mask & XFRM_SP_ATTR_IF_ID) {
920 NLA_PUT_U32 (msg, XFRMA_IF_ID, tmpl->if_id);
921 }
922
923 *result = msg;
924 return 0;
925
926 nla_put_failure:
927 nlmsg_free(msg);
928 return -NLE_MSGSIZE;
929 }
930
931 /**
932 * @name XFRM SP Add
933 * @{
934 */
935
936 int xfrmnl_sp_build_add_request(struct xfrmnl_sp* tmpl, int flags, struct nl_msg **result)
937 {
938 return build_xfrm_sp_message (tmpl, XFRM_MSG_NEWPOLICY, flags, result);
939 }
940
941 int xfrmnl_sp_add(struct nl_sock* sk, struct xfrmnl_sp* tmpl, int flags)
942 {
943 int err;
944 struct nl_msg *msg;
945
946 if ((err = xfrmnl_sp_build_add_request(tmpl, flags, &msg)) < 0)
947 return err;
948
949 err = nl_send_auto_complete(sk, msg);
950 nlmsg_free(msg);
951 if (err < 0)
952 return err;
953
954 return nl_wait_for_ack(sk);
955 }
956
957 /**
958 * @name XFRM SP Update
959 * @{
960 */
961
962 int xfrmnl_sp_build_update_request(struct xfrmnl_sp* tmpl, int flags, struct nl_msg **result)
963 {
964 return build_xfrm_sp_message (tmpl, XFRM_MSG_UPDPOLICY, flags, result);
965 }
966
967 int xfrmnl_sp_update(struct nl_sock* sk, struct xfrmnl_sp* tmpl, int flags)
968 {
969 int err;
970 struct nl_msg *msg;
971
972 if ((err = xfrmnl_sp_build_update_request(tmpl, flags, &msg)) < 0)
973 return err;
974
975 err = nl_send_auto_complete(sk, msg);
976 nlmsg_free(msg);
977 if (err < 0)
978 return err;
979
980 return nl_wait_for_ack(sk);
981 }
982
983 /** @} */
984
985 /**
986 * \brief Builds a xfrm_sp_delete_message. Uses either index and direction
987 * or security-context (not set is a valid value), selector and
988 * direction for identification.
989 * Returns error if necessary values aren't set.
990 *
991 * \param tmpl The policy template.
992 * \param cmd The command. Should be XFRM_MSG_DELPOLICY.
993 * \param flags Additional flags
994 * \param result Resulting message.
995 *
996 * \return 0 if successful, else error value < 0
997 */
998 static int build_xfrm_sp_delete_message(struct xfrmnl_sp *tmpl, int cmd, int flags, struct nl_msg **result)
999 {
1000 struct nl_msg* msg;
1001 struct xfrm_userpolicy_id spid;
1002 struct nl_addr* addr;
1003 uint32_t len;
1004
1005 if (!(tmpl->ce_mask & XFRM_SP_ATTR_DIR) ||
1006 (!(tmpl->ce_mask & XFRM_SP_ATTR_INDEX) &&
1007 !(tmpl->ce_mask & XFRM_SP_ATTR_SEL)))
1008 return -NLE_MISSING_ATTR;
1009
1010 memset(&spid, 0, sizeof(spid));
1011 spid.dir = tmpl->dir;
1012 if(tmpl->ce_mask & XFRM_SP_ATTR_INDEX)
1013 spid.index = tmpl->index;
1014
1015 if (tmpl->ce_mask & XFRM_SP_ATTR_SEL)
1016 {
1017 addr = xfrmnl_sel_get_daddr (tmpl->sel);
1018 memcpy ((void*)&spid.sel.daddr, (void*)nl_addr_get_binary_addr (addr), sizeof (uint8_t) * nl_addr_get_len (addr));
1019 addr = xfrmnl_sel_get_saddr (tmpl->sel);
1020 memcpy ((void*)&spid.sel.saddr, (void*)nl_addr_get_binary_addr (addr), sizeof (uint8_t) * nl_addr_get_len (addr));
1021 spid.sel.dport = htons (xfrmnl_sel_get_dport (tmpl->sel));
1022 spid.sel.dport_mask = htons (xfrmnl_sel_get_dportmask (tmpl->sel));
1023 spid.sel.sport = htons (xfrmnl_sel_get_sport (tmpl->sel));
1024 spid.sel.sport_mask = htons (xfrmnl_sel_get_sportmask (tmpl->sel));
1025 spid.sel.family = xfrmnl_sel_get_family (tmpl->sel);
1026 spid.sel.prefixlen_d = xfrmnl_sel_get_prefixlen_d (tmpl->sel);
1027 spid.sel.prefixlen_s = xfrmnl_sel_get_prefixlen_s (tmpl->sel);
1028 spid.sel.proto = xfrmnl_sel_get_proto (tmpl->sel);
1029 spid.sel.ifindex = xfrmnl_sel_get_ifindex (tmpl->sel);
1030 spid.sel.user = xfrmnl_sel_get_userid (tmpl->sel);
1031 }
1032
1033 msg = nlmsg_alloc_simple(cmd, flags);
1034 if (!msg)
1035 return -NLE_NOMEM;
1036
1037 if (nlmsg_append(msg, &spid, sizeof(spid), NLMSG_ALIGNTO) < 0)
1038 goto nla_put_failure;
1039
1040 if (tmpl->ce_mask & XFRM_SP_ATTR_SECCTX) {
1041 len = (sizeof (struct xfrm_user_sec_ctx)) + tmpl->sec_ctx->ctx_len;
1042 NLA_PUT (msg, XFRMA_SEC_CTX, len, tmpl->sec_ctx);
1043 }
1044
1045 if (tmpl->ce_mask & XFRM_SP_ATTR_MARK) {
1046 len = sizeof (struct xfrm_mark);
1047 NLA_PUT (msg, XFRMA_MARK, len, &tmpl->mark);
1048 }
1049
1050 if (tmpl->ce_mask & XFRM_SP_ATTR_IF_ID) {
1051 NLA_PUT_U32 (msg, XFRMA_IF_ID, tmpl->if_id);
1052 }
1053
1054 *result = msg;
1055 return 0;
1056
1057 nla_put_failure:
1058 nlmsg_free(msg);
1059 return -NLE_MSGSIZE;
1060 }
1061
1062 /**
1063 * @name XFRM SA Delete
1064 * @{
1065 */
1066
1067 int xfrmnl_sp_build_delete_request(struct xfrmnl_sp* tmpl, int flags, struct nl_msg **result)
1068 {
1069 return build_xfrm_sp_delete_message (tmpl, XFRM_MSG_DELPOLICY, flags, result);
1070 }
1071
1072 int xfrmnl_sp_delete(struct nl_sock* sk, struct xfrmnl_sp* tmpl, int flags)
1073 {
1074 int err;
1075 struct nl_msg *msg;
1076
1077 if ((err = xfrmnl_sp_build_delete_request(tmpl, flags, &msg)) < 0)
1078 return err;
1079
1080 err = nl_send_auto_complete(sk, msg);
1081 nlmsg_free(msg);
1082 if (err < 0)
1083 return err;
1084
1085 return nl_wait_for_ack(sk);
1086 }
1087
1088 /** @} */
1089
1090
1091 /**
1092 * @name Attributes
1093 * @{
1094 */
1095
1096 struct xfrmnl_sel* xfrmnl_sp_get_sel (struct xfrmnl_sp* sp)
1097 {
1098 if (sp->ce_mask & XFRM_SP_ATTR_SEL)
1099 return sp->sel;
1100 else
1101 return NULL;
1102 }
1103
1104 int xfrmnl_sp_set_sel (struct xfrmnl_sp* sp, struct xfrmnl_sel* sel)
1105 {
1106 /* Release any previously held selector object from the SP */
1107 if (sp->sel)
1108 xfrmnl_sel_put (sp->sel);
1109
1110 /* Increment ref count on new selector and save it in the SP */
1111 xfrmnl_sel_get (sel);
1112 sp->sel = sel;
1113 sp->ce_mask |= XFRM_SP_ATTR_SEL;
1114
1115 return 0;
1116 }
1117
1118 struct xfrmnl_ltime_cfg* xfrmnl_sp_get_lifetime_cfg (struct xfrmnl_sp* sp)
1119 {
1120 if (sp->ce_mask & XFRM_SP_ATTR_LTIME_CFG)
1121 return sp->lft;
1122 else
1123 return NULL;
1124 }
1125
1126 int xfrmnl_sp_set_lifetime_cfg (struct xfrmnl_sp* sp, struct xfrmnl_ltime_cfg* ltime)
1127 {
1128 /* Release any previously held lifetime cfg object from the SP */
1129 if (sp->lft)
1130 xfrmnl_ltime_cfg_put (sp->lft);
1131
1132 /* Increment ref count on new lifetime object and save it in the SP */
1133 xfrmnl_ltime_cfg_get (ltime);
1134 sp->lft = ltime;
1135 sp->ce_mask |= XFRM_SP_ATTR_LTIME_CFG;
1136
1137 return 0;
1138 }
1139
1140 int xfrmnl_sp_get_curlifetime (struct xfrmnl_sp* sp, unsigned long long int* curr_bytes,
1141 unsigned long long int* curr_packets, unsigned long long int* curr_add_time, unsigned long long int* curr_use_time)
1142 {
1143 if (sp == NULL || curr_bytes == NULL || curr_packets == NULL || curr_add_time == NULL || curr_use_time == NULL)
1144 return -1;
1145
1146 *curr_bytes = sp->curlft.bytes;
1147 *curr_packets = sp->curlft.packets;
1148 *curr_add_time = sp->curlft.add_time;
1149 *curr_use_time = sp->curlft.use_time;
1150
1151 return 0;
1152 }
1153
1154 int xfrmnl_sp_get_priority (struct xfrmnl_sp* sp)
1155 {
1156 if (sp->ce_mask & XFRM_SP_ATTR_PRIO)
1157 return sp->priority;
1158 else
1159 return -1;
1160 }
1161
1162 int xfrmnl_sp_set_priority (struct xfrmnl_sp* sp, unsigned int prio)
1163 {
1164 sp->priority = prio;
1165 sp->ce_mask |= XFRM_SP_ATTR_PRIO;
1166
1167 return 0;
1168 }
1169
1170 int xfrmnl_sp_get_index (struct xfrmnl_sp* sp)
1171 {
1172 if (sp->ce_mask & XFRM_SP_ATTR_INDEX)
1173 return sp->index;
1174 else
1175 return -1;
1176 }
1177
1178 int xfrmnl_sp_set_index (struct xfrmnl_sp* sp, unsigned int index)
1179 {
1180 sp->index = index;
1181 sp->ce_mask |= XFRM_SP_ATTR_INDEX;
1182
1183 return 0;
1184 }
1185
1186 int xfrmnl_sp_get_dir (struct xfrmnl_sp* sp)
1187 {
1188 if (sp->ce_mask & XFRM_SP_ATTR_DIR)
1189 return sp->dir;
1190 else
1191 return -1;
1192 }
1193
1194 int xfrmnl_sp_set_dir (struct xfrmnl_sp* sp, unsigned int dir)
1195 {
1196 sp->dir = dir;
1197 sp->ce_mask |= XFRM_SP_ATTR_DIR;
1198
1199 return 0;
1200 }
1201
1202 int xfrmnl_sp_get_action (struct xfrmnl_sp* sp)
1203 {
1204 if (sp->ce_mask & XFRM_SP_ATTR_ACTION)
1205 return sp->action;
1206 else
1207 return -1;
1208 }
1209
1210 int xfrmnl_sp_set_action (struct xfrmnl_sp* sp, unsigned int action)
1211 {
1212 sp->action = action;
1213 sp->ce_mask |= XFRM_SP_ATTR_ACTION;
1214
1215 return 0;
1216 }
1217
1218 int xfrmnl_sp_get_flags (struct xfrmnl_sp* sp)
1219 {
1220 if (sp->ce_mask & XFRM_SP_ATTR_FLAGS)
1221 return sp->flags;
1222 else
1223 return -1;
1224 }
1225
1226 int xfrmnl_sp_set_flags (struct xfrmnl_sp* sp, unsigned int flags)
1227 {
1228 sp->flags = flags;
1229 sp->ce_mask |= XFRM_SP_ATTR_FLAGS;
1230
1231 return 0;
1232 }
1233
1234 int xfrmnl_sp_get_share (struct xfrmnl_sp* sp)
1235 {
1236 if (sp->ce_mask & XFRM_SP_ATTR_SHARE)
1237 return sp->share;
1238 else
1239 return -1;
1240 }
1241
1242 int xfrmnl_sp_set_share (struct xfrmnl_sp* sp, unsigned int share)
1243 {
1244 sp->share = share;
1245 sp->ce_mask |= XFRM_SP_ATTR_SHARE;
1246
1247 return 0;
1248 }
1249
1250 /**
1251 * Get the security context.
1252 *
1253 * @arg sp The xfrmnl_sp object.
1254 * @arg len An optional output value for the ctx_str length including the xfrmnl_sp header.
1255 * @arg exttype An optional output value.
1256 * @arg alg An optional output value for the security context algorithm.
1257 * @arg doi An optional output value for the security context domain of interpretation.
1258 * @arg ctx_len An optional output value for the security context length, including the
1259 * terminating null byte ('\0').
1260 * @arg ctx_str An optional buffer large enough for the security context string. It must
1261 * contain at least @ctx_len bytes. You are advised to create the ctx_str
1262 * buffer one element larger and ensure NUL termination yourself.
1263 *
1264 * Warning: you must ensure that @ctx_str is large enough. If you don't know the length before-hand,
1265 * call xfrmnl_sp_get_sec_ctx() without @ctx_str argument to query only the required buffer size.
1266 * This modified API is available in all versions of libnl3 that support the capability
1267 * @def NL_CAPABILITY_XFRM_SP_SEC_CTX_LEN (@see nl_has_capability for further information).
1268 *
1269 * @return 0 on success or a negative error code.
1270 */
1271 int xfrmnl_sp_get_sec_ctx (struct xfrmnl_sp* sp, unsigned int* len, unsigned int* exttype, unsigned int* alg, unsigned int* doi, unsigned int* ctx_len, char* ctx_str)
1272 {
1273 if (sp->ce_mask & XFRM_SP_ATTR_SECCTX)
1274 {
1275 if (len)
1276 *len = sizeof (struct xfrmnl_user_sec_ctx) + sp->sec_ctx->ctx_len;
1277 if (exttype)
1278 *exttype = sp->sec_ctx->exttype;
1279 if (alg)
1280 *alg = sp->sec_ctx->ctx_alg;
1281 if (doi)
1282 *doi = sp->sec_ctx->ctx_doi;
1283 if (ctx_len)
1284 *ctx_len = sp->sec_ctx->ctx_len;
1285 if (ctx_str)
1286 memcpy ((void *)ctx_str, (void *)sp->sec_ctx->ctx, sp->sec_ctx->ctx_len);
1287 }
1288 else
1289 return -1;
1290
1291 return 0;
1292 }
1293 /**
1294 * @brief Set security context (ctx_str) for XFRM Polixy.
1295 *
1296 * @param sp XFRM Policy
1297 * @param len !!! depricated unused parameter !!!
1298 * @param exttype netlink message attribute - probably XFRMA_SEC_CTX
1299 * @param alg security context algorithm
1300 * @param doi security context domain interpretation
1301 * @param ctx_len Length of the context string.
1302 * @param ctx_str The context string.
1303 *
1304 * @return 0 if sucessfull, else -1
1305 */
1306 int xfrmnl_sp_set_sec_ctx (struct xfrmnl_sp* sp, unsigned int len, unsigned int exttype, unsigned int alg, unsigned int doi, unsigned int ctx_len, char* ctx_str)
1307 {
1308 /* Free up the old context string and allocate new one */
1309 if (sp->sec_ctx)
1310 free (sp->sec_ctx);
1311 if ((sp->sec_ctx = calloc (1, sizeof (struct xfrmnl_user_sec_ctx) + 1 + ctx_len)) == NULL)
1312 return -1;
1313
1314 /* Save the new info */
1315 sp->sec_ctx->len = sizeof (struct xfrmnl_user_sec_ctx) + ctx_len;
1316 sp->sec_ctx->exttype = exttype;
1317 sp->sec_ctx->ctx_alg = alg;
1318 sp->sec_ctx->ctx_doi = doi;
1319 sp->sec_ctx->ctx_len = ctx_len;
1320 memcpy ((void *)sp->sec_ctx->ctx, (void *)ctx_str, ctx_len);
1321 sp->sec_ctx->ctx[ctx_len] = '\0';
1322
1323 sp->ce_mask |= XFRM_SP_ATTR_SECCTX;
1324
1325 return 0;
1326 }
1327
1328 int xfrmnl_sp_get_userpolicy_type (struct xfrmnl_sp* sp)
1329 {
1330 if (sp->ce_mask & XFRM_SP_ATTR_POLTYPE)
1331 return sp->uptype.type;
1332 else
1333 return -1;
1334 }
1335
1336 int xfrmnl_sp_set_userpolicy_type (struct xfrmnl_sp* sp, unsigned int type)
1337 {
1338 sp->uptype.type = type;
1339 sp->ce_mask |= XFRM_SP_ATTR_POLTYPE;
1340
1341 return 0;
1342 }
1343
1344 void xfrmnl_sp_add_usertemplate(struct xfrmnl_sp *sp, struct xfrmnl_user_tmpl *utmpl)
1345 {
1346 nl_list_add_tail(&utmpl->utmpl_list, &sp->usertmpl_list);
1347 sp->nr_user_tmpl++;
1348 sp->ce_mask |= XFRM_SP_ATTR_TMPL;
1349 }
1350
1351 void xfrmnl_sp_remove_usertemplate(struct xfrmnl_sp *sp, struct xfrmnl_user_tmpl *utmpl)
1352 {
1353 if (sp->ce_mask & XFRM_SP_ATTR_TMPL) {
1354 sp->nr_user_tmpl--;
1355 nl_list_del(&utmpl->utmpl_list);
1356 if (sp->nr_user_tmpl == 0)
1357 sp->ce_mask &= ~XFRM_SP_ATTR_TMPL;
1358 }
1359 }
1360
1361 struct nl_list_head *xfrmnl_sp_get_usertemplates(struct xfrmnl_sp *sp)
1362 {
1363 if (sp->ce_mask & XFRM_SP_ATTR_TMPL)
1364 return &sp->usertmpl_list;
1365
1366 return NULL;
1367 }
1368
1369 int xfrmnl_sp_get_nusertemplates(struct xfrmnl_sp *sp)
1370 {
1371 if (sp->ce_mask & XFRM_SP_ATTR_TMPL)
1372 return sp->nr_user_tmpl;
1373
1374 return 0;
1375 }
1376
1377 void xfrmnl_sp_foreach_usertemplate(struct xfrmnl_sp *r,
1378 void (*cb)(struct xfrmnl_user_tmpl *, void *),
1379 void *arg)
1380 {
1381 struct xfrmnl_user_tmpl *utmpl;
1382
1383 if (r->ce_mask & XFRM_SP_ATTR_TMPL) {
1384 nl_list_for_each_entry(utmpl, &r->usertmpl_list, utmpl_list) {
1385 cb(utmpl, arg);
1386 }
1387 }
1388 }
1389
1390 struct xfrmnl_user_tmpl *xfrmnl_sp_usertemplate_n(struct xfrmnl_sp *r, int n)
1391 {
1392 struct xfrmnl_user_tmpl *utmpl;
1393
1394 if (r->ce_mask & XFRM_SP_ATTR_TMPL && n >= 0 &&
1395 ((unsigned)n) < r->nr_user_tmpl) {
1396 uint32_t i;
1397
1398 i = 0;
1399 nl_list_for_each_entry(utmpl, &r->usertmpl_list, utmpl_list) {
1400 if (i == ((unsigned)n))
1401 return utmpl;
1402 i++;
1403 }
1404 }
1405 return NULL;
1406 }
1407
1408 int xfrmnl_sp_get_mark (struct xfrmnl_sp* sp, unsigned int* mark_mask, unsigned int* mark_value)
1409 {
1410 if (mark_mask == NULL || mark_value == NULL)
1411 return -1;
1412
1413 if (sp->ce_mask & XFRM_SP_ATTR_MARK)
1414 {
1415 *mark_mask = sp->mark.m;
1416 *mark_value = sp->mark.v;
1417
1418 return 0;
1419 }
1420 else
1421 return -1;
1422 }
1423
1424 int xfrmnl_sp_set_mark (struct xfrmnl_sp* sp, unsigned int value, unsigned int mask)
1425 {
1426 sp->mark.v = value;
1427 sp->mark.m = mask;
1428 sp->ce_mask |= XFRM_SP_ATTR_MARK;
1429
1430 return 0;
1431 }
1432
1433 int xfrmnl_sp_get_if_id (struct xfrmnl_sp* sp, uint32_t* if_id)
1434 {
1435 if (if_id == NULL)
1436 return -NLE_INVAL;
1437
1438 if (!(sp->ce_mask & XFRM_SP_ATTR_IF_ID))
1439 return -NLE_NOATTR;
1440
1441 *if_id = sp->if_id;
1442
1443 return 0;
1444 }
1445
1446 int xfrmnl_sp_set_if_id (struct xfrmnl_sp* sp, uint32_t if_id)
1447 {
1448 sp->if_id = if_id;
1449 sp->ce_mask |= XFRM_SP_ATTR_IF_ID;
1450
1451 return 0;
1452 }
1453
1454 /** @} */
1455
1456 static struct nl_object_ops xfrm_sp_obj_ops = {
1457 .oo_name = "xfrm/sp",
1458 .oo_size = sizeof(struct xfrmnl_sp),
1459 .oo_constructor = xfrm_sp_alloc_data,
1460 .oo_free_data = xfrm_sp_free_data,
1461 .oo_clone = xfrm_sp_clone,
1462 .oo_dump = {
1463 [NL_DUMP_LINE] = xfrm_sp_dump_line,
1464 [NL_DUMP_DETAILS] = xfrm_sp_dump_details,
1465 [NL_DUMP_STATS] = xfrm_sp_dump_stats,
1466 },
1467 .oo_compare = xfrm_sp_compare,
1468 .oo_attrs2str = xfrm_sp_attrs2str,
1469 .oo_id_attrs = (XFRM_SP_ATTR_SEL | XFRM_SP_ATTR_INDEX | XFRM_SP_ATTR_DIR),
1470 };
1471
1472 static struct nl_af_group xfrm_sp_groups[] = {
1473 { AF_UNSPEC, XFRMNLGRP_POLICY },
1474 { END_OF_GROUP_LIST },
1475 };
1476
1477 static struct nl_cache_ops xfrmnl_sp_ops = {
1478 .co_name = "xfrm/sp",
1479 .co_hdrsize = sizeof(struct xfrm_userpolicy_info),
1480 .co_msgtypes = {
1481 { XFRM_MSG_NEWPOLICY, NL_ACT_NEW, "new" },
1482 { XFRM_MSG_DELPOLICY, NL_ACT_DEL, "del" },
1483 { XFRM_MSG_GETPOLICY, NL_ACT_GET, "get" },
1484 { XFRM_MSG_UPDPOLICY, NL_ACT_NEW, "update" },
1485 END_OF_MSGTYPES_LIST,
1486 },
1487 .co_protocol = NETLINK_XFRM,
1488 .co_groups = xfrm_sp_groups,
1489 .co_request_update = xfrm_sp_request_update,
1490 .co_msg_parser = xfrm_sp_msg_parser,
1491 .co_obj_ops = &xfrm_sp_obj_ops,
1492 };
1493
1494 /**
1495 * @name XFRM SA Cache Managament
1496 * @{
1497 */
1498
1499 static void _nl_init xfrm_sp_init(void)
1500 {
1501 nl_cache_mngt_register(&xfrmnl_sp_ops);
1502 }
1503
1504 static void _nl_exit xfrm_sp_exit(void)
1505 {
1506 nl_cache_mngt_unregister(&xfrmnl_sp_ops);
1507 }
1508
1509 /** @} */