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>
33 static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
35 struct rtattr *rt = xfrma[type - 1];
36 struct xfrm_algo *algp;
42 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
48 len -= (algp->alg_key_len + 7U) / 8;
54 if (!algp->alg_key_len &&
55 strcmp(algp->alg_name, "digest_null") != 0)
60 if (!algp->alg_key_len &&
61 strcmp(algp->alg_name, "cipher_null") != 0)
66 /* Zero length keys are legal. */
73 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
77 static int verify_encap_tmpl(struct rtattr **xfrma)
79 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
80 struct xfrm_encap_tmpl *encap;
85 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
92 static inline int verify_sec_ctx_len(struct rtattr **xfrma)
94 struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
95 struct xfrm_user_sec_ctx *uctx;
101 if (rt->rta_len < sizeof(*uctx))
106 if (uctx->ctx_len > PAGE_SIZE)
109 len += sizeof(struct xfrm_user_sec_ctx);
110 len += uctx->ctx_len;
112 if (uctx->len != len)
119 static int verify_newsa_info(struct xfrm_usersa_info *p,
120 struct rtattr **xfrma)
130 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
142 switch (p->id.proto) {
144 if (!xfrma[XFRMA_ALG_AUTH-1] ||
145 xfrma[XFRMA_ALG_CRYPT-1] ||
146 xfrma[XFRMA_ALG_COMP-1])
151 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
152 !xfrma[XFRMA_ALG_CRYPT-1]) ||
153 xfrma[XFRMA_ALG_COMP-1])
158 if (!xfrma[XFRMA_ALG_COMP-1] ||
159 xfrma[XFRMA_ALG_AUTH-1] ||
160 xfrma[XFRMA_ALG_CRYPT-1])
168 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
170 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
172 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
174 if ((err = verify_encap_tmpl(xfrma)))
176 if ((err = verify_sec_ctx_len(xfrma)))
195 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
196 struct xfrm_algo_desc *(*get_byname)(char *, int),
197 struct rtattr *u_arg)
199 struct rtattr *rta = u_arg;
200 struct xfrm_algo *p, *ualg;
201 struct xfrm_algo_desc *algo;
207 ualg = RTA_DATA(rta);
209 algo = get_byname(ualg->alg_name, 1);
212 *props = algo->desc.sadb_alg_id;
214 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
215 p = kmalloc(len, GFP_KERNEL);
219 memcpy(p, ualg, len);
224 static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
226 struct rtattr *rta = u_arg;
227 struct xfrm_encap_tmpl *p, *uencap;
232 uencap = RTA_DATA(rta);
233 p = kmalloc(sizeof(*p), GFP_KERNEL);
237 memcpy(p, uencap, sizeof(*p));
243 static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
245 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
249 len += sizeof(struct xfrm_user_sec_ctx);
250 len += xfrm_ctx->ctx_len;
255 static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
257 struct xfrm_user_sec_ctx *uctx;
262 uctx = RTA_DATA(u_arg);
263 return security_xfrm_state_alloc(x, uctx);
266 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
268 memcpy(&x->id, &p->id, sizeof(x->id));
269 memcpy(&x->sel, &p->sel, sizeof(x->sel));
270 memcpy(&x->lft, &p->lft, sizeof(x->lft));
271 x->props.mode = p->mode;
272 x->props.replay_window = p->replay_window;
273 x->props.reqid = p->reqid;
274 x->props.family = p->family;
275 x->props.saddr = p->saddr;
276 x->props.flags = p->flags;
280 * someday when pfkey also has support, we could have the code
281 * somehow made shareable and move it to xfrm_state.c - JHS
284 static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
287 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
288 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
289 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
290 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
293 struct xfrm_replay_state *replay;
294 if (RTA_PAYLOAD(rp) < sizeof(*replay))
296 replay = RTA_DATA(rp);
297 memcpy(&x->replay, replay, sizeof(*replay));
298 memcpy(&x->preplay, replay, sizeof(*replay));
302 struct xfrm_lifetime_cur *ltime;
303 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
305 ltime = RTA_DATA(lt);
306 x->curlft.bytes = ltime->bytes;
307 x->curlft.packets = ltime->packets;
308 x->curlft.add_time = ltime->add_time;
309 x->curlft.use_time = ltime->use_time;
313 if (RTA_PAYLOAD(et) < sizeof(u32))
315 x->replay_maxage = *(u32*)RTA_DATA(et);
319 if (RTA_PAYLOAD(rt) < sizeof(u32))
321 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
329 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
330 struct rtattr **xfrma,
333 struct xfrm_state *x = xfrm_state_alloc();
339 copy_from_user_state(x, p);
341 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
342 xfrm_aalg_get_byname,
343 xfrma[XFRMA_ALG_AUTH-1])))
345 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
346 xfrm_ealg_get_byname,
347 xfrma[XFRMA_ALG_CRYPT-1])))
349 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
350 xfrm_calg_get_byname,
351 xfrma[XFRMA_ALG_COMP-1])))
353 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
356 err = xfrm_init_state(x);
360 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
364 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
365 /* sysctl_xfrm_aevent_etime is in 100ms units */
366 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
367 x->preplay.bitmap = 0;
368 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
369 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
371 /* override default values from above */
373 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
380 x->km.state = XFRM_STATE_DEAD;
387 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
389 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
390 struct xfrm_state *x;
394 err = verify_newsa_info(p, (struct rtattr **)xfrma);
398 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
403 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
404 err = xfrm_state_add(x);
406 err = xfrm_state_update(x);
409 x->km.state = XFRM_STATE_DEAD;
414 c.seq = nlh->nlmsg_seq;
415 c.pid = nlh->nlmsg_pid;
416 c.event = nlh->nlmsg_type;
418 km_state_notify(x, &c);
424 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
426 struct xfrm_state *x;
429 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
431 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
435 if (xfrm_state_kern(x)) {
440 err = xfrm_state_delete(x);
446 c.seq = nlh->nlmsg_seq;
447 c.pid = nlh->nlmsg_pid;
448 c.event = nlh->nlmsg_type;
449 km_state_notify(x, &c);
455 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
457 memcpy(&p->id, &x->id, sizeof(p->id));
458 memcpy(&p->sel, &x->sel, sizeof(p->sel));
459 memcpy(&p->lft, &x->lft, sizeof(p->lft));
460 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
461 memcpy(&p->stats, &x->stats, sizeof(p->stats));
462 p->saddr = x->props.saddr;
463 p->mode = x->props.mode;
464 p->replay_window = x->props.replay_window;
465 p->reqid = x->props.reqid;
466 p->family = x->props.family;
467 p->flags = x->props.flags;
471 struct xfrm_dump_info {
472 struct sk_buff *in_skb;
473 struct sk_buff *out_skb;
480 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
482 struct xfrm_dump_info *sp = ptr;
483 struct sk_buff *in_skb = sp->in_skb;
484 struct sk_buff *skb = sp->out_skb;
485 struct xfrm_usersa_info *p;
486 struct nlmsghdr *nlh;
487 unsigned char *b = skb->tail;
489 if (sp->this_idx < sp->start_idx)
492 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
494 XFRM_MSG_NEWSA, sizeof(*p));
495 nlh->nlmsg_flags = sp->nlmsg_flags;
498 copy_to_user_state(x, p);
501 RTA_PUT(skb, XFRMA_ALG_AUTH,
502 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
504 RTA_PUT(skb, XFRMA_ALG_CRYPT,
505 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
507 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
510 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
513 int ctx_size = sizeof(struct xfrm_sec_ctx) +
514 x->security->ctx_len;
515 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
516 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
518 uctx->exttype = XFRMA_SEC_CTX;
519 uctx->len = ctx_size;
520 uctx->ctx_doi = x->security->ctx_doi;
521 uctx->ctx_alg = x->security->ctx_alg;
522 uctx->ctx_len = x->security->ctx_len;
523 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
525 nlh->nlmsg_len = skb->tail - b;
532 skb_trim(skb, b - skb->data);
536 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
538 struct xfrm_dump_info info;
540 info.in_skb = cb->skb;
542 info.nlmsg_seq = cb->nlh->nlmsg_seq;
543 info.nlmsg_flags = NLM_F_MULTI;
545 info.start_idx = cb->args[0];
546 (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
547 cb->args[0] = info.this_idx;
552 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
553 struct xfrm_state *x, u32 seq)
555 struct xfrm_dump_info info;
558 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
560 return ERR_PTR(-ENOMEM);
562 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
563 info.in_skb = in_skb;
565 info.nlmsg_seq = seq;
566 info.nlmsg_flags = 0;
567 info.this_idx = info.start_idx = 0;
569 if (dump_one_state(x, 0, &info)) {
577 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
579 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
580 struct xfrm_state *x;
581 struct sk_buff *resp_skb;
584 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
589 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
590 if (IS_ERR(resp_skb)) {
591 err = PTR_ERR(resp_skb);
593 err = netlink_unicast(xfrm_nl, resp_skb,
594 NETLINK_CB(skb).pid, MSG_DONTWAIT);
601 static int verify_userspi_info(struct xfrm_userspi_info *p)
603 switch (p->info.id.proto) {
609 /* IPCOMP spi is 16-bits. */
610 if (p->max >= 0x10000)
624 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
626 struct xfrm_state *x;
627 struct xfrm_userspi_info *p;
628 struct sk_buff *resp_skb;
629 xfrm_address_t *daddr;
634 err = verify_userspi_info(p);
638 family = p->info.family;
639 daddr = &p->info.id.daddr;
643 x = xfrm_find_acq_byseq(p->info.seq);
644 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
651 x = xfrm_find_acq(p->info.mode, p->info.reqid,
652 p->info.id.proto, daddr,
659 resp_skb = ERR_PTR(-ENOENT);
661 spin_lock_bh(&x->lock);
662 if (x->km.state != XFRM_STATE_DEAD) {
663 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
665 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
667 spin_unlock_bh(&x->lock);
669 if (IS_ERR(resp_skb)) {
670 err = PTR_ERR(resp_skb);
674 err = netlink_unicast(xfrm_nl, resp_skb,
675 NETLINK_CB(skb).pid, MSG_DONTWAIT);
683 static int verify_policy_dir(__u8 dir)
687 case XFRM_POLICY_OUT:
688 case XFRM_POLICY_FWD:
698 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
702 case XFRM_SHARE_SESSION:
703 case XFRM_SHARE_USER:
704 case XFRM_SHARE_UNIQUE:
712 case XFRM_POLICY_ALLOW:
713 case XFRM_POLICY_BLOCK:
720 switch (p->sel.family) {
725 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
728 return -EAFNOSUPPORT;
735 return verify_policy_dir(p->dir);
738 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
740 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
741 struct xfrm_user_sec_ctx *uctx;
747 return security_xfrm_policy_alloc(pol, uctx);
750 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
756 for (i = 0; i < nr; i++, ut++) {
757 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
759 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
760 memcpy(&t->saddr, &ut->saddr,
761 sizeof(xfrm_address_t));
762 t->reqid = ut->reqid;
764 t->share = ut->share;
765 t->optional = ut->optional;
766 t->aalgos = ut->aalgos;
767 t->ealgos = ut->ealgos;
768 t->calgos = ut->calgos;
772 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
774 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
775 struct xfrm_user_tmpl *utmpl;
781 nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
783 if (nr > XFRM_MAX_DEPTH)
786 copy_templates(pol, RTA_DATA(rt), nr);
791 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
793 xp->priority = p->priority;
794 xp->index = p->index;
795 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
796 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
797 xp->action = p->action;
798 xp->flags = p->flags;
799 xp->family = p->sel.family;
800 /* XXX xp->share = p->share; */
803 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
805 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
806 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
807 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
808 p->priority = xp->priority;
809 p->index = xp->index;
810 p->sel.family = xp->family;
812 p->action = xp->action;
813 p->flags = xp->flags;
814 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
817 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
819 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
827 copy_from_user_policy(xp, p);
829 if (!(err = copy_from_user_tmpl(xp, xfrma)))
830 err = copy_from_user_sec_ctx(xp, xfrma);
841 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
843 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
844 struct xfrm_policy *xp;
849 err = verify_newpolicy_info(p);
852 err = verify_sec_ctx_len((struct rtattr **)xfrma);
856 xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
860 /* shouldnt excl be based on nlh flags??
861 * Aha! this is anti-netlink really i.e more pfkey derived
862 * in netlink excl is a flag and you wouldnt need
863 * a type XFRM_MSG_UPDPOLICY - JHS */
864 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
865 err = xfrm_policy_insert(p->dir, xp, excl);
867 security_xfrm_policy_free(xp);
872 c.event = nlh->nlmsg_type;
873 c.seq = nlh->nlmsg_seq;
874 c.pid = nlh->nlmsg_pid;
875 km_policy_notify(xp, p->dir, &c);
882 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
884 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
887 if (xp->xfrm_nr == 0)
890 for (i = 0; i < xp->xfrm_nr; i++) {
891 struct xfrm_user_tmpl *up = &vec[i];
892 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
894 memcpy(&up->id, &kp->id, sizeof(up->id));
895 up->family = xp->family;
896 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
897 up->reqid = kp->reqid;
899 up->share = kp->share;
900 up->optional = kp->optional;
901 up->aalgos = kp->aalgos;
902 up->ealgos = kp->ealgos;
903 up->calgos = kp->calgos;
905 RTA_PUT(skb, XFRMA_TMPL,
906 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
915 static int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
918 int ctx_size = sizeof(struct xfrm_sec_ctx) +
919 xp->security->ctx_len;
920 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
921 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
923 uctx->exttype = XFRMA_SEC_CTX;
924 uctx->len = ctx_size;
925 uctx->ctx_doi = xp->security->ctx_doi;
926 uctx->ctx_alg = xp->security->ctx_alg;
927 uctx->ctx_len = xp->security->ctx_len;
928 memcpy(uctx + 1, xp->security->ctx_str, xp->security->ctx_len);
936 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
938 struct xfrm_dump_info *sp = ptr;
939 struct xfrm_userpolicy_info *p;
940 struct sk_buff *in_skb = sp->in_skb;
941 struct sk_buff *skb = sp->out_skb;
942 struct nlmsghdr *nlh;
943 unsigned char *b = skb->tail;
945 if (sp->this_idx < sp->start_idx)
948 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
950 XFRM_MSG_NEWPOLICY, sizeof(*p));
952 nlh->nlmsg_flags = sp->nlmsg_flags;
954 copy_to_user_policy(xp, p, dir);
955 if (copy_to_user_tmpl(xp, skb) < 0)
957 if (copy_to_user_sec_ctx(xp, skb))
960 nlh->nlmsg_len = skb->tail - b;
966 skb_trim(skb, b - skb->data);
970 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
972 struct xfrm_dump_info info;
974 info.in_skb = cb->skb;
976 info.nlmsg_seq = cb->nlh->nlmsg_seq;
977 info.nlmsg_flags = NLM_F_MULTI;
979 info.start_idx = cb->args[0];
980 (void) xfrm_policy_walk(dump_one_policy, &info);
981 cb->args[0] = info.this_idx;
986 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
987 struct xfrm_policy *xp,
990 struct xfrm_dump_info info;
993 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
995 return ERR_PTR(-ENOMEM);
997 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
998 info.in_skb = in_skb;
1000 info.nlmsg_seq = seq;
1001 info.nlmsg_flags = 0;
1002 info.this_idx = info.start_idx = 0;
1004 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1012 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1014 struct xfrm_policy *xp;
1015 struct xfrm_userpolicy_id *p;
1020 p = NLMSG_DATA(nlh);
1021 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1023 err = verify_policy_dir(p->dir);
1028 xp = xfrm_policy_byid(p->dir, p->index, delete);
1030 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1031 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1032 struct xfrm_policy tmp;
1034 err = verify_sec_ctx_len(rtattrs);
1038 memset(&tmp, 0, sizeof(struct xfrm_policy));
1040 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1042 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1045 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, delete);
1046 security_xfrm_policy_free(&tmp);
1052 struct sk_buff *resp_skb;
1054 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1055 if (IS_ERR(resp_skb)) {
1056 err = PTR_ERR(resp_skb);
1058 err = netlink_unicast(xfrm_nl, resp_skb,
1059 NETLINK_CB(skb).pid,
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);
1075 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1078 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1080 xfrm_state_flush(p->proto);
1081 c.data.proto = p->proto;
1082 c.event = nlh->nlmsg_type;
1083 c.seq = nlh->nlmsg_seq;
1084 c.pid = nlh->nlmsg_pid;
1085 km_state_notify(NULL, &c);
1091 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1093 struct xfrm_aevent_id *id;
1094 struct nlmsghdr *nlh;
1095 struct xfrm_lifetime_cur ltime;
1096 unsigned char *b = skb->tail;
1098 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1099 id = NLMSG_DATA(nlh);
1100 nlh->nlmsg_flags = 0;
1102 id->sa_id.daddr = x->id.daddr;
1103 id->sa_id.spi = x->id.spi;
1104 id->sa_id.family = x->props.family;
1105 id->sa_id.proto = x->id.proto;
1106 id->flags = c->data.aevent;
1108 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1110 ltime.bytes = x->curlft.bytes;
1111 ltime.packets = x->curlft.packets;
1112 ltime.add_time = x->curlft.add_time;
1113 ltime.use_time = x->curlft.use_time;
1115 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), <ime);
1117 if (id->flags&XFRM_AE_RTHR) {
1118 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1121 if (id->flags&XFRM_AE_ETHR) {
1122 u32 etimer = x->replay_maxage*10/HZ;
1123 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1126 nlh->nlmsg_len = skb->tail - b;
1131 skb_trim(skb, b - skb->data);
1135 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1137 struct xfrm_state *x;
1138 struct sk_buff *r_skb;
1141 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1142 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1143 struct xfrm_usersa_id *id = &p->sa_id;
1145 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1146 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1148 if (p->flags&XFRM_AE_RTHR)
1149 len+=RTA_SPACE(sizeof(u32));
1151 if (p->flags&XFRM_AE_ETHR)
1152 len+=RTA_SPACE(sizeof(u32));
1154 r_skb = alloc_skb(len, GFP_ATOMIC);
1158 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1165 * XXX: is this lock really needed - none of the other
1166 * gets lock (the concern is things getting updated
1167 * while we are still reading) - jhs
1169 spin_lock_bh(&x->lock);
1170 c.data.aevent = p->flags;
1171 c.seq = nlh->nlmsg_seq;
1172 c.pid = nlh->nlmsg_pid;
1174 if (build_aevent(r_skb, x, &c) < 0)
1176 err = netlink_unicast(xfrm_nl, r_skb,
1177 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1178 spin_unlock_bh(&x->lock);
1183 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1185 struct xfrm_state *x;
1188 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1189 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1190 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1195 /* pedantic mode - thou shalt sayeth replaceth */
1196 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1199 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1203 if (x->km.state != XFRM_STATE_VALID)
1206 spin_lock_bh(&x->lock);
1207 err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
1208 spin_unlock_bh(&x->lock);
1212 c.event = nlh->nlmsg_type;
1213 c.seq = nlh->nlmsg_seq;
1214 c.pid = nlh->nlmsg_pid;
1215 c.data.aevent = XFRM_AE_CU;
1216 km_state_notify(x, &c);
1223 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1227 xfrm_policy_flush();
1228 c.event = nlh->nlmsg_type;
1229 c.seq = nlh->nlmsg_seq;
1230 c.pid = nlh->nlmsg_pid;
1231 km_policy_notify(NULL, 0, &c);
1236 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1238 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1239 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1240 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1241 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1242 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1243 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1244 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1245 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1246 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1247 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1248 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1249 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
1250 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1251 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1256 static struct xfrm_link {
1257 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1258 int (*dump)(struct sk_buff *, struct netlink_callback *);
1259 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1260 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1261 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1262 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1263 .dump = xfrm_dump_sa },
1264 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1265 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1266 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1267 .dump = xfrm_dump_policy },
1268 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1269 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1270 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1271 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1272 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
1273 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1274 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1277 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1279 struct rtattr *xfrma[XFRMA_MAX];
1280 struct xfrm_link *link;
1283 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1286 type = nlh->nlmsg_type;
1288 /* A control message: ignore them */
1289 if (type < XFRM_MSG_BASE)
1292 /* Unknown message: reply with EINVAL */
1293 if (type > XFRM_MSG_MAX)
1296 type -= XFRM_MSG_BASE;
1297 link = &xfrm_dispatch[type];
1299 /* All operations require privileges, even GET */
1300 if (security_netlink_recv(skb)) {
1305 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1306 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1307 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1308 if (link->dump == NULL)
1311 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
1312 link->dump, NULL)) != 0) {
1316 netlink_queue_skip(nlh, skb);
1320 memset(xfrma, 0, sizeof(xfrma));
1322 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1325 if (nlh->nlmsg_len > min_len) {
1326 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1327 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1329 while (RTA_OK(attr, attrlen)) {
1330 unsigned short flavor = attr->rta_type;
1332 if (flavor > XFRMA_MAX)
1334 xfrma[flavor - 1] = attr;
1336 attr = RTA_NEXT(attr, attrlen);
1340 if (link->doit == NULL)
1342 *errp = link->doit(skb, nlh, (void **) &xfrma);
1351 static void xfrm_netlink_rcv(struct sock *sk, int len)
1353 unsigned int qlen = 0;
1356 down(&xfrm_cfg_sem);
1357 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
1363 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1365 struct xfrm_user_expire *ue;
1366 struct nlmsghdr *nlh;
1367 unsigned char *b = skb->tail;
1369 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
1371 ue = NLMSG_DATA(nlh);
1372 nlh->nlmsg_flags = 0;
1374 copy_to_user_state(x, &ue->state);
1375 ue->hard = (c->data.hard != 0) ? 1 : 0;
1377 nlh->nlmsg_len = skb->tail - b;
1381 skb_trim(skb, b - skb->data);
1385 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1387 struct sk_buff *skb;
1388 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
1390 skb = alloc_skb(len, GFP_ATOMIC);
1394 if (build_expire(skb, x, c) < 0)
1397 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1398 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1401 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1403 struct sk_buff *skb;
1404 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1406 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1407 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1408 skb = alloc_skb(len, GFP_ATOMIC);
1412 if (build_aevent(skb, x, c) < 0)
1415 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1416 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1419 static int xfrm_notify_sa_flush(struct km_event *c)
1421 struct xfrm_usersa_flush *p;
1422 struct nlmsghdr *nlh;
1423 struct sk_buff *skb;
1425 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1427 skb = alloc_skb(len, GFP_ATOMIC);
1432 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1433 XFRM_MSG_FLUSHSA, sizeof(*p));
1434 nlh->nlmsg_flags = 0;
1436 p = NLMSG_DATA(nlh);
1437 p->proto = c->data.proto;
1439 nlh->nlmsg_len = skb->tail - b;
1441 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1442 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1449 static int inline xfrm_sa_len(struct xfrm_state *x)
1453 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1455 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1457 l += RTA_SPACE(sizeof(*x->calg));
1459 l += RTA_SPACE(sizeof(*x->encap));
1464 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1466 struct xfrm_usersa_info *p;
1467 struct xfrm_usersa_id *id;
1468 struct nlmsghdr *nlh;
1469 struct sk_buff *skb;
1471 int len = xfrm_sa_len(x);
1474 headlen = sizeof(*p);
1475 if (c->event == XFRM_MSG_DELSA) {
1476 len += RTA_SPACE(headlen);
1477 headlen = sizeof(*id);
1479 len += NLMSG_SPACE(headlen);
1481 skb = alloc_skb(len, GFP_ATOMIC);
1486 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
1487 nlh->nlmsg_flags = 0;
1489 p = NLMSG_DATA(nlh);
1490 if (c->event == XFRM_MSG_DELSA) {
1491 id = NLMSG_DATA(nlh);
1492 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1493 id->spi = x->id.spi;
1494 id->family = x->props.family;
1495 id->proto = x->id.proto;
1497 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1500 copy_to_user_state(x, p);
1503 RTA_PUT(skb, XFRMA_ALG_AUTH,
1504 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1506 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1507 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1509 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1512 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1514 nlh->nlmsg_len = skb->tail - b;
1516 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1517 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1525 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1529 case XFRM_MSG_EXPIRE:
1530 return xfrm_exp_state_notify(x, c);
1531 case XFRM_MSG_NEWAE:
1532 return xfrm_aevent_state_notify(x, c);
1533 case XFRM_MSG_DELSA:
1534 case XFRM_MSG_UPDSA:
1535 case XFRM_MSG_NEWSA:
1536 return xfrm_notify_sa(x, c);
1537 case XFRM_MSG_FLUSHSA:
1538 return xfrm_notify_sa_flush(c);
1540 printk("xfrm_user: Unknown SA event %d\n", c->event);
1548 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1549 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1552 struct xfrm_user_acquire *ua;
1553 struct nlmsghdr *nlh;
1554 unsigned char *b = skb->tail;
1555 __u32 seq = xfrm_get_acqseq();
1557 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1559 ua = NLMSG_DATA(nlh);
1560 nlh->nlmsg_flags = 0;
1562 memcpy(&ua->id, &x->id, sizeof(ua->id));
1563 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1564 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1565 copy_to_user_policy(xp, &ua->policy, dir);
1566 ua->aalgos = xt->aalgos;
1567 ua->ealgos = xt->ealgos;
1568 ua->calgos = xt->calgos;
1569 ua->seq = x->km.seq = seq;
1571 if (copy_to_user_tmpl(xp, skb) < 0)
1573 if (copy_to_user_sec_ctx(xp, skb))
1576 nlh->nlmsg_len = skb->tail - b;
1580 skb_trim(skb, b - skb->data);
1584 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1585 struct xfrm_policy *xp, int dir)
1587 struct sk_buff *skb;
1590 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1591 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
1592 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1593 skb = alloc_skb(len, GFP_ATOMIC);
1597 if (build_acquire(skb, x, xt, xp, dir) < 0)
1600 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
1601 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
1604 /* User gives us xfrm_user_policy_info followed by an array of 0
1605 * or more templates.
1607 static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
1608 u8 *data, int len, int *dir)
1610 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1611 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1612 struct xfrm_policy *xp;
1617 if (opt != IP_XFRM_POLICY) {
1622 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1624 if (opt != IPV6_XFRM_POLICY) {
1637 if (len < sizeof(*p) ||
1638 verify_newpolicy_info(p))
1641 nr = ((len - sizeof(*p)) / sizeof(*ut));
1642 if (nr > XFRM_MAX_DEPTH)
1645 if (p->dir > XFRM_POLICY_OUT)
1648 xp = xfrm_policy_alloc(GFP_KERNEL);
1654 copy_from_user_policy(xp, p);
1655 copy_templates(xp, ut, nr);
1662 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
1663 int dir, struct km_event *c)
1665 struct xfrm_user_polexpire *upe;
1666 struct nlmsghdr *nlh;
1667 int hard = c->data.hard;
1668 unsigned char *b = skb->tail;
1670 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
1671 upe = NLMSG_DATA(nlh);
1672 nlh->nlmsg_flags = 0;
1674 copy_to_user_policy(xp, &upe->pol, dir);
1675 if (copy_to_user_tmpl(xp, skb) < 0)
1677 if (copy_to_user_sec_ctx(xp, skb))
1681 nlh->nlmsg_len = skb->tail - b;
1685 skb_trim(skb, b - skb->data);
1689 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1691 struct sk_buff *skb;
1694 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1695 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
1696 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1697 skb = alloc_skb(len, GFP_ATOMIC);
1701 if (build_polexpire(skb, xp, dir, c) < 0)
1704 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1705 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1708 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
1710 struct xfrm_userpolicy_info *p;
1711 struct xfrm_userpolicy_id *id;
1712 struct nlmsghdr *nlh;
1713 struct sk_buff *skb;
1715 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1718 headlen = sizeof(*p);
1719 if (c->event == XFRM_MSG_DELPOLICY) {
1720 len += RTA_SPACE(headlen);
1721 headlen = sizeof(*id);
1723 len += NLMSG_SPACE(headlen);
1725 skb = alloc_skb(len, GFP_ATOMIC);
1730 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
1732 p = NLMSG_DATA(nlh);
1733 if (c->event == XFRM_MSG_DELPOLICY) {
1734 id = NLMSG_DATA(nlh);
1735 memset(id, 0, sizeof(*id));
1738 id->index = xp->index;
1740 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
1742 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
1745 nlh->nlmsg_flags = 0;
1747 copy_to_user_policy(xp, p, dir);
1748 if (copy_to_user_tmpl(xp, skb) < 0)
1751 nlh->nlmsg_len = skb->tail - b;
1753 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1754 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
1762 static int xfrm_notify_policy_flush(struct km_event *c)
1764 struct nlmsghdr *nlh;
1765 struct sk_buff *skb;
1767 int len = NLMSG_LENGTH(0);
1769 skb = alloc_skb(len, GFP_ATOMIC);
1775 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
1777 nlh->nlmsg_len = skb->tail - b;
1779 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1780 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
1787 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1791 case XFRM_MSG_NEWPOLICY:
1792 case XFRM_MSG_UPDPOLICY:
1793 case XFRM_MSG_DELPOLICY:
1794 return xfrm_notify_policy(xp, dir, c);
1795 case XFRM_MSG_FLUSHPOLICY:
1796 return xfrm_notify_policy_flush(c);
1797 case XFRM_MSG_POLEXPIRE:
1798 return xfrm_exp_policy_notify(xp, dir, c);
1800 printk("xfrm_user: Unknown Policy event %d\n", c->event);
1807 static struct xfrm_mgr netlink_mgr = {
1809 .notify = xfrm_send_state_notify,
1810 .acquire = xfrm_send_acquire,
1811 .compile_policy = xfrm_compile_policy,
1812 .notify_policy = xfrm_send_policy_notify,
1815 static int __init xfrm_user_init(void)
1817 printk(KERN_INFO "Initializing IPsec netlink socket\n");
1819 xfrm_nl = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
1820 xfrm_netlink_rcv, THIS_MODULE);
1821 if (xfrm_nl == NULL)
1824 xfrm_register_km(&netlink_mgr);
1829 static void __exit xfrm_user_exit(void)
1831 xfrm_unregister_km(&netlink_mgr);
1832 sock_release(xfrm_nl->sk_socket);
1835 module_init(xfrm_user_init);
1836 module_exit(xfrm_user_exit);
1837 MODULE_LICENSE("GPL");
1838 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
1839 EXPORT_SYMBOL(xfrm_nl);