1 /* xfrm_user.c: User interface to configure xfrm engine.
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/socket.h>
18 #include <linux/string.h>
19 #include <linux/net.h>
20 #include <linux/skbuff.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/pfkeyv2.h>
23 #include <linux/ipsec.h>
24 #include <linux/init.h>
25 #include <linux/security.h>
28 #include <net/netlink.h>
29 #include <asm/uaccess.h>
31 static struct sock *xfrm_nl;
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;
279 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
280 struct rtattr **xfrma,
283 struct xfrm_state *x = xfrm_state_alloc();
289 copy_from_user_state(x, p);
291 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
292 xfrm_aalg_get_byname,
293 xfrma[XFRMA_ALG_AUTH-1])))
295 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
296 xfrm_ealg_get_byname,
297 xfrma[XFRMA_ALG_CRYPT-1])))
299 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
300 xfrm_calg_get_byname,
301 xfrma[XFRMA_ALG_COMP-1])))
303 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
306 err = xfrm_init_state(x);
310 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
318 x->km.state = XFRM_STATE_DEAD;
325 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
327 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
328 struct xfrm_state *x;
332 err = verify_newsa_info(p, (struct rtattr **)xfrma);
336 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
341 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
342 err = xfrm_state_add(x);
344 err = xfrm_state_update(x);
347 x->km.state = XFRM_STATE_DEAD;
352 c.seq = nlh->nlmsg_seq;
353 c.pid = nlh->nlmsg_pid;
354 c.event = nlh->nlmsg_type;
356 km_state_notify(x, &c);
362 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
364 struct xfrm_state *x;
367 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
369 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
373 if (xfrm_state_kern(x)) {
378 err = xfrm_state_delete(x);
384 c.seq = nlh->nlmsg_seq;
385 c.pid = nlh->nlmsg_pid;
386 c.event = nlh->nlmsg_type;
387 km_state_notify(x, &c);
393 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
395 memcpy(&p->id, &x->id, sizeof(p->id));
396 memcpy(&p->sel, &x->sel, sizeof(p->sel));
397 memcpy(&p->lft, &x->lft, sizeof(p->lft));
398 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
399 memcpy(&p->stats, &x->stats, sizeof(p->stats));
400 p->saddr = x->props.saddr;
401 p->mode = x->props.mode;
402 p->replay_window = x->props.replay_window;
403 p->reqid = x->props.reqid;
404 p->family = x->props.family;
405 p->flags = x->props.flags;
409 struct xfrm_dump_info {
410 struct sk_buff *in_skb;
411 struct sk_buff *out_skb;
418 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
420 struct xfrm_dump_info *sp = ptr;
421 struct sk_buff *in_skb = sp->in_skb;
422 struct sk_buff *skb = sp->out_skb;
423 struct xfrm_usersa_info *p;
424 struct nlmsghdr *nlh;
425 unsigned char *b = skb->tail;
427 if (sp->this_idx < sp->start_idx)
430 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
432 XFRM_MSG_NEWSA, sizeof(*p));
433 nlh->nlmsg_flags = sp->nlmsg_flags;
436 copy_to_user_state(x, p);
439 RTA_PUT(skb, XFRMA_ALG_AUTH,
440 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
442 RTA_PUT(skb, XFRMA_ALG_CRYPT,
443 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
445 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
448 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
451 int ctx_size = sizeof(struct xfrm_sec_ctx) +
452 x->security->ctx_len;
453 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
454 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
456 uctx->exttype = XFRMA_SEC_CTX;
457 uctx->len = ctx_size;
458 uctx->ctx_doi = x->security->ctx_doi;
459 uctx->ctx_alg = x->security->ctx_alg;
460 uctx->ctx_len = x->security->ctx_len;
461 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
463 nlh->nlmsg_len = skb->tail - b;
470 skb_trim(skb, b - skb->data);
474 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
476 struct xfrm_dump_info info;
478 info.in_skb = cb->skb;
480 info.nlmsg_seq = cb->nlh->nlmsg_seq;
481 info.nlmsg_flags = NLM_F_MULTI;
483 info.start_idx = cb->args[0];
484 (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
485 cb->args[0] = info.this_idx;
490 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
491 struct xfrm_state *x, u32 seq)
493 struct xfrm_dump_info info;
496 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
498 return ERR_PTR(-ENOMEM);
500 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
501 info.in_skb = in_skb;
503 info.nlmsg_seq = seq;
504 info.nlmsg_flags = 0;
505 info.this_idx = info.start_idx = 0;
507 if (dump_one_state(x, 0, &info)) {
515 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
517 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
518 struct xfrm_state *x;
519 struct sk_buff *resp_skb;
522 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
527 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
528 if (IS_ERR(resp_skb)) {
529 err = PTR_ERR(resp_skb);
531 err = netlink_unicast(xfrm_nl, resp_skb,
532 NETLINK_CB(skb).pid, MSG_DONTWAIT);
539 static int verify_userspi_info(struct xfrm_userspi_info *p)
541 switch (p->info.id.proto) {
547 /* IPCOMP spi is 16-bits. */
548 if (p->max >= 0x10000)
562 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
564 struct xfrm_state *x;
565 struct xfrm_userspi_info *p;
566 struct sk_buff *resp_skb;
567 xfrm_address_t *daddr;
572 err = verify_userspi_info(p);
576 family = p->info.family;
577 daddr = &p->info.id.daddr;
581 x = xfrm_find_acq_byseq(p->info.seq);
582 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
589 x = xfrm_find_acq(p->info.mode, p->info.reqid,
590 p->info.id.proto, daddr,
597 resp_skb = ERR_PTR(-ENOENT);
599 spin_lock_bh(&x->lock);
600 if (x->km.state != XFRM_STATE_DEAD) {
601 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
603 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
605 spin_unlock_bh(&x->lock);
607 if (IS_ERR(resp_skb)) {
608 err = PTR_ERR(resp_skb);
612 err = netlink_unicast(xfrm_nl, resp_skb,
613 NETLINK_CB(skb).pid, MSG_DONTWAIT);
621 static int verify_policy_dir(__u8 dir)
625 case XFRM_POLICY_OUT:
626 case XFRM_POLICY_FWD:
636 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
640 case XFRM_SHARE_SESSION:
641 case XFRM_SHARE_USER:
642 case XFRM_SHARE_UNIQUE:
650 case XFRM_POLICY_ALLOW:
651 case XFRM_POLICY_BLOCK:
658 switch (p->sel.family) {
663 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
666 return -EAFNOSUPPORT;
673 return verify_policy_dir(p->dir);
676 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
678 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
679 struct xfrm_user_sec_ctx *uctx;
685 return security_xfrm_policy_alloc(pol, uctx);
688 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
694 for (i = 0; i < nr; i++, ut++) {
695 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
697 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
698 memcpy(&t->saddr, &ut->saddr,
699 sizeof(xfrm_address_t));
700 t->reqid = ut->reqid;
702 t->share = ut->share;
703 t->optional = ut->optional;
704 t->aalgos = ut->aalgos;
705 t->ealgos = ut->ealgos;
706 t->calgos = ut->calgos;
710 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
712 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
713 struct xfrm_user_tmpl *utmpl;
719 nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
721 if (nr > XFRM_MAX_DEPTH)
724 copy_templates(pol, RTA_DATA(rt), nr);
729 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
731 xp->priority = p->priority;
732 xp->index = p->index;
733 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
734 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
735 xp->action = p->action;
736 xp->flags = p->flags;
737 xp->family = p->sel.family;
738 /* XXX xp->share = p->share; */
741 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
743 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
744 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
745 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
746 p->priority = xp->priority;
747 p->index = xp->index;
748 p->sel.family = xp->family;
750 p->action = xp->action;
751 p->flags = xp->flags;
752 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
755 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
757 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
765 copy_from_user_policy(xp, p);
767 if (!(err = copy_from_user_tmpl(xp, xfrma)))
768 err = copy_from_user_sec_ctx(xp, xfrma);
779 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
781 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
782 struct xfrm_policy *xp;
787 err = verify_newpolicy_info(p);
790 err = verify_sec_ctx_len((struct rtattr **)xfrma);
794 xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
798 /* shouldnt excl be based on nlh flags??
799 * Aha! this is anti-netlink really i.e more pfkey derived
800 * in netlink excl is a flag and you wouldnt need
801 * a type XFRM_MSG_UPDPOLICY - JHS */
802 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
803 err = xfrm_policy_insert(p->dir, xp, excl);
809 c.event = nlh->nlmsg_type;
810 c.seq = nlh->nlmsg_seq;
811 c.pid = nlh->nlmsg_pid;
812 km_policy_notify(xp, p->dir, &c);
819 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
821 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
824 if (xp->xfrm_nr == 0)
827 for (i = 0; i < xp->xfrm_nr; i++) {
828 struct xfrm_user_tmpl *up = &vec[i];
829 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
831 memcpy(&up->id, &kp->id, sizeof(up->id));
832 up->family = xp->family;
833 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
834 up->reqid = kp->reqid;
836 up->share = kp->share;
837 up->optional = kp->optional;
838 up->aalgos = kp->aalgos;
839 up->ealgos = kp->ealgos;
840 up->calgos = kp->calgos;
842 RTA_PUT(skb, XFRMA_TMPL,
843 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
852 static int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
855 int ctx_size = sizeof(struct xfrm_sec_ctx) +
856 xp->security->ctx_len;
857 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
858 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
860 uctx->exttype = XFRMA_SEC_CTX;
861 uctx->len = ctx_size;
862 uctx->ctx_doi = xp->security->ctx_doi;
863 uctx->ctx_alg = xp->security->ctx_alg;
864 uctx->ctx_len = xp->security->ctx_len;
865 memcpy(uctx + 1, xp->security->ctx_str, xp->security->ctx_len);
873 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
875 struct xfrm_dump_info *sp = ptr;
876 struct xfrm_userpolicy_info *p;
877 struct sk_buff *in_skb = sp->in_skb;
878 struct sk_buff *skb = sp->out_skb;
879 struct nlmsghdr *nlh;
880 unsigned char *b = skb->tail;
882 if (sp->this_idx < sp->start_idx)
885 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
887 XFRM_MSG_NEWPOLICY, sizeof(*p));
889 nlh->nlmsg_flags = sp->nlmsg_flags;
891 copy_to_user_policy(xp, p, dir);
892 if (copy_to_user_tmpl(xp, skb) < 0)
894 if (copy_to_user_sec_ctx(xp, skb))
897 nlh->nlmsg_len = skb->tail - b;
903 skb_trim(skb, b - skb->data);
907 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
909 struct xfrm_dump_info info;
911 info.in_skb = cb->skb;
913 info.nlmsg_seq = cb->nlh->nlmsg_seq;
914 info.nlmsg_flags = NLM_F_MULTI;
916 info.start_idx = cb->args[0];
917 (void) xfrm_policy_walk(dump_one_policy, &info);
918 cb->args[0] = info.this_idx;
923 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
924 struct xfrm_policy *xp,
927 struct xfrm_dump_info info;
930 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
932 return ERR_PTR(-ENOMEM);
934 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
935 info.in_skb = in_skb;
937 info.nlmsg_seq = seq;
938 info.nlmsg_flags = 0;
939 info.this_idx = info.start_idx = 0;
941 if (dump_one_policy(xp, dir, 0, &info) < 0) {
949 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
951 struct xfrm_policy *xp;
952 struct xfrm_userpolicy_id *p;
958 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
960 err = verify_policy_dir(p->dir);
965 xp = xfrm_policy_byid(p->dir, p->index, delete);
967 struct rtattr **rtattrs = (struct rtattr **)xfrma;
968 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
969 struct xfrm_policy tmp;
971 err = verify_sec_ctx_len(rtattrs);
975 memset(&tmp, 0, sizeof(struct xfrm_policy));
977 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
979 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
982 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, delete);
983 security_xfrm_policy_free(&tmp);
989 struct sk_buff *resp_skb;
991 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
992 if (IS_ERR(resp_skb)) {
993 err = PTR_ERR(resp_skb);
995 err = netlink_unicast(xfrm_nl, resp_skb,
1000 c.data.byid = p->index;
1001 c.event = nlh->nlmsg_type;
1002 c.seq = nlh->nlmsg_seq;
1003 c.pid = nlh->nlmsg_pid;
1004 km_policy_notify(xp, p->dir, &c);
1012 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1015 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1017 xfrm_state_flush(p->proto);
1018 c.data.proto = p->proto;
1019 c.event = nlh->nlmsg_type;
1020 c.seq = nlh->nlmsg_seq;
1021 c.pid = nlh->nlmsg_pid;
1022 km_state_notify(NULL, &c);
1027 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1031 xfrm_policy_flush();
1032 c.event = nlh->nlmsg_type;
1033 c.seq = nlh->nlmsg_seq;
1034 c.pid = nlh->nlmsg_pid;
1035 km_policy_notify(NULL, 0, &c);
1039 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1041 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1042 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1043 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1044 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1045 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1046 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1047 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1048 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1049 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1050 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1051 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1052 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1053 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1054 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1055 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
1060 static struct xfrm_link {
1061 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1062 int (*dump)(struct sk_buff *, struct netlink_callback *);
1063 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1064 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1065 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1066 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1067 .dump = xfrm_dump_sa },
1068 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1069 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1070 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1071 .dump = xfrm_dump_policy },
1072 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1073 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1074 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1075 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1076 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
1079 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1081 struct rtattr *xfrma[XFRMA_MAX];
1082 struct xfrm_link *link;
1085 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1088 type = nlh->nlmsg_type;
1090 /* A control message: ignore them */
1091 if (type < XFRM_MSG_BASE)
1094 /* Unknown message: reply with EINVAL */
1095 if (type > XFRM_MSG_MAX)
1098 type -= XFRM_MSG_BASE;
1099 link = &xfrm_dispatch[type];
1101 /* All operations require privileges, even GET */
1102 if (security_netlink_recv(skb)) {
1107 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1108 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1109 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1110 if (link->dump == NULL)
1113 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
1114 link->dump, NULL)) != 0) {
1118 netlink_queue_skip(nlh, skb);
1122 memset(xfrma, 0, sizeof(xfrma));
1124 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1127 if (nlh->nlmsg_len > min_len) {
1128 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1129 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1131 while (RTA_OK(attr, attrlen)) {
1132 unsigned short flavor = attr->rta_type;
1134 if (flavor > XFRMA_MAX)
1136 xfrma[flavor - 1] = attr;
1138 attr = RTA_NEXT(attr, attrlen);
1142 if (link->doit == NULL)
1144 *errp = link->doit(skb, nlh, (void **) &xfrma);
1153 static void xfrm_netlink_rcv(struct sock *sk, int len)
1155 unsigned int qlen = 0;
1158 down(&xfrm_cfg_sem);
1159 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
1165 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, int hard)
1167 struct xfrm_user_expire *ue;
1168 struct nlmsghdr *nlh;
1169 unsigned char *b = skb->tail;
1171 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_EXPIRE,
1173 ue = NLMSG_DATA(nlh);
1174 nlh->nlmsg_flags = 0;
1176 copy_to_user_state(x, &ue->state);
1177 ue->hard = (hard != 0) ? 1 : 0;
1179 nlh->nlmsg_len = skb->tail - b;
1183 skb_trim(skb, b - skb->data);
1187 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1189 struct sk_buff *skb;
1190 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
1192 skb = alloc_skb(len, GFP_ATOMIC);
1196 if (build_expire(skb, x, c->data.hard) < 0)
1199 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1200 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1203 static int xfrm_notify_sa_flush(struct km_event *c)
1205 struct xfrm_usersa_flush *p;
1206 struct nlmsghdr *nlh;
1207 struct sk_buff *skb;
1209 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1211 skb = alloc_skb(len, GFP_ATOMIC);
1216 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1217 XFRM_MSG_FLUSHSA, sizeof(*p));
1218 nlh->nlmsg_flags = 0;
1220 p = NLMSG_DATA(nlh);
1221 p->proto = c->data.proto;
1223 nlh->nlmsg_len = skb->tail - b;
1225 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1226 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1233 static int inline xfrm_sa_len(struct xfrm_state *x)
1237 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1239 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1241 l += RTA_SPACE(sizeof(*x->calg));
1243 l += RTA_SPACE(sizeof(*x->encap));
1248 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1250 struct xfrm_usersa_info *p;
1251 struct xfrm_usersa_id *id;
1252 struct nlmsghdr *nlh;
1253 struct sk_buff *skb;
1255 int len = xfrm_sa_len(x);
1258 headlen = sizeof(*p);
1259 if (c->event == XFRM_MSG_DELSA) {
1260 len += RTA_SPACE(headlen);
1261 headlen = sizeof(*id);
1263 len += NLMSG_SPACE(headlen);
1265 skb = alloc_skb(len, GFP_ATOMIC);
1270 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
1271 nlh->nlmsg_flags = 0;
1273 p = NLMSG_DATA(nlh);
1274 if (c->event == XFRM_MSG_DELSA) {
1275 id = NLMSG_DATA(nlh);
1276 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1277 id->spi = x->id.spi;
1278 id->family = x->props.family;
1279 id->proto = x->id.proto;
1281 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1284 copy_to_user_state(x, p);
1287 RTA_PUT(skb, XFRMA_ALG_AUTH,
1288 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1290 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1291 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1293 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1296 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1298 nlh->nlmsg_len = skb->tail - b;
1300 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1301 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1309 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1313 case XFRM_MSG_EXPIRE:
1314 return xfrm_exp_state_notify(x, c);
1315 case XFRM_MSG_DELSA:
1316 case XFRM_MSG_UPDSA:
1317 case XFRM_MSG_NEWSA:
1318 return xfrm_notify_sa(x, c);
1319 case XFRM_MSG_FLUSHSA:
1320 return xfrm_notify_sa_flush(c);
1322 printk("xfrm_user: Unknown SA event %d\n", c->event);
1330 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1331 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1334 struct xfrm_user_acquire *ua;
1335 struct nlmsghdr *nlh;
1336 unsigned char *b = skb->tail;
1337 __u32 seq = xfrm_get_acqseq();
1339 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1341 ua = NLMSG_DATA(nlh);
1342 nlh->nlmsg_flags = 0;
1344 memcpy(&ua->id, &x->id, sizeof(ua->id));
1345 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1346 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1347 copy_to_user_policy(xp, &ua->policy, dir);
1348 ua->aalgos = xt->aalgos;
1349 ua->ealgos = xt->ealgos;
1350 ua->calgos = xt->calgos;
1351 ua->seq = x->km.seq = seq;
1353 if (copy_to_user_tmpl(xp, skb) < 0)
1355 if (copy_to_user_sec_ctx(xp, skb))
1358 nlh->nlmsg_len = skb->tail - b;
1362 skb_trim(skb, b - skb->data);
1366 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1367 struct xfrm_policy *xp, int dir)
1369 struct sk_buff *skb;
1372 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1373 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
1374 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1375 skb = alloc_skb(len, GFP_ATOMIC);
1379 if (build_acquire(skb, x, xt, xp, dir) < 0)
1382 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
1383 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
1386 /* User gives us xfrm_user_policy_info followed by an array of 0
1387 * or more templates.
1389 static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
1390 u8 *data, int len, int *dir)
1392 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1393 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1394 struct xfrm_policy *xp;
1399 if (opt != IP_XFRM_POLICY) {
1404 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1406 if (opt != IPV6_XFRM_POLICY) {
1419 if (len < sizeof(*p) ||
1420 verify_newpolicy_info(p))
1423 nr = ((len - sizeof(*p)) / sizeof(*ut));
1424 if (nr > XFRM_MAX_DEPTH)
1427 if (p->dir > XFRM_POLICY_OUT)
1430 xp = xfrm_policy_alloc(GFP_KERNEL);
1436 copy_from_user_policy(xp, p);
1437 copy_templates(xp, ut, nr);
1444 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
1447 struct xfrm_user_polexpire *upe;
1448 struct nlmsghdr *nlh;
1449 unsigned char *b = skb->tail;
1451 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
1452 upe = NLMSG_DATA(nlh);
1453 nlh->nlmsg_flags = 0;
1455 copy_to_user_policy(xp, &upe->pol, dir);
1456 if (copy_to_user_tmpl(xp, skb) < 0)
1458 if (copy_to_user_sec_ctx(xp, skb))
1462 nlh->nlmsg_len = skb->tail - b;
1466 skb_trim(skb, b - skb->data);
1470 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1472 struct sk_buff *skb;
1475 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1476 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
1477 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1478 skb = alloc_skb(len, GFP_ATOMIC);
1482 if (build_polexpire(skb, xp, dir, c->data.hard) < 0)
1485 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1486 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1489 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
1491 struct xfrm_userpolicy_info *p;
1492 struct xfrm_userpolicy_id *id;
1493 struct nlmsghdr *nlh;
1494 struct sk_buff *skb;
1496 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1499 headlen = sizeof(*p);
1500 if (c->event == XFRM_MSG_DELPOLICY) {
1501 len += RTA_SPACE(headlen);
1502 headlen = sizeof(*id);
1504 len += NLMSG_SPACE(headlen);
1506 skb = alloc_skb(len, GFP_ATOMIC);
1511 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
1513 p = NLMSG_DATA(nlh);
1514 if (c->event == XFRM_MSG_DELPOLICY) {
1515 id = NLMSG_DATA(nlh);
1516 memset(id, 0, sizeof(*id));
1519 id->index = xp->index;
1521 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
1523 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
1526 nlh->nlmsg_flags = 0;
1528 copy_to_user_policy(xp, p, dir);
1529 if (copy_to_user_tmpl(xp, skb) < 0)
1532 nlh->nlmsg_len = skb->tail - b;
1534 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1535 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
1543 static int xfrm_notify_policy_flush(struct km_event *c)
1545 struct nlmsghdr *nlh;
1546 struct sk_buff *skb;
1548 int len = NLMSG_LENGTH(0);
1550 skb = alloc_skb(len, GFP_ATOMIC);
1556 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
1558 nlh->nlmsg_len = skb->tail - b;
1560 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1561 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
1568 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1572 case XFRM_MSG_NEWPOLICY:
1573 case XFRM_MSG_UPDPOLICY:
1574 case XFRM_MSG_DELPOLICY:
1575 return xfrm_notify_policy(xp, dir, c);
1576 case XFRM_MSG_FLUSHPOLICY:
1577 return xfrm_notify_policy_flush(c);
1578 case XFRM_MSG_POLEXPIRE:
1579 return xfrm_exp_policy_notify(xp, dir, c);
1581 printk("xfrm_user: Unknown Policy event %d\n", c->event);
1588 static struct xfrm_mgr netlink_mgr = {
1590 .notify = xfrm_send_state_notify,
1591 .acquire = xfrm_send_acquire,
1592 .compile_policy = xfrm_compile_policy,
1593 .notify_policy = xfrm_send_policy_notify,
1596 static int __init xfrm_user_init(void)
1598 printk(KERN_INFO "Initializing IPsec netlink socket\n");
1600 xfrm_nl = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
1601 xfrm_netlink_rcv, THIS_MODULE);
1602 if (xfrm_nl == NULL)
1605 xfrm_register_km(&netlink_mgr);
1610 static void __exit xfrm_user_exit(void)
1612 xfrm_unregister_km(&netlink_mgr);
1613 sock_release(xfrm_nl->sk_socket);
1616 module_init(xfrm_user_init);
1617 module_exit(xfrm_user_exit);
1618 MODULE_LICENSE("GPL");
1619 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);