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>
32 static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
34 struct rtattr *rt = xfrma[type - 1];
35 struct xfrm_algo *algp;
41 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
47 len -= (algp->alg_key_len + 7U) / 8;
53 if (!algp->alg_key_len &&
54 strcmp(algp->alg_name, "digest_null") != 0)
59 if (!algp->alg_key_len &&
60 strcmp(algp->alg_name, "cipher_null") != 0)
65 /* Zero length keys are legal. */
72 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
76 static int verify_encap_tmpl(struct rtattr **xfrma)
78 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
79 struct xfrm_encap_tmpl *encap;
84 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
91 static inline int verify_sec_ctx_len(struct rtattr **xfrma)
93 struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
94 struct xfrm_user_sec_ctx *uctx;
100 if (rt->rta_len < sizeof(*uctx))
105 len += sizeof(struct xfrm_user_sec_ctx);
106 len += uctx->ctx_len;
108 if (uctx->len != len)
115 static int verify_newsa_info(struct xfrm_usersa_info *p,
116 struct rtattr **xfrma)
126 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
138 switch (p->id.proto) {
140 if (!xfrma[XFRMA_ALG_AUTH-1] ||
141 xfrma[XFRMA_ALG_CRYPT-1] ||
142 xfrma[XFRMA_ALG_COMP-1])
147 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
148 !xfrma[XFRMA_ALG_CRYPT-1]) ||
149 xfrma[XFRMA_ALG_COMP-1])
154 if (!xfrma[XFRMA_ALG_COMP-1] ||
155 xfrma[XFRMA_ALG_AUTH-1] ||
156 xfrma[XFRMA_ALG_CRYPT-1])
164 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
166 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
168 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
170 if ((err = verify_encap_tmpl(xfrma)))
172 if ((err = verify_sec_ctx_len(xfrma)))
191 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
192 struct xfrm_algo_desc *(*get_byname)(char *, int),
193 struct rtattr *u_arg)
195 struct rtattr *rta = u_arg;
196 struct xfrm_algo *p, *ualg;
197 struct xfrm_algo_desc *algo;
203 ualg = RTA_DATA(rta);
205 algo = get_byname(ualg->alg_name, 1);
208 *props = algo->desc.sadb_alg_id;
210 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
211 p = kmalloc(len, GFP_KERNEL);
215 memcpy(p, ualg, len);
216 strcpy(p->alg_name, algo->name);
221 static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
223 struct rtattr *rta = u_arg;
224 struct xfrm_encap_tmpl *p, *uencap;
229 uencap = RTA_DATA(rta);
230 p = kmalloc(sizeof(*p), GFP_KERNEL);
234 memcpy(p, uencap, sizeof(*p));
240 static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
242 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
246 len += sizeof(struct xfrm_user_sec_ctx);
247 len += xfrm_ctx->ctx_len;
252 static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
254 struct xfrm_user_sec_ctx *uctx;
259 uctx = RTA_DATA(u_arg);
260 return security_xfrm_state_alloc(x, uctx);
263 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
265 memcpy(&x->id, &p->id, sizeof(x->id));
266 memcpy(&x->sel, &p->sel, sizeof(x->sel));
267 memcpy(&x->lft, &p->lft, sizeof(x->lft));
268 x->props.mode = p->mode;
269 x->props.replay_window = p->replay_window;
270 x->props.reqid = p->reqid;
271 x->props.family = p->family;
272 x->props.saddr = p->saddr;
273 x->props.flags = p->flags;
277 * someday when pfkey also has support, we could have the code
278 * somehow made shareable and move it to xfrm_state.c - JHS
281 static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
284 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
285 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
286 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
287 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
290 struct xfrm_replay_state *replay;
291 if (RTA_PAYLOAD(rp) < sizeof(*replay))
293 replay = RTA_DATA(rp);
294 memcpy(&x->replay, replay, sizeof(*replay));
295 memcpy(&x->preplay, replay, sizeof(*replay));
299 struct xfrm_lifetime_cur *ltime;
300 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
302 ltime = RTA_DATA(lt);
303 x->curlft.bytes = ltime->bytes;
304 x->curlft.packets = ltime->packets;
305 x->curlft.add_time = ltime->add_time;
306 x->curlft.use_time = ltime->use_time;
310 if (RTA_PAYLOAD(et) < sizeof(u32))
312 x->replay_maxage = *(u32*)RTA_DATA(et);
316 if (RTA_PAYLOAD(rt) < sizeof(u32))
318 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
326 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
327 struct rtattr **xfrma,
330 struct xfrm_state *x = xfrm_state_alloc();
336 copy_from_user_state(x, p);
338 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
339 xfrm_aalg_get_byname,
340 xfrma[XFRMA_ALG_AUTH-1])))
342 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
343 xfrm_ealg_get_byname,
344 xfrma[XFRMA_ALG_CRYPT-1])))
346 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
347 xfrm_calg_get_byname,
348 xfrma[XFRMA_ALG_COMP-1])))
350 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
353 err = xfrm_init_state(x);
357 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
361 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
362 /* sysctl_xfrm_aevent_etime is in 100ms units */
363 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
364 x->preplay.bitmap = 0;
365 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
366 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
368 /* override default values from above */
370 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
377 x->km.state = XFRM_STATE_DEAD;
384 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
386 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
387 struct xfrm_state *x;
391 err = verify_newsa_info(p, (struct rtattr **)xfrma);
395 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
400 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
401 err = xfrm_state_add(x);
403 err = xfrm_state_update(x);
406 x->km.state = XFRM_STATE_DEAD;
411 c.seq = nlh->nlmsg_seq;
412 c.pid = nlh->nlmsg_pid;
413 c.event = nlh->nlmsg_type;
415 km_state_notify(x, &c);
421 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
423 struct xfrm_state *x;
426 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
428 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
432 if ((err = security_xfrm_state_delete(x)) != 0)
435 if (xfrm_state_kern(x)) {
440 err = xfrm_state_delete(x);
444 c.seq = nlh->nlmsg_seq;
445 c.pid = nlh->nlmsg_pid;
446 c.event = nlh->nlmsg_type;
447 km_state_notify(x, &c);
454 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
456 memcpy(&p->id, &x->id, sizeof(p->id));
457 memcpy(&p->sel, &x->sel, sizeof(p->sel));
458 memcpy(&p->lft, &x->lft, sizeof(p->lft));
459 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
460 memcpy(&p->stats, &x->stats, sizeof(p->stats));
461 p->saddr = x->props.saddr;
462 p->mode = x->props.mode;
463 p->replay_window = x->props.replay_window;
464 p->reqid = x->props.reqid;
465 p->family = x->props.family;
466 p->flags = x->props.flags;
470 struct xfrm_dump_info {
471 struct sk_buff *in_skb;
472 struct sk_buff *out_skb;
479 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
481 struct xfrm_dump_info *sp = ptr;
482 struct sk_buff *in_skb = sp->in_skb;
483 struct sk_buff *skb = sp->out_skb;
484 struct xfrm_usersa_info *p;
485 struct nlmsghdr *nlh;
486 unsigned char *b = skb->tail;
488 if (sp->this_idx < sp->start_idx)
491 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
493 XFRM_MSG_NEWSA, sizeof(*p));
494 nlh->nlmsg_flags = sp->nlmsg_flags;
497 copy_to_user_state(x, p);
500 RTA_PUT(skb, XFRMA_ALG_AUTH,
501 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
503 RTA_PUT(skb, XFRMA_ALG_CRYPT,
504 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
506 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
509 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
512 int ctx_size = sizeof(struct xfrm_sec_ctx) +
513 x->security->ctx_len;
514 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
515 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
517 uctx->exttype = XFRMA_SEC_CTX;
518 uctx->len = ctx_size;
519 uctx->ctx_doi = x->security->ctx_doi;
520 uctx->ctx_alg = x->security->ctx_alg;
521 uctx->ctx_len = x->security->ctx_len;
522 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
524 nlh->nlmsg_len = skb->tail - b;
531 skb_trim(skb, b - skb->data);
535 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
537 struct xfrm_dump_info info;
539 info.in_skb = cb->skb;
541 info.nlmsg_seq = cb->nlh->nlmsg_seq;
542 info.nlmsg_flags = NLM_F_MULTI;
544 info.start_idx = cb->args[0];
545 (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
546 cb->args[0] = info.this_idx;
551 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
552 struct xfrm_state *x, u32 seq)
554 struct xfrm_dump_info info;
557 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
559 return ERR_PTR(-ENOMEM);
561 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
562 info.in_skb = in_skb;
564 info.nlmsg_seq = seq;
565 info.nlmsg_flags = 0;
566 info.this_idx = info.start_idx = 0;
568 if (dump_one_state(x, 0, &info)) {
576 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
578 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
579 struct xfrm_state *x;
580 struct sk_buff *resp_skb;
583 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
588 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
589 if (IS_ERR(resp_skb)) {
590 err = PTR_ERR(resp_skb);
592 err = netlink_unicast(xfrm_nl, resp_skb,
593 NETLINK_CB(skb).pid, MSG_DONTWAIT);
600 static int verify_userspi_info(struct xfrm_userspi_info *p)
602 switch (p->info.id.proto) {
608 /* IPCOMP spi is 16-bits. */
609 if (p->max >= 0x10000)
623 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
625 struct xfrm_state *x;
626 struct xfrm_userspi_info *p;
627 struct sk_buff *resp_skb;
628 xfrm_address_t *daddr;
633 err = verify_userspi_info(p);
637 family = p->info.family;
638 daddr = &p->info.id.daddr;
642 x = xfrm_find_acq_byseq(p->info.seq);
643 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
650 x = xfrm_find_acq(p->info.mode, p->info.reqid,
651 p->info.id.proto, daddr,
658 resp_skb = ERR_PTR(-ENOENT);
660 spin_lock_bh(&x->lock);
661 if (x->km.state != XFRM_STATE_DEAD) {
662 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
664 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
666 spin_unlock_bh(&x->lock);
668 if (IS_ERR(resp_skb)) {
669 err = PTR_ERR(resp_skb);
673 err = netlink_unicast(xfrm_nl, resp_skb,
674 NETLINK_CB(skb).pid, MSG_DONTWAIT);
682 static int verify_policy_dir(__u8 dir)
686 case XFRM_POLICY_OUT:
687 case XFRM_POLICY_FWD:
697 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
701 case XFRM_SHARE_SESSION:
702 case XFRM_SHARE_USER:
703 case XFRM_SHARE_UNIQUE:
711 case XFRM_POLICY_ALLOW:
712 case XFRM_POLICY_BLOCK:
719 switch (p->sel.family) {
724 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
727 return -EAFNOSUPPORT;
734 return verify_policy_dir(p->dir);
737 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
739 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
740 struct xfrm_user_sec_ctx *uctx;
746 return security_xfrm_policy_alloc(pol, uctx);
749 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
755 for (i = 0; i < nr; i++, ut++) {
756 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
758 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
759 memcpy(&t->saddr, &ut->saddr,
760 sizeof(xfrm_address_t));
761 t->reqid = ut->reqid;
763 t->share = ut->share;
764 t->optional = ut->optional;
765 t->aalgos = ut->aalgos;
766 t->ealgos = ut->ealgos;
767 t->calgos = ut->calgos;
771 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
773 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
774 struct xfrm_user_tmpl *utmpl;
780 nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
782 if (nr > XFRM_MAX_DEPTH)
785 copy_templates(pol, RTA_DATA(rt), nr);
790 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
792 xp->priority = p->priority;
793 xp->index = p->index;
794 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
795 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
796 xp->action = p->action;
797 xp->flags = p->flags;
798 xp->family = p->sel.family;
799 /* XXX xp->share = p->share; */
802 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
804 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
805 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
806 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
807 p->priority = xp->priority;
808 p->index = xp->index;
809 p->sel.family = xp->family;
811 p->action = xp->action;
812 p->flags = xp->flags;
813 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
816 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
818 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
826 copy_from_user_policy(xp, p);
828 if (!(err = copy_from_user_tmpl(xp, xfrma)))
829 err = copy_from_user_sec_ctx(xp, xfrma);
840 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
842 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
843 struct xfrm_policy *xp;
848 err = verify_newpolicy_info(p);
851 err = verify_sec_ctx_len((struct rtattr **)xfrma);
855 xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
859 /* shouldnt excl be based on nlh flags??
860 * Aha! this is anti-netlink really i.e more pfkey derived
861 * in netlink excl is a flag and you wouldnt need
862 * a type XFRM_MSG_UPDPOLICY - JHS */
863 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
864 err = xfrm_policy_insert(p->dir, xp, excl);
866 security_xfrm_policy_free(xp);
871 c.event = nlh->nlmsg_type;
872 c.seq = nlh->nlmsg_seq;
873 c.pid = nlh->nlmsg_pid;
874 km_policy_notify(xp, p->dir, &c);
881 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
883 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
886 if (xp->xfrm_nr == 0)
889 for (i = 0; i < xp->xfrm_nr; i++) {
890 struct xfrm_user_tmpl *up = &vec[i];
891 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
893 memcpy(&up->id, &kp->id, sizeof(up->id));
894 up->family = xp->family;
895 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
896 up->reqid = kp->reqid;
898 up->share = kp->share;
899 up->optional = kp->optional;
900 up->aalgos = kp->aalgos;
901 up->ealgos = kp->ealgos;
902 up->calgos = kp->calgos;
904 RTA_PUT(skb, XFRMA_TMPL,
905 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
914 static int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
917 int ctx_size = sizeof(struct xfrm_sec_ctx) +
918 xp->security->ctx_len;
919 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
920 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
922 uctx->exttype = XFRMA_SEC_CTX;
923 uctx->len = ctx_size;
924 uctx->ctx_doi = xp->security->ctx_doi;
925 uctx->ctx_alg = xp->security->ctx_alg;
926 uctx->ctx_len = xp->security->ctx_len;
927 memcpy(uctx + 1, xp->security->ctx_str, xp->security->ctx_len);
935 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
937 struct xfrm_dump_info *sp = ptr;
938 struct xfrm_userpolicy_info *p;
939 struct sk_buff *in_skb = sp->in_skb;
940 struct sk_buff *skb = sp->out_skb;
941 struct nlmsghdr *nlh;
942 unsigned char *b = skb->tail;
944 if (sp->this_idx < sp->start_idx)
947 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
949 XFRM_MSG_NEWPOLICY, sizeof(*p));
951 nlh->nlmsg_flags = sp->nlmsg_flags;
953 copy_to_user_policy(xp, p, dir);
954 if (copy_to_user_tmpl(xp, skb) < 0)
956 if (copy_to_user_sec_ctx(xp, skb))
959 nlh->nlmsg_len = skb->tail - b;
965 skb_trim(skb, b - skb->data);
969 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
971 struct xfrm_dump_info info;
973 info.in_skb = cb->skb;
975 info.nlmsg_seq = cb->nlh->nlmsg_seq;
976 info.nlmsg_flags = NLM_F_MULTI;
978 info.start_idx = cb->args[0];
979 (void) xfrm_policy_walk(dump_one_policy, &info);
980 cb->args[0] = info.this_idx;
985 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
986 struct xfrm_policy *xp,
989 struct xfrm_dump_info info;
992 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
994 return ERR_PTR(-ENOMEM);
996 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
997 info.in_skb = in_skb;
999 info.nlmsg_seq = seq;
1000 info.nlmsg_flags = 0;
1001 info.this_idx = info.start_idx = 0;
1003 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1011 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1013 struct xfrm_policy *xp;
1014 struct xfrm_userpolicy_id *p;
1019 p = NLMSG_DATA(nlh);
1020 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1022 err = verify_policy_dir(p->dir);
1027 xp = xfrm_policy_byid(p->dir, p->index, delete);
1029 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1030 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1031 struct xfrm_policy tmp;
1033 err = verify_sec_ctx_len(rtattrs);
1037 memset(&tmp, 0, sizeof(struct xfrm_policy));
1039 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1041 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1044 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, delete);
1045 security_xfrm_policy_free(&tmp);
1051 struct sk_buff *resp_skb;
1053 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1054 if (IS_ERR(resp_skb)) {
1055 err = PTR_ERR(resp_skb);
1057 err = netlink_unicast(xfrm_nl, resp_skb,
1058 NETLINK_CB(skb).pid,
1062 if ((err = security_xfrm_policy_delete(xp)) != 0)
1064 c.data.byid = p->index;
1065 c.event = nlh->nlmsg_type;
1066 c.seq = nlh->nlmsg_seq;
1067 c.pid = nlh->nlmsg_pid;
1068 km_policy_notify(xp, p->dir, &c);
1077 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1080 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1082 xfrm_state_flush(p->proto);
1083 c.data.proto = p->proto;
1084 c.event = nlh->nlmsg_type;
1085 c.seq = nlh->nlmsg_seq;
1086 c.pid = nlh->nlmsg_pid;
1087 km_state_notify(NULL, &c);
1093 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1095 struct xfrm_aevent_id *id;
1096 struct nlmsghdr *nlh;
1097 struct xfrm_lifetime_cur ltime;
1098 unsigned char *b = skb->tail;
1100 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1101 id = NLMSG_DATA(nlh);
1102 nlh->nlmsg_flags = 0;
1104 id->sa_id.daddr = x->id.daddr;
1105 id->sa_id.spi = x->id.spi;
1106 id->sa_id.family = x->props.family;
1107 id->sa_id.proto = x->id.proto;
1108 id->flags = c->data.aevent;
1110 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1112 ltime.bytes = x->curlft.bytes;
1113 ltime.packets = x->curlft.packets;
1114 ltime.add_time = x->curlft.add_time;
1115 ltime.use_time = x->curlft.use_time;
1117 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), <ime);
1119 if (id->flags&XFRM_AE_RTHR) {
1120 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1123 if (id->flags&XFRM_AE_ETHR) {
1124 u32 etimer = x->replay_maxage*10/HZ;
1125 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1128 nlh->nlmsg_len = skb->tail - b;
1133 skb_trim(skb, b - skb->data);
1137 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1139 struct xfrm_state *x;
1140 struct sk_buff *r_skb;
1143 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1144 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1145 struct xfrm_usersa_id *id = &p->sa_id;
1147 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1148 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1150 if (p->flags&XFRM_AE_RTHR)
1151 len+=RTA_SPACE(sizeof(u32));
1153 if (p->flags&XFRM_AE_ETHR)
1154 len+=RTA_SPACE(sizeof(u32));
1156 r_skb = alloc_skb(len, GFP_ATOMIC);
1160 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1167 * XXX: is this lock really needed - none of the other
1168 * gets lock (the concern is things getting updated
1169 * while we are still reading) - jhs
1171 spin_lock_bh(&x->lock);
1172 c.data.aevent = p->flags;
1173 c.seq = nlh->nlmsg_seq;
1174 c.pid = nlh->nlmsg_pid;
1176 if (build_aevent(r_skb, x, &c) < 0)
1178 err = netlink_unicast(xfrm_nl, r_skb,
1179 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1180 spin_unlock_bh(&x->lock);
1185 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1187 struct xfrm_state *x;
1190 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1191 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1192 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1197 /* pedantic mode - thou shalt sayeth replaceth */
1198 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1201 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1205 if (x->km.state != XFRM_STATE_VALID)
1208 spin_lock_bh(&x->lock);
1209 err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
1210 spin_unlock_bh(&x->lock);
1214 c.event = nlh->nlmsg_type;
1215 c.seq = nlh->nlmsg_seq;
1216 c.pid = nlh->nlmsg_pid;
1217 c.data.aevent = XFRM_AE_CU;
1218 km_state_notify(x, &c);
1225 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1229 xfrm_policy_flush();
1230 c.event = nlh->nlmsg_type;
1231 c.seq = nlh->nlmsg_seq;
1232 c.pid = nlh->nlmsg_pid;
1233 km_policy_notify(NULL, 0, &c);
1237 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1239 struct xfrm_policy *xp;
1240 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1241 struct xfrm_userpolicy_info *p = &up->pol;
1245 xp = xfrm_policy_byid(p->dir, p->index, 0);
1247 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1248 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1249 struct xfrm_policy tmp;
1251 err = verify_sec_ctx_len(rtattrs);
1255 memset(&tmp, 0, sizeof(struct xfrm_policy));
1257 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1259 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1262 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, 0);
1263 security_xfrm_policy_free(&tmp);
1268 read_lock(&xp->lock);
1270 read_unlock(&xp->lock);
1274 read_unlock(&xp->lock);
1277 xfrm_policy_delete(xp, p->dir);
1279 // reset the timers here?
1280 printk("Dont know what to do with soft policy expire\n");
1282 km_policy_expired(xp, p->dir, up->hard, current->pid);
1289 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1291 struct xfrm_state *x;
1293 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1294 struct xfrm_usersa_info *p = &ue->state;
1296 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1304 spin_lock_bh(&x->lock);
1305 if (x->km.state != XFRM_STATE_VALID)
1307 km_state_expired(x, ue->hard, current->pid);
1310 __xfrm_state_delete(x);
1312 spin_unlock_bh(&x->lock);
1317 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1319 struct xfrm_policy *xp;
1320 struct xfrm_user_tmpl *ut;
1322 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1324 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1325 struct xfrm_state *x = xfrm_state_alloc();
1331 err = verify_newpolicy_info(&ua->policy);
1333 printk("BAD policy passed\n");
1339 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err); if (!xp) {
1344 memcpy(&x->id, &ua->id, sizeof(ua->id));
1345 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1346 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1349 /* extract the templates and for each call km_key */
1350 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1351 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1352 memcpy(&x->id, &t->id, sizeof(x->id));
1353 x->props.mode = t->mode;
1354 x->props.reqid = t->reqid;
1355 x->props.family = ut->family;
1356 t->aalgos = ua->aalgos;
1357 t->ealgos = ua->ealgos;
1358 t->calgos = ua->calgos;
1359 err = km_query(x, t, xp);
1370 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1372 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1373 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1374 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1375 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1376 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1377 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1378 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1379 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1380 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1381 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1382 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1383 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1384 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1385 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1386 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
1387 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1388 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1393 static struct xfrm_link {
1394 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1395 int (*dump)(struct sk_buff *, struct netlink_callback *);
1396 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1397 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1398 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1399 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1400 .dump = xfrm_dump_sa },
1401 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1402 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1403 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1404 .dump = xfrm_dump_policy },
1405 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1406 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
1407 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1408 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1409 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1410 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1411 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1412 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
1413 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1414 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1417 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1419 struct rtattr *xfrma[XFRMA_MAX];
1420 struct xfrm_link *link;
1423 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1426 type = nlh->nlmsg_type;
1428 /* A control message: ignore them */
1429 if (type < XFRM_MSG_BASE)
1432 /* Unknown message: reply with EINVAL */
1433 if (type > XFRM_MSG_MAX)
1436 type -= XFRM_MSG_BASE;
1437 link = &xfrm_dispatch[type];
1439 /* All operations require privileges, even GET */
1440 if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
1445 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1446 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1447 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1448 if (link->dump == NULL)
1451 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
1452 link->dump, NULL)) != 0) {
1456 netlink_queue_skip(nlh, skb);
1460 memset(xfrma, 0, sizeof(xfrma));
1462 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1465 if (nlh->nlmsg_len > min_len) {
1466 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1467 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1469 while (RTA_OK(attr, attrlen)) {
1470 unsigned short flavor = attr->rta_type;
1472 if (flavor > XFRMA_MAX)
1474 xfrma[flavor - 1] = attr;
1476 attr = RTA_NEXT(attr, attrlen);
1480 if (link->doit == NULL)
1482 *errp = link->doit(skb, nlh, (void **) &xfrma);
1491 static void xfrm_netlink_rcv(struct sock *sk, int len)
1493 unsigned int qlen = 0;
1496 mutex_lock(&xfrm_cfg_mutex);
1497 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
1498 mutex_unlock(&xfrm_cfg_mutex);
1503 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1505 struct xfrm_user_expire *ue;
1506 struct nlmsghdr *nlh;
1507 unsigned char *b = skb->tail;
1509 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
1511 ue = NLMSG_DATA(nlh);
1512 nlh->nlmsg_flags = 0;
1514 copy_to_user_state(x, &ue->state);
1515 ue->hard = (c->data.hard != 0) ? 1 : 0;
1517 nlh->nlmsg_len = skb->tail - b;
1521 skb_trim(skb, b - skb->data);
1525 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1527 struct sk_buff *skb;
1528 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
1530 skb = alloc_skb(len, GFP_ATOMIC);
1534 if (build_expire(skb, x, c) < 0)
1537 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1538 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1541 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1543 struct sk_buff *skb;
1544 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1546 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1547 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1548 skb = alloc_skb(len, GFP_ATOMIC);
1552 if (build_aevent(skb, x, c) < 0)
1555 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1556 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1559 static int xfrm_notify_sa_flush(struct km_event *c)
1561 struct xfrm_usersa_flush *p;
1562 struct nlmsghdr *nlh;
1563 struct sk_buff *skb;
1565 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1567 skb = alloc_skb(len, GFP_ATOMIC);
1572 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1573 XFRM_MSG_FLUSHSA, sizeof(*p));
1574 nlh->nlmsg_flags = 0;
1576 p = NLMSG_DATA(nlh);
1577 p->proto = c->data.proto;
1579 nlh->nlmsg_len = skb->tail - b;
1581 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1582 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1589 static int inline xfrm_sa_len(struct xfrm_state *x)
1593 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1595 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1597 l += RTA_SPACE(sizeof(*x->calg));
1599 l += RTA_SPACE(sizeof(*x->encap));
1604 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1606 struct xfrm_usersa_info *p;
1607 struct xfrm_usersa_id *id;
1608 struct nlmsghdr *nlh;
1609 struct sk_buff *skb;
1611 int len = xfrm_sa_len(x);
1614 headlen = sizeof(*p);
1615 if (c->event == XFRM_MSG_DELSA) {
1616 len += RTA_SPACE(headlen);
1617 headlen = sizeof(*id);
1619 len += NLMSG_SPACE(headlen);
1621 skb = alloc_skb(len, GFP_ATOMIC);
1626 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
1627 nlh->nlmsg_flags = 0;
1629 p = NLMSG_DATA(nlh);
1630 if (c->event == XFRM_MSG_DELSA) {
1631 id = NLMSG_DATA(nlh);
1632 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1633 id->spi = x->id.spi;
1634 id->family = x->props.family;
1635 id->proto = x->id.proto;
1637 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1640 copy_to_user_state(x, p);
1643 RTA_PUT(skb, XFRMA_ALG_AUTH,
1644 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1646 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1647 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1649 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1652 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1654 nlh->nlmsg_len = skb->tail - b;
1656 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1657 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1665 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1669 case XFRM_MSG_EXPIRE:
1670 return xfrm_exp_state_notify(x, c);
1671 case XFRM_MSG_NEWAE:
1672 return xfrm_aevent_state_notify(x, c);
1673 case XFRM_MSG_DELSA:
1674 case XFRM_MSG_UPDSA:
1675 case XFRM_MSG_NEWSA:
1676 return xfrm_notify_sa(x, c);
1677 case XFRM_MSG_FLUSHSA:
1678 return xfrm_notify_sa_flush(c);
1680 printk("xfrm_user: Unknown SA event %d\n", c->event);
1688 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1689 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1692 struct xfrm_user_acquire *ua;
1693 struct nlmsghdr *nlh;
1694 unsigned char *b = skb->tail;
1695 __u32 seq = xfrm_get_acqseq();
1697 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1699 ua = NLMSG_DATA(nlh);
1700 nlh->nlmsg_flags = 0;
1702 memcpy(&ua->id, &x->id, sizeof(ua->id));
1703 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1704 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1705 copy_to_user_policy(xp, &ua->policy, dir);
1706 ua->aalgos = xt->aalgos;
1707 ua->ealgos = xt->ealgos;
1708 ua->calgos = xt->calgos;
1709 ua->seq = x->km.seq = seq;
1711 if (copy_to_user_tmpl(xp, skb) < 0)
1713 if (copy_to_user_sec_ctx(xp, skb))
1716 nlh->nlmsg_len = skb->tail - b;
1720 skb_trim(skb, b - skb->data);
1724 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1725 struct xfrm_policy *xp, int dir)
1727 struct sk_buff *skb;
1730 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1731 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
1732 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1733 skb = alloc_skb(len, GFP_ATOMIC);
1737 if (build_acquire(skb, x, xt, xp, dir) < 0)
1740 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
1741 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
1744 /* User gives us xfrm_user_policy_info followed by an array of 0
1745 * or more templates.
1747 static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
1748 u8 *data, int len, int *dir)
1750 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1751 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1752 struct xfrm_policy *xp;
1757 if (opt != IP_XFRM_POLICY) {
1762 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1764 if (opt != IPV6_XFRM_POLICY) {
1777 if (len < sizeof(*p) ||
1778 verify_newpolicy_info(p))
1781 nr = ((len - sizeof(*p)) / sizeof(*ut));
1782 if (nr > XFRM_MAX_DEPTH)
1785 if (p->dir > XFRM_POLICY_OUT)
1788 xp = xfrm_policy_alloc(GFP_KERNEL);
1794 copy_from_user_policy(xp, p);
1795 copy_templates(xp, ut, nr);
1802 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
1803 int dir, struct km_event *c)
1805 struct xfrm_user_polexpire *upe;
1806 struct nlmsghdr *nlh;
1807 int hard = c->data.hard;
1808 unsigned char *b = skb->tail;
1810 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
1811 upe = NLMSG_DATA(nlh);
1812 nlh->nlmsg_flags = 0;
1814 copy_to_user_policy(xp, &upe->pol, dir);
1815 if (copy_to_user_tmpl(xp, skb) < 0)
1817 if (copy_to_user_sec_ctx(xp, skb))
1821 nlh->nlmsg_len = skb->tail - b;
1825 skb_trim(skb, b - skb->data);
1829 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1831 struct sk_buff *skb;
1834 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1835 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
1836 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1837 skb = alloc_skb(len, GFP_ATOMIC);
1841 if (build_polexpire(skb, xp, dir, c) < 0)
1844 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1845 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1848 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
1850 struct xfrm_userpolicy_info *p;
1851 struct xfrm_userpolicy_id *id;
1852 struct nlmsghdr *nlh;
1853 struct sk_buff *skb;
1855 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1858 headlen = sizeof(*p);
1859 if (c->event == XFRM_MSG_DELPOLICY) {
1860 len += RTA_SPACE(headlen);
1861 headlen = sizeof(*id);
1863 len += NLMSG_SPACE(headlen);
1865 skb = alloc_skb(len, GFP_ATOMIC);
1870 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
1872 p = NLMSG_DATA(nlh);
1873 if (c->event == XFRM_MSG_DELPOLICY) {
1874 id = NLMSG_DATA(nlh);
1875 memset(id, 0, sizeof(*id));
1878 id->index = xp->index;
1880 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
1882 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
1885 nlh->nlmsg_flags = 0;
1887 copy_to_user_policy(xp, p, dir);
1888 if (copy_to_user_tmpl(xp, skb) < 0)
1891 nlh->nlmsg_len = skb->tail - b;
1893 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1894 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
1902 static int xfrm_notify_policy_flush(struct km_event *c)
1904 struct nlmsghdr *nlh;
1905 struct sk_buff *skb;
1907 int len = NLMSG_LENGTH(0);
1909 skb = alloc_skb(len, GFP_ATOMIC);
1915 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
1917 nlh->nlmsg_len = skb->tail - b;
1919 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1920 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
1927 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1931 case XFRM_MSG_NEWPOLICY:
1932 case XFRM_MSG_UPDPOLICY:
1933 case XFRM_MSG_DELPOLICY:
1934 return xfrm_notify_policy(xp, dir, c);
1935 case XFRM_MSG_FLUSHPOLICY:
1936 return xfrm_notify_policy_flush(c);
1937 case XFRM_MSG_POLEXPIRE:
1938 return xfrm_exp_policy_notify(xp, dir, c);
1940 printk("xfrm_user: Unknown Policy event %d\n", c->event);
1947 static struct xfrm_mgr netlink_mgr = {
1949 .notify = xfrm_send_state_notify,
1950 .acquire = xfrm_send_acquire,
1951 .compile_policy = xfrm_compile_policy,
1952 .notify_policy = xfrm_send_policy_notify,
1955 static int __init xfrm_user_init(void)
1959 printk(KERN_INFO "Initializing IPsec netlink socket\n");
1961 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
1962 xfrm_netlink_rcv, THIS_MODULE);
1965 rcu_assign_pointer(xfrm_nl, nlsk);
1967 xfrm_register_km(&netlink_mgr);
1972 static void __exit xfrm_user_exit(void)
1974 struct sock *nlsk = xfrm_nl;
1976 xfrm_unregister_km(&netlink_mgr);
1977 rcu_assign_pointer(xfrm_nl, NULL);
1979 sock_release(nlsk->sk_socket);
1982 module_init(xfrm_user_init);
1983 module_exit(xfrm_user_exit);
1984 MODULE_LICENSE("GPL");
1985 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);