42#include "nl-default.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>
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"
80 uint32_t nr_user_tmpl;
86#define XFRM_SP_ATTR_SEL 0x01
87#define XFRM_SP_ATTR_LTIME_CFG 0x02
88#define XFRM_SP_ATTR_LTIME_CUR 0x04
89#define XFRM_SP_ATTR_PRIO 0x08
90#define XFRM_SP_ATTR_INDEX 0x10
91#define XFRM_SP_ATTR_DIR 0x20
92#define XFRM_SP_ATTR_ACTION 0x40
93#define XFRM_SP_ATTR_FLAGS 0x80
94#define XFRM_SP_ATTR_SHARE 0x100
95#define XFRM_SP_ATTR_POLTYPE 0x200
96#define XFRM_SP_ATTR_SECCTX 0x400
97#define XFRM_SP_ATTR_TMPL 0x800
98#define XFRM_SP_ATTR_MARK 0x1000
100static struct nl_cache_ops xfrmnl_sp_ops;
101static struct nl_object_ops xfrm_sp_obj_ops;
104static void xfrm_sp_alloc_data(
struct nl_object *c)
106 struct xfrmnl_sp* sp = nl_object_priv (c);
114 nl_init_list_head(&sp->usertmpl_list);
119static void xfrm_sp_free_data(
struct nl_object *c)
121 struct xfrmnl_sp* sp = nl_object_priv (c);
127 xfrmnl_sel_put (sp->sel);
128 xfrmnl_ltime_cfg_put (sp->lft);
134 nl_list_for_each_entry_safe(utmpl, tmp, &sp->usertmpl_list, utmpl_list) {
135 xfrmnl_sp_remove_usertemplate (sp, utmpl);
136 xfrmnl_user_tmpl_free (utmpl);
140static int xfrm_sp_clone(
struct nl_object *_dst,
struct nl_object *_src)
142 struct xfrmnl_sp* dst = nl_object_priv(_dst);
143 struct xfrmnl_sp* src = nl_object_priv(_src);
150 nl_init_list_head(&dst->usertmpl_list);
165 if ((dst->sec_ctx = malloc (len)) == NULL)
167 memcpy(dst->sec_ctx, src->sec_ctx, len);
170 nl_list_for_each_entry(utmpl, &src->usertmpl_list, utmpl_list) {
174 xfrmnl_sp_add_usertemplate(dst,
new);
180static uint64_t xfrm_sp_compare(
struct nl_object *_a,
struct nl_object *_b,
181 uint64_t attrs,
int flags)
188#define _DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ATTR, a, b, EXPR)
190 diff |= _DIFF(XFRM_SP_ATTR_LTIME_CFG,
192 diff |= _DIFF(XFRM_SP_ATTR_PRIO, a->priority != b->priority);
193 diff |= _DIFF(XFRM_SP_ATTR_INDEX, a->index != b->index);
194 diff |= _DIFF(XFRM_SP_ATTR_DIR, a->dir != b->dir);
195 diff |= _DIFF(XFRM_SP_ATTR_ACTION, a->action != b->action);
196 diff |= _DIFF(XFRM_SP_ATTR_FLAGS, a->flags != b->flags);
197 diff |= _DIFF(XFRM_SP_ATTR_SHARE, a->share != b->share);
198 diff |= _DIFF(XFRM_SP_ATTR_SECCTX,
199 ((a->sec_ctx->len != b->sec_ctx->len) ||
200 (a->sec_ctx->exttype != b->sec_ctx->exttype) ||
201 (a->sec_ctx->ctx_alg != b->sec_ctx->ctx_alg) ||
202 (a->sec_ctx->ctx_doi != b->sec_ctx->ctx_doi) ||
203 (a->sec_ctx->ctx_len != b->sec_ctx->ctx_len) ||
204 strcmp(a->sec_ctx->ctx, b->sec_ctx->ctx)));
205 diff |= _DIFF(XFRM_SP_ATTR_POLTYPE, (a->uptype.type != b->uptype.type));
206 diff |= _DIFF(XFRM_SP_ATTR_TMPL, (a->nr_user_tmpl != b->nr_user_tmpl));
207 diff |= _DIFF(XFRM_SP_ATTR_MARK,
208 (a->mark.m != b->mark.m) || (a->mark.v != b->mark.v));
211 nl_list_for_each_entry(tmpl_b, &b->usertmpl_list, utmpl_list)
212 nl_list_for_each_entry(tmpl_a, &a->usertmpl_list, utmpl_list)
223static const struct trans_tbl sp_attrs[] = {
224 __ADD(XFRM_SP_ATTR_SEL, selector),
225 __ADD(XFRM_SP_ATTR_LTIME_CFG, lifetime_cfg),
226 __ADD(XFRM_SP_ATTR_LTIME_CUR, lifetime_cur),
227 __ADD(XFRM_SP_ATTR_PRIO, priority),
228 __ADD(XFRM_SP_ATTR_INDEX, index),
229 __ADD(XFRM_SP_ATTR_DIR, direction),
230 __ADD(XFRM_SP_ATTR_ACTION, action),
231 __ADD(XFRM_SP_ATTR_FLAGS, flags),
232 __ADD(XFRM_SP_ATTR_SHARE, share),
233 __ADD(XFRM_SP_ATTR_POLTYPE, policy_type),
234 __ADD(XFRM_SP_ATTR_SECCTX, security_context),
235 __ADD(XFRM_SP_ATTR_TMPL, user_template),
236 __ADD(XFRM_SP_ATTR_MARK, mark),
239static char* xfrm_sp_attrs2str(
int attrs,
char *buf,
size_t len)
241 return __flags2str (attrs, buf, len, sp_attrs, ARRAY_SIZE(sp_attrs));
249static const struct trans_tbl sa_actions[] = {
250 __ADD(XFRM_POLICY_ALLOW, allow),
251 __ADD(XFRM_POLICY_BLOCK, block),
254char* xfrmnl_sp_action2str(
int action,
char *buf,
size_t len)
256 return __type2str (action, buf, len, sa_actions, ARRAY_SIZE(sa_actions));
259int xfrmnl_sp_str2action(
const char *name)
261 return __str2type (name, sa_actions, ARRAY_SIZE(sa_actions));
269static const struct trans_tbl sp_flags[] = {
270 __ADD(XFRM_POLICY_LOCALOK, allow policy
override by user),
271 __ADD(XFRM_POLICY_ICMP,
auto include ICMP in policy),
274char* xfrmnl_sp_flags2str(
int flags,
char *buf,
size_t len)
276 return __flags2str (flags, buf, len, sp_flags, ARRAY_SIZE(sp_flags));
279int xfrmnl_sp_str2flag(
const char *name)
281 return __str2flags(name, sp_flags, ARRAY_SIZE(sp_flags));
289static const struct trans_tbl sp_types[] = {
290 __ADD(XFRM_POLICY_TYPE_MAIN, main),
291 __ADD(XFRM_POLICY_TYPE_SUB, sub),
292 __ADD(XFRM_POLICY_TYPE_MAX, max),
293 __ADD(XFRM_POLICY_TYPE_ANY, any),
296char* xfrmnl_sp_type2str(
int type,
char *buf,
size_t len)
298 return __type2str(type, buf, len, sp_types, ARRAY_SIZE(sp_types));
301int xfrmnl_sp_str2type(
const char *name)
303 return __str2type(name, sp_types, ARRAY_SIZE(sp_types));
311static const struct trans_tbl sp_dir[] = {
312 __ADD(XFRM_POLICY_IN, in),
313 __ADD(XFRM_POLICY_OUT, out),
314 __ADD(XFRM_POLICY_FWD, fwd),
315 __ADD(XFRM_POLICY_MASK, mask),
318char* xfrmnl_sp_dir2str(
int dir,
char *buf,
size_t len)
320 return __type2str (dir, buf, len, sp_dir, ARRAY_SIZE(sp_dir));
323int xfrmnl_sp_str2dir(
const char *name)
325 return __str2type (name, sp_dir, ARRAY_SIZE(sp_dir));
328int xfrmnl_sp_index2dir (
unsigned int index)
338static const struct trans_tbl sp_share[] = {
339 __ADD(XFRM_SHARE_ANY, any),
340 __ADD(XFRM_SHARE_SESSION, session),
341 __ADD(XFRM_SHARE_USER, user),
342 __ADD(XFRM_SHARE_UNIQUE, unique),
345char* xfrmnl_sp_share2str(
int share,
char *buf,
size_t len)
347 return __type2str (share, buf, len, sp_share, ARRAY_SIZE(sp_share));
350int xfrmnl_sp_str2share(
const char *name)
352 return __str2type (name, sp_share, ARRAY_SIZE(sp_share));
356static void xfrm_sp_dump_line(
struct nl_object *a,
struct nl_dump_params *p)
359 char dir[32], action[32], share[32], flags[32];
360 char dst[INET6_ADDRSTRLEN+5], src[INET6_ADDRSTRLEN+5];
361 time_t add_time, use_time;
362 struct tm *add_time_tm, *use_time_tm;
365 nl_addr2str(xfrmnl_sel_get_saddr (sp->sel), src,
sizeof(src));
366 nl_addr2str (xfrmnl_sel_get_daddr (sp->sel), dst, sizeof (dst));
367 nl_af2str (xfrmnl_sel_get_family (sp->sel), dir, 32);
368 nl_dump_line(p,
"src %s dst %s family: %s\n", src, dst, dir);
369 nl_dump_line (p,
"src port/mask: %d/%d dst port/mask: %d/%d\n",
370 xfrmnl_sel_get_dport (sp->sel), xfrmnl_sel_get_dportmask (sp->sel),
371 xfrmnl_sel_get_sport (sp->sel), xfrmnl_sel_get_sportmask (sp->sel));
372 nl_dump_line (p,
"protocol: %s ifindex: %u uid: %u\n",
373 nl_ip_proto2str (xfrmnl_sel_get_proto (sp->sel), dir,
sizeof(dir)),
374 xfrmnl_sel_get_ifindex (sp->sel),
375 xfrmnl_sel_get_userid (sp->sel));
377 xfrmnl_sp_dir2str (sp->dir, dir, 32);
378 xfrmnl_sp_action2str (sp->action, action, 32);
379 xfrmnl_sp_share2str (sp->share, share, 32);
380 xfrmnl_sp_flags2str (sp->flags, flags, 32);
381 nl_dump_line(p,
"\tdir: %s action: %s index: %u priority: %u share: %s flags: %s(0x%x) \n",
382 dir, action, sp->index, sp->priority, share, flags, sp->flags);
384 nl_dump_line(p,
"\tlifetime configuration: \n");
385 if (sp->lft->soft_byte_limit == XFRM_INF)
386 sprintf (dir,
"INF");
388 sprintf (dir,
"%" PRIu64, sp->lft->soft_byte_limit);
389 if (sp->lft->soft_packet_limit == XFRM_INF)
390 sprintf (action,
"INF");
392 sprintf (action,
"%" PRIu64, sp->lft->soft_packet_limit);
393 if (sp->lft->hard_byte_limit == XFRM_INF)
394 sprintf (flags,
"INF");
396 sprintf (flags,
"%" PRIu64, sp->lft->hard_byte_limit);
397 if (sp->lft->hard_packet_limit == XFRM_INF)
398 sprintf (share,
"INF");
400 sprintf (share,
"%" PRIu64, sp->lft->hard_packet_limit);
401 nl_dump_line(p,
"\t\tsoft limit: %s (bytes), %s (packets) \n", dir,
403 nl_dump_line(p,
"\t\thard limit: %s (bytes), %s (packets) \n", flags,
407 "\t\tsoft add_time: %llu (seconds), soft use_time: %llu (seconds) \n",
408 (
long long unsigned)sp->lft->soft_add_expires_seconds,
409 (
long long unsigned)sp->lft->soft_use_expires_seconds);
412 "\t\thard add_time: %llu (seconds), hard use_time: %llu (seconds) \n",
413 (
long long unsigned)sp->lft->hard_add_expires_seconds,
414 (
long long unsigned)sp->lft->hard_use_expires_seconds);
416 nl_dump_line(p,
"\tlifetime current: \n");
417 nl_dump_line(p,
"\t\t%llu bytes, %llu packets\n",
418 (
long long unsigned)sp->curlft.bytes,
419 (
long long unsigned)sp->curlft.packets);
421 if (sp->curlft.add_time != 0)
423 add_time = sp->curlft.add_time;
424 add_time_tm = gmtime_r (&add_time, &tm_buf);
425 strftime (dst, INET6_ADDRSTRLEN+5,
"%Y-%m-%d %H-%M-%S", add_time_tm);
429 sprintf (dst,
"%s",
"-");
432 if (sp->curlft.use_time != 0)
434 use_time = sp->curlft.use_time;
435 use_time_tm = gmtime_r (&use_time, &tm_buf);
436 strftime (src, INET6_ADDRSTRLEN+5,
"%Y-%m-%d %H-%M-%S", use_time_tm);
440 sprintf (src,
"%s",
"-");
442 nl_dump_line(p,
"\t\tadd_time: %s, use_time: %s\n", dst, src);
444 if (sp->ce_mask & XFRM_SP_ATTR_SECCTX)
446 nl_dump_line(p,
"\tUser security context: \n");
447 nl_dump_line(p,
"\t\tlen: %d exttype: %d Algo: %d DOI: %d ctxlen: %d\n",
448 sp->sec_ctx->len, sp->sec_ctx->exttype,
449 sp->sec_ctx->ctx_alg, sp->sec_ctx->ctx_doi, sp->sec_ctx->ctx_len);
450 nl_dump_line (p,
"\t\tctx: %s \n", sp->sec_ctx->ctx);
453 xfrmnl_sp_type2str (sp->uptype.type, flags, 32);
454 if (sp->ce_mask & XFRM_SP_ATTR_POLTYPE)
455 nl_dump_line(p,
"\tUser policy type: %s\n", flags);
457 if (sp->ce_mask & XFRM_SP_ATTR_TMPL)
461 nl_dump_line(p,
"\tUser template: \n");
463 nl_list_for_each_entry(utmpl, &sp->usertmpl_list, utmpl_list)
464 xfrmnl_user_tmpl_dump (utmpl, p);
467 if (sp->ce_mask & XFRM_SP_ATTR_MARK)
468 nl_dump_line(p,
"\tMark mask: 0x%x Mark value: 0x%x\n", sp->mark.m, sp->mark.v);
473static void xfrm_sp_dump_details(
struct nl_object *a,
struct nl_dump_params *p)
475 xfrm_sp_dump_line(a, p);
478static void xfrm_sp_dump_stats(
struct nl_object *a,
struct nl_dump_params *p)
480 xfrm_sp_dump_details(a, p);
517int xfrmnl_sp_alloc_cache(
struct nl_sock *sock,
struct nl_cache **result)
529struct xfrmnl_sp* xfrmnl_sp_get(
struct nl_cache* cache,
unsigned int index,
unsigned int dir)
538 if (sp->index == index && sp->dir == dir)
554 [XFRMA_SEC_CTX] = { .minlen =
sizeof(
struct xfrm_sec_ctx) },
555 [XFRMA_TMPL] = { .minlen =
sizeof(
struct xfrm_user_tmpl) },
556 [XFRMA_POLICY_TYPE] = { .minlen =
sizeof(
struct xfrm_userpolicy_type)},
557 [XFRMA_MARK] = { .minlen =
sizeof(
struct xfrm_mark) },
560static int xfrm_sp_request_update(
struct nl_cache *c,
struct nl_sock *h)
562 return nl_send_simple (h, XFRM_MSG_GETPOLICY, NLM_F_DUMP, NULL, 0);
565int xfrmnl_sp_parse(
struct nlmsghdr *n,
struct xfrmnl_sp **result)
567 _nl_auto_nl_addr
struct nl_addr *addr1 = NULL;
568 _nl_auto_nl_addr
struct nl_addr *addr2 = NULL;
569 _nl_auto_xfrmnl_sp
struct xfrmnl_sp *sp = NULL;
570 struct nlattr *tb[XFRMA_MAX + 1];
571 struct xfrm_userpolicy_info *sp_info;
574 sp = xfrmnl_sp_alloc();
578 sp->ce_msgtype = n->nlmsg_type;
579 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
580 sp_info = (
struct xfrm_userpolicy_info*)((
char *)
nlmsg_data(n) +
sizeof (
struct xfrm_userpolicy_id) + NLA_HDRLEN);
584 err =
nlmsg_parse(n,
sizeof(
struct xfrm_userpolicy_info), tb, XFRMA_MAX, xfrm_sp_policy);
588 if (!(addr1 = _nl_addr_build(sp_info->sel.family, &sp_info->sel.daddr)))
591 xfrmnl_sel_set_daddr (sp->sel, addr1);
592 xfrmnl_sel_set_prefixlen_d (sp->sel, sp_info->sel.prefixlen_d);
594 if (!(addr2 = _nl_addr_build(sp_info->sel.family, &sp_info->sel.saddr)))
597 xfrmnl_sel_set_saddr (sp->sel, addr2);
598 xfrmnl_sel_set_prefixlen_s (sp->sel, sp_info->sel.prefixlen_s);
600 xfrmnl_sel_set_dport (sp->sel, ntohs (sp_info->sel.dport));
601 xfrmnl_sel_set_dportmask (sp->sel, ntohs (sp_info->sel.dport_mask));
602 xfrmnl_sel_set_sport (sp->sel, ntohs (sp_info->sel.sport));
603 xfrmnl_sel_set_sportmask (sp->sel, ntohs (sp_info->sel.sport_mask));
604 xfrmnl_sel_set_family (sp->sel, sp_info->sel.family);
605 xfrmnl_sel_set_proto (sp->sel, sp_info->sel.proto);
606 xfrmnl_sel_set_ifindex (sp->sel, sp_info->sel.ifindex);
607 xfrmnl_sel_set_userid (sp->sel, sp_info->sel.user);
608 sp->ce_mask |= XFRM_SP_ATTR_SEL;
610 sp->lft->soft_byte_limit = sp_info->lft.soft_byte_limit;
611 sp->lft->hard_byte_limit = sp_info->lft.hard_byte_limit;
612 sp->lft->soft_packet_limit = sp_info->lft.soft_packet_limit;
613 sp->lft->hard_packet_limit = sp_info->lft.hard_packet_limit;
614 sp->lft->soft_add_expires_seconds = sp_info->lft.soft_add_expires_seconds;
615 sp->lft->hard_add_expires_seconds = sp_info->lft.hard_add_expires_seconds;
616 sp->lft->soft_use_expires_seconds = sp_info->lft.soft_use_expires_seconds;
617 sp->lft->hard_use_expires_seconds = sp_info->lft.hard_use_expires_seconds;
618 sp->ce_mask |= XFRM_SP_ATTR_LTIME_CFG;
620 sp->curlft.bytes = sp_info->curlft.bytes;
621 sp->curlft.packets = sp_info->curlft.packets;
622 sp->curlft.add_time = sp_info->curlft.add_time;
623 sp->curlft.use_time = sp_info->curlft.use_time;
624 sp->ce_mask |= XFRM_SP_ATTR_LTIME_CUR;
626 sp->priority = sp_info->priority;
627 sp->index = sp_info->index;
628 sp->dir = sp_info->dir;
629 sp->action = sp_info->action;
630 sp->flags = sp_info->flags;
631 sp->share = sp_info->share;
632 sp->ce_mask |= (XFRM_SP_ATTR_PRIO | XFRM_SP_ATTR_INDEX |
633 XFRM_SP_ATTR_DIR | XFRM_SP_ATTR_ACTION |
634 XFRM_SP_ATTR_FLAGS | XFRM_SP_ATTR_SHARE);
636 if (tb[XFRMA_SEC_CTX]) {
637 struct xfrm_user_sec_ctx* ctx =
nla_data(tb[XFRMA_SEC_CTX]);
640 if ((sp->sec_ctx = calloc (1, len)) == NULL)
642 memcpy ((
void *)sp->sec_ctx, (
void *)ctx, len);
643 sp->ce_mask |= XFRM_SP_ATTR_SECCTX;
646 if (tb[XFRMA_POLICY_TYPE]) {
647 struct xfrm_userpolicy_type* up =
nla_data(tb[XFRMA_POLICY_TYPE]);
649 memcpy ((
void *)&sp->uptype, (
void *)up, sizeof (
struct xfrm_userpolicy_type));
650 sp->ce_mask |= XFRM_SP_ATTR_POLTYPE;
653 if (tb[XFRMA_TMPL]) {
654 struct xfrm_user_tmpl* tmpl =
nla_data(tb[XFRMA_TMPL]);
656 uint32_t num_tmpls =
nla_len(tb[XFRMA_TMPL]) /
sizeof (*tmpl);
658 for (i = 0; (i < num_tmpls) && (tmpl); i ++, tmpl++)
661 _nl_auto_nl_addr
struct nl_addr *addr1 = NULL;
662 _nl_auto_nl_addr
struct nl_addr *addr2 = NULL;
667 if (!(addr1 = _nl_addr_build(tmpl->family, &tmpl->id.daddr)))
669 xfrmnl_user_tmpl_set_daddr (sputmpl, addr1);
670 xfrmnl_user_tmpl_set_spi (sputmpl, ntohl(tmpl->id.spi));
671 xfrmnl_user_tmpl_set_proto (sputmpl, tmpl->id.proto);
672 xfrmnl_user_tmpl_set_family (sputmpl, tmpl->family);
674 if (!(addr2 = _nl_addr_build(tmpl->family, &tmpl->saddr)))
676 xfrmnl_user_tmpl_set_saddr (sputmpl, addr2);
678 xfrmnl_user_tmpl_set_reqid (sputmpl, tmpl->reqid);
679 xfrmnl_user_tmpl_set_mode (sputmpl, tmpl->mode);
680 xfrmnl_user_tmpl_set_share (sputmpl, tmpl->share);
681 xfrmnl_user_tmpl_set_optional (sputmpl, tmpl->optional);
682 xfrmnl_user_tmpl_set_aalgos (sputmpl, tmpl->aalgos);
683 xfrmnl_user_tmpl_set_ealgos (sputmpl, tmpl->ealgos);
684 xfrmnl_user_tmpl_set_calgos (sputmpl, tmpl->calgos);
685 xfrmnl_sp_add_usertemplate (sp, _nl_steal_pointer(&sputmpl));
687 sp->ce_mask |= XFRM_SP_ATTR_TMPL;
691 if (tb[XFRMA_MARK]) {
692 struct xfrm_mark* m =
nla_data(tb[XFRMA_MARK]);
695 sp->ce_mask |= XFRM_SP_ATTR_MARK;
698 *result = _nl_steal_pointer(&sp);
702static int xfrm_sp_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
703 struct nlmsghdr *n,
struct nl_parser_param *pp)
708 if ((err = xfrmnl_sp_parse(n, &sp)) < 0)
710 printf (
"received error: %d \n", err);
714 err = pp->pp_cb((
struct nl_object *) sp, pp);
725int xfrmnl_sp_build_get_request(
unsigned int index,
unsigned int dir,
unsigned int mark_v,
unsigned int mark_m,
struct nl_msg **result)
728 struct xfrm_userpolicy_id spid;
729 struct xfrm_mark mark;
731 memset(&spid, 0,
sizeof(spid));
738 if (
nlmsg_append(msg, &spid,
sizeof(spid), NLMSG_ALIGNTO) < 0)
739 goto nla_put_failure;
741 if ((mark_m & mark_v) != 0)
743 memset(&mark, 0,
sizeof(
struct xfrm_mark));
747 NLA_PUT (msg, XFRMA_MARK,
sizeof (
struct xfrm_mark), &mark);
758int 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)
760 struct nl_msg *msg = NULL;
761 struct nl_object *obj;
764 if ((err = xfrmnl_sp_build_get_request(index, dir, mark_m, mark_v, &msg)) < 0)
772 if ((err =
nl_pickup(sock, &xfrm_sp_msg_parser, &obj)) < 0)
787static int build_xfrm_sp_message(
struct xfrmnl_sp *tmpl,
int cmd,
int flags,
struct nl_msg **result)
790 struct xfrm_userpolicy_info sp_info;
792 struct nl_addr* addr;
794 if (!(tmpl->ce_mask & XFRM_SP_ATTR_DIR) ||
795 (!(tmpl->ce_mask & XFRM_SP_ATTR_INDEX) &&
796 !(tmpl->ce_mask & XFRM_SP_ATTR_SEL)))
797 return -NLE_MISSING_ATTR;
799 memset ((
void*)&sp_info, 0,
sizeof (sp_info));
800 if (tmpl->ce_mask & XFRM_SP_ATTR_SEL)
802 addr = xfrmnl_sel_get_daddr (tmpl->sel);
804 addr = xfrmnl_sel_get_saddr (tmpl->sel);
806 sp_info.sel.dport = htons (xfrmnl_sel_get_dport (tmpl->sel));
807 sp_info.sel.dport_mask = htons (xfrmnl_sel_get_dportmask (tmpl->sel));
808 sp_info.sel.sport = htons (xfrmnl_sel_get_sport (tmpl->sel));
809 sp_info.sel.sport_mask = htons (xfrmnl_sel_get_sportmask (tmpl->sel));
810 sp_info.sel.family = xfrmnl_sel_get_family (tmpl->sel);
811 sp_info.sel.prefixlen_d = xfrmnl_sel_get_prefixlen_d (tmpl->sel);
812 sp_info.sel.prefixlen_s = xfrmnl_sel_get_prefixlen_s (tmpl->sel);
813 sp_info.sel.proto = xfrmnl_sel_get_proto (tmpl->sel);
814 sp_info.sel.ifindex = xfrmnl_sel_get_ifindex (tmpl->sel);
815 sp_info.sel.user = xfrmnl_sel_get_userid (tmpl->sel);
818 if (tmpl->ce_mask & XFRM_SP_ATTR_LTIME_CFG)
820 sp_info.lft.soft_byte_limit = xfrmnl_ltime_cfg_get_soft_bytelimit (tmpl->lft);
821 sp_info.lft.hard_byte_limit = xfrmnl_ltime_cfg_get_hard_bytelimit (tmpl->lft);
822 sp_info.lft.soft_packet_limit = xfrmnl_ltime_cfg_get_soft_packetlimit (tmpl->lft);
823 sp_info.lft.hard_packet_limit = xfrmnl_ltime_cfg_get_hard_packetlimit (tmpl->lft);
824 sp_info.lft.soft_add_expires_seconds = xfrmnl_ltime_cfg_get_soft_addexpires (tmpl->lft);
825 sp_info.lft.hard_add_expires_seconds = xfrmnl_ltime_cfg_get_hard_addexpires (tmpl->lft);
826 sp_info.lft.soft_use_expires_seconds = xfrmnl_ltime_cfg_get_soft_useexpires (tmpl->lft);
827 sp_info.lft.hard_use_expires_seconds = xfrmnl_ltime_cfg_get_hard_useexpires (tmpl->lft);
832 if (tmpl->ce_mask & XFRM_SP_ATTR_PRIO)
833 sp_info.priority = tmpl->priority;
835 if (tmpl->ce_mask & XFRM_SP_ATTR_INDEX)
836 sp_info.index = tmpl->index;
838 if (tmpl->ce_mask & XFRM_SP_ATTR_DIR)
839 sp_info.dir = tmpl->dir;
841 if (tmpl->ce_mask & XFRM_SP_ATTR_ACTION)
842 sp_info.action = tmpl->action;
844 if (tmpl->ce_mask & XFRM_SP_ATTR_FLAGS)
845 sp_info.flags = tmpl->flags;
847 if (tmpl->ce_mask & XFRM_SP_ATTR_SHARE)
848 sp_info.share = tmpl->share;
854 if (
nlmsg_append(msg, &sp_info,
sizeof(sp_info), NLMSG_ALIGNTO) < 0)
855 goto nla_put_failure;
857 if (tmpl->ce_mask & XFRM_SP_ATTR_SECCTX) {
858 len = (
sizeof (
struct xfrm_user_sec_ctx)) + tmpl->sec_ctx->ctx_len;
859 NLA_PUT (msg, XFRMA_SEC_CTX, len, tmpl->sec_ctx);
862 if (tmpl->ce_mask & XFRM_SP_ATTR_POLTYPE) {
863 len =
sizeof (
struct xfrm_userpolicy_type);
864 NLA_PUT (msg, XFRMA_POLICY_TYPE, len, &tmpl->uptype);
867 if (tmpl->ce_mask & XFRM_SP_ATTR_TMPL) {
868 struct nlattr* tmpls;
870 struct nl_addr* addr;
873 goto nla_put_failure;
875 nl_list_for_each_entry(utmpl, &tmpl->usertmpl_list, utmpl_list) {
876 struct xfrm_user_tmpl* tmpl;
880 goto nla_put_failure;
881 addr = xfrmnl_user_tmpl_get_daddr (utmpl);
884 tmpl->id.spi = htonl(xfrmnl_user_tmpl_get_spi (utmpl));
885 tmpl->id.proto = xfrmnl_user_tmpl_get_proto (utmpl);
886 tmpl->family = xfrmnl_user_tmpl_get_family (utmpl);
887 addr = xfrmnl_user_tmpl_get_saddr (utmpl);
890 tmpl->reqid = xfrmnl_user_tmpl_get_reqid (utmpl);
891 tmpl->mode = xfrmnl_user_tmpl_get_mode (utmpl);
892 tmpl->share = xfrmnl_user_tmpl_get_share (utmpl);
893 tmpl->optional = xfrmnl_user_tmpl_get_optional (utmpl);
894 tmpl->aalgos = xfrmnl_user_tmpl_get_aalgos (utmpl);
895 tmpl->ealgos = xfrmnl_user_tmpl_get_ealgos (utmpl);
896 tmpl->calgos = xfrmnl_user_tmpl_get_calgos (utmpl);
901 if (tmpl->ce_mask & XFRM_SP_ATTR_MARK) {
902 NLA_PUT (msg, XFRMA_MARK,
sizeof (
struct xfrm_mark), &tmpl->mark);
918int xfrmnl_sp_build_add_request(
struct xfrmnl_sp* tmpl,
int flags,
struct nl_msg **result)
920 return build_xfrm_sp_message (tmpl, XFRM_MSG_NEWPOLICY, flags, result);
923int xfrmnl_sp_add(
struct nl_sock* sk,
struct xfrmnl_sp* tmpl,
int flags)
928 if ((err = xfrmnl_sp_build_add_request(tmpl, flags, &msg)) < 0)
944int xfrmnl_sp_build_update_request(
struct xfrmnl_sp* tmpl,
int flags,
struct nl_msg **result)
946 return build_xfrm_sp_message (tmpl, XFRM_MSG_UPDPOLICY, flags, result);
949int xfrmnl_sp_update(
struct nl_sock* sk,
struct xfrmnl_sp* tmpl,
int flags)
954 if ((err = xfrmnl_sp_build_update_request(tmpl, flags, &msg)) < 0)
980static int build_xfrm_sp_delete_message(
struct xfrmnl_sp *tmpl,
int cmd,
int flags,
struct nl_msg **result)
983 struct xfrm_userpolicy_id spid;
984 struct nl_addr* addr;
987 if (!(tmpl->ce_mask & XFRM_SP_ATTR_DIR) ||
988 (!(tmpl->ce_mask & XFRM_SP_ATTR_INDEX) &&
989 !(tmpl->ce_mask & XFRM_SP_ATTR_SEL)))
990 return -NLE_MISSING_ATTR;
992 memset(&spid, 0,
sizeof(spid));
993 spid.dir = tmpl->dir;
994 if(tmpl->ce_mask & XFRM_SP_ATTR_INDEX)
995 spid.index = tmpl->index;
997 if (tmpl->ce_mask & XFRM_SP_ATTR_SEL)
999 addr = xfrmnl_sel_get_daddr (tmpl->sel);
1001 addr = xfrmnl_sel_get_saddr (tmpl->sel);
1003 spid.sel.dport = htons (xfrmnl_sel_get_dport (tmpl->sel));
1004 spid.sel.dport_mask = htons (xfrmnl_sel_get_dportmask (tmpl->sel));
1005 spid.sel.sport = htons (xfrmnl_sel_get_sport (tmpl->sel));
1006 spid.sel.sport_mask = htons (xfrmnl_sel_get_sportmask (tmpl->sel));
1007 spid.sel.family = xfrmnl_sel_get_family (tmpl->sel);
1008 spid.sel.prefixlen_d = xfrmnl_sel_get_prefixlen_d (tmpl->sel);
1009 spid.sel.prefixlen_s = xfrmnl_sel_get_prefixlen_s (tmpl->sel);
1010 spid.sel.proto = xfrmnl_sel_get_proto (tmpl->sel);
1011 spid.sel.ifindex = xfrmnl_sel_get_ifindex (tmpl->sel);
1012 spid.sel.user = xfrmnl_sel_get_userid (tmpl->sel);
1019 if (
nlmsg_append(msg, &spid,
sizeof(spid), NLMSG_ALIGNTO) < 0)
1020 goto nla_put_failure;
1022 if (tmpl->ce_mask & XFRM_SP_ATTR_SECCTX) {
1023 len = (
sizeof (
struct xfrm_user_sec_ctx)) + tmpl->sec_ctx->ctx_len;
1024 NLA_PUT (msg, XFRMA_SEC_CTX, len, tmpl->sec_ctx);
1027 if (tmpl->ce_mask & XFRM_SP_ATTR_MARK) {
1028 len =
sizeof (
struct xfrm_mark);
1029 NLA_PUT (msg, XFRMA_MARK, len, &tmpl->mark);
1037 return -NLE_MSGSIZE;
1045int xfrmnl_sp_build_delete_request(
struct xfrmnl_sp* tmpl,
int flags,
struct nl_msg **result)
1047 return build_xfrm_sp_delete_message (tmpl, XFRM_MSG_DELPOLICY, flags, result);
1050int xfrmnl_sp_delete(
struct nl_sock* sk,
struct xfrmnl_sp* tmpl,
int flags)
1055 if ((err = xfrmnl_sp_build_delete_request(tmpl, flags, &msg)) < 0)
1076 if (sp->ce_mask & XFRM_SP_ATTR_SEL)
1086 xfrmnl_sel_put (sp->sel);
1089 xfrmnl_sel_get (sel);
1091 sp->ce_mask |= XFRM_SP_ATTR_SEL;
1098 if (sp->ce_mask & XFRM_SP_ATTR_LTIME_CFG)
1108 xfrmnl_ltime_cfg_put (sp->lft);
1111 xfrmnl_ltime_cfg_get (ltime);
1113 sp->ce_mask |= XFRM_SP_ATTR_LTIME_CFG;
1118int xfrmnl_sp_get_curlifetime (
struct xfrmnl_sp* sp,
unsigned long long int* curr_bytes,
1119 unsigned long long int* curr_packets,
unsigned long long int* curr_add_time,
unsigned long long int* curr_use_time)
1121 if (sp == NULL || curr_bytes == NULL || curr_packets == NULL || curr_add_time == NULL || curr_use_time == NULL)
1124 *curr_bytes = sp->curlft.bytes;
1125 *curr_packets = sp->curlft.packets;
1126 *curr_add_time = sp->curlft.add_time;
1127 *curr_use_time = sp->curlft.use_time;
1132int xfrmnl_sp_get_priority (
struct xfrmnl_sp* sp)
1134 if (sp->ce_mask & XFRM_SP_ATTR_PRIO)
1135 return sp->priority;
1140int xfrmnl_sp_set_priority (
struct xfrmnl_sp* sp,
unsigned int prio)
1142 sp->priority = prio;
1143 sp->ce_mask |= XFRM_SP_ATTR_PRIO;
1148int xfrmnl_sp_get_index (
struct xfrmnl_sp* sp)
1150 if (sp->ce_mask & XFRM_SP_ATTR_INDEX)
1156int xfrmnl_sp_set_index (
struct xfrmnl_sp* sp,
unsigned int index)
1159 sp->ce_mask |= XFRM_SP_ATTR_INDEX;
1164int xfrmnl_sp_get_dir (
struct xfrmnl_sp* sp)
1166 if (sp->ce_mask & XFRM_SP_ATTR_DIR)
1172int xfrmnl_sp_set_dir (
struct xfrmnl_sp* sp,
unsigned int dir)
1175 sp->ce_mask |= XFRM_SP_ATTR_DIR;
1180int xfrmnl_sp_get_action (
struct xfrmnl_sp* sp)
1182 if (sp->ce_mask & XFRM_SP_ATTR_ACTION)
1188int xfrmnl_sp_set_action (
struct xfrmnl_sp* sp,
unsigned int action)
1190 sp->action = action;
1191 sp->ce_mask |= XFRM_SP_ATTR_ACTION;
1196int xfrmnl_sp_get_flags (
struct xfrmnl_sp* sp)
1198 if (sp->ce_mask & XFRM_SP_ATTR_FLAGS)
1204int xfrmnl_sp_set_flags (
struct xfrmnl_sp* sp,
unsigned int flags)
1207 sp->ce_mask |= XFRM_SP_ATTR_FLAGS;
1212int xfrmnl_sp_get_share (
struct xfrmnl_sp* sp)
1214 if (sp->ce_mask & XFRM_SP_ATTR_SHARE)
1220int xfrmnl_sp_set_share (
struct xfrmnl_sp* sp,
unsigned int share)
1223 sp->ce_mask |= XFRM_SP_ATTR_SHARE;
1249int 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)
1251 if (sp->ce_mask & XFRM_SP_ATTR_SECCTX)
1256 *exttype = sp->sec_ctx->exttype;
1258 *alg = sp->sec_ctx->ctx_alg;
1260 *doi = sp->sec_ctx->ctx_doi;
1262 *ctx_len = sp->sec_ctx->ctx_len;
1264 memcpy ((
void *)ctx_str, (
void *)sp->sec_ctx->ctx, sp->sec_ctx->ctx_len);
1284int 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)
1289 if ((sp->sec_ctx = calloc (1,
sizeof (
struct xfrmnl_user_sec_ctx) + 1 + ctx_len)) == NULL)
1294 sp->sec_ctx->exttype = exttype;
1295 sp->sec_ctx->ctx_alg = alg;
1296 sp->sec_ctx->ctx_doi = doi;
1297 sp->sec_ctx->ctx_len = ctx_len;
1298 memcpy ((
void *)sp->sec_ctx->ctx, (
void *)ctx_str, ctx_len);
1299 sp->sec_ctx->ctx[ctx_len] =
'\0';
1301 sp->ce_mask |= XFRM_SP_ATTR_SECCTX;
1306int xfrmnl_sp_get_userpolicy_type (
struct xfrmnl_sp* sp)
1308 if (sp->ce_mask & XFRM_SP_ATTR_POLTYPE)
1309 return sp->uptype.type;
1314int xfrmnl_sp_set_userpolicy_type (
struct xfrmnl_sp* sp,
unsigned int type)
1316 sp->uptype.type = type;
1317 sp->ce_mask |= XFRM_SP_ATTR_POLTYPE;
1324 nl_list_add_tail(&utmpl->utmpl_list, &sp->usertmpl_list);
1326 sp->ce_mask |= XFRM_SP_ATTR_TMPL;
1331 if (sp->ce_mask & XFRM_SP_ATTR_TMPL) {
1333 nl_list_del(&utmpl->utmpl_list);
1334 if (sp->nr_user_tmpl == 0)
1335 sp->ce_mask &= ~XFRM_SP_ATTR_TMPL;
1341 if (sp->ce_mask & XFRM_SP_ATTR_TMPL)
1342 return &sp->usertmpl_list;
1347int xfrmnl_sp_get_nusertemplates(
struct xfrmnl_sp *sp)
1349 if (sp->ce_mask & XFRM_SP_ATTR_TMPL)
1350 return sp->nr_user_tmpl;
1355void xfrmnl_sp_foreach_usertemplate(
struct xfrmnl_sp *r,
1361 if (r->ce_mask & XFRM_SP_ATTR_TMPL) {
1362 nl_list_for_each_entry(utmpl, &r->usertmpl_list, utmpl_list) {
1372 if (r->ce_mask & XFRM_SP_ATTR_TMPL && n >= 0 &&
1373 ((
unsigned)n) < r->nr_user_tmpl) {
1377 nl_list_for_each_entry(utmpl, &r->usertmpl_list, utmpl_list) {
1378 if (i == ((
unsigned)n))
1386int xfrmnl_sp_get_mark (
struct xfrmnl_sp* sp,
unsigned int* mark_mask,
unsigned int* mark_value)
1388 if (mark_mask == NULL || mark_value == NULL)
1391 if (sp->ce_mask & XFRM_SP_ATTR_MARK)
1393 *mark_mask = sp->mark.m;
1394 *mark_value = sp->mark.v;
1402int xfrmnl_sp_set_mark (
struct xfrmnl_sp* sp,
unsigned int value,
unsigned int mask)
1406 sp->ce_mask |= XFRM_SP_ATTR_MARK;
1413static struct nl_object_ops xfrm_sp_obj_ops = {
1414 .oo_name =
"xfrm/sp",
1416 .oo_constructor = xfrm_sp_alloc_data,
1417 .oo_free_data = xfrm_sp_free_data,
1418 .oo_clone = xfrm_sp_clone,
1424 .oo_compare = xfrm_sp_compare,
1425 .oo_attrs2str = xfrm_sp_attrs2str,
1426 .oo_id_attrs = (XFRM_SP_ATTR_SEL | XFRM_SP_ATTR_INDEX | XFRM_SP_ATTR_DIR),
1429static struct nl_af_group xfrm_sp_groups[] = {
1430 { AF_UNSPEC, XFRMNLGRP_POLICY },
1431 { END_OF_GROUP_LIST },
1434static struct nl_cache_ops xfrmnl_sp_ops = {
1435 .co_name =
"xfrm/sp",
1436 .co_hdrsize =
sizeof(
struct xfrm_userpolicy_info),
1438 { XFRM_MSG_NEWPOLICY, NL_ACT_NEW,
"new" },
1439 { XFRM_MSG_DELPOLICY, NL_ACT_DEL,
"del" },
1440 { XFRM_MSG_GETPOLICY, NL_ACT_GET,
"get" },
1441 { XFRM_MSG_UPDPOLICY, NL_ACT_NEW,
"update" },
1442 END_OF_MSGTYPES_LIST,
1444 .co_protocol = NETLINK_XFRM,
1445 .co_groups = xfrm_sp_groups,
1446 .co_request_update = xfrm_sp_request_update,
1447 .co_msg_parser = xfrm_sp_msg_parser,
1448 .co_obj_ops = &xfrm_sp_obj_ops,
1456static void _nl_init xfrm_sp_init(
void)
1461static void _nl_exit xfrm_sp_exit(
void)
struct xfrmnl_user_tmpl * xfrmnl_user_tmpl_clone(struct xfrmnl_user_tmpl *utmpl)
Clone existing user template object.
int xfrmnl_sel_cmp(struct xfrmnl_sel *a, struct xfrmnl_sel *b)
Compares two selector objects.
struct xfrmnl_ltime_cfg * xfrmnl_ltime_cfg_alloc()
Allocate new lifetime config object.
int xfrmnl_ltime_cfg_cmp(struct xfrmnl_ltime_cfg *a, struct xfrmnl_ltime_cfg *b)
Compares two lifetime config objects.
struct xfrmnl_ltime_cfg * xfrmnl_ltime_cfg_clone(struct xfrmnl_ltime_cfg *ltime)
Clone existing lifetime config object.
struct xfrmnl_sel * xfrmnl_sel_alloc()
Allocate new selector object.
int xfrmnl_user_tmpl_cmp(struct xfrmnl_user_tmpl *a, struct xfrmnl_user_tmpl *b)
Compares two user template objects.
struct xfrmnl_sel * xfrmnl_sel_clone(struct xfrmnl_sel *sel)
Clone existing selector object.
struct xfrmnl_user_tmpl * xfrmnl_user_tmpl_alloc()
Allocate new user template object.
void nl_addr_set_prefixlen(struct nl_addr *addr, int prefixlen)
Set the prefix length of an abstract address.
void * nl_addr_get_binary_addr(const struct nl_addr *addr)
Get binary address of abstract address object.
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.
unsigned int nl_addr_get_len(const struct nl_addr *addr)
Get length of binary address of abstract address object.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
int nla_len(const struct nlattr *nla)
Return length of the payload .
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
struct nl_object * nl_cache_get_next(struct nl_object *obj)
Return the next element in the cache.
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 nl_object * nl_cache_get_first(struct nl_cache *cache)
Return the first element in the cache.
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
void * nlmsg_reserve(struct nl_msg *n, size_t len, int pad)
Reserve room for additional data in a netlink message.
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
parse attributes of a netlink message
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
int nl_send_auto(struct nl_sock *sk, struct nl_msg *msg)
Finalize and transmit Netlink message.
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
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.
int nl_wait_for_ack(struct nl_sock *sk)
Wait for ACK.
int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf, size_t size)
Construct and transmit a Netlink message.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
@ NL_DUMP_STATS
Dump all attributes including statistics.
@ NL_DUMP_LINE
Dump object briefly on one line.
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Attribute validation policy.
uint16_t minlen
Minimal length of payload required.
uint16_t type
Type of attribute or NLA_UNSPEC.