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>
34 #include <linux/audit.h>
36 static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
38 struct rtattr *rt = xfrma[type - 1];
39 struct xfrm_algo *algp;
45 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
51 len -= (algp->alg_key_len + 7U) / 8;
57 if (!algp->alg_key_len &&
58 strcmp(algp->alg_name, "digest_null") != 0)
63 if (!algp->alg_key_len &&
64 strcmp(algp->alg_name, "cipher_null") != 0)
69 /* Zero length keys are legal. */
76 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
80 static int verify_encap_tmpl(struct rtattr **xfrma)
82 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
83 struct xfrm_encap_tmpl *encap;
88 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
94 static int verify_one_addr(struct rtattr **xfrma, enum xfrm_attr_type_t type,
95 xfrm_address_t **addrp)
97 struct rtattr *rt = xfrma[type - 1];
102 if ((rt->rta_len - sizeof(*rt)) < sizeof(**addrp))
106 *addrp = RTA_DATA(rt);
111 static inline int verify_sec_ctx_len(struct rtattr **xfrma)
113 struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
114 struct xfrm_user_sec_ctx *uctx;
120 if (rt->rta_len < sizeof(*uctx))
125 len += sizeof(struct xfrm_user_sec_ctx);
126 len += uctx->ctx_len;
128 if (uctx->len != len)
135 static int verify_newsa_info(struct xfrm_usersa_info *p,
136 struct rtattr **xfrma)
146 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
158 switch (p->id.proto) {
160 if (!xfrma[XFRMA_ALG_AUTH-1] ||
161 xfrma[XFRMA_ALG_CRYPT-1] ||
162 xfrma[XFRMA_ALG_COMP-1])
167 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
168 !xfrma[XFRMA_ALG_CRYPT-1]) ||
169 xfrma[XFRMA_ALG_COMP-1])
174 if (!xfrma[XFRMA_ALG_COMP-1] ||
175 xfrma[XFRMA_ALG_AUTH-1] ||
176 xfrma[XFRMA_ALG_CRYPT-1])
180 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
181 case IPPROTO_DSTOPTS:
182 case IPPROTO_ROUTING:
183 if (xfrma[XFRMA_ALG_COMP-1] ||
184 xfrma[XFRMA_ALG_AUTH-1] ||
185 xfrma[XFRMA_ALG_CRYPT-1] ||
186 xfrma[XFRMA_ENCAP-1] ||
187 xfrma[XFRMA_SEC_CTX-1] ||
188 !xfrma[XFRMA_COADDR-1])
197 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
199 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
201 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
203 if ((err = verify_encap_tmpl(xfrma)))
205 if ((err = verify_sec_ctx_len(xfrma)))
207 if ((err = verify_one_addr(xfrma, XFRMA_COADDR, NULL)))
212 case XFRM_MODE_TRANSPORT:
213 case XFRM_MODE_TUNNEL:
214 case XFRM_MODE_ROUTEOPTIMIZATION:
228 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
229 struct xfrm_algo_desc *(*get_byname)(char *, int),
230 struct rtattr *u_arg)
232 struct rtattr *rta = u_arg;
233 struct xfrm_algo *p, *ualg;
234 struct xfrm_algo_desc *algo;
240 ualg = RTA_DATA(rta);
242 algo = get_byname(ualg->alg_name, 1);
245 *props = algo->desc.sadb_alg_id;
247 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
248 p = kmemdup(ualg, len, GFP_KERNEL);
252 strcpy(p->alg_name, algo->name);
257 static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
259 struct rtattr *rta = u_arg;
260 struct xfrm_encap_tmpl *p, *uencap;
265 uencap = RTA_DATA(rta);
266 p = kmemdup(uencap, sizeof(*p), GFP_KERNEL);
275 static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
277 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
281 len += sizeof(struct xfrm_user_sec_ctx);
282 len += xfrm_ctx->ctx_len;
287 static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
289 struct xfrm_user_sec_ctx *uctx;
294 uctx = RTA_DATA(u_arg);
295 return security_xfrm_state_alloc(x, uctx);
298 static int attach_one_addr(xfrm_address_t **addrpp, struct rtattr *u_arg)
300 struct rtattr *rta = u_arg;
301 xfrm_address_t *p, *uaddrp;
306 uaddrp = RTA_DATA(rta);
307 p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
315 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
317 memcpy(&x->id, &p->id, sizeof(x->id));
318 memcpy(&x->sel, &p->sel, sizeof(x->sel));
319 memcpy(&x->lft, &p->lft, sizeof(x->lft));
320 x->props.mode = p->mode;
321 x->props.replay_window = p->replay_window;
322 x->props.reqid = p->reqid;
323 x->props.family = p->family;
324 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
325 x->props.flags = p->flags;
329 * someday when pfkey also has support, we could have the code
330 * somehow made shareable and move it to xfrm_state.c - JHS
333 static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
336 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
337 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
338 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
339 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
342 struct xfrm_replay_state *replay;
343 if (RTA_PAYLOAD(rp) < sizeof(*replay))
345 replay = RTA_DATA(rp);
346 memcpy(&x->replay, replay, sizeof(*replay));
347 memcpy(&x->preplay, replay, sizeof(*replay));
351 struct xfrm_lifetime_cur *ltime;
352 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
354 ltime = RTA_DATA(lt);
355 x->curlft.bytes = ltime->bytes;
356 x->curlft.packets = ltime->packets;
357 x->curlft.add_time = ltime->add_time;
358 x->curlft.use_time = ltime->use_time;
362 if (RTA_PAYLOAD(et) < sizeof(u32))
364 x->replay_maxage = *(u32*)RTA_DATA(et);
368 if (RTA_PAYLOAD(rt) < sizeof(u32))
370 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
378 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
379 struct rtattr **xfrma,
382 struct xfrm_state *x = xfrm_state_alloc();
388 copy_from_user_state(x, p);
390 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
391 xfrm_aalg_get_byname,
392 xfrma[XFRMA_ALG_AUTH-1])))
394 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
395 xfrm_ealg_get_byname,
396 xfrma[XFRMA_ALG_CRYPT-1])))
398 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
399 xfrm_calg_get_byname,
400 xfrma[XFRMA_ALG_COMP-1])))
402 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
404 if ((err = attach_one_addr(&x->coaddr, xfrma[XFRMA_COADDR-1])))
406 err = xfrm_init_state(x);
410 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
414 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
415 /* sysctl_xfrm_aevent_etime is in 100ms units */
416 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
417 x->preplay.bitmap = 0;
418 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
419 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
421 /* override default values from above */
423 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
430 x->km.state = XFRM_STATE_DEAD;
437 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
439 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
440 struct xfrm_state *x;
444 err = verify_newsa_info(p, (struct rtattr **)xfrma);
448 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
453 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
454 err = xfrm_state_add(x);
456 err = xfrm_state_update(x);
458 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
459 AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
462 x->km.state = XFRM_STATE_DEAD;
467 c.seq = nlh->nlmsg_seq;
468 c.pid = nlh->nlmsg_pid;
469 c.event = nlh->nlmsg_type;
471 km_state_notify(x, &c);
477 static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
478 struct rtattr **xfrma,
481 struct xfrm_state *x = NULL;
484 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
486 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
488 xfrm_address_t *saddr = NULL;
490 err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr);
500 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
510 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
512 struct xfrm_state *x;
515 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
517 x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err);
521 if ((err = security_xfrm_state_delete(x)) != 0)
524 if (xfrm_state_kern(x)) {
529 err = xfrm_state_delete(x);
531 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
532 AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
537 c.seq = nlh->nlmsg_seq;
538 c.pid = nlh->nlmsg_pid;
539 c.event = nlh->nlmsg_type;
540 km_state_notify(x, &c);
547 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
549 memcpy(&p->id, &x->id, sizeof(p->id));
550 memcpy(&p->sel, &x->sel, sizeof(p->sel));
551 memcpy(&p->lft, &x->lft, sizeof(p->lft));
552 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
553 memcpy(&p->stats, &x->stats, sizeof(p->stats));
554 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
555 p->mode = x->props.mode;
556 p->replay_window = x->props.replay_window;
557 p->reqid = x->props.reqid;
558 p->family = x->props.family;
559 p->flags = x->props.flags;
563 struct xfrm_dump_info {
564 struct sk_buff *in_skb;
565 struct sk_buff *out_skb;
572 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
574 struct xfrm_dump_info *sp = ptr;
575 struct sk_buff *in_skb = sp->in_skb;
576 struct sk_buff *skb = sp->out_skb;
577 struct xfrm_usersa_info *p;
578 struct nlmsghdr *nlh;
579 unsigned char *b = skb->tail;
581 if (sp->this_idx < sp->start_idx)
584 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
586 XFRM_MSG_NEWSA, sizeof(*p));
587 nlh->nlmsg_flags = sp->nlmsg_flags;
590 copy_to_user_state(x, p);
593 RTA_PUT(skb, XFRMA_ALG_AUTH,
594 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
596 RTA_PUT(skb, XFRMA_ALG_CRYPT,
597 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
599 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
602 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
605 int ctx_size = sizeof(struct xfrm_sec_ctx) +
606 x->security->ctx_len;
607 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
608 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
610 uctx->exttype = XFRMA_SEC_CTX;
611 uctx->len = ctx_size;
612 uctx->ctx_doi = x->security->ctx_doi;
613 uctx->ctx_alg = x->security->ctx_alg;
614 uctx->ctx_len = x->security->ctx_len;
615 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
619 RTA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
622 RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused);
624 nlh->nlmsg_len = skb->tail - b;
631 skb_trim(skb, b - skb->data);
635 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
637 struct xfrm_dump_info info;
639 info.in_skb = cb->skb;
641 info.nlmsg_seq = cb->nlh->nlmsg_seq;
642 info.nlmsg_flags = NLM_F_MULTI;
644 info.start_idx = cb->args[0];
645 (void) xfrm_state_walk(0, dump_one_state, &info);
646 cb->args[0] = info.this_idx;
651 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
652 struct xfrm_state *x, u32 seq)
654 struct xfrm_dump_info info;
657 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
659 return ERR_PTR(-ENOMEM);
661 info.in_skb = in_skb;
663 info.nlmsg_seq = seq;
664 info.nlmsg_flags = 0;
665 info.this_idx = info.start_idx = 0;
667 if (dump_one_state(x, 0, &info)) {
675 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
677 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
678 struct xfrm_state *x;
679 struct sk_buff *resp_skb;
682 x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err);
686 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
687 if (IS_ERR(resp_skb)) {
688 err = PTR_ERR(resp_skb);
690 err = netlink_unicast(xfrm_nl, resp_skb,
691 NETLINK_CB(skb).pid, MSG_DONTWAIT);
698 static int verify_userspi_info(struct xfrm_userspi_info *p)
700 switch (p->info.id.proto) {
706 /* IPCOMP spi is 16-bits. */
707 if (p->max >= 0x10000)
721 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
723 struct xfrm_state *x;
724 struct xfrm_userspi_info *p;
725 struct sk_buff *resp_skb;
726 xfrm_address_t *daddr;
731 err = verify_userspi_info(p);
735 family = p->info.family;
736 daddr = &p->info.id.daddr;
740 x = xfrm_find_acq_byseq(p->info.seq);
741 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
748 x = xfrm_find_acq(p->info.mode, p->info.reqid,
749 p->info.id.proto, daddr,
756 resp_skb = ERR_PTR(-ENOENT);
758 spin_lock_bh(&x->lock);
759 if (x->km.state != XFRM_STATE_DEAD) {
760 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
762 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
764 spin_unlock_bh(&x->lock);
766 if (IS_ERR(resp_skb)) {
767 err = PTR_ERR(resp_skb);
771 err = netlink_unicast(xfrm_nl, resp_skb,
772 NETLINK_CB(skb).pid, MSG_DONTWAIT);
780 static int verify_policy_dir(u8 dir)
784 case XFRM_POLICY_OUT:
785 case XFRM_POLICY_FWD:
795 static int verify_policy_type(u8 type)
798 case XFRM_POLICY_TYPE_MAIN:
799 #ifdef CONFIG_XFRM_SUB_POLICY
800 case XFRM_POLICY_TYPE_SUB:
811 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
815 case XFRM_SHARE_SESSION:
816 case XFRM_SHARE_USER:
817 case XFRM_SHARE_UNIQUE:
825 case XFRM_POLICY_ALLOW:
826 case XFRM_POLICY_BLOCK:
833 switch (p->sel.family) {
838 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
841 return -EAFNOSUPPORT;
848 return verify_policy_dir(p->dir);
851 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
853 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
854 struct xfrm_user_sec_ctx *uctx;
860 return security_xfrm_policy_alloc(pol, uctx);
863 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
869 for (i = 0; i < nr; i++, ut++) {
870 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
872 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
873 memcpy(&t->saddr, &ut->saddr,
874 sizeof(xfrm_address_t));
875 t->reqid = ut->reqid;
877 t->share = ut->share;
878 t->optional = ut->optional;
879 t->aalgos = ut->aalgos;
880 t->ealgos = ut->ealgos;
881 t->calgos = ut->calgos;
882 t->encap_family = ut->family;
886 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
890 if (nr > XFRM_MAX_DEPTH)
893 for (i = 0; i < nr; i++) {
894 /* We never validated the ut->family value, so many
895 * applications simply leave it at zero. The check was
896 * never made and ut->family was ignored because all
897 * templates could be assumed to have the same family as
898 * the policy itself. Now that we will have ipv4-in-ipv6
899 * and ipv6-in-ipv4 tunnels, this is no longer true.
902 ut[i].family = family;
904 switch (ut[i].family) {
907 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
919 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
921 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
926 struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
927 int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
930 err = validate_tmpl(nr, utmpl, pol->family);
934 copy_templates(pol, RTA_DATA(rt), nr);
939 static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
941 struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
942 struct xfrm_userpolicy_type *upt;
943 u8 type = XFRM_POLICY_TYPE_MAIN;
947 if (rt->rta_len < sizeof(*upt))
954 err = verify_policy_type(type);
962 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
964 xp->priority = p->priority;
965 xp->index = p->index;
966 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
967 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
968 xp->action = p->action;
969 xp->flags = p->flags;
970 xp->family = p->sel.family;
971 /* XXX xp->share = p->share; */
974 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
976 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
977 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
978 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
979 p->priority = xp->priority;
980 p->index = xp->index;
981 p->sel.family = xp->family;
983 p->action = xp->action;
984 p->flags = xp->flags;
985 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
988 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
990 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
998 copy_from_user_policy(xp, p);
1000 err = copy_from_user_policy_type(&xp->type, xfrma);
1004 if (!(err = copy_from_user_tmpl(xp, xfrma)))
1005 err = copy_from_user_sec_ctx(xp, xfrma);
1016 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1018 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
1019 struct xfrm_policy *xp;
1024 err = verify_newpolicy_info(p);
1027 err = verify_sec_ctx_len((struct rtattr **)xfrma);
1031 xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
1035 /* shouldnt excl be based on nlh flags??
1036 * Aha! this is anti-netlink really i.e more pfkey derived
1037 * in netlink excl is a flag and you wouldnt need
1038 * a type XFRM_MSG_UPDPOLICY - JHS */
1039 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1040 err = xfrm_policy_insert(p->dir, xp, excl);
1041 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1042 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1045 security_xfrm_policy_free(xp);
1050 c.event = nlh->nlmsg_type;
1051 c.seq = nlh->nlmsg_seq;
1052 c.pid = nlh->nlmsg_pid;
1053 km_policy_notify(xp, p->dir, &c);
1060 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1062 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1065 if (xp->xfrm_nr == 0)
1068 for (i = 0; i < xp->xfrm_nr; i++) {
1069 struct xfrm_user_tmpl *up = &vec[i];
1070 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1072 memcpy(&up->id, &kp->id, sizeof(up->id));
1073 up->family = kp->encap_family;
1074 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1075 up->reqid = kp->reqid;
1076 up->mode = kp->mode;
1077 up->share = kp->share;
1078 up->optional = kp->optional;
1079 up->aalgos = kp->aalgos;
1080 up->ealgos = kp->ealgos;
1081 up->calgos = kp->calgos;
1083 RTA_PUT(skb, XFRMA_TMPL,
1084 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
1093 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
1095 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
1096 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
1097 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1099 uctx->exttype = XFRMA_SEC_CTX;
1100 uctx->len = ctx_size;
1101 uctx->ctx_doi = s->ctx_doi;
1102 uctx->ctx_alg = s->ctx_alg;
1103 uctx->ctx_len = s->ctx_len;
1104 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
1111 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1114 return copy_sec_ctx(x->security, skb);
1119 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1122 return copy_sec_ctx(xp->security, skb);
1127 #ifdef CONFIG_XFRM_SUB_POLICY
1128 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1130 struct xfrm_userpolicy_type upt;
1132 memset(&upt, 0, sizeof(upt));
1135 RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1144 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1150 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1152 struct xfrm_dump_info *sp = ptr;
1153 struct xfrm_userpolicy_info *p;
1154 struct sk_buff *in_skb = sp->in_skb;
1155 struct sk_buff *skb = sp->out_skb;
1156 struct nlmsghdr *nlh;
1157 unsigned char *b = skb->tail;
1159 if (sp->this_idx < sp->start_idx)
1162 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
1164 XFRM_MSG_NEWPOLICY, sizeof(*p));
1165 p = NLMSG_DATA(nlh);
1166 nlh->nlmsg_flags = sp->nlmsg_flags;
1168 copy_to_user_policy(xp, p, dir);
1169 if (copy_to_user_tmpl(xp, skb) < 0)
1171 if (copy_to_user_sec_ctx(xp, skb))
1173 if (copy_to_user_policy_type(xp->type, skb) < 0)
1176 nlh->nlmsg_len = skb->tail - b;
1182 skb_trim(skb, b - skb->data);
1186 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1188 struct xfrm_dump_info info;
1190 info.in_skb = cb->skb;
1192 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1193 info.nlmsg_flags = NLM_F_MULTI;
1195 info.start_idx = cb->args[0];
1196 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1197 #ifdef CONFIG_XFRM_SUB_POLICY
1198 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1200 cb->args[0] = info.this_idx;
1205 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1206 struct xfrm_policy *xp,
1209 struct xfrm_dump_info info;
1210 struct sk_buff *skb;
1212 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1214 return ERR_PTR(-ENOMEM);
1216 info.in_skb = in_skb;
1218 info.nlmsg_seq = seq;
1219 info.nlmsg_flags = 0;
1220 info.this_idx = info.start_idx = 0;
1222 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1230 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1232 struct xfrm_policy *xp;
1233 struct xfrm_userpolicy_id *p;
1234 u8 type = XFRM_POLICY_TYPE_MAIN;
1239 p = NLMSG_DATA(nlh);
1240 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1242 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1246 err = verify_policy_dir(p->dir);
1251 xp = xfrm_policy_byid(type, p->dir, p->index, delete);
1253 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1254 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1255 struct xfrm_policy tmp;
1257 err = verify_sec_ctx_len(rtattrs);
1261 memset(&tmp, 0, sizeof(struct xfrm_policy));
1263 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1265 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1268 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, delete);
1269 security_xfrm_policy_free(&tmp);
1272 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1273 AUDIT_MAC_IPSEC_DELSPD, (xp) ? 1 : 0, xp, NULL);
1279 struct sk_buff *resp_skb;
1281 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1282 if (IS_ERR(resp_skb)) {
1283 err = PTR_ERR(resp_skb);
1285 err = netlink_unicast(xfrm_nl, resp_skb,
1286 NETLINK_CB(skb).pid,
1290 if ((err = security_xfrm_policy_delete(xp)) != 0)
1292 c.data.byid = p->index;
1293 c.event = nlh->nlmsg_type;
1294 c.seq = nlh->nlmsg_seq;
1295 c.pid = nlh->nlmsg_pid;
1296 km_policy_notify(xp, p->dir, &c);
1305 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1308 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1309 struct xfrm_audit audit_info;
1311 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1312 audit_info.secid = NETLINK_CB(skb).sid;
1313 xfrm_state_flush(p->proto, &audit_info);
1314 c.data.proto = p->proto;
1315 c.event = nlh->nlmsg_type;
1316 c.seq = nlh->nlmsg_seq;
1317 c.pid = nlh->nlmsg_pid;
1318 km_state_notify(NULL, &c);
1324 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1326 struct xfrm_aevent_id *id;
1327 struct nlmsghdr *nlh;
1328 struct xfrm_lifetime_cur ltime;
1329 unsigned char *b = skb->tail;
1331 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1332 id = NLMSG_DATA(nlh);
1333 nlh->nlmsg_flags = 0;
1335 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1336 id->sa_id.spi = x->id.spi;
1337 id->sa_id.family = x->props.family;
1338 id->sa_id.proto = x->id.proto;
1339 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1340 id->reqid = x->props.reqid;
1341 id->flags = c->data.aevent;
1343 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1345 ltime.bytes = x->curlft.bytes;
1346 ltime.packets = x->curlft.packets;
1347 ltime.add_time = x->curlft.add_time;
1348 ltime.use_time = x->curlft.use_time;
1350 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), <ime);
1352 if (id->flags&XFRM_AE_RTHR) {
1353 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1356 if (id->flags&XFRM_AE_ETHR) {
1357 u32 etimer = x->replay_maxage*10/HZ;
1358 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1361 nlh->nlmsg_len = skb->tail - b;
1366 skb_trim(skb, b - skb->data);
1370 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1372 struct xfrm_state *x;
1373 struct sk_buff *r_skb;
1376 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1377 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1378 struct xfrm_usersa_id *id = &p->sa_id;
1380 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1381 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1383 if (p->flags&XFRM_AE_RTHR)
1384 len+=RTA_SPACE(sizeof(u32));
1386 if (p->flags&XFRM_AE_ETHR)
1387 len+=RTA_SPACE(sizeof(u32));
1389 r_skb = alloc_skb(len, GFP_ATOMIC);
1393 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1400 * XXX: is this lock really needed - none of the other
1401 * gets lock (the concern is things getting updated
1402 * while we are still reading) - jhs
1404 spin_lock_bh(&x->lock);
1405 c.data.aevent = p->flags;
1406 c.seq = nlh->nlmsg_seq;
1407 c.pid = nlh->nlmsg_pid;
1409 if (build_aevent(r_skb, x, &c) < 0)
1411 err = netlink_unicast(xfrm_nl, r_skb,
1412 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1413 spin_unlock_bh(&x->lock);
1418 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1420 struct xfrm_state *x;
1423 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1424 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1425 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1430 /* pedantic mode - thou shalt sayeth replaceth */
1431 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1434 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1438 if (x->km.state != XFRM_STATE_VALID)
1441 spin_lock_bh(&x->lock);
1442 err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
1443 spin_unlock_bh(&x->lock);
1447 c.event = nlh->nlmsg_type;
1448 c.seq = nlh->nlmsg_seq;
1449 c.pid = nlh->nlmsg_pid;
1450 c.data.aevent = XFRM_AE_CU;
1451 km_state_notify(x, &c);
1458 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1461 u8 type = XFRM_POLICY_TYPE_MAIN;
1463 struct xfrm_audit audit_info;
1465 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1469 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1470 audit_info.secid = NETLINK_CB(skb).sid;
1471 xfrm_policy_flush(type, &audit_info);
1473 c.event = nlh->nlmsg_type;
1474 c.seq = nlh->nlmsg_seq;
1475 c.pid = nlh->nlmsg_pid;
1476 km_policy_notify(NULL, 0, &c);
1480 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1482 struct xfrm_policy *xp;
1483 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1484 struct xfrm_userpolicy_info *p = &up->pol;
1485 u8 type = XFRM_POLICY_TYPE_MAIN;
1488 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1493 xp = xfrm_policy_byid(type, p->dir, p->index, 0);
1495 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1496 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1497 struct xfrm_policy tmp;
1499 err = verify_sec_ctx_len(rtattrs);
1503 memset(&tmp, 0, sizeof(struct xfrm_policy));
1505 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1507 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1510 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, 0);
1511 security_xfrm_policy_free(&tmp);
1516 read_lock(&xp->lock);
1518 read_unlock(&xp->lock);
1522 read_unlock(&xp->lock);
1525 xfrm_policy_delete(xp, p->dir);
1526 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1527 AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
1530 // reset the timers here?
1531 printk("Dont know what to do with soft policy expire\n");
1533 km_policy_expired(xp, p->dir, up->hard, current->pid);
1540 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1542 struct xfrm_state *x;
1544 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1545 struct xfrm_usersa_info *p = &ue->state;
1547 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1555 spin_lock_bh(&x->lock);
1556 if (x->km.state != XFRM_STATE_VALID)
1558 km_state_expired(x, ue->hard, current->pid);
1561 __xfrm_state_delete(x);
1562 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1563 AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
1566 spin_unlock_bh(&x->lock);
1571 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1573 struct xfrm_policy *xp;
1574 struct xfrm_user_tmpl *ut;
1576 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1578 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1579 struct xfrm_state *x = xfrm_state_alloc();
1585 err = verify_newpolicy_info(&ua->policy);
1587 printk("BAD policy passed\n");
1593 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);
1599 memcpy(&x->id, &ua->id, sizeof(ua->id));
1600 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1601 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1604 /* extract the templates and for each call km_key */
1605 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1606 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1607 memcpy(&x->id, &t->id, sizeof(x->id));
1608 x->props.mode = t->mode;
1609 x->props.reqid = t->reqid;
1610 x->props.family = ut->family;
1611 t->aalgos = ua->aalgos;
1612 t->ealgos = ua->ealgos;
1613 t->calgos = ua->calgos;
1614 err = km_query(x, t, xp);
1625 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1627 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1628 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1629 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1630 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1631 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1632 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1633 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1634 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1635 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1636 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1637 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1638 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1639 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1640 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1641 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
1642 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1643 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1644 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
1649 static struct xfrm_link {
1650 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1651 int (*dump)(struct sk_buff *, struct netlink_callback *);
1652 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1653 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1654 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1655 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1656 .dump = xfrm_dump_sa },
1657 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1658 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1659 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1660 .dump = xfrm_dump_policy },
1661 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1662 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
1663 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1664 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1665 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1666 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1667 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1668 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
1669 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1670 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1673 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1675 struct rtattr *xfrma[XFRMA_MAX];
1676 struct xfrm_link *link;
1679 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1682 type = nlh->nlmsg_type;
1684 /* A control message: ignore them */
1685 if (type < XFRM_MSG_BASE)
1688 /* Unknown message: reply with EINVAL */
1689 if (type > XFRM_MSG_MAX)
1692 type -= XFRM_MSG_BASE;
1693 link = &xfrm_dispatch[type];
1695 /* All operations require privileges, even GET */
1696 if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
1701 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1702 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1703 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1704 if (link->dump == NULL)
1707 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
1708 link->dump, NULL)) != 0) {
1712 netlink_queue_skip(nlh, skb);
1716 memset(xfrma, 0, sizeof(xfrma));
1718 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1721 if (nlh->nlmsg_len > min_len) {
1722 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1723 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1725 while (RTA_OK(attr, attrlen)) {
1726 unsigned short flavor = attr->rta_type;
1728 if (flavor > XFRMA_MAX)
1730 xfrma[flavor - 1] = attr;
1732 attr = RTA_NEXT(attr, attrlen);
1736 if (link->doit == NULL)
1738 *errp = link->doit(skb, nlh, (void **) &xfrma);
1747 static void xfrm_netlink_rcv(struct sock *sk, int len)
1749 unsigned int qlen = 0;
1752 mutex_lock(&xfrm_cfg_mutex);
1753 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
1754 mutex_unlock(&xfrm_cfg_mutex);
1759 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1761 struct xfrm_user_expire *ue;
1762 struct nlmsghdr *nlh;
1763 unsigned char *b = skb->tail;
1765 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
1767 ue = NLMSG_DATA(nlh);
1768 nlh->nlmsg_flags = 0;
1770 copy_to_user_state(x, &ue->state);
1771 ue->hard = (c->data.hard != 0) ? 1 : 0;
1773 nlh->nlmsg_len = skb->tail - b;
1777 skb_trim(skb, b - skb->data);
1781 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1783 struct sk_buff *skb;
1784 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
1786 skb = alloc_skb(len, GFP_ATOMIC);
1790 if (build_expire(skb, x, c) < 0)
1793 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1794 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1797 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1799 struct sk_buff *skb;
1800 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1802 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1803 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1804 skb = alloc_skb(len, GFP_ATOMIC);
1808 if (build_aevent(skb, x, c) < 0)
1811 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1812 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1815 static int xfrm_notify_sa_flush(struct km_event *c)
1817 struct xfrm_usersa_flush *p;
1818 struct nlmsghdr *nlh;
1819 struct sk_buff *skb;
1821 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1823 skb = alloc_skb(len, GFP_ATOMIC);
1828 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1829 XFRM_MSG_FLUSHSA, sizeof(*p));
1830 nlh->nlmsg_flags = 0;
1832 p = NLMSG_DATA(nlh);
1833 p->proto = c->data.proto;
1835 nlh->nlmsg_len = skb->tail - b;
1837 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1838 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1845 static int inline xfrm_sa_len(struct xfrm_state *x)
1849 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1851 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1853 l += RTA_SPACE(sizeof(*x->calg));
1855 l += RTA_SPACE(sizeof(*x->encap));
1860 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1862 struct xfrm_usersa_info *p;
1863 struct xfrm_usersa_id *id;
1864 struct nlmsghdr *nlh;
1865 struct sk_buff *skb;
1867 int len = xfrm_sa_len(x);
1870 headlen = sizeof(*p);
1871 if (c->event == XFRM_MSG_DELSA) {
1872 len += RTA_SPACE(headlen);
1873 headlen = sizeof(*id);
1875 len += NLMSG_SPACE(headlen);
1877 skb = alloc_skb(len, GFP_ATOMIC);
1882 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
1883 nlh->nlmsg_flags = 0;
1885 p = NLMSG_DATA(nlh);
1886 if (c->event == XFRM_MSG_DELSA) {
1887 id = NLMSG_DATA(nlh);
1888 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1889 id->spi = x->id.spi;
1890 id->family = x->props.family;
1891 id->proto = x->id.proto;
1893 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1896 copy_to_user_state(x, p);
1899 RTA_PUT(skb, XFRMA_ALG_AUTH,
1900 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1902 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1903 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1905 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1908 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1910 nlh->nlmsg_len = skb->tail - b;
1912 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1913 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1921 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1925 case XFRM_MSG_EXPIRE:
1926 return xfrm_exp_state_notify(x, c);
1927 case XFRM_MSG_NEWAE:
1928 return xfrm_aevent_state_notify(x, c);
1929 case XFRM_MSG_DELSA:
1930 case XFRM_MSG_UPDSA:
1931 case XFRM_MSG_NEWSA:
1932 return xfrm_notify_sa(x, c);
1933 case XFRM_MSG_FLUSHSA:
1934 return xfrm_notify_sa_flush(c);
1936 printk("xfrm_user: Unknown SA event %d\n", c->event);
1944 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1945 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1948 struct xfrm_user_acquire *ua;
1949 struct nlmsghdr *nlh;
1950 unsigned char *b = skb->tail;
1951 __u32 seq = xfrm_get_acqseq();
1953 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1955 ua = NLMSG_DATA(nlh);
1956 nlh->nlmsg_flags = 0;
1958 memcpy(&ua->id, &x->id, sizeof(ua->id));
1959 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1960 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1961 copy_to_user_policy(xp, &ua->policy, dir);
1962 ua->aalgos = xt->aalgos;
1963 ua->ealgos = xt->ealgos;
1964 ua->calgos = xt->calgos;
1965 ua->seq = x->km.seq = seq;
1967 if (copy_to_user_tmpl(xp, skb) < 0)
1969 if (copy_to_user_state_sec_ctx(x, skb))
1971 if (copy_to_user_policy_type(xp->type, skb) < 0)
1974 nlh->nlmsg_len = skb->tail - b;
1978 skb_trim(skb, b - skb->data);
1982 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1983 struct xfrm_policy *xp, int dir)
1985 struct sk_buff *skb;
1988 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1989 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
1990 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1991 #ifdef CONFIG_XFRM_SUB_POLICY
1992 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
1994 skb = alloc_skb(len, GFP_ATOMIC);
1998 if (build_acquire(skb, x, xt, xp, dir) < 0)
2001 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
2002 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2005 /* User gives us xfrm_user_policy_info followed by an array of 0
2006 * or more templates.
2008 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2009 u8 *data, int len, int *dir)
2011 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2012 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2013 struct xfrm_policy *xp;
2016 switch (sk->sk_family) {
2018 if (opt != IP_XFRM_POLICY) {
2023 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2025 if (opt != IPV6_XFRM_POLICY) {
2038 if (len < sizeof(*p) ||
2039 verify_newpolicy_info(p))
2042 nr = ((len - sizeof(*p)) / sizeof(*ut));
2043 if (validate_tmpl(nr, ut, p->sel.family))
2046 if (p->dir > XFRM_POLICY_OUT)
2049 xp = xfrm_policy_alloc(GFP_KERNEL);
2055 copy_from_user_policy(xp, p);
2056 xp->type = XFRM_POLICY_TYPE_MAIN;
2057 copy_templates(xp, ut, nr);
2064 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2065 int dir, struct km_event *c)
2067 struct xfrm_user_polexpire *upe;
2068 struct nlmsghdr *nlh;
2069 int hard = c->data.hard;
2070 unsigned char *b = skb->tail;
2072 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
2073 upe = NLMSG_DATA(nlh);
2074 nlh->nlmsg_flags = 0;
2076 copy_to_user_policy(xp, &upe->pol, dir);
2077 if (copy_to_user_tmpl(xp, skb) < 0)
2079 if (copy_to_user_sec_ctx(xp, skb))
2081 if (copy_to_user_policy_type(xp->type, skb) < 0)
2085 nlh->nlmsg_len = skb->tail - b;
2089 skb_trim(skb, b - skb->data);
2093 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2095 struct sk_buff *skb;
2098 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2099 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
2100 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
2101 #ifdef CONFIG_XFRM_SUB_POLICY
2102 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2104 skb = alloc_skb(len, GFP_ATOMIC);
2108 if (build_polexpire(skb, xp, dir, c) < 0)
2111 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
2112 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2115 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2117 struct xfrm_userpolicy_info *p;
2118 struct xfrm_userpolicy_id *id;
2119 struct nlmsghdr *nlh;
2120 struct sk_buff *skb;
2122 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2125 headlen = sizeof(*p);
2126 if (c->event == XFRM_MSG_DELPOLICY) {
2127 len += RTA_SPACE(headlen);
2128 headlen = sizeof(*id);
2130 #ifdef CONFIG_XFRM_SUB_POLICY
2131 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2133 len += NLMSG_SPACE(headlen);
2135 skb = alloc_skb(len, GFP_ATOMIC);
2140 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
2142 p = NLMSG_DATA(nlh);
2143 if (c->event == XFRM_MSG_DELPOLICY) {
2144 id = NLMSG_DATA(nlh);
2145 memset(id, 0, sizeof(*id));
2148 id->index = xp->index;
2150 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2152 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
2155 nlh->nlmsg_flags = 0;
2157 copy_to_user_policy(xp, p, dir);
2158 if (copy_to_user_tmpl(xp, skb) < 0)
2160 if (copy_to_user_policy_type(xp->type, skb) < 0)
2163 nlh->nlmsg_len = skb->tail - b;
2165 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2166 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2174 static int xfrm_notify_policy_flush(struct km_event *c)
2176 struct nlmsghdr *nlh;
2177 struct sk_buff *skb;
2180 #ifdef CONFIG_XFRM_SUB_POLICY
2181 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2183 len += NLMSG_LENGTH(0);
2185 skb = alloc_skb(len, GFP_ATOMIC);
2191 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
2192 nlh->nlmsg_flags = 0;
2193 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2196 nlh->nlmsg_len = skb->tail - b;
2198 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2199 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2206 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2210 case XFRM_MSG_NEWPOLICY:
2211 case XFRM_MSG_UPDPOLICY:
2212 case XFRM_MSG_DELPOLICY:
2213 return xfrm_notify_policy(xp, dir, c);
2214 case XFRM_MSG_FLUSHPOLICY:
2215 return xfrm_notify_policy_flush(c);
2216 case XFRM_MSG_POLEXPIRE:
2217 return xfrm_exp_policy_notify(xp, dir, c);
2219 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2226 static int build_report(struct sk_buff *skb, u8 proto,
2227 struct xfrm_selector *sel, xfrm_address_t *addr)
2229 struct xfrm_user_report *ur;
2230 struct nlmsghdr *nlh;
2231 unsigned char *b = skb->tail;
2233 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur));
2234 ur = NLMSG_DATA(nlh);
2235 nlh->nlmsg_flags = 0;
2238 memcpy(&ur->sel, sel, sizeof(ur->sel));
2241 RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2243 nlh->nlmsg_len = skb->tail - b;
2248 skb_trim(skb, b - skb->data);
2252 static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2253 xfrm_address_t *addr)
2255 struct sk_buff *skb;
2258 len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
2259 skb = alloc_skb(len, GFP_ATOMIC);
2263 if (build_report(skb, proto, sel, addr) < 0)
2266 NETLINK_CB(skb).dst_group = XFRMNLGRP_REPORT;
2267 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2270 static struct xfrm_mgr netlink_mgr = {
2272 .notify = xfrm_send_state_notify,
2273 .acquire = xfrm_send_acquire,
2274 .compile_policy = xfrm_compile_policy,
2275 .notify_policy = xfrm_send_policy_notify,
2276 .report = xfrm_send_report,
2279 static int __init xfrm_user_init(void)
2283 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2285 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
2286 xfrm_netlink_rcv, THIS_MODULE);
2289 rcu_assign_pointer(xfrm_nl, nlsk);
2291 xfrm_register_km(&netlink_mgr);
2296 static void __exit xfrm_user_exit(void)
2298 struct sock *nlsk = xfrm_nl;
2300 xfrm_unregister_km(&netlink_mgr);
2301 rcu_assign_pointer(xfrm_nl, NULL);
2303 sock_release(nlsk->sk_socket);
2306 module_init(xfrm_user_init);
2307 module_exit(xfrm_user_exit);
2308 MODULE_LICENSE("GPL");
2309 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);