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/rtnetlink.h>
22 #include <linux/pfkeyv2.h>
23 #include <linux/ipsec.h>
24 #include <linux/init.h>
25 #include <linux/security.h>
28 #include <net/netlink.h>
29 #include <asm/uaccess.h>
31 static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
33 struct rtattr *rt = xfrma[type - 1];
34 struct xfrm_algo *algp;
40 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
46 len -= (algp->alg_key_len + 7U) / 8;
52 if (!algp->alg_key_len &&
53 strcmp(algp->alg_name, "digest_null") != 0)
58 if (!algp->alg_key_len &&
59 strcmp(algp->alg_name, "cipher_null") != 0)
64 /* Zero length keys are legal. */
71 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
75 static int verify_encap_tmpl(struct rtattr **xfrma)
77 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
78 struct xfrm_encap_tmpl *encap;
83 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
90 static inline int verify_sec_ctx_len(struct rtattr **xfrma)
92 struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
93 struct xfrm_user_sec_ctx *uctx;
99 if (rt->rta_len < sizeof(*uctx))
104 if (uctx->ctx_len > PAGE_SIZE)
107 len += sizeof(struct xfrm_user_sec_ctx);
108 len += uctx->ctx_len;
110 if (uctx->len != len)
117 static int verify_newsa_info(struct xfrm_usersa_info *p,
118 struct rtattr **xfrma)
128 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
140 switch (p->id.proto) {
142 if (!xfrma[XFRMA_ALG_AUTH-1] ||
143 xfrma[XFRMA_ALG_CRYPT-1] ||
144 xfrma[XFRMA_ALG_COMP-1])
149 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
150 !xfrma[XFRMA_ALG_CRYPT-1]) ||
151 xfrma[XFRMA_ALG_COMP-1])
156 if (!xfrma[XFRMA_ALG_COMP-1] ||
157 xfrma[XFRMA_ALG_AUTH-1] ||
158 xfrma[XFRMA_ALG_CRYPT-1])
166 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
168 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
170 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
172 if ((err = verify_encap_tmpl(xfrma)))
174 if ((err = verify_sec_ctx_len(xfrma)))
193 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
194 struct xfrm_algo_desc *(*get_byname)(char *, int),
195 struct rtattr *u_arg)
197 struct rtattr *rta = u_arg;
198 struct xfrm_algo *p, *ualg;
199 struct xfrm_algo_desc *algo;
205 ualg = RTA_DATA(rta);
207 algo = get_byname(ualg->alg_name, 1);
210 *props = algo->desc.sadb_alg_id;
212 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
213 p = kmalloc(len, GFP_KERNEL);
217 memcpy(p, ualg, len);
222 static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
224 struct rtattr *rta = u_arg;
225 struct xfrm_encap_tmpl *p, *uencap;
230 uencap = RTA_DATA(rta);
231 p = kmalloc(sizeof(*p), GFP_KERNEL);
235 memcpy(p, uencap, sizeof(*p));
241 static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
243 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
247 len += sizeof(struct xfrm_user_sec_ctx);
248 len += xfrm_ctx->ctx_len;
253 static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
255 struct xfrm_user_sec_ctx *uctx;
260 uctx = RTA_DATA(u_arg);
261 return security_xfrm_state_alloc(x, uctx);
264 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
266 memcpy(&x->id, &p->id, sizeof(x->id));
267 memcpy(&x->sel, &p->sel, sizeof(x->sel));
268 memcpy(&x->lft, &p->lft, sizeof(x->lft));
269 x->props.mode = p->mode;
270 x->props.replay_window = p->replay_window;
271 x->props.reqid = p->reqid;
272 x->props.family = p->family;
273 x->props.saddr = p->saddr;
274 x->props.flags = p->flags;
278 * someday when pfkey also has support, we could have the code
279 * somehow made shareable and move it to xfrm_state.c - JHS
282 static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
285 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
286 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
287 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
288 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
291 struct xfrm_replay_state *replay;
292 if (RTA_PAYLOAD(rp) < sizeof(*replay))
294 replay = RTA_DATA(rp);
295 memcpy(&x->replay, replay, sizeof(*replay));
296 memcpy(&x->preplay, replay, sizeof(*replay));
300 struct xfrm_lifetime_cur *ltime;
301 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
303 ltime = RTA_DATA(lt);
304 x->curlft.bytes = ltime->bytes;
305 x->curlft.packets = ltime->packets;
306 x->curlft.add_time = ltime->add_time;
307 x->curlft.use_time = ltime->use_time;
311 if (RTA_PAYLOAD(et) < sizeof(u32))
313 x->replay_maxage = *(u32*)RTA_DATA(et);
317 if (RTA_PAYLOAD(rt) < sizeof(u32))
319 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
327 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
328 struct rtattr **xfrma,
331 struct xfrm_state *x = xfrm_state_alloc();
337 copy_from_user_state(x, p);
339 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
340 xfrm_aalg_get_byname,
341 xfrma[XFRMA_ALG_AUTH-1])))
343 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
344 xfrm_ealg_get_byname,
345 xfrma[XFRMA_ALG_CRYPT-1])))
347 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
348 xfrm_calg_get_byname,
349 xfrma[XFRMA_ALG_COMP-1])))
351 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
354 err = xfrm_init_state(x);
358 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
362 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
363 /* sysctl_xfrm_aevent_etime is in 100ms units */
364 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
365 x->preplay.bitmap = 0;
366 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
367 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
369 /* override default values from above */
371 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
378 x->km.state = XFRM_STATE_DEAD;
385 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
387 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
388 struct xfrm_state *x;
392 err = verify_newsa_info(p, (struct rtattr **)xfrma);
396 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
401 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
402 err = xfrm_state_add(x);
404 err = xfrm_state_update(x);
407 x->km.state = XFRM_STATE_DEAD;
412 c.seq = nlh->nlmsg_seq;
413 c.pid = nlh->nlmsg_pid;
414 c.event = nlh->nlmsg_type;
416 km_state_notify(x, &c);
422 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
424 struct xfrm_state *x;
427 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
429 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
433 if (xfrm_state_kern(x)) {
438 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);
453 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
455 memcpy(&p->id, &x->id, sizeof(p->id));
456 memcpy(&p->sel, &x->sel, sizeof(p->sel));
457 memcpy(&p->lft, &x->lft, sizeof(p->lft));
458 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
459 memcpy(&p->stats, &x->stats, sizeof(p->stats));
460 p->saddr = x->props.saddr;
461 p->mode = x->props.mode;
462 p->replay_window = x->props.replay_window;
463 p->reqid = x->props.reqid;
464 p->family = x->props.family;
465 p->flags = x->props.flags;
469 struct xfrm_dump_info {
470 struct sk_buff *in_skb;
471 struct sk_buff *out_skb;
478 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
480 struct xfrm_dump_info *sp = ptr;
481 struct sk_buff *in_skb = sp->in_skb;
482 struct sk_buff *skb = sp->out_skb;
483 struct xfrm_usersa_info *p;
484 struct nlmsghdr *nlh;
485 unsigned char *b = skb->tail;
487 if (sp->this_idx < sp->start_idx)
490 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
492 XFRM_MSG_NEWSA, sizeof(*p));
493 nlh->nlmsg_flags = sp->nlmsg_flags;
496 copy_to_user_state(x, p);
499 RTA_PUT(skb, XFRMA_ALG_AUTH,
500 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
502 RTA_PUT(skb, XFRMA_ALG_CRYPT,
503 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
505 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
508 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
511 int ctx_size = sizeof(struct xfrm_sec_ctx) +
512 x->security->ctx_len;
513 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
514 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
516 uctx->exttype = XFRMA_SEC_CTX;
517 uctx->len = ctx_size;
518 uctx->ctx_doi = x->security->ctx_doi;
519 uctx->ctx_alg = x->security->ctx_alg;
520 uctx->ctx_len = x->security->ctx_len;
521 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
523 nlh->nlmsg_len = skb->tail - b;
530 skb_trim(skb, b - skb->data);
534 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
536 struct xfrm_dump_info info;
538 info.in_skb = cb->skb;
540 info.nlmsg_seq = cb->nlh->nlmsg_seq;
541 info.nlmsg_flags = NLM_F_MULTI;
543 info.start_idx = cb->args[0];
544 (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
545 cb->args[0] = info.this_idx;
550 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
551 struct xfrm_state *x, u32 seq)
553 struct xfrm_dump_info info;
556 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
558 return ERR_PTR(-ENOMEM);
560 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
561 info.in_skb = in_skb;
563 info.nlmsg_seq = seq;
564 info.nlmsg_flags = 0;
565 info.this_idx = info.start_idx = 0;
567 if (dump_one_state(x, 0, &info)) {
575 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
577 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
578 struct xfrm_state *x;
579 struct sk_buff *resp_skb;
582 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
587 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
588 if (IS_ERR(resp_skb)) {
589 err = PTR_ERR(resp_skb);
591 err = netlink_unicast(xfrm_nl, resp_skb,
592 NETLINK_CB(skb).pid, MSG_DONTWAIT);
599 static int verify_userspi_info(struct xfrm_userspi_info *p)
601 switch (p->info.id.proto) {
607 /* IPCOMP spi is 16-bits. */
608 if (p->max >= 0x10000)
622 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
624 struct xfrm_state *x;
625 struct xfrm_userspi_info *p;
626 struct sk_buff *resp_skb;
627 xfrm_address_t *daddr;
632 err = verify_userspi_info(p);
636 family = p->info.family;
637 daddr = &p->info.id.daddr;
641 x = xfrm_find_acq_byseq(p->info.seq);
642 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
649 x = xfrm_find_acq(p->info.mode, p->info.reqid,
650 p->info.id.proto, daddr,
657 resp_skb = ERR_PTR(-ENOENT);
659 spin_lock_bh(&x->lock);
660 if (x->km.state != XFRM_STATE_DEAD) {
661 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
663 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
665 spin_unlock_bh(&x->lock);
667 if (IS_ERR(resp_skb)) {
668 err = PTR_ERR(resp_skb);
672 err = netlink_unicast(xfrm_nl, resp_skb,
673 NETLINK_CB(skb).pid, MSG_DONTWAIT);
681 static int verify_policy_dir(__u8 dir)
685 case XFRM_POLICY_OUT:
686 case XFRM_POLICY_FWD:
696 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
700 case XFRM_SHARE_SESSION:
701 case XFRM_SHARE_USER:
702 case XFRM_SHARE_UNIQUE:
710 case XFRM_POLICY_ALLOW:
711 case XFRM_POLICY_BLOCK:
718 switch (p->sel.family) {
723 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
726 return -EAFNOSUPPORT;
733 return verify_policy_dir(p->dir);
736 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
738 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
739 struct xfrm_user_sec_ctx *uctx;
745 return security_xfrm_policy_alloc(pol, uctx);
748 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
754 for (i = 0; i < nr; i++, ut++) {
755 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
757 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
758 memcpy(&t->saddr, &ut->saddr,
759 sizeof(xfrm_address_t));
760 t->reqid = ut->reqid;
762 t->share = ut->share;
763 t->optional = ut->optional;
764 t->aalgos = ut->aalgos;
765 t->ealgos = ut->ealgos;
766 t->calgos = ut->calgos;
770 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
772 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
773 struct xfrm_user_tmpl *utmpl;
779 nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
781 if (nr > XFRM_MAX_DEPTH)
784 copy_templates(pol, RTA_DATA(rt), nr);
789 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
791 xp->priority = p->priority;
792 xp->index = p->index;
793 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
794 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
795 xp->action = p->action;
796 xp->flags = p->flags;
797 xp->family = p->sel.family;
798 /* XXX xp->share = p->share; */
801 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
803 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
804 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
805 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
806 p->priority = xp->priority;
807 p->index = xp->index;
808 p->sel.family = xp->family;
810 p->action = xp->action;
811 p->flags = xp->flags;
812 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
815 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
817 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
825 copy_from_user_policy(xp, p);
827 if (!(err = copy_from_user_tmpl(xp, xfrma)))
828 err = copy_from_user_sec_ctx(xp, xfrma);
839 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
841 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
842 struct xfrm_policy *xp;
847 err = verify_newpolicy_info(p);
850 err = verify_sec_ctx_len((struct rtattr **)xfrma);
854 xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
858 /* shouldnt excl be based on nlh flags??
859 * Aha! this is anti-netlink really i.e more pfkey derived
860 * in netlink excl is a flag and you wouldnt need
861 * a type XFRM_MSG_UPDPOLICY - JHS */
862 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
863 err = xfrm_policy_insert(p->dir, xp, excl);
865 security_xfrm_policy_free(xp);
870 c.event = nlh->nlmsg_type;
871 c.seq = nlh->nlmsg_seq;
872 c.pid = nlh->nlmsg_pid;
873 km_policy_notify(xp, p->dir, &c);
880 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
882 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
885 if (xp->xfrm_nr == 0)
888 for (i = 0; i < xp->xfrm_nr; i++) {
889 struct xfrm_user_tmpl *up = &vec[i];
890 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
892 memcpy(&up->id, &kp->id, sizeof(up->id));
893 up->family = xp->family;
894 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
895 up->reqid = kp->reqid;
897 up->share = kp->share;
898 up->optional = kp->optional;
899 up->aalgos = kp->aalgos;
900 up->ealgos = kp->ealgos;
901 up->calgos = kp->calgos;
903 RTA_PUT(skb, XFRMA_TMPL,
904 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
913 static int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
916 int ctx_size = sizeof(struct xfrm_sec_ctx) +
917 xp->security->ctx_len;
918 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
919 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
921 uctx->exttype = XFRMA_SEC_CTX;
922 uctx->len = ctx_size;
923 uctx->ctx_doi = xp->security->ctx_doi;
924 uctx->ctx_alg = xp->security->ctx_alg;
925 uctx->ctx_len = xp->security->ctx_len;
926 memcpy(uctx + 1, xp->security->ctx_str, xp->security->ctx_len);
934 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
936 struct xfrm_dump_info *sp = ptr;
937 struct xfrm_userpolicy_info *p;
938 struct sk_buff *in_skb = sp->in_skb;
939 struct sk_buff *skb = sp->out_skb;
940 struct nlmsghdr *nlh;
941 unsigned char *b = skb->tail;
943 if (sp->this_idx < sp->start_idx)
946 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
948 XFRM_MSG_NEWPOLICY, sizeof(*p));
950 nlh->nlmsg_flags = sp->nlmsg_flags;
952 copy_to_user_policy(xp, p, dir);
953 if (copy_to_user_tmpl(xp, skb) < 0)
955 if (copy_to_user_sec_ctx(xp, skb))
958 nlh->nlmsg_len = skb->tail - b;
964 skb_trim(skb, b - skb->data);
968 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
970 struct xfrm_dump_info info;
972 info.in_skb = cb->skb;
974 info.nlmsg_seq = cb->nlh->nlmsg_seq;
975 info.nlmsg_flags = NLM_F_MULTI;
977 info.start_idx = cb->args[0];
978 (void) xfrm_policy_walk(dump_one_policy, &info);
979 cb->args[0] = info.this_idx;
984 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
985 struct xfrm_policy *xp,
988 struct xfrm_dump_info info;
991 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
993 return ERR_PTR(-ENOMEM);
995 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
996 info.in_skb = in_skb;
998 info.nlmsg_seq = seq;
999 info.nlmsg_flags = 0;
1000 info.this_idx = info.start_idx = 0;
1002 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1010 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1012 struct xfrm_policy *xp;
1013 struct xfrm_userpolicy_id *p;
1018 p = NLMSG_DATA(nlh);
1019 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1021 err = verify_policy_dir(p->dir);
1026 xp = xfrm_policy_byid(p->dir, p->index, delete);
1028 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1029 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1030 struct xfrm_policy tmp;
1032 err = verify_sec_ctx_len(rtattrs);
1036 memset(&tmp, 0, sizeof(struct xfrm_policy));
1038 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1040 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1043 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, delete);
1044 security_xfrm_policy_free(&tmp);
1050 struct sk_buff *resp_skb;
1052 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1053 if (IS_ERR(resp_skb)) {
1054 err = PTR_ERR(resp_skb);
1056 err = netlink_unicast(xfrm_nl, resp_skb,
1057 NETLINK_CB(skb).pid,
1061 c.data.byid = p->index;
1062 c.event = nlh->nlmsg_type;
1063 c.seq = nlh->nlmsg_seq;
1064 c.pid = nlh->nlmsg_pid;
1065 km_policy_notify(xp, p->dir, &c);
1073 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1076 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1078 xfrm_state_flush(p->proto);
1079 c.data.proto = p->proto;
1080 c.event = nlh->nlmsg_type;
1081 c.seq = nlh->nlmsg_seq;
1082 c.pid = nlh->nlmsg_pid;
1083 km_state_notify(NULL, &c);
1089 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1091 struct xfrm_aevent_id *id;
1092 struct nlmsghdr *nlh;
1093 struct xfrm_lifetime_cur ltime;
1094 unsigned char *b = skb->tail;
1096 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1097 id = NLMSG_DATA(nlh);
1098 nlh->nlmsg_flags = 0;
1100 id->sa_id.daddr = x->id.daddr;
1101 id->sa_id.spi = x->id.spi;
1102 id->sa_id.family = x->props.family;
1103 id->sa_id.proto = x->id.proto;
1104 id->flags = c->data.aevent;
1106 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1108 ltime.bytes = x->curlft.bytes;
1109 ltime.packets = x->curlft.packets;
1110 ltime.add_time = x->curlft.add_time;
1111 ltime.use_time = x->curlft.use_time;
1113 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), <ime);
1115 if (id->flags&XFRM_AE_RTHR) {
1116 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1119 if (id->flags&XFRM_AE_ETHR) {
1120 u32 etimer = x->replay_maxage*10/HZ;
1121 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1124 nlh->nlmsg_len = skb->tail - b;
1129 skb_trim(skb, b - skb->data);
1133 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1135 struct xfrm_state *x;
1136 struct sk_buff *r_skb;
1139 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1140 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1141 struct xfrm_usersa_id *id = &p->sa_id;
1143 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1144 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1146 if (p->flags&XFRM_AE_RTHR)
1147 len+=RTA_SPACE(sizeof(u32));
1149 if (p->flags&XFRM_AE_ETHR)
1150 len+=RTA_SPACE(sizeof(u32));
1152 r_skb = alloc_skb(len, GFP_ATOMIC);
1156 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1163 * XXX: is this lock really needed - none of the other
1164 * gets lock (the concern is things getting updated
1165 * while we are still reading) - jhs
1167 spin_lock_bh(&x->lock);
1168 c.data.aevent = p->flags;
1169 c.seq = nlh->nlmsg_seq;
1170 c.pid = nlh->nlmsg_pid;
1172 if (build_aevent(r_skb, x, &c) < 0)
1174 err = netlink_unicast(xfrm_nl, r_skb,
1175 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1176 spin_unlock_bh(&x->lock);
1181 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1183 struct xfrm_state *x;
1186 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1187 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1188 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1193 /* pedantic mode - thou shalt sayeth replaceth */
1194 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1197 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1201 if (x->km.state != XFRM_STATE_VALID)
1204 spin_lock_bh(&x->lock);
1205 err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
1206 spin_unlock_bh(&x->lock);
1210 c.event = nlh->nlmsg_type;
1211 c.seq = nlh->nlmsg_seq;
1212 c.pid = nlh->nlmsg_pid;
1213 c.data.aevent = XFRM_AE_CU;
1214 km_state_notify(x, &c);
1221 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1225 xfrm_policy_flush();
1226 c.event = nlh->nlmsg_type;
1227 c.seq = nlh->nlmsg_seq;
1228 c.pid = nlh->nlmsg_pid;
1229 km_policy_notify(NULL, 0, &c);
1233 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1235 struct xfrm_policy *xp;
1236 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1237 struct xfrm_userpolicy_info *p = &up->pol;
1241 xp = xfrm_policy_byid(p->dir, p->index, 0);
1243 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1244 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1245 struct xfrm_policy tmp;
1247 err = verify_sec_ctx_len(rtattrs);
1251 memset(&tmp, 0, sizeof(struct xfrm_policy));
1253 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1255 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1258 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, 0);
1259 security_xfrm_policy_free(&tmp);
1264 read_lock(&xp->lock);
1266 read_unlock(&xp->lock);
1270 read_unlock(&xp->lock);
1273 xfrm_policy_delete(xp, p->dir);
1275 // reset the timers here?
1276 printk("Dont know what to do with soft policy expire\n");
1278 km_policy_expired(xp, p->dir, up->hard, current->pid);
1285 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1287 struct xfrm_state *x;
1289 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1290 struct xfrm_usersa_info *p = &ue->state;
1292 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1300 spin_lock_bh(&x->lock);
1301 if (x->km.state != XFRM_STATE_VALID)
1303 km_state_expired(x, ue->hard, current->pid);
1306 __xfrm_state_delete(x);
1308 spin_unlock_bh(&x->lock);
1313 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1315 struct xfrm_policy *xp;
1316 struct xfrm_user_tmpl *ut;
1318 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1320 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1321 struct xfrm_state *x = xfrm_state_alloc();
1327 err = verify_newpolicy_info(&ua->policy);
1329 printk("BAD policy passed\n");
1335 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err); if (!xp) {
1340 memcpy(&x->id, &ua->id, sizeof(ua->id));
1341 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1342 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1345 /* extract the templates and for each call km_key */
1346 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1347 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1348 memcpy(&x->id, &t->id, sizeof(x->id));
1349 x->props.mode = t->mode;
1350 x->props.reqid = t->reqid;
1351 x->props.family = ut->family;
1352 t->aalgos = ua->aalgos;
1353 t->ealgos = ua->ealgos;
1354 t->calgos = ua->calgos;
1355 err = km_query(x, t, xp);
1366 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1368 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1369 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1370 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1371 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1372 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1373 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1374 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1375 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1376 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1377 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1378 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1379 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1380 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1381 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1382 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
1383 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1384 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1389 static struct xfrm_link {
1390 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1391 int (*dump)(struct sk_buff *, struct netlink_callback *);
1392 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1393 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1394 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1395 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1396 .dump = xfrm_dump_sa },
1397 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1398 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1399 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1400 .dump = xfrm_dump_policy },
1401 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1402 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
1403 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1404 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1405 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1406 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1407 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1408 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
1409 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1410 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1413 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1415 struct rtattr *xfrma[XFRMA_MAX];
1416 struct xfrm_link *link;
1419 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1422 type = nlh->nlmsg_type;
1424 /* A control message: ignore them */
1425 if (type < XFRM_MSG_BASE)
1428 /* Unknown message: reply with EINVAL */
1429 if (type > XFRM_MSG_MAX)
1432 type -= XFRM_MSG_BASE;
1433 link = &xfrm_dispatch[type];
1435 /* All operations require privileges, even GET */
1436 if (security_netlink_recv(skb)) {
1441 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1442 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1443 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1444 if (link->dump == NULL)
1447 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
1448 link->dump, NULL)) != 0) {
1452 netlink_queue_skip(nlh, skb);
1456 memset(xfrma, 0, sizeof(xfrma));
1458 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1461 if (nlh->nlmsg_len > min_len) {
1462 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1463 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1465 while (RTA_OK(attr, attrlen)) {
1466 unsigned short flavor = attr->rta_type;
1468 if (flavor > XFRMA_MAX)
1470 xfrma[flavor - 1] = attr;
1472 attr = RTA_NEXT(attr, attrlen);
1476 if (link->doit == NULL)
1478 *errp = link->doit(skb, nlh, (void **) &xfrma);
1487 static void xfrm_netlink_rcv(struct sock *sk, int len)
1489 unsigned int qlen = 0;
1492 down(&xfrm_cfg_sem);
1493 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
1499 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1501 struct xfrm_user_expire *ue;
1502 struct nlmsghdr *nlh;
1503 unsigned char *b = skb->tail;
1505 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
1507 ue = NLMSG_DATA(nlh);
1508 nlh->nlmsg_flags = 0;
1510 copy_to_user_state(x, &ue->state);
1511 ue->hard = (c->data.hard != 0) ? 1 : 0;
1513 nlh->nlmsg_len = skb->tail - b;
1517 skb_trim(skb, b - skb->data);
1521 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1523 struct sk_buff *skb;
1524 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
1526 skb = alloc_skb(len, GFP_ATOMIC);
1530 if (build_expire(skb, x, c) < 0)
1533 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1534 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1537 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1539 struct sk_buff *skb;
1540 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1542 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1543 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1544 skb = alloc_skb(len, GFP_ATOMIC);
1548 if (build_aevent(skb, x, c) < 0)
1551 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1552 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1555 static int xfrm_notify_sa_flush(struct km_event *c)
1557 struct xfrm_usersa_flush *p;
1558 struct nlmsghdr *nlh;
1559 struct sk_buff *skb;
1561 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1563 skb = alloc_skb(len, GFP_ATOMIC);
1568 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1569 XFRM_MSG_FLUSHSA, sizeof(*p));
1570 nlh->nlmsg_flags = 0;
1572 p = NLMSG_DATA(nlh);
1573 p->proto = c->data.proto;
1575 nlh->nlmsg_len = skb->tail - b;
1577 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1578 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1585 static int inline xfrm_sa_len(struct xfrm_state *x)
1589 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1591 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1593 l += RTA_SPACE(sizeof(*x->calg));
1595 l += RTA_SPACE(sizeof(*x->encap));
1600 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1602 struct xfrm_usersa_info *p;
1603 struct xfrm_usersa_id *id;
1604 struct nlmsghdr *nlh;
1605 struct sk_buff *skb;
1607 int len = xfrm_sa_len(x);
1610 headlen = sizeof(*p);
1611 if (c->event == XFRM_MSG_DELSA) {
1612 len += RTA_SPACE(headlen);
1613 headlen = sizeof(*id);
1615 len += NLMSG_SPACE(headlen);
1617 skb = alloc_skb(len, GFP_ATOMIC);
1622 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
1623 nlh->nlmsg_flags = 0;
1625 p = NLMSG_DATA(nlh);
1626 if (c->event == XFRM_MSG_DELSA) {
1627 id = NLMSG_DATA(nlh);
1628 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1629 id->spi = x->id.spi;
1630 id->family = x->props.family;
1631 id->proto = x->id.proto;
1633 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1636 copy_to_user_state(x, p);
1639 RTA_PUT(skb, XFRMA_ALG_AUTH,
1640 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1642 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1643 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1645 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1648 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1650 nlh->nlmsg_len = skb->tail - b;
1652 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1653 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1661 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1665 case XFRM_MSG_EXPIRE:
1666 return xfrm_exp_state_notify(x, c);
1667 case XFRM_MSG_NEWAE:
1668 return xfrm_aevent_state_notify(x, c);
1669 case XFRM_MSG_DELSA:
1670 case XFRM_MSG_UPDSA:
1671 case XFRM_MSG_NEWSA:
1672 return xfrm_notify_sa(x, c);
1673 case XFRM_MSG_FLUSHSA:
1674 return xfrm_notify_sa_flush(c);
1676 printk("xfrm_user: Unknown SA event %d\n", c->event);
1684 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1685 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1688 struct xfrm_user_acquire *ua;
1689 struct nlmsghdr *nlh;
1690 unsigned char *b = skb->tail;
1691 __u32 seq = xfrm_get_acqseq();
1693 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1695 ua = NLMSG_DATA(nlh);
1696 nlh->nlmsg_flags = 0;
1698 memcpy(&ua->id, &x->id, sizeof(ua->id));
1699 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1700 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1701 copy_to_user_policy(xp, &ua->policy, dir);
1702 ua->aalgos = xt->aalgos;
1703 ua->ealgos = xt->ealgos;
1704 ua->calgos = xt->calgos;
1705 ua->seq = x->km.seq = seq;
1707 if (copy_to_user_tmpl(xp, skb) < 0)
1709 if (copy_to_user_sec_ctx(xp, skb))
1712 nlh->nlmsg_len = skb->tail - b;
1716 skb_trim(skb, b - skb->data);
1720 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1721 struct xfrm_policy *xp, int dir)
1723 struct sk_buff *skb;
1726 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1727 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
1728 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1729 skb = alloc_skb(len, GFP_ATOMIC);
1733 if (build_acquire(skb, x, xt, xp, dir) < 0)
1736 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
1737 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
1740 /* User gives us xfrm_user_policy_info followed by an array of 0
1741 * or more templates.
1743 static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
1744 u8 *data, int len, int *dir)
1746 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1747 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1748 struct xfrm_policy *xp;
1753 if (opt != IP_XFRM_POLICY) {
1758 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1760 if (opt != IPV6_XFRM_POLICY) {
1773 if (len < sizeof(*p) ||
1774 verify_newpolicy_info(p))
1777 nr = ((len - sizeof(*p)) / sizeof(*ut));
1778 if (nr > XFRM_MAX_DEPTH)
1781 if (p->dir > XFRM_POLICY_OUT)
1784 xp = xfrm_policy_alloc(GFP_KERNEL);
1790 copy_from_user_policy(xp, p);
1791 copy_templates(xp, ut, nr);
1798 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
1799 int dir, struct km_event *c)
1801 struct xfrm_user_polexpire *upe;
1802 struct nlmsghdr *nlh;
1803 int hard = c->data.hard;
1804 unsigned char *b = skb->tail;
1806 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
1807 upe = NLMSG_DATA(nlh);
1808 nlh->nlmsg_flags = 0;
1810 copy_to_user_policy(xp, &upe->pol, dir);
1811 if (copy_to_user_tmpl(xp, skb) < 0)
1813 if (copy_to_user_sec_ctx(xp, skb))
1817 nlh->nlmsg_len = skb->tail - b;
1821 skb_trim(skb, b - skb->data);
1825 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1827 struct sk_buff *skb;
1830 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1831 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
1832 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1833 skb = alloc_skb(len, GFP_ATOMIC);
1837 if (build_polexpire(skb, xp, dir, c) < 0)
1840 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1841 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1844 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
1846 struct xfrm_userpolicy_info *p;
1847 struct xfrm_userpolicy_id *id;
1848 struct nlmsghdr *nlh;
1849 struct sk_buff *skb;
1851 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1854 headlen = sizeof(*p);
1855 if (c->event == XFRM_MSG_DELPOLICY) {
1856 len += RTA_SPACE(headlen);
1857 headlen = sizeof(*id);
1859 len += NLMSG_SPACE(headlen);
1861 skb = alloc_skb(len, GFP_ATOMIC);
1866 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
1868 p = NLMSG_DATA(nlh);
1869 if (c->event == XFRM_MSG_DELPOLICY) {
1870 id = NLMSG_DATA(nlh);
1871 memset(id, 0, sizeof(*id));
1874 id->index = xp->index;
1876 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
1878 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
1881 nlh->nlmsg_flags = 0;
1883 copy_to_user_policy(xp, p, dir);
1884 if (copy_to_user_tmpl(xp, skb) < 0)
1887 nlh->nlmsg_len = skb->tail - b;
1889 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1890 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
1898 static int xfrm_notify_policy_flush(struct km_event *c)
1900 struct nlmsghdr *nlh;
1901 struct sk_buff *skb;
1903 int len = NLMSG_LENGTH(0);
1905 skb = alloc_skb(len, GFP_ATOMIC);
1911 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
1913 nlh->nlmsg_len = skb->tail - b;
1915 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1916 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
1923 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1927 case XFRM_MSG_NEWPOLICY:
1928 case XFRM_MSG_UPDPOLICY:
1929 case XFRM_MSG_DELPOLICY:
1930 return xfrm_notify_policy(xp, dir, c);
1931 case XFRM_MSG_FLUSHPOLICY:
1932 return xfrm_notify_policy_flush(c);
1933 case XFRM_MSG_POLEXPIRE:
1934 return xfrm_exp_policy_notify(xp, dir, c);
1936 printk("xfrm_user: Unknown Policy event %d\n", c->event);
1943 static struct xfrm_mgr netlink_mgr = {
1945 .notify = xfrm_send_state_notify,
1946 .acquire = xfrm_send_acquire,
1947 .compile_policy = xfrm_compile_policy,
1948 .notify_policy = xfrm_send_policy_notify,
1951 static int __init xfrm_user_init(void)
1953 printk(KERN_INFO "Initializing IPsec netlink socket\n");
1955 xfrm_nl = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
1956 xfrm_netlink_rcv, THIS_MODULE);
1957 if (xfrm_nl == NULL)
1960 xfrm_register_km(&netlink_mgr);
1965 static void __exit xfrm_user_exit(void)
1967 xfrm_unregister_km(&netlink_mgr);
1968 sock_release(xfrm_nl->sk_socket);
1971 module_init(xfrm_user_init);
1972 module_exit(xfrm_user_exit);
1973 MODULE_LICENSE("GPL");
1974 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);