OMAP3430SDP: Enable config options CONFIG_OMAP_RESET_CLOCKS and CONFIG_SUSPEND
[linux-ginger.git] / net / xfrm / xfrm_user.c
bloba1b0fbe3ea351f6e7e953078ffd90d29dd29e196
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/pfkeyv2.h>
23 #include <linux/ipsec.h>
24 #include <linux/init.h>
25 #include <linux/security.h>
26 #include <net/sock.h>
27 #include <net/xfrm.h>
28 #include <net/netlink.h>
29 #include <asm/uaccess.h>
30 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
31 #include <linux/in6.h>
32 #endif
34 static inline int aead_len(struct xfrm_algo_aead *alg)
36 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
39 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
41 struct nlattr *rt = attrs[type];
42 struct xfrm_algo *algp;
44 if (!rt)
45 return 0;
47 algp = nla_data(rt);
48 if (nla_len(rt) < xfrm_alg_len(algp))
49 return -EINVAL;
51 switch (type) {
52 case XFRMA_ALG_AUTH:
53 if (!algp->alg_key_len &&
54 strcmp(algp->alg_name, "digest_null") != 0)
55 return -EINVAL;
56 break;
58 case XFRMA_ALG_CRYPT:
59 if (!algp->alg_key_len &&
60 strcmp(algp->alg_name, "cipher_null") != 0)
61 return -EINVAL;
62 break;
64 case XFRMA_ALG_COMP:
65 /* Zero length keys are legal. */
66 break;
68 default:
69 return -EINVAL;
72 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
73 return 0;
76 static int verify_aead(struct nlattr **attrs)
78 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
79 struct xfrm_algo_aead *algp;
81 if (!rt)
82 return 0;
84 algp = nla_data(rt);
85 if (nla_len(rt) < aead_len(algp))
86 return -EINVAL;
88 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
89 return 0;
92 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
93 xfrm_address_t **addrp)
95 struct nlattr *rt = attrs[type];
97 if (rt && addrp)
98 *addrp = nla_data(rt);
101 static inline int verify_sec_ctx_len(struct nlattr **attrs)
103 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
104 struct xfrm_user_sec_ctx *uctx;
106 if (!rt)
107 return 0;
109 uctx = nla_data(rt);
110 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
111 return -EINVAL;
113 return 0;
117 static int verify_newsa_info(struct xfrm_usersa_info *p,
118 struct nlattr **attrs)
120 int err;
122 err = -EINVAL;
123 switch (p->family) {
124 case AF_INET:
125 break;
127 case AF_INET6:
128 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
129 break;
130 #else
131 err = -EAFNOSUPPORT;
132 goto out;
133 #endif
135 default:
136 goto out;
139 err = -EINVAL;
140 switch (p->id.proto) {
141 case IPPROTO_AH:
142 if (!attrs[XFRMA_ALG_AUTH] ||
143 attrs[XFRMA_ALG_AEAD] ||
144 attrs[XFRMA_ALG_CRYPT] ||
145 attrs[XFRMA_ALG_COMP])
146 goto out;
147 break;
149 case IPPROTO_ESP:
150 if (attrs[XFRMA_ALG_COMP])
151 goto out;
152 if (!attrs[XFRMA_ALG_AUTH] &&
153 !attrs[XFRMA_ALG_CRYPT] &&
154 !attrs[XFRMA_ALG_AEAD])
155 goto out;
156 if ((attrs[XFRMA_ALG_AUTH] ||
157 attrs[XFRMA_ALG_CRYPT]) &&
158 attrs[XFRMA_ALG_AEAD])
159 goto out;
160 break;
162 case IPPROTO_COMP:
163 if (!attrs[XFRMA_ALG_COMP] ||
164 attrs[XFRMA_ALG_AEAD] ||
165 attrs[XFRMA_ALG_AUTH] ||
166 attrs[XFRMA_ALG_CRYPT])
167 goto out;
168 break;
170 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
171 case IPPROTO_DSTOPTS:
172 case IPPROTO_ROUTING:
173 if (attrs[XFRMA_ALG_COMP] ||
174 attrs[XFRMA_ALG_AUTH] ||
175 attrs[XFRMA_ALG_AEAD] ||
176 attrs[XFRMA_ALG_CRYPT] ||
177 attrs[XFRMA_ENCAP] ||
178 attrs[XFRMA_SEC_CTX] ||
179 !attrs[XFRMA_COADDR])
180 goto out;
181 break;
182 #endif
184 default:
185 goto out;
188 if ((err = verify_aead(attrs)))
189 goto out;
190 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
191 goto out;
192 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
193 goto out;
194 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
195 goto out;
196 if ((err = verify_sec_ctx_len(attrs)))
197 goto out;
199 err = -EINVAL;
200 switch (p->mode) {
201 case XFRM_MODE_TRANSPORT:
202 case XFRM_MODE_TUNNEL:
203 case XFRM_MODE_ROUTEOPTIMIZATION:
204 case XFRM_MODE_BEET:
205 break;
207 default:
208 goto out;
211 err = 0;
213 out:
214 return err;
217 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
218 struct xfrm_algo_desc *(*get_byname)(char *, int),
219 struct nlattr *rta)
221 struct xfrm_algo *p, *ualg;
222 struct xfrm_algo_desc *algo;
224 if (!rta)
225 return 0;
227 ualg = nla_data(rta);
229 algo = get_byname(ualg->alg_name, 1);
230 if (!algo)
231 return -ENOSYS;
232 *props = algo->desc.sadb_alg_id;
234 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
235 if (!p)
236 return -ENOMEM;
238 strcpy(p->alg_name, algo->name);
239 *algpp = p;
240 return 0;
243 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
244 struct nlattr *rta)
246 struct xfrm_algo_aead *p, *ualg;
247 struct xfrm_algo_desc *algo;
249 if (!rta)
250 return 0;
252 ualg = nla_data(rta);
254 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
255 if (!algo)
256 return -ENOSYS;
257 *props = algo->desc.sadb_alg_id;
259 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
260 if (!p)
261 return -ENOMEM;
263 strcpy(p->alg_name, algo->name);
264 *algpp = p;
265 return 0;
268 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
270 int len = 0;
272 if (xfrm_ctx) {
273 len += sizeof(struct xfrm_user_sec_ctx);
274 len += xfrm_ctx->ctx_len;
276 return len;
279 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
281 memcpy(&x->id, &p->id, sizeof(x->id));
282 memcpy(&x->sel, &p->sel, sizeof(x->sel));
283 memcpy(&x->lft, &p->lft, sizeof(x->lft));
284 x->props.mode = p->mode;
285 x->props.replay_window = p->replay_window;
286 x->props.reqid = p->reqid;
287 x->props.family = p->family;
288 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
289 x->props.flags = p->flags;
291 if (!x->sel.family)
292 x->sel.family = p->family;
297 * someday when pfkey also has support, we could have the code
298 * somehow made shareable and move it to xfrm_state.c - JHS
301 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
303 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
304 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
305 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
306 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
308 if (rp) {
309 struct xfrm_replay_state *replay;
310 replay = nla_data(rp);
311 memcpy(&x->replay, replay, sizeof(*replay));
312 memcpy(&x->preplay, replay, sizeof(*replay));
315 if (lt) {
316 struct xfrm_lifetime_cur *ltime;
317 ltime = nla_data(lt);
318 x->curlft.bytes = ltime->bytes;
319 x->curlft.packets = ltime->packets;
320 x->curlft.add_time = ltime->add_time;
321 x->curlft.use_time = ltime->use_time;
324 if (et)
325 x->replay_maxage = nla_get_u32(et);
327 if (rt)
328 x->replay_maxdiff = nla_get_u32(rt);
331 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
332 struct nlattr **attrs,
333 int *errp)
335 struct xfrm_state *x = xfrm_state_alloc();
336 int err = -ENOMEM;
338 if (!x)
339 goto error_no_put;
341 copy_from_user_state(x, p);
343 if ((err = attach_aead(&x->aead, &x->props.ealgo,
344 attrs[XFRMA_ALG_AEAD])))
345 goto error;
346 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
347 xfrm_aalg_get_byname,
348 attrs[XFRMA_ALG_AUTH])))
349 goto error;
350 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
351 xfrm_ealg_get_byname,
352 attrs[XFRMA_ALG_CRYPT])))
353 goto error;
354 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
355 xfrm_calg_get_byname,
356 attrs[XFRMA_ALG_COMP])))
357 goto error;
359 if (attrs[XFRMA_ENCAP]) {
360 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
361 sizeof(*x->encap), GFP_KERNEL);
362 if (x->encap == NULL)
363 goto error;
366 if (attrs[XFRMA_COADDR]) {
367 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
368 sizeof(*x->coaddr), GFP_KERNEL);
369 if (x->coaddr == NULL)
370 goto error;
373 err = xfrm_init_state(x);
374 if (err)
375 goto error;
377 if (attrs[XFRMA_SEC_CTX] &&
378 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
379 goto error;
381 x->km.seq = p->seq;
382 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
383 /* sysctl_xfrm_aevent_etime is in 100ms units */
384 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
385 x->preplay.bitmap = 0;
386 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
387 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
389 /* override default values from above */
391 xfrm_update_ae_params(x, attrs);
393 return x;
395 error:
396 x->km.state = XFRM_STATE_DEAD;
397 xfrm_state_put(x);
398 error_no_put:
399 *errp = err;
400 return NULL;
403 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
404 struct nlattr **attrs)
406 struct xfrm_usersa_info *p = nlmsg_data(nlh);
407 struct xfrm_state *x;
408 int err;
409 struct km_event c;
410 uid_t loginuid = NETLINK_CB(skb).loginuid;
411 u32 sessionid = NETLINK_CB(skb).sessionid;
412 u32 sid = NETLINK_CB(skb).sid;
414 err = verify_newsa_info(p, attrs);
415 if (err)
416 return err;
418 x = xfrm_state_construct(p, attrs, &err);
419 if (!x)
420 return err;
422 xfrm_state_hold(x);
423 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
424 err = xfrm_state_add(x);
425 else
426 err = xfrm_state_update(x);
428 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
430 if (err < 0) {
431 x->km.state = XFRM_STATE_DEAD;
432 __xfrm_state_put(x);
433 goto out;
436 c.seq = nlh->nlmsg_seq;
437 c.pid = nlh->nlmsg_pid;
438 c.event = nlh->nlmsg_type;
440 km_state_notify(x, &c);
441 out:
442 xfrm_state_put(x);
443 return err;
446 static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
447 struct nlattr **attrs,
448 int *errp)
450 struct xfrm_state *x = NULL;
451 int err;
453 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
454 err = -ESRCH;
455 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
456 } else {
457 xfrm_address_t *saddr = NULL;
459 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
460 if (!saddr) {
461 err = -EINVAL;
462 goto out;
465 err = -ESRCH;
466 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
467 p->family);
470 out:
471 if (!x && errp)
472 *errp = err;
473 return x;
476 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
477 struct nlattr **attrs)
479 struct xfrm_state *x;
480 int err = -ESRCH;
481 struct km_event c;
482 struct xfrm_usersa_id *p = nlmsg_data(nlh);
483 uid_t loginuid = NETLINK_CB(skb).loginuid;
484 u32 sessionid = NETLINK_CB(skb).sessionid;
485 u32 sid = NETLINK_CB(skb).sid;
487 x = xfrm_user_state_lookup(p, attrs, &err);
488 if (x == NULL)
489 return err;
491 if ((err = security_xfrm_state_delete(x)) != 0)
492 goto out;
494 if (xfrm_state_kern(x)) {
495 err = -EPERM;
496 goto out;
499 err = xfrm_state_delete(x);
501 if (err < 0)
502 goto out;
504 c.seq = nlh->nlmsg_seq;
505 c.pid = nlh->nlmsg_pid;
506 c.event = nlh->nlmsg_type;
507 km_state_notify(x, &c);
509 out:
510 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
511 xfrm_state_put(x);
512 return err;
515 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
517 memcpy(&p->id, &x->id, sizeof(p->id));
518 memcpy(&p->sel, &x->sel, sizeof(p->sel));
519 memcpy(&p->lft, &x->lft, sizeof(p->lft));
520 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
521 memcpy(&p->stats, &x->stats, sizeof(p->stats));
522 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
523 p->mode = x->props.mode;
524 p->replay_window = x->props.replay_window;
525 p->reqid = x->props.reqid;
526 p->family = x->props.family;
527 p->flags = x->props.flags;
528 p->seq = x->km.seq;
531 struct xfrm_dump_info {
532 struct sk_buff *in_skb;
533 struct sk_buff *out_skb;
534 u32 nlmsg_seq;
535 u16 nlmsg_flags;
538 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
540 struct xfrm_user_sec_ctx *uctx;
541 struct nlattr *attr;
542 int ctx_size = sizeof(*uctx) + s->ctx_len;
544 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
545 if (attr == NULL)
546 return -EMSGSIZE;
548 uctx = nla_data(attr);
549 uctx->exttype = XFRMA_SEC_CTX;
550 uctx->len = ctx_size;
551 uctx->ctx_doi = s->ctx_doi;
552 uctx->ctx_alg = s->ctx_alg;
553 uctx->ctx_len = s->ctx_len;
554 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
556 return 0;
559 /* Don't change this without updating xfrm_sa_len! */
560 static int copy_to_user_state_extra(struct xfrm_state *x,
561 struct xfrm_usersa_info *p,
562 struct sk_buff *skb)
564 copy_to_user_state(x, p);
566 if (x->coaddr)
567 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
569 if (x->lastused)
570 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
572 if (x->aead)
573 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
574 if (x->aalg)
575 NLA_PUT(skb, XFRMA_ALG_AUTH, xfrm_alg_len(x->aalg), x->aalg);
576 if (x->ealg)
577 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
578 if (x->calg)
579 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
581 if (x->encap)
582 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
584 if (x->security && copy_sec_ctx(x->security, skb) < 0)
585 goto nla_put_failure;
587 return 0;
589 nla_put_failure:
590 return -EMSGSIZE;
593 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
595 struct xfrm_dump_info *sp = ptr;
596 struct sk_buff *in_skb = sp->in_skb;
597 struct sk_buff *skb = sp->out_skb;
598 struct xfrm_usersa_info *p;
599 struct nlmsghdr *nlh;
600 int err;
602 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
603 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
604 if (nlh == NULL)
605 return -EMSGSIZE;
607 p = nlmsg_data(nlh);
609 err = copy_to_user_state_extra(x, p, skb);
610 if (err)
611 goto nla_put_failure;
613 nlmsg_end(skb, nlh);
614 return 0;
616 nla_put_failure:
617 nlmsg_cancel(skb, nlh);
618 return err;
621 static int xfrm_dump_sa_done(struct netlink_callback *cb)
623 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
624 xfrm_state_walk_done(walk);
625 return 0;
628 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
630 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
631 struct xfrm_dump_info info;
633 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
634 sizeof(cb->args) - sizeof(cb->args[0]));
636 info.in_skb = cb->skb;
637 info.out_skb = skb;
638 info.nlmsg_seq = cb->nlh->nlmsg_seq;
639 info.nlmsg_flags = NLM_F_MULTI;
641 if (!cb->args[0]) {
642 cb->args[0] = 1;
643 xfrm_state_walk_init(walk, 0);
646 (void) xfrm_state_walk(walk, dump_one_state, &info);
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 = nlmsg_new(NLMSG_DEFAULT_SIZE, 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;
666 if (dump_one_state(x, 0, &info)) {
667 kfree_skb(skb);
668 return NULL;
671 return skb;
674 static inline size_t xfrm_spdinfo_msgsize(void)
676 return NLMSG_ALIGN(4)
677 + nla_total_size(sizeof(struct xfrmu_spdinfo))
678 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
681 static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
683 struct xfrmk_spdinfo si;
684 struct xfrmu_spdinfo spc;
685 struct xfrmu_spdhinfo sph;
686 struct nlmsghdr *nlh;
687 u32 *f;
689 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
690 if (nlh == NULL) /* shouldnt really happen ... */
691 return -EMSGSIZE;
693 f = nlmsg_data(nlh);
694 *f = flags;
695 xfrm_spd_getinfo(&si);
696 spc.incnt = si.incnt;
697 spc.outcnt = si.outcnt;
698 spc.fwdcnt = si.fwdcnt;
699 spc.inscnt = si.inscnt;
700 spc.outscnt = si.outscnt;
701 spc.fwdscnt = si.fwdscnt;
702 sph.spdhcnt = si.spdhcnt;
703 sph.spdhmcnt = si.spdhmcnt;
705 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
706 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
708 return nlmsg_end(skb, nlh);
710 nla_put_failure:
711 nlmsg_cancel(skb, nlh);
712 return -EMSGSIZE;
715 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
716 struct nlattr **attrs)
718 struct sk_buff *r_skb;
719 u32 *flags = nlmsg_data(nlh);
720 u32 spid = NETLINK_CB(skb).pid;
721 u32 seq = nlh->nlmsg_seq;
723 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
724 if (r_skb == NULL)
725 return -ENOMEM;
727 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
728 BUG();
730 return nlmsg_unicast(xfrm_nl, r_skb, spid);
733 static inline size_t xfrm_sadinfo_msgsize(void)
735 return NLMSG_ALIGN(4)
736 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
737 + nla_total_size(4); /* XFRMA_SAD_CNT */
740 static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
742 struct xfrmk_sadinfo si;
743 struct xfrmu_sadhinfo sh;
744 struct nlmsghdr *nlh;
745 u32 *f;
747 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
748 if (nlh == NULL) /* shouldnt really happen ... */
749 return -EMSGSIZE;
751 f = nlmsg_data(nlh);
752 *f = flags;
753 xfrm_sad_getinfo(&si);
755 sh.sadhmcnt = si.sadhmcnt;
756 sh.sadhcnt = si.sadhcnt;
758 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
759 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
761 return nlmsg_end(skb, nlh);
763 nla_put_failure:
764 nlmsg_cancel(skb, nlh);
765 return -EMSGSIZE;
768 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
769 struct nlattr **attrs)
771 struct sk_buff *r_skb;
772 u32 *flags = nlmsg_data(nlh);
773 u32 spid = NETLINK_CB(skb).pid;
774 u32 seq = nlh->nlmsg_seq;
776 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
777 if (r_skb == NULL)
778 return -ENOMEM;
780 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
781 BUG();
783 return nlmsg_unicast(xfrm_nl, r_skb, spid);
786 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
787 struct nlattr **attrs)
789 struct xfrm_usersa_id *p = nlmsg_data(nlh);
790 struct xfrm_state *x;
791 struct sk_buff *resp_skb;
792 int err = -ESRCH;
794 x = xfrm_user_state_lookup(p, attrs, &err);
795 if (x == NULL)
796 goto out_noput;
798 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
799 if (IS_ERR(resp_skb)) {
800 err = PTR_ERR(resp_skb);
801 } else {
802 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
804 xfrm_state_put(x);
805 out_noput:
806 return err;
809 static int verify_userspi_info(struct xfrm_userspi_info *p)
811 switch (p->info.id.proto) {
812 case IPPROTO_AH:
813 case IPPROTO_ESP:
814 break;
816 case IPPROTO_COMP:
817 /* IPCOMP spi is 16-bits. */
818 if (p->max >= 0x10000)
819 return -EINVAL;
820 break;
822 default:
823 return -EINVAL;
826 if (p->min > p->max)
827 return -EINVAL;
829 return 0;
832 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
833 struct nlattr **attrs)
835 struct xfrm_state *x;
836 struct xfrm_userspi_info *p;
837 struct sk_buff *resp_skb;
838 xfrm_address_t *daddr;
839 int family;
840 int err;
842 p = nlmsg_data(nlh);
843 err = verify_userspi_info(p);
844 if (err)
845 goto out_noput;
847 family = p->info.family;
848 daddr = &p->info.id.daddr;
850 x = NULL;
851 if (p->info.seq) {
852 x = xfrm_find_acq_byseq(p->info.seq);
853 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
854 xfrm_state_put(x);
855 x = NULL;
859 if (!x)
860 x = xfrm_find_acq(p->info.mode, p->info.reqid,
861 p->info.id.proto, daddr,
862 &p->info.saddr, 1,
863 family);
864 err = -ENOENT;
865 if (x == NULL)
866 goto out_noput;
868 err = xfrm_alloc_spi(x, p->min, p->max);
869 if (err)
870 goto out;
872 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
873 if (IS_ERR(resp_skb)) {
874 err = PTR_ERR(resp_skb);
875 goto out;
878 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
880 out:
881 xfrm_state_put(x);
882 out_noput:
883 return err;
886 static int verify_policy_dir(u8 dir)
888 switch (dir) {
889 case XFRM_POLICY_IN:
890 case XFRM_POLICY_OUT:
891 case XFRM_POLICY_FWD:
892 break;
894 default:
895 return -EINVAL;
898 return 0;
901 static int verify_policy_type(u8 type)
903 switch (type) {
904 case XFRM_POLICY_TYPE_MAIN:
905 #ifdef CONFIG_XFRM_SUB_POLICY
906 case XFRM_POLICY_TYPE_SUB:
907 #endif
908 break;
910 default:
911 return -EINVAL;
914 return 0;
917 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
919 switch (p->share) {
920 case XFRM_SHARE_ANY:
921 case XFRM_SHARE_SESSION:
922 case XFRM_SHARE_USER:
923 case XFRM_SHARE_UNIQUE:
924 break;
926 default:
927 return -EINVAL;
930 switch (p->action) {
931 case XFRM_POLICY_ALLOW:
932 case XFRM_POLICY_BLOCK:
933 break;
935 default:
936 return -EINVAL;
939 switch (p->sel.family) {
940 case AF_INET:
941 break;
943 case AF_INET6:
944 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
945 break;
946 #else
947 return -EAFNOSUPPORT;
948 #endif
950 default:
951 return -EINVAL;
954 return verify_policy_dir(p->dir);
957 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
959 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
960 struct xfrm_user_sec_ctx *uctx;
962 if (!rt)
963 return 0;
965 uctx = nla_data(rt);
966 return security_xfrm_policy_alloc(&pol->security, uctx);
969 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
970 int nr)
972 int i;
974 xp->xfrm_nr = nr;
975 for (i = 0; i < nr; i++, ut++) {
976 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
978 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
979 memcpy(&t->saddr, &ut->saddr,
980 sizeof(xfrm_address_t));
981 t->reqid = ut->reqid;
982 t->mode = ut->mode;
983 t->share = ut->share;
984 t->optional = ut->optional;
985 t->aalgos = ut->aalgos;
986 t->ealgos = ut->ealgos;
987 t->calgos = ut->calgos;
988 /* If all masks are ~0, then we allow all algorithms. */
989 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
990 t->encap_family = ut->family;
994 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
996 int i;
998 if (nr > XFRM_MAX_DEPTH)
999 return -EINVAL;
1001 for (i = 0; i < nr; i++) {
1002 /* We never validated the ut->family value, so many
1003 * applications simply leave it at zero. The check was
1004 * never made and ut->family was ignored because all
1005 * templates could be assumed to have the same family as
1006 * the policy itself. Now that we will have ipv4-in-ipv6
1007 * and ipv6-in-ipv4 tunnels, this is no longer true.
1009 if (!ut[i].family)
1010 ut[i].family = family;
1012 switch (ut[i].family) {
1013 case AF_INET:
1014 break;
1015 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1016 case AF_INET6:
1017 break;
1018 #endif
1019 default:
1020 return -EINVAL;
1024 return 0;
1027 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1029 struct nlattr *rt = attrs[XFRMA_TMPL];
1031 if (!rt) {
1032 pol->xfrm_nr = 0;
1033 } else {
1034 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1035 int nr = nla_len(rt) / sizeof(*utmpl);
1036 int err;
1038 err = validate_tmpl(nr, utmpl, pol->family);
1039 if (err)
1040 return err;
1042 copy_templates(pol, utmpl, nr);
1044 return 0;
1047 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1049 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1050 struct xfrm_userpolicy_type *upt;
1051 u8 type = XFRM_POLICY_TYPE_MAIN;
1052 int err;
1054 if (rt) {
1055 upt = nla_data(rt);
1056 type = upt->type;
1059 err = verify_policy_type(type);
1060 if (err)
1061 return err;
1063 *tp = type;
1064 return 0;
1067 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1069 xp->priority = p->priority;
1070 xp->index = p->index;
1071 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1072 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1073 xp->action = p->action;
1074 xp->flags = p->flags;
1075 xp->family = p->sel.family;
1076 /* XXX xp->share = p->share; */
1079 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1081 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1082 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1083 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1084 p->priority = xp->priority;
1085 p->index = xp->index;
1086 p->sel.family = xp->family;
1087 p->dir = dir;
1088 p->action = xp->action;
1089 p->flags = xp->flags;
1090 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1093 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1095 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1096 int err;
1098 if (!xp) {
1099 *errp = -ENOMEM;
1100 return NULL;
1103 copy_from_user_policy(xp, p);
1105 err = copy_from_user_policy_type(&xp->type, attrs);
1106 if (err)
1107 goto error;
1109 if (!(err = copy_from_user_tmpl(xp, attrs)))
1110 err = copy_from_user_sec_ctx(xp, attrs);
1111 if (err)
1112 goto error;
1114 return xp;
1115 error:
1116 *errp = err;
1117 xp->dead = 1;
1118 xfrm_policy_destroy(xp);
1119 return NULL;
1122 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1123 struct nlattr **attrs)
1125 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1126 struct xfrm_policy *xp;
1127 struct km_event c;
1128 int err;
1129 int excl;
1130 uid_t loginuid = NETLINK_CB(skb).loginuid;
1131 u32 sessionid = NETLINK_CB(skb).sessionid;
1132 u32 sid = NETLINK_CB(skb).sid;
1134 err = verify_newpolicy_info(p);
1135 if (err)
1136 return err;
1137 err = verify_sec_ctx_len(attrs);
1138 if (err)
1139 return err;
1141 xp = xfrm_policy_construct(p, attrs, &err);
1142 if (!xp)
1143 return err;
1145 /* shouldnt excl be based on nlh flags??
1146 * Aha! this is anti-netlink really i.e more pfkey derived
1147 * in netlink excl is a flag and you wouldnt need
1148 * a type XFRM_MSG_UPDPOLICY - JHS */
1149 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1150 err = xfrm_policy_insert(p->dir, xp, excl);
1151 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1153 if (err) {
1154 security_xfrm_policy_free(xp->security);
1155 kfree(xp);
1156 return err;
1159 c.event = nlh->nlmsg_type;
1160 c.seq = nlh->nlmsg_seq;
1161 c.pid = nlh->nlmsg_pid;
1162 km_policy_notify(xp, p->dir, &c);
1164 xfrm_pol_put(xp);
1166 return 0;
1169 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1171 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1172 int i;
1174 if (xp->xfrm_nr == 0)
1175 return 0;
1177 for (i = 0; i < xp->xfrm_nr; i++) {
1178 struct xfrm_user_tmpl *up = &vec[i];
1179 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1181 memcpy(&up->id, &kp->id, sizeof(up->id));
1182 up->family = kp->encap_family;
1183 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1184 up->reqid = kp->reqid;
1185 up->mode = kp->mode;
1186 up->share = kp->share;
1187 up->optional = kp->optional;
1188 up->aalgos = kp->aalgos;
1189 up->ealgos = kp->ealgos;
1190 up->calgos = kp->calgos;
1193 return nla_put(skb, XFRMA_TMPL,
1194 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1197 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1199 if (x->security) {
1200 return copy_sec_ctx(x->security, skb);
1202 return 0;
1205 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1207 if (xp->security) {
1208 return copy_sec_ctx(xp->security, skb);
1210 return 0;
1212 static inline size_t userpolicy_type_attrsize(void)
1214 #ifdef CONFIG_XFRM_SUB_POLICY
1215 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1216 #else
1217 return 0;
1218 #endif
1221 #ifdef CONFIG_XFRM_SUB_POLICY
1222 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1224 struct xfrm_userpolicy_type upt = {
1225 .type = type,
1228 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1231 #else
1232 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1234 return 0;
1236 #endif
1238 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1240 struct xfrm_dump_info *sp = ptr;
1241 struct xfrm_userpolicy_info *p;
1242 struct sk_buff *in_skb = sp->in_skb;
1243 struct sk_buff *skb = sp->out_skb;
1244 struct nlmsghdr *nlh;
1246 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1247 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1248 if (nlh == NULL)
1249 return -EMSGSIZE;
1251 p = nlmsg_data(nlh);
1252 copy_to_user_policy(xp, p, dir);
1253 if (copy_to_user_tmpl(xp, skb) < 0)
1254 goto nlmsg_failure;
1255 if (copy_to_user_sec_ctx(xp, skb))
1256 goto nlmsg_failure;
1257 if (copy_to_user_policy_type(xp->type, skb) < 0)
1258 goto nlmsg_failure;
1260 nlmsg_end(skb, nlh);
1261 return 0;
1263 nlmsg_failure:
1264 nlmsg_cancel(skb, nlh);
1265 return -EMSGSIZE;
1268 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1270 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1272 xfrm_policy_walk_done(walk);
1273 return 0;
1276 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1278 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1279 struct xfrm_dump_info info;
1281 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1282 sizeof(cb->args) - sizeof(cb->args[0]));
1284 info.in_skb = cb->skb;
1285 info.out_skb = skb;
1286 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1287 info.nlmsg_flags = NLM_F_MULTI;
1289 if (!cb->args[0]) {
1290 cb->args[0] = 1;
1291 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1294 (void) xfrm_policy_walk(walk, dump_one_policy, &info);
1296 return skb->len;
1299 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1300 struct xfrm_policy *xp,
1301 int dir, u32 seq)
1303 struct xfrm_dump_info info;
1304 struct sk_buff *skb;
1306 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1307 if (!skb)
1308 return ERR_PTR(-ENOMEM);
1310 info.in_skb = in_skb;
1311 info.out_skb = skb;
1312 info.nlmsg_seq = seq;
1313 info.nlmsg_flags = 0;
1315 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1316 kfree_skb(skb);
1317 return NULL;
1320 return skb;
1323 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1324 struct nlattr **attrs)
1326 struct xfrm_policy *xp;
1327 struct xfrm_userpolicy_id *p;
1328 u8 type = XFRM_POLICY_TYPE_MAIN;
1329 int err;
1330 struct km_event c;
1331 int delete;
1333 p = nlmsg_data(nlh);
1334 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1336 err = copy_from_user_policy_type(&type, attrs);
1337 if (err)
1338 return err;
1340 err = verify_policy_dir(p->dir);
1341 if (err)
1342 return err;
1344 if (p->index)
1345 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
1346 else {
1347 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1348 struct xfrm_sec_ctx *ctx;
1350 err = verify_sec_ctx_len(attrs);
1351 if (err)
1352 return err;
1354 ctx = NULL;
1355 if (rt) {
1356 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1358 err = security_xfrm_policy_alloc(&ctx, uctx);
1359 if (err)
1360 return err;
1362 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, ctx,
1363 delete, &err);
1364 security_xfrm_policy_free(ctx);
1366 if (xp == NULL)
1367 return -ENOENT;
1369 if (!delete) {
1370 struct sk_buff *resp_skb;
1372 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1373 if (IS_ERR(resp_skb)) {
1374 err = PTR_ERR(resp_skb);
1375 } else {
1376 err = nlmsg_unicast(xfrm_nl, resp_skb,
1377 NETLINK_CB(skb).pid);
1379 } else {
1380 uid_t loginuid = NETLINK_CB(skb).loginuid;
1381 u32 sessionid = NETLINK_CB(skb).sessionid;
1382 u32 sid = NETLINK_CB(skb).sid;
1384 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1385 sid);
1387 if (err != 0)
1388 goto out;
1390 c.data.byid = p->index;
1391 c.event = nlh->nlmsg_type;
1392 c.seq = nlh->nlmsg_seq;
1393 c.pid = nlh->nlmsg_pid;
1394 km_policy_notify(xp, p->dir, &c);
1397 out:
1398 xfrm_pol_put(xp);
1399 return err;
1402 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1403 struct nlattr **attrs)
1405 struct km_event c;
1406 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1407 struct xfrm_audit audit_info;
1408 int err;
1410 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1411 audit_info.sessionid = NETLINK_CB(skb).sessionid;
1412 audit_info.secid = NETLINK_CB(skb).sid;
1413 err = xfrm_state_flush(p->proto, &audit_info);
1414 if (err)
1415 return err;
1416 c.data.proto = p->proto;
1417 c.event = nlh->nlmsg_type;
1418 c.seq = nlh->nlmsg_seq;
1419 c.pid = nlh->nlmsg_pid;
1420 km_state_notify(NULL, &c);
1422 return 0;
1425 static inline size_t xfrm_aevent_msgsize(void)
1427 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1428 + nla_total_size(sizeof(struct xfrm_replay_state))
1429 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1430 + nla_total_size(4) /* XFRM_AE_RTHR */
1431 + nla_total_size(4); /* XFRM_AE_ETHR */
1434 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1436 struct xfrm_aevent_id *id;
1437 struct nlmsghdr *nlh;
1439 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1440 if (nlh == NULL)
1441 return -EMSGSIZE;
1443 id = nlmsg_data(nlh);
1444 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1445 id->sa_id.spi = x->id.spi;
1446 id->sa_id.family = x->props.family;
1447 id->sa_id.proto = x->id.proto;
1448 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1449 id->reqid = x->props.reqid;
1450 id->flags = c->data.aevent;
1452 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1453 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1455 if (id->flags & XFRM_AE_RTHR)
1456 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1458 if (id->flags & XFRM_AE_ETHR)
1459 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1460 x->replay_maxage * 10 / HZ);
1462 return nlmsg_end(skb, nlh);
1464 nla_put_failure:
1465 nlmsg_cancel(skb, nlh);
1466 return -EMSGSIZE;
1469 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1470 struct nlattr **attrs)
1472 struct xfrm_state *x;
1473 struct sk_buff *r_skb;
1474 int err;
1475 struct km_event c;
1476 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1477 struct xfrm_usersa_id *id = &p->sa_id;
1479 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1480 if (r_skb == NULL)
1481 return -ENOMEM;
1483 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1484 if (x == NULL) {
1485 kfree_skb(r_skb);
1486 return -ESRCH;
1490 * XXX: is this lock really needed - none of the other
1491 * gets lock (the concern is things getting updated
1492 * while we are still reading) - jhs
1494 spin_lock_bh(&x->lock);
1495 c.data.aevent = p->flags;
1496 c.seq = nlh->nlmsg_seq;
1497 c.pid = nlh->nlmsg_pid;
1499 if (build_aevent(r_skb, x, &c) < 0)
1500 BUG();
1501 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
1502 spin_unlock_bh(&x->lock);
1503 xfrm_state_put(x);
1504 return err;
1507 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1508 struct nlattr **attrs)
1510 struct xfrm_state *x;
1511 struct km_event c;
1512 int err = - EINVAL;
1513 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1514 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1515 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1517 if (!lt && !rp)
1518 return err;
1520 /* pedantic mode - thou shalt sayeth replaceth */
1521 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1522 return err;
1524 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1525 if (x == NULL)
1526 return -ESRCH;
1528 if (x->km.state != XFRM_STATE_VALID)
1529 goto out;
1531 spin_lock_bh(&x->lock);
1532 xfrm_update_ae_params(x, attrs);
1533 spin_unlock_bh(&x->lock);
1535 c.event = nlh->nlmsg_type;
1536 c.seq = nlh->nlmsg_seq;
1537 c.pid = nlh->nlmsg_pid;
1538 c.data.aevent = XFRM_AE_CU;
1539 km_state_notify(x, &c);
1540 err = 0;
1541 out:
1542 xfrm_state_put(x);
1543 return err;
1546 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1547 struct nlattr **attrs)
1549 struct km_event c;
1550 u8 type = XFRM_POLICY_TYPE_MAIN;
1551 int err;
1552 struct xfrm_audit audit_info;
1554 err = copy_from_user_policy_type(&type, attrs);
1555 if (err)
1556 return err;
1558 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1559 audit_info.sessionid = NETLINK_CB(skb).sessionid;
1560 audit_info.secid = NETLINK_CB(skb).sid;
1561 err = xfrm_policy_flush(type, &audit_info);
1562 if (err)
1563 return err;
1564 c.data.type = type;
1565 c.event = nlh->nlmsg_type;
1566 c.seq = nlh->nlmsg_seq;
1567 c.pid = nlh->nlmsg_pid;
1568 km_policy_notify(NULL, 0, &c);
1569 return 0;
1572 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1573 struct nlattr **attrs)
1575 struct xfrm_policy *xp;
1576 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1577 struct xfrm_userpolicy_info *p = &up->pol;
1578 u8 type = XFRM_POLICY_TYPE_MAIN;
1579 int err = -ENOENT;
1581 err = copy_from_user_policy_type(&type, attrs);
1582 if (err)
1583 return err;
1585 if (p->index)
1586 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
1587 else {
1588 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1589 struct xfrm_sec_ctx *ctx;
1591 err = verify_sec_ctx_len(attrs);
1592 if (err)
1593 return err;
1595 ctx = NULL;
1596 if (rt) {
1597 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1599 err = security_xfrm_policy_alloc(&ctx, uctx);
1600 if (err)
1601 return err;
1603 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, ctx, 0, &err);
1604 security_xfrm_policy_free(ctx);
1606 if (xp == NULL)
1607 return -ENOENT;
1609 read_lock(&xp->lock);
1610 if (xp->dead) {
1611 read_unlock(&xp->lock);
1612 goto out;
1615 read_unlock(&xp->lock);
1616 err = 0;
1617 if (up->hard) {
1618 uid_t loginuid = NETLINK_CB(skb).loginuid;
1619 uid_t sessionid = NETLINK_CB(skb).sessionid;
1620 u32 sid = NETLINK_CB(skb).sid;
1621 xfrm_policy_delete(xp, p->dir);
1622 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1624 } else {
1625 // reset the timers here?
1626 printk("Dont know what to do with soft policy expire\n");
1628 km_policy_expired(xp, p->dir, up->hard, current->pid);
1630 out:
1631 xfrm_pol_put(xp);
1632 return err;
1635 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1636 struct nlattr **attrs)
1638 struct xfrm_state *x;
1639 int err;
1640 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1641 struct xfrm_usersa_info *p = &ue->state;
1643 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1645 err = -ENOENT;
1646 if (x == NULL)
1647 return err;
1649 spin_lock_bh(&x->lock);
1650 err = -EINVAL;
1651 if (x->km.state != XFRM_STATE_VALID)
1652 goto out;
1653 km_state_expired(x, ue->hard, current->pid);
1655 if (ue->hard) {
1656 uid_t loginuid = NETLINK_CB(skb).loginuid;
1657 uid_t sessionid = NETLINK_CB(skb).sessionid;
1658 u32 sid = NETLINK_CB(skb).sid;
1659 __xfrm_state_delete(x);
1660 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
1662 err = 0;
1663 out:
1664 spin_unlock_bh(&x->lock);
1665 xfrm_state_put(x);
1666 return err;
1669 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1670 struct nlattr **attrs)
1672 struct xfrm_policy *xp;
1673 struct xfrm_user_tmpl *ut;
1674 int i;
1675 struct nlattr *rt = attrs[XFRMA_TMPL];
1677 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1678 struct xfrm_state *x = xfrm_state_alloc();
1679 int err = -ENOMEM;
1681 if (!x)
1682 return err;
1684 err = verify_newpolicy_info(&ua->policy);
1685 if (err) {
1686 printk("BAD policy passed\n");
1687 kfree(x);
1688 return err;
1691 /* build an XP */
1692 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
1693 if (!xp) {
1694 kfree(x);
1695 return err;
1698 memcpy(&x->id, &ua->id, sizeof(ua->id));
1699 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1700 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1702 ut = nla_data(rt);
1703 /* extract the templates and for each call km_key */
1704 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1705 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1706 memcpy(&x->id, &t->id, sizeof(x->id));
1707 x->props.mode = t->mode;
1708 x->props.reqid = t->reqid;
1709 x->props.family = ut->family;
1710 t->aalgos = ua->aalgos;
1711 t->ealgos = ua->ealgos;
1712 t->calgos = ua->calgos;
1713 err = km_query(x, t, xp);
1717 kfree(x);
1718 kfree(xp);
1720 return 0;
1723 #ifdef CONFIG_XFRM_MIGRATE
1724 static int copy_from_user_migrate(struct xfrm_migrate *ma,
1725 struct nlattr **attrs, int *num)
1727 struct nlattr *rt = attrs[XFRMA_MIGRATE];
1728 struct xfrm_user_migrate *um;
1729 int i, num_migrate;
1731 um = nla_data(rt);
1732 num_migrate = nla_len(rt) / sizeof(*um);
1734 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1735 return -EINVAL;
1737 for (i = 0; i < num_migrate; i++, um++, ma++) {
1738 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1739 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1740 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1741 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1743 ma->proto = um->proto;
1744 ma->mode = um->mode;
1745 ma->reqid = um->reqid;
1747 ma->old_family = um->old_family;
1748 ma->new_family = um->new_family;
1751 *num = i;
1752 return 0;
1755 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1756 struct nlattr **attrs)
1758 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
1759 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1760 u8 type;
1761 int err;
1762 int n = 0;
1764 if (attrs[XFRMA_MIGRATE] == NULL)
1765 return -EINVAL;
1767 err = copy_from_user_policy_type(&type, attrs);
1768 if (err)
1769 return err;
1771 err = copy_from_user_migrate((struct xfrm_migrate *)m,
1772 attrs, &n);
1773 if (err)
1774 return err;
1776 if (!n)
1777 return 0;
1779 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1781 return 0;
1783 #else
1784 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1785 struct nlattr **attrs)
1787 return -ENOPROTOOPT;
1789 #endif
1791 #ifdef CONFIG_XFRM_MIGRATE
1792 static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1794 struct xfrm_user_migrate um;
1796 memset(&um, 0, sizeof(um));
1797 um.proto = m->proto;
1798 um.mode = m->mode;
1799 um.reqid = m->reqid;
1800 um.old_family = m->old_family;
1801 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1802 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1803 um.new_family = m->new_family;
1804 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1805 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1807 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
1810 static inline size_t xfrm_migrate_msgsize(int num_migrate)
1812 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1813 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1814 + userpolicy_type_attrsize();
1817 static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1818 int num_migrate, struct xfrm_selector *sel,
1819 u8 dir, u8 type)
1821 struct xfrm_migrate *mp;
1822 struct xfrm_userpolicy_id *pol_id;
1823 struct nlmsghdr *nlh;
1824 int i;
1826 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1827 if (nlh == NULL)
1828 return -EMSGSIZE;
1830 pol_id = nlmsg_data(nlh);
1831 /* copy data from selector, dir, and type to the pol_id */
1832 memset(pol_id, 0, sizeof(*pol_id));
1833 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1834 pol_id->dir = dir;
1836 if (copy_to_user_policy_type(type, skb) < 0)
1837 goto nlmsg_failure;
1839 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1840 if (copy_to_user_migrate(mp, skb) < 0)
1841 goto nlmsg_failure;
1844 return nlmsg_end(skb, nlh);
1845 nlmsg_failure:
1846 nlmsg_cancel(skb, nlh);
1847 return -EMSGSIZE;
1850 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1851 struct xfrm_migrate *m, int num_migrate)
1853 struct sk_buff *skb;
1855 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
1856 if (skb == NULL)
1857 return -ENOMEM;
1859 /* build migrate */
1860 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1861 BUG();
1863 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
1865 #else
1866 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1867 struct xfrm_migrate *m, int num_migrate)
1869 return -ENOPROTOOPT;
1871 #endif
1873 #define XMSGSIZE(type) sizeof(struct type)
1875 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1876 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1877 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1878 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1879 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1880 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1881 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1882 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1883 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1884 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1885 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1886 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1887 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1888 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1889 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
1890 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1891 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1892 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
1893 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1894 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1895 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
1898 #undef XMSGSIZE
1900 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
1901 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
1902 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1903 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1904 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1905 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1906 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1907 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1908 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1909 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1910 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1911 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1912 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1913 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1914 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1915 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1918 static struct xfrm_link {
1919 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
1920 int (*dump)(struct sk_buff *, struct netlink_callback *);
1921 int (*done)(struct netlink_callback *);
1922 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1923 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1924 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1925 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1926 .dump = xfrm_dump_sa,
1927 .done = xfrm_dump_sa_done },
1928 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1929 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1930 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1931 .dump = xfrm_dump_policy,
1932 .done = xfrm_dump_policy_done },
1933 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1934 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
1935 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1936 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1937 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1938 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1939 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1940 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
1941 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1942 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1943 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
1944 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
1945 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
1948 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1950 struct nlattr *attrs[XFRMA_MAX+1];
1951 struct xfrm_link *link;
1952 int type, err;
1954 type = nlh->nlmsg_type;
1955 if (type > XFRM_MSG_MAX)
1956 return -EINVAL;
1958 type -= XFRM_MSG_BASE;
1959 link = &xfrm_dispatch[type];
1961 /* All operations require privileges, even GET */
1962 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1963 return -EPERM;
1965 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1966 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1967 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1968 if (link->dump == NULL)
1969 return -EINVAL;
1971 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, link->done);
1974 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
1975 xfrma_policy);
1976 if (err < 0)
1977 return err;
1979 if (link->doit == NULL)
1980 return -EINVAL;
1982 return link->doit(skb, nlh, attrs);
1985 static void xfrm_netlink_rcv(struct sk_buff *skb)
1987 mutex_lock(&xfrm_cfg_mutex);
1988 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
1989 mutex_unlock(&xfrm_cfg_mutex);
1992 static inline size_t xfrm_expire_msgsize(void)
1994 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1997 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1999 struct xfrm_user_expire *ue;
2000 struct nlmsghdr *nlh;
2002 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2003 if (nlh == NULL)
2004 return -EMSGSIZE;
2006 ue = nlmsg_data(nlh);
2007 copy_to_user_state(x, &ue->state);
2008 ue->hard = (c->data.hard != 0) ? 1 : 0;
2010 return nlmsg_end(skb, nlh);
2013 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
2015 struct sk_buff *skb;
2017 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2018 if (skb == NULL)
2019 return -ENOMEM;
2021 if (build_expire(skb, x, c) < 0)
2022 BUG();
2024 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2027 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2029 struct sk_buff *skb;
2031 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
2032 if (skb == NULL)
2033 return -ENOMEM;
2035 if (build_aevent(skb, x, c) < 0)
2036 BUG();
2038 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2041 static int xfrm_notify_sa_flush(struct km_event *c)
2043 struct xfrm_usersa_flush *p;
2044 struct nlmsghdr *nlh;
2045 struct sk_buff *skb;
2046 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2048 skb = nlmsg_new(len, GFP_ATOMIC);
2049 if (skb == NULL)
2050 return -ENOMEM;
2052 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2053 if (nlh == NULL) {
2054 kfree_skb(skb);
2055 return -EMSGSIZE;
2058 p = nlmsg_data(nlh);
2059 p->proto = c->data.proto;
2061 nlmsg_end(skb, nlh);
2063 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2066 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2068 size_t l = 0;
2069 if (x->aead)
2070 l += nla_total_size(aead_len(x->aead));
2071 if (x->aalg)
2072 l += nla_total_size(xfrm_alg_len(x->aalg));
2073 if (x->ealg)
2074 l += nla_total_size(xfrm_alg_len(x->ealg));
2075 if (x->calg)
2076 l += nla_total_size(sizeof(*x->calg));
2077 if (x->encap)
2078 l += nla_total_size(sizeof(*x->encap));
2079 if (x->security)
2080 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2081 x->security->ctx_len);
2082 if (x->coaddr)
2083 l += nla_total_size(sizeof(*x->coaddr));
2085 /* Must count x->lastused as it may become non-zero behind our back. */
2086 l += nla_total_size(sizeof(u64));
2088 return l;
2091 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2093 struct xfrm_usersa_info *p;
2094 struct xfrm_usersa_id *id;
2095 struct nlmsghdr *nlh;
2096 struct sk_buff *skb;
2097 int len = xfrm_sa_len(x);
2098 int headlen;
2100 headlen = sizeof(*p);
2101 if (c->event == XFRM_MSG_DELSA) {
2102 len += nla_total_size(headlen);
2103 headlen = sizeof(*id);
2105 len += NLMSG_ALIGN(headlen);
2107 skb = nlmsg_new(len, GFP_ATOMIC);
2108 if (skb == NULL)
2109 return -ENOMEM;
2111 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2112 if (nlh == NULL)
2113 goto nla_put_failure;
2115 p = nlmsg_data(nlh);
2116 if (c->event == XFRM_MSG_DELSA) {
2117 struct nlattr *attr;
2119 id = nlmsg_data(nlh);
2120 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2121 id->spi = x->id.spi;
2122 id->family = x->props.family;
2123 id->proto = x->id.proto;
2125 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2126 if (attr == NULL)
2127 goto nla_put_failure;
2129 p = nla_data(attr);
2132 if (copy_to_user_state_extra(x, p, skb))
2133 goto nla_put_failure;
2135 nlmsg_end(skb, nlh);
2137 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2139 nla_put_failure:
2140 /* Somebody screwed up with xfrm_sa_len! */
2141 WARN_ON(1);
2142 kfree_skb(skb);
2143 return -1;
2146 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2149 switch (c->event) {
2150 case XFRM_MSG_EXPIRE:
2151 return xfrm_exp_state_notify(x, c);
2152 case XFRM_MSG_NEWAE:
2153 return xfrm_aevent_state_notify(x, c);
2154 case XFRM_MSG_DELSA:
2155 case XFRM_MSG_UPDSA:
2156 case XFRM_MSG_NEWSA:
2157 return xfrm_notify_sa(x, c);
2158 case XFRM_MSG_FLUSHSA:
2159 return xfrm_notify_sa_flush(c);
2160 default:
2161 printk("xfrm_user: Unknown SA event %d\n", c->event);
2162 break;
2165 return 0;
2169 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2170 struct xfrm_policy *xp)
2172 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2173 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2174 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2175 + userpolicy_type_attrsize();
2178 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2179 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2180 int dir)
2182 struct xfrm_user_acquire *ua;
2183 struct nlmsghdr *nlh;
2184 __u32 seq = xfrm_get_acqseq();
2186 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2187 if (nlh == NULL)
2188 return -EMSGSIZE;
2190 ua = nlmsg_data(nlh);
2191 memcpy(&ua->id, &x->id, sizeof(ua->id));
2192 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2193 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2194 copy_to_user_policy(xp, &ua->policy, dir);
2195 ua->aalgos = xt->aalgos;
2196 ua->ealgos = xt->ealgos;
2197 ua->calgos = xt->calgos;
2198 ua->seq = x->km.seq = seq;
2200 if (copy_to_user_tmpl(xp, skb) < 0)
2201 goto nlmsg_failure;
2202 if (copy_to_user_state_sec_ctx(x, skb))
2203 goto nlmsg_failure;
2204 if (copy_to_user_policy_type(xp->type, skb) < 0)
2205 goto nlmsg_failure;
2207 return nlmsg_end(skb, nlh);
2209 nlmsg_failure:
2210 nlmsg_cancel(skb, nlh);
2211 return -EMSGSIZE;
2214 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2215 struct xfrm_policy *xp, int dir)
2217 struct sk_buff *skb;
2219 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2220 if (skb == NULL)
2221 return -ENOMEM;
2223 if (build_acquire(skb, x, xt, xp, dir) < 0)
2224 BUG();
2226 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2229 /* User gives us xfrm_user_policy_info followed by an array of 0
2230 * or more templates.
2232 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2233 u8 *data, int len, int *dir)
2235 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2236 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2237 struct xfrm_policy *xp;
2238 int nr;
2240 switch (sk->sk_family) {
2241 case AF_INET:
2242 if (opt != IP_XFRM_POLICY) {
2243 *dir = -EOPNOTSUPP;
2244 return NULL;
2246 break;
2247 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2248 case AF_INET6:
2249 if (opt != IPV6_XFRM_POLICY) {
2250 *dir = -EOPNOTSUPP;
2251 return NULL;
2253 break;
2254 #endif
2255 default:
2256 *dir = -EINVAL;
2257 return NULL;
2260 *dir = -EINVAL;
2262 if (len < sizeof(*p) ||
2263 verify_newpolicy_info(p))
2264 return NULL;
2266 nr = ((len - sizeof(*p)) / sizeof(*ut));
2267 if (validate_tmpl(nr, ut, p->sel.family))
2268 return NULL;
2270 if (p->dir > XFRM_POLICY_OUT)
2271 return NULL;
2273 xp = xfrm_policy_alloc(GFP_KERNEL);
2274 if (xp == NULL) {
2275 *dir = -ENOBUFS;
2276 return NULL;
2279 copy_from_user_policy(xp, p);
2280 xp->type = XFRM_POLICY_TYPE_MAIN;
2281 copy_templates(xp, ut, nr);
2283 *dir = p->dir;
2285 return xp;
2288 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2290 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2291 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2292 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2293 + userpolicy_type_attrsize();
2296 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2297 int dir, struct km_event *c)
2299 struct xfrm_user_polexpire *upe;
2300 struct nlmsghdr *nlh;
2301 int hard = c->data.hard;
2303 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2304 if (nlh == NULL)
2305 return -EMSGSIZE;
2307 upe = nlmsg_data(nlh);
2308 copy_to_user_policy(xp, &upe->pol, dir);
2309 if (copy_to_user_tmpl(xp, skb) < 0)
2310 goto nlmsg_failure;
2311 if (copy_to_user_sec_ctx(xp, skb))
2312 goto nlmsg_failure;
2313 if (copy_to_user_policy_type(xp->type, skb) < 0)
2314 goto nlmsg_failure;
2315 upe->hard = !!hard;
2317 return nlmsg_end(skb, nlh);
2319 nlmsg_failure:
2320 nlmsg_cancel(skb, nlh);
2321 return -EMSGSIZE;
2324 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2326 struct sk_buff *skb;
2328 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2329 if (skb == NULL)
2330 return -ENOMEM;
2332 if (build_polexpire(skb, xp, dir, c) < 0)
2333 BUG();
2335 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2338 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2340 struct xfrm_userpolicy_info *p;
2341 struct xfrm_userpolicy_id *id;
2342 struct nlmsghdr *nlh;
2343 struct sk_buff *skb;
2344 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2345 int headlen;
2347 headlen = sizeof(*p);
2348 if (c->event == XFRM_MSG_DELPOLICY) {
2349 len += nla_total_size(headlen);
2350 headlen = sizeof(*id);
2352 len += userpolicy_type_attrsize();
2353 len += NLMSG_ALIGN(headlen);
2355 skb = nlmsg_new(len, GFP_ATOMIC);
2356 if (skb == NULL)
2357 return -ENOMEM;
2359 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2360 if (nlh == NULL)
2361 goto nlmsg_failure;
2363 p = nlmsg_data(nlh);
2364 if (c->event == XFRM_MSG_DELPOLICY) {
2365 struct nlattr *attr;
2367 id = nlmsg_data(nlh);
2368 memset(id, 0, sizeof(*id));
2369 id->dir = dir;
2370 if (c->data.byid)
2371 id->index = xp->index;
2372 else
2373 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2375 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2376 if (attr == NULL)
2377 goto nlmsg_failure;
2379 p = nla_data(attr);
2382 copy_to_user_policy(xp, p, dir);
2383 if (copy_to_user_tmpl(xp, skb) < 0)
2384 goto nlmsg_failure;
2385 if (copy_to_user_policy_type(xp->type, skb) < 0)
2386 goto nlmsg_failure;
2388 nlmsg_end(skb, nlh);
2390 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2392 nlmsg_failure:
2393 kfree_skb(skb);
2394 return -1;
2397 static int xfrm_notify_policy_flush(struct km_event *c)
2399 struct nlmsghdr *nlh;
2400 struct sk_buff *skb;
2402 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2403 if (skb == NULL)
2404 return -ENOMEM;
2406 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2407 if (nlh == NULL)
2408 goto nlmsg_failure;
2409 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2410 goto nlmsg_failure;
2412 nlmsg_end(skb, nlh);
2414 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2416 nlmsg_failure:
2417 kfree_skb(skb);
2418 return -1;
2421 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2424 switch (c->event) {
2425 case XFRM_MSG_NEWPOLICY:
2426 case XFRM_MSG_UPDPOLICY:
2427 case XFRM_MSG_DELPOLICY:
2428 return xfrm_notify_policy(xp, dir, c);
2429 case XFRM_MSG_FLUSHPOLICY:
2430 return xfrm_notify_policy_flush(c);
2431 case XFRM_MSG_POLEXPIRE:
2432 return xfrm_exp_policy_notify(xp, dir, c);
2433 default:
2434 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2437 return 0;
2441 static inline size_t xfrm_report_msgsize(void)
2443 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2446 static int build_report(struct sk_buff *skb, u8 proto,
2447 struct xfrm_selector *sel, xfrm_address_t *addr)
2449 struct xfrm_user_report *ur;
2450 struct nlmsghdr *nlh;
2452 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2453 if (nlh == NULL)
2454 return -EMSGSIZE;
2456 ur = nlmsg_data(nlh);
2457 ur->proto = proto;
2458 memcpy(&ur->sel, sel, sizeof(ur->sel));
2460 if (addr)
2461 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2463 return nlmsg_end(skb, nlh);
2465 nla_put_failure:
2466 nlmsg_cancel(skb, nlh);
2467 return -EMSGSIZE;
2470 static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2471 xfrm_address_t *addr)
2473 struct sk_buff *skb;
2475 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2476 if (skb == NULL)
2477 return -ENOMEM;
2479 if (build_report(skb, proto, sel, addr) < 0)
2480 BUG();
2482 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2485 static struct xfrm_mgr netlink_mgr = {
2486 .id = "netlink",
2487 .notify = xfrm_send_state_notify,
2488 .acquire = xfrm_send_acquire,
2489 .compile_policy = xfrm_compile_policy,
2490 .notify_policy = xfrm_send_policy_notify,
2491 .report = xfrm_send_report,
2492 .migrate = xfrm_send_migrate,
2495 static int __init xfrm_user_init(void)
2497 struct sock *nlsk;
2499 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2501 nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX,
2502 xfrm_netlink_rcv, NULL, THIS_MODULE);
2503 if (nlsk == NULL)
2504 return -ENOMEM;
2505 rcu_assign_pointer(xfrm_nl, nlsk);
2507 xfrm_register_km(&netlink_mgr);
2509 return 0;
2512 static void __exit xfrm_user_exit(void)
2514 struct sock *nlsk = xfrm_nl;
2516 xfrm_unregister_km(&netlink_mgr);
2517 rcu_assign_pointer(xfrm_nl, NULL);
2518 synchronize_rcu();
2519 netlink_kernel_release(nlsk);
2522 module_init(xfrm_user_init);
2523 module_exit(xfrm_user_exit);
2524 MODULE_LICENSE("GPL");
2525 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);