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);
220 static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
222 struct rtattr *rta = u_arg;
223 struct xfrm_encap_tmpl *p, *uencap;
228 uencap = RTA_DATA(rta);
229 p = kmalloc(sizeof(*p), GFP_KERNEL);
233 memcpy(p, uencap, sizeof(*p));
239 static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
241 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
245 len += sizeof(struct xfrm_user_sec_ctx);
246 len += xfrm_ctx->ctx_len;
251 static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
253 struct xfrm_user_sec_ctx *uctx;
258 uctx = RTA_DATA(u_arg);
259 return security_xfrm_state_alloc(x, uctx);
262 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
264 memcpy(&x->id, &p->id, sizeof(x->id));
265 memcpy(&x->sel, &p->sel, sizeof(x->sel));
266 memcpy(&x->lft, &p->lft, sizeof(x->lft));
267 x->props.mode = p->mode;
268 x->props.replay_window = p->replay_window;
269 x->props.reqid = p->reqid;
270 x->props.family = p->family;
271 x->props.saddr = p->saddr;
272 x->props.flags = p->flags;
276 * someday when pfkey also has support, we could have the code
277 * somehow made shareable and move it to xfrm_state.c - JHS
280 static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
283 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
284 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
285 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
286 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
289 struct xfrm_replay_state *replay;
290 if (RTA_PAYLOAD(rp) < sizeof(*replay))
292 replay = RTA_DATA(rp);
293 memcpy(&x->replay, replay, sizeof(*replay));
294 memcpy(&x->preplay, replay, sizeof(*replay));
298 struct xfrm_lifetime_cur *ltime;
299 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
301 ltime = RTA_DATA(lt);
302 x->curlft.bytes = ltime->bytes;
303 x->curlft.packets = ltime->packets;
304 x->curlft.add_time = ltime->add_time;
305 x->curlft.use_time = ltime->use_time;
309 if (RTA_PAYLOAD(et) < sizeof(u32))
311 x->replay_maxage = *(u32*)RTA_DATA(et);
315 if (RTA_PAYLOAD(rt) < sizeof(u32))
317 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
325 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
326 struct rtattr **xfrma,
329 struct xfrm_state *x = xfrm_state_alloc();
335 copy_from_user_state(x, p);
337 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
338 xfrm_aalg_get_byname,
339 xfrma[XFRMA_ALG_AUTH-1])))
341 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
342 xfrm_ealg_get_byname,
343 xfrma[XFRMA_ALG_CRYPT-1])))
345 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
346 xfrm_calg_get_byname,
347 xfrma[XFRMA_ALG_COMP-1])))
349 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
352 err = xfrm_init_state(x);
356 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
360 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
361 /* sysctl_xfrm_aevent_etime is in 100ms units */
362 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
363 x->preplay.bitmap = 0;
364 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
365 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
367 /* override default values from above */
369 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
376 x->km.state = XFRM_STATE_DEAD;
383 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
385 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
386 struct xfrm_state *x;
390 err = verify_newsa_info(p, (struct rtattr **)xfrma);
394 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
399 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
400 err = xfrm_state_add(x);
402 err = xfrm_state_update(x);
405 x->km.state = XFRM_STATE_DEAD;
410 c.seq = nlh->nlmsg_seq;
411 c.pid = nlh->nlmsg_pid;
412 c.event = nlh->nlmsg_type;
414 km_state_notify(x, &c);
420 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
422 struct xfrm_state *x;
425 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
427 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
431 if ((err = security_xfrm_state_delete(x)) != 0)
434 if (xfrm_state_kern(x)) {
439 err = xfrm_state_delete(x);
443 c.seq = nlh->nlmsg_seq;
444 c.pid = nlh->nlmsg_pid;
445 c.event = nlh->nlmsg_type;
446 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 if ((err = security_xfrm_policy_delete(xp)) != 0)
1063 c.data.byid = p->index;
1064 c.event = nlh->nlmsg_type;
1065 c.seq = nlh->nlmsg_seq;
1066 c.pid = nlh->nlmsg_pid;
1067 km_policy_notify(xp, p->dir, &c);
1076 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1079 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1081 xfrm_state_flush(p->proto);
1082 c.data.proto = p->proto;
1083 c.event = nlh->nlmsg_type;
1084 c.seq = nlh->nlmsg_seq;
1085 c.pid = nlh->nlmsg_pid;
1086 km_state_notify(NULL, &c);
1092 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1094 struct xfrm_aevent_id *id;
1095 struct nlmsghdr *nlh;
1096 struct xfrm_lifetime_cur ltime;
1097 unsigned char *b = skb->tail;
1099 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1100 id = NLMSG_DATA(nlh);
1101 nlh->nlmsg_flags = 0;
1103 id->sa_id.daddr = x->id.daddr;
1104 id->sa_id.spi = x->id.spi;
1105 id->sa_id.family = x->props.family;
1106 id->sa_id.proto = x->id.proto;
1107 id->flags = c->data.aevent;
1109 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1111 ltime.bytes = x->curlft.bytes;
1112 ltime.packets = x->curlft.packets;
1113 ltime.add_time = x->curlft.add_time;
1114 ltime.use_time = x->curlft.use_time;
1116 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), <ime);
1118 if (id->flags&XFRM_AE_RTHR) {
1119 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1122 if (id->flags&XFRM_AE_ETHR) {
1123 u32 etimer = x->replay_maxage*10/HZ;
1124 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1127 nlh->nlmsg_len = skb->tail - b;
1132 skb_trim(skb, b - skb->data);
1136 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1138 struct xfrm_state *x;
1139 struct sk_buff *r_skb;
1142 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1143 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1144 struct xfrm_usersa_id *id = &p->sa_id;
1146 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1147 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1149 if (p->flags&XFRM_AE_RTHR)
1150 len+=RTA_SPACE(sizeof(u32));
1152 if (p->flags&XFRM_AE_ETHR)
1153 len+=RTA_SPACE(sizeof(u32));
1155 r_skb = alloc_skb(len, GFP_ATOMIC);
1159 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1166 * XXX: is this lock really needed - none of the other
1167 * gets lock (the concern is things getting updated
1168 * while we are still reading) - jhs
1170 spin_lock_bh(&x->lock);
1171 c.data.aevent = p->flags;
1172 c.seq = nlh->nlmsg_seq;
1173 c.pid = nlh->nlmsg_pid;
1175 if (build_aevent(r_skb, x, &c) < 0)
1177 err = netlink_unicast(xfrm_nl, r_skb,
1178 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1179 spin_unlock_bh(&x->lock);
1184 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1186 struct xfrm_state *x;
1189 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1190 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1191 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1196 /* pedantic mode - thou shalt sayeth replaceth */
1197 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1200 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1204 if (x->km.state != XFRM_STATE_VALID)
1207 spin_lock_bh(&x->lock);
1208 err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
1209 spin_unlock_bh(&x->lock);
1213 c.event = nlh->nlmsg_type;
1214 c.seq = nlh->nlmsg_seq;
1215 c.pid = nlh->nlmsg_pid;
1216 c.data.aevent = XFRM_AE_CU;
1217 km_state_notify(x, &c);
1224 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1228 xfrm_policy_flush();
1229 c.event = nlh->nlmsg_type;
1230 c.seq = nlh->nlmsg_seq;
1231 c.pid = nlh->nlmsg_pid;
1232 km_policy_notify(NULL, 0, &c);
1236 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1238 struct xfrm_policy *xp;
1239 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1240 struct xfrm_userpolicy_info *p = &up->pol;
1244 xp = xfrm_policy_byid(p->dir, p->index, 0);
1246 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1247 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1248 struct xfrm_policy tmp;
1250 err = verify_sec_ctx_len(rtattrs);
1254 memset(&tmp, 0, sizeof(struct xfrm_policy));
1256 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1258 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1261 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, 0);
1262 security_xfrm_policy_free(&tmp);
1267 read_lock(&xp->lock);
1269 read_unlock(&xp->lock);
1273 read_unlock(&xp->lock);
1276 xfrm_policy_delete(xp, p->dir);
1278 // reset the timers here?
1279 printk("Dont know what to do with soft policy expire\n");
1281 km_policy_expired(xp, p->dir, up->hard, current->pid);
1288 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1290 struct xfrm_state *x;
1292 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1293 struct xfrm_usersa_info *p = &ue->state;
1295 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1303 spin_lock_bh(&x->lock);
1304 if (x->km.state != XFRM_STATE_VALID)
1306 km_state_expired(x, ue->hard, current->pid);
1309 __xfrm_state_delete(x);
1311 spin_unlock_bh(&x->lock);
1316 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1318 struct xfrm_policy *xp;
1319 struct xfrm_user_tmpl *ut;
1321 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1323 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1324 struct xfrm_state *x = xfrm_state_alloc();
1330 err = verify_newpolicy_info(&ua->policy);
1332 printk("BAD policy passed\n");
1338 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err); if (!xp) {
1343 memcpy(&x->id, &ua->id, sizeof(ua->id));
1344 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1345 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1348 /* extract the templates and for each call km_key */
1349 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1350 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1351 memcpy(&x->id, &t->id, sizeof(x->id));
1352 x->props.mode = t->mode;
1353 x->props.reqid = t->reqid;
1354 x->props.family = ut->family;
1355 t->aalgos = ua->aalgos;
1356 t->ealgos = ua->ealgos;
1357 t->calgos = ua->calgos;
1358 err = km_query(x, t, xp);
1369 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1371 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1372 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1373 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1374 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1375 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1376 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1377 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1378 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1379 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1380 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1381 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1382 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1383 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1384 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1385 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
1386 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1387 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1392 static struct xfrm_link {
1393 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1394 int (*dump)(struct sk_buff *, struct netlink_callback *);
1395 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1396 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1397 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1398 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1399 .dump = xfrm_dump_sa },
1400 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1401 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1402 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1403 .dump = xfrm_dump_policy },
1404 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1405 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
1406 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1407 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1408 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1409 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1410 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1411 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
1412 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1413 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1416 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1418 struct rtattr *xfrma[XFRMA_MAX];
1419 struct xfrm_link *link;
1422 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1425 type = nlh->nlmsg_type;
1427 /* A control message: ignore them */
1428 if (type < XFRM_MSG_BASE)
1431 /* Unknown message: reply with EINVAL */
1432 if (type > XFRM_MSG_MAX)
1435 type -= XFRM_MSG_BASE;
1436 link = &xfrm_dispatch[type];
1438 /* All operations require privileges, even GET */
1439 if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
1444 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1445 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1446 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1447 if (link->dump == NULL)
1450 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
1451 link->dump, NULL)) != 0) {
1455 netlink_queue_skip(nlh, skb);
1459 memset(xfrma, 0, sizeof(xfrma));
1461 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1464 if (nlh->nlmsg_len > min_len) {
1465 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1466 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1468 while (RTA_OK(attr, attrlen)) {
1469 unsigned short flavor = attr->rta_type;
1471 if (flavor > XFRMA_MAX)
1473 xfrma[flavor - 1] = attr;
1475 attr = RTA_NEXT(attr, attrlen);
1479 if (link->doit == NULL)
1481 *errp = link->doit(skb, nlh, (void **) &xfrma);
1490 static void xfrm_netlink_rcv(struct sock *sk, int len)
1492 unsigned int qlen = 0;
1495 mutex_lock(&xfrm_cfg_mutex);
1496 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
1497 mutex_unlock(&xfrm_cfg_mutex);
1502 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1504 struct xfrm_user_expire *ue;
1505 struct nlmsghdr *nlh;
1506 unsigned char *b = skb->tail;
1508 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
1510 ue = NLMSG_DATA(nlh);
1511 nlh->nlmsg_flags = 0;
1513 copy_to_user_state(x, &ue->state);
1514 ue->hard = (c->data.hard != 0) ? 1 : 0;
1516 nlh->nlmsg_len = skb->tail - b;
1520 skb_trim(skb, b - skb->data);
1524 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1526 struct sk_buff *skb;
1527 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
1529 skb = alloc_skb(len, GFP_ATOMIC);
1533 if (build_expire(skb, x, c) < 0)
1536 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1537 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1540 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1542 struct sk_buff *skb;
1543 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1545 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1546 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1547 skb = alloc_skb(len, GFP_ATOMIC);
1551 if (build_aevent(skb, x, c) < 0)
1554 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1555 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1558 static int xfrm_notify_sa_flush(struct km_event *c)
1560 struct xfrm_usersa_flush *p;
1561 struct nlmsghdr *nlh;
1562 struct sk_buff *skb;
1564 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1566 skb = alloc_skb(len, GFP_ATOMIC);
1571 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1572 XFRM_MSG_FLUSHSA, sizeof(*p));
1573 nlh->nlmsg_flags = 0;
1575 p = NLMSG_DATA(nlh);
1576 p->proto = c->data.proto;
1578 nlh->nlmsg_len = skb->tail - b;
1580 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1581 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1588 static int inline xfrm_sa_len(struct xfrm_state *x)
1592 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1594 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1596 l += RTA_SPACE(sizeof(*x->calg));
1598 l += RTA_SPACE(sizeof(*x->encap));
1603 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1605 struct xfrm_usersa_info *p;
1606 struct xfrm_usersa_id *id;
1607 struct nlmsghdr *nlh;
1608 struct sk_buff *skb;
1610 int len = xfrm_sa_len(x);
1613 headlen = sizeof(*p);
1614 if (c->event == XFRM_MSG_DELSA) {
1615 len += RTA_SPACE(headlen);
1616 headlen = sizeof(*id);
1618 len += NLMSG_SPACE(headlen);
1620 skb = alloc_skb(len, GFP_ATOMIC);
1625 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
1626 nlh->nlmsg_flags = 0;
1628 p = NLMSG_DATA(nlh);
1629 if (c->event == XFRM_MSG_DELSA) {
1630 id = NLMSG_DATA(nlh);
1631 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1632 id->spi = x->id.spi;
1633 id->family = x->props.family;
1634 id->proto = x->id.proto;
1636 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1639 copy_to_user_state(x, p);
1642 RTA_PUT(skb, XFRMA_ALG_AUTH,
1643 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1645 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1646 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1648 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1651 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1653 nlh->nlmsg_len = skb->tail - b;
1655 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1656 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1664 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1668 case XFRM_MSG_EXPIRE:
1669 return xfrm_exp_state_notify(x, c);
1670 case XFRM_MSG_NEWAE:
1671 return xfrm_aevent_state_notify(x, c);
1672 case XFRM_MSG_DELSA:
1673 case XFRM_MSG_UPDSA:
1674 case XFRM_MSG_NEWSA:
1675 return xfrm_notify_sa(x, c);
1676 case XFRM_MSG_FLUSHSA:
1677 return xfrm_notify_sa_flush(c);
1679 printk("xfrm_user: Unknown SA event %d\n", c->event);
1687 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1688 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1691 struct xfrm_user_acquire *ua;
1692 struct nlmsghdr *nlh;
1693 unsigned char *b = skb->tail;
1694 __u32 seq = xfrm_get_acqseq();
1696 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1698 ua = NLMSG_DATA(nlh);
1699 nlh->nlmsg_flags = 0;
1701 memcpy(&ua->id, &x->id, sizeof(ua->id));
1702 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1703 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1704 copy_to_user_policy(xp, &ua->policy, dir);
1705 ua->aalgos = xt->aalgos;
1706 ua->ealgos = xt->ealgos;
1707 ua->calgos = xt->calgos;
1708 ua->seq = x->km.seq = seq;
1710 if (copy_to_user_tmpl(xp, skb) < 0)
1712 if (copy_to_user_sec_ctx(xp, skb))
1715 nlh->nlmsg_len = skb->tail - b;
1719 skb_trim(skb, b - skb->data);
1723 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1724 struct xfrm_policy *xp, int dir)
1726 struct sk_buff *skb;
1729 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1730 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
1731 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1732 skb = alloc_skb(len, GFP_ATOMIC);
1736 if (build_acquire(skb, x, xt, xp, dir) < 0)
1739 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
1740 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
1743 /* User gives us xfrm_user_policy_info followed by an array of 0
1744 * or more templates.
1746 static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
1747 u8 *data, int len, int *dir)
1749 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1750 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1751 struct xfrm_policy *xp;
1756 if (opt != IP_XFRM_POLICY) {
1761 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1763 if (opt != IPV6_XFRM_POLICY) {
1776 if (len < sizeof(*p) ||
1777 verify_newpolicy_info(p))
1780 nr = ((len - sizeof(*p)) / sizeof(*ut));
1781 if (nr > XFRM_MAX_DEPTH)
1784 if (p->dir > XFRM_POLICY_OUT)
1787 xp = xfrm_policy_alloc(GFP_KERNEL);
1793 copy_from_user_policy(xp, p);
1794 copy_templates(xp, ut, nr);
1801 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
1802 int dir, struct km_event *c)
1804 struct xfrm_user_polexpire *upe;
1805 struct nlmsghdr *nlh;
1806 int hard = c->data.hard;
1807 unsigned char *b = skb->tail;
1809 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
1810 upe = NLMSG_DATA(nlh);
1811 nlh->nlmsg_flags = 0;
1813 copy_to_user_policy(xp, &upe->pol, dir);
1814 if (copy_to_user_tmpl(xp, skb) < 0)
1816 if (copy_to_user_sec_ctx(xp, skb))
1820 nlh->nlmsg_len = skb->tail - b;
1824 skb_trim(skb, b - skb->data);
1828 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1830 struct sk_buff *skb;
1833 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1834 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
1835 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1836 skb = alloc_skb(len, GFP_ATOMIC);
1840 if (build_polexpire(skb, xp, dir, c) < 0)
1843 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1844 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1847 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
1849 struct xfrm_userpolicy_info *p;
1850 struct xfrm_userpolicy_id *id;
1851 struct nlmsghdr *nlh;
1852 struct sk_buff *skb;
1854 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1857 headlen = sizeof(*p);
1858 if (c->event == XFRM_MSG_DELPOLICY) {
1859 len += RTA_SPACE(headlen);
1860 headlen = sizeof(*id);
1862 len += NLMSG_SPACE(headlen);
1864 skb = alloc_skb(len, GFP_ATOMIC);
1869 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
1871 p = NLMSG_DATA(nlh);
1872 if (c->event == XFRM_MSG_DELPOLICY) {
1873 id = NLMSG_DATA(nlh);
1874 memset(id, 0, sizeof(*id));
1877 id->index = xp->index;
1879 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
1881 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
1884 nlh->nlmsg_flags = 0;
1886 copy_to_user_policy(xp, p, dir);
1887 if (copy_to_user_tmpl(xp, skb) < 0)
1890 nlh->nlmsg_len = skb->tail - b;
1892 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1893 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
1901 static int xfrm_notify_policy_flush(struct km_event *c)
1903 struct nlmsghdr *nlh;
1904 struct sk_buff *skb;
1906 int len = NLMSG_LENGTH(0);
1908 skb = alloc_skb(len, GFP_ATOMIC);
1914 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
1916 nlh->nlmsg_len = skb->tail - b;
1918 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1919 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
1926 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1930 case XFRM_MSG_NEWPOLICY:
1931 case XFRM_MSG_UPDPOLICY:
1932 case XFRM_MSG_DELPOLICY:
1933 return xfrm_notify_policy(xp, dir, c);
1934 case XFRM_MSG_FLUSHPOLICY:
1935 return xfrm_notify_policy_flush(c);
1936 case XFRM_MSG_POLEXPIRE:
1937 return xfrm_exp_policy_notify(xp, dir, c);
1939 printk("xfrm_user: Unknown Policy event %d\n", c->event);
1946 static struct xfrm_mgr netlink_mgr = {
1948 .notify = xfrm_send_state_notify,
1949 .acquire = xfrm_send_acquire,
1950 .compile_policy = xfrm_compile_policy,
1951 .notify_policy = xfrm_send_policy_notify,
1954 static int __init xfrm_user_init(void)
1958 printk(KERN_INFO "Initializing IPsec netlink socket\n");
1960 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
1961 xfrm_netlink_rcv, THIS_MODULE);
1964 rcu_assign_pointer(xfrm_nl, nlsk);
1966 xfrm_register_km(&netlink_mgr);
1971 static void __exit xfrm_user_exit(void)
1973 struct sock *nlsk = xfrm_nl;
1975 xfrm_unregister_km(&netlink_mgr);
1976 rcu_assign_pointer(xfrm_nl, NULL);
1978 sock_release(nlsk->sk_socket);
1981 module_init(xfrm_user_init);
1982 module_exit(xfrm_user_exit);
1983 MODULE_LICENSE("GPL");
1984 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);