1 /* xfrm_user.c: User interface to configure xfrm engine.
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13 #include <linux/crypto.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/socket.h>
19 #include <linux/string.h>
20 #include <linux/net.h>
21 #include <linux/skbuff.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/pfkeyv2.h>
24 #include <linux/ipsec.h>
25 #include <linux/init.h>
26 #include <linux/security.h>
29 #include <net/netlink.h>
30 #include <asm/uaccess.h>
31 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
32 #include <linux/in6.h>
35 static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
37 struct rtattr *rt = xfrma[type - 1];
38 struct xfrm_algo *algp;
44 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
50 len -= (algp->alg_key_len + 7U) / 8;
56 if (!algp->alg_key_len &&
57 strcmp(algp->alg_name, "digest_null") != 0)
62 if (!algp->alg_key_len &&
63 strcmp(algp->alg_name, "cipher_null") != 0)
68 /* Zero length keys are legal. */
75 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
79 static int verify_encap_tmpl(struct rtattr **xfrma)
81 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
82 struct xfrm_encap_tmpl *encap;
87 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
93 static int verify_one_addr(struct rtattr **xfrma, enum xfrm_attr_type_t type,
94 xfrm_address_t **addrp)
96 struct rtattr *rt = xfrma[type - 1];
101 if ((rt->rta_len - sizeof(*rt)) < sizeof(**addrp))
105 *addrp = RTA_DATA(rt);
110 static inline int verify_sec_ctx_len(struct rtattr **xfrma)
112 struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
113 struct xfrm_user_sec_ctx *uctx;
119 if (rt->rta_len < sizeof(*uctx))
124 len += sizeof(struct xfrm_user_sec_ctx);
125 len += uctx->ctx_len;
127 if (uctx->len != len)
134 static int verify_newsa_info(struct xfrm_usersa_info *p,
135 struct rtattr **xfrma)
145 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
157 switch (p->id.proto) {
159 if (!xfrma[XFRMA_ALG_AUTH-1] ||
160 xfrma[XFRMA_ALG_CRYPT-1] ||
161 xfrma[XFRMA_ALG_COMP-1])
166 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
167 !xfrma[XFRMA_ALG_CRYPT-1]) ||
168 xfrma[XFRMA_ALG_COMP-1])
173 if (!xfrma[XFRMA_ALG_COMP-1] ||
174 xfrma[XFRMA_ALG_AUTH-1] ||
175 xfrma[XFRMA_ALG_CRYPT-1])
179 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
180 case IPPROTO_DSTOPTS:
181 case IPPROTO_ROUTING:
182 if (xfrma[XFRMA_ALG_COMP-1] ||
183 xfrma[XFRMA_ALG_AUTH-1] ||
184 xfrma[XFRMA_ALG_CRYPT-1] ||
185 xfrma[XFRMA_ENCAP-1] ||
186 xfrma[XFRMA_SEC_CTX-1] ||
187 !xfrma[XFRMA_COADDR-1])
196 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
198 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
200 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
202 if ((err = verify_encap_tmpl(xfrma)))
204 if ((err = verify_sec_ctx_len(xfrma)))
206 if ((err = verify_one_addr(xfrma, XFRMA_COADDR, NULL)))
211 case XFRM_MODE_TRANSPORT:
212 case XFRM_MODE_TUNNEL:
213 case XFRM_MODE_ROUTEOPTIMIZATION:
226 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
227 struct xfrm_algo_desc *(*get_byname)(char *, int),
228 struct rtattr *u_arg)
230 struct rtattr *rta = u_arg;
231 struct xfrm_algo *p, *ualg;
232 struct xfrm_algo_desc *algo;
238 ualg = RTA_DATA(rta);
240 algo = get_byname(ualg->alg_name, 1);
243 *props = algo->desc.sadb_alg_id;
245 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
246 p = kmalloc(len, GFP_KERNEL);
250 memcpy(p, ualg, len);
251 strcpy(p->alg_name, algo->name);
256 static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
258 struct rtattr *rta = u_arg;
259 struct xfrm_encap_tmpl *p, *uencap;
264 uencap = RTA_DATA(rta);
265 p = kmalloc(sizeof(*p), GFP_KERNEL);
269 memcpy(p, uencap, sizeof(*p));
275 static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
277 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
281 len += sizeof(struct xfrm_user_sec_ctx);
282 len += xfrm_ctx->ctx_len;
287 static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
289 struct xfrm_user_sec_ctx *uctx;
294 uctx = RTA_DATA(u_arg);
295 return security_xfrm_state_alloc(x, uctx);
298 static int attach_one_addr(xfrm_address_t **addrpp, struct rtattr *u_arg)
300 struct rtattr *rta = u_arg;
301 xfrm_address_t *p, *uaddrp;
306 uaddrp = RTA_DATA(rta);
307 p = kmalloc(sizeof(*p), GFP_KERNEL);
311 memcpy(p, uaddrp, sizeof(*p));
316 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
318 memcpy(&x->id, &p->id, sizeof(x->id));
319 memcpy(&x->sel, &p->sel, sizeof(x->sel));
320 memcpy(&x->lft, &p->lft, sizeof(x->lft));
321 x->props.mode = p->mode;
322 x->props.replay_window = p->replay_window;
323 x->props.reqid = p->reqid;
324 x->props.family = p->family;
325 x->props.saddr = p->saddr;
326 x->props.flags = p->flags;
330 * someday when pfkey also has support, we could have the code
331 * somehow made shareable and move it to xfrm_state.c - JHS
334 static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
337 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
338 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
339 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
340 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
343 struct xfrm_replay_state *replay;
344 if (RTA_PAYLOAD(rp) < sizeof(*replay))
346 replay = RTA_DATA(rp);
347 memcpy(&x->replay, replay, sizeof(*replay));
348 memcpy(&x->preplay, replay, sizeof(*replay));
352 struct xfrm_lifetime_cur *ltime;
353 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
355 ltime = RTA_DATA(lt);
356 x->curlft.bytes = ltime->bytes;
357 x->curlft.packets = ltime->packets;
358 x->curlft.add_time = ltime->add_time;
359 x->curlft.use_time = ltime->use_time;
363 if (RTA_PAYLOAD(et) < sizeof(u32))
365 x->replay_maxage = *(u32*)RTA_DATA(et);
369 if (RTA_PAYLOAD(rt) < sizeof(u32))
371 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
379 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
380 struct rtattr **xfrma,
383 struct xfrm_state *x = xfrm_state_alloc();
389 copy_from_user_state(x, p);
391 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
392 xfrm_aalg_get_byname,
393 xfrma[XFRMA_ALG_AUTH-1])))
395 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
396 xfrm_ealg_get_byname,
397 xfrma[XFRMA_ALG_CRYPT-1])))
399 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
400 xfrm_calg_get_byname,
401 xfrma[XFRMA_ALG_COMP-1])))
403 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
405 if ((err = attach_one_addr(&x->coaddr, xfrma[XFRMA_COADDR-1])))
407 err = xfrm_init_state(x);
411 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
415 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
416 /* sysctl_xfrm_aevent_etime is in 100ms units */
417 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
418 x->preplay.bitmap = 0;
419 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
420 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
422 /* override default values from above */
424 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
431 x->km.state = XFRM_STATE_DEAD;
438 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
440 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
441 struct xfrm_state *x;
445 err = verify_newsa_info(p, (struct rtattr **)xfrma);
449 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
454 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
455 err = xfrm_state_add(x);
457 err = xfrm_state_update(x);
460 x->km.state = XFRM_STATE_DEAD;
465 c.seq = nlh->nlmsg_seq;
466 c.pid = nlh->nlmsg_pid;
467 c.event = nlh->nlmsg_type;
469 km_state_notify(x, &c);
475 static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
476 struct rtattr **xfrma,
479 struct xfrm_state *x = NULL;
482 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
484 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
486 xfrm_address_t *saddr = NULL;
488 err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr);
497 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
507 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
509 struct xfrm_state *x;
512 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
514 x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err);
518 if ((err = security_xfrm_state_delete(x)) != 0)
521 if (xfrm_state_kern(x)) {
526 err = xfrm_state_delete(x);
530 c.seq = nlh->nlmsg_seq;
531 c.pid = nlh->nlmsg_pid;
532 c.event = nlh->nlmsg_type;
533 km_state_notify(x, &c);
540 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
542 memcpy(&p->id, &x->id, sizeof(p->id));
543 memcpy(&p->sel, &x->sel, sizeof(p->sel));
544 memcpy(&p->lft, &x->lft, sizeof(p->lft));
545 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
546 memcpy(&p->stats, &x->stats, sizeof(p->stats));
547 p->saddr = x->props.saddr;
548 p->mode = x->props.mode;
549 p->replay_window = x->props.replay_window;
550 p->reqid = x->props.reqid;
551 p->family = x->props.family;
552 p->flags = x->props.flags;
556 struct xfrm_dump_info {
557 struct sk_buff *in_skb;
558 struct sk_buff *out_skb;
565 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
567 struct xfrm_dump_info *sp = ptr;
568 struct sk_buff *in_skb = sp->in_skb;
569 struct sk_buff *skb = sp->out_skb;
570 struct xfrm_usersa_info *p;
571 struct nlmsghdr *nlh;
572 unsigned char *b = skb->tail;
574 if (sp->this_idx < sp->start_idx)
577 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
579 XFRM_MSG_NEWSA, sizeof(*p));
580 nlh->nlmsg_flags = sp->nlmsg_flags;
583 copy_to_user_state(x, p);
586 RTA_PUT(skb, XFRMA_ALG_AUTH,
587 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
589 RTA_PUT(skb, XFRMA_ALG_CRYPT,
590 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
592 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
595 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
598 int ctx_size = sizeof(struct xfrm_sec_ctx) +
599 x->security->ctx_len;
600 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
601 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
603 uctx->exttype = XFRMA_SEC_CTX;
604 uctx->len = ctx_size;
605 uctx->ctx_doi = x->security->ctx_doi;
606 uctx->ctx_alg = x->security->ctx_alg;
607 uctx->ctx_len = x->security->ctx_len;
608 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
612 RTA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
615 RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused);
617 nlh->nlmsg_len = skb->tail - b;
624 skb_trim(skb, b - skb->data);
628 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
630 struct xfrm_dump_info info;
632 info.in_skb = cb->skb;
634 info.nlmsg_seq = cb->nlh->nlmsg_seq;
635 info.nlmsg_flags = NLM_F_MULTI;
637 info.start_idx = cb->args[0];
638 (void) xfrm_state_walk(0, dump_one_state, &info);
639 cb->args[0] = info.this_idx;
644 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
645 struct xfrm_state *x, u32 seq)
647 struct xfrm_dump_info info;
650 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
652 return ERR_PTR(-ENOMEM);
654 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
655 info.in_skb = in_skb;
657 info.nlmsg_seq = seq;
658 info.nlmsg_flags = 0;
659 info.this_idx = info.start_idx = 0;
661 if (dump_one_state(x, 0, &info)) {
669 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
671 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
672 struct xfrm_state *x;
673 struct sk_buff *resp_skb;
676 x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err);
680 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
681 if (IS_ERR(resp_skb)) {
682 err = PTR_ERR(resp_skb);
684 err = netlink_unicast(xfrm_nl, resp_skb,
685 NETLINK_CB(skb).pid, MSG_DONTWAIT);
692 static int verify_userspi_info(struct xfrm_userspi_info *p)
694 switch (p->info.id.proto) {
700 /* IPCOMP spi is 16-bits. */
701 if (p->max >= 0x10000)
715 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
717 struct xfrm_state *x;
718 struct xfrm_userspi_info *p;
719 struct sk_buff *resp_skb;
720 xfrm_address_t *daddr;
725 err = verify_userspi_info(p);
729 family = p->info.family;
730 daddr = &p->info.id.daddr;
734 x = xfrm_find_acq_byseq(p->info.seq);
735 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
742 x = xfrm_find_acq(p->info.mode, p->info.reqid,
743 p->info.id.proto, daddr,
750 resp_skb = ERR_PTR(-ENOENT);
752 spin_lock_bh(&x->lock);
753 if (x->km.state != XFRM_STATE_DEAD) {
754 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
756 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
758 spin_unlock_bh(&x->lock);
760 if (IS_ERR(resp_skb)) {
761 err = PTR_ERR(resp_skb);
765 err = netlink_unicast(xfrm_nl, resp_skb,
766 NETLINK_CB(skb).pid, MSG_DONTWAIT);
774 static int verify_policy_dir(__u8 dir)
778 case XFRM_POLICY_OUT:
779 case XFRM_POLICY_FWD:
789 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
793 case XFRM_SHARE_SESSION:
794 case XFRM_SHARE_USER:
795 case XFRM_SHARE_UNIQUE:
803 case XFRM_POLICY_ALLOW:
804 case XFRM_POLICY_BLOCK:
811 switch (p->sel.family) {
816 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
819 return -EAFNOSUPPORT;
826 return verify_policy_dir(p->dir);
829 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
831 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
832 struct xfrm_user_sec_ctx *uctx;
838 return security_xfrm_policy_alloc(pol, uctx);
841 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
847 for (i = 0; i < nr; i++, ut++) {
848 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
850 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
851 memcpy(&t->saddr, &ut->saddr,
852 sizeof(xfrm_address_t));
853 t->reqid = ut->reqid;
855 t->share = ut->share;
856 t->optional = ut->optional;
857 t->aalgos = ut->aalgos;
858 t->ealgos = ut->ealgos;
859 t->calgos = ut->calgos;
863 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
865 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
866 struct xfrm_user_tmpl *utmpl;
872 nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
874 if (nr > XFRM_MAX_DEPTH)
877 copy_templates(pol, RTA_DATA(rt), nr);
882 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
884 xp->priority = p->priority;
885 xp->index = p->index;
886 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
887 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
888 xp->action = p->action;
889 xp->flags = p->flags;
890 xp->family = p->sel.family;
891 /* XXX xp->share = p->share; */
894 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
896 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
897 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
898 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
899 p->priority = xp->priority;
900 p->index = xp->index;
901 p->sel.family = xp->family;
903 p->action = xp->action;
904 p->flags = xp->flags;
905 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
908 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
910 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
918 copy_from_user_policy(xp, p);
920 if (!(err = copy_from_user_tmpl(xp, xfrma)))
921 err = copy_from_user_sec_ctx(xp, xfrma);
932 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
934 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
935 struct xfrm_policy *xp;
940 err = verify_newpolicy_info(p);
943 err = verify_sec_ctx_len((struct rtattr **)xfrma);
947 xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
951 /* shouldnt excl be based on nlh flags??
952 * Aha! this is anti-netlink really i.e more pfkey derived
953 * in netlink excl is a flag and you wouldnt need
954 * a type XFRM_MSG_UPDPOLICY - JHS */
955 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
956 err = xfrm_policy_insert(p->dir, xp, excl);
958 security_xfrm_policy_free(xp);
963 c.event = nlh->nlmsg_type;
964 c.seq = nlh->nlmsg_seq;
965 c.pid = nlh->nlmsg_pid;
966 km_policy_notify(xp, p->dir, &c);
973 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
975 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
978 if (xp->xfrm_nr == 0)
981 for (i = 0; i < xp->xfrm_nr; i++) {
982 struct xfrm_user_tmpl *up = &vec[i];
983 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
985 memcpy(&up->id, &kp->id, sizeof(up->id));
986 up->family = xp->family;
987 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
988 up->reqid = kp->reqid;
990 up->share = kp->share;
991 up->optional = kp->optional;
992 up->aalgos = kp->aalgos;
993 up->ealgos = kp->ealgos;
994 up->calgos = kp->calgos;
996 RTA_PUT(skb, XFRMA_TMPL,
997 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
1006 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
1008 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
1009 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
1010 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1012 uctx->exttype = XFRMA_SEC_CTX;
1013 uctx->len = ctx_size;
1014 uctx->ctx_doi = s->ctx_doi;
1015 uctx->ctx_alg = s->ctx_alg;
1016 uctx->ctx_len = s->ctx_len;
1017 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
1024 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1027 return copy_sec_ctx(x->security, skb);
1032 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1035 return copy_sec_ctx(xp->security, skb);
1040 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1042 struct xfrm_dump_info *sp = ptr;
1043 struct xfrm_userpolicy_info *p;
1044 struct sk_buff *in_skb = sp->in_skb;
1045 struct sk_buff *skb = sp->out_skb;
1046 struct nlmsghdr *nlh;
1047 unsigned char *b = skb->tail;
1049 if (sp->this_idx < sp->start_idx)
1052 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
1054 XFRM_MSG_NEWPOLICY, sizeof(*p));
1055 p = NLMSG_DATA(nlh);
1056 nlh->nlmsg_flags = sp->nlmsg_flags;
1058 copy_to_user_policy(xp, p, dir);
1059 if (copy_to_user_tmpl(xp, skb) < 0)
1061 if (copy_to_user_sec_ctx(xp, skb))
1064 nlh->nlmsg_len = skb->tail - b;
1070 skb_trim(skb, b - skb->data);
1074 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1076 struct xfrm_dump_info info;
1078 info.in_skb = cb->skb;
1080 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1081 info.nlmsg_flags = NLM_F_MULTI;
1083 info.start_idx = cb->args[0];
1084 (void) xfrm_policy_walk(dump_one_policy, &info);
1085 cb->args[0] = info.this_idx;
1090 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1091 struct xfrm_policy *xp,
1094 struct xfrm_dump_info info;
1095 struct sk_buff *skb;
1097 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1099 return ERR_PTR(-ENOMEM);
1101 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
1102 info.in_skb = in_skb;
1104 info.nlmsg_seq = seq;
1105 info.nlmsg_flags = 0;
1106 info.this_idx = info.start_idx = 0;
1108 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1116 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1118 struct xfrm_policy *xp;
1119 struct xfrm_userpolicy_id *p;
1124 p = NLMSG_DATA(nlh);
1125 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1127 err = verify_policy_dir(p->dir);
1132 xp = xfrm_policy_byid(p->dir, p->index, delete);
1134 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1135 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1136 struct xfrm_policy tmp;
1138 err = verify_sec_ctx_len(rtattrs);
1142 memset(&tmp, 0, sizeof(struct xfrm_policy));
1144 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1146 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1149 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, delete);
1150 security_xfrm_policy_free(&tmp);
1156 struct sk_buff *resp_skb;
1158 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1159 if (IS_ERR(resp_skb)) {
1160 err = PTR_ERR(resp_skb);
1162 err = netlink_unicast(xfrm_nl, resp_skb,
1163 NETLINK_CB(skb).pid,
1167 if ((err = security_xfrm_policy_delete(xp)) != 0)
1169 c.data.byid = p->index;
1170 c.event = nlh->nlmsg_type;
1171 c.seq = nlh->nlmsg_seq;
1172 c.pid = nlh->nlmsg_pid;
1173 km_policy_notify(xp, p->dir, &c);
1182 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1185 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1187 xfrm_state_flush(p->proto);
1188 c.data.proto = p->proto;
1189 c.event = nlh->nlmsg_type;
1190 c.seq = nlh->nlmsg_seq;
1191 c.pid = nlh->nlmsg_pid;
1192 km_state_notify(NULL, &c);
1198 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1200 struct xfrm_aevent_id *id;
1201 struct nlmsghdr *nlh;
1202 struct xfrm_lifetime_cur ltime;
1203 unsigned char *b = skb->tail;
1205 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1206 id = NLMSG_DATA(nlh);
1207 nlh->nlmsg_flags = 0;
1209 id->sa_id.daddr = x->id.daddr;
1210 id->sa_id.spi = x->id.spi;
1211 id->sa_id.family = x->props.family;
1212 id->sa_id.proto = x->id.proto;
1213 id->flags = c->data.aevent;
1215 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1217 ltime.bytes = x->curlft.bytes;
1218 ltime.packets = x->curlft.packets;
1219 ltime.add_time = x->curlft.add_time;
1220 ltime.use_time = x->curlft.use_time;
1222 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), <ime);
1224 if (id->flags&XFRM_AE_RTHR) {
1225 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1228 if (id->flags&XFRM_AE_ETHR) {
1229 u32 etimer = x->replay_maxage*10/HZ;
1230 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1233 nlh->nlmsg_len = skb->tail - b;
1238 skb_trim(skb, b - skb->data);
1242 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1244 struct xfrm_state *x;
1245 struct sk_buff *r_skb;
1248 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1249 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1250 struct xfrm_usersa_id *id = &p->sa_id;
1252 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1253 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1255 if (p->flags&XFRM_AE_RTHR)
1256 len+=RTA_SPACE(sizeof(u32));
1258 if (p->flags&XFRM_AE_ETHR)
1259 len+=RTA_SPACE(sizeof(u32));
1261 r_skb = alloc_skb(len, GFP_ATOMIC);
1265 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1272 * XXX: is this lock really needed - none of the other
1273 * gets lock (the concern is things getting updated
1274 * while we are still reading) - jhs
1276 spin_lock_bh(&x->lock);
1277 c.data.aevent = p->flags;
1278 c.seq = nlh->nlmsg_seq;
1279 c.pid = nlh->nlmsg_pid;
1281 if (build_aevent(r_skb, x, &c) < 0)
1283 err = netlink_unicast(xfrm_nl, r_skb,
1284 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1285 spin_unlock_bh(&x->lock);
1290 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1292 struct xfrm_state *x;
1295 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1296 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1297 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1302 /* pedantic mode - thou shalt sayeth replaceth */
1303 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1306 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1310 if (x->km.state != XFRM_STATE_VALID)
1313 spin_lock_bh(&x->lock);
1314 err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
1315 spin_unlock_bh(&x->lock);
1319 c.event = nlh->nlmsg_type;
1320 c.seq = nlh->nlmsg_seq;
1321 c.pid = nlh->nlmsg_pid;
1322 c.data.aevent = XFRM_AE_CU;
1323 km_state_notify(x, &c);
1330 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1334 xfrm_policy_flush();
1335 c.event = nlh->nlmsg_type;
1336 c.seq = nlh->nlmsg_seq;
1337 c.pid = nlh->nlmsg_pid;
1338 km_policy_notify(NULL, 0, &c);
1342 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1344 struct xfrm_policy *xp;
1345 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1346 struct xfrm_userpolicy_info *p = &up->pol;
1350 xp = xfrm_policy_byid(p->dir, p->index, 0);
1352 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1353 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1354 struct xfrm_policy tmp;
1356 err = verify_sec_ctx_len(rtattrs);
1360 memset(&tmp, 0, sizeof(struct xfrm_policy));
1362 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1364 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1367 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, 0);
1368 security_xfrm_policy_free(&tmp);
1373 read_lock(&xp->lock);
1375 read_unlock(&xp->lock);
1379 read_unlock(&xp->lock);
1382 xfrm_policy_delete(xp, p->dir);
1384 // reset the timers here?
1385 printk("Dont know what to do with soft policy expire\n");
1387 km_policy_expired(xp, p->dir, up->hard, current->pid);
1394 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1396 struct xfrm_state *x;
1398 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1399 struct xfrm_usersa_info *p = &ue->state;
1401 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1409 spin_lock_bh(&x->lock);
1410 if (x->km.state != XFRM_STATE_VALID)
1412 km_state_expired(x, ue->hard, current->pid);
1415 __xfrm_state_delete(x);
1417 spin_unlock_bh(&x->lock);
1422 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1424 struct xfrm_policy *xp;
1425 struct xfrm_user_tmpl *ut;
1427 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1429 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1430 struct xfrm_state *x = xfrm_state_alloc();
1436 err = verify_newpolicy_info(&ua->policy);
1438 printk("BAD policy passed\n");
1444 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err); if (!xp) {
1449 memcpy(&x->id, &ua->id, sizeof(ua->id));
1450 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1451 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1454 /* extract the templates and for each call km_key */
1455 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1456 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1457 memcpy(&x->id, &t->id, sizeof(x->id));
1458 x->props.mode = t->mode;
1459 x->props.reqid = t->reqid;
1460 x->props.family = ut->family;
1461 t->aalgos = ua->aalgos;
1462 t->ealgos = ua->ealgos;
1463 t->calgos = ua->calgos;
1464 err = km_query(x, t, xp);
1475 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1477 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1478 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1479 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1480 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1481 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1482 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1483 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1484 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1485 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1486 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1487 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1488 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1489 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1490 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1491 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
1492 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1493 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1494 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
1499 static struct xfrm_link {
1500 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1501 int (*dump)(struct sk_buff *, struct netlink_callback *);
1502 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1503 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1504 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1505 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1506 .dump = xfrm_dump_sa },
1507 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1508 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1509 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1510 .dump = xfrm_dump_policy },
1511 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1512 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
1513 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1514 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1515 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1516 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1517 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1518 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
1519 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1520 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1523 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1525 struct rtattr *xfrma[XFRMA_MAX];
1526 struct xfrm_link *link;
1529 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1532 type = nlh->nlmsg_type;
1534 /* A control message: ignore them */
1535 if (type < XFRM_MSG_BASE)
1538 /* Unknown message: reply with EINVAL */
1539 if (type > XFRM_MSG_MAX)
1542 type -= XFRM_MSG_BASE;
1543 link = &xfrm_dispatch[type];
1545 /* All operations require privileges, even GET */
1546 if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
1551 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1552 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1553 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1554 if (link->dump == NULL)
1557 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
1558 link->dump, NULL)) != 0) {
1562 netlink_queue_skip(nlh, skb);
1566 memset(xfrma, 0, sizeof(xfrma));
1568 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1571 if (nlh->nlmsg_len > min_len) {
1572 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1573 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1575 while (RTA_OK(attr, attrlen)) {
1576 unsigned short flavor = attr->rta_type;
1578 if (flavor > XFRMA_MAX)
1580 xfrma[flavor - 1] = attr;
1582 attr = RTA_NEXT(attr, attrlen);
1586 if (link->doit == NULL)
1588 *errp = link->doit(skb, nlh, (void **) &xfrma);
1597 static void xfrm_netlink_rcv(struct sock *sk, int len)
1599 unsigned int qlen = 0;
1602 mutex_lock(&xfrm_cfg_mutex);
1603 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
1604 mutex_unlock(&xfrm_cfg_mutex);
1609 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1611 struct xfrm_user_expire *ue;
1612 struct nlmsghdr *nlh;
1613 unsigned char *b = skb->tail;
1615 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
1617 ue = NLMSG_DATA(nlh);
1618 nlh->nlmsg_flags = 0;
1620 copy_to_user_state(x, &ue->state);
1621 ue->hard = (c->data.hard != 0) ? 1 : 0;
1623 nlh->nlmsg_len = skb->tail - b;
1627 skb_trim(skb, b - skb->data);
1631 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1633 struct sk_buff *skb;
1634 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
1636 skb = alloc_skb(len, GFP_ATOMIC);
1640 if (build_expire(skb, x, c) < 0)
1643 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1644 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1647 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1649 struct sk_buff *skb;
1650 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1652 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1653 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1654 skb = alloc_skb(len, GFP_ATOMIC);
1658 if (build_aevent(skb, x, c) < 0)
1661 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1662 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1665 static int xfrm_notify_sa_flush(struct km_event *c)
1667 struct xfrm_usersa_flush *p;
1668 struct nlmsghdr *nlh;
1669 struct sk_buff *skb;
1671 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1673 skb = alloc_skb(len, GFP_ATOMIC);
1678 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1679 XFRM_MSG_FLUSHSA, sizeof(*p));
1680 nlh->nlmsg_flags = 0;
1682 p = NLMSG_DATA(nlh);
1683 p->proto = c->data.proto;
1685 nlh->nlmsg_len = skb->tail - b;
1687 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1688 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1695 static int inline xfrm_sa_len(struct xfrm_state *x)
1699 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1701 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1703 l += RTA_SPACE(sizeof(*x->calg));
1705 l += RTA_SPACE(sizeof(*x->encap));
1710 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1712 struct xfrm_usersa_info *p;
1713 struct xfrm_usersa_id *id;
1714 struct nlmsghdr *nlh;
1715 struct sk_buff *skb;
1717 int len = xfrm_sa_len(x);
1720 headlen = sizeof(*p);
1721 if (c->event == XFRM_MSG_DELSA) {
1722 len += RTA_SPACE(headlen);
1723 headlen = sizeof(*id);
1725 len += NLMSG_SPACE(headlen);
1727 skb = alloc_skb(len, GFP_ATOMIC);
1732 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
1733 nlh->nlmsg_flags = 0;
1735 p = NLMSG_DATA(nlh);
1736 if (c->event == XFRM_MSG_DELSA) {
1737 id = NLMSG_DATA(nlh);
1738 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1739 id->spi = x->id.spi;
1740 id->family = x->props.family;
1741 id->proto = x->id.proto;
1743 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1746 copy_to_user_state(x, p);
1749 RTA_PUT(skb, XFRMA_ALG_AUTH,
1750 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1752 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1753 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1755 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1758 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1760 nlh->nlmsg_len = skb->tail - b;
1762 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1763 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1771 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1775 case XFRM_MSG_EXPIRE:
1776 return xfrm_exp_state_notify(x, c);
1777 case XFRM_MSG_NEWAE:
1778 return xfrm_aevent_state_notify(x, c);
1779 case XFRM_MSG_DELSA:
1780 case XFRM_MSG_UPDSA:
1781 case XFRM_MSG_NEWSA:
1782 return xfrm_notify_sa(x, c);
1783 case XFRM_MSG_FLUSHSA:
1784 return xfrm_notify_sa_flush(c);
1786 printk("xfrm_user: Unknown SA event %d\n", c->event);
1794 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1795 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1798 struct xfrm_user_acquire *ua;
1799 struct nlmsghdr *nlh;
1800 unsigned char *b = skb->tail;
1801 __u32 seq = xfrm_get_acqseq();
1803 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1805 ua = NLMSG_DATA(nlh);
1806 nlh->nlmsg_flags = 0;
1808 memcpy(&ua->id, &x->id, sizeof(ua->id));
1809 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1810 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1811 copy_to_user_policy(xp, &ua->policy, dir);
1812 ua->aalgos = xt->aalgos;
1813 ua->ealgos = xt->ealgos;
1814 ua->calgos = xt->calgos;
1815 ua->seq = x->km.seq = seq;
1817 if (copy_to_user_tmpl(xp, skb) < 0)
1819 if (copy_to_user_state_sec_ctx(x, skb))
1822 nlh->nlmsg_len = skb->tail - b;
1826 skb_trim(skb, b - skb->data);
1830 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1831 struct xfrm_policy *xp, int dir)
1833 struct sk_buff *skb;
1836 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1837 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
1838 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1839 skb = alloc_skb(len, GFP_ATOMIC);
1843 if (build_acquire(skb, x, xt, xp, dir) < 0)
1846 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
1847 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
1850 /* User gives us xfrm_user_policy_info followed by an array of 0
1851 * or more templates.
1853 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
1854 u8 *data, int len, int *dir)
1856 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1857 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1858 struct xfrm_policy *xp;
1861 switch (sk->sk_family) {
1863 if (opt != IP_XFRM_POLICY) {
1868 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1870 if (opt != IPV6_XFRM_POLICY) {
1883 if (len < sizeof(*p) ||
1884 verify_newpolicy_info(p))
1887 nr = ((len - sizeof(*p)) / sizeof(*ut));
1888 if (nr > XFRM_MAX_DEPTH)
1891 if (p->dir > XFRM_POLICY_OUT)
1894 xp = xfrm_policy_alloc(GFP_KERNEL);
1900 copy_from_user_policy(xp, p);
1901 copy_templates(xp, ut, nr);
1903 if (!xp->security) {
1904 int err = security_xfrm_sock_policy_alloc(xp, sk);
1917 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
1918 int dir, struct km_event *c)
1920 struct xfrm_user_polexpire *upe;
1921 struct nlmsghdr *nlh;
1922 int hard = c->data.hard;
1923 unsigned char *b = skb->tail;
1925 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
1926 upe = NLMSG_DATA(nlh);
1927 nlh->nlmsg_flags = 0;
1929 copy_to_user_policy(xp, &upe->pol, dir);
1930 if (copy_to_user_tmpl(xp, skb) < 0)
1932 if (copy_to_user_sec_ctx(xp, skb))
1936 nlh->nlmsg_len = skb->tail - b;
1940 skb_trim(skb, b - skb->data);
1944 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1946 struct sk_buff *skb;
1949 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1950 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
1951 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1952 skb = alloc_skb(len, GFP_ATOMIC);
1956 if (build_polexpire(skb, xp, dir, c) < 0)
1959 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1960 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1963 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
1965 struct xfrm_userpolicy_info *p;
1966 struct xfrm_userpolicy_id *id;
1967 struct nlmsghdr *nlh;
1968 struct sk_buff *skb;
1970 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1973 headlen = sizeof(*p);
1974 if (c->event == XFRM_MSG_DELPOLICY) {
1975 len += RTA_SPACE(headlen);
1976 headlen = sizeof(*id);
1978 len += NLMSG_SPACE(headlen);
1980 skb = alloc_skb(len, GFP_ATOMIC);
1985 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
1987 p = NLMSG_DATA(nlh);
1988 if (c->event == XFRM_MSG_DELPOLICY) {
1989 id = NLMSG_DATA(nlh);
1990 memset(id, 0, sizeof(*id));
1993 id->index = xp->index;
1995 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
1997 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
2000 nlh->nlmsg_flags = 0;
2002 copy_to_user_policy(xp, p, dir);
2003 if (copy_to_user_tmpl(xp, skb) < 0)
2006 nlh->nlmsg_len = skb->tail - b;
2008 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2009 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2017 static int xfrm_notify_policy_flush(struct km_event *c)
2019 struct nlmsghdr *nlh;
2020 struct sk_buff *skb;
2022 int len = NLMSG_LENGTH(0);
2024 skb = alloc_skb(len, GFP_ATOMIC);
2030 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
2032 nlh->nlmsg_len = skb->tail - b;
2034 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2035 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2042 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2046 case XFRM_MSG_NEWPOLICY:
2047 case XFRM_MSG_UPDPOLICY:
2048 case XFRM_MSG_DELPOLICY:
2049 return xfrm_notify_policy(xp, dir, c);
2050 case XFRM_MSG_FLUSHPOLICY:
2051 return xfrm_notify_policy_flush(c);
2052 case XFRM_MSG_POLEXPIRE:
2053 return xfrm_exp_policy_notify(xp, dir, c);
2055 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2062 static int build_report(struct sk_buff *skb, u8 proto,
2063 struct xfrm_selector *sel, xfrm_address_t *addr)
2065 struct xfrm_user_report *ur;
2066 struct nlmsghdr *nlh;
2067 unsigned char *b = skb->tail;
2069 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur));
2070 ur = NLMSG_DATA(nlh);
2071 nlh->nlmsg_flags = 0;
2074 memcpy(&ur->sel, sel, sizeof(ur->sel));
2077 RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2079 nlh->nlmsg_len = skb->tail - b;
2084 skb_trim(skb, b - skb->data);
2088 static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2089 xfrm_address_t *addr)
2091 struct sk_buff *skb;
2094 len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
2095 skb = alloc_skb(len, GFP_ATOMIC);
2099 if (build_report(skb, proto, sel, addr) < 0)
2102 NETLINK_CB(skb).dst_group = XFRMNLGRP_REPORT;
2103 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2106 static struct xfrm_mgr netlink_mgr = {
2108 .notify = xfrm_send_state_notify,
2109 .acquire = xfrm_send_acquire,
2110 .compile_policy = xfrm_compile_policy,
2111 .notify_policy = xfrm_send_policy_notify,
2112 .report = xfrm_send_report,
2115 static int __init xfrm_user_init(void)
2119 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2121 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
2122 xfrm_netlink_rcv, THIS_MODULE);
2125 rcu_assign_pointer(xfrm_nl, nlsk);
2127 xfrm_register_km(&netlink_mgr);
2132 static void __exit xfrm_user_exit(void)
2134 struct sock *nlsk = xfrm_nl;
2136 xfrm_unregister_km(&netlink_mgr);
2137 rcu_assign_pointer(xfrm_nl, NULL);
2139 sock_release(nlsk->sk_socket);
2142 module_init(xfrm_user_init);
2143 module_exit(xfrm_user_exit);
2144 MODULE_LICENSE("GPL");
2145 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);