[MLSXFRM]: Add security context to acquire messages using netlink
[linux-2.6.git] / net / xfrm / xfrm_user.c
blobdac8db1088bcd44aa404c0f30be2821a699c5dce
1 /* xfrm_user.c: User interface to configure xfrm engine.
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
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>
27 #include <net/sock.h>
28 #include <net/xfrm.h>
29 #include <net/netlink.h>
30 #include <asm/uaccess.h>
32 static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
34 struct rtattr *rt = xfrma[type - 1];
35 struct xfrm_algo *algp;
36 int len;
38 if (!rt)
39 return 0;
41 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
42 if (len < 0)
43 return -EINVAL;
45 algp = RTA_DATA(rt);
47 len -= (algp->alg_key_len + 7U) / 8;
48 if (len < 0)
49 return -EINVAL;
51 switch (type) {
52 case XFRMA_ALG_AUTH:
53 if (!algp->alg_key_len &&
54 strcmp(algp->alg_name, "digest_null") != 0)
55 return -EINVAL;
56 break;
58 case XFRMA_ALG_CRYPT:
59 if (!algp->alg_key_len &&
60 strcmp(algp->alg_name, "cipher_null") != 0)
61 return -EINVAL;
62 break;
64 case XFRMA_ALG_COMP:
65 /* Zero length keys are legal. */
66 break;
68 default:
69 return -EINVAL;
72 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
73 return 0;
76 static int verify_encap_tmpl(struct rtattr **xfrma)
78 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
79 struct xfrm_encap_tmpl *encap;
81 if (!rt)
82 return 0;
84 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
85 return -EINVAL;
87 return 0;
91 static inline int verify_sec_ctx_len(struct rtattr **xfrma)
93 struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
94 struct xfrm_user_sec_ctx *uctx;
95 int len = 0;
97 if (!rt)
98 return 0;
100 if (rt->rta_len < sizeof(*uctx))
101 return -EINVAL;
103 uctx = RTA_DATA(rt);
105 len += sizeof(struct xfrm_user_sec_ctx);
106 len += uctx->ctx_len;
108 if (uctx->len != len)
109 return -EINVAL;
111 return 0;
115 static int verify_newsa_info(struct xfrm_usersa_info *p,
116 struct rtattr **xfrma)
118 int err;
120 err = -EINVAL;
121 switch (p->family) {
122 case AF_INET:
123 break;
125 case AF_INET6:
126 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
127 break;
128 #else
129 err = -EAFNOSUPPORT;
130 goto out;
131 #endif
133 default:
134 goto out;
137 err = -EINVAL;
138 switch (p->id.proto) {
139 case IPPROTO_AH:
140 if (!xfrma[XFRMA_ALG_AUTH-1] ||
141 xfrma[XFRMA_ALG_CRYPT-1] ||
142 xfrma[XFRMA_ALG_COMP-1])
143 goto out;
144 break;
146 case IPPROTO_ESP:
147 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
148 !xfrma[XFRMA_ALG_CRYPT-1]) ||
149 xfrma[XFRMA_ALG_COMP-1])
150 goto out;
151 break;
153 case IPPROTO_COMP:
154 if (!xfrma[XFRMA_ALG_COMP-1] ||
155 xfrma[XFRMA_ALG_AUTH-1] ||
156 xfrma[XFRMA_ALG_CRYPT-1])
157 goto out;
158 break;
160 default:
161 goto out;
164 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
165 goto out;
166 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
167 goto out;
168 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
169 goto out;
170 if ((err = verify_encap_tmpl(xfrma)))
171 goto out;
172 if ((err = verify_sec_ctx_len(xfrma)))
173 goto out;
175 err = -EINVAL;
176 switch (p->mode) {
177 case 0:
178 case 1:
179 break;
181 default:
182 goto out;
185 err = 0;
187 out:
188 return err;
191 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
192 struct xfrm_algo_desc *(*get_byname)(char *, int),
193 struct rtattr *u_arg)
195 struct rtattr *rta = u_arg;
196 struct xfrm_algo *p, *ualg;
197 struct xfrm_algo_desc *algo;
198 int len;
200 if (!rta)
201 return 0;
203 ualg = RTA_DATA(rta);
205 algo = get_byname(ualg->alg_name, 1);
206 if (!algo)
207 return -ENOSYS;
208 *props = algo->desc.sadb_alg_id;
210 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
211 p = kmalloc(len, GFP_KERNEL);
212 if (!p)
213 return -ENOMEM;
215 memcpy(p, ualg, len);
216 strcpy(p->alg_name, algo->name);
217 *algpp = p;
218 return 0;
221 static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
223 struct rtattr *rta = u_arg;
224 struct xfrm_encap_tmpl *p, *uencap;
226 if (!rta)
227 return 0;
229 uencap = RTA_DATA(rta);
230 p = kmalloc(sizeof(*p), GFP_KERNEL);
231 if (!p)
232 return -ENOMEM;
234 memcpy(p, uencap, sizeof(*p));
235 *encapp = p;
236 return 0;
240 static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
242 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
243 int len = 0;
245 if (xfrm_ctx) {
246 len += sizeof(struct xfrm_user_sec_ctx);
247 len += xfrm_ctx->ctx_len;
249 return len;
252 static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
254 struct xfrm_user_sec_ctx *uctx;
256 if (!u_arg)
257 return 0;
259 uctx = RTA_DATA(u_arg);
260 return security_xfrm_state_alloc(x, uctx);
263 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
265 memcpy(&x->id, &p->id, sizeof(x->id));
266 memcpy(&x->sel, &p->sel, sizeof(x->sel));
267 memcpy(&x->lft, &p->lft, sizeof(x->lft));
268 x->props.mode = p->mode;
269 x->props.replay_window = p->replay_window;
270 x->props.reqid = p->reqid;
271 x->props.family = p->family;
272 x->props.saddr = p->saddr;
273 x->props.flags = p->flags;
277 * someday when pfkey also has support, we could have the code
278 * somehow made shareable and move it to xfrm_state.c - JHS
281 static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
283 int err = - EINVAL;
284 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
285 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
286 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
287 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
289 if (rp) {
290 struct xfrm_replay_state *replay;
291 if (RTA_PAYLOAD(rp) < sizeof(*replay))
292 goto error;
293 replay = RTA_DATA(rp);
294 memcpy(&x->replay, replay, sizeof(*replay));
295 memcpy(&x->preplay, replay, sizeof(*replay));
298 if (lt) {
299 struct xfrm_lifetime_cur *ltime;
300 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
301 goto error;
302 ltime = RTA_DATA(lt);
303 x->curlft.bytes = ltime->bytes;
304 x->curlft.packets = ltime->packets;
305 x->curlft.add_time = ltime->add_time;
306 x->curlft.use_time = ltime->use_time;
309 if (et) {
310 if (RTA_PAYLOAD(et) < sizeof(u32))
311 goto error;
312 x->replay_maxage = *(u32*)RTA_DATA(et);
315 if (rt) {
316 if (RTA_PAYLOAD(rt) < sizeof(u32))
317 goto error;
318 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
321 return 0;
322 error:
323 return err;
326 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
327 struct rtattr **xfrma,
328 int *errp)
330 struct xfrm_state *x = xfrm_state_alloc();
331 int err = -ENOMEM;
333 if (!x)
334 goto error_no_put;
336 copy_from_user_state(x, p);
338 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
339 xfrm_aalg_get_byname,
340 xfrma[XFRMA_ALG_AUTH-1])))
341 goto error;
342 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
343 xfrm_ealg_get_byname,
344 xfrma[XFRMA_ALG_CRYPT-1])))
345 goto error;
346 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
347 xfrm_calg_get_byname,
348 xfrma[XFRMA_ALG_COMP-1])))
349 goto error;
350 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
351 goto error;
353 err = xfrm_init_state(x);
354 if (err)
355 goto error;
357 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
358 goto error;
360 x->km.seq = p->seq;
361 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
362 /* sysctl_xfrm_aevent_etime is in 100ms units */
363 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
364 x->preplay.bitmap = 0;
365 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
366 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
368 /* override default values from above */
370 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
371 if (err < 0)
372 goto error;
374 return x;
376 error:
377 x->km.state = XFRM_STATE_DEAD;
378 xfrm_state_put(x);
379 error_no_put:
380 *errp = err;
381 return NULL;
384 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
386 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
387 struct xfrm_state *x;
388 int err;
389 struct km_event c;
391 err = verify_newsa_info(p, (struct rtattr **)xfrma);
392 if (err)
393 return err;
395 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
396 if (!x)
397 return err;
399 xfrm_state_hold(x);
400 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
401 err = xfrm_state_add(x);
402 else
403 err = xfrm_state_update(x);
405 if (err < 0) {
406 x->km.state = XFRM_STATE_DEAD;
407 __xfrm_state_put(x);
408 goto out;
411 c.seq = nlh->nlmsg_seq;
412 c.pid = nlh->nlmsg_pid;
413 c.event = nlh->nlmsg_type;
415 km_state_notify(x, &c);
416 out:
417 xfrm_state_put(x);
418 return err;
421 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
423 struct xfrm_state *x;
424 int err;
425 struct km_event c;
426 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
428 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
429 if (x == NULL)
430 return -ESRCH;
432 if ((err = security_xfrm_state_delete(x)) != 0)
433 goto out;
435 if (xfrm_state_kern(x)) {
436 err = -EPERM;
437 goto out;
440 err = xfrm_state_delete(x);
441 if (err < 0)
442 goto out;
444 c.seq = nlh->nlmsg_seq;
445 c.pid = nlh->nlmsg_pid;
446 c.event = nlh->nlmsg_type;
447 km_state_notify(x, &c);
449 out:
450 xfrm_state_put(x);
451 return err;
454 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
456 memcpy(&p->id, &x->id, sizeof(p->id));
457 memcpy(&p->sel, &x->sel, sizeof(p->sel));
458 memcpy(&p->lft, &x->lft, sizeof(p->lft));
459 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
460 memcpy(&p->stats, &x->stats, sizeof(p->stats));
461 p->saddr = x->props.saddr;
462 p->mode = x->props.mode;
463 p->replay_window = x->props.replay_window;
464 p->reqid = x->props.reqid;
465 p->family = x->props.family;
466 p->flags = x->props.flags;
467 p->seq = x->km.seq;
470 struct xfrm_dump_info {
471 struct sk_buff *in_skb;
472 struct sk_buff *out_skb;
473 u32 nlmsg_seq;
474 u16 nlmsg_flags;
475 int start_idx;
476 int this_idx;
479 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
481 struct xfrm_dump_info *sp = ptr;
482 struct sk_buff *in_skb = sp->in_skb;
483 struct sk_buff *skb = sp->out_skb;
484 struct xfrm_usersa_info *p;
485 struct nlmsghdr *nlh;
486 unsigned char *b = skb->tail;
488 if (sp->this_idx < sp->start_idx)
489 goto out;
491 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
492 sp->nlmsg_seq,
493 XFRM_MSG_NEWSA, sizeof(*p));
494 nlh->nlmsg_flags = sp->nlmsg_flags;
496 p = NLMSG_DATA(nlh);
497 copy_to_user_state(x, p);
499 if (x->aalg)
500 RTA_PUT(skb, XFRMA_ALG_AUTH,
501 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
502 if (x->ealg)
503 RTA_PUT(skb, XFRMA_ALG_CRYPT,
504 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
505 if (x->calg)
506 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
508 if (x->encap)
509 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
511 if (x->security) {
512 int ctx_size = sizeof(struct xfrm_sec_ctx) +
513 x->security->ctx_len;
514 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
515 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
517 uctx->exttype = XFRMA_SEC_CTX;
518 uctx->len = ctx_size;
519 uctx->ctx_doi = x->security->ctx_doi;
520 uctx->ctx_alg = x->security->ctx_alg;
521 uctx->ctx_len = x->security->ctx_len;
522 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
524 nlh->nlmsg_len = skb->tail - b;
525 out:
526 sp->this_idx++;
527 return 0;
529 nlmsg_failure:
530 rtattr_failure:
531 skb_trim(skb, b - skb->data);
532 return -1;
535 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
537 struct xfrm_dump_info info;
539 info.in_skb = cb->skb;
540 info.out_skb = skb;
541 info.nlmsg_seq = cb->nlh->nlmsg_seq;
542 info.nlmsg_flags = NLM_F_MULTI;
543 info.this_idx = 0;
544 info.start_idx = cb->args[0];
545 (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
546 cb->args[0] = info.this_idx;
548 return skb->len;
551 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
552 struct xfrm_state *x, u32 seq)
554 struct xfrm_dump_info info;
555 struct sk_buff *skb;
557 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
558 if (!skb)
559 return ERR_PTR(-ENOMEM);
561 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
562 info.in_skb = in_skb;
563 info.out_skb = skb;
564 info.nlmsg_seq = seq;
565 info.nlmsg_flags = 0;
566 info.this_idx = info.start_idx = 0;
568 if (dump_one_state(x, 0, &info)) {
569 kfree_skb(skb);
570 return NULL;
573 return skb;
576 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
578 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
579 struct xfrm_state *x;
580 struct sk_buff *resp_skb;
581 int err;
583 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
584 err = -ESRCH;
585 if (x == NULL)
586 goto out_noput;
588 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
589 if (IS_ERR(resp_skb)) {
590 err = PTR_ERR(resp_skb);
591 } else {
592 err = netlink_unicast(xfrm_nl, resp_skb,
593 NETLINK_CB(skb).pid, MSG_DONTWAIT);
595 xfrm_state_put(x);
596 out_noput:
597 return err;
600 static int verify_userspi_info(struct xfrm_userspi_info *p)
602 switch (p->info.id.proto) {
603 case IPPROTO_AH:
604 case IPPROTO_ESP:
605 break;
607 case IPPROTO_COMP:
608 /* IPCOMP spi is 16-bits. */
609 if (p->max >= 0x10000)
610 return -EINVAL;
611 break;
613 default:
614 return -EINVAL;
617 if (p->min > p->max)
618 return -EINVAL;
620 return 0;
623 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
625 struct xfrm_state *x;
626 struct xfrm_userspi_info *p;
627 struct sk_buff *resp_skb;
628 xfrm_address_t *daddr;
629 int family;
630 int err;
632 p = NLMSG_DATA(nlh);
633 err = verify_userspi_info(p);
634 if (err)
635 goto out_noput;
637 family = p->info.family;
638 daddr = &p->info.id.daddr;
640 x = NULL;
641 if (p->info.seq) {
642 x = xfrm_find_acq_byseq(p->info.seq);
643 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
644 xfrm_state_put(x);
645 x = NULL;
649 if (!x)
650 x = xfrm_find_acq(p->info.mode, p->info.reqid,
651 p->info.id.proto, daddr,
652 &p->info.saddr, 1,
653 family);
654 err = -ENOENT;
655 if (x == NULL)
656 goto out_noput;
658 resp_skb = ERR_PTR(-ENOENT);
660 spin_lock_bh(&x->lock);
661 if (x->km.state != XFRM_STATE_DEAD) {
662 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
663 if (x->id.spi)
664 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
666 spin_unlock_bh(&x->lock);
668 if (IS_ERR(resp_skb)) {
669 err = PTR_ERR(resp_skb);
670 goto out;
673 err = netlink_unicast(xfrm_nl, resp_skb,
674 NETLINK_CB(skb).pid, MSG_DONTWAIT);
676 out:
677 xfrm_state_put(x);
678 out_noput:
679 return err;
682 static int verify_policy_dir(__u8 dir)
684 switch (dir) {
685 case XFRM_POLICY_IN:
686 case XFRM_POLICY_OUT:
687 case XFRM_POLICY_FWD:
688 break;
690 default:
691 return -EINVAL;
694 return 0;
697 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
699 switch (p->share) {
700 case XFRM_SHARE_ANY:
701 case XFRM_SHARE_SESSION:
702 case XFRM_SHARE_USER:
703 case XFRM_SHARE_UNIQUE:
704 break;
706 default:
707 return -EINVAL;
710 switch (p->action) {
711 case XFRM_POLICY_ALLOW:
712 case XFRM_POLICY_BLOCK:
713 break;
715 default:
716 return -EINVAL;
719 switch (p->sel.family) {
720 case AF_INET:
721 break;
723 case AF_INET6:
724 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
725 break;
726 #else
727 return -EAFNOSUPPORT;
728 #endif
730 default:
731 return -EINVAL;
734 return verify_policy_dir(p->dir);
737 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
739 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
740 struct xfrm_user_sec_ctx *uctx;
742 if (!rt)
743 return 0;
745 uctx = RTA_DATA(rt);
746 return security_xfrm_policy_alloc(pol, uctx);
749 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
750 int nr)
752 int i;
754 xp->xfrm_nr = nr;
755 for (i = 0; i < nr; i++, ut++) {
756 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
758 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
759 memcpy(&t->saddr, &ut->saddr,
760 sizeof(xfrm_address_t));
761 t->reqid = ut->reqid;
762 t->mode = ut->mode;
763 t->share = ut->share;
764 t->optional = ut->optional;
765 t->aalgos = ut->aalgos;
766 t->ealgos = ut->ealgos;
767 t->calgos = ut->calgos;
771 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
773 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
774 struct xfrm_user_tmpl *utmpl;
775 int nr;
777 if (!rt) {
778 pol->xfrm_nr = 0;
779 } else {
780 nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
782 if (nr > XFRM_MAX_DEPTH)
783 return -EINVAL;
785 copy_templates(pol, RTA_DATA(rt), nr);
787 return 0;
790 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
792 xp->priority = p->priority;
793 xp->index = p->index;
794 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
795 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
796 xp->action = p->action;
797 xp->flags = p->flags;
798 xp->family = p->sel.family;
799 /* XXX xp->share = p->share; */
802 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
804 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
805 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
806 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
807 p->priority = xp->priority;
808 p->index = xp->index;
809 p->sel.family = xp->family;
810 p->dir = dir;
811 p->action = xp->action;
812 p->flags = xp->flags;
813 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
816 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
818 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
819 int err;
821 if (!xp) {
822 *errp = -ENOMEM;
823 return NULL;
826 copy_from_user_policy(xp, p);
828 if (!(err = copy_from_user_tmpl(xp, xfrma)))
829 err = copy_from_user_sec_ctx(xp, xfrma);
831 if (err) {
832 *errp = err;
833 kfree(xp);
834 xp = NULL;
837 return xp;
840 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
842 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
843 struct xfrm_policy *xp;
844 struct km_event c;
845 int err;
846 int excl;
848 err = verify_newpolicy_info(p);
849 if (err)
850 return err;
851 err = verify_sec_ctx_len((struct rtattr **)xfrma);
852 if (err)
853 return err;
855 xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
856 if (!xp)
857 return err;
859 /* shouldnt excl be based on nlh flags??
860 * Aha! this is anti-netlink really i.e more pfkey derived
861 * in netlink excl is a flag and you wouldnt need
862 * a type XFRM_MSG_UPDPOLICY - JHS */
863 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
864 err = xfrm_policy_insert(p->dir, xp, excl);
865 if (err) {
866 security_xfrm_policy_free(xp);
867 kfree(xp);
868 return err;
871 c.event = nlh->nlmsg_type;
872 c.seq = nlh->nlmsg_seq;
873 c.pid = nlh->nlmsg_pid;
874 km_policy_notify(xp, p->dir, &c);
876 xfrm_pol_put(xp);
878 return 0;
881 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
883 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
884 int i;
886 if (xp->xfrm_nr == 0)
887 return 0;
889 for (i = 0; i < xp->xfrm_nr; i++) {
890 struct xfrm_user_tmpl *up = &vec[i];
891 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
893 memcpy(&up->id, &kp->id, sizeof(up->id));
894 up->family = xp->family;
895 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
896 up->reqid = kp->reqid;
897 up->mode = kp->mode;
898 up->share = kp->share;
899 up->optional = kp->optional;
900 up->aalgos = kp->aalgos;
901 up->ealgos = kp->ealgos;
902 up->calgos = kp->calgos;
904 RTA_PUT(skb, XFRMA_TMPL,
905 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
906 vec);
908 return 0;
910 rtattr_failure:
911 return -1;
914 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
916 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
917 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
918 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
920 uctx->exttype = XFRMA_SEC_CTX;
921 uctx->len = ctx_size;
922 uctx->ctx_doi = s->ctx_doi;
923 uctx->ctx_alg = s->ctx_alg;
924 uctx->ctx_len = s->ctx_len;
925 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
926 return 0;
928 rtattr_failure:
929 return -1;
932 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
934 if (x->security) {
935 return copy_sec_ctx(x->security, skb);
937 return 0;
940 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
942 if (xp->security) {
943 return copy_sec_ctx(xp->security, skb);
945 return 0;
948 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
950 struct xfrm_dump_info *sp = ptr;
951 struct xfrm_userpolicy_info *p;
952 struct sk_buff *in_skb = sp->in_skb;
953 struct sk_buff *skb = sp->out_skb;
954 struct nlmsghdr *nlh;
955 unsigned char *b = skb->tail;
957 if (sp->this_idx < sp->start_idx)
958 goto out;
960 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
961 sp->nlmsg_seq,
962 XFRM_MSG_NEWPOLICY, sizeof(*p));
963 p = NLMSG_DATA(nlh);
964 nlh->nlmsg_flags = sp->nlmsg_flags;
966 copy_to_user_policy(xp, p, dir);
967 if (copy_to_user_tmpl(xp, skb) < 0)
968 goto nlmsg_failure;
969 if (copy_to_user_sec_ctx(xp, skb))
970 goto nlmsg_failure;
972 nlh->nlmsg_len = skb->tail - b;
973 out:
974 sp->this_idx++;
975 return 0;
977 nlmsg_failure:
978 skb_trim(skb, b - skb->data);
979 return -1;
982 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
984 struct xfrm_dump_info info;
986 info.in_skb = cb->skb;
987 info.out_skb = skb;
988 info.nlmsg_seq = cb->nlh->nlmsg_seq;
989 info.nlmsg_flags = NLM_F_MULTI;
990 info.this_idx = 0;
991 info.start_idx = cb->args[0];
992 (void) xfrm_policy_walk(dump_one_policy, &info);
993 cb->args[0] = info.this_idx;
995 return skb->len;
998 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
999 struct xfrm_policy *xp,
1000 int dir, u32 seq)
1002 struct xfrm_dump_info info;
1003 struct sk_buff *skb;
1005 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1006 if (!skb)
1007 return ERR_PTR(-ENOMEM);
1009 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
1010 info.in_skb = in_skb;
1011 info.out_skb = skb;
1012 info.nlmsg_seq = seq;
1013 info.nlmsg_flags = 0;
1014 info.this_idx = info.start_idx = 0;
1016 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1017 kfree_skb(skb);
1018 return NULL;
1021 return skb;
1024 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1026 struct xfrm_policy *xp;
1027 struct xfrm_userpolicy_id *p;
1028 int err;
1029 struct km_event c;
1030 int delete;
1032 p = NLMSG_DATA(nlh);
1033 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1035 err = verify_policy_dir(p->dir);
1036 if (err)
1037 return err;
1039 if (p->index)
1040 xp = xfrm_policy_byid(p->dir, p->index, delete);
1041 else {
1042 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1043 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1044 struct xfrm_policy tmp;
1046 err = verify_sec_ctx_len(rtattrs);
1047 if (err)
1048 return err;
1050 memset(&tmp, 0, sizeof(struct xfrm_policy));
1051 if (rt) {
1052 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1054 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1055 return err;
1057 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, delete);
1058 security_xfrm_policy_free(&tmp);
1060 if (xp == NULL)
1061 return -ENOENT;
1063 if (!delete) {
1064 struct sk_buff *resp_skb;
1066 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1067 if (IS_ERR(resp_skb)) {
1068 err = PTR_ERR(resp_skb);
1069 } else {
1070 err = netlink_unicast(xfrm_nl, resp_skb,
1071 NETLINK_CB(skb).pid,
1072 MSG_DONTWAIT);
1074 } else {
1075 if ((err = security_xfrm_policy_delete(xp)) != 0)
1076 goto out;
1077 c.data.byid = p->index;
1078 c.event = nlh->nlmsg_type;
1079 c.seq = nlh->nlmsg_seq;
1080 c.pid = nlh->nlmsg_pid;
1081 km_policy_notify(xp, p->dir, &c);
1084 xfrm_pol_put(xp);
1086 out:
1087 return err;
1090 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1092 struct km_event c;
1093 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1095 xfrm_state_flush(p->proto);
1096 c.data.proto = p->proto;
1097 c.event = nlh->nlmsg_type;
1098 c.seq = nlh->nlmsg_seq;
1099 c.pid = nlh->nlmsg_pid;
1100 km_state_notify(NULL, &c);
1102 return 0;
1106 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1108 struct xfrm_aevent_id *id;
1109 struct nlmsghdr *nlh;
1110 struct xfrm_lifetime_cur ltime;
1111 unsigned char *b = skb->tail;
1113 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1114 id = NLMSG_DATA(nlh);
1115 nlh->nlmsg_flags = 0;
1117 id->sa_id.daddr = x->id.daddr;
1118 id->sa_id.spi = x->id.spi;
1119 id->sa_id.family = x->props.family;
1120 id->sa_id.proto = x->id.proto;
1121 id->flags = c->data.aevent;
1123 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1125 ltime.bytes = x->curlft.bytes;
1126 ltime.packets = x->curlft.packets;
1127 ltime.add_time = x->curlft.add_time;
1128 ltime.use_time = x->curlft.use_time;
1130 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
1132 if (id->flags&XFRM_AE_RTHR) {
1133 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1136 if (id->flags&XFRM_AE_ETHR) {
1137 u32 etimer = x->replay_maxage*10/HZ;
1138 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1141 nlh->nlmsg_len = skb->tail - b;
1142 return skb->len;
1144 rtattr_failure:
1145 nlmsg_failure:
1146 skb_trim(skb, b - skb->data);
1147 return -1;
1150 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1152 struct xfrm_state *x;
1153 struct sk_buff *r_skb;
1154 int err;
1155 struct km_event c;
1156 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1157 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1158 struct xfrm_usersa_id *id = &p->sa_id;
1160 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1161 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1163 if (p->flags&XFRM_AE_RTHR)
1164 len+=RTA_SPACE(sizeof(u32));
1166 if (p->flags&XFRM_AE_ETHR)
1167 len+=RTA_SPACE(sizeof(u32));
1169 r_skb = alloc_skb(len, GFP_ATOMIC);
1170 if (r_skb == NULL)
1171 return -ENOMEM;
1173 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1174 if (x == NULL) {
1175 kfree(r_skb);
1176 return -ESRCH;
1180 * XXX: is this lock really needed - none of the other
1181 * gets lock (the concern is things getting updated
1182 * while we are still reading) - jhs
1184 spin_lock_bh(&x->lock);
1185 c.data.aevent = p->flags;
1186 c.seq = nlh->nlmsg_seq;
1187 c.pid = nlh->nlmsg_pid;
1189 if (build_aevent(r_skb, x, &c) < 0)
1190 BUG();
1191 err = netlink_unicast(xfrm_nl, r_skb,
1192 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1193 spin_unlock_bh(&x->lock);
1194 xfrm_state_put(x);
1195 return err;
1198 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1200 struct xfrm_state *x;
1201 struct km_event c;
1202 int err = - EINVAL;
1203 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1204 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1205 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1207 if (!lt && !rp)
1208 return err;
1210 /* pedantic mode - thou shalt sayeth replaceth */
1211 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1212 return err;
1214 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1215 if (x == NULL)
1216 return -ESRCH;
1218 if (x->km.state != XFRM_STATE_VALID)
1219 goto out;
1221 spin_lock_bh(&x->lock);
1222 err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
1223 spin_unlock_bh(&x->lock);
1224 if (err < 0)
1225 goto out;
1227 c.event = nlh->nlmsg_type;
1228 c.seq = nlh->nlmsg_seq;
1229 c.pid = nlh->nlmsg_pid;
1230 c.data.aevent = XFRM_AE_CU;
1231 km_state_notify(x, &c);
1232 err = 0;
1233 out:
1234 xfrm_state_put(x);
1235 return err;
1238 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1240 struct km_event c;
1242 xfrm_policy_flush();
1243 c.event = nlh->nlmsg_type;
1244 c.seq = nlh->nlmsg_seq;
1245 c.pid = nlh->nlmsg_pid;
1246 km_policy_notify(NULL, 0, &c);
1247 return 0;
1250 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1252 struct xfrm_policy *xp;
1253 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1254 struct xfrm_userpolicy_info *p = &up->pol;
1255 int err = -ENOENT;
1257 if (p->index)
1258 xp = xfrm_policy_byid(p->dir, p->index, 0);
1259 else {
1260 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1261 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1262 struct xfrm_policy tmp;
1264 err = verify_sec_ctx_len(rtattrs);
1265 if (err)
1266 return err;
1268 memset(&tmp, 0, sizeof(struct xfrm_policy));
1269 if (rt) {
1270 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1272 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1273 return err;
1275 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, 0);
1276 security_xfrm_policy_free(&tmp);
1279 if (xp == NULL)
1280 return err;
1281 read_lock(&xp->lock);
1282 if (xp->dead) {
1283 read_unlock(&xp->lock);
1284 goto out;
1287 read_unlock(&xp->lock);
1288 err = 0;
1289 if (up->hard) {
1290 xfrm_policy_delete(xp, p->dir);
1291 } else {
1292 // reset the timers here?
1293 printk("Dont know what to do with soft policy expire\n");
1295 km_policy_expired(xp, p->dir, up->hard, current->pid);
1297 out:
1298 xfrm_pol_put(xp);
1299 return err;
1302 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1304 struct xfrm_state *x;
1305 int err;
1306 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1307 struct xfrm_usersa_info *p = &ue->state;
1309 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1310 err = -ENOENT;
1312 if (x == NULL)
1313 return err;
1315 err = -EINVAL;
1317 spin_lock_bh(&x->lock);
1318 if (x->km.state != XFRM_STATE_VALID)
1319 goto out;
1320 km_state_expired(x, ue->hard, current->pid);
1322 if (ue->hard)
1323 __xfrm_state_delete(x);
1324 out:
1325 spin_unlock_bh(&x->lock);
1326 xfrm_state_put(x);
1327 return err;
1330 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1332 struct xfrm_policy *xp;
1333 struct xfrm_user_tmpl *ut;
1334 int i;
1335 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1337 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1338 struct xfrm_state *x = xfrm_state_alloc();
1339 int err = -ENOMEM;
1341 if (!x)
1342 return err;
1344 err = verify_newpolicy_info(&ua->policy);
1345 if (err) {
1346 printk("BAD policy passed\n");
1347 kfree(x);
1348 return err;
1351 /* build an XP */
1352 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err); if (!xp) {
1353 kfree(x);
1354 return err;
1357 memcpy(&x->id, &ua->id, sizeof(ua->id));
1358 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1359 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1361 ut = RTA_DATA(rt);
1362 /* extract the templates and for each call km_key */
1363 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1364 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1365 memcpy(&x->id, &t->id, sizeof(x->id));
1366 x->props.mode = t->mode;
1367 x->props.reqid = t->reqid;
1368 x->props.family = ut->family;
1369 t->aalgos = ua->aalgos;
1370 t->ealgos = ua->ealgos;
1371 t->calgos = ua->calgos;
1372 err = km_query(x, t, xp);
1376 kfree(x);
1377 kfree(xp);
1379 return 0;
1383 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1385 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1386 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1387 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1388 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1389 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1390 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1391 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1392 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1393 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1394 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1395 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1396 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1397 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1398 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1399 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
1400 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1401 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1404 #undef XMSGSIZE
1406 static struct xfrm_link {
1407 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1408 int (*dump)(struct sk_buff *, struct netlink_callback *);
1409 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1410 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1411 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1412 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1413 .dump = xfrm_dump_sa },
1414 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1415 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1416 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1417 .dump = xfrm_dump_policy },
1418 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1419 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
1420 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1421 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1422 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1423 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1424 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1425 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
1426 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1427 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1430 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1432 struct rtattr *xfrma[XFRMA_MAX];
1433 struct xfrm_link *link;
1434 int type, min_len;
1436 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1437 return 0;
1439 type = nlh->nlmsg_type;
1441 /* A control message: ignore them */
1442 if (type < XFRM_MSG_BASE)
1443 return 0;
1445 /* Unknown message: reply with EINVAL */
1446 if (type > XFRM_MSG_MAX)
1447 goto err_einval;
1449 type -= XFRM_MSG_BASE;
1450 link = &xfrm_dispatch[type];
1452 /* All operations require privileges, even GET */
1453 if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
1454 *errp = -EPERM;
1455 return -1;
1458 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1459 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1460 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1461 if (link->dump == NULL)
1462 goto err_einval;
1464 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
1465 link->dump, NULL)) != 0) {
1466 return -1;
1469 netlink_queue_skip(nlh, skb);
1470 return -1;
1473 memset(xfrma, 0, sizeof(xfrma));
1475 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1476 goto err_einval;
1478 if (nlh->nlmsg_len > min_len) {
1479 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1480 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1482 while (RTA_OK(attr, attrlen)) {
1483 unsigned short flavor = attr->rta_type;
1484 if (flavor) {
1485 if (flavor > XFRMA_MAX)
1486 goto err_einval;
1487 xfrma[flavor - 1] = attr;
1489 attr = RTA_NEXT(attr, attrlen);
1493 if (link->doit == NULL)
1494 goto err_einval;
1495 *errp = link->doit(skb, nlh, (void **) &xfrma);
1497 return *errp;
1499 err_einval:
1500 *errp = -EINVAL;
1501 return -1;
1504 static void xfrm_netlink_rcv(struct sock *sk, int len)
1506 unsigned int qlen = 0;
1508 do {
1509 mutex_lock(&xfrm_cfg_mutex);
1510 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
1511 mutex_unlock(&xfrm_cfg_mutex);
1513 } while (qlen);
1516 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1518 struct xfrm_user_expire *ue;
1519 struct nlmsghdr *nlh;
1520 unsigned char *b = skb->tail;
1522 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
1523 sizeof(*ue));
1524 ue = NLMSG_DATA(nlh);
1525 nlh->nlmsg_flags = 0;
1527 copy_to_user_state(x, &ue->state);
1528 ue->hard = (c->data.hard != 0) ? 1 : 0;
1530 nlh->nlmsg_len = skb->tail - b;
1531 return skb->len;
1533 nlmsg_failure:
1534 skb_trim(skb, b - skb->data);
1535 return -1;
1538 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1540 struct sk_buff *skb;
1541 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
1543 skb = alloc_skb(len, GFP_ATOMIC);
1544 if (skb == NULL)
1545 return -ENOMEM;
1547 if (build_expire(skb, x, c) < 0)
1548 BUG();
1550 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1551 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1554 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1556 struct sk_buff *skb;
1557 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1559 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1560 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1561 skb = alloc_skb(len, GFP_ATOMIC);
1562 if (skb == NULL)
1563 return -ENOMEM;
1565 if (build_aevent(skb, x, c) < 0)
1566 BUG();
1568 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1569 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1572 static int xfrm_notify_sa_flush(struct km_event *c)
1574 struct xfrm_usersa_flush *p;
1575 struct nlmsghdr *nlh;
1576 struct sk_buff *skb;
1577 unsigned char *b;
1578 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1580 skb = alloc_skb(len, GFP_ATOMIC);
1581 if (skb == NULL)
1582 return -ENOMEM;
1583 b = skb->tail;
1585 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1586 XFRM_MSG_FLUSHSA, sizeof(*p));
1587 nlh->nlmsg_flags = 0;
1589 p = NLMSG_DATA(nlh);
1590 p->proto = c->data.proto;
1592 nlh->nlmsg_len = skb->tail - b;
1594 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1595 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1597 nlmsg_failure:
1598 kfree_skb(skb);
1599 return -1;
1602 static int inline xfrm_sa_len(struct xfrm_state *x)
1604 int l = 0;
1605 if (x->aalg)
1606 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1607 if (x->ealg)
1608 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1609 if (x->calg)
1610 l += RTA_SPACE(sizeof(*x->calg));
1611 if (x->encap)
1612 l += RTA_SPACE(sizeof(*x->encap));
1614 return l;
1617 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1619 struct xfrm_usersa_info *p;
1620 struct xfrm_usersa_id *id;
1621 struct nlmsghdr *nlh;
1622 struct sk_buff *skb;
1623 unsigned char *b;
1624 int len = xfrm_sa_len(x);
1625 int headlen;
1627 headlen = sizeof(*p);
1628 if (c->event == XFRM_MSG_DELSA) {
1629 len += RTA_SPACE(headlen);
1630 headlen = sizeof(*id);
1632 len += NLMSG_SPACE(headlen);
1634 skb = alloc_skb(len, GFP_ATOMIC);
1635 if (skb == NULL)
1636 return -ENOMEM;
1637 b = skb->tail;
1639 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
1640 nlh->nlmsg_flags = 0;
1642 p = NLMSG_DATA(nlh);
1643 if (c->event == XFRM_MSG_DELSA) {
1644 id = NLMSG_DATA(nlh);
1645 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1646 id->spi = x->id.spi;
1647 id->family = x->props.family;
1648 id->proto = x->id.proto;
1650 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1653 copy_to_user_state(x, p);
1655 if (x->aalg)
1656 RTA_PUT(skb, XFRMA_ALG_AUTH,
1657 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1658 if (x->ealg)
1659 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1660 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1661 if (x->calg)
1662 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1664 if (x->encap)
1665 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1667 nlh->nlmsg_len = skb->tail - b;
1669 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1670 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1672 nlmsg_failure:
1673 rtattr_failure:
1674 kfree_skb(skb);
1675 return -1;
1678 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1681 switch (c->event) {
1682 case XFRM_MSG_EXPIRE:
1683 return xfrm_exp_state_notify(x, c);
1684 case XFRM_MSG_NEWAE:
1685 return xfrm_aevent_state_notify(x, c);
1686 case XFRM_MSG_DELSA:
1687 case XFRM_MSG_UPDSA:
1688 case XFRM_MSG_NEWSA:
1689 return xfrm_notify_sa(x, c);
1690 case XFRM_MSG_FLUSHSA:
1691 return xfrm_notify_sa_flush(c);
1692 default:
1693 printk("xfrm_user: Unknown SA event %d\n", c->event);
1694 break;
1697 return 0;
1701 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1702 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1703 int dir)
1705 struct xfrm_user_acquire *ua;
1706 struct nlmsghdr *nlh;
1707 unsigned char *b = skb->tail;
1708 __u32 seq = xfrm_get_acqseq();
1710 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1711 sizeof(*ua));
1712 ua = NLMSG_DATA(nlh);
1713 nlh->nlmsg_flags = 0;
1715 memcpy(&ua->id, &x->id, sizeof(ua->id));
1716 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1717 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1718 copy_to_user_policy(xp, &ua->policy, dir);
1719 ua->aalgos = xt->aalgos;
1720 ua->ealgos = xt->ealgos;
1721 ua->calgos = xt->calgos;
1722 ua->seq = x->km.seq = seq;
1724 if (copy_to_user_tmpl(xp, skb) < 0)
1725 goto nlmsg_failure;
1726 if (copy_to_user_state_sec_ctx(x, skb))
1727 goto nlmsg_failure;
1729 nlh->nlmsg_len = skb->tail - b;
1730 return skb->len;
1732 nlmsg_failure:
1733 skb_trim(skb, b - skb->data);
1734 return -1;
1737 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1738 struct xfrm_policy *xp, int dir)
1740 struct sk_buff *skb;
1741 size_t len;
1743 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1744 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
1745 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1746 skb = alloc_skb(len, GFP_ATOMIC);
1747 if (skb == NULL)
1748 return -ENOMEM;
1750 if (build_acquire(skb, x, xt, xp, dir) < 0)
1751 BUG();
1753 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
1754 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
1757 /* User gives us xfrm_user_policy_info followed by an array of 0
1758 * or more templates.
1760 static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
1761 u8 *data, int len, int *dir)
1763 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1764 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1765 struct xfrm_policy *xp;
1766 int nr;
1768 switch (family) {
1769 case AF_INET:
1770 if (opt != IP_XFRM_POLICY) {
1771 *dir = -EOPNOTSUPP;
1772 return NULL;
1774 break;
1775 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1776 case AF_INET6:
1777 if (opt != IPV6_XFRM_POLICY) {
1778 *dir = -EOPNOTSUPP;
1779 return NULL;
1781 break;
1782 #endif
1783 default:
1784 *dir = -EINVAL;
1785 return NULL;
1788 *dir = -EINVAL;
1790 if (len < sizeof(*p) ||
1791 verify_newpolicy_info(p))
1792 return NULL;
1794 nr = ((len - sizeof(*p)) / sizeof(*ut));
1795 if (nr > XFRM_MAX_DEPTH)
1796 return NULL;
1798 if (p->dir > XFRM_POLICY_OUT)
1799 return NULL;
1801 xp = xfrm_policy_alloc(GFP_KERNEL);
1802 if (xp == NULL) {
1803 *dir = -ENOBUFS;
1804 return NULL;
1807 copy_from_user_policy(xp, p);
1808 copy_templates(xp, ut, nr);
1810 *dir = p->dir;
1812 return xp;
1815 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
1816 int dir, struct km_event *c)
1818 struct xfrm_user_polexpire *upe;
1819 struct nlmsghdr *nlh;
1820 int hard = c->data.hard;
1821 unsigned char *b = skb->tail;
1823 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
1824 upe = NLMSG_DATA(nlh);
1825 nlh->nlmsg_flags = 0;
1827 copy_to_user_policy(xp, &upe->pol, dir);
1828 if (copy_to_user_tmpl(xp, skb) < 0)
1829 goto nlmsg_failure;
1830 if (copy_to_user_sec_ctx(xp, skb))
1831 goto nlmsg_failure;
1832 upe->hard = !!hard;
1834 nlh->nlmsg_len = skb->tail - b;
1835 return skb->len;
1837 nlmsg_failure:
1838 skb_trim(skb, b - skb->data);
1839 return -1;
1842 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1844 struct sk_buff *skb;
1845 size_t len;
1847 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1848 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
1849 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1850 skb = alloc_skb(len, GFP_ATOMIC);
1851 if (skb == NULL)
1852 return -ENOMEM;
1854 if (build_polexpire(skb, xp, dir, c) < 0)
1855 BUG();
1857 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1858 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1861 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
1863 struct xfrm_userpolicy_info *p;
1864 struct xfrm_userpolicy_id *id;
1865 struct nlmsghdr *nlh;
1866 struct sk_buff *skb;
1867 unsigned char *b;
1868 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1869 int headlen;
1871 headlen = sizeof(*p);
1872 if (c->event == XFRM_MSG_DELPOLICY) {
1873 len += RTA_SPACE(headlen);
1874 headlen = sizeof(*id);
1876 len += NLMSG_SPACE(headlen);
1878 skb = alloc_skb(len, GFP_ATOMIC);
1879 if (skb == NULL)
1880 return -ENOMEM;
1881 b = skb->tail;
1883 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
1885 p = NLMSG_DATA(nlh);
1886 if (c->event == XFRM_MSG_DELPOLICY) {
1887 id = NLMSG_DATA(nlh);
1888 memset(id, 0, sizeof(*id));
1889 id->dir = dir;
1890 if (c->data.byid)
1891 id->index = xp->index;
1892 else
1893 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
1895 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
1898 nlh->nlmsg_flags = 0;
1900 copy_to_user_policy(xp, p, dir);
1901 if (copy_to_user_tmpl(xp, skb) < 0)
1902 goto nlmsg_failure;
1904 nlh->nlmsg_len = skb->tail - b;
1906 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1907 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
1909 nlmsg_failure:
1910 rtattr_failure:
1911 kfree_skb(skb);
1912 return -1;
1915 static int xfrm_notify_policy_flush(struct km_event *c)
1917 struct nlmsghdr *nlh;
1918 struct sk_buff *skb;
1919 unsigned char *b;
1920 int len = NLMSG_LENGTH(0);
1922 skb = alloc_skb(len, GFP_ATOMIC);
1923 if (skb == NULL)
1924 return -ENOMEM;
1925 b = skb->tail;
1928 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
1930 nlh->nlmsg_len = skb->tail - b;
1932 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1933 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
1935 nlmsg_failure:
1936 kfree_skb(skb);
1937 return -1;
1940 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1943 switch (c->event) {
1944 case XFRM_MSG_NEWPOLICY:
1945 case XFRM_MSG_UPDPOLICY:
1946 case XFRM_MSG_DELPOLICY:
1947 return xfrm_notify_policy(xp, dir, c);
1948 case XFRM_MSG_FLUSHPOLICY:
1949 return xfrm_notify_policy_flush(c);
1950 case XFRM_MSG_POLEXPIRE:
1951 return xfrm_exp_policy_notify(xp, dir, c);
1952 default:
1953 printk("xfrm_user: Unknown Policy event %d\n", c->event);
1956 return 0;
1960 static struct xfrm_mgr netlink_mgr = {
1961 .id = "netlink",
1962 .notify = xfrm_send_state_notify,
1963 .acquire = xfrm_send_acquire,
1964 .compile_policy = xfrm_compile_policy,
1965 .notify_policy = xfrm_send_policy_notify,
1968 static int __init xfrm_user_init(void)
1970 struct sock *nlsk;
1972 printk(KERN_INFO "Initializing IPsec netlink socket\n");
1974 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
1975 xfrm_netlink_rcv, THIS_MODULE);
1976 if (nlsk == NULL)
1977 return -ENOMEM;
1978 rcu_assign_pointer(xfrm_nl, nlsk);
1980 xfrm_register_km(&netlink_mgr);
1982 return 0;
1985 static void __exit xfrm_user_exit(void)
1987 struct sock *nlsk = xfrm_nl;
1989 xfrm_unregister_km(&netlink_mgr);
1990 rcu_assign_pointer(xfrm_nl, NULL);
1991 synchronize_rcu();
1992 sock_release(nlsk->sk_socket);
1995 module_init(xfrm_user_init);
1996 module_exit(xfrm_user_exit);
1997 MODULE_LICENSE("GPL");
1998 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);