audit: Add auditing to ipsec
[linux-2.6/openmoko-kernel/knife-kernel.git] / net / xfrm / xfrm_user.c
blobe5372b11fc8f105bd4ecf6071c0418f71855760c
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, void **xfrma)
439 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
440 struct xfrm_state *x;
441 int err;
442 struct km_event c;
444 err = verify_newsa_info(p, (struct rtattr **)xfrma);
445 if (err)
446 return err;
448 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
449 if (!x)
450 return err;
452 xfrm_state_hold(x);
453 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
454 err = xfrm_state_add(x);
455 else
456 err = xfrm_state_update(x);
458 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
459 AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
461 if (err < 0) {
462 x->km.state = XFRM_STATE_DEAD;
463 __xfrm_state_put(x);
464 goto out;
467 c.seq = nlh->nlmsg_seq;
468 c.pid = nlh->nlmsg_pid;
469 c.event = nlh->nlmsg_type;
471 km_state_notify(x, &c);
472 out:
473 xfrm_state_put(x);
474 return err;
477 static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
478 struct rtattr **xfrma,
479 int *errp)
481 struct xfrm_state *x = NULL;
482 int err;
484 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
485 err = -ESRCH;
486 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
487 } else {
488 xfrm_address_t *saddr = NULL;
490 err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr);
491 if (err)
492 goto out;
494 if (!saddr) {
495 err = -EINVAL;
496 goto out;
499 err = -ESRCH;
500 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
501 p->family);
504 out:
505 if (!x && errp)
506 *errp = err;
507 return x;
510 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
512 struct xfrm_state *x;
513 int err = -ESRCH;
514 struct km_event c;
515 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
517 x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err);
518 if (x == NULL)
519 return err;
521 if ((err = security_xfrm_state_delete(x)) != 0)
522 goto out;
524 if (xfrm_state_kern(x)) {
525 err = -EPERM;
526 goto out;
529 err = xfrm_state_delete(x);
531 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
532 AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
534 if (err < 0)
535 goto out;
537 c.seq = nlh->nlmsg_seq;
538 c.pid = nlh->nlmsg_pid;
539 c.event = nlh->nlmsg_type;
540 km_state_notify(x, &c);
542 out:
543 xfrm_state_put(x);
544 return err;
547 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
549 memcpy(&p->id, &x->id, sizeof(p->id));
550 memcpy(&p->sel, &x->sel, sizeof(p->sel));
551 memcpy(&p->lft, &x->lft, sizeof(p->lft));
552 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
553 memcpy(&p->stats, &x->stats, sizeof(p->stats));
554 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
555 p->mode = x->props.mode;
556 p->replay_window = x->props.replay_window;
557 p->reqid = x->props.reqid;
558 p->family = x->props.family;
559 p->flags = x->props.flags;
560 p->seq = x->km.seq;
563 struct xfrm_dump_info {
564 struct sk_buff *in_skb;
565 struct sk_buff *out_skb;
566 u32 nlmsg_seq;
567 u16 nlmsg_flags;
568 int start_idx;
569 int this_idx;
572 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
574 struct xfrm_dump_info *sp = ptr;
575 struct sk_buff *in_skb = sp->in_skb;
576 struct sk_buff *skb = sp->out_skb;
577 struct xfrm_usersa_info *p;
578 struct nlmsghdr *nlh;
579 unsigned char *b = skb->tail;
581 if (sp->this_idx < sp->start_idx)
582 goto out;
584 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
585 sp->nlmsg_seq,
586 XFRM_MSG_NEWSA, sizeof(*p));
587 nlh->nlmsg_flags = sp->nlmsg_flags;
589 p = NLMSG_DATA(nlh);
590 copy_to_user_state(x, p);
592 if (x->aalg)
593 RTA_PUT(skb, XFRMA_ALG_AUTH,
594 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
595 if (x->ealg)
596 RTA_PUT(skb, XFRMA_ALG_CRYPT,
597 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
598 if (x->calg)
599 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
601 if (x->encap)
602 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
604 if (x->security) {
605 int ctx_size = sizeof(struct xfrm_sec_ctx) +
606 x->security->ctx_len;
607 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
608 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
610 uctx->exttype = XFRMA_SEC_CTX;
611 uctx->len = ctx_size;
612 uctx->ctx_doi = x->security->ctx_doi;
613 uctx->ctx_alg = x->security->ctx_alg;
614 uctx->ctx_len = x->security->ctx_len;
615 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
618 if (x->coaddr)
619 RTA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
621 if (x->lastused)
622 RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused);
624 nlh->nlmsg_len = skb->tail - b;
625 out:
626 sp->this_idx++;
627 return 0;
629 nlmsg_failure:
630 rtattr_failure:
631 skb_trim(skb, b - skb->data);
632 return -1;
635 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
637 struct xfrm_dump_info info;
639 info.in_skb = cb->skb;
640 info.out_skb = skb;
641 info.nlmsg_seq = cb->nlh->nlmsg_seq;
642 info.nlmsg_flags = NLM_F_MULTI;
643 info.this_idx = 0;
644 info.start_idx = cb->args[0];
645 (void) xfrm_state_walk(0, dump_one_state, &info);
646 cb->args[0] = info.this_idx;
648 return skb->len;
651 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
652 struct xfrm_state *x, u32 seq)
654 struct xfrm_dump_info info;
655 struct sk_buff *skb;
657 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
658 if (!skb)
659 return ERR_PTR(-ENOMEM);
661 info.in_skb = in_skb;
662 info.out_skb = skb;
663 info.nlmsg_seq = seq;
664 info.nlmsg_flags = 0;
665 info.this_idx = info.start_idx = 0;
667 if (dump_one_state(x, 0, &info)) {
668 kfree_skb(skb);
669 return NULL;
672 return skb;
675 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
677 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
678 struct xfrm_state *x;
679 struct sk_buff *resp_skb;
680 int err = -ESRCH;
682 x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err);
683 if (x == NULL)
684 goto out_noput;
686 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
687 if (IS_ERR(resp_skb)) {
688 err = PTR_ERR(resp_skb);
689 } else {
690 err = netlink_unicast(xfrm_nl, resp_skb,
691 NETLINK_CB(skb).pid, MSG_DONTWAIT);
693 xfrm_state_put(x);
694 out_noput:
695 return err;
698 static int verify_userspi_info(struct xfrm_userspi_info *p)
700 switch (p->info.id.proto) {
701 case IPPROTO_AH:
702 case IPPROTO_ESP:
703 break;
705 case IPPROTO_COMP:
706 /* IPCOMP spi is 16-bits. */
707 if (p->max >= 0x10000)
708 return -EINVAL;
709 break;
711 default:
712 return -EINVAL;
715 if (p->min > p->max)
716 return -EINVAL;
718 return 0;
721 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
723 struct xfrm_state *x;
724 struct xfrm_userspi_info *p;
725 struct sk_buff *resp_skb;
726 xfrm_address_t *daddr;
727 int family;
728 int err;
730 p = NLMSG_DATA(nlh);
731 err = verify_userspi_info(p);
732 if (err)
733 goto out_noput;
735 family = p->info.family;
736 daddr = &p->info.id.daddr;
738 x = NULL;
739 if (p->info.seq) {
740 x = xfrm_find_acq_byseq(p->info.seq);
741 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
742 xfrm_state_put(x);
743 x = NULL;
747 if (!x)
748 x = xfrm_find_acq(p->info.mode, p->info.reqid,
749 p->info.id.proto, daddr,
750 &p->info.saddr, 1,
751 family);
752 err = -ENOENT;
753 if (x == NULL)
754 goto out_noput;
756 resp_skb = ERR_PTR(-ENOENT);
758 spin_lock_bh(&x->lock);
759 if (x->km.state != XFRM_STATE_DEAD) {
760 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
761 if (x->id.spi)
762 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
764 spin_unlock_bh(&x->lock);
766 if (IS_ERR(resp_skb)) {
767 err = PTR_ERR(resp_skb);
768 goto out;
771 err = netlink_unicast(xfrm_nl, resp_skb,
772 NETLINK_CB(skb).pid, MSG_DONTWAIT);
774 out:
775 xfrm_state_put(x);
776 out_noput:
777 return err;
780 static int verify_policy_dir(u8 dir)
782 switch (dir) {
783 case XFRM_POLICY_IN:
784 case XFRM_POLICY_OUT:
785 case XFRM_POLICY_FWD:
786 break;
788 default:
789 return -EINVAL;
792 return 0;
795 static int verify_policy_type(u8 type)
797 switch (type) {
798 case XFRM_POLICY_TYPE_MAIN:
799 #ifdef CONFIG_XFRM_SUB_POLICY
800 case XFRM_POLICY_TYPE_SUB:
801 #endif
802 break;
804 default:
805 return -EINVAL;
808 return 0;
811 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
813 switch (p->share) {
814 case XFRM_SHARE_ANY:
815 case XFRM_SHARE_SESSION:
816 case XFRM_SHARE_USER:
817 case XFRM_SHARE_UNIQUE:
818 break;
820 default:
821 return -EINVAL;
824 switch (p->action) {
825 case XFRM_POLICY_ALLOW:
826 case XFRM_POLICY_BLOCK:
827 break;
829 default:
830 return -EINVAL;
833 switch (p->sel.family) {
834 case AF_INET:
835 break;
837 case AF_INET6:
838 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
839 break;
840 #else
841 return -EAFNOSUPPORT;
842 #endif
844 default:
845 return -EINVAL;
848 return verify_policy_dir(p->dir);
851 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
853 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
854 struct xfrm_user_sec_ctx *uctx;
856 if (!rt)
857 return 0;
859 uctx = RTA_DATA(rt);
860 return security_xfrm_policy_alloc(pol, uctx);
863 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
864 int nr)
866 int i;
868 xp->xfrm_nr = nr;
869 for (i = 0; i < nr; i++, ut++) {
870 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
872 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
873 memcpy(&t->saddr, &ut->saddr,
874 sizeof(xfrm_address_t));
875 t->reqid = ut->reqid;
876 t->mode = ut->mode;
877 t->share = ut->share;
878 t->optional = ut->optional;
879 t->aalgos = ut->aalgos;
880 t->ealgos = ut->ealgos;
881 t->calgos = ut->calgos;
882 t->encap_family = ut->family;
886 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
888 int i;
890 if (nr > XFRM_MAX_DEPTH)
891 return -EINVAL;
893 for (i = 0; i < nr; i++) {
894 /* We never validated the ut->family value, so many
895 * applications simply leave it at zero. The check was
896 * never made and ut->family was ignored because all
897 * templates could be assumed to have the same family as
898 * the policy itself. Now that we will have ipv4-in-ipv6
899 * and ipv6-in-ipv4 tunnels, this is no longer true.
901 if (!ut[i].family)
902 ut[i].family = family;
904 switch (ut[i].family) {
905 case AF_INET:
906 break;
907 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
908 case AF_INET6:
909 break;
910 #endif
911 default:
912 return -EINVAL;
916 return 0;
919 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
921 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
923 if (!rt) {
924 pol->xfrm_nr = 0;
925 } else {
926 struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
927 int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
928 int err;
930 err = validate_tmpl(nr, utmpl, pol->family);
931 if (err)
932 return err;
934 copy_templates(pol, RTA_DATA(rt), nr);
936 return 0;
939 static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
941 struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
942 struct xfrm_userpolicy_type *upt;
943 u8 type = XFRM_POLICY_TYPE_MAIN;
944 int err;
946 if (rt) {
947 if (rt->rta_len < sizeof(*upt))
948 return -EINVAL;
950 upt = RTA_DATA(rt);
951 type = upt->type;
954 err = verify_policy_type(type);
955 if (err)
956 return err;
958 *tp = type;
959 return 0;
962 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
964 xp->priority = p->priority;
965 xp->index = p->index;
966 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
967 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
968 xp->action = p->action;
969 xp->flags = p->flags;
970 xp->family = p->sel.family;
971 /* XXX xp->share = p->share; */
974 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
976 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
977 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
978 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
979 p->priority = xp->priority;
980 p->index = xp->index;
981 p->sel.family = xp->family;
982 p->dir = dir;
983 p->action = xp->action;
984 p->flags = xp->flags;
985 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
988 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
990 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
991 int err;
993 if (!xp) {
994 *errp = -ENOMEM;
995 return NULL;
998 copy_from_user_policy(xp, p);
1000 err = copy_from_user_policy_type(&xp->type, xfrma);
1001 if (err)
1002 goto error;
1004 if (!(err = copy_from_user_tmpl(xp, xfrma)))
1005 err = copy_from_user_sec_ctx(xp, xfrma);
1006 if (err)
1007 goto error;
1009 return xp;
1010 error:
1011 *errp = err;
1012 kfree(xp);
1013 return NULL;
1016 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1018 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
1019 struct xfrm_policy *xp;
1020 struct km_event c;
1021 int err;
1022 int excl;
1024 err = verify_newpolicy_info(p);
1025 if (err)
1026 return err;
1027 err = verify_sec_ctx_len((struct rtattr **)xfrma);
1028 if (err)
1029 return err;
1031 xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
1032 if (!xp)
1033 return err;
1035 /* shouldnt excl be based on nlh flags??
1036 * Aha! this is anti-netlink really i.e more pfkey derived
1037 * in netlink excl is a flag and you wouldnt need
1038 * a type XFRM_MSG_UPDPOLICY - JHS */
1039 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1040 err = xfrm_policy_insert(p->dir, xp, excl);
1041 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1042 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1044 if (err) {
1045 security_xfrm_policy_free(xp);
1046 kfree(xp);
1047 return err;
1050 c.event = nlh->nlmsg_type;
1051 c.seq = nlh->nlmsg_seq;
1052 c.pid = nlh->nlmsg_pid;
1053 km_policy_notify(xp, p->dir, &c);
1055 xfrm_pol_put(xp);
1057 return 0;
1060 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1062 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1063 int i;
1065 if (xp->xfrm_nr == 0)
1066 return 0;
1068 for (i = 0; i < xp->xfrm_nr; i++) {
1069 struct xfrm_user_tmpl *up = &vec[i];
1070 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1072 memcpy(&up->id, &kp->id, sizeof(up->id));
1073 up->family = kp->encap_family;
1074 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1075 up->reqid = kp->reqid;
1076 up->mode = kp->mode;
1077 up->share = kp->share;
1078 up->optional = kp->optional;
1079 up->aalgos = kp->aalgos;
1080 up->ealgos = kp->ealgos;
1081 up->calgos = kp->calgos;
1083 RTA_PUT(skb, XFRMA_TMPL,
1084 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
1085 vec);
1087 return 0;
1089 rtattr_failure:
1090 return -1;
1093 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
1095 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
1096 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
1097 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1099 uctx->exttype = XFRMA_SEC_CTX;
1100 uctx->len = ctx_size;
1101 uctx->ctx_doi = s->ctx_doi;
1102 uctx->ctx_alg = s->ctx_alg;
1103 uctx->ctx_len = s->ctx_len;
1104 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
1105 return 0;
1107 rtattr_failure:
1108 return -1;
1111 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1113 if (x->security) {
1114 return copy_sec_ctx(x->security, skb);
1116 return 0;
1119 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1121 if (xp->security) {
1122 return copy_sec_ctx(xp->security, skb);
1124 return 0;
1127 #ifdef CONFIG_XFRM_SUB_POLICY
1128 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1130 struct xfrm_userpolicy_type upt;
1132 memset(&upt, 0, sizeof(upt));
1133 upt.type = type;
1135 RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1137 return 0;
1139 rtattr_failure:
1140 return -1;
1143 #else
1144 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1146 return 0;
1148 #endif
1150 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1152 struct xfrm_dump_info *sp = ptr;
1153 struct xfrm_userpolicy_info *p;
1154 struct sk_buff *in_skb = sp->in_skb;
1155 struct sk_buff *skb = sp->out_skb;
1156 struct nlmsghdr *nlh;
1157 unsigned char *b = skb->tail;
1159 if (sp->this_idx < sp->start_idx)
1160 goto out;
1162 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
1163 sp->nlmsg_seq,
1164 XFRM_MSG_NEWPOLICY, sizeof(*p));
1165 p = NLMSG_DATA(nlh);
1166 nlh->nlmsg_flags = sp->nlmsg_flags;
1168 copy_to_user_policy(xp, p, dir);
1169 if (copy_to_user_tmpl(xp, skb) < 0)
1170 goto nlmsg_failure;
1171 if (copy_to_user_sec_ctx(xp, skb))
1172 goto nlmsg_failure;
1173 if (copy_to_user_policy_type(xp->type, skb) < 0)
1174 goto nlmsg_failure;
1176 nlh->nlmsg_len = skb->tail - b;
1177 out:
1178 sp->this_idx++;
1179 return 0;
1181 nlmsg_failure:
1182 skb_trim(skb, b - skb->data);
1183 return -1;
1186 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1188 struct xfrm_dump_info info;
1190 info.in_skb = cb->skb;
1191 info.out_skb = skb;
1192 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1193 info.nlmsg_flags = NLM_F_MULTI;
1194 info.this_idx = 0;
1195 info.start_idx = cb->args[0];
1196 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1197 #ifdef CONFIG_XFRM_SUB_POLICY
1198 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1199 #endif
1200 cb->args[0] = info.this_idx;
1202 return skb->len;
1205 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1206 struct xfrm_policy *xp,
1207 int dir, u32 seq)
1209 struct xfrm_dump_info info;
1210 struct sk_buff *skb;
1212 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1213 if (!skb)
1214 return ERR_PTR(-ENOMEM);
1216 info.in_skb = in_skb;
1217 info.out_skb = skb;
1218 info.nlmsg_seq = seq;
1219 info.nlmsg_flags = 0;
1220 info.this_idx = info.start_idx = 0;
1222 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1223 kfree_skb(skb);
1224 return NULL;
1227 return skb;
1230 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1232 struct xfrm_policy *xp;
1233 struct xfrm_userpolicy_id *p;
1234 u8 type = XFRM_POLICY_TYPE_MAIN;
1235 int err;
1236 struct km_event c;
1237 int delete;
1239 p = NLMSG_DATA(nlh);
1240 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1242 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1243 if (err)
1244 return err;
1246 err = verify_policy_dir(p->dir);
1247 if (err)
1248 return err;
1250 if (p->index)
1251 xp = xfrm_policy_byid(type, p->dir, p->index, delete);
1252 else {
1253 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1254 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1255 struct xfrm_policy tmp;
1257 err = verify_sec_ctx_len(rtattrs);
1258 if (err)
1259 return err;
1261 memset(&tmp, 0, sizeof(struct xfrm_policy));
1262 if (rt) {
1263 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1265 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1266 return err;
1268 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, delete);
1269 security_xfrm_policy_free(&tmp);
1271 if (delete)
1272 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1273 AUDIT_MAC_IPSEC_DELSPD, (xp) ? 1 : 0, xp, NULL);
1275 if (xp == NULL)
1276 return -ENOENT;
1278 if (!delete) {
1279 struct sk_buff *resp_skb;
1281 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1282 if (IS_ERR(resp_skb)) {
1283 err = PTR_ERR(resp_skb);
1284 } else {
1285 err = netlink_unicast(xfrm_nl, resp_skb,
1286 NETLINK_CB(skb).pid,
1287 MSG_DONTWAIT);
1289 } else {
1290 if ((err = security_xfrm_policy_delete(xp)) != 0)
1291 goto out;
1292 c.data.byid = p->index;
1293 c.event = nlh->nlmsg_type;
1294 c.seq = nlh->nlmsg_seq;
1295 c.pid = nlh->nlmsg_pid;
1296 km_policy_notify(xp, p->dir, &c);
1299 xfrm_pol_put(xp);
1301 out:
1302 return err;
1305 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1307 struct km_event c;
1308 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1309 struct xfrm_audit audit_info;
1311 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1312 audit_info.secid = NETLINK_CB(skb).sid;
1313 xfrm_state_flush(p->proto, &audit_info);
1314 c.data.proto = p->proto;
1315 c.event = nlh->nlmsg_type;
1316 c.seq = nlh->nlmsg_seq;
1317 c.pid = nlh->nlmsg_pid;
1318 km_state_notify(NULL, &c);
1320 return 0;
1324 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1326 struct xfrm_aevent_id *id;
1327 struct nlmsghdr *nlh;
1328 struct xfrm_lifetime_cur ltime;
1329 unsigned char *b = skb->tail;
1331 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1332 id = NLMSG_DATA(nlh);
1333 nlh->nlmsg_flags = 0;
1335 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1336 id->sa_id.spi = x->id.spi;
1337 id->sa_id.family = x->props.family;
1338 id->sa_id.proto = x->id.proto;
1339 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1340 id->reqid = x->props.reqid;
1341 id->flags = c->data.aevent;
1343 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1345 ltime.bytes = x->curlft.bytes;
1346 ltime.packets = x->curlft.packets;
1347 ltime.add_time = x->curlft.add_time;
1348 ltime.use_time = x->curlft.use_time;
1350 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
1352 if (id->flags&XFRM_AE_RTHR) {
1353 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1356 if (id->flags&XFRM_AE_ETHR) {
1357 u32 etimer = x->replay_maxage*10/HZ;
1358 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1361 nlh->nlmsg_len = skb->tail - b;
1362 return skb->len;
1364 rtattr_failure:
1365 nlmsg_failure:
1366 skb_trim(skb, b - skb->data);
1367 return -1;
1370 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1372 struct xfrm_state *x;
1373 struct sk_buff *r_skb;
1374 int err;
1375 struct km_event c;
1376 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1377 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1378 struct xfrm_usersa_id *id = &p->sa_id;
1380 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1381 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1383 if (p->flags&XFRM_AE_RTHR)
1384 len+=RTA_SPACE(sizeof(u32));
1386 if (p->flags&XFRM_AE_ETHR)
1387 len+=RTA_SPACE(sizeof(u32));
1389 r_skb = alloc_skb(len, GFP_ATOMIC);
1390 if (r_skb == NULL)
1391 return -ENOMEM;
1393 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1394 if (x == NULL) {
1395 kfree(r_skb);
1396 return -ESRCH;
1400 * XXX: is this lock really needed - none of the other
1401 * gets lock (the concern is things getting updated
1402 * while we are still reading) - jhs
1404 spin_lock_bh(&x->lock);
1405 c.data.aevent = p->flags;
1406 c.seq = nlh->nlmsg_seq;
1407 c.pid = nlh->nlmsg_pid;
1409 if (build_aevent(r_skb, x, &c) < 0)
1410 BUG();
1411 err = netlink_unicast(xfrm_nl, r_skb,
1412 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1413 spin_unlock_bh(&x->lock);
1414 xfrm_state_put(x);
1415 return err;
1418 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1420 struct xfrm_state *x;
1421 struct km_event c;
1422 int err = - EINVAL;
1423 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1424 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1425 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1427 if (!lt && !rp)
1428 return err;
1430 /* pedantic mode - thou shalt sayeth replaceth */
1431 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1432 return err;
1434 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1435 if (x == NULL)
1436 return -ESRCH;
1438 if (x->km.state != XFRM_STATE_VALID)
1439 goto out;
1441 spin_lock_bh(&x->lock);
1442 err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
1443 spin_unlock_bh(&x->lock);
1444 if (err < 0)
1445 goto out;
1447 c.event = nlh->nlmsg_type;
1448 c.seq = nlh->nlmsg_seq;
1449 c.pid = nlh->nlmsg_pid;
1450 c.data.aevent = XFRM_AE_CU;
1451 km_state_notify(x, &c);
1452 err = 0;
1453 out:
1454 xfrm_state_put(x);
1455 return err;
1458 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1460 struct km_event c;
1461 u8 type = XFRM_POLICY_TYPE_MAIN;
1462 int err;
1463 struct xfrm_audit audit_info;
1465 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1466 if (err)
1467 return err;
1469 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1470 audit_info.secid = NETLINK_CB(skb).sid;
1471 xfrm_policy_flush(type, &audit_info);
1472 c.data.type = type;
1473 c.event = nlh->nlmsg_type;
1474 c.seq = nlh->nlmsg_seq;
1475 c.pid = nlh->nlmsg_pid;
1476 km_policy_notify(NULL, 0, &c);
1477 return 0;
1480 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1482 struct xfrm_policy *xp;
1483 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1484 struct xfrm_userpolicy_info *p = &up->pol;
1485 u8 type = XFRM_POLICY_TYPE_MAIN;
1486 int err = -ENOENT;
1488 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1489 if (err)
1490 return err;
1492 if (p->index)
1493 xp = xfrm_policy_byid(type, p->dir, p->index, 0);
1494 else {
1495 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1496 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1497 struct xfrm_policy tmp;
1499 err = verify_sec_ctx_len(rtattrs);
1500 if (err)
1501 return err;
1503 memset(&tmp, 0, sizeof(struct xfrm_policy));
1504 if (rt) {
1505 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1507 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1508 return err;
1510 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, 0);
1511 security_xfrm_policy_free(&tmp);
1514 if (xp == NULL)
1515 return err;
1516 read_lock(&xp->lock);
1517 if (xp->dead) {
1518 read_unlock(&xp->lock);
1519 goto out;
1522 read_unlock(&xp->lock);
1523 err = 0;
1524 if (up->hard) {
1525 xfrm_policy_delete(xp, p->dir);
1526 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1527 AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
1529 } else {
1530 // reset the timers here?
1531 printk("Dont know what to do with soft policy expire\n");
1533 km_policy_expired(xp, p->dir, up->hard, current->pid);
1535 out:
1536 xfrm_pol_put(xp);
1537 return err;
1540 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1542 struct xfrm_state *x;
1543 int err;
1544 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1545 struct xfrm_usersa_info *p = &ue->state;
1547 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1548 err = -ENOENT;
1550 if (x == NULL)
1551 return err;
1553 err = -EINVAL;
1555 spin_lock_bh(&x->lock);
1556 if (x->km.state != XFRM_STATE_VALID)
1557 goto out;
1558 km_state_expired(x, ue->hard, current->pid);
1560 if (ue->hard) {
1561 __xfrm_state_delete(x);
1562 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1563 AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
1565 out:
1566 spin_unlock_bh(&x->lock);
1567 xfrm_state_put(x);
1568 return err;
1571 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1573 struct xfrm_policy *xp;
1574 struct xfrm_user_tmpl *ut;
1575 int i;
1576 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1578 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1579 struct xfrm_state *x = xfrm_state_alloc();
1580 int err = -ENOMEM;
1582 if (!x)
1583 return err;
1585 err = verify_newpolicy_info(&ua->policy);
1586 if (err) {
1587 printk("BAD policy passed\n");
1588 kfree(x);
1589 return err;
1592 /* build an XP */
1593 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);
1594 if (!xp) {
1595 kfree(x);
1596 return err;
1599 memcpy(&x->id, &ua->id, sizeof(ua->id));
1600 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1601 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1603 ut = RTA_DATA(rt);
1604 /* extract the templates and for each call km_key */
1605 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1606 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1607 memcpy(&x->id, &t->id, sizeof(x->id));
1608 x->props.mode = t->mode;
1609 x->props.reqid = t->reqid;
1610 x->props.family = ut->family;
1611 t->aalgos = ua->aalgos;
1612 t->ealgos = ua->ealgos;
1613 t->calgos = ua->calgos;
1614 err = km_query(x, t, xp);
1618 kfree(x);
1619 kfree(xp);
1621 return 0;
1625 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1627 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1628 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1629 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1630 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1631 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1632 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1633 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1634 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1635 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1636 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1637 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1638 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1639 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1640 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1641 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
1642 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1643 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1644 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
1647 #undef XMSGSIZE
1649 static struct xfrm_link {
1650 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1651 int (*dump)(struct sk_buff *, struct netlink_callback *);
1652 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1653 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1654 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1655 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1656 .dump = xfrm_dump_sa },
1657 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1658 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1659 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1660 .dump = xfrm_dump_policy },
1661 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1662 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
1663 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1664 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1665 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1666 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1667 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1668 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
1669 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1670 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1673 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1675 struct rtattr *xfrma[XFRMA_MAX];
1676 struct xfrm_link *link;
1677 int type, min_len;
1679 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1680 return 0;
1682 type = nlh->nlmsg_type;
1684 /* A control message: ignore them */
1685 if (type < XFRM_MSG_BASE)
1686 return 0;
1688 /* Unknown message: reply with EINVAL */
1689 if (type > XFRM_MSG_MAX)
1690 goto err_einval;
1692 type -= XFRM_MSG_BASE;
1693 link = &xfrm_dispatch[type];
1695 /* All operations require privileges, even GET */
1696 if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
1697 *errp = -EPERM;
1698 return -1;
1701 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1702 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1703 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1704 if (link->dump == NULL)
1705 goto err_einval;
1707 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
1708 link->dump, NULL)) != 0) {
1709 return -1;
1712 netlink_queue_skip(nlh, skb);
1713 return -1;
1716 memset(xfrma, 0, sizeof(xfrma));
1718 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1719 goto err_einval;
1721 if (nlh->nlmsg_len > min_len) {
1722 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1723 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1725 while (RTA_OK(attr, attrlen)) {
1726 unsigned short flavor = attr->rta_type;
1727 if (flavor) {
1728 if (flavor > XFRMA_MAX)
1729 goto err_einval;
1730 xfrma[flavor - 1] = attr;
1732 attr = RTA_NEXT(attr, attrlen);
1736 if (link->doit == NULL)
1737 goto err_einval;
1738 *errp = link->doit(skb, nlh, (void **) &xfrma);
1740 return *errp;
1742 err_einval:
1743 *errp = -EINVAL;
1744 return -1;
1747 static void xfrm_netlink_rcv(struct sock *sk, int len)
1749 unsigned int qlen = 0;
1751 do {
1752 mutex_lock(&xfrm_cfg_mutex);
1753 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
1754 mutex_unlock(&xfrm_cfg_mutex);
1756 } while (qlen);
1759 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1761 struct xfrm_user_expire *ue;
1762 struct nlmsghdr *nlh;
1763 unsigned char *b = skb->tail;
1765 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
1766 sizeof(*ue));
1767 ue = NLMSG_DATA(nlh);
1768 nlh->nlmsg_flags = 0;
1770 copy_to_user_state(x, &ue->state);
1771 ue->hard = (c->data.hard != 0) ? 1 : 0;
1773 nlh->nlmsg_len = skb->tail - b;
1774 return skb->len;
1776 nlmsg_failure:
1777 skb_trim(skb, b - skb->data);
1778 return -1;
1781 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1783 struct sk_buff *skb;
1784 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
1786 skb = alloc_skb(len, GFP_ATOMIC);
1787 if (skb == NULL)
1788 return -ENOMEM;
1790 if (build_expire(skb, x, c) < 0)
1791 BUG();
1793 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1794 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1797 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1799 struct sk_buff *skb;
1800 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1802 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1803 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1804 skb = alloc_skb(len, GFP_ATOMIC);
1805 if (skb == NULL)
1806 return -ENOMEM;
1808 if (build_aevent(skb, x, c) < 0)
1809 BUG();
1811 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1812 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1815 static int xfrm_notify_sa_flush(struct km_event *c)
1817 struct xfrm_usersa_flush *p;
1818 struct nlmsghdr *nlh;
1819 struct sk_buff *skb;
1820 unsigned char *b;
1821 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1823 skb = alloc_skb(len, GFP_ATOMIC);
1824 if (skb == NULL)
1825 return -ENOMEM;
1826 b = skb->tail;
1828 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1829 XFRM_MSG_FLUSHSA, sizeof(*p));
1830 nlh->nlmsg_flags = 0;
1832 p = NLMSG_DATA(nlh);
1833 p->proto = c->data.proto;
1835 nlh->nlmsg_len = skb->tail - b;
1837 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1838 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1840 nlmsg_failure:
1841 kfree_skb(skb);
1842 return -1;
1845 static int inline xfrm_sa_len(struct xfrm_state *x)
1847 int l = 0;
1848 if (x->aalg)
1849 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1850 if (x->ealg)
1851 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1852 if (x->calg)
1853 l += RTA_SPACE(sizeof(*x->calg));
1854 if (x->encap)
1855 l += RTA_SPACE(sizeof(*x->encap));
1857 return l;
1860 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1862 struct xfrm_usersa_info *p;
1863 struct xfrm_usersa_id *id;
1864 struct nlmsghdr *nlh;
1865 struct sk_buff *skb;
1866 unsigned char *b;
1867 int len = xfrm_sa_len(x);
1868 int headlen;
1870 headlen = sizeof(*p);
1871 if (c->event == XFRM_MSG_DELSA) {
1872 len += RTA_SPACE(headlen);
1873 headlen = sizeof(*id);
1875 len += NLMSG_SPACE(headlen);
1877 skb = alloc_skb(len, GFP_ATOMIC);
1878 if (skb == NULL)
1879 return -ENOMEM;
1880 b = skb->tail;
1882 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
1883 nlh->nlmsg_flags = 0;
1885 p = NLMSG_DATA(nlh);
1886 if (c->event == XFRM_MSG_DELSA) {
1887 id = NLMSG_DATA(nlh);
1888 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1889 id->spi = x->id.spi;
1890 id->family = x->props.family;
1891 id->proto = x->id.proto;
1893 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1896 copy_to_user_state(x, p);
1898 if (x->aalg)
1899 RTA_PUT(skb, XFRMA_ALG_AUTH,
1900 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1901 if (x->ealg)
1902 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1903 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1904 if (x->calg)
1905 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1907 if (x->encap)
1908 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1910 nlh->nlmsg_len = skb->tail - b;
1912 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1913 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1915 nlmsg_failure:
1916 rtattr_failure:
1917 kfree_skb(skb);
1918 return -1;
1921 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1924 switch (c->event) {
1925 case XFRM_MSG_EXPIRE:
1926 return xfrm_exp_state_notify(x, c);
1927 case XFRM_MSG_NEWAE:
1928 return xfrm_aevent_state_notify(x, c);
1929 case XFRM_MSG_DELSA:
1930 case XFRM_MSG_UPDSA:
1931 case XFRM_MSG_NEWSA:
1932 return xfrm_notify_sa(x, c);
1933 case XFRM_MSG_FLUSHSA:
1934 return xfrm_notify_sa_flush(c);
1935 default:
1936 printk("xfrm_user: Unknown SA event %d\n", c->event);
1937 break;
1940 return 0;
1944 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1945 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1946 int dir)
1948 struct xfrm_user_acquire *ua;
1949 struct nlmsghdr *nlh;
1950 unsigned char *b = skb->tail;
1951 __u32 seq = xfrm_get_acqseq();
1953 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1954 sizeof(*ua));
1955 ua = NLMSG_DATA(nlh);
1956 nlh->nlmsg_flags = 0;
1958 memcpy(&ua->id, &x->id, sizeof(ua->id));
1959 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1960 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1961 copy_to_user_policy(xp, &ua->policy, dir);
1962 ua->aalgos = xt->aalgos;
1963 ua->ealgos = xt->ealgos;
1964 ua->calgos = xt->calgos;
1965 ua->seq = x->km.seq = seq;
1967 if (copy_to_user_tmpl(xp, skb) < 0)
1968 goto nlmsg_failure;
1969 if (copy_to_user_state_sec_ctx(x, skb))
1970 goto nlmsg_failure;
1971 if (copy_to_user_policy_type(xp->type, skb) < 0)
1972 goto nlmsg_failure;
1974 nlh->nlmsg_len = skb->tail - b;
1975 return skb->len;
1977 nlmsg_failure:
1978 skb_trim(skb, b - skb->data);
1979 return -1;
1982 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1983 struct xfrm_policy *xp, int dir)
1985 struct sk_buff *skb;
1986 size_t len;
1988 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1989 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
1990 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1991 #ifdef CONFIG_XFRM_SUB_POLICY
1992 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
1993 #endif
1994 skb = alloc_skb(len, GFP_ATOMIC);
1995 if (skb == NULL)
1996 return -ENOMEM;
1998 if (build_acquire(skb, x, xt, xp, dir) < 0)
1999 BUG();
2001 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
2002 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2005 /* User gives us xfrm_user_policy_info followed by an array of 0
2006 * or more templates.
2008 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2009 u8 *data, int len, int *dir)
2011 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2012 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2013 struct xfrm_policy *xp;
2014 int nr;
2016 switch (sk->sk_family) {
2017 case AF_INET:
2018 if (opt != IP_XFRM_POLICY) {
2019 *dir = -EOPNOTSUPP;
2020 return NULL;
2022 break;
2023 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2024 case AF_INET6:
2025 if (opt != IPV6_XFRM_POLICY) {
2026 *dir = -EOPNOTSUPP;
2027 return NULL;
2029 break;
2030 #endif
2031 default:
2032 *dir = -EINVAL;
2033 return NULL;
2036 *dir = -EINVAL;
2038 if (len < sizeof(*p) ||
2039 verify_newpolicy_info(p))
2040 return NULL;
2042 nr = ((len - sizeof(*p)) / sizeof(*ut));
2043 if (validate_tmpl(nr, ut, p->sel.family))
2044 return NULL;
2046 if (p->dir > XFRM_POLICY_OUT)
2047 return NULL;
2049 xp = xfrm_policy_alloc(GFP_KERNEL);
2050 if (xp == NULL) {
2051 *dir = -ENOBUFS;
2052 return NULL;
2055 copy_from_user_policy(xp, p);
2056 xp->type = XFRM_POLICY_TYPE_MAIN;
2057 copy_templates(xp, ut, nr);
2059 *dir = p->dir;
2061 return xp;
2064 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2065 int dir, struct km_event *c)
2067 struct xfrm_user_polexpire *upe;
2068 struct nlmsghdr *nlh;
2069 int hard = c->data.hard;
2070 unsigned char *b = skb->tail;
2072 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
2073 upe = NLMSG_DATA(nlh);
2074 nlh->nlmsg_flags = 0;
2076 copy_to_user_policy(xp, &upe->pol, dir);
2077 if (copy_to_user_tmpl(xp, skb) < 0)
2078 goto nlmsg_failure;
2079 if (copy_to_user_sec_ctx(xp, skb))
2080 goto nlmsg_failure;
2081 if (copy_to_user_policy_type(xp->type, skb) < 0)
2082 goto nlmsg_failure;
2083 upe->hard = !!hard;
2085 nlh->nlmsg_len = skb->tail - b;
2086 return skb->len;
2088 nlmsg_failure:
2089 skb_trim(skb, b - skb->data);
2090 return -1;
2093 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2095 struct sk_buff *skb;
2096 size_t len;
2098 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2099 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
2100 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
2101 #ifdef CONFIG_XFRM_SUB_POLICY
2102 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2103 #endif
2104 skb = alloc_skb(len, GFP_ATOMIC);
2105 if (skb == NULL)
2106 return -ENOMEM;
2108 if (build_polexpire(skb, xp, dir, c) < 0)
2109 BUG();
2111 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
2112 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2115 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2117 struct xfrm_userpolicy_info *p;
2118 struct xfrm_userpolicy_id *id;
2119 struct nlmsghdr *nlh;
2120 struct sk_buff *skb;
2121 unsigned char *b;
2122 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2123 int headlen;
2125 headlen = sizeof(*p);
2126 if (c->event == XFRM_MSG_DELPOLICY) {
2127 len += RTA_SPACE(headlen);
2128 headlen = sizeof(*id);
2130 #ifdef CONFIG_XFRM_SUB_POLICY
2131 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2132 #endif
2133 len += NLMSG_SPACE(headlen);
2135 skb = alloc_skb(len, GFP_ATOMIC);
2136 if (skb == NULL)
2137 return -ENOMEM;
2138 b = skb->tail;
2140 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
2142 p = NLMSG_DATA(nlh);
2143 if (c->event == XFRM_MSG_DELPOLICY) {
2144 id = NLMSG_DATA(nlh);
2145 memset(id, 0, sizeof(*id));
2146 id->dir = dir;
2147 if (c->data.byid)
2148 id->index = xp->index;
2149 else
2150 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2152 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
2155 nlh->nlmsg_flags = 0;
2157 copy_to_user_policy(xp, p, dir);
2158 if (copy_to_user_tmpl(xp, skb) < 0)
2159 goto nlmsg_failure;
2160 if (copy_to_user_policy_type(xp->type, skb) < 0)
2161 goto nlmsg_failure;
2163 nlh->nlmsg_len = skb->tail - b;
2165 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2166 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2168 nlmsg_failure:
2169 rtattr_failure:
2170 kfree_skb(skb);
2171 return -1;
2174 static int xfrm_notify_policy_flush(struct km_event *c)
2176 struct nlmsghdr *nlh;
2177 struct sk_buff *skb;
2178 unsigned char *b;
2179 int len = 0;
2180 #ifdef CONFIG_XFRM_SUB_POLICY
2181 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2182 #endif
2183 len += NLMSG_LENGTH(0);
2185 skb = alloc_skb(len, GFP_ATOMIC);
2186 if (skb == NULL)
2187 return -ENOMEM;
2188 b = skb->tail;
2191 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
2192 nlh->nlmsg_flags = 0;
2193 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2194 goto nlmsg_failure;
2196 nlh->nlmsg_len = skb->tail - b;
2198 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2199 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2201 nlmsg_failure:
2202 kfree_skb(skb);
2203 return -1;
2206 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2209 switch (c->event) {
2210 case XFRM_MSG_NEWPOLICY:
2211 case XFRM_MSG_UPDPOLICY:
2212 case XFRM_MSG_DELPOLICY:
2213 return xfrm_notify_policy(xp, dir, c);
2214 case XFRM_MSG_FLUSHPOLICY:
2215 return xfrm_notify_policy_flush(c);
2216 case XFRM_MSG_POLEXPIRE:
2217 return xfrm_exp_policy_notify(xp, dir, c);
2218 default:
2219 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2222 return 0;
2226 static int build_report(struct sk_buff *skb, u8 proto,
2227 struct xfrm_selector *sel, xfrm_address_t *addr)
2229 struct xfrm_user_report *ur;
2230 struct nlmsghdr *nlh;
2231 unsigned char *b = skb->tail;
2233 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur));
2234 ur = NLMSG_DATA(nlh);
2235 nlh->nlmsg_flags = 0;
2237 ur->proto = proto;
2238 memcpy(&ur->sel, sel, sizeof(ur->sel));
2240 if (addr)
2241 RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2243 nlh->nlmsg_len = skb->tail - b;
2244 return skb->len;
2246 nlmsg_failure:
2247 rtattr_failure:
2248 skb_trim(skb, b - skb->data);
2249 return -1;
2252 static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2253 xfrm_address_t *addr)
2255 struct sk_buff *skb;
2256 size_t len;
2258 len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
2259 skb = alloc_skb(len, GFP_ATOMIC);
2260 if (skb == NULL)
2261 return -ENOMEM;
2263 if (build_report(skb, proto, sel, addr) < 0)
2264 BUG();
2266 NETLINK_CB(skb).dst_group = XFRMNLGRP_REPORT;
2267 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2270 static struct xfrm_mgr netlink_mgr = {
2271 .id = "netlink",
2272 .notify = xfrm_send_state_notify,
2273 .acquire = xfrm_send_acquire,
2274 .compile_policy = xfrm_compile_policy,
2275 .notify_policy = xfrm_send_policy_notify,
2276 .report = xfrm_send_report,
2279 static int __init xfrm_user_init(void)
2281 struct sock *nlsk;
2283 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2285 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
2286 xfrm_netlink_rcv, THIS_MODULE);
2287 if (nlsk == NULL)
2288 return -ENOMEM;
2289 rcu_assign_pointer(xfrm_nl, nlsk);
2291 xfrm_register_km(&netlink_mgr);
2293 return 0;
2296 static void __exit xfrm_user_exit(void)
2298 struct sock *nlsk = xfrm_nl;
2300 xfrm_unregister_km(&netlink_mgr);
2301 rcu_assign_pointer(xfrm_nl, NULL);
2302 synchronize_rcu();
2303 sock_release(nlsk->sk_socket);
2306 module_init(xfrm_user_init);
2307 module_exit(xfrm_user_exit);
2308 MODULE_LICENSE("GPL");
2309 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);