[PATCH] Scheduled removal of SA_xxx interrupt flags fixups 3
[linux-2.6/kvm.git] / net / xfrm / xfrm_user.c
blob956cfe0ff7f82b91067fae393ae9085538ad5972
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>
31 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
32 #include <linux/in6.h>
33 #endif
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;
40 int len;
42 if (!rt)
43 return 0;
45 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
46 if (len < 0)
47 return -EINVAL;
49 algp = RTA_DATA(rt);
51 len -= (algp->alg_key_len + 7U) / 8;
52 if (len < 0)
53 return -EINVAL;
55 switch (type) {
56 case XFRMA_ALG_AUTH:
57 if (!algp->alg_key_len &&
58 strcmp(algp->alg_name, "digest_null") != 0)
59 return -EINVAL;
60 break;
62 case XFRMA_ALG_CRYPT:
63 if (!algp->alg_key_len &&
64 strcmp(algp->alg_name, "cipher_null") != 0)
65 return -EINVAL;
66 break;
68 case XFRMA_ALG_COMP:
69 /* Zero length keys are legal. */
70 break;
72 default:
73 return -EINVAL;
76 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
77 return 0;
80 static int verify_encap_tmpl(struct rtattr **xfrma)
82 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
83 struct xfrm_encap_tmpl *encap;
85 if (!rt)
86 return 0;
88 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
89 return -EINVAL;
91 return 0;
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];
99 if (!rt)
100 return 0;
102 if ((rt->rta_len - sizeof(*rt)) < sizeof(**addrp))
103 return -EINVAL;
105 if (addrp)
106 *addrp = RTA_DATA(rt);
108 return 0;
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;
115 int len = 0;
117 if (!rt)
118 return 0;
120 if (rt->rta_len < sizeof(*uctx))
121 return -EINVAL;
123 uctx = RTA_DATA(rt);
125 len += sizeof(struct xfrm_user_sec_ctx);
126 len += uctx->ctx_len;
128 if (uctx->len != len)
129 return -EINVAL;
131 return 0;
135 static int verify_newsa_info(struct xfrm_usersa_info *p,
136 struct rtattr **xfrma)
138 int err;
140 err = -EINVAL;
141 switch (p->family) {
142 case AF_INET:
143 break;
145 case AF_INET6:
146 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
147 break;
148 #else
149 err = -EAFNOSUPPORT;
150 goto out;
151 #endif
153 default:
154 goto out;
157 err = -EINVAL;
158 switch (p->id.proto) {
159 case IPPROTO_AH:
160 if (!xfrma[XFRMA_ALG_AUTH-1] ||
161 xfrma[XFRMA_ALG_CRYPT-1] ||
162 xfrma[XFRMA_ALG_COMP-1])
163 goto out;
164 break;
166 case IPPROTO_ESP:
167 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
168 !xfrma[XFRMA_ALG_CRYPT-1]) ||
169 xfrma[XFRMA_ALG_COMP-1])
170 goto out;
171 break;
173 case IPPROTO_COMP:
174 if (!xfrma[XFRMA_ALG_COMP-1] ||
175 xfrma[XFRMA_ALG_AUTH-1] ||
176 xfrma[XFRMA_ALG_CRYPT-1])
177 goto out;
178 break;
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])
189 goto out;
190 break;
191 #endif
193 default:
194 goto out;
197 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
198 goto out;
199 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
200 goto out;
201 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
202 goto out;
203 if ((err = verify_encap_tmpl(xfrma)))
204 goto out;
205 if ((err = verify_sec_ctx_len(xfrma)))
206 goto out;
207 if ((err = verify_one_addr(xfrma, XFRMA_COADDR, NULL)))
208 goto out;
210 err = -EINVAL;
211 switch (p->mode) {
212 case XFRM_MODE_TRANSPORT:
213 case XFRM_MODE_TUNNEL:
214 case XFRM_MODE_ROUTEOPTIMIZATION:
215 case XFRM_MODE_BEET:
216 break;
218 default:
219 goto out;
222 err = 0;
224 out:
225 return err;
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;
235 int len;
237 if (!rta)
238 return 0;
240 ualg = RTA_DATA(rta);
242 algo = get_byname(ualg->alg_name, 1);
243 if (!algo)
244 return -ENOSYS;
245 *props = algo->desc.sadb_alg_id;
247 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
248 p = kmemdup(ualg, len, GFP_KERNEL);
249 if (!p)
250 return -ENOMEM;
252 strcpy(p->alg_name, algo->name);
253 *algpp = p;
254 return 0;
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;
262 if (!rta)
263 return 0;
265 uencap = RTA_DATA(rta);
266 p = kmemdup(uencap, sizeof(*p), GFP_KERNEL);
267 if (!p)
268 return -ENOMEM;
270 *encapp = p;
271 return 0;
275 static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
277 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
278 int len = 0;
280 if (xfrm_ctx) {
281 len += sizeof(struct xfrm_user_sec_ctx);
282 len += xfrm_ctx->ctx_len;
284 return len;
287 static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
289 struct xfrm_user_sec_ctx *uctx;
291 if (!u_arg)
292 return 0;
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;
303 if (!rta)
304 return 0;
306 uaddrp = RTA_DATA(rta);
307 p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
308 if (!p)
309 return -ENOMEM;
311 *addrpp = p;
312 return 0;
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)
335 int err = - EINVAL;
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];
341 if (rp) {
342 struct xfrm_replay_state *replay;
343 if (RTA_PAYLOAD(rp) < sizeof(*replay))
344 goto error;
345 replay = RTA_DATA(rp);
346 memcpy(&x->replay, replay, sizeof(*replay));
347 memcpy(&x->preplay, replay, sizeof(*replay));
350 if (lt) {
351 struct xfrm_lifetime_cur *ltime;
352 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
353 goto error;
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;
361 if (et) {
362 if (RTA_PAYLOAD(et) < sizeof(u32))
363 goto error;
364 x->replay_maxage = *(u32*)RTA_DATA(et);
367 if (rt) {
368 if (RTA_PAYLOAD(rt) < sizeof(u32))
369 goto error;
370 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
373 return 0;
374 error:
375 return err;
378 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
379 struct rtattr **xfrma,
380 int *errp)
382 struct xfrm_state *x = xfrm_state_alloc();
383 int err = -ENOMEM;
385 if (!x)
386 goto error_no_put;
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])))
393 goto error;
394 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
395 xfrm_ealg_get_byname,
396 xfrma[XFRMA_ALG_CRYPT-1])))
397 goto error;
398 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
399 xfrm_calg_get_byname,
400 xfrma[XFRMA_ALG_COMP-1])))
401 goto error;
402 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
403 goto error;
404 if ((err = attach_one_addr(&x->coaddr, xfrma[XFRMA_COADDR-1])))
405 goto error;
406 err = xfrm_init_state(x);
407 if (err)
408 goto error;
410 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
411 goto error;
413 x->km.seq = p->seq;
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);
424 if (err < 0)
425 goto error;
427 return x;
429 error:
430 x->km.state = XFRM_STATE_DEAD;
431 xfrm_state_put(x);
432 error_no_put:
433 *errp = err;
434 return NULL;
437 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
438 struct rtattr **xfrma)
440 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
441 struct xfrm_state *x;
442 int err;
443 struct km_event c;
445 err = verify_newsa_info(p, xfrma);
446 if (err)
447 return err;
449 x = xfrm_state_construct(p, xfrma, &err);
450 if (!x)
451 return err;
453 xfrm_state_hold(x);
454 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
455 err = xfrm_state_add(x);
456 else
457 err = xfrm_state_update(x);
459 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
460 AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
462 if (err < 0) {
463 x->km.state = XFRM_STATE_DEAD;
464 __xfrm_state_put(x);
465 goto out;
468 c.seq = nlh->nlmsg_seq;
469 c.pid = nlh->nlmsg_pid;
470 c.event = nlh->nlmsg_type;
472 km_state_notify(x, &c);
473 out:
474 xfrm_state_put(x);
475 return err;
478 static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
479 struct rtattr **xfrma,
480 int *errp)
482 struct xfrm_state *x = NULL;
483 int err;
485 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
486 err = -ESRCH;
487 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
488 } else {
489 xfrm_address_t *saddr = NULL;
491 err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr);
492 if (err)
493 goto out;
495 if (!saddr) {
496 err = -EINVAL;
497 goto out;
500 err = -ESRCH;
501 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
502 p->family);
505 out:
506 if (!x && errp)
507 *errp = err;
508 return x;
511 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
512 struct rtattr **xfrma)
514 struct xfrm_state *x;
515 int err = -ESRCH;
516 struct km_event c;
517 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
519 x = xfrm_user_state_lookup(p, xfrma, &err);
520 if (x == NULL)
521 return err;
523 if ((err = security_xfrm_state_delete(x)) != 0)
524 goto out;
526 if (xfrm_state_kern(x)) {
527 err = -EPERM;
528 goto out;
531 err = xfrm_state_delete(x);
533 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
534 AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
536 if (err < 0)
537 goto out;
539 c.seq = nlh->nlmsg_seq;
540 c.pid = nlh->nlmsg_pid;
541 c.event = nlh->nlmsg_type;
542 km_state_notify(x, &c);
544 out:
545 xfrm_state_put(x);
546 return err;
549 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
551 memcpy(&p->id, &x->id, sizeof(p->id));
552 memcpy(&p->sel, &x->sel, sizeof(p->sel));
553 memcpy(&p->lft, &x->lft, sizeof(p->lft));
554 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
555 memcpy(&p->stats, &x->stats, sizeof(p->stats));
556 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
557 p->mode = x->props.mode;
558 p->replay_window = x->props.replay_window;
559 p->reqid = x->props.reqid;
560 p->family = x->props.family;
561 p->flags = x->props.flags;
562 p->seq = x->km.seq;
565 struct xfrm_dump_info {
566 struct sk_buff *in_skb;
567 struct sk_buff *out_skb;
568 u32 nlmsg_seq;
569 u16 nlmsg_flags;
570 int start_idx;
571 int this_idx;
574 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
576 struct xfrm_dump_info *sp = ptr;
577 struct sk_buff *in_skb = sp->in_skb;
578 struct sk_buff *skb = sp->out_skb;
579 struct xfrm_usersa_info *p;
580 struct nlmsghdr *nlh;
581 unsigned char *b = skb->tail;
583 if (sp->this_idx < sp->start_idx)
584 goto out;
586 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
587 sp->nlmsg_seq,
588 XFRM_MSG_NEWSA, sizeof(*p));
589 nlh->nlmsg_flags = sp->nlmsg_flags;
591 p = NLMSG_DATA(nlh);
592 copy_to_user_state(x, p);
594 if (x->aalg)
595 RTA_PUT(skb, XFRMA_ALG_AUTH,
596 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
597 if (x->ealg)
598 RTA_PUT(skb, XFRMA_ALG_CRYPT,
599 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
600 if (x->calg)
601 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
603 if (x->encap)
604 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
606 if (x->security) {
607 int ctx_size = sizeof(struct xfrm_sec_ctx) +
608 x->security->ctx_len;
609 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
610 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
612 uctx->exttype = XFRMA_SEC_CTX;
613 uctx->len = ctx_size;
614 uctx->ctx_doi = x->security->ctx_doi;
615 uctx->ctx_alg = x->security->ctx_alg;
616 uctx->ctx_len = x->security->ctx_len;
617 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
620 if (x->coaddr)
621 RTA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
623 if (x->lastused)
624 RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused);
626 nlh->nlmsg_len = skb->tail - b;
627 out:
628 sp->this_idx++;
629 return 0;
631 nlmsg_failure:
632 rtattr_failure:
633 skb_trim(skb, b - skb->data);
634 return -1;
637 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
639 struct xfrm_dump_info info;
641 info.in_skb = cb->skb;
642 info.out_skb = skb;
643 info.nlmsg_seq = cb->nlh->nlmsg_seq;
644 info.nlmsg_flags = NLM_F_MULTI;
645 info.this_idx = 0;
646 info.start_idx = cb->args[0];
647 (void) xfrm_state_walk(0, dump_one_state, &info);
648 cb->args[0] = info.this_idx;
650 return skb->len;
653 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
654 struct xfrm_state *x, u32 seq)
656 struct xfrm_dump_info info;
657 struct sk_buff *skb;
659 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
660 if (!skb)
661 return ERR_PTR(-ENOMEM);
663 info.in_skb = in_skb;
664 info.out_skb = skb;
665 info.nlmsg_seq = seq;
666 info.nlmsg_flags = 0;
667 info.this_idx = info.start_idx = 0;
669 if (dump_one_state(x, 0, &info)) {
670 kfree_skb(skb);
671 return NULL;
674 return skb;
677 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
678 struct rtattr **xfrma)
680 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
681 struct xfrm_state *x;
682 struct sk_buff *resp_skb;
683 int err = -ESRCH;
685 x = xfrm_user_state_lookup(p, xfrma, &err);
686 if (x == NULL)
687 goto out_noput;
689 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
690 if (IS_ERR(resp_skb)) {
691 err = PTR_ERR(resp_skb);
692 } else {
693 err = netlink_unicast(xfrm_nl, resp_skb,
694 NETLINK_CB(skb).pid, MSG_DONTWAIT);
696 xfrm_state_put(x);
697 out_noput:
698 return err;
701 static int verify_userspi_info(struct xfrm_userspi_info *p)
703 switch (p->info.id.proto) {
704 case IPPROTO_AH:
705 case IPPROTO_ESP:
706 break;
708 case IPPROTO_COMP:
709 /* IPCOMP spi is 16-bits. */
710 if (p->max >= 0x10000)
711 return -EINVAL;
712 break;
714 default:
715 return -EINVAL;
718 if (p->min > p->max)
719 return -EINVAL;
721 return 0;
724 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
725 struct rtattr **xfrma)
727 struct xfrm_state *x;
728 struct xfrm_userspi_info *p;
729 struct sk_buff *resp_skb;
730 xfrm_address_t *daddr;
731 int family;
732 int err;
734 p = NLMSG_DATA(nlh);
735 err = verify_userspi_info(p);
736 if (err)
737 goto out_noput;
739 family = p->info.family;
740 daddr = &p->info.id.daddr;
742 x = NULL;
743 if (p->info.seq) {
744 x = xfrm_find_acq_byseq(p->info.seq);
745 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
746 xfrm_state_put(x);
747 x = NULL;
751 if (!x)
752 x = xfrm_find_acq(p->info.mode, p->info.reqid,
753 p->info.id.proto, daddr,
754 &p->info.saddr, 1,
755 family);
756 err = -ENOENT;
757 if (x == NULL)
758 goto out_noput;
760 resp_skb = ERR_PTR(-ENOENT);
762 spin_lock_bh(&x->lock);
763 if (x->km.state != XFRM_STATE_DEAD) {
764 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
765 if (x->id.spi)
766 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
768 spin_unlock_bh(&x->lock);
770 if (IS_ERR(resp_skb)) {
771 err = PTR_ERR(resp_skb);
772 goto out;
775 err = netlink_unicast(xfrm_nl, resp_skb,
776 NETLINK_CB(skb).pid, MSG_DONTWAIT);
778 out:
779 xfrm_state_put(x);
780 out_noput:
781 return err;
784 static int verify_policy_dir(u8 dir)
786 switch (dir) {
787 case XFRM_POLICY_IN:
788 case XFRM_POLICY_OUT:
789 case XFRM_POLICY_FWD:
790 break;
792 default:
793 return -EINVAL;
796 return 0;
799 static int verify_policy_type(u8 type)
801 switch (type) {
802 case XFRM_POLICY_TYPE_MAIN:
803 #ifdef CONFIG_XFRM_SUB_POLICY
804 case XFRM_POLICY_TYPE_SUB:
805 #endif
806 break;
808 default:
809 return -EINVAL;
812 return 0;
815 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
817 switch (p->share) {
818 case XFRM_SHARE_ANY:
819 case XFRM_SHARE_SESSION:
820 case XFRM_SHARE_USER:
821 case XFRM_SHARE_UNIQUE:
822 break;
824 default:
825 return -EINVAL;
828 switch (p->action) {
829 case XFRM_POLICY_ALLOW:
830 case XFRM_POLICY_BLOCK:
831 break;
833 default:
834 return -EINVAL;
837 switch (p->sel.family) {
838 case AF_INET:
839 break;
841 case AF_INET6:
842 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
843 break;
844 #else
845 return -EAFNOSUPPORT;
846 #endif
848 default:
849 return -EINVAL;
852 return verify_policy_dir(p->dir);
855 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
857 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
858 struct xfrm_user_sec_ctx *uctx;
860 if (!rt)
861 return 0;
863 uctx = RTA_DATA(rt);
864 return security_xfrm_policy_alloc(pol, uctx);
867 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
868 int nr)
870 int i;
872 xp->xfrm_nr = nr;
873 for (i = 0; i < nr; i++, ut++) {
874 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
876 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
877 memcpy(&t->saddr, &ut->saddr,
878 sizeof(xfrm_address_t));
879 t->reqid = ut->reqid;
880 t->mode = ut->mode;
881 t->share = ut->share;
882 t->optional = ut->optional;
883 t->aalgos = ut->aalgos;
884 t->ealgos = ut->ealgos;
885 t->calgos = ut->calgos;
886 t->encap_family = ut->family;
890 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
892 int i;
894 if (nr > XFRM_MAX_DEPTH)
895 return -EINVAL;
897 for (i = 0; i < nr; i++) {
898 /* We never validated the ut->family value, so many
899 * applications simply leave it at zero. The check was
900 * never made and ut->family was ignored because all
901 * templates could be assumed to have the same family as
902 * the policy itself. Now that we will have ipv4-in-ipv6
903 * and ipv6-in-ipv4 tunnels, this is no longer true.
905 if (!ut[i].family)
906 ut[i].family = family;
908 switch (ut[i].family) {
909 case AF_INET:
910 break;
911 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
912 case AF_INET6:
913 break;
914 #endif
915 default:
916 return -EINVAL;
920 return 0;
923 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
925 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
927 if (!rt) {
928 pol->xfrm_nr = 0;
929 } else {
930 struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
931 int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
932 int err;
934 err = validate_tmpl(nr, utmpl, pol->family);
935 if (err)
936 return err;
938 copy_templates(pol, RTA_DATA(rt), nr);
940 return 0;
943 static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
945 struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
946 struct xfrm_userpolicy_type *upt;
947 u8 type = XFRM_POLICY_TYPE_MAIN;
948 int err;
950 if (rt) {
951 if (rt->rta_len < sizeof(*upt))
952 return -EINVAL;
954 upt = RTA_DATA(rt);
955 type = upt->type;
958 err = verify_policy_type(type);
959 if (err)
960 return err;
962 *tp = type;
963 return 0;
966 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
968 xp->priority = p->priority;
969 xp->index = p->index;
970 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
971 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
972 xp->action = p->action;
973 xp->flags = p->flags;
974 xp->family = p->sel.family;
975 /* XXX xp->share = p->share; */
978 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
980 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
981 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
982 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
983 p->priority = xp->priority;
984 p->index = xp->index;
985 p->sel.family = xp->family;
986 p->dir = dir;
987 p->action = xp->action;
988 p->flags = xp->flags;
989 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
992 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
994 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
995 int err;
997 if (!xp) {
998 *errp = -ENOMEM;
999 return NULL;
1002 copy_from_user_policy(xp, p);
1004 err = copy_from_user_policy_type(&xp->type, xfrma);
1005 if (err)
1006 goto error;
1008 if (!(err = copy_from_user_tmpl(xp, xfrma)))
1009 err = copy_from_user_sec_ctx(xp, xfrma);
1010 if (err)
1011 goto error;
1013 return xp;
1014 error:
1015 *errp = err;
1016 kfree(xp);
1017 return NULL;
1020 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1021 struct rtattr **xfrma)
1023 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
1024 struct xfrm_policy *xp;
1025 struct km_event c;
1026 int err;
1027 int excl;
1029 err = verify_newpolicy_info(p);
1030 if (err)
1031 return err;
1032 err = verify_sec_ctx_len(xfrma);
1033 if (err)
1034 return err;
1036 xp = xfrm_policy_construct(p, xfrma, &err);
1037 if (!xp)
1038 return err;
1040 /* shouldnt excl be based on nlh flags??
1041 * Aha! this is anti-netlink really i.e more pfkey derived
1042 * in netlink excl is a flag and you wouldnt need
1043 * a type XFRM_MSG_UPDPOLICY - JHS */
1044 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1045 err = xfrm_policy_insert(p->dir, xp, excl);
1046 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1047 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1049 if (err) {
1050 security_xfrm_policy_free(xp);
1051 kfree(xp);
1052 return err;
1055 c.event = nlh->nlmsg_type;
1056 c.seq = nlh->nlmsg_seq;
1057 c.pid = nlh->nlmsg_pid;
1058 km_policy_notify(xp, p->dir, &c);
1060 xfrm_pol_put(xp);
1062 return 0;
1065 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1067 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1068 int i;
1070 if (xp->xfrm_nr == 0)
1071 return 0;
1073 for (i = 0; i < xp->xfrm_nr; i++) {
1074 struct xfrm_user_tmpl *up = &vec[i];
1075 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1077 memcpy(&up->id, &kp->id, sizeof(up->id));
1078 up->family = kp->encap_family;
1079 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1080 up->reqid = kp->reqid;
1081 up->mode = kp->mode;
1082 up->share = kp->share;
1083 up->optional = kp->optional;
1084 up->aalgos = kp->aalgos;
1085 up->ealgos = kp->ealgos;
1086 up->calgos = kp->calgos;
1088 RTA_PUT(skb, XFRMA_TMPL,
1089 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
1090 vec);
1092 return 0;
1094 rtattr_failure:
1095 return -1;
1098 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
1100 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
1101 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
1102 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1104 uctx->exttype = XFRMA_SEC_CTX;
1105 uctx->len = ctx_size;
1106 uctx->ctx_doi = s->ctx_doi;
1107 uctx->ctx_alg = s->ctx_alg;
1108 uctx->ctx_len = s->ctx_len;
1109 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
1110 return 0;
1112 rtattr_failure:
1113 return -1;
1116 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1118 if (x->security) {
1119 return copy_sec_ctx(x->security, skb);
1121 return 0;
1124 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1126 if (xp->security) {
1127 return copy_sec_ctx(xp->security, skb);
1129 return 0;
1132 #ifdef CONFIG_XFRM_SUB_POLICY
1133 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1135 struct xfrm_userpolicy_type upt;
1137 memset(&upt, 0, sizeof(upt));
1138 upt.type = type;
1140 RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1142 return 0;
1144 rtattr_failure:
1145 return -1;
1148 #else
1149 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1151 return 0;
1153 #endif
1155 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1157 struct xfrm_dump_info *sp = ptr;
1158 struct xfrm_userpolicy_info *p;
1159 struct sk_buff *in_skb = sp->in_skb;
1160 struct sk_buff *skb = sp->out_skb;
1161 struct nlmsghdr *nlh;
1162 unsigned char *b = skb->tail;
1164 if (sp->this_idx < sp->start_idx)
1165 goto out;
1167 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
1168 sp->nlmsg_seq,
1169 XFRM_MSG_NEWPOLICY, sizeof(*p));
1170 p = NLMSG_DATA(nlh);
1171 nlh->nlmsg_flags = sp->nlmsg_flags;
1173 copy_to_user_policy(xp, p, dir);
1174 if (copy_to_user_tmpl(xp, skb) < 0)
1175 goto nlmsg_failure;
1176 if (copy_to_user_sec_ctx(xp, skb))
1177 goto nlmsg_failure;
1178 if (copy_to_user_policy_type(xp->type, skb) < 0)
1179 goto nlmsg_failure;
1181 nlh->nlmsg_len = skb->tail - b;
1182 out:
1183 sp->this_idx++;
1184 return 0;
1186 nlmsg_failure:
1187 skb_trim(skb, b - skb->data);
1188 return -1;
1191 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1193 struct xfrm_dump_info info;
1195 info.in_skb = cb->skb;
1196 info.out_skb = skb;
1197 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1198 info.nlmsg_flags = NLM_F_MULTI;
1199 info.this_idx = 0;
1200 info.start_idx = cb->args[0];
1201 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1202 #ifdef CONFIG_XFRM_SUB_POLICY
1203 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1204 #endif
1205 cb->args[0] = info.this_idx;
1207 return skb->len;
1210 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1211 struct xfrm_policy *xp,
1212 int dir, u32 seq)
1214 struct xfrm_dump_info info;
1215 struct sk_buff *skb;
1217 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1218 if (!skb)
1219 return ERR_PTR(-ENOMEM);
1221 info.in_skb = in_skb;
1222 info.out_skb = skb;
1223 info.nlmsg_seq = seq;
1224 info.nlmsg_flags = 0;
1225 info.this_idx = info.start_idx = 0;
1227 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1228 kfree_skb(skb);
1229 return NULL;
1232 return skb;
1235 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1236 struct rtattr **xfrma)
1238 struct xfrm_policy *xp;
1239 struct xfrm_userpolicy_id *p;
1240 u8 type = XFRM_POLICY_TYPE_MAIN;
1241 int err;
1242 struct km_event c;
1243 int delete;
1245 p = NLMSG_DATA(nlh);
1246 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1248 err = copy_from_user_policy_type(&type, xfrma);
1249 if (err)
1250 return err;
1252 err = verify_policy_dir(p->dir);
1253 if (err)
1254 return err;
1256 if (p->index)
1257 xp = xfrm_policy_byid(type, p->dir, p->index, delete);
1258 else {
1259 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
1260 struct xfrm_policy tmp;
1262 err = verify_sec_ctx_len(xfrma);
1263 if (err)
1264 return err;
1266 memset(&tmp, 0, sizeof(struct xfrm_policy));
1267 if (rt) {
1268 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1270 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1271 return err;
1273 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, delete);
1274 security_xfrm_policy_free(&tmp);
1276 if (xp == NULL)
1277 return -ENOENT;
1279 if (!delete) {
1280 struct sk_buff *resp_skb;
1282 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1283 if (IS_ERR(resp_skb)) {
1284 err = PTR_ERR(resp_skb);
1285 } else {
1286 err = netlink_unicast(xfrm_nl, resp_skb,
1287 NETLINK_CB(skb).pid,
1288 MSG_DONTWAIT);
1290 } else {
1291 err = security_xfrm_policy_delete(xp);
1293 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1294 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1296 if (err != 0)
1297 goto out;
1299 c.data.byid = p->index;
1300 c.event = nlh->nlmsg_type;
1301 c.seq = nlh->nlmsg_seq;
1302 c.pid = nlh->nlmsg_pid;
1303 km_policy_notify(xp, p->dir, &c);
1306 xfrm_pol_put(xp);
1308 out:
1309 return err;
1312 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1313 struct rtattr **xfrma)
1315 struct km_event c;
1316 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1317 struct xfrm_audit audit_info;
1319 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1320 audit_info.secid = NETLINK_CB(skb).sid;
1321 xfrm_state_flush(p->proto, &audit_info);
1322 c.data.proto = p->proto;
1323 c.event = nlh->nlmsg_type;
1324 c.seq = nlh->nlmsg_seq;
1325 c.pid = nlh->nlmsg_pid;
1326 km_state_notify(NULL, &c);
1328 return 0;
1332 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1334 struct xfrm_aevent_id *id;
1335 struct nlmsghdr *nlh;
1336 struct xfrm_lifetime_cur ltime;
1337 unsigned char *b = skb->tail;
1339 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1340 id = NLMSG_DATA(nlh);
1341 nlh->nlmsg_flags = 0;
1343 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1344 id->sa_id.spi = x->id.spi;
1345 id->sa_id.family = x->props.family;
1346 id->sa_id.proto = x->id.proto;
1347 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1348 id->reqid = x->props.reqid;
1349 id->flags = c->data.aevent;
1351 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1353 ltime.bytes = x->curlft.bytes;
1354 ltime.packets = x->curlft.packets;
1355 ltime.add_time = x->curlft.add_time;
1356 ltime.use_time = x->curlft.use_time;
1358 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
1360 if (id->flags&XFRM_AE_RTHR) {
1361 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1364 if (id->flags&XFRM_AE_ETHR) {
1365 u32 etimer = x->replay_maxage*10/HZ;
1366 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1369 nlh->nlmsg_len = skb->tail - b;
1370 return skb->len;
1372 rtattr_failure:
1373 nlmsg_failure:
1374 skb_trim(skb, b - skb->data);
1375 return -1;
1378 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1379 struct rtattr **xfrma)
1381 struct xfrm_state *x;
1382 struct sk_buff *r_skb;
1383 int err;
1384 struct km_event c;
1385 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1386 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1387 struct xfrm_usersa_id *id = &p->sa_id;
1389 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1390 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1392 if (p->flags&XFRM_AE_RTHR)
1393 len+=RTA_SPACE(sizeof(u32));
1395 if (p->flags&XFRM_AE_ETHR)
1396 len+=RTA_SPACE(sizeof(u32));
1398 r_skb = alloc_skb(len, GFP_ATOMIC);
1399 if (r_skb == NULL)
1400 return -ENOMEM;
1402 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1403 if (x == NULL) {
1404 kfree_skb(r_skb);
1405 return -ESRCH;
1409 * XXX: is this lock really needed - none of the other
1410 * gets lock (the concern is things getting updated
1411 * while we are still reading) - jhs
1413 spin_lock_bh(&x->lock);
1414 c.data.aevent = p->flags;
1415 c.seq = nlh->nlmsg_seq;
1416 c.pid = nlh->nlmsg_pid;
1418 if (build_aevent(r_skb, x, &c) < 0)
1419 BUG();
1420 err = netlink_unicast(xfrm_nl, r_skb,
1421 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1422 spin_unlock_bh(&x->lock);
1423 xfrm_state_put(x);
1424 return err;
1427 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1428 struct rtattr **xfrma)
1430 struct xfrm_state *x;
1431 struct km_event c;
1432 int err = - EINVAL;
1433 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1434 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1435 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1437 if (!lt && !rp)
1438 return err;
1440 /* pedantic mode - thou shalt sayeth replaceth */
1441 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1442 return err;
1444 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1445 if (x == NULL)
1446 return -ESRCH;
1448 if (x->km.state != XFRM_STATE_VALID)
1449 goto out;
1451 spin_lock_bh(&x->lock);
1452 err = xfrm_update_ae_params(x, xfrma);
1453 spin_unlock_bh(&x->lock);
1454 if (err < 0)
1455 goto out;
1457 c.event = nlh->nlmsg_type;
1458 c.seq = nlh->nlmsg_seq;
1459 c.pid = nlh->nlmsg_pid;
1460 c.data.aevent = XFRM_AE_CU;
1461 km_state_notify(x, &c);
1462 err = 0;
1463 out:
1464 xfrm_state_put(x);
1465 return err;
1468 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1469 struct rtattr **xfrma)
1471 struct km_event c;
1472 u8 type = XFRM_POLICY_TYPE_MAIN;
1473 int err;
1474 struct xfrm_audit audit_info;
1476 err = copy_from_user_policy_type(&type, xfrma);
1477 if (err)
1478 return err;
1480 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1481 audit_info.secid = NETLINK_CB(skb).sid;
1482 xfrm_policy_flush(type, &audit_info);
1483 c.data.type = type;
1484 c.event = nlh->nlmsg_type;
1485 c.seq = nlh->nlmsg_seq;
1486 c.pid = nlh->nlmsg_pid;
1487 km_policy_notify(NULL, 0, &c);
1488 return 0;
1491 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1492 struct rtattr **xfrma)
1494 struct xfrm_policy *xp;
1495 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1496 struct xfrm_userpolicy_info *p = &up->pol;
1497 u8 type = XFRM_POLICY_TYPE_MAIN;
1498 int err = -ENOENT;
1500 err = copy_from_user_policy_type(&type, xfrma);
1501 if (err)
1502 return err;
1504 if (p->index)
1505 xp = xfrm_policy_byid(type, p->dir, p->index, 0);
1506 else {
1507 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
1508 struct xfrm_policy tmp;
1510 err = verify_sec_ctx_len(xfrma);
1511 if (err)
1512 return err;
1514 memset(&tmp, 0, sizeof(struct xfrm_policy));
1515 if (rt) {
1516 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1518 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1519 return err;
1521 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, 0);
1522 security_xfrm_policy_free(&tmp);
1525 if (xp == NULL)
1526 return err;
1527 read_lock(&xp->lock);
1528 if (xp->dead) {
1529 read_unlock(&xp->lock);
1530 goto out;
1533 read_unlock(&xp->lock);
1534 err = 0;
1535 if (up->hard) {
1536 xfrm_policy_delete(xp, p->dir);
1537 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1538 AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
1540 } else {
1541 // reset the timers here?
1542 printk("Dont know what to do with soft policy expire\n");
1544 km_policy_expired(xp, p->dir, up->hard, current->pid);
1546 out:
1547 xfrm_pol_put(xp);
1548 return err;
1551 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1552 struct rtattr **xfrma)
1554 struct xfrm_state *x;
1555 int err;
1556 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1557 struct xfrm_usersa_info *p = &ue->state;
1559 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1561 err = -ENOENT;
1562 if (x == NULL)
1563 return err;
1565 spin_lock_bh(&x->lock);
1566 err = -EINVAL;
1567 if (x->km.state != XFRM_STATE_VALID)
1568 goto out;
1569 km_state_expired(x, ue->hard, current->pid);
1571 if (ue->hard) {
1572 __xfrm_state_delete(x);
1573 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1574 AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
1576 err = 0;
1577 out:
1578 spin_unlock_bh(&x->lock);
1579 xfrm_state_put(x);
1580 return err;
1583 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1584 struct rtattr **xfrma)
1586 struct xfrm_policy *xp;
1587 struct xfrm_user_tmpl *ut;
1588 int i;
1589 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1591 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1592 struct xfrm_state *x = xfrm_state_alloc();
1593 int err = -ENOMEM;
1595 if (!x)
1596 return err;
1598 err = verify_newpolicy_info(&ua->policy);
1599 if (err) {
1600 printk("BAD policy passed\n");
1601 kfree(x);
1602 return err;
1605 /* build an XP */
1606 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);
1607 if (!xp) {
1608 kfree(x);
1609 return err;
1612 memcpy(&x->id, &ua->id, sizeof(ua->id));
1613 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1614 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1616 ut = RTA_DATA(rt);
1617 /* extract the templates and for each call km_key */
1618 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1619 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1620 memcpy(&x->id, &t->id, sizeof(x->id));
1621 x->props.mode = t->mode;
1622 x->props.reqid = t->reqid;
1623 x->props.family = ut->family;
1624 t->aalgos = ua->aalgos;
1625 t->ealgos = ua->ealgos;
1626 t->calgos = ua->calgos;
1627 err = km_query(x, t, xp);
1631 kfree(x);
1632 kfree(xp);
1634 return 0;
1637 #ifdef CONFIG_XFRM_MIGRATE
1638 static int verify_user_migrate(struct rtattr **xfrma)
1640 struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
1641 struct xfrm_user_migrate *um;
1643 if (!rt)
1644 return -EINVAL;
1646 if ((rt->rta_len - sizeof(*rt)) < sizeof(*um))
1647 return -EINVAL;
1649 return 0;
1652 static int copy_from_user_migrate(struct xfrm_migrate *ma,
1653 struct rtattr **xfrma, int *num)
1655 struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
1656 struct xfrm_user_migrate *um;
1657 int i, num_migrate;
1659 um = RTA_DATA(rt);
1660 num_migrate = (rt->rta_len - sizeof(*rt)) / sizeof(*um);
1662 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1663 return -EINVAL;
1665 for (i = 0; i < num_migrate; i++, um++, ma++) {
1666 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1667 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1668 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1669 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1671 ma->proto = um->proto;
1672 ma->mode = um->mode;
1673 ma->reqid = um->reqid;
1675 ma->old_family = um->old_family;
1676 ma->new_family = um->new_family;
1679 *num = i;
1680 return 0;
1683 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1684 struct rtattr **xfrma)
1686 struct xfrm_userpolicy_id *pi = NLMSG_DATA(nlh);
1687 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1688 u8 type;
1689 int err;
1690 int n = 0;
1692 err = verify_user_migrate((struct rtattr **)xfrma);
1693 if (err)
1694 return err;
1696 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1697 if (err)
1698 return err;
1700 err = copy_from_user_migrate((struct xfrm_migrate *)m,
1701 (struct rtattr **)xfrma, &n);
1702 if (err)
1703 return err;
1705 if (!n)
1706 return 0;
1708 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1710 return 0;
1712 #else
1713 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1714 struct rtattr **xfrma)
1716 return -ENOPROTOOPT;
1718 #endif
1720 #ifdef CONFIG_XFRM_MIGRATE
1721 static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1723 struct xfrm_user_migrate um;
1725 memset(&um, 0, sizeof(um));
1726 um.proto = m->proto;
1727 um.mode = m->mode;
1728 um.reqid = m->reqid;
1729 um.old_family = m->old_family;
1730 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1731 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1732 um.new_family = m->new_family;
1733 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1734 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1736 RTA_PUT(skb, XFRMA_MIGRATE, sizeof(um), &um);
1737 return 0;
1739 rtattr_failure:
1740 return -1;
1743 static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1744 int num_migrate, struct xfrm_selector *sel,
1745 u8 dir, u8 type)
1747 struct xfrm_migrate *mp;
1748 struct xfrm_userpolicy_id *pol_id;
1749 struct nlmsghdr *nlh;
1750 unsigned char *b = skb->tail;
1751 int i;
1753 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id));
1754 pol_id = NLMSG_DATA(nlh);
1755 nlh->nlmsg_flags = 0;
1757 /* copy data from selector, dir, and type to the pol_id */
1758 memset(pol_id, 0, sizeof(*pol_id));
1759 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1760 pol_id->dir = dir;
1762 if (copy_to_user_policy_type(type, skb) < 0)
1763 goto nlmsg_failure;
1765 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1766 if (copy_to_user_migrate(mp, skb) < 0)
1767 goto nlmsg_failure;
1770 nlh->nlmsg_len = skb->tail - b;
1771 return skb->len;
1772 nlmsg_failure:
1773 skb_trim(skb, b - skb->data);
1774 return -1;
1777 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1778 struct xfrm_migrate *m, int num_migrate)
1780 struct sk_buff *skb;
1781 size_t len;
1783 len = RTA_SPACE(sizeof(struct xfrm_user_migrate) * num_migrate);
1784 len += NLMSG_SPACE(sizeof(struct xfrm_userpolicy_id));
1785 #ifdef CONFIG_XFRM_SUB_POLICY
1786 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
1787 #endif
1788 skb = alloc_skb(len, GFP_ATOMIC);
1789 if (skb == NULL)
1790 return -ENOMEM;
1792 /* build migrate */
1793 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1794 BUG();
1796 NETLINK_CB(skb).dst_group = XFRMNLGRP_MIGRATE;
1797 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE,
1798 GFP_ATOMIC);
1800 #else
1801 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1802 struct xfrm_migrate *m, int num_migrate)
1804 return -ENOPROTOOPT;
1806 #endif
1808 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1810 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1811 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1812 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1813 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1814 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1815 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1816 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1817 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1818 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1819 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1820 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1821 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1822 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1823 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1824 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
1825 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1826 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1827 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
1828 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1831 #undef XMSGSIZE
1833 static struct xfrm_link {
1834 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct rtattr **);
1835 int (*dump)(struct sk_buff *, struct netlink_callback *);
1836 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1837 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1838 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1839 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1840 .dump = xfrm_dump_sa },
1841 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1842 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1843 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1844 .dump = xfrm_dump_policy },
1845 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1846 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
1847 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1848 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1849 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1850 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1851 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1852 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
1853 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1854 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1855 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
1858 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1860 struct rtattr *xfrma[XFRMA_MAX];
1861 struct xfrm_link *link;
1862 int type, min_len;
1864 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1865 return 0;
1867 type = nlh->nlmsg_type;
1869 /* A control message: ignore them */
1870 if (type < XFRM_MSG_BASE)
1871 return 0;
1873 /* Unknown message: reply with EINVAL */
1874 if (type > XFRM_MSG_MAX)
1875 goto err_einval;
1877 type -= XFRM_MSG_BASE;
1878 link = &xfrm_dispatch[type];
1880 /* All operations require privileges, even GET */
1881 if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
1882 *errp = -EPERM;
1883 return -1;
1886 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1887 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1888 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1889 if (link->dump == NULL)
1890 goto err_einval;
1892 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
1893 link->dump, NULL)) != 0) {
1894 return -1;
1897 netlink_queue_skip(nlh, skb);
1898 return -1;
1901 memset(xfrma, 0, sizeof(xfrma));
1903 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1904 goto err_einval;
1906 if (nlh->nlmsg_len > min_len) {
1907 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1908 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1910 while (RTA_OK(attr, attrlen)) {
1911 unsigned short flavor = attr->rta_type;
1912 if (flavor) {
1913 if (flavor > XFRMA_MAX)
1914 goto err_einval;
1915 xfrma[flavor - 1] = attr;
1917 attr = RTA_NEXT(attr, attrlen);
1921 if (link->doit == NULL)
1922 goto err_einval;
1923 *errp = link->doit(skb, nlh, xfrma);
1925 return *errp;
1927 err_einval:
1928 *errp = -EINVAL;
1929 return -1;
1932 static void xfrm_netlink_rcv(struct sock *sk, int len)
1934 unsigned int qlen = 0;
1936 do {
1937 mutex_lock(&xfrm_cfg_mutex);
1938 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
1939 mutex_unlock(&xfrm_cfg_mutex);
1941 } while (qlen);
1944 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1946 struct xfrm_user_expire *ue;
1947 struct nlmsghdr *nlh;
1948 unsigned char *b = skb->tail;
1950 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
1951 sizeof(*ue));
1952 ue = NLMSG_DATA(nlh);
1953 nlh->nlmsg_flags = 0;
1955 copy_to_user_state(x, &ue->state);
1956 ue->hard = (c->data.hard != 0) ? 1 : 0;
1958 nlh->nlmsg_len = skb->tail - b;
1959 return skb->len;
1961 nlmsg_failure:
1962 skb_trim(skb, b - skb->data);
1963 return -1;
1966 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1968 struct sk_buff *skb;
1969 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
1971 skb = alloc_skb(len, GFP_ATOMIC);
1972 if (skb == NULL)
1973 return -ENOMEM;
1975 if (build_expire(skb, x, c) < 0)
1976 BUG();
1978 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1979 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1982 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1984 struct sk_buff *skb;
1985 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1987 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1988 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1989 skb = alloc_skb(len, GFP_ATOMIC);
1990 if (skb == NULL)
1991 return -ENOMEM;
1993 if (build_aevent(skb, x, c) < 0)
1994 BUG();
1996 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1997 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2000 static int xfrm_notify_sa_flush(struct km_event *c)
2002 struct xfrm_usersa_flush *p;
2003 struct nlmsghdr *nlh;
2004 struct sk_buff *skb;
2005 unsigned char *b;
2006 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
2008 skb = alloc_skb(len, GFP_ATOMIC);
2009 if (skb == NULL)
2010 return -ENOMEM;
2011 b = skb->tail;
2013 nlh = NLMSG_PUT(skb, c->pid, c->seq,
2014 XFRM_MSG_FLUSHSA, sizeof(*p));
2015 nlh->nlmsg_flags = 0;
2017 p = NLMSG_DATA(nlh);
2018 p->proto = c->data.proto;
2020 nlh->nlmsg_len = skb->tail - b;
2022 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
2023 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2025 nlmsg_failure:
2026 kfree_skb(skb);
2027 return -1;
2030 static int inline xfrm_sa_len(struct xfrm_state *x)
2032 int l = 0;
2033 if (x->aalg)
2034 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
2035 if (x->ealg)
2036 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
2037 if (x->calg)
2038 l += RTA_SPACE(sizeof(*x->calg));
2039 if (x->encap)
2040 l += RTA_SPACE(sizeof(*x->encap));
2042 return l;
2045 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2047 struct xfrm_usersa_info *p;
2048 struct xfrm_usersa_id *id;
2049 struct nlmsghdr *nlh;
2050 struct sk_buff *skb;
2051 unsigned char *b;
2052 int len = xfrm_sa_len(x);
2053 int headlen;
2055 headlen = sizeof(*p);
2056 if (c->event == XFRM_MSG_DELSA) {
2057 len += RTA_SPACE(headlen);
2058 headlen = sizeof(*id);
2060 len += NLMSG_SPACE(headlen);
2062 skb = alloc_skb(len, GFP_ATOMIC);
2063 if (skb == NULL)
2064 return -ENOMEM;
2065 b = skb->tail;
2067 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
2068 nlh->nlmsg_flags = 0;
2070 p = NLMSG_DATA(nlh);
2071 if (c->event == XFRM_MSG_DELSA) {
2072 id = NLMSG_DATA(nlh);
2073 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2074 id->spi = x->id.spi;
2075 id->family = x->props.family;
2076 id->proto = x->id.proto;
2078 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
2081 copy_to_user_state(x, p);
2083 if (x->aalg)
2084 RTA_PUT(skb, XFRMA_ALG_AUTH,
2085 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
2086 if (x->ealg)
2087 RTA_PUT(skb, XFRMA_ALG_CRYPT,
2088 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
2089 if (x->calg)
2090 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
2092 if (x->encap)
2093 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
2095 nlh->nlmsg_len = skb->tail - b;
2097 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
2098 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2100 nlmsg_failure:
2101 rtattr_failure:
2102 kfree_skb(skb);
2103 return -1;
2106 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2109 switch (c->event) {
2110 case XFRM_MSG_EXPIRE:
2111 return xfrm_exp_state_notify(x, c);
2112 case XFRM_MSG_NEWAE:
2113 return xfrm_aevent_state_notify(x, c);
2114 case XFRM_MSG_DELSA:
2115 case XFRM_MSG_UPDSA:
2116 case XFRM_MSG_NEWSA:
2117 return xfrm_notify_sa(x, c);
2118 case XFRM_MSG_FLUSHSA:
2119 return xfrm_notify_sa_flush(c);
2120 default:
2121 printk("xfrm_user: Unknown SA event %d\n", c->event);
2122 break;
2125 return 0;
2129 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2130 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2131 int dir)
2133 struct xfrm_user_acquire *ua;
2134 struct nlmsghdr *nlh;
2135 unsigned char *b = skb->tail;
2136 __u32 seq = xfrm_get_acqseq();
2138 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
2139 sizeof(*ua));
2140 ua = NLMSG_DATA(nlh);
2141 nlh->nlmsg_flags = 0;
2143 memcpy(&ua->id, &x->id, sizeof(ua->id));
2144 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2145 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2146 copy_to_user_policy(xp, &ua->policy, dir);
2147 ua->aalgos = xt->aalgos;
2148 ua->ealgos = xt->ealgos;
2149 ua->calgos = xt->calgos;
2150 ua->seq = x->km.seq = seq;
2152 if (copy_to_user_tmpl(xp, skb) < 0)
2153 goto nlmsg_failure;
2154 if (copy_to_user_state_sec_ctx(x, skb))
2155 goto nlmsg_failure;
2156 if (copy_to_user_policy_type(xp->type, skb) < 0)
2157 goto nlmsg_failure;
2159 nlh->nlmsg_len = skb->tail - b;
2160 return skb->len;
2162 nlmsg_failure:
2163 skb_trim(skb, b - skb->data);
2164 return -1;
2167 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2168 struct xfrm_policy *xp, int dir)
2170 struct sk_buff *skb;
2171 size_t len;
2173 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2174 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
2175 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
2176 #ifdef CONFIG_XFRM_SUB_POLICY
2177 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2178 #endif
2179 skb = alloc_skb(len, GFP_ATOMIC);
2180 if (skb == NULL)
2181 return -ENOMEM;
2183 if (build_acquire(skb, x, xt, xp, dir) < 0)
2184 BUG();
2186 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
2187 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2190 /* User gives us xfrm_user_policy_info followed by an array of 0
2191 * or more templates.
2193 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2194 u8 *data, int len, int *dir)
2196 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2197 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2198 struct xfrm_policy *xp;
2199 int nr;
2201 switch (sk->sk_family) {
2202 case AF_INET:
2203 if (opt != IP_XFRM_POLICY) {
2204 *dir = -EOPNOTSUPP;
2205 return NULL;
2207 break;
2208 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2209 case AF_INET6:
2210 if (opt != IPV6_XFRM_POLICY) {
2211 *dir = -EOPNOTSUPP;
2212 return NULL;
2214 break;
2215 #endif
2216 default:
2217 *dir = -EINVAL;
2218 return NULL;
2221 *dir = -EINVAL;
2223 if (len < sizeof(*p) ||
2224 verify_newpolicy_info(p))
2225 return NULL;
2227 nr = ((len - sizeof(*p)) / sizeof(*ut));
2228 if (validate_tmpl(nr, ut, p->sel.family))
2229 return NULL;
2231 if (p->dir > XFRM_POLICY_OUT)
2232 return NULL;
2234 xp = xfrm_policy_alloc(GFP_KERNEL);
2235 if (xp == NULL) {
2236 *dir = -ENOBUFS;
2237 return NULL;
2240 copy_from_user_policy(xp, p);
2241 xp->type = XFRM_POLICY_TYPE_MAIN;
2242 copy_templates(xp, ut, nr);
2244 *dir = p->dir;
2246 return xp;
2249 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2250 int dir, struct km_event *c)
2252 struct xfrm_user_polexpire *upe;
2253 struct nlmsghdr *nlh;
2254 int hard = c->data.hard;
2255 unsigned char *b = skb->tail;
2257 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
2258 upe = NLMSG_DATA(nlh);
2259 nlh->nlmsg_flags = 0;
2261 copy_to_user_policy(xp, &upe->pol, dir);
2262 if (copy_to_user_tmpl(xp, skb) < 0)
2263 goto nlmsg_failure;
2264 if (copy_to_user_sec_ctx(xp, skb))
2265 goto nlmsg_failure;
2266 if (copy_to_user_policy_type(xp->type, skb) < 0)
2267 goto nlmsg_failure;
2268 upe->hard = !!hard;
2270 nlh->nlmsg_len = skb->tail - b;
2271 return skb->len;
2273 nlmsg_failure:
2274 skb_trim(skb, b - skb->data);
2275 return -1;
2278 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2280 struct sk_buff *skb;
2281 size_t len;
2283 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2284 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
2285 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
2286 #ifdef CONFIG_XFRM_SUB_POLICY
2287 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2288 #endif
2289 skb = alloc_skb(len, GFP_ATOMIC);
2290 if (skb == NULL)
2291 return -ENOMEM;
2293 if (build_polexpire(skb, xp, dir, c) < 0)
2294 BUG();
2296 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
2297 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2300 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2302 struct xfrm_userpolicy_info *p;
2303 struct xfrm_userpolicy_id *id;
2304 struct nlmsghdr *nlh;
2305 struct sk_buff *skb;
2306 unsigned char *b;
2307 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2308 int headlen;
2310 headlen = sizeof(*p);
2311 if (c->event == XFRM_MSG_DELPOLICY) {
2312 len += RTA_SPACE(headlen);
2313 headlen = sizeof(*id);
2315 #ifdef CONFIG_XFRM_SUB_POLICY
2316 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2317 #endif
2318 len += NLMSG_SPACE(headlen);
2320 skb = alloc_skb(len, GFP_ATOMIC);
2321 if (skb == NULL)
2322 return -ENOMEM;
2323 b = skb->tail;
2325 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
2327 p = NLMSG_DATA(nlh);
2328 if (c->event == XFRM_MSG_DELPOLICY) {
2329 id = NLMSG_DATA(nlh);
2330 memset(id, 0, sizeof(*id));
2331 id->dir = dir;
2332 if (c->data.byid)
2333 id->index = xp->index;
2334 else
2335 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2337 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
2340 nlh->nlmsg_flags = 0;
2342 copy_to_user_policy(xp, p, dir);
2343 if (copy_to_user_tmpl(xp, skb) < 0)
2344 goto nlmsg_failure;
2345 if (copy_to_user_policy_type(xp->type, skb) < 0)
2346 goto nlmsg_failure;
2348 nlh->nlmsg_len = skb->tail - b;
2350 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2351 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2353 nlmsg_failure:
2354 rtattr_failure:
2355 kfree_skb(skb);
2356 return -1;
2359 static int xfrm_notify_policy_flush(struct km_event *c)
2361 struct nlmsghdr *nlh;
2362 struct sk_buff *skb;
2363 unsigned char *b;
2364 int len = 0;
2365 #ifdef CONFIG_XFRM_SUB_POLICY
2366 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2367 #endif
2368 len += NLMSG_LENGTH(0);
2370 skb = alloc_skb(len, GFP_ATOMIC);
2371 if (skb == NULL)
2372 return -ENOMEM;
2373 b = skb->tail;
2376 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
2377 nlh->nlmsg_flags = 0;
2378 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2379 goto nlmsg_failure;
2381 nlh->nlmsg_len = skb->tail - b;
2383 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2384 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2386 nlmsg_failure:
2387 kfree_skb(skb);
2388 return -1;
2391 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2394 switch (c->event) {
2395 case XFRM_MSG_NEWPOLICY:
2396 case XFRM_MSG_UPDPOLICY:
2397 case XFRM_MSG_DELPOLICY:
2398 return xfrm_notify_policy(xp, dir, c);
2399 case XFRM_MSG_FLUSHPOLICY:
2400 return xfrm_notify_policy_flush(c);
2401 case XFRM_MSG_POLEXPIRE:
2402 return xfrm_exp_policy_notify(xp, dir, c);
2403 default:
2404 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2407 return 0;
2411 static int build_report(struct sk_buff *skb, u8 proto,
2412 struct xfrm_selector *sel, xfrm_address_t *addr)
2414 struct xfrm_user_report *ur;
2415 struct nlmsghdr *nlh;
2416 unsigned char *b = skb->tail;
2418 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur));
2419 ur = NLMSG_DATA(nlh);
2420 nlh->nlmsg_flags = 0;
2422 ur->proto = proto;
2423 memcpy(&ur->sel, sel, sizeof(ur->sel));
2425 if (addr)
2426 RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2428 nlh->nlmsg_len = skb->tail - b;
2429 return skb->len;
2431 nlmsg_failure:
2432 rtattr_failure:
2433 skb_trim(skb, b - skb->data);
2434 return -1;
2437 static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2438 xfrm_address_t *addr)
2440 struct sk_buff *skb;
2441 size_t len;
2443 len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
2444 skb = alloc_skb(len, GFP_ATOMIC);
2445 if (skb == NULL)
2446 return -ENOMEM;
2448 if (build_report(skb, proto, sel, addr) < 0)
2449 BUG();
2451 NETLINK_CB(skb).dst_group = XFRMNLGRP_REPORT;
2452 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2455 static struct xfrm_mgr netlink_mgr = {
2456 .id = "netlink",
2457 .notify = xfrm_send_state_notify,
2458 .acquire = xfrm_send_acquire,
2459 .compile_policy = xfrm_compile_policy,
2460 .notify_policy = xfrm_send_policy_notify,
2461 .report = xfrm_send_report,
2462 .migrate = xfrm_send_migrate,
2465 static int __init xfrm_user_init(void)
2467 struct sock *nlsk;
2469 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2471 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
2472 xfrm_netlink_rcv, THIS_MODULE);
2473 if (nlsk == NULL)
2474 return -ENOMEM;
2475 rcu_assign_pointer(xfrm_nl, nlsk);
2477 xfrm_register_km(&netlink_mgr);
2479 return 0;
2482 static void __exit xfrm_user_exit(void)
2484 struct sock *nlsk = xfrm_nl;
2486 xfrm_unregister_km(&netlink_mgr);
2487 rcu_assign_pointer(xfrm_nl, NULL);
2488 synchronize_rcu();
2489 sock_release(nlsk->sk_socket);
2492 module_init(xfrm_user_init);
2493 module_exit(xfrm_user_exit);
2494 MODULE_LICENSE("GPL");
2495 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);