41 #include <netlink-private/netlink.h> 42 #include <netlink/netlink.h> 43 #include <netlink/cache.h> 44 #include <netlink/object.h> 45 #include <netlink/xfrm/selector.h> 46 #include <netlink/xfrm/lifetime.h> 47 #include <netlink/xfrm/template.h> 48 #include <netlink/xfrm/sp.h> 51 #define XFRM_SP_ATTR_SEL 0x01 52 #define XFRM_SP_ATTR_LTIME_CFG 0x02 53 #define XFRM_SP_ATTR_LTIME_CUR 0x04 54 #define XFRM_SP_ATTR_PRIO 0x08 55 #define XFRM_SP_ATTR_INDEX 0x10 56 #define XFRM_SP_ATTR_DIR 0x20 57 #define XFRM_SP_ATTR_ACTION 0x40 58 #define XFRM_SP_ATTR_FLAGS 0x80 59 #define XFRM_SP_ATTR_SHARE 0x100 60 #define XFRM_SP_ATTR_POLTYPE 0x200 61 #define XFRM_SP_ATTR_SECCTX 0x400 62 #define XFRM_SP_ATTR_TMPL 0x800 63 #define XFRM_SP_ATTR_MARK 0x1000 65 static struct nl_cache_ops xfrmnl_sp_ops;
66 static struct nl_object_ops xfrm_sp_obj_ops;
69 static void xfrm_sp_alloc_data(
struct nl_object *c)
71 struct xfrmnl_sp* sp = nl_object_priv (c);
79 nl_init_list_head(&sp->usertmpl_list);
84 static void xfrm_sp_free_data(
struct nl_object *c)
86 struct xfrmnl_sp* sp = nl_object_priv (c);
87 struct xfrmnl_user_tmpl *utmpl, *tmp;
92 xfrmnl_sel_put (sp->sel);
93 xfrmnl_ltime_cfg_put (sp->lft);
100 nl_list_for_each_entry_safe(utmpl, tmp, &sp->usertmpl_list, utmpl_list) {
101 xfrmnl_sp_remove_usertemplate (sp, utmpl);
102 xfrmnl_user_tmpl_free (utmpl);
106 static int xfrm_sp_clone(
struct nl_object *_dst,
struct nl_object *_src)
108 struct xfrmnl_sp* dst = nl_object_priv(_dst);
109 struct xfrmnl_sp* src = nl_object_priv(_src);
111 struct xfrmnl_user_tmpl *utmpl, *
new;
123 len =
sizeof (
struct xfrmnl_user_sec_ctx) + src->sec_ctx->ctx_len;
124 if ((dst->sec_ctx = calloc (1, len)) == NULL)
126 memcpy ((
void *)dst->sec_ctx, (
void *)src->sec_ctx, len);
129 nl_init_list_head(&dst->usertmpl_list);
130 nl_list_for_each_entry(utmpl, &src->usertmpl_list, utmpl_list) {
135 xfrmnl_sp_add_usertemplate(dst,
new);
141 static uint64_t xfrm_sp_compare(
struct nl_object *_a,
struct nl_object *_b,
142 uint64_t attrs,
int flags)
144 struct xfrmnl_sp* a = (
struct xfrmnl_sp *) _a;
145 struct xfrmnl_sp* b = (
struct xfrmnl_sp *) _b;
146 struct xfrmnl_user_tmpl *tmpl_a, *tmpl_b;
149 #define XFRM_SP_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, XFRM_SP_ATTR_##ATTR, a, b, EXPR) 152 diff |= XFRM_SP_DIFF(PRIO, a->priority != b->priority);
153 diff |= XFRM_SP_DIFF(INDEX, a->index != b->index);
154 diff |= XFRM_SP_DIFF(DIR, a->dir != b->dir);
155 diff |= XFRM_SP_DIFF(ACTION, a->action != b->action);
156 diff |= XFRM_SP_DIFF(FLAGS, a->flags != b->flags);
157 diff |= XFRM_SP_DIFF(SHARE, a->share != b->share);
158 diff |= XFRM_SP_DIFF(SECCTX,((a->sec_ctx->len != b->sec_ctx->len) ||
159 (a->sec_ctx->exttype != b->sec_ctx->exttype) ||
160 (a->sec_ctx->ctx_alg != b->sec_ctx->ctx_alg) ||
161 (a->sec_ctx->ctx_doi != b->sec_ctx->ctx_doi) ||
162 (a->sec_ctx->ctx_len != b->sec_ctx->ctx_len) ||
163 strcmp(a->sec_ctx->ctx, b->sec_ctx->ctx)));
164 diff |= XFRM_SP_DIFF(POLTYPE,(a->uptype.type != b->uptype.type));
165 diff |= XFRM_SP_DIFF(TMPL,(a->nr_user_tmpl != b->nr_user_tmpl));
166 diff |= XFRM_SP_DIFF(MARK,(a->mark.m != b->mark.m) ||
167 (a->mark.v != b->mark.v));
170 nl_list_for_each_entry(tmpl_b, &b->usertmpl_list, utmpl_list)
171 nl_list_for_each_entry(tmpl_a, &a->usertmpl_list, utmpl_list)
182 static const struct trans_tbl sp_attrs[] = {
183 __ADD(XFRM_SP_ATTR_SEL, selector),
184 __ADD(XFRM_SP_ATTR_LTIME_CFG, lifetime_cfg),
185 __ADD(XFRM_SP_ATTR_LTIME_CUR, lifetime_cur),
186 __ADD(XFRM_SP_ATTR_PRIO, priority),
187 __ADD(XFRM_SP_ATTR_INDEX, index),
188 __ADD(XFRM_SP_ATTR_DIR, direction),
189 __ADD(XFRM_SP_ATTR_ACTION, action),
190 __ADD(XFRM_SP_ATTR_FLAGS, flags),
191 __ADD(XFRM_SP_ATTR_SHARE, share),
192 __ADD(XFRM_SP_ATTR_POLTYPE, policy_type),
193 __ADD(XFRM_SP_ATTR_SECCTX, security_context),
194 __ADD(XFRM_SP_ATTR_TMPL, user_template),
195 __ADD(XFRM_SP_ATTR_MARK, mark),
198 static char* xfrm_sp_attrs2str(
int attrs,
char *buf,
size_t len)
200 return __flags2str (attrs, buf, len, sp_attrs, ARRAY_SIZE(sp_attrs));
208 static const struct trans_tbl sa_actions[] = {
209 __ADD(XFRM_POLICY_ALLOW, allow),
210 __ADD(XFRM_POLICY_BLOCK, block),
213 char* xfrmnl_sp_action2str(
int action,
char *buf,
size_t len)
215 return __type2str (action, buf, len, sa_actions, ARRAY_SIZE(sa_actions));
218 int xfrmnl_sp_str2action(
const char *name)
220 return __str2type (name, sa_actions, ARRAY_SIZE(sa_actions));
228 static const struct trans_tbl sp_flags[] = {
229 __ADD(XFRM_POLICY_LOCALOK, allow policy
override by user),
230 __ADD(XFRM_POLICY_ICMP,
auto include ICMP in policy),
233 char* xfrmnl_sp_flags2str(
int flags,
char *buf,
size_t len)
235 return __flags2str (flags, buf, len, sp_flags, ARRAY_SIZE(sp_flags));
238 int xfrmnl_sp_str2flag(
const char *name)
240 return __str2flags(name, sp_flags, ARRAY_SIZE(sp_flags));
248 static const struct trans_tbl sp_types[] = {
249 __ADD(XFRM_POLICY_TYPE_MAIN, main),
250 __ADD(XFRM_POLICY_TYPE_SUB, sub),
251 __ADD(XFRM_POLICY_TYPE_MAX, max),
252 __ADD(XFRM_POLICY_TYPE_ANY, any),
255 char* xfrmnl_sp_type2str(
int type,
char *buf,
size_t len)
257 return __type2str(type, buf, len, sp_types, ARRAY_SIZE(sp_types));
260 int xfrmnl_sp_str2type(
const char *name)
262 return __str2type(name, sp_types, ARRAY_SIZE(sp_types));
270 static const struct trans_tbl sp_dir[] = {
271 __ADD(XFRM_POLICY_IN, in),
272 __ADD(XFRM_POLICY_OUT, out),
273 __ADD(XFRM_POLICY_FWD, fwd),
274 __ADD(XFRM_POLICY_MASK, mask),
277 char* xfrmnl_sp_dir2str(
int dir,
char *buf,
size_t len)
279 return __type2str (dir, buf, len, sp_dir, ARRAY_SIZE(sp_dir));
282 int xfrmnl_sp_str2dir(
const char *name)
284 return __str2type (name, sp_dir, ARRAY_SIZE(sp_dir));
287 int xfrmnl_sp_index2dir (
unsigned int index)
297 static const struct trans_tbl sp_share[] = {
298 __ADD(XFRM_SHARE_ANY, any),
299 __ADD(XFRM_SHARE_SESSION, session),
300 __ADD(XFRM_SHARE_USER, user),
301 __ADD(XFRM_SHARE_UNIQUE, unique),
304 char* xfrmnl_sp_share2str(
int share,
char *buf,
size_t len)
306 return __type2str (share, buf, len, sp_share, ARRAY_SIZE(sp_share));
309 int xfrmnl_sp_str2share(
const char *name)
311 return __str2type (name, sp_share, ARRAY_SIZE(sp_share));
315 static void xfrm_sp_dump_line(
struct nl_object *a,
struct nl_dump_params *p)
317 struct xfrmnl_sp* sp = (
struct xfrmnl_sp *) a;
318 char dir[32], action[32], share[32], flags[32];
319 char dst[INET6_ADDRSTRLEN+5], src[INET6_ADDRSTRLEN+5];
320 time_t add_time, use_time;
321 struct tm *add_time_tm, *use_time_tm;
323 nl_addr2str(xfrmnl_sel_get_saddr (sp->sel), src,
sizeof(src));
324 nl_addr2str (xfrmnl_sel_get_daddr (sp->sel), dst, sizeof (dst));
325 nl_af2str (xfrmnl_sel_get_family (sp->sel), dir, 32);
326 nl_dump_line(p,
"src %s dst %s family: %s\n", src, dst, dir);
327 nl_dump_line (p,
"src port/mask: %d/%d dst port/mask: %d/%d\n",
328 xfrmnl_sel_get_dport (sp->sel), xfrmnl_sel_get_dportmask (sp->sel),
329 xfrmnl_sel_get_sport (sp->sel), xfrmnl_sel_get_sportmask (sp->sel));
330 nl_dump_line (p,
"protocol: %s ifindex: %u uid: %u\n",
331 nl_ip_proto2str (xfrmnl_sel_get_proto (sp->sel), dir,
sizeof(dir)),
332 xfrmnl_sel_get_ifindex (sp->sel),
333 xfrmnl_sel_get_userid (sp->sel));
335 xfrmnl_sp_dir2str (sp->dir, dir, 32);
336 xfrmnl_sp_action2str (sp->action, action, 32);
337 xfrmnl_sp_share2str (sp->share, share, 32);
338 xfrmnl_sp_flags2str (sp->flags, flags, 32);
339 nl_dump_line(p,
"\tdir: %s action: %s index: %u priority: %u share: %s flags: %s(0x%x) \n",
340 dir, action, sp->index, sp->priority, share, flags, sp->flags);
342 nl_dump_line(p,
"\tlifetime configuration: \n");
343 if (sp->lft->soft_byte_limit == XFRM_INF)
344 sprintf (dir,
"INF");
346 sprintf (dir,
"%" PRIu64, sp->lft->soft_byte_limit);
347 if (sp->lft->soft_packet_limit == XFRM_INF)
348 sprintf (action,
"INF");
350 sprintf (action,
"%" PRIu64, sp->lft->soft_packet_limit);
351 if (sp->lft->hard_byte_limit == XFRM_INF)
352 sprintf (flags,
"INF");
354 sprintf (flags,
"%" PRIu64, sp->lft->hard_byte_limit);
355 if (sp->lft->hard_packet_limit == XFRM_INF)
356 sprintf (share,
"INF");
358 sprintf (share,
"%" PRIu64, sp->lft->hard_packet_limit);
359 nl_dump_line(p,
"\t\tsoft limit: %s (bytes), %s (packets) \n", dir, action);
360 nl_dump_line(p,
"\t\thard limit: %s (bytes), %s (packets) \n", flags, share);
361 nl_dump_line(p,
"\t\tsoft add_time: %llu (seconds), soft use_time: %llu (seconds) \n",
362 sp->lft->soft_add_expires_seconds, sp->lft->soft_use_expires_seconds);
363 nl_dump_line(p,
"\t\thard add_time: %llu (seconds), hard use_time: %llu (seconds) \n",
364 sp->lft->hard_add_expires_seconds, sp->lft->hard_use_expires_seconds);
366 nl_dump_line(p,
"\tlifetime current: \n");
367 nl_dump_line(p,
"\t\t%llu bytes, %llu packets\n", sp->curlft.bytes, sp->curlft.packets);
369 if (sp->curlft.add_time != 0)
371 add_time = sp->curlft.add_time;
372 add_time_tm = gmtime (&add_time);
373 strftime (dst, INET6_ADDRSTRLEN+5,
"%Y-%m-%d %H-%M-%S", add_time_tm);
377 sprintf (dst,
"%s",
"-");
380 if (sp->curlft.use_time != 0)
382 use_time = sp->curlft.use_time;
383 use_time_tm = gmtime (&use_time);
384 strftime (src, INET6_ADDRSTRLEN+5,
"%Y-%m-%d %H-%M-%S", use_time_tm);
388 sprintf (src,
"%s",
"-");
390 nl_dump_line(p,
"\t\tadd_time: %s, use_time: %s\n", dst, src);
392 if (sp->ce_mask & XFRM_SP_ATTR_SECCTX)
394 nl_dump_line(p,
"\tUser security context: \n");
395 nl_dump_line(p,
"\t\tlen: %d exttype: %d Algo: %d DOI: %d ctxlen: %d\n",
396 sp->sec_ctx->len, sp->sec_ctx->exttype,
397 sp->sec_ctx->ctx_alg, sp->sec_ctx->ctx_doi, sp->sec_ctx->ctx_len);
398 nl_dump_line (p,
"\t\tctx: %s \n", sp->sec_ctx->ctx);
401 xfrmnl_sp_type2str (sp->uptype.type, flags, 32);
402 if (sp->ce_mask & XFRM_SP_ATTR_POLTYPE)
403 nl_dump_line(p,
"\tUser policy type: %s\n", flags);
405 if (sp->ce_mask & XFRM_SP_ATTR_TMPL)
407 struct xfrmnl_user_tmpl* utmpl;
409 nl_dump_line(p,
"\tUser template: \n");
411 nl_list_for_each_entry(utmpl, &sp->usertmpl_list, utmpl_list)
412 xfrmnl_user_tmpl_dump (utmpl, p);
415 if (sp->ce_mask & XFRM_SP_ATTR_MARK)
416 nl_dump_line(p, "\tMark mask: 0x%x Mark value: 0x%x\n", sp->mark.m, sp->mark.v);
421 static
void xfrm_sp_dump_details(struct nl_object *a, struct
nl_dump_params *p)
423 xfrm_sp_dump_line(a, p);
426 static void xfrm_sp_dump_stats(
struct nl_object *a,
struct nl_dump_params *p)
428 xfrm_sp_dump_details(a, p);
438 struct xfrmnl_sp* xfrmnl_sp_alloc(
void)
443 void xfrmnl_sp_put(
struct xfrmnl_sp* sp)
465 int xfrmnl_sp_alloc_cache(
struct nl_sock *sock,
struct nl_cache **result)
477 struct xfrmnl_sp* xfrmnl_sp_get(
struct nl_cache* cache,
unsigned int index,
unsigned int dir)
479 struct xfrmnl_sp *sp;
486 if (sp->index == index && sp->dir == dir)
500 static struct nla_policy xfrm_sp_policy[XFRMA_MAX+1] = {
501 [XFRMA_POLICY] = { .
minlen =
sizeof(
struct xfrm_userpolicy_info)},
502 [XFRMA_SEC_CTX] = { .minlen =
sizeof(
struct xfrm_sec_ctx) },
503 [XFRMA_TMPL] = { .minlen =
sizeof(
struct xfrm_user_tmpl) },
504 [XFRMA_POLICY_TYPE] = { .minlen =
sizeof(
struct xfrm_userpolicy_type)},
505 [XFRMA_MARK] = { .minlen =
sizeof(
struct xfrm_mark) },
508 static int xfrm_sp_request_update(
struct nl_cache *c,
struct nl_sock *h)
510 struct xfrm_userpolicy_id sp_id;
512 memset (&sp_id, 0,
sizeof (sp_id));
514 &sp_id,
sizeof (sp_id));
517 int xfrmnl_sp_parse(
struct nlmsghdr *n,
struct xfrmnl_sp **result)
519 struct xfrmnl_sp *sp;
520 struct nlattr *tb[XFRMA_MAX + 1];
521 struct xfrm_userpolicy_info *sp_info;
523 struct nl_addr* addr;
525 sp = xfrmnl_sp_alloc();
531 sp->ce_msgtype = n->nlmsg_type;
532 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
534 sp_info = (
struct xfrm_userpolicy_info*)(
nlmsg_data(n) +
sizeof (
struct xfrm_userpolicy_id) + NLA_HDRLEN);
541 err =
nlmsg_parse(n,
sizeof(
struct xfrm_userpolicy_info), tb, XFRMA_MAX, xfrm_sp_policy);
544 printf (
"parse error: %d \n", err);
548 if (sp_info->sel.family == AF_INET)
549 addr =
nl_addr_build (sp_info->sel.family, &sp_info->sel.daddr.a4, sizeof (sp_info->sel.daddr.a4));
551 addr =
nl_addr_build (sp_info->sel.family, &sp_info->sel.daddr.a6, sizeof (sp_info->sel.daddr.a6));
553 xfrmnl_sel_set_daddr (sp->sel, addr);
554 xfrmnl_sel_set_prefixlen_d (sp->sel, sp_info->sel.prefixlen_d);
556 if (sp_info->sel.family == AF_INET)
557 addr =
nl_addr_build (sp_info->sel.family, &sp_info->sel.saddr.a4, sizeof (sp_info->sel.saddr.a4));
559 addr =
nl_addr_build (sp_info->sel.family, &sp_info->sel.saddr.a6, sizeof (sp_info->sel.saddr.a6));
561 xfrmnl_sel_set_saddr (sp->sel, addr);
562 xfrmnl_sel_set_prefixlen_s (sp->sel, sp_info->sel.prefixlen_s);
564 xfrmnl_sel_set_dport (sp->sel, ntohs (sp_info->sel.dport));
565 xfrmnl_sel_set_dportmask (sp->sel, ntohs (sp_info->sel.dport_mask));
566 xfrmnl_sel_set_sport (sp->sel, ntohs (sp_info->sel.sport));
567 xfrmnl_sel_set_sportmask (sp->sel, ntohs (sp_info->sel.sport_mask));
568 xfrmnl_sel_set_family (sp->sel, sp_info->sel.family);
569 xfrmnl_sel_set_proto (sp->sel, sp_info->sel.proto);
570 xfrmnl_sel_set_ifindex (sp->sel, sp_info->sel.ifindex);
571 xfrmnl_sel_set_userid (sp->sel, sp_info->sel.user);
572 sp->ce_mask |= XFRM_SP_ATTR_SEL;
574 sp->lft->soft_byte_limit = sp_info->lft.soft_byte_limit;
575 sp->lft->hard_byte_limit = sp_info->lft.hard_byte_limit;
576 sp->lft->soft_packet_limit = sp_info->lft.soft_packet_limit;
577 sp->lft->hard_packet_limit = sp_info->lft.hard_packet_limit;
578 sp->lft->soft_add_expires_seconds = sp_info->lft.soft_add_expires_seconds;
579 sp->lft->hard_add_expires_seconds = sp_info->lft.hard_add_expires_seconds;
580 sp->lft->soft_use_expires_seconds = sp_info->lft.soft_use_expires_seconds;
581 sp->lft->hard_use_expires_seconds = sp_info->lft.hard_use_expires_seconds;
582 sp->ce_mask |= XFRM_SP_ATTR_LTIME_CFG;
584 sp->curlft.bytes = sp_info->curlft.bytes;
585 sp->curlft.packets = sp_info->curlft.packets;
586 sp->curlft.add_time = sp_info->curlft.add_time;
587 sp->curlft.use_time = sp_info->curlft.use_time;
588 sp->ce_mask |= XFRM_SP_ATTR_LTIME_CUR;
590 sp->priority = sp_info->priority;
591 sp->index = sp_info->index;
592 sp->dir = sp_info->dir;
593 sp->action = sp_info->action;
594 sp->flags = sp_info->flags;
595 sp->share = sp_info->share;
596 sp->ce_mask |= (XFRM_SP_ATTR_PRIO | XFRM_SP_ATTR_INDEX |
597 XFRM_SP_ATTR_DIR | XFRM_SP_ATTR_ACTION |
598 XFRM_SP_ATTR_FLAGS | XFRM_SP_ATTR_SHARE);
600 if (tb[XFRMA_SEC_CTX]) {
601 struct xfrm_user_sec_ctx* ctx =
nla_data(tb[XFRMA_SEC_CTX]);
602 len =
sizeof (
struct xfrmnl_user_sec_ctx) + ctx->ctx_len;
603 if ((sp->sec_ctx = calloc (1, len)) == NULL)
608 memcpy ((
void *)sp->sec_ctx, (
void *)ctx, len);
609 sp->ce_mask |= XFRM_SP_ATTR_SECCTX;
612 if (tb[XFRMA_POLICY_TYPE]) {
613 struct xfrm_userpolicy_type* up =
nla_data(tb[XFRMA_POLICY_TYPE]);
614 memcpy ((
void *)&sp->uptype, (
void *)up, sizeof (
struct xfrm_userpolicy_type));
615 sp->ce_mask |= XFRM_SP_ATTR_POLTYPE;
618 if (tb[XFRMA_TMPL]) {
619 struct xfrm_user_tmpl* tmpl =
nla_data(tb[XFRMA_TMPL]);
620 struct xfrmnl_user_tmpl* sputmpl;
622 uint32_t num_tmpls =
nla_len(tb[XFRMA_TMPL]) /
sizeof (*tmpl);
623 struct nl_addr* addr;
625 for (i = 0; (i < num_tmpls) && (tmpl); i ++, tmpl++)
633 if (tmpl->family == AF_INET)
634 addr =
nl_addr_build(tmpl->family, &tmpl->id.daddr.a4, sizeof (tmpl->id.daddr.a4));
636 addr =
nl_addr_build(tmpl->family, &tmpl->id.daddr.a6, sizeof (tmpl->id.daddr.a6));
637 xfrmnl_user_tmpl_set_daddr (sputmpl, addr);
638 xfrmnl_user_tmpl_set_spi (sputmpl, ntohl(tmpl->id.spi));
639 xfrmnl_user_tmpl_set_proto (sputmpl, tmpl->id.proto);
640 xfrmnl_user_tmpl_set_family (sputmpl, tmpl->family);
642 if (tmpl->family == AF_INET)
643 addr =
nl_addr_build(tmpl->family, &tmpl->saddr.a4, sizeof (tmpl->saddr.a4));
645 addr =
nl_addr_build(tmpl->family, &tmpl->saddr.a6, sizeof (tmpl->saddr.a6));
646 xfrmnl_user_tmpl_set_saddr (sputmpl, addr);
648 xfrmnl_user_tmpl_set_reqid (sputmpl, tmpl->reqid);
649 xfrmnl_user_tmpl_set_mode (sputmpl, tmpl->mode);
650 xfrmnl_user_tmpl_set_share (sputmpl, tmpl->share);
651 xfrmnl_user_tmpl_set_optional (sputmpl, tmpl->optional);
652 xfrmnl_user_tmpl_set_aalgos (sputmpl, tmpl->aalgos);
653 xfrmnl_user_tmpl_set_ealgos (sputmpl, tmpl->ealgos);
654 xfrmnl_user_tmpl_set_calgos (sputmpl, tmpl->calgos);
655 xfrmnl_sp_add_usertemplate (sp, sputmpl);
657 sp->ce_mask |= XFRM_SP_ATTR_TMPL;
661 if (tb[XFRMA_MARK]) {
662 struct xfrm_mark* m =
nla_data(tb[XFRMA_MARK]);
665 sp->ce_mask |= XFRM_SP_ATTR_MARK;
676 static int xfrm_sp_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
677 struct nlmsghdr *n,
struct nl_parser_param *pp)
679 struct xfrmnl_sp* sp;
682 if ((err = xfrmnl_sp_parse(n, &sp)) < 0)
684 printf (
"received error: %d \n", err);
688 err = pp->pp_cb((
struct nl_object *) sp, pp);
699 int xfrmnl_sp_build_get_request(
unsigned int index,
unsigned int dir,
unsigned int mark_v,
unsigned int mark_m,
struct nl_msg **result)
702 struct xfrm_userpolicy_id spid;
703 struct xfrm_mark mark;
705 memset(&spid, 0,
sizeof(spid));
712 if (
nlmsg_append(msg, &spid,
sizeof(spid), NLMSG_ALIGNTO) < 0)
713 goto nla_put_failure;
715 if ((mark_m & mark_v) != 0)
717 memset(&mark, 0,
sizeof(
struct xfrm_mark));
721 NLA_PUT (msg, XFRMA_MARK,
sizeof (
struct xfrm_mark), &mark);
732 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)
734 struct nl_msg *msg = NULL;
735 struct nl_object *obj;
738 if ((err = xfrmnl_sp_build_get_request(index, dir, mark_m, mark_v, &msg)) < 0)
746 if ((err =
nl_pickup(sock, &xfrm_sp_msg_parser, &obj)) < 0)
750 *result = (
struct xfrmnl_sp *) obj;
761 static int build_xfrm_sp_message(
struct xfrmnl_sp *tmpl,
int cmd,
int flags,
struct nl_msg **result)
764 struct xfrm_userpolicy_info sp_info;
766 struct nl_addr* addr;
768 if (!(tmpl->ce_mask & XFRM_SP_ATTR_INDEX) ||
769 !(tmpl->ce_mask & XFRM_SP_ATTR_DIR))
770 return -NLE_MISSING_ATTR;
772 memset ((
void*)&sp_info, 0,
sizeof (sp_info));
773 if (tmpl->ce_mask & XFRM_SP_ATTR_SEL)
775 addr = xfrmnl_sel_get_daddr (tmpl->sel);
777 addr = xfrmnl_sel_get_saddr (tmpl->sel);
779 sp_info.sel.dport = htons (xfrmnl_sel_get_dport (tmpl->sel));
780 sp_info.sel.dport_mask = htons (xfrmnl_sel_get_dportmask (tmpl->sel));
781 sp_info.sel.sport = htons (xfrmnl_sel_get_sport (tmpl->sel));
782 sp_info.sel.sport_mask = htons (xfrmnl_sel_get_sportmask (tmpl->sel));
783 sp_info.sel.family = xfrmnl_sel_get_family (tmpl->sel);
784 sp_info.sel.prefixlen_d = xfrmnl_sel_get_prefixlen_d (tmpl->sel);
785 sp_info.sel.prefixlen_s = xfrmnl_sel_get_prefixlen_s (tmpl->sel);
786 sp_info.sel.proto = xfrmnl_sel_get_proto (tmpl->sel);
787 sp_info.sel.ifindex = xfrmnl_sel_get_ifindex (tmpl->sel);
788 sp_info.sel.user = xfrmnl_sel_get_userid (tmpl->sel);
791 if (tmpl->ce_mask & XFRM_SP_ATTR_LTIME_CFG)
793 sp_info.lft.soft_byte_limit = xfrmnl_ltime_cfg_get_soft_bytelimit (tmpl->lft);
794 sp_info.lft.hard_byte_limit = xfrmnl_ltime_cfg_get_hard_bytelimit (tmpl->lft);
795 sp_info.lft.soft_packet_limit = xfrmnl_ltime_cfg_get_soft_packetlimit (tmpl->lft);
796 sp_info.lft.hard_packet_limit = xfrmnl_ltime_cfg_get_hard_packetlimit (tmpl->lft);
797 sp_info.lft.soft_add_expires_seconds = xfrmnl_ltime_cfg_get_soft_addexpires (tmpl->lft);
798 sp_info.lft.hard_add_expires_seconds = xfrmnl_ltime_cfg_get_hard_addexpires (tmpl->lft);
799 sp_info.lft.soft_use_expires_seconds = xfrmnl_ltime_cfg_get_soft_useexpires (tmpl->lft);
800 sp_info.lft.hard_use_expires_seconds = xfrmnl_ltime_cfg_get_hard_useexpires (tmpl->lft);
805 if (tmpl->ce_mask & XFRM_SP_ATTR_PRIO)
806 sp_info.priority = tmpl->priority;
808 if (tmpl->ce_mask & XFRM_SP_ATTR_INDEX)
809 sp_info.index = tmpl->index;
811 if (tmpl->ce_mask & XFRM_SP_ATTR_DIR)
812 sp_info.dir = tmpl->dir;
814 if (tmpl->ce_mask & XFRM_SP_ATTR_ACTION)
815 sp_info.action = tmpl->action;
817 if (tmpl->ce_mask & XFRM_SP_ATTR_FLAGS)
818 sp_info.flags = tmpl->flags;
820 if (tmpl->ce_mask & XFRM_SP_ATTR_SHARE)
821 sp_info.share = tmpl->share;
827 if (
nlmsg_append(msg, &sp_info,
sizeof(sp_info), NLMSG_ALIGNTO) < 0)
828 goto nla_put_failure;
830 if (tmpl->ce_mask & XFRM_SP_ATTR_SECCTX) {
831 len = (
sizeof (
struct xfrm_user_sec_ctx)) + tmpl->sec_ctx->ctx_len;
832 NLA_PUT (msg, XFRMA_SEC_CTX, len, tmpl->sec_ctx);
835 if (tmpl->ce_mask & XFRM_SP_ATTR_POLTYPE) {
836 len =
sizeof (
struct xfrm_userpolicy_type);
837 NLA_PUT (msg, XFRMA_POLICY_TYPE, len, &tmpl->uptype);
840 if (tmpl->ce_mask & XFRM_SP_ATTR_TMPL) {
841 struct nlattr* tmpls;
842 struct xfrmnl_user_tmpl* utmpl;
843 struct nl_addr* addr;
846 goto nla_put_failure;
848 nl_list_for_each_entry(utmpl, &tmpl->usertmpl_list, utmpl_list) {
849 struct xfrm_user_tmpl* tmpl;
853 goto nla_put_failure;
854 addr = xfrmnl_user_tmpl_get_daddr (utmpl);
857 tmpl->id.spi = htonl(xfrmnl_user_tmpl_get_spi (utmpl));
858 tmpl->id.proto = xfrmnl_user_tmpl_get_proto (utmpl);
859 tmpl->family = xfrmnl_user_tmpl_get_family (utmpl);
860 addr = xfrmnl_user_tmpl_get_saddr (utmpl);
863 tmpl->reqid = xfrmnl_user_tmpl_get_reqid (utmpl);
864 tmpl->mode = xfrmnl_user_tmpl_get_mode (utmpl);
865 tmpl->share = xfrmnl_user_tmpl_get_share (utmpl);
866 tmpl->optional = xfrmnl_user_tmpl_get_optional (utmpl);
867 tmpl->aalgos = xfrmnl_user_tmpl_get_aalgos (utmpl);
868 tmpl->ealgos = xfrmnl_user_tmpl_get_ealgos (utmpl);
869 tmpl->calgos = xfrmnl_user_tmpl_get_calgos (utmpl);
874 if (tmpl->ce_mask & XFRM_SP_ATTR_MARK) {
875 NLA_PUT (msg, XFRMA_MARK,
sizeof (
struct xfrm_mark), &tmpl->mark);
891 int xfrmnl_sp_build_add_request(
struct xfrmnl_sp* tmpl,
int flags,
struct nl_msg **result)
893 return build_xfrm_sp_message (tmpl, XFRM_MSG_NEWPOLICY, flags, result);
896 int xfrmnl_sp_add(
struct nl_sock* sk,
struct xfrmnl_sp* tmpl,
int flags)
901 if ((err = xfrmnl_sp_build_add_request(tmpl, flags, &msg)) < 0)
917 int xfrmnl_sp_build_update_request(
struct xfrmnl_sp* tmpl,
int flags,
struct nl_msg **result)
919 return build_xfrm_sp_message (tmpl, XFRM_MSG_UPDPOLICY, flags, result);
922 int xfrmnl_sp_update(
struct nl_sock* sk,
struct xfrmnl_sp* tmpl,
int flags)
927 if ((err = xfrmnl_sp_build_update_request(tmpl, flags, &msg)) < 0)
940 static int build_xfrm_sp_delete_message(
struct xfrmnl_sp *tmpl,
int cmd,
int flags,
struct nl_msg **result)
943 struct xfrm_userpolicy_id spid;
945 if (!(tmpl->ce_mask & XFRM_SP_ATTR_INDEX) ||
946 !(tmpl->ce_mask & XFRM_SP_ATTR_DIR))
947 return -NLE_MISSING_ATTR;
949 memset(&spid, 0,
sizeof(spid));
950 spid.index = tmpl->index;
951 spid.dir = tmpl->dir;
957 if (
nlmsg_append(msg, &spid,
sizeof(spid), NLMSG_ALIGNTO) < 0)
958 goto nla_put_failure;
960 if (tmpl->ce_mask & XFRM_SP_ATTR_MARK) {
961 NLA_PUT (msg, XFRMA_MARK,
sizeof (
struct xfrm_mark), &tmpl->mark);
977 int xfrmnl_sp_build_delete_request(
struct xfrmnl_sp* tmpl,
int flags,
struct nl_msg **result)
979 return build_xfrm_sp_delete_message (tmpl, XFRM_MSG_DELPOLICY, flags, result);
982 int xfrmnl_sp_delete(
struct nl_sock* sk,
struct xfrmnl_sp* tmpl,
int flags)
987 if ((err = xfrmnl_sp_build_delete_request(tmpl, flags, &msg)) < 0)
1006 struct xfrmnl_sel* xfrmnl_sp_get_sel (
struct xfrmnl_sp* sp)
1008 if (sp->ce_mask & XFRM_SP_ATTR_SEL)
1014 int xfrmnl_sp_set_sel (
struct xfrmnl_sp* sp,
struct xfrmnl_sel* sel)
1018 xfrmnl_sel_put (sp->sel);
1021 xfrmnl_sel_get (sel);
1023 sp->ce_mask |= XFRM_SP_ATTR_SEL;
1028 struct xfrmnl_ltime_cfg* xfrmnl_sp_get_lifetime_cfg (
struct xfrmnl_sp* sp)
1030 if (sp->ce_mask & XFRM_SP_ATTR_LTIME_CFG)
1036 int xfrmnl_sp_set_lifetime_cfg (
struct xfrmnl_sp* sp,
struct xfrmnl_ltime_cfg* ltime)
1040 xfrmnl_ltime_cfg_put (sp->lft);
1043 xfrmnl_ltime_cfg_get (ltime);
1045 sp->ce_mask |= XFRM_SP_ATTR_LTIME_CFG;
1050 int xfrmnl_sp_get_curlifetime (
struct xfrmnl_sp* sa,
unsigned long long int* curr_bytes,
1051 unsigned long long int* curr_packets,
unsigned long long int* curr_add_time,
unsigned long long int* curr_use_time)
1053 if (sa == NULL || curr_bytes == NULL || curr_packets == NULL || curr_add_time == NULL || curr_use_time == NULL)
1056 *curr_bytes = sa->curlft.bytes;
1057 *curr_packets = sa->curlft.packets;
1058 *curr_add_time = sa->curlft.add_time;
1059 *curr_use_time = sa->curlft.use_time;
1064 int xfrmnl_sp_get_priority (
struct xfrmnl_sp* sp)
1066 if (sp->ce_mask & XFRM_SP_ATTR_PRIO)
1067 return sp->priority;
1072 int xfrmnl_sp_set_priority (
struct xfrmnl_sp* sp,
unsigned int prio)
1074 sp->priority = prio;
1075 sp->ce_mask |= XFRM_SP_ATTR_PRIO;
1080 int xfrmnl_sp_get_index (
struct xfrmnl_sp* sp)
1082 if (sp->ce_mask & XFRM_SP_ATTR_INDEX)
1088 int xfrmnl_sp_set_index (
struct xfrmnl_sp* sp,
unsigned int index)
1091 sp->ce_mask |= XFRM_SP_ATTR_INDEX;
1096 int xfrmnl_sp_get_dir (
struct xfrmnl_sp* sp)
1098 if (sp->ce_mask & XFRM_SP_ATTR_DIR)
1104 int xfrmnl_sp_set_dir (
struct xfrmnl_sp* sp,
unsigned int dir)
1107 sp->ce_mask |= XFRM_SP_ATTR_DIR;
1112 int xfrmnl_sp_get_action (
struct xfrmnl_sp* sp)
1114 if (sp->ce_mask & XFRM_SP_ATTR_ACTION)
1120 int xfrmnl_sp_set_action (
struct xfrmnl_sp* sp,
unsigned int action)
1122 sp->action = action;
1123 sp->ce_mask |= XFRM_SP_ATTR_ACTION;
1128 int xfrmnl_sp_get_flags (
struct xfrmnl_sp* sp)
1130 if (sp->ce_mask & XFRM_SP_ATTR_FLAGS)
1136 int xfrmnl_sp_set_flags (
struct xfrmnl_sp* sp,
unsigned int flags)
1139 sp->ce_mask |= XFRM_SP_ATTR_FLAGS;
1144 int xfrmnl_sp_get_share (
struct xfrmnl_sp* sp)
1146 if (sp->ce_mask & XFRM_SP_ATTR_SHARE)
1152 int xfrmnl_sp_set_share (
struct xfrmnl_sp* sp,
unsigned int share)
1155 sp->ce_mask |= XFRM_SP_ATTR_SHARE;
1160 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)
1162 if (sp->ce_mask & XFRM_SP_ATTR_SECCTX)
1164 *len = sp->sec_ctx->len;
1165 *exttype= sp->sec_ctx->exttype;
1166 *alg = sp->sec_ctx->ctx_alg;
1167 *doi = sp->sec_ctx->ctx_doi;
1168 *ctx_len= sp->sec_ctx->ctx_len;
1169 memcpy ((
void *)ctx_str, (
void *)sp->sec_ctx->ctx, sizeof (uint8_t) * sp->sec_ctx->ctx_len);
1177 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)
1182 if ((sp->sec_ctx = calloc (1,
sizeof (
struct xfrmnl_user_sec_ctx) + (
sizeof (uint8_t) * ctx_len))) == NULL)
1186 sp->sec_ctx->len = len;
1187 sp->sec_ctx->exttype = exttype;
1188 sp->sec_ctx->ctx_alg = alg;
1189 sp->sec_ctx->ctx_doi = doi;
1190 sp->sec_ctx->ctx_len = len;
1191 memcpy ((
void *)sp->sec_ctx->ctx, (
void *)ctx_str, sizeof (uint8_t) * ctx_len);
1193 sp->ce_mask |= XFRM_SP_ATTR_SECCTX;
1198 int xfrmnl_sp_get_userpolicy_type (
struct xfrmnl_sp* sp)
1200 if (sp->ce_mask & XFRM_SP_ATTR_POLTYPE)
1201 return sp->uptype.type;
1206 int xfrmnl_sp_set_userpolicy_type (
struct xfrmnl_sp* sp,
unsigned int type)
1208 sp->uptype.type = type;
1209 sp->ce_mask |= XFRM_SP_ATTR_POLTYPE;
1214 void xfrmnl_sp_add_usertemplate(
struct xfrmnl_sp *sp,
struct xfrmnl_user_tmpl *utmpl)
1216 nl_list_add_tail(&utmpl->utmpl_list, &sp->usertmpl_list);
1218 sp->ce_mask |= XFRM_SP_ATTR_TMPL;
1221 void xfrmnl_sp_remove_usertemplate(
struct xfrmnl_sp *sp,
struct xfrmnl_user_tmpl *utmpl)
1223 if (sp->ce_mask & XFRM_SP_ATTR_TMPL) {
1225 nl_list_del(&utmpl->utmpl_list);
1229 struct nl_list_head *xfrmnl_sp_get_usertemplates(
struct xfrmnl_sp *sp)
1231 if (sp->ce_mask & XFRM_SP_ATTR_TMPL)
1232 return &sp->usertmpl_list;
1237 int xfrmnl_sp_get_nusertemplates(
struct xfrmnl_sp *sp)
1239 if (sp->ce_mask & XFRM_SP_ATTR_TMPL)
1240 return sp->nr_user_tmpl;
1245 void xfrmnl_sp_foreach_usertemplate(
struct xfrmnl_sp *r,
1246 void (*cb)(
struct xfrmnl_user_tmpl *,
void *),
1249 struct xfrmnl_user_tmpl *utmpl;
1251 if (r->ce_mask & XFRM_SP_ATTR_TMPL) {
1252 nl_list_for_each_entry(utmpl, &r->usertmpl_list, utmpl_list) {
1258 struct xfrmnl_user_tmpl *xfrmnl_sp_usertemplate_n(
struct xfrmnl_sp *r,
int n)
1260 struct xfrmnl_user_tmpl *utmpl;
1263 if (r->ce_mask & XFRM_SP_ATTR_TMPL && r->nr_user_tmpl > n) {
1265 nl_list_for_each_entry(utmpl, &r->usertmpl_list, utmpl_list) {
1266 if (i == n)
return utmpl;
1273 int xfrmnl_sp_get_mark (
struct xfrmnl_sp* sp,
unsigned int* mark_mask,
unsigned int* mark_value)
1275 if (mark_mask == NULL || mark_value == NULL)
1278 if (sp->ce_mask & XFRM_SP_ATTR_MARK)
1280 *mark_mask = sp->mark.m;
1281 *mark_value = sp->mark.v;
1289 int xfrmnl_sp_set_mark (
struct xfrmnl_sp* sp,
unsigned int value,
unsigned int mask)
1293 sp->ce_mask |= XFRM_SP_ATTR_MARK;
1300 static struct nl_object_ops xfrm_sp_obj_ops = {
1301 .oo_name =
"xfrm/sp",
1302 .oo_size =
sizeof(
struct xfrmnl_sp),
1303 .oo_constructor = xfrm_sp_alloc_data,
1304 .oo_free_data = xfrm_sp_free_data,
1305 .oo_clone = xfrm_sp_clone,
1311 .oo_compare = xfrm_sp_compare,
1312 .oo_attrs2str = xfrm_sp_attrs2str,
1313 .oo_id_attrs = (XFRM_SP_ATTR_SEL | XFRM_SP_ATTR_INDEX | XFRM_SP_ATTR_DIR),
1316 static struct nl_af_group xfrm_sp_groups[] = {
1317 { AF_UNSPEC, XFRMNLGRP_POLICY },
1318 { END_OF_GROUP_LIST },
1321 static struct nl_cache_ops xfrmnl_sp_ops = {
1322 .co_name =
"xfrm/sp",
1323 .co_hdrsize =
sizeof(
struct xfrm_userpolicy_info),
1325 { XFRM_MSG_NEWPOLICY, NL_ACT_NEW,
"new" },
1326 { XFRM_MSG_DELPOLICY, NL_ACT_DEL,
"del" },
1327 { XFRM_MSG_GETPOLICY, NL_ACT_GET,
"get" },
1328 { XFRM_MSG_UPDPOLICY, NL_ACT_NEW,
"update" },
1329 END_OF_MSGTYPES_LIST,
1331 .co_protocol = NETLINK_XFRM,
1332 .co_groups = xfrm_sp_groups,
1333 .co_request_update = xfrm_sp_request_update,
1334 .co_msg_parser = xfrm_sp_msg_parser,
1335 .co_obj_ops = &xfrm_sp_obj_ops,
1343 static void __attribute__ ((constructor)) xfrm_sp_init(
void)
1348 static void __attribute__ ((destructor)) xfrm_sp_exit(
void)
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Dump object briefly on one line.
void nl_addr_set_prefixlen(struct nl_addr *addr, int prefixlen)
Set the prefix length of an abstract address.
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
void * nlmsg_reserve(struct nl_msg *n, size_t len, int pad)
Reserve room for additional data in a netlink message.
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
Attribute validation policy.
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
struct nl_addr * nl_addr_build(int family, const void *buf, size_t size)
Allocate abstract address based on a binary address.
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, struct nla_policy *policy)
parse attributes of a netlink message
struct xfrmnl_ltime_cfg * xfrmnl_ltime_cfg_clone(struct xfrmnl_ltime_cfg *ltime)
Clone existing lifetime config object.
Dump all attributes but no statistics.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
int xfrmnl_sel_cmp(struct xfrmnl_sel *a, struct xfrmnl_sel *b)
Compares two selector objects.
int nla_len(const struct nlattr *nla)
Return length of the payload .
uint16_t minlen
Minimal length of payload required.
int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf, size_t size)
Construct and transmit a Netlink message.
struct nl_object * nl_cache_get_next(struct nl_object *obj)
Return the next element in the cache.
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
int nl_wait_for_ack(struct nl_sock *sk)
Wait for ACK.
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
struct xfrmnl_sel * xfrmnl_sel_alloc()
Allocate new selector object.
struct xfrmnl_sel * xfrmnl_sel_clone(struct xfrmnl_sel *sel)
Clone existing selector object.
struct xfrmnl_ltime_cfg * xfrmnl_ltime_cfg_alloc()
Allocate new lifetime config object.
struct xfrmnl_user_tmpl * xfrmnl_user_tmpl_clone(struct xfrmnl_user_tmpl *utmpl)
Clone existing user template object.
struct xfrmnl_user_tmpl * xfrmnl_user_tmpl_alloc()
Allocate new user template object.
int xfrmnl_ltime_cfg_cmp(struct xfrmnl_ltime_cfg *a, struct xfrmnl_ltime_cfg *b)
Compares two lifetime config objects.
int nl_pickup(struct nl_sock *sk, int(*parser)(struct nl_cache_ops *, struct sockaddr_nl *, struct nlmsghdr *, struct nl_parser_param *), struct nl_object **result)
Pickup netlink answer, parse is and return object.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
int xfrmnl_user_tmpl_cmp(struct xfrmnl_user_tmpl *a, struct xfrmnl_user_tmpl *b)
Compares two user template objects.
int nl_send_auto(struct nl_sock *sk, struct nl_msg *msg)
Finalize and transmit Netlink message.
unsigned int nl_addr_get_len(const struct nl_addr *addr)
Get length of binary address of abstract address object.
Dump all attributes including statistics.
struct nl_object * nl_cache_get_first(struct nl_cache *cache)
Return the first element in the cache.
void * nl_addr_get_binary_addr(const struct nl_addr *addr)
Get binary address of abstract address object.
int nl_cache_alloc_and_fill(struct nl_cache_ops *ops, struct nl_sock *sock, struct nl_cache **result)
Allocate new cache and fill it.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.