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, u8 *props,
159 struct xfrm_algo_desc *(*get_byname)(char *),
160 struct rtattr *u_arg)
162 struct rtattr *rta = u_arg;
163 struct xfrm_algo *p, *ualg;
164 struct xfrm_algo_desc *algo;
169 ualg = RTA_DATA(rta);
171 algo = get_byname(ualg->alg_name);
174 *props = algo->desc.sadb_alg_id;
176 p = kmalloc(sizeof(*ualg) + ualg->alg_key_len, GFP_KERNEL);
180 memcpy(p, ualg, sizeof(*ualg) + ualg->alg_key_len);
185 static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
187 struct rtattr *rta = u_arg;
188 struct xfrm_encap_tmpl *p, *uencap;
193 uencap = RTA_DATA(rta);
194 p = kmalloc(sizeof(*p), GFP_KERNEL);
198 memcpy(p, uencap, sizeof(*p));
203 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
205 memcpy(&x->id, &p->id, sizeof(x->id));
206 memcpy(&x->sel, &p->sel, sizeof(x->sel));
207 memcpy(&x->lft, &p->lft, sizeof(x->lft));
208 x->props.mode = p->mode;
209 x->props.replay_window = p->replay_window;
210 x->props.reqid = p->reqid;
211 x->props.family = p->family;
212 x->props.saddr = p->saddr;
213 x->props.flags = p->flags;
216 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
217 struct rtattr **xfrma,
220 struct xfrm_state *x = xfrm_state_alloc();
226 copy_from_user_state(x, p);
228 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
229 xfrm_aalg_get_byname,
230 xfrma[XFRMA_ALG_AUTH-1])))
232 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
233 xfrm_ealg_get_byname,
234 xfrma[XFRMA_ALG_CRYPT-1])))
236 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
237 xfrm_calg_get_byname,
238 xfrma[XFRMA_ALG_COMP-1])))
240 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
244 x->type = xfrm_get_type(x->id.proto, x->props.family);
248 err = x->type->init_state(x, NULL);
252 x->curlft.add_time = (unsigned long) xtime.tv_sec;
253 x->km.state = XFRM_STATE_VALID;
259 x->km.state = XFRM_STATE_DEAD;
266 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
268 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
269 struct xfrm_state *x;
272 err = verify_newsa_info(p, (struct rtattr **) xfrma);
278 x = xfrm_state_construct(p, (struct rtattr **) xfrma, &err);
282 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
283 err = xfrm_state_add(x);
285 err = xfrm_state_update(x);
288 x->km.state = XFRM_STATE_DEAD;
295 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
297 struct xfrm_state *x;
298 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
300 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
304 if (xfrm_state_kern(x)) {
309 xfrm_state_delete(x);
315 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
317 memcpy(&p->id, &x->id, sizeof(p->id));
318 memcpy(&p->sel, &x->sel, sizeof(p->sel));
319 memcpy(&p->lft, &x->lft, sizeof(p->lft));
320 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
321 memcpy(&p->stats, &x->stats, sizeof(p->stats));
322 p->saddr = x->props.saddr;
323 p->mode = x->props.mode;
324 p->replay_window = x->props.replay_window;
325 p->reqid = x->props.reqid;
326 p->family = x->props.family;
327 p->flags = x->props.flags;
331 struct xfrm_dump_info {
332 struct sk_buff *in_skb;
333 struct sk_buff *out_skb;
339 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
341 struct xfrm_dump_info *sp = ptr;
342 struct sk_buff *in_skb = sp->in_skb;
343 struct sk_buff *skb = sp->out_skb;
344 struct xfrm_usersa_info *p;
345 struct nlmsghdr *nlh;
346 unsigned char *b = skb->tail;
348 if (sp->this_idx < sp->start_idx)
351 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
353 XFRM_MSG_NEWSA, sizeof(*p));
354 nlh->nlmsg_flags = 0;
357 copy_to_user_state(x, p);
360 RTA_PUT(skb, XFRMA_ALG_AUTH,
361 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
363 RTA_PUT(skb, XFRMA_ALG_CRYPT,
364 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
366 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
369 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
371 nlh->nlmsg_len = skb->tail - b;
378 skb_trim(skb, b - skb->data);
382 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
384 struct xfrm_dump_info info;
386 info.in_skb = cb->skb;
388 info.nlmsg_seq = cb->nlh->nlmsg_seq;
390 info.start_idx = cb->args[0];
391 (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
392 cb->args[0] = info.this_idx;
397 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
398 struct xfrm_state *x, u32 seq)
400 struct xfrm_dump_info info;
403 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
405 return ERR_PTR(-ENOMEM);
407 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
408 info.in_skb = in_skb;
410 info.nlmsg_seq = seq;
411 info.this_idx = info.start_idx = 0;
413 if (dump_one_state(x, 0, &info)) {
421 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
423 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
424 struct xfrm_state *x;
425 struct sk_buff *resp_skb;
428 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
433 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
434 if (IS_ERR(resp_skb)) {
435 err = PTR_ERR(resp_skb);
437 err = netlink_unicast(xfrm_nl, resp_skb,
438 NETLINK_CB(skb).pid, MSG_DONTWAIT);
445 static int verify_userspi_info(struct xfrm_userspi_info *p)
447 switch (p->info.id.proto) {
453 /* IPCOMP spi is 16-bits. */
454 if (p->max >= 0x10000)
468 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
470 struct xfrm_state *x;
471 struct xfrm_userspi_info *p;
472 struct sk_buff *resp_skb;
473 xfrm_address_t *daddr;
478 err = verify_userspi_info(p);
482 family = p->info.family;
483 daddr = &p->info.id.daddr;
487 x = xfrm_find_acq_byseq(p->info.seq);
488 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
495 x = xfrm_find_acq(p->info.mode, p->info.reqid,
496 p->info.id.proto, daddr,
503 resp_skb = ERR_PTR(-ENOENT);
505 spin_lock_bh(&x->lock);
506 if (x->km.state != XFRM_STATE_DEAD) {
507 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
509 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
511 spin_unlock_bh(&x->lock);
513 if (IS_ERR(resp_skb)) {
514 err = PTR_ERR(resp_skb);
518 err = netlink_unicast(xfrm_nl, resp_skb,
519 NETLINK_CB(skb).pid, MSG_DONTWAIT);
527 static int verify_policy_dir(__u8 dir)
531 case XFRM_POLICY_OUT:
532 case XFRM_POLICY_FWD:
542 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
546 case XFRM_SHARE_SESSION:
547 case XFRM_SHARE_USER:
548 case XFRM_SHARE_UNIQUE:
556 case XFRM_POLICY_ALLOW:
557 case XFRM_POLICY_BLOCK:
564 switch (p->sel.family) {
569 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
572 return -EAFNOSUPPORT;
579 return verify_policy_dir(p->dir);
582 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
588 for (i = 0; i < nr; i++, ut++) {
589 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
591 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
592 memcpy(&t->saddr, &ut->saddr,
593 sizeof(xfrm_address_t));
594 t->reqid = ut->reqid;
596 t->share = ut->share;
597 t->optional = ut->optional;
598 t->aalgos = ut->aalgos;
599 t->ealgos = ut->ealgos;
600 t->calgos = ut->calgos;
604 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
606 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
607 struct xfrm_user_tmpl *utmpl;
613 nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
615 if (nr > XFRM_MAX_DEPTH)
618 copy_templates(pol, RTA_DATA(rt), nr);
623 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
625 xp->priority = p->priority;
626 xp->index = p->index;
627 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
628 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
629 xp->action = p->action;
630 xp->flags = p->flags;
631 xp->family = p->sel.family;
632 /* XXX xp->share = p->share; */
635 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
637 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
638 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
639 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
640 p->priority = xp->priority;
641 p->index = xp->index;
642 p->sel.family = xp->family;
644 p->action = xp->action;
645 p->flags = xp->flags;
646 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
649 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
651 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
659 copy_from_user_policy(xp, p);
660 err = copy_from_user_tmpl(xp, xfrma);
670 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
672 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
673 struct xfrm_policy *xp;
677 err = verify_newpolicy_info(p);
681 xp = xfrm_policy_construct(p, (struct rtattr **) xfrma, &err);
685 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
686 err = xfrm_policy_insert(p->dir, xp, excl);
697 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
699 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
702 if (xp->xfrm_nr == 0)
705 for (i = 0; i < xp->xfrm_nr; i++) {
706 struct xfrm_user_tmpl *up = &vec[i];
707 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
709 memcpy(&up->id, &kp->id, sizeof(up->id));
710 up->family = xp->family;
711 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
712 up->reqid = kp->reqid;
714 up->share = kp->share;
715 up->optional = kp->optional;
716 up->aalgos = kp->aalgos;
717 up->ealgos = kp->ealgos;
718 up->calgos = kp->calgos;
720 RTA_PUT(skb, XFRMA_TMPL,
721 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
730 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
732 struct xfrm_dump_info *sp = ptr;
733 struct xfrm_userpolicy_info *p;
734 struct sk_buff *in_skb = sp->in_skb;
735 struct sk_buff *skb = sp->out_skb;
736 struct nlmsghdr *nlh;
737 unsigned char *b = skb->tail;
739 if (sp->this_idx < sp->start_idx)
742 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
744 XFRM_MSG_NEWPOLICY, sizeof(*p));
746 nlh->nlmsg_flags = 0;
748 copy_to_user_policy(xp, p, dir);
749 if (copy_to_user_tmpl(xp, skb) < 0)
752 nlh->nlmsg_len = skb->tail - b;
758 skb_trim(skb, b - skb->data);
762 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
764 struct xfrm_dump_info info;
766 info.in_skb = cb->skb;
768 info.nlmsg_seq = cb->nlh->nlmsg_seq;
770 info.start_idx = cb->args[0];
771 (void) xfrm_policy_walk(dump_one_policy, &info);
772 cb->args[0] = info.this_idx;
777 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
778 struct xfrm_policy *xp,
781 struct xfrm_dump_info info;
784 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
786 return ERR_PTR(-ENOMEM);
788 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
789 info.in_skb = in_skb;
791 info.nlmsg_seq = seq;
792 info.this_idx = info.start_idx = 0;
794 if (dump_one_policy(xp, dir, 0, &info) < 0) {
802 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
804 struct xfrm_policy *xp;
805 struct xfrm_userpolicy_id *p;
810 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
812 err = verify_policy_dir(p->dir);
817 xp = xfrm_policy_byid(p->dir, p->index, delete);
819 xp = xfrm_policy_bysel(p->dir, &p->sel, delete);
824 struct sk_buff *resp_skb;
826 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
827 if (IS_ERR(resp_skb)) {
828 err = PTR_ERR(resp_skb);
830 err = netlink_unicast(xfrm_nl, resp_skb,
841 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
843 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
845 xfrm_state_flush(p->proto);
849 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
855 static const int xfrm_msg_min[(XFRM_MSG_MAX + 1 - XFRM_MSG_BASE)] = {
856 NLMSG_LENGTH(sizeof(struct xfrm_usersa_info)), /* NEW SA */
857 NLMSG_LENGTH(sizeof(struct xfrm_usersa_id)), /* DEL SA */
858 NLMSG_LENGTH(sizeof(struct xfrm_usersa_id)), /* GET SA */
859 NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info)),/* NEW POLICY */
860 NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id)), /* DEL POLICY */
861 NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id)), /* GET POLICY */
862 NLMSG_LENGTH(sizeof(struct xfrm_userspi_info)), /* ALLOC SPI */
863 NLMSG_LENGTH(sizeof(struct xfrm_user_acquire)), /* ACQUIRE */
864 NLMSG_LENGTH(sizeof(struct xfrm_user_expire)), /* EXPIRE */
865 NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info)),/* UPD POLICY */
866 NLMSG_LENGTH(sizeof(struct xfrm_usersa_info)), /* UPD SA */
867 NLMSG_LENGTH(sizeof(struct xfrm_user_polexpire)), /* POLEXPIRE */
868 NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush)), /* FLUSH SA */
869 NLMSG_LENGTH(0), /* FLUSH POLICY */
872 static struct xfrm_link {
873 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
874 int (*dump)(struct sk_buff *, struct netlink_callback *);
875 } xfrm_dispatch[] = {
876 { .doit = xfrm_add_sa, },
877 { .doit = xfrm_del_sa, },
880 .dump = xfrm_dump_sa,
882 { .doit = xfrm_add_policy },
883 { .doit = xfrm_get_policy },
885 .doit = xfrm_get_policy,
886 .dump = xfrm_dump_policy,
888 { .doit = xfrm_alloc_userspi },
891 { .doit = xfrm_add_policy },
892 { .doit = xfrm_add_sa, },
894 { .doit = xfrm_flush_sa },
895 { .doit = xfrm_flush_policy },
898 static int xfrm_done(struct netlink_callback *cb)
903 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
905 struct rtattr *xfrma[XFRMA_MAX];
906 struct xfrm_link *link;
909 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
912 type = nlh->nlmsg_type;
914 /* A control message: ignore them */
915 if (type < XFRM_MSG_BASE)
918 /* Unknown message: reply with EINVAL */
919 if (type > XFRM_MSG_MAX)
922 type -= XFRM_MSG_BASE;
923 link = &xfrm_dispatch[type];
925 /* All operations require privileges, even GET */
926 if (security_netlink_recv(skb)) {
931 if ((type == 2 || type == 5) && (nlh->nlmsg_flags & NLM_F_DUMP)) {
934 if (link->dump == NULL)
937 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
942 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
949 memset(xfrma, 0, sizeof(xfrma));
951 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
954 if (nlh->nlmsg_len > min_len) {
955 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
956 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
958 while (RTA_OK(attr, attrlen)) {
959 unsigned short flavor = attr->rta_type;
961 if (flavor > XFRMA_MAX)
963 xfrma[flavor - 1] = attr;
965 attr = RTA_NEXT(attr, attrlen);
969 if (link->doit == NULL)
971 *errp = link->doit(skb, nlh, (void **) &xfrma);
980 static int xfrm_user_rcv_skb(struct sk_buff *skb)
983 struct nlmsghdr *nlh;
985 while (skb->len >= NLMSG_SPACE(0)) {
988 nlh = (struct nlmsghdr *) skb->data;
989 if (nlh->nlmsg_len < sizeof(*nlh) ||
990 skb->len < nlh->nlmsg_len)
992 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
995 if (xfrm_user_rcv_msg(skb, nlh, &err) < 0) {
998 netlink_ack(skb, nlh, err);
999 } else if (nlh->nlmsg_flags & NLM_F_ACK)
1000 netlink_ack(skb, nlh, 0);
1001 skb_pull(skb, rlen);
1007 static void xfrm_netlink_rcv(struct sock *sk, int len)
1010 struct sk_buff *skb;
1012 down(&xfrm_cfg_sem);
1014 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
1015 if (xfrm_user_rcv_skb(skb)) {
1017 skb_queue_head(&sk->sk_receive_queue,
1028 } while (xfrm_nl && xfrm_nl->sk_receive_queue.qlen);
1031 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, int hard)
1033 struct xfrm_user_expire *ue;
1034 struct nlmsghdr *nlh;
1035 unsigned char *b = skb->tail;
1037 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_EXPIRE,
1039 ue = NLMSG_DATA(nlh);
1040 nlh->nlmsg_flags = 0;
1042 copy_to_user_state(x, &ue->state);
1043 ue->hard = (hard != 0) ? 1 : 0;
1045 nlh->nlmsg_len = skb->tail - b;
1049 skb_trim(skb, b - skb->data);
1053 static int xfrm_send_state_notify(struct xfrm_state *x, int hard)
1055 struct sk_buff *skb;
1057 skb = alloc_skb(sizeof(struct xfrm_user_expire) + 16, GFP_ATOMIC);
1061 if (build_expire(skb, x, hard) < 0)
1064 NETLINK_CB(skb).dst_groups = XFRMGRP_EXPIRE;
1066 return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_EXPIRE, GFP_ATOMIC);
1069 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1070 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1073 struct xfrm_user_acquire *ua;
1074 struct nlmsghdr *nlh;
1075 unsigned char *b = skb->tail;
1076 __u32 seq = xfrm_get_acqseq();
1078 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1080 ua = NLMSG_DATA(nlh);
1081 nlh->nlmsg_flags = 0;
1083 memcpy(&ua->id, &x->id, sizeof(ua->id));
1084 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1085 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1086 copy_to_user_policy(xp, &ua->policy, dir);
1087 ua->aalgos = xt->aalgos;
1088 ua->ealgos = xt->ealgos;
1089 ua->calgos = xt->calgos;
1090 ua->seq = x->km.seq = seq;
1092 if (copy_to_user_tmpl(xp, skb) < 0)
1095 nlh->nlmsg_len = skb->tail - b;
1099 skb_trim(skb, b - skb->data);
1103 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1104 struct xfrm_policy *xp, int dir)
1106 struct sk_buff *skb;
1109 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1110 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
1111 skb = alloc_skb(len, GFP_ATOMIC);
1115 if (build_acquire(skb, x, xt, xp, dir) < 0)
1118 NETLINK_CB(skb).dst_groups = XFRMGRP_ACQUIRE;
1120 return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_ACQUIRE, GFP_ATOMIC);
1123 /* User gives us xfrm_user_policy_info followed by an array of 0
1124 * or more templates.
1126 struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
1127 u8 *data, int len, int *dir)
1129 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1130 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1131 struct xfrm_policy *xp;
1136 if (opt != IP_XFRM_POLICY) {
1141 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1143 if (opt != IPV6_XFRM_POLICY) {
1156 if (len < sizeof(*p) ||
1157 verify_newpolicy_info(p))
1160 nr = ((len - sizeof(*p)) / sizeof(*ut));
1161 if (nr > XFRM_MAX_DEPTH)
1164 xp = xfrm_policy_alloc(GFP_KERNEL);
1170 copy_from_user_policy(xp, p);
1171 copy_templates(xp, ut, nr);
1178 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
1181 struct xfrm_user_polexpire *upe;
1182 struct nlmsghdr *nlh;
1183 unsigned char *b = skb->tail;
1185 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
1186 upe = NLMSG_DATA(nlh);
1187 nlh->nlmsg_flags = 0;
1189 copy_to_user_policy(xp, &upe->pol, dir);
1190 if (copy_to_user_tmpl(xp, skb) < 0)
1194 nlh->nlmsg_len = skb->tail - b;
1198 skb_trim(skb, b - skb->data);
1202 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, int hard)
1204 struct sk_buff *skb;
1207 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1208 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
1209 skb = alloc_skb(len, GFP_ATOMIC);
1213 if (build_polexpire(skb, xp, dir, hard) < 0)
1216 NETLINK_CB(skb).dst_groups = XFRMGRP_EXPIRE;
1218 return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_EXPIRE, GFP_ATOMIC);
1221 static struct xfrm_mgr netlink_mgr = {
1223 .notify = xfrm_send_state_notify,
1224 .acquire = xfrm_send_acquire,
1225 .compile_policy = xfrm_compile_policy,
1226 .notify_policy = xfrm_send_policy_notify,
1229 static int __init xfrm_user_init(void)
1231 printk(KERN_INFO "Initializing IPsec netlink socket\n");
1233 xfrm_nl = netlink_kernel_create(NETLINK_XFRM, xfrm_netlink_rcv);
1234 if (xfrm_nl == NULL)
1235 panic("xfrm_user_init: cannot initialize xfrm_nl\n");
1238 xfrm_register_km(&netlink_mgr);
1243 static void __exit xfrm_user_exit(void)
1245 xfrm_unregister_km(&netlink_mgr);
1246 sock_release(xfrm_nl->sk_socket);
1249 module_init(xfrm_user_init);
1250 module_exit(xfrm_user_exit);
1251 MODULE_LICENSE("GPL");