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>
31 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
32 #include <linux/in6.h>
35 static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
37 struct rtattr *rt = xfrma[type - 1];
38 struct xfrm_algo *algp;
44 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
50 len -= (algp->alg_key_len + 7U) / 8;
56 if (!algp->alg_key_len &&
57 strcmp(algp->alg_name, "digest_null") != 0)
62 if (!algp->alg_key_len &&
63 strcmp(algp->alg_name, "cipher_null") != 0)
68 /* Zero length keys are legal. */
75 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
79 static int verify_encap_tmpl(struct rtattr **xfrma)
81 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
82 struct xfrm_encap_tmpl *encap;
87 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
93 static int verify_one_addr(struct rtattr **xfrma, enum xfrm_attr_type_t type,
94 xfrm_address_t **addrp)
96 struct rtattr *rt = xfrma[type - 1];
101 if ((rt->rta_len - sizeof(*rt)) < sizeof(**addrp))
105 *addrp = RTA_DATA(rt);
110 static inline int verify_sec_ctx_len(struct rtattr **xfrma)
112 struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
113 struct xfrm_user_sec_ctx *uctx;
119 if (rt->rta_len < sizeof(*uctx))
124 len += sizeof(struct xfrm_user_sec_ctx);
125 len += uctx->ctx_len;
127 if (uctx->len != len)
134 static int verify_newsa_info(struct xfrm_usersa_info *p,
135 struct rtattr **xfrma)
145 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
157 switch (p->id.proto) {
159 if (!xfrma[XFRMA_ALG_AUTH-1] ||
160 xfrma[XFRMA_ALG_CRYPT-1] ||
161 xfrma[XFRMA_ALG_COMP-1])
166 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
167 !xfrma[XFRMA_ALG_CRYPT-1]) ||
168 xfrma[XFRMA_ALG_COMP-1])
173 if (!xfrma[XFRMA_ALG_COMP-1] ||
174 xfrma[XFRMA_ALG_AUTH-1] ||
175 xfrma[XFRMA_ALG_CRYPT-1])
179 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
180 case IPPROTO_DSTOPTS:
181 case IPPROTO_ROUTING:
182 if (xfrma[XFRMA_ALG_COMP-1] ||
183 xfrma[XFRMA_ALG_AUTH-1] ||
184 xfrma[XFRMA_ALG_CRYPT-1] ||
185 xfrma[XFRMA_ENCAP-1] ||
186 xfrma[XFRMA_SEC_CTX-1] ||
187 !xfrma[XFRMA_COADDR-1])
196 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
198 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
200 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
202 if ((err = verify_encap_tmpl(xfrma)))
204 if ((err = verify_sec_ctx_len(xfrma)))
206 if ((err = verify_one_addr(xfrma, XFRMA_COADDR, NULL)))
211 case XFRM_MODE_TRANSPORT:
212 case XFRM_MODE_TUNNEL:
213 case XFRM_MODE_ROUTEOPTIMIZATION:
227 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
228 struct xfrm_algo_desc *(*get_byname)(char *, int),
229 struct rtattr *u_arg)
231 struct rtattr *rta = u_arg;
232 struct xfrm_algo *p, *ualg;
233 struct xfrm_algo_desc *algo;
239 ualg = RTA_DATA(rta);
241 algo = get_byname(ualg->alg_name, 1);
244 *props = algo->desc.sadb_alg_id;
246 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
247 p = kmemdup(ualg, len, GFP_KERNEL);
251 strcpy(p->alg_name, algo->name);
256 static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
258 struct rtattr *rta = u_arg;
259 struct xfrm_encap_tmpl *p, *uencap;
264 uencap = RTA_DATA(rta);
265 p = kmemdup(uencap, sizeof(*p), GFP_KERNEL);
274 static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
276 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
280 len += sizeof(struct xfrm_user_sec_ctx);
281 len += xfrm_ctx->ctx_len;
286 static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
288 struct xfrm_user_sec_ctx *uctx;
293 uctx = RTA_DATA(u_arg);
294 return security_xfrm_state_alloc(x, uctx);
297 static int attach_one_addr(xfrm_address_t **addrpp, struct rtattr *u_arg)
299 struct rtattr *rta = u_arg;
300 xfrm_address_t *p, *uaddrp;
305 uaddrp = RTA_DATA(rta);
306 p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
314 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
316 memcpy(&x->id, &p->id, sizeof(x->id));
317 memcpy(&x->sel, &p->sel, sizeof(x->sel));
318 memcpy(&x->lft, &p->lft, sizeof(x->lft));
319 x->props.mode = p->mode;
320 x->props.replay_window = p->replay_window;
321 x->props.reqid = p->reqid;
322 x->props.family = p->family;
323 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
324 x->props.flags = p->flags;
328 * someday when pfkey also has support, we could have the code
329 * somehow made shareable and move it to xfrm_state.c - JHS
332 static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
335 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
336 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
337 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
338 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
341 struct xfrm_replay_state *replay;
342 if (RTA_PAYLOAD(rp) < sizeof(*replay))
344 replay = RTA_DATA(rp);
345 memcpy(&x->replay, replay, sizeof(*replay));
346 memcpy(&x->preplay, replay, sizeof(*replay));
350 struct xfrm_lifetime_cur *ltime;
351 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
353 ltime = RTA_DATA(lt);
354 x->curlft.bytes = ltime->bytes;
355 x->curlft.packets = ltime->packets;
356 x->curlft.add_time = ltime->add_time;
357 x->curlft.use_time = ltime->use_time;
361 if (RTA_PAYLOAD(et) < sizeof(u32))
363 x->replay_maxage = *(u32*)RTA_DATA(et);
367 if (RTA_PAYLOAD(rt) < sizeof(u32))
369 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
377 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
378 struct rtattr **xfrma,
381 struct xfrm_state *x = xfrm_state_alloc();
387 copy_from_user_state(x, p);
389 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
390 xfrm_aalg_get_byname,
391 xfrma[XFRMA_ALG_AUTH-1])))
393 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
394 xfrm_ealg_get_byname,
395 xfrma[XFRMA_ALG_CRYPT-1])))
397 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
398 xfrm_calg_get_byname,
399 xfrma[XFRMA_ALG_COMP-1])))
401 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
403 if ((err = attach_one_addr(&x->coaddr, xfrma[XFRMA_COADDR-1])))
405 err = xfrm_init_state(x);
409 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
413 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
414 /* sysctl_xfrm_aevent_etime is in 100ms units */
415 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
416 x->preplay.bitmap = 0;
417 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
418 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
420 /* override default values from above */
422 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
429 x->km.state = XFRM_STATE_DEAD;
436 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
438 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
439 struct xfrm_state *x;
443 err = verify_newsa_info(p, (struct rtattr **)xfrma);
447 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
452 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
453 err = xfrm_state_add(x);
455 err = xfrm_state_update(x);
458 x->km.state = XFRM_STATE_DEAD;
463 c.seq = nlh->nlmsg_seq;
464 c.pid = nlh->nlmsg_pid;
465 c.event = nlh->nlmsg_type;
467 km_state_notify(x, &c);
473 static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
474 struct rtattr **xfrma,
477 struct xfrm_state *x = NULL;
480 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
482 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
484 xfrm_address_t *saddr = NULL;
486 err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr);
496 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
506 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
508 struct xfrm_state *x;
511 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
513 x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err);
517 if ((err = security_xfrm_state_delete(x)) != 0)
520 if (xfrm_state_kern(x)) {
525 err = xfrm_state_delete(x);
529 c.seq = nlh->nlmsg_seq;
530 c.pid = nlh->nlmsg_pid;
531 c.event = nlh->nlmsg_type;
532 km_state_notify(x, &c);
539 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
541 memcpy(&p->id, &x->id, sizeof(p->id));
542 memcpy(&p->sel, &x->sel, sizeof(p->sel));
543 memcpy(&p->lft, &x->lft, sizeof(p->lft));
544 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
545 memcpy(&p->stats, &x->stats, sizeof(p->stats));
546 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
547 p->mode = x->props.mode;
548 p->replay_window = x->props.replay_window;
549 p->reqid = x->props.reqid;
550 p->family = x->props.family;
551 p->flags = x->props.flags;
555 struct xfrm_dump_info {
556 struct sk_buff *in_skb;
557 struct sk_buff *out_skb;
564 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
566 struct xfrm_dump_info *sp = ptr;
567 struct sk_buff *in_skb = sp->in_skb;
568 struct sk_buff *skb = sp->out_skb;
569 struct xfrm_usersa_info *p;
570 struct nlmsghdr *nlh;
571 unsigned char *b = skb->tail;
573 if (sp->this_idx < sp->start_idx)
576 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
578 XFRM_MSG_NEWSA, sizeof(*p));
579 nlh->nlmsg_flags = sp->nlmsg_flags;
582 copy_to_user_state(x, p);
585 RTA_PUT(skb, XFRMA_ALG_AUTH,
586 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
588 RTA_PUT(skb, XFRMA_ALG_CRYPT,
589 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
591 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
594 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
597 int ctx_size = sizeof(struct xfrm_sec_ctx) +
598 x->security->ctx_len;
599 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
600 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
602 uctx->exttype = XFRMA_SEC_CTX;
603 uctx->len = ctx_size;
604 uctx->ctx_doi = x->security->ctx_doi;
605 uctx->ctx_alg = x->security->ctx_alg;
606 uctx->ctx_len = x->security->ctx_len;
607 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
611 RTA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
614 RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused);
616 nlh->nlmsg_len = skb->tail - b;
623 skb_trim(skb, b - skb->data);
627 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
629 struct xfrm_dump_info info;
631 info.in_skb = cb->skb;
633 info.nlmsg_seq = cb->nlh->nlmsg_seq;
634 info.nlmsg_flags = NLM_F_MULTI;
636 info.start_idx = cb->args[0];
637 (void) xfrm_state_walk(0, dump_one_state, &info);
638 cb->args[0] = info.this_idx;
643 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
644 struct xfrm_state *x, u32 seq)
646 struct xfrm_dump_info info;
649 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
651 return ERR_PTR(-ENOMEM);
653 info.in_skb = in_skb;
655 info.nlmsg_seq = seq;
656 info.nlmsg_flags = 0;
657 info.this_idx = info.start_idx = 0;
659 if (dump_one_state(x, 0, &info)) {
667 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
669 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
670 struct xfrm_state *x;
671 struct sk_buff *resp_skb;
674 x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err);
678 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
679 if (IS_ERR(resp_skb)) {
680 err = PTR_ERR(resp_skb);
682 err = netlink_unicast(xfrm_nl, resp_skb,
683 NETLINK_CB(skb).pid, MSG_DONTWAIT);
690 static int verify_userspi_info(struct xfrm_userspi_info *p)
692 switch (p->info.id.proto) {
698 /* IPCOMP spi is 16-bits. */
699 if (p->max >= 0x10000)
713 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
715 struct xfrm_state *x;
716 struct xfrm_userspi_info *p;
717 struct sk_buff *resp_skb;
718 xfrm_address_t *daddr;
723 err = verify_userspi_info(p);
727 family = p->info.family;
728 daddr = &p->info.id.daddr;
732 x = xfrm_find_acq_byseq(p->info.seq);
733 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
740 x = xfrm_find_acq(p->info.mode, p->info.reqid,
741 p->info.id.proto, daddr,
748 resp_skb = ERR_PTR(-ENOENT);
750 spin_lock_bh(&x->lock);
751 if (x->km.state != XFRM_STATE_DEAD) {
752 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
754 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
756 spin_unlock_bh(&x->lock);
758 if (IS_ERR(resp_skb)) {
759 err = PTR_ERR(resp_skb);
763 err = netlink_unicast(xfrm_nl, resp_skb,
764 NETLINK_CB(skb).pid, MSG_DONTWAIT);
772 static int verify_policy_dir(u8 dir)
776 case XFRM_POLICY_OUT:
777 case XFRM_POLICY_FWD:
787 static int verify_policy_type(u8 type)
790 case XFRM_POLICY_TYPE_MAIN:
791 #ifdef CONFIG_XFRM_SUB_POLICY
792 case XFRM_POLICY_TYPE_SUB:
803 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
807 case XFRM_SHARE_SESSION:
808 case XFRM_SHARE_USER:
809 case XFRM_SHARE_UNIQUE:
817 case XFRM_POLICY_ALLOW:
818 case XFRM_POLICY_BLOCK:
825 switch (p->sel.family) {
830 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
833 return -EAFNOSUPPORT;
840 return verify_policy_dir(p->dir);
843 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
845 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
846 struct xfrm_user_sec_ctx *uctx;
852 return security_xfrm_policy_alloc(pol, uctx);
855 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
861 xp->family = ut->family;
862 for (i = 0; i < nr; i++, ut++) {
863 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
865 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
866 memcpy(&t->saddr, &ut->saddr,
867 sizeof(xfrm_address_t));
868 t->reqid = ut->reqid;
870 t->share = ut->share;
871 t->optional = ut->optional;
872 t->aalgos = ut->aalgos;
873 t->ealgos = ut->ealgos;
874 t->calgos = ut->calgos;
875 t->encap_family = ut->family;
879 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
881 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
882 struct xfrm_user_tmpl *utmpl;
888 nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
890 if (nr > XFRM_MAX_DEPTH)
893 copy_templates(pol, RTA_DATA(rt), nr);
898 static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
900 struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
901 struct xfrm_userpolicy_type *upt;
902 u8 type = XFRM_POLICY_TYPE_MAIN;
906 if (rt->rta_len < sizeof(*upt))
913 err = verify_policy_type(type);
921 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
923 xp->priority = p->priority;
924 xp->index = p->index;
925 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
926 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
927 xp->action = p->action;
928 xp->flags = p->flags;
929 xp->family = p->sel.family;
930 /* XXX xp->share = p->share; */
933 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
935 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
936 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
937 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
938 p->priority = xp->priority;
939 p->index = xp->index;
940 p->sel.family = xp->family;
942 p->action = xp->action;
943 p->flags = xp->flags;
944 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
947 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
949 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
957 copy_from_user_policy(xp, p);
959 err = copy_from_user_policy_type(&xp->type, xfrma);
963 if (!(err = copy_from_user_tmpl(xp, xfrma)))
964 err = copy_from_user_sec_ctx(xp, xfrma);
975 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
977 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
978 struct xfrm_policy *xp;
983 err = verify_newpolicy_info(p);
986 err = verify_sec_ctx_len((struct rtattr **)xfrma);
990 xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
994 /* shouldnt excl be based on nlh flags??
995 * Aha! this is anti-netlink really i.e more pfkey derived
996 * in netlink excl is a flag and you wouldnt need
997 * a type XFRM_MSG_UPDPOLICY - JHS */
998 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
999 err = xfrm_policy_insert(p->dir, xp, excl);
1001 security_xfrm_policy_free(xp);
1006 c.event = nlh->nlmsg_type;
1007 c.seq = nlh->nlmsg_seq;
1008 c.pid = nlh->nlmsg_pid;
1009 km_policy_notify(xp, p->dir, &c);
1016 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1018 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1021 if (xp->xfrm_nr == 0)
1024 for (i = 0; i < xp->xfrm_nr; i++) {
1025 struct xfrm_user_tmpl *up = &vec[i];
1026 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1028 memcpy(&up->id, &kp->id, sizeof(up->id));
1029 up->family = kp->encap_family;
1030 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1031 up->reqid = kp->reqid;
1032 up->mode = kp->mode;
1033 up->share = kp->share;
1034 up->optional = kp->optional;
1035 up->aalgos = kp->aalgos;
1036 up->ealgos = kp->ealgos;
1037 up->calgos = kp->calgos;
1039 RTA_PUT(skb, XFRMA_TMPL,
1040 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
1049 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
1051 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
1052 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
1053 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1055 uctx->exttype = XFRMA_SEC_CTX;
1056 uctx->len = ctx_size;
1057 uctx->ctx_doi = s->ctx_doi;
1058 uctx->ctx_alg = s->ctx_alg;
1059 uctx->ctx_len = s->ctx_len;
1060 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
1067 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1070 return copy_sec_ctx(x->security, skb);
1075 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1078 return copy_sec_ctx(xp->security, skb);
1083 #ifdef CONFIG_XFRM_SUB_POLICY
1084 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1086 struct xfrm_userpolicy_type upt;
1088 memset(&upt, 0, sizeof(upt));
1091 RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1100 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1106 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1108 struct xfrm_dump_info *sp = ptr;
1109 struct xfrm_userpolicy_info *p;
1110 struct sk_buff *in_skb = sp->in_skb;
1111 struct sk_buff *skb = sp->out_skb;
1112 struct nlmsghdr *nlh;
1113 unsigned char *b = skb->tail;
1115 if (sp->this_idx < sp->start_idx)
1118 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
1120 XFRM_MSG_NEWPOLICY, sizeof(*p));
1121 p = NLMSG_DATA(nlh);
1122 nlh->nlmsg_flags = sp->nlmsg_flags;
1124 copy_to_user_policy(xp, p, dir);
1125 if (copy_to_user_tmpl(xp, skb) < 0)
1127 if (copy_to_user_sec_ctx(xp, skb))
1129 if (copy_to_user_policy_type(xp->type, skb) < 0)
1132 nlh->nlmsg_len = skb->tail - b;
1138 skb_trim(skb, b - skb->data);
1142 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1144 struct xfrm_dump_info info;
1146 info.in_skb = cb->skb;
1148 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1149 info.nlmsg_flags = NLM_F_MULTI;
1151 info.start_idx = cb->args[0];
1152 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1153 #ifdef CONFIG_XFRM_SUB_POLICY
1154 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1156 cb->args[0] = info.this_idx;
1161 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1162 struct xfrm_policy *xp,
1165 struct xfrm_dump_info info;
1166 struct sk_buff *skb;
1168 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1170 return ERR_PTR(-ENOMEM);
1172 info.in_skb = in_skb;
1174 info.nlmsg_seq = seq;
1175 info.nlmsg_flags = 0;
1176 info.this_idx = info.start_idx = 0;
1178 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1186 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1188 struct xfrm_policy *xp;
1189 struct xfrm_userpolicy_id *p;
1190 u8 type = XFRM_POLICY_TYPE_MAIN;
1195 p = NLMSG_DATA(nlh);
1196 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1198 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1202 err = verify_policy_dir(p->dir);
1207 xp = xfrm_policy_byid(type, p->dir, p->index, delete);
1209 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1210 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1211 struct xfrm_policy tmp;
1213 err = verify_sec_ctx_len(rtattrs);
1217 memset(&tmp, 0, sizeof(struct xfrm_policy));
1219 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1221 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1224 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, delete);
1225 security_xfrm_policy_free(&tmp);
1231 struct sk_buff *resp_skb;
1233 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1234 if (IS_ERR(resp_skb)) {
1235 err = PTR_ERR(resp_skb);
1237 err = netlink_unicast(xfrm_nl, resp_skb,
1238 NETLINK_CB(skb).pid,
1242 if ((err = security_xfrm_policy_delete(xp)) != 0)
1244 c.data.byid = p->index;
1245 c.event = nlh->nlmsg_type;
1246 c.seq = nlh->nlmsg_seq;
1247 c.pid = nlh->nlmsg_pid;
1248 km_policy_notify(xp, p->dir, &c);
1257 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1260 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1262 xfrm_state_flush(p->proto);
1263 c.data.proto = p->proto;
1264 c.event = nlh->nlmsg_type;
1265 c.seq = nlh->nlmsg_seq;
1266 c.pid = nlh->nlmsg_pid;
1267 km_state_notify(NULL, &c);
1273 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1275 struct xfrm_aevent_id *id;
1276 struct nlmsghdr *nlh;
1277 struct xfrm_lifetime_cur ltime;
1278 unsigned char *b = skb->tail;
1280 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1281 id = NLMSG_DATA(nlh);
1282 nlh->nlmsg_flags = 0;
1284 id->sa_id.daddr = x->id.daddr;
1285 id->sa_id.spi = x->id.spi;
1286 id->sa_id.family = x->props.family;
1287 id->sa_id.proto = x->id.proto;
1288 id->flags = c->data.aevent;
1290 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1292 ltime.bytes = x->curlft.bytes;
1293 ltime.packets = x->curlft.packets;
1294 ltime.add_time = x->curlft.add_time;
1295 ltime.use_time = x->curlft.use_time;
1297 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), <ime);
1299 if (id->flags&XFRM_AE_RTHR) {
1300 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1303 if (id->flags&XFRM_AE_ETHR) {
1304 u32 etimer = x->replay_maxage*10/HZ;
1305 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1308 nlh->nlmsg_len = skb->tail - b;
1313 skb_trim(skb, b - skb->data);
1317 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1319 struct xfrm_state *x;
1320 struct sk_buff *r_skb;
1323 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1324 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1325 struct xfrm_usersa_id *id = &p->sa_id;
1327 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1328 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1330 if (p->flags&XFRM_AE_RTHR)
1331 len+=RTA_SPACE(sizeof(u32));
1333 if (p->flags&XFRM_AE_ETHR)
1334 len+=RTA_SPACE(sizeof(u32));
1336 r_skb = alloc_skb(len, GFP_ATOMIC);
1340 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1347 * XXX: is this lock really needed - none of the other
1348 * gets lock (the concern is things getting updated
1349 * while we are still reading) - jhs
1351 spin_lock_bh(&x->lock);
1352 c.data.aevent = p->flags;
1353 c.seq = nlh->nlmsg_seq;
1354 c.pid = nlh->nlmsg_pid;
1356 if (build_aevent(r_skb, x, &c) < 0)
1358 err = netlink_unicast(xfrm_nl, r_skb,
1359 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1360 spin_unlock_bh(&x->lock);
1365 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1367 struct xfrm_state *x;
1370 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1371 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1372 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1377 /* pedantic mode - thou shalt sayeth replaceth */
1378 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1381 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1385 if (x->km.state != XFRM_STATE_VALID)
1388 spin_lock_bh(&x->lock);
1389 err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
1390 spin_unlock_bh(&x->lock);
1394 c.event = nlh->nlmsg_type;
1395 c.seq = nlh->nlmsg_seq;
1396 c.pid = nlh->nlmsg_pid;
1397 c.data.aevent = XFRM_AE_CU;
1398 km_state_notify(x, &c);
1405 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1408 u8 type = XFRM_POLICY_TYPE_MAIN;
1411 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1415 xfrm_policy_flush(type);
1417 c.event = nlh->nlmsg_type;
1418 c.seq = nlh->nlmsg_seq;
1419 c.pid = nlh->nlmsg_pid;
1420 km_policy_notify(NULL, 0, &c);
1424 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1426 struct xfrm_policy *xp;
1427 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1428 struct xfrm_userpolicy_info *p = &up->pol;
1429 u8 type = XFRM_POLICY_TYPE_MAIN;
1432 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1437 xp = xfrm_policy_byid(type, p->dir, p->index, 0);
1439 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1440 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1441 struct xfrm_policy tmp;
1443 err = verify_sec_ctx_len(rtattrs);
1447 memset(&tmp, 0, sizeof(struct xfrm_policy));
1449 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1451 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1454 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, 0);
1455 security_xfrm_policy_free(&tmp);
1460 read_lock(&xp->lock);
1462 read_unlock(&xp->lock);
1466 read_unlock(&xp->lock);
1469 xfrm_policy_delete(xp, p->dir);
1471 // reset the timers here?
1472 printk("Dont know what to do with soft policy expire\n");
1474 km_policy_expired(xp, p->dir, up->hard, current->pid);
1481 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1483 struct xfrm_state *x;
1485 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1486 struct xfrm_usersa_info *p = &ue->state;
1488 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1496 spin_lock_bh(&x->lock);
1497 if (x->km.state != XFRM_STATE_VALID)
1499 km_state_expired(x, ue->hard, current->pid);
1502 __xfrm_state_delete(x);
1504 spin_unlock_bh(&x->lock);
1509 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1511 struct xfrm_policy *xp;
1512 struct xfrm_user_tmpl *ut;
1514 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1516 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1517 struct xfrm_state *x = xfrm_state_alloc();
1523 err = verify_newpolicy_info(&ua->policy);
1525 printk("BAD policy passed\n");
1531 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err); if (!xp) {
1536 memcpy(&x->id, &ua->id, sizeof(ua->id));
1537 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1538 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1541 /* extract the templates and for each call km_key */
1542 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1543 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1544 memcpy(&x->id, &t->id, sizeof(x->id));
1545 x->props.mode = t->mode;
1546 x->props.reqid = t->reqid;
1547 x->props.family = ut->family;
1548 t->aalgos = ua->aalgos;
1549 t->ealgos = ua->ealgos;
1550 t->calgos = ua->calgos;
1551 err = km_query(x, t, xp);
1562 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1564 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1565 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1566 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1567 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1568 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1569 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1570 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1571 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1572 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1573 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1574 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1575 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1576 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1577 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1578 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
1579 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1580 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1581 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
1586 static struct xfrm_link {
1587 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1588 int (*dump)(struct sk_buff *, struct netlink_callback *);
1589 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1590 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1591 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1592 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1593 .dump = xfrm_dump_sa },
1594 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1595 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1596 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1597 .dump = xfrm_dump_policy },
1598 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1599 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
1600 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1601 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1602 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1603 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1604 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1605 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
1606 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1607 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1610 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1612 struct rtattr *xfrma[XFRMA_MAX];
1613 struct xfrm_link *link;
1616 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1619 type = nlh->nlmsg_type;
1621 /* A control message: ignore them */
1622 if (type < XFRM_MSG_BASE)
1625 /* Unknown message: reply with EINVAL */
1626 if (type > XFRM_MSG_MAX)
1629 type -= XFRM_MSG_BASE;
1630 link = &xfrm_dispatch[type];
1632 /* All operations require privileges, even GET */
1633 if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
1638 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1639 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1640 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1641 if (link->dump == NULL)
1644 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
1645 link->dump, NULL)) != 0) {
1649 netlink_queue_skip(nlh, skb);
1653 memset(xfrma, 0, sizeof(xfrma));
1655 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1658 if (nlh->nlmsg_len > min_len) {
1659 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1660 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1662 while (RTA_OK(attr, attrlen)) {
1663 unsigned short flavor = attr->rta_type;
1665 if (flavor > XFRMA_MAX)
1667 xfrma[flavor - 1] = attr;
1669 attr = RTA_NEXT(attr, attrlen);
1673 if (link->doit == NULL)
1675 *errp = link->doit(skb, nlh, (void **) &xfrma);
1684 static void xfrm_netlink_rcv(struct sock *sk, int len)
1686 unsigned int qlen = 0;
1689 mutex_lock(&xfrm_cfg_mutex);
1690 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
1691 mutex_unlock(&xfrm_cfg_mutex);
1696 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1698 struct xfrm_user_expire *ue;
1699 struct nlmsghdr *nlh;
1700 unsigned char *b = skb->tail;
1702 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
1704 ue = NLMSG_DATA(nlh);
1705 nlh->nlmsg_flags = 0;
1707 copy_to_user_state(x, &ue->state);
1708 ue->hard = (c->data.hard != 0) ? 1 : 0;
1710 nlh->nlmsg_len = skb->tail - b;
1714 skb_trim(skb, b - skb->data);
1718 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1720 struct sk_buff *skb;
1721 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
1723 skb = alloc_skb(len, GFP_ATOMIC);
1727 if (build_expire(skb, x, c) < 0)
1730 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1731 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1734 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1736 struct sk_buff *skb;
1737 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1739 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1740 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1741 skb = alloc_skb(len, GFP_ATOMIC);
1745 if (build_aevent(skb, x, c) < 0)
1748 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1749 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1752 static int xfrm_notify_sa_flush(struct km_event *c)
1754 struct xfrm_usersa_flush *p;
1755 struct nlmsghdr *nlh;
1756 struct sk_buff *skb;
1758 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1760 skb = alloc_skb(len, GFP_ATOMIC);
1765 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1766 XFRM_MSG_FLUSHSA, sizeof(*p));
1767 nlh->nlmsg_flags = 0;
1769 p = NLMSG_DATA(nlh);
1770 p->proto = c->data.proto;
1772 nlh->nlmsg_len = skb->tail - b;
1774 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1775 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1782 static int inline xfrm_sa_len(struct xfrm_state *x)
1786 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1788 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1790 l += RTA_SPACE(sizeof(*x->calg));
1792 l += RTA_SPACE(sizeof(*x->encap));
1797 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1799 struct xfrm_usersa_info *p;
1800 struct xfrm_usersa_id *id;
1801 struct nlmsghdr *nlh;
1802 struct sk_buff *skb;
1804 int len = xfrm_sa_len(x);
1807 headlen = sizeof(*p);
1808 if (c->event == XFRM_MSG_DELSA) {
1809 len += RTA_SPACE(headlen);
1810 headlen = sizeof(*id);
1812 len += NLMSG_SPACE(headlen);
1814 skb = alloc_skb(len, GFP_ATOMIC);
1819 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
1820 nlh->nlmsg_flags = 0;
1822 p = NLMSG_DATA(nlh);
1823 if (c->event == XFRM_MSG_DELSA) {
1824 id = NLMSG_DATA(nlh);
1825 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1826 id->spi = x->id.spi;
1827 id->family = x->props.family;
1828 id->proto = x->id.proto;
1830 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1833 copy_to_user_state(x, p);
1836 RTA_PUT(skb, XFRMA_ALG_AUTH,
1837 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1839 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1840 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1842 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1845 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1847 nlh->nlmsg_len = skb->tail - b;
1849 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1850 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1858 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1862 case XFRM_MSG_EXPIRE:
1863 return xfrm_exp_state_notify(x, c);
1864 case XFRM_MSG_NEWAE:
1865 return xfrm_aevent_state_notify(x, c);
1866 case XFRM_MSG_DELSA:
1867 case XFRM_MSG_UPDSA:
1868 case XFRM_MSG_NEWSA:
1869 return xfrm_notify_sa(x, c);
1870 case XFRM_MSG_FLUSHSA:
1871 return xfrm_notify_sa_flush(c);
1873 printk("xfrm_user: Unknown SA event %d\n", c->event);
1881 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1882 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1885 struct xfrm_user_acquire *ua;
1886 struct nlmsghdr *nlh;
1887 unsigned char *b = skb->tail;
1888 __u32 seq = xfrm_get_acqseq();
1890 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1892 ua = NLMSG_DATA(nlh);
1893 nlh->nlmsg_flags = 0;
1895 memcpy(&ua->id, &x->id, sizeof(ua->id));
1896 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1897 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1898 copy_to_user_policy(xp, &ua->policy, dir);
1899 ua->aalgos = xt->aalgos;
1900 ua->ealgos = xt->ealgos;
1901 ua->calgos = xt->calgos;
1902 ua->seq = x->km.seq = seq;
1904 if (copy_to_user_tmpl(xp, skb) < 0)
1906 if (copy_to_user_state_sec_ctx(x, skb))
1908 if (copy_to_user_policy_type(xp->type, skb) < 0)
1911 nlh->nlmsg_len = skb->tail - b;
1915 skb_trim(skb, b - skb->data);
1919 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1920 struct xfrm_policy *xp, int dir)
1922 struct sk_buff *skb;
1925 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1926 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
1927 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1928 #ifdef CONFIG_XFRM_SUB_POLICY
1929 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
1931 skb = alloc_skb(len, GFP_ATOMIC);
1935 if (build_acquire(skb, x, xt, xp, dir) < 0)
1938 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
1939 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
1942 /* User gives us xfrm_user_policy_info followed by an array of 0
1943 * or more templates.
1945 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
1946 u8 *data, int len, int *dir)
1948 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1949 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1950 struct xfrm_policy *xp;
1953 switch (sk->sk_family) {
1955 if (opt != IP_XFRM_POLICY) {
1960 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1962 if (opt != IPV6_XFRM_POLICY) {
1975 if (len < sizeof(*p) ||
1976 verify_newpolicy_info(p))
1979 nr = ((len - sizeof(*p)) / sizeof(*ut));
1980 if (nr > XFRM_MAX_DEPTH)
1983 if (p->dir > XFRM_POLICY_OUT)
1986 xp = xfrm_policy_alloc(GFP_KERNEL);
1992 copy_from_user_policy(xp, p);
1993 xp->type = XFRM_POLICY_TYPE_MAIN;
1994 copy_templates(xp, ut, nr);
2001 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2002 int dir, struct km_event *c)
2004 struct xfrm_user_polexpire *upe;
2005 struct nlmsghdr *nlh;
2006 int hard = c->data.hard;
2007 unsigned char *b = skb->tail;
2009 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
2010 upe = NLMSG_DATA(nlh);
2011 nlh->nlmsg_flags = 0;
2013 copy_to_user_policy(xp, &upe->pol, dir);
2014 if (copy_to_user_tmpl(xp, skb) < 0)
2016 if (copy_to_user_sec_ctx(xp, skb))
2018 if (copy_to_user_policy_type(xp->type, skb) < 0)
2022 nlh->nlmsg_len = skb->tail - b;
2026 skb_trim(skb, b - skb->data);
2030 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2032 struct sk_buff *skb;
2035 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2036 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
2037 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
2038 #ifdef CONFIG_XFRM_SUB_POLICY
2039 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2041 skb = alloc_skb(len, GFP_ATOMIC);
2045 if (build_polexpire(skb, xp, dir, c) < 0)
2048 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
2049 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2052 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2054 struct xfrm_userpolicy_info *p;
2055 struct xfrm_userpolicy_id *id;
2056 struct nlmsghdr *nlh;
2057 struct sk_buff *skb;
2059 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2062 headlen = sizeof(*p);
2063 if (c->event == XFRM_MSG_DELPOLICY) {
2064 len += RTA_SPACE(headlen);
2065 headlen = sizeof(*id);
2067 #ifdef CONFIG_XFRM_SUB_POLICY
2068 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2070 len += NLMSG_SPACE(headlen);
2072 skb = alloc_skb(len, GFP_ATOMIC);
2077 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
2079 p = NLMSG_DATA(nlh);
2080 if (c->event == XFRM_MSG_DELPOLICY) {
2081 id = NLMSG_DATA(nlh);
2082 memset(id, 0, sizeof(*id));
2085 id->index = xp->index;
2087 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2089 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
2092 nlh->nlmsg_flags = 0;
2094 copy_to_user_policy(xp, p, dir);
2095 if (copy_to_user_tmpl(xp, skb) < 0)
2097 if (copy_to_user_policy_type(xp->type, skb) < 0)
2100 nlh->nlmsg_len = skb->tail - b;
2102 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2103 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2111 static int xfrm_notify_policy_flush(struct km_event *c)
2113 struct nlmsghdr *nlh;
2114 struct sk_buff *skb;
2117 #ifdef CONFIG_XFRM_SUB_POLICY
2118 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2120 len += NLMSG_LENGTH(0);
2122 skb = alloc_skb(len, GFP_ATOMIC);
2128 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
2129 nlh->nlmsg_flags = 0;
2130 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2133 nlh->nlmsg_len = skb->tail - b;
2135 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2136 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2143 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2147 case XFRM_MSG_NEWPOLICY:
2148 case XFRM_MSG_UPDPOLICY:
2149 case XFRM_MSG_DELPOLICY:
2150 return xfrm_notify_policy(xp, dir, c);
2151 case XFRM_MSG_FLUSHPOLICY:
2152 return xfrm_notify_policy_flush(c);
2153 case XFRM_MSG_POLEXPIRE:
2154 return xfrm_exp_policy_notify(xp, dir, c);
2156 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2163 static int build_report(struct sk_buff *skb, u8 proto,
2164 struct xfrm_selector *sel, xfrm_address_t *addr)
2166 struct xfrm_user_report *ur;
2167 struct nlmsghdr *nlh;
2168 unsigned char *b = skb->tail;
2170 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur));
2171 ur = NLMSG_DATA(nlh);
2172 nlh->nlmsg_flags = 0;
2175 memcpy(&ur->sel, sel, sizeof(ur->sel));
2178 RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2180 nlh->nlmsg_len = skb->tail - b;
2185 skb_trim(skb, b - skb->data);
2189 static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2190 xfrm_address_t *addr)
2192 struct sk_buff *skb;
2195 len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
2196 skb = alloc_skb(len, GFP_ATOMIC);
2200 if (build_report(skb, proto, sel, addr) < 0)
2203 NETLINK_CB(skb).dst_group = XFRMNLGRP_REPORT;
2204 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2207 static struct xfrm_mgr netlink_mgr = {
2209 .notify = xfrm_send_state_notify,
2210 .acquire = xfrm_send_acquire,
2211 .compile_policy = xfrm_compile_policy,
2212 .notify_policy = xfrm_send_policy_notify,
2213 .report = xfrm_send_report,
2216 static int __init xfrm_user_init(void)
2220 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2222 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
2223 xfrm_netlink_rcv, THIS_MODULE);
2226 rcu_assign_pointer(xfrm_nl, nlsk);
2228 xfrm_register_km(&netlink_mgr);
2233 static void __exit xfrm_user_exit(void)
2235 struct sock *nlsk = xfrm_nl;
2237 xfrm_unregister_km(&netlink_mgr);
2238 rcu_assign_pointer(xfrm_nl, NULL);
2240 sock_release(nlsk->sk_socket);
2243 module_init(xfrm_user_init);
2244 module_exit(xfrm_user_exit);
2245 MODULE_LICENSE("GPL");
2246 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);