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/module.h>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/socket.h>
18 #include <linux/string.h>
19 #include <linux/net.h>
20 #include <linux/skbuff.h>
21 #include <linux/netlink.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 <asm/uaccess.h>
31 static struct sock *xfrm_nl;
33 static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
35 struct rtattr *rt = xfrma[type - 1];
36 struct xfrm_algo *algp;
41 if ((rt->rta_len - sizeof(*rt)) < sizeof(*algp))
47 if (!algp->alg_key_len &&
48 strcmp(algp->alg_name, "digest_null") != 0)
53 if (!algp->alg_key_len &&
54 strcmp(algp->alg_name, "cipher_null") != 0)
59 /* Zero length keys are legal. */
66 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
70 static int verify_encap_tmpl(struct rtattr **xfrma)
72 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
73 struct xfrm_encap_tmpl *encap;
78 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
84 static int verify_newsa_info(struct xfrm_usersa_info *p,
85 struct rtattr **xfrma)
95 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
107 switch (p->id.proto) {
109 if (!xfrma[XFRMA_ALG_AUTH-1] ||
110 xfrma[XFRMA_ALG_CRYPT-1] ||
111 xfrma[XFRMA_ALG_COMP-1])
116 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
117 !xfrma[XFRMA_ALG_CRYPT-1]) ||
118 xfrma[XFRMA_ALG_COMP-1])
123 if (!xfrma[XFRMA_ALG_COMP-1] ||
124 xfrma[XFRMA_ALG_AUTH-1] ||
125 xfrma[XFRMA_ALG_CRYPT-1])
133 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
135 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
137 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
139 if ((err = verify_encap_tmpl(xfrma)))
158 static int attach_one_algo(struct xfrm_algo **algpp, struct rtattr *u_arg)
160 struct rtattr *rta = u_arg;
161 struct xfrm_algo *p, *ualg;
166 ualg = RTA_DATA(rta);
167 p = kmalloc(sizeof(*ualg) + ualg->alg_key_len, GFP_KERNEL);
171 memcpy(p, ualg, sizeof(*ualg) + ualg->alg_key_len);
176 static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
178 struct rtattr *rta = u_arg;
179 struct xfrm_encap_tmpl *p, *uencap;
184 uencap = RTA_DATA(rta);
185 p = kmalloc(sizeof(*p), GFP_KERNEL);
189 memcpy(p, uencap, sizeof(*p));
194 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
196 memcpy(&x->id, &p->id, sizeof(x->id));
197 memcpy(&x->sel, &p->sel, sizeof(x->sel));
198 memcpy(&x->lft, &p->lft, sizeof(x->lft));
199 x->props.mode = p->mode;
200 x->props.replay_window = p->replay_window;
201 x->props.reqid = p->reqid;
202 x->props.family = p->family;
203 x->props.saddr = p->saddr;
204 x->props.flags = p->flags;
207 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
208 struct rtattr **xfrma,
211 struct xfrm_state *x = xfrm_state_alloc();
217 copy_from_user_state(x, p);
219 if ((err = attach_one_algo(&x->aalg, xfrma[XFRMA_ALG_AUTH-1])))
221 if ((err = attach_one_algo(&x->ealg, xfrma[XFRMA_ALG_CRYPT-1])))
223 if ((err = attach_one_algo(&x->calg, xfrma[XFRMA_ALG_COMP-1])))
225 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
229 x->type = xfrm_get_type(x->id.proto, x->props.family);
233 err = x->type->init_state(x, NULL);
237 x->curlft.add_time = (unsigned long) xtime.tv_sec;
238 x->km.state = XFRM_STATE_VALID;
244 x->km.state = XFRM_STATE_DEAD;
251 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
253 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
254 struct xfrm_state *x;
257 err = verify_newsa_info(p, (struct rtattr **) xfrma);
261 x = xfrm_state_construct(p, (struct rtattr **) xfrma, &err);
265 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
266 err = xfrm_state_add(x);
268 err = xfrm_state_update(x);
271 x->km.state = XFRM_STATE_DEAD;
278 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
280 struct xfrm_state *x;
281 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
283 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
287 if (xfrm_state_kern(x)) {
292 xfrm_state_delete(x);
298 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
300 memcpy(&p->id, &x->id, sizeof(p->id));
301 memcpy(&p->sel, &x->sel, sizeof(p->sel));
302 memcpy(&p->lft, &x->lft, sizeof(p->lft));
303 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
304 memcpy(&p->stats, &x->stats, sizeof(p->stats));
305 p->saddr = x->props.saddr;
306 p->mode = x->props.mode;
307 p->replay_window = x->props.replay_window;
308 p->reqid = x->props.reqid;
309 p->family = x->props.family;
310 p->flags = x->props.flags;
314 struct xfrm_dump_info {
315 struct sk_buff *in_skb;
316 struct sk_buff *out_skb;
322 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
324 struct xfrm_dump_info *sp = ptr;
325 struct sk_buff *in_skb = sp->in_skb;
326 struct sk_buff *skb = sp->out_skb;
327 struct xfrm_usersa_info *p;
328 struct nlmsghdr *nlh;
329 unsigned char *b = skb->tail;
331 if (sp->this_idx < sp->start_idx)
334 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
336 XFRM_MSG_NEWSA, sizeof(*p));
337 nlh->nlmsg_flags = 0;
340 copy_to_user_state(x, p);
343 RTA_PUT(skb, XFRMA_ALG_AUTH,
344 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
346 RTA_PUT(skb, XFRMA_ALG_CRYPT,
347 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
349 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
352 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
354 nlh->nlmsg_len = skb->tail - b;
361 skb_trim(skb, b - skb->data);
365 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
367 struct xfrm_dump_info info;
369 info.in_skb = cb->skb;
371 info.nlmsg_seq = cb->nlh->nlmsg_seq;
373 info.start_idx = cb->args[0];
374 (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
375 cb->args[0] = info.this_idx;
380 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
381 struct xfrm_state *x, u32 seq)
383 struct xfrm_dump_info info;
386 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
388 return ERR_PTR(-ENOMEM);
390 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
391 info.in_skb = in_skb;
393 info.nlmsg_seq = seq;
394 info.this_idx = info.start_idx = 0;
396 if (dump_one_state(x, 0, &info)) {
404 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
406 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
407 struct xfrm_state *x;
408 struct sk_buff *resp_skb;
411 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
416 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
417 if (IS_ERR(resp_skb)) {
418 err = PTR_ERR(resp_skb);
420 err = netlink_unicast(xfrm_nl, resp_skb,
421 NETLINK_CB(skb).pid, MSG_DONTWAIT);
428 static int verify_userspi_info(struct xfrm_userspi_info *p)
430 switch (p->info.id.proto) {
436 /* IPCOMP spi is 16-bits. */
437 if (p->max >= 0x10000)
451 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
453 struct xfrm_state *x;
454 struct xfrm_userspi_info *p;
455 struct sk_buff *resp_skb;
459 err = verify_userspi_info(p);
462 x = xfrm_find_acq(p->info.mode, p->info.reqid, p->info.id.proto,
470 resp_skb = ERR_PTR(-ENOENT);
472 spin_lock_bh(&x->lock);
473 if (x->km.state != XFRM_STATE_DEAD) {
474 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
476 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
478 spin_unlock_bh(&x->lock);
480 if (IS_ERR(resp_skb)) {
481 err = PTR_ERR(resp_skb);
485 err = netlink_unicast(xfrm_nl, resp_skb,
486 NETLINK_CB(skb).pid, MSG_DONTWAIT);
494 static int verify_policy_dir(__u8 dir)
498 case XFRM_POLICY_OUT:
499 case XFRM_POLICY_FWD:
509 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
513 case XFRM_SHARE_SESSION:
514 case XFRM_SHARE_USER:
515 case XFRM_SHARE_UNIQUE:
523 case XFRM_POLICY_ALLOW:
524 case XFRM_POLICY_BLOCK:
531 switch (p->sel.family) {
536 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
539 return -EAFNOSUPPORT;
546 return verify_policy_dir(p->dir);
549 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
555 for (i = 0; i < nr; i++, ut++) {
556 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
558 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
559 memcpy(&t->saddr, &ut->saddr,
560 sizeof(xfrm_address_t));
561 t->reqid = ut->reqid;
563 t->share = ut->share;
564 t->optional = ut->optional;
565 t->aalgos = ut->aalgos;
566 t->ealgos = ut->ealgos;
567 t->calgos = ut->calgos;
571 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
573 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
574 struct xfrm_user_tmpl *utmpl;
580 nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
582 if (nr > XFRM_MAX_DEPTH)
585 copy_templates(pol, RTA_DATA(rt), nr);
590 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
592 xp->priority = p->priority;
593 xp->index = p->index;
594 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
595 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
596 xp->action = p->action;
597 xp->flags = p->flags;
598 xp->family = p->sel.family;
599 /* XXX xp->share = p->share; */
602 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
604 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
605 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
606 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
607 p->priority = xp->priority;
608 p->index = xp->index;
609 p->sel.family = xp->family;
611 p->action = xp->action;
612 p->flags = xp->flags;
613 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
616 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
618 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
626 copy_from_user_policy(xp, p);
627 err = copy_from_user_tmpl(xp, xfrma);
637 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
639 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
640 struct xfrm_policy *xp;
644 err = verify_newpolicy_info(p);
648 xp = xfrm_policy_construct(p, (struct rtattr **) xfrma, &err);
652 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
653 err = xfrm_policy_insert(p->dir, xp, excl);
664 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
666 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
669 if (xp->xfrm_nr == 0)
672 for (i = 0; i < xp->xfrm_nr; i++) {
673 struct xfrm_user_tmpl *up = &vec[i];
674 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
676 memcpy(&up->id, &kp->id, sizeof(up->id));
677 up->family = xp->family;
678 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
679 up->reqid = kp->reqid;
681 up->share = kp->share;
682 up->optional = kp->optional;
683 up->aalgos = kp->aalgos;
684 up->ealgos = kp->ealgos;
685 up->calgos = kp->calgos;
687 RTA_PUT(skb, XFRMA_TMPL,
688 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
697 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
699 struct xfrm_dump_info *sp = ptr;
700 struct xfrm_userpolicy_info *p;
701 struct sk_buff *in_skb = sp->in_skb;
702 struct sk_buff *skb = sp->out_skb;
703 struct nlmsghdr *nlh;
704 unsigned char *b = skb->tail;
706 if (sp->this_idx < sp->start_idx)
709 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
711 XFRM_MSG_NEWPOLICY, sizeof(*p));
713 nlh->nlmsg_flags = 0;
715 copy_to_user_policy(xp, p, dir);
716 if (copy_to_user_tmpl(xp, skb) < 0)
719 nlh->nlmsg_len = skb->tail - b;
725 skb_trim(skb, b - skb->data);
729 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
731 struct xfrm_dump_info info;
733 info.in_skb = cb->skb;
735 info.nlmsg_seq = cb->nlh->nlmsg_seq;
737 info.start_idx = cb->args[0];
738 (void) xfrm_policy_walk(dump_one_policy, &info);
739 cb->args[0] = info.this_idx;
744 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
745 struct xfrm_policy *xp,
748 struct xfrm_dump_info info;
751 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
753 return ERR_PTR(-ENOMEM);
755 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
756 info.in_skb = in_skb;
758 info.nlmsg_seq = seq;
759 info.this_idx = info.start_idx = 0;
761 if (dump_one_policy(xp, dir, 0, &info) < 0) {
769 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
771 struct xfrm_policy *xp;
772 struct xfrm_userpolicy_id *p;
777 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
779 err = verify_policy_dir(p->dir);
784 xp = xfrm_policy_byid(p->dir, p->index, delete);
786 xp = xfrm_policy_bysel(p->dir, &p->sel, delete);
791 struct sk_buff *resp_skb;
793 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
794 if (IS_ERR(resp_skb)) {
795 err = PTR_ERR(resp_skb);
797 err = netlink_unicast(xfrm_nl, resp_skb,
808 static const int xfrm_msg_min[(XFRM_MSG_MAX + 1 - XFRM_MSG_BASE)] = {
809 NLMSG_LENGTH(sizeof(struct xfrm_usersa_info)), /* NEW SA */
810 NLMSG_LENGTH(sizeof(struct xfrm_usersa_id)), /* DEL SA */
811 NLMSG_LENGTH(sizeof(struct xfrm_usersa_id)), /* GET SA */
812 NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info)),/* NEW POLICY */
813 NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id)), /* DEL POLICY */
814 NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id)), /* GET POLICY */
815 NLMSG_LENGTH(sizeof(struct xfrm_userspi_info)), /* ALLOC SPI */
816 NLMSG_LENGTH(sizeof(struct xfrm_user_acquire)), /* ACQUIRE */
817 NLMSG_LENGTH(sizeof(struct xfrm_user_expire)), /* EXPIRE */
818 NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info)),/* UPD POLICY */
819 NLMSG_LENGTH(sizeof(struct xfrm_usersa_info)), /* UPD SA */
822 static struct xfrm_link {
823 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
824 int (*dump)(struct sk_buff *, struct netlink_callback *);
825 } xfrm_dispatch[] = {
826 { .doit = xfrm_add_sa, },
827 { .doit = xfrm_del_sa, },
830 .dump = xfrm_dump_sa,
832 { .doit = xfrm_add_policy },
833 { .doit = xfrm_get_policy },
835 .doit = xfrm_get_policy,
836 .dump = xfrm_dump_policy,
838 { .doit = xfrm_alloc_userspi },
841 { .doit = xfrm_add_policy },
842 { .doit = xfrm_add_sa, },
845 static int xfrm_done(struct netlink_callback *cb)
850 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
852 struct rtattr *xfrma[XFRMA_MAX];
853 struct xfrm_link *link;
856 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
859 type = nlh->nlmsg_type;
861 /* A control message: ignore them */
862 if (type < XFRM_MSG_BASE)
865 /* Unknown message: reply with EINVAL */
866 if (type > XFRM_MSG_MAX)
869 type -= XFRM_MSG_BASE;
870 link = &xfrm_dispatch[type];
872 /* All operations require privileges, even GET */
873 if (security_netlink_recv(skb)) {
878 if ((type == 2 || type == 5) && (nlh->nlmsg_flags & NLM_F_DUMP)) {
881 if (link->dump == NULL)
884 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
889 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
896 memset(xfrma, 0, sizeof(xfrma));
898 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
901 if (nlh->nlmsg_len > min_len) {
902 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
903 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
905 while (RTA_OK(attr, attrlen)) {
906 unsigned short flavor = attr->rta_type;
908 if (flavor > XFRMA_MAX)
910 xfrma[flavor - 1] = attr;
912 attr = RTA_NEXT(attr, attrlen);
916 if (link->doit == NULL)
918 *errp = link->doit(skb, nlh, (void **) &xfrma);
927 static int xfrm_user_rcv_skb(struct sk_buff *skb)
930 struct nlmsghdr *nlh;
932 while (skb->len >= NLMSG_SPACE(0)) {
935 nlh = (struct nlmsghdr *) skb->data;
936 if (nlh->nlmsg_len < sizeof(*nlh) ||
937 skb->len < nlh->nlmsg_len)
939 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
942 if (xfrm_user_rcv_msg(skb, nlh, &err) < 0) {
945 netlink_ack(skb, nlh, err);
946 } else if (nlh->nlmsg_flags & NLM_F_ACK)
947 netlink_ack(skb, nlh, 0);
954 static void xfrm_netlink_rcv(struct sock *sk, int len)
961 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
962 if (xfrm_user_rcv_skb(skb)) {
964 skb_queue_head(&sk->sk_receive_queue,
975 } while (xfrm_nl && xfrm_nl->sk_receive_queue.qlen);
978 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, int hard)
980 struct xfrm_user_expire *ue;
981 struct nlmsghdr *nlh;
982 unsigned char *b = skb->tail;
984 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_EXPIRE,
986 ue = NLMSG_DATA(nlh);
987 nlh->nlmsg_flags = 0;
989 copy_to_user_state(x, &ue->state);
990 ue->hard = (hard != 0) ? 1 : 0;
992 nlh->nlmsg_len = skb->tail - b;
996 skb_trim(skb, b - skb->data);
1000 static int xfrm_send_state_notify(struct xfrm_state *x, int hard)
1002 struct sk_buff *skb;
1004 skb = alloc_skb(sizeof(struct xfrm_user_expire) + 16, GFP_ATOMIC);
1008 if (build_expire(skb, x, hard) < 0)
1011 NETLINK_CB(skb).dst_groups = XFRMGRP_EXPIRE;
1013 return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_EXPIRE, GFP_ATOMIC);
1016 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1017 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1020 struct xfrm_user_acquire *ua;
1021 struct nlmsghdr *nlh;
1022 unsigned char *b = skb->tail;
1023 __u32 seq = xfrm_get_acqseq();
1025 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1027 ua = NLMSG_DATA(nlh);
1028 nlh->nlmsg_flags = 0;
1030 memcpy(&ua->id, &x->id, sizeof(ua->id));
1031 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1032 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1033 copy_to_user_policy(xp, &ua->policy, dir);
1034 ua->aalgos = xt->aalgos;
1035 ua->ealgos = xt->ealgos;
1036 ua->calgos = xt->calgos;
1037 ua->seq = x->km.seq = seq;
1039 if (copy_to_user_tmpl(xp, skb) < 0)
1042 nlh->nlmsg_len = skb->tail - b;
1046 skb_trim(skb, b - skb->data);
1050 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1051 struct xfrm_policy *xp, int dir)
1053 struct sk_buff *skb;
1056 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1057 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
1058 skb = alloc_skb(len, GFP_ATOMIC);
1062 if (build_acquire(skb, x, xt, xp, dir) < 0)
1065 NETLINK_CB(skb).dst_groups = XFRMGRP_ACQUIRE;
1067 return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_ACQUIRE, GFP_ATOMIC);
1070 /* User gives us xfrm_user_policy_info followed by an array of 0
1071 * or more templates.
1073 struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
1074 u8 *data, int len, int *dir)
1076 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1077 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1078 struct xfrm_policy *xp;
1083 if (opt != IP_XFRM_POLICY) {
1088 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1090 if (opt != IPV6_XFRM_POLICY) {
1103 if (len < sizeof(*p) ||
1104 verify_newpolicy_info(p))
1107 nr = ((len - sizeof(*p)) / sizeof(*ut));
1108 if (nr > XFRM_MAX_DEPTH)
1111 xp = xfrm_policy_alloc(GFP_KERNEL);
1117 copy_from_user_policy(xp, p);
1118 copy_templates(xp, ut, nr);
1125 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
1128 struct xfrm_user_polexpire *upe;
1129 struct nlmsghdr *nlh;
1130 unsigned char *b = skb->tail;
1132 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
1133 upe = NLMSG_DATA(nlh);
1134 nlh->nlmsg_flags = 0;
1136 copy_to_user_policy(xp, &upe->pol, dir);
1137 if (copy_to_user_tmpl(xp, skb) < 0)
1141 nlh->nlmsg_len = skb->tail - b;
1145 skb_trim(skb, b - skb->data);
1149 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, int hard)
1151 struct sk_buff *skb;
1154 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1155 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
1156 skb = alloc_skb(len, GFP_ATOMIC);
1160 if (build_polexpire(skb, xp, dir, hard) < 0)
1163 NETLINK_CB(skb).dst_groups = XFRMGRP_EXPIRE;
1165 return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_EXPIRE, GFP_ATOMIC);
1168 static struct xfrm_mgr netlink_mgr = {
1170 .notify = xfrm_send_state_notify,
1171 .acquire = xfrm_send_acquire,
1172 .compile_policy = xfrm_compile_policy,
1173 .notify_policy = xfrm_send_policy_notify,
1176 static int __init xfrm_user_init(void)
1178 printk(KERN_INFO "Initializing IPsec netlink socket\n");
1180 xfrm_nl = netlink_kernel_create(NETLINK_XFRM, xfrm_netlink_rcv);
1181 if (xfrm_nl == NULL)
1182 panic("xfrm_user_init: cannot initialize xfrm_nl\n");
1185 xfrm_register_km(&netlink_mgr);
1190 static void __exit xfrm_user_exit(void)
1192 xfrm_unregister_km(&netlink_mgr);
1193 sock_release(xfrm_nl->sk_socket);
1196 module_init(xfrm_user_init);
1197 module_exit(xfrm_user_exit);
1198 MODULE_LICENSE("GPL");