[XFRM] netlink: Use nlattr instead of rtattr
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / xfrm / xfrm_user.c
blobe46bcf02ac69bbd75f934830adfbec8ce14dd97a
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 inline int alg_len(struct xfrm_algo *alg)
38 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
41 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
43 struct nlattr *rt = attrs[type];
44 struct xfrm_algo *algp;
46 if (!rt)
47 return 0;
49 algp = nla_data(rt);
50 if (nla_len(rt) < alg_len(algp))
51 return -EINVAL;
53 switch (type) {
54 case XFRMA_ALG_AUTH:
55 if (!algp->alg_key_len &&
56 strcmp(algp->alg_name, "digest_null") != 0)
57 return -EINVAL;
58 break;
60 case XFRMA_ALG_CRYPT:
61 if (!algp->alg_key_len &&
62 strcmp(algp->alg_name, "cipher_null") != 0)
63 return -EINVAL;
64 break;
66 case XFRMA_ALG_COMP:
67 /* Zero length keys are legal. */
68 break;
70 default:
71 return -EINVAL;
74 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
75 return 0;
78 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
79 xfrm_address_t **addrp)
81 struct nlattr *rt = attrs[type];
83 if (rt && addrp)
84 *addrp = nla_data(rt);
87 static inline int verify_sec_ctx_len(struct nlattr **attrs)
89 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
90 struct xfrm_user_sec_ctx *uctx;
92 if (!rt)
93 return 0;
95 uctx = nla_data(rt);
96 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
97 return -EINVAL;
99 return 0;
103 static int verify_newsa_info(struct xfrm_usersa_info *p,
104 struct nlattr **attrs)
106 int err;
108 err = -EINVAL;
109 switch (p->family) {
110 case AF_INET:
111 break;
113 case AF_INET6:
114 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
115 break;
116 #else
117 err = -EAFNOSUPPORT;
118 goto out;
119 #endif
121 default:
122 goto out;
125 err = -EINVAL;
126 switch (p->id.proto) {
127 case IPPROTO_AH:
128 if (!attrs[XFRMA_ALG_AUTH] ||
129 attrs[XFRMA_ALG_CRYPT] ||
130 attrs[XFRMA_ALG_COMP])
131 goto out;
132 break;
134 case IPPROTO_ESP:
135 if ((!attrs[XFRMA_ALG_AUTH] &&
136 !attrs[XFRMA_ALG_CRYPT]) ||
137 attrs[XFRMA_ALG_COMP])
138 goto out;
139 break;
141 case IPPROTO_COMP:
142 if (!attrs[XFRMA_ALG_COMP] ||
143 attrs[XFRMA_ALG_AUTH] ||
144 attrs[XFRMA_ALG_CRYPT])
145 goto out;
146 break;
148 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
149 case IPPROTO_DSTOPTS:
150 case IPPROTO_ROUTING:
151 if (attrs[XFRMA_ALG_COMP] ||
152 attrs[XFRMA_ALG_AUTH] ||
153 attrs[XFRMA_ALG_CRYPT] ||
154 attrs[XFRMA_ENCAP] ||
155 attrs[XFRMA_SEC_CTX] ||
156 !attrs[XFRMA_COADDR])
157 goto out;
158 break;
159 #endif
161 default:
162 goto out;
165 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
166 goto out;
167 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
168 goto out;
169 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
170 goto out;
171 if ((err = verify_sec_ctx_len(attrs)))
172 goto out;
174 err = -EINVAL;
175 switch (p->mode) {
176 case XFRM_MODE_TRANSPORT:
177 case XFRM_MODE_TUNNEL:
178 case XFRM_MODE_ROUTEOPTIMIZATION:
179 case XFRM_MODE_BEET:
180 break;
182 default:
183 goto out;
186 err = 0;
188 out:
189 return err;
192 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
193 struct xfrm_algo_desc *(*get_byname)(char *, int),
194 struct nlattr *rta)
196 struct xfrm_algo *p, *ualg;
197 struct xfrm_algo_desc *algo;
199 if (!rta)
200 return 0;
202 ualg = nla_data(rta);
204 algo = get_byname(ualg->alg_name, 1);
205 if (!algo)
206 return -ENOSYS;
207 *props = algo->desc.sadb_alg_id;
209 p = kmemdup(ualg, alg_len(ualg), GFP_KERNEL);
210 if (!p)
211 return -ENOMEM;
213 strcpy(p->alg_name, algo->name);
214 *algpp = p;
215 return 0;
218 static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct nlattr *rta)
220 struct xfrm_encap_tmpl *p, *uencap;
222 if (!rta)
223 return 0;
225 uencap = nla_data(rta);
226 p = kmemdup(uencap, sizeof(*p), GFP_KERNEL);
227 if (!p)
228 return -ENOMEM;
230 *encapp = p;
231 return 0;
235 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
237 int len = 0;
239 if (xfrm_ctx) {
240 len += sizeof(struct xfrm_user_sec_ctx);
241 len += xfrm_ctx->ctx_len;
243 return len;
246 static int attach_sec_ctx(struct xfrm_state *x, struct nlattr *u_arg)
248 struct xfrm_user_sec_ctx *uctx;
250 if (!u_arg)
251 return 0;
253 uctx = nla_data(u_arg);
254 return security_xfrm_state_alloc(x, uctx);
257 static int attach_one_addr(xfrm_address_t **addrpp, struct nlattr *rta)
259 xfrm_address_t *p, *uaddrp;
261 if (!rta)
262 return 0;
264 uaddrp = nla_data(rta);
265 p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
266 if (!p)
267 return -ENOMEM;
269 *addrpp = p;
270 return 0;
273 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
275 memcpy(&x->id, &p->id, sizeof(x->id));
276 memcpy(&x->sel, &p->sel, sizeof(x->sel));
277 memcpy(&x->lft, &p->lft, sizeof(x->lft));
278 x->props.mode = p->mode;
279 x->props.replay_window = p->replay_window;
280 x->props.reqid = p->reqid;
281 x->props.family = p->family;
282 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
283 x->props.flags = p->flags;
286 * Set inner address family if the KM left it as zero.
287 * See comment in validate_tmpl.
289 if (!x->sel.family)
290 x->sel.family = p->family;
294 * someday when pfkey also has support, we could have the code
295 * somehow made shareable and move it to xfrm_state.c - JHS
298 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
300 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
301 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
302 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
303 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
305 if (rp) {
306 struct xfrm_replay_state *replay;
307 replay = nla_data(rp);
308 memcpy(&x->replay, replay, sizeof(*replay));
309 memcpy(&x->preplay, replay, sizeof(*replay));
312 if (lt) {
313 struct xfrm_lifetime_cur *ltime;
314 ltime = nla_data(lt);
315 x->curlft.bytes = ltime->bytes;
316 x->curlft.packets = ltime->packets;
317 x->curlft.add_time = ltime->add_time;
318 x->curlft.use_time = ltime->use_time;
321 if (et)
322 x->replay_maxage = nla_get_u32(et);
324 if (rt)
325 x->replay_maxdiff = nla_get_u32(rt);
328 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
329 struct nlattr **attrs,
330 int *errp)
332 struct xfrm_state *x = xfrm_state_alloc();
333 int err = -ENOMEM;
335 if (!x)
336 goto error_no_put;
338 copy_from_user_state(x, p);
340 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
341 xfrm_aalg_get_byname,
342 attrs[XFRMA_ALG_AUTH])))
343 goto error;
344 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
345 xfrm_ealg_get_byname,
346 attrs[XFRMA_ALG_CRYPT])))
347 goto error;
348 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
349 xfrm_calg_get_byname,
350 attrs[XFRMA_ALG_COMP])))
351 goto error;
352 if ((err = attach_encap_tmpl(&x->encap, attrs[XFRMA_ENCAP])))
353 goto error;
354 if ((err = attach_one_addr(&x->coaddr, attrs[XFRMA_COADDR])))
355 goto error;
356 err = xfrm_init_state(x);
357 if (err)
358 goto error;
360 if ((err = attach_sec_ctx(x, attrs[XFRMA_SEC_CTX])))
361 goto error;
363 x->km.seq = p->seq;
364 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
365 /* sysctl_xfrm_aevent_etime is in 100ms units */
366 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
367 x->preplay.bitmap = 0;
368 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
369 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
371 /* override default values from above */
373 xfrm_update_ae_params(x, attrs);
375 return x;
377 error:
378 x->km.state = XFRM_STATE_DEAD;
379 xfrm_state_put(x);
380 error_no_put:
381 *errp = err;
382 return NULL;
385 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
386 struct nlattr **attrs)
388 struct xfrm_usersa_info *p = nlmsg_data(nlh);
389 struct xfrm_state *x;
390 int err;
391 struct km_event c;
393 err = verify_newsa_info(p, attrs);
394 if (err)
395 return err;
397 x = xfrm_state_construct(p, attrs, &err);
398 if (!x)
399 return err;
401 xfrm_state_hold(x);
402 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
403 err = xfrm_state_add(x);
404 else
405 err = xfrm_state_update(x);
407 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
408 AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
410 if (err < 0) {
411 x->km.state = XFRM_STATE_DEAD;
412 __xfrm_state_put(x);
413 goto out;
416 c.seq = nlh->nlmsg_seq;
417 c.pid = nlh->nlmsg_pid;
418 c.event = nlh->nlmsg_type;
420 km_state_notify(x, &c);
421 out:
422 xfrm_state_put(x);
423 return err;
426 static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
427 struct nlattr **attrs,
428 int *errp)
430 struct xfrm_state *x = NULL;
431 int err;
433 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
434 err = -ESRCH;
435 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
436 } else {
437 xfrm_address_t *saddr = NULL;
439 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
440 if (!saddr) {
441 err = -EINVAL;
442 goto out;
445 err = -ESRCH;
446 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
447 p->family);
450 out:
451 if (!x && errp)
452 *errp = err;
453 return x;
456 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
457 struct nlattr **attrs)
459 struct xfrm_state *x;
460 int err = -ESRCH;
461 struct km_event c;
462 struct xfrm_usersa_id *p = nlmsg_data(nlh);
464 x = xfrm_user_state_lookup(p, attrs, &err);
465 if (x == NULL)
466 return err;
468 if ((err = security_xfrm_state_delete(x)) != 0)
469 goto out;
471 if (xfrm_state_kern(x)) {
472 err = -EPERM;
473 goto out;
476 err = xfrm_state_delete(x);
478 if (err < 0)
479 goto out;
481 c.seq = nlh->nlmsg_seq;
482 c.pid = nlh->nlmsg_pid;
483 c.event = nlh->nlmsg_type;
484 km_state_notify(x, &c);
486 out:
487 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
488 AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
489 xfrm_state_put(x);
490 return err;
493 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
495 memcpy(&p->id, &x->id, sizeof(p->id));
496 memcpy(&p->sel, &x->sel, sizeof(p->sel));
497 memcpy(&p->lft, &x->lft, sizeof(p->lft));
498 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
499 memcpy(&p->stats, &x->stats, sizeof(p->stats));
500 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
501 p->mode = x->props.mode;
502 p->replay_window = x->props.replay_window;
503 p->reqid = x->props.reqid;
504 p->family = x->props.family;
505 p->flags = x->props.flags;
506 p->seq = x->km.seq;
509 struct xfrm_dump_info {
510 struct sk_buff *in_skb;
511 struct sk_buff *out_skb;
512 u32 nlmsg_seq;
513 u16 nlmsg_flags;
514 int start_idx;
515 int this_idx;
518 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
520 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
521 struct xfrm_user_sec_ctx *uctx;
522 struct nlattr *attr;
524 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
525 if (attr == NULL)
526 return -EMSGSIZE;
528 uctx = nla_data(attr);
529 uctx->exttype = XFRMA_SEC_CTX;
530 uctx->len = ctx_size;
531 uctx->ctx_doi = s->ctx_doi;
532 uctx->ctx_alg = s->ctx_alg;
533 uctx->ctx_len = s->ctx_len;
534 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
536 return 0;
539 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
541 struct xfrm_dump_info *sp = ptr;
542 struct sk_buff *in_skb = sp->in_skb;
543 struct sk_buff *skb = sp->out_skb;
544 struct xfrm_usersa_info *p;
545 struct nlmsghdr *nlh;
547 if (sp->this_idx < sp->start_idx)
548 goto out;
550 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
551 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
552 if (nlh == NULL)
553 return -EMSGSIZE;
555 p = nlmsg_data(nlh);
556 copy_to_user_state(x, p);
558 if (x->aalg)
559 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
560 if (x->ealg)
561 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
562 if (x->calg)
563 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
565 if (x->encap)
566 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
568 if (x->security && copy_sec_ctx(x->security, skb) < 0)
569 goto nla_put_failure;
571 if (x->coaddr)
572 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
574 if (x->lastused)
575 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
577 nlmsg_end(skb, nlh);
578 out:
579 sp->this_idx++;
580 return 0;
582 nla_put_failure:
583 nlmsg_cancel(skb, nlh);
584 return -EMSGSIZE;
587 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
589 struct xfrm_dump_info info;
591 info.in_skb = cb->skb;
592 info.out_skb = skb;
593 info.nlmsg_seq = cb->nlh->nlmsg_seq;
594 info.nlmsg_flags = NLM_F_MULTI;
595 info.this_idx = 0;
596 info.start_idx = cb->args[0];
597 (void) xfrm_state_walk(0, dump_one_state, &info);
598 cb->args[0] = info.this_idx;
600 return skb->len;
603 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
604 struct xfrm_state *x, u32 seq)
606 struct xfrm_dump_info info;
607 struct sk_buff *skb;
609 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
610 if (!skb)
611 return ERR_PTR(-ENOMEM);
613 info.in_skb = in_skb;
614 info.out_skb = skb;
615 info.nlmsg_seq = seq;
616 info.nlmsg_flags = 0;
617 info.this_idx = info.start_idx = 0;
619 if (dump_one_state(x, 0, &info)) {
620 kfree_skb(skb);
621 return NULL;
624 return skb;
627 static inline size_t xfrm_spdinfo_msgsize(void)
629 return NLMSG_ALIGN(4)
630 + nla_total_size(sizeof(struct xfrmu_spdinfo))
631 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
634 static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
636 struct xfrmk_spdinfo si;
637 struct xfrmu_spdinfo spc;
638 struct xfrmu_spdhinfo sph;
639 struct nlmsghdr *nlh;
640 u32 *f;
642 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
643 if (nlh == NULL) /* shouldnt really happen ... */
644 return -EMSGSIZE;
646 f = nlmsg_data(nlh);
647 *f = flags;
648 xfrm_spd_getinfo(&si);
649 spc.incnt = si.incnt;
650 spc.outcnt = si.outcnt;
651 spc.fwdcnt = si.fwdcnt;
652 spc.inscnt = si.inscnt;
653 spc.outscnt = si.outscnt;
654 spc.fwdscnt = si.fwdscnt;
655 sph.spdhcnt = si.spdhcnt;
656 sph.spdhmcnt = si.spdhmcnt;
658 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
659 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
661 return nlmsg_end(skb, nlh);
663 nla_put_failure:
664 nlmsg_cancel(skb, nlh);
665 return -EMSGSIZE;
668 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
669 struct nlattr **attrs)
671 struct sk_buff *r_skb;
672 u32 *flags = nlmsg_data(nlh);
673 u32 spid = NETLINK_CB(skb).pid;
674 u32 seq = nlh->nlmsg_seq;
676 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
677 if (r_skb == NULL)
678 return -ENOMEM;
680 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
681 BUG();
683 return nlmsg_unicast(xfrm_nl, r_skb, spid);
686 static inline size_t xfrm_sadinfo_msgsize(void)
688 return NLMSG_ALIGN(4)
689 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
690 + nla_total_size(4); /* XFRMA_SAD_CNT */
693 static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
695 struct xfrmk_sadinfo si;
696 struct xfrmu_sadhinfo sh;
697 struct nlmsghdr *nlh;
698 u32 *f;
700 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
701 if (nlh == NULL) /* shouldnt really happen ... */
702 return -EMSGSIZE;
704 f = nlmsg_data(nlh);
705 *f = flags;
706 xfrm_sad_getinfo(&si);
708 sh.sadhmcnt = si.sadhmcnt;
709 sh.sadhcnt = si.sadhcnt;
711 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
712 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
714 return nlmsg_end(skb, nlh);
716 nla_put_failure:
717 nlmsg_cancel(skb, nlh);
718 return -EMSGSIZE;
721 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
722 struct nlattr **attrs)
724 struct sk_buff *r_skb;
725 u32 *flags = nlmsg_data(nlh);
726 u32 spid = NETLINK_CB(skb).pid;
727 u32 seq = nlh->nlmsg_seq;
729 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
730 if (r_skb == NULL)
731 return -ENOMEM;
733 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
734 BUG();
736 return nlmsg_unicast(xfrm_nl, r_skb, spid);
739 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
740 struct nlattr **attrs)
742 struct xfrm_usersa_id *p = nlmsg_data(nlh);
743 struct xfrm_state *x;
744 struct sk_buff *resp_skb;
745 int err = -ESRCH;
747 x = xfrm_user_state_lookup(p, attrs, &err);
748 if (x == NULL)
749 goto out_noput;
751 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
752 if (IS_ERR(resp_skb)) {
753 err = PTR_ERR(resp_skb);
754 } else {
755 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
757 xfrm_state_put(x);
758 out_noput:
759 return err;
762 static int verify_userspi_info(struct xfrm_userspi_info *p)
764 switch (p->info.id.proto) {
765 case IPPROTO_AH:
766 case IPPROTO_ESP:
767 break;
769 case IPPROTO_COMP:
770 /* IPCOMP spi is 16-bits. */
771 if (p->max >= 0x10000)
772 return -EINVAL;
773 break;
775 default:
776 return -EINVAL;
779 if (p->min > p->max)
780 return -EINVAL;
782 return 0;
785 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
786 struct nlattr **attrs)
788 struct xfrm_state *x;
789 struct xfrm_userspi_info *p;
790 struct sk_buff *resp_skb;
791 xfrm_address_t *daddr;
792 int family;
793 int err;
795 p = nlmsg_data(nlh);
796 err = verify_userspi_info(p);
797 if (err)
798 goto out_noput;
800 family = p->info.family;
801 daddr = &p->info.id.daddr;
803 x = NULL;
804 if (p->info.seq) {
805 x = xfrm_find_acq_byseq(p->info.seq);
806 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
807 xfrm_state_put(x);
808 x = NULL;
812 if (!x)
813 x = xfrm_find_acq(p->info.mode, p->info.reqid,
814 p->info.id.proto, daddr,
815 &p->info.saddr, 1,
816 family);
817 err = -ENOENT;
818 if (x == NULL)
819 goto out_noput;
821 resp_skb = ERR_PTR(-ENOENT);
823 spin_lock_bh(&x->lock);
824 if (x->km.state != XFRM_STATE_DEAD) {
825 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
826 if (x->id.spi)
827 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
829 spin_unlock_bh(&x->lock);
831 if (IS_ERR(resp_skb)) {
832 err = PTR_ERR(resp_skb);
833 goto out;
836 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
838 out:
839 xfrm_state_put(x);
840 out_noput:
841 return err;
844 static int verify_policy_dir(u8 dir)
846 switch (dir) {
847 case XFRM_POLICY_IN:
848 case XFRM_POLICY_OUT:
849 case XFRM_POLICY_FWD:
850 break;
852 default:
853 return -EINVAL;
856 return 0;
859 static int verify_policy_type(u8 type)
861 switch (type) {
862 case XFRM_POLICY_TYPE_MAIN:
863 #ifdef CONFIG_XFRM_SUB_POLICY
864 case XFRM_POLICY_TYPE_SUB:
865 #endif
866 break;
868 default:
869 return -EINVAL;
872 return 0;
875 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
877 switch (p->share) {
878 case XFRM_SHARE_ANY:
879 case XFRM_SHARE_SESSION:
880 case XFRM_SHARE_USER:
881 case XFRM_SHARE_UNIQUE:
882 break;
884 default:
885 return -EINVAL;
888 switch (p->action) {
889 case XFRM_POLICY_ALLOW:
890 case XFRM_POLICY_BLOCK:
891 break;
893 default:
894 return -EINVAL;
897 switch (p->sel.family) {
898 case AF_INET:
899 break;
901 case AF_INET6:
902 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
903 break;
904 #else
905 return -EAFNOSUPPORT;
906 #endif
908 default:
909 return -EINVAL;
912 return verify_policy_dir(p->dir);
915 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
917 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
918 struct xfrm_user_sec_ctx *uctx;
920 if (!rt)
921 return 0;
923 uctx = nla_data(rt);
924 return security_xfrm_policy_alloc(pol, uctx);
927 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
928 int nr)
930 int i;
932 xp->xfrm_nr = nr;
933 for (i = 0; i < nr; i++, ut++) {
934 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
936 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
937 memcpy(&t->saddr, &ut->saddr,
938 sizeof(xfrm_address_t));
939 t->reqid = ut->reqid;
940 t->mode = ut->mode;
941 t->share = ut->share;
942 t->optional = ut->optional;
943 t->aalgos = ut->aalgos;
944 t->ealgos = ut->ealgos;
945 t->calgos = ut->calgos;
946 t->encap_family = ut->family;
950 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
952 int i;
954 if (nr > XFRM_MAX_DEPTH)
955 return -EINVAL;
957 for (i = 0; i < nr; i++) {
958 /* We never validated the ut->family value, so many
959 * applications simply leave it at zero. The check was
960 * never made and ut->family was ignored because all
961 * templates could be assumed to have the same family as
962 * the policy itself. Now that we will have ipv4-in-ipv6
963 * and ipv6-in-ipv4 tunnels, this is no longer true.
965 if (!ut[i].family)
966 ut[i].family = family;
968 switch (ut[i].family) {
969 case AF_INET:
970 break;
971 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
972 case AF_INET6:
973 break;
974 #endif
975 default:
976 return -EINVAL;
980 return 0;
983 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
985 struct nlattr *rt = attrs[XFRMA_TMPL];
987 if (!rt) {
988 pol->xfrm_nr = 0;
989 } else {
990 struct xfrm_user_tmpl *utmpl = nla_data(rt);
991 int nr = nla_len(rt) / sizeof(*utmpl);
992 int err;
994 err = validate_tmpl(nr, utmpl, pol->family);
995 if (err)
996 return err;
998 copy_templates(pol, utmpl, nr);
1000 return 0;
1003 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1005 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1006 struct xfrm_userpolicy_type *upt;
1007 u8 type = XFRM_POLICY_TYPE_MAIN;
1008 int err;
1010 if (rt) {
1011 upt = nla_data(rt);
1012 type = upt->type;
1015 err = verify_policy_type(type);
1016 if (err)
1017 return err;
1019 *tp = type;
1020 return 0;
1023 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1025 xp->priority = p->priority;
1026 xp->index = p->index;
1027 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1028 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1029 xp->action = p->action;
1030 xp->flags = p->flags;
1031 xp->family = p->sel.family;
1032 /* XXX xp->share = p->share; */
1035 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1037 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1038 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1039 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1040 p->priority = xp->priority;
1041 p->index = xp->index;
1042 p->sel.family = xp->family;
1043 p->dir = dir;
1044 p->action = xp->action;
1045 p->flags = xp->flags;
1046 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1049 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1051 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1052 int err;
1054 if (!xp) {
1055 *errp = -ENOMEM;
1056 return NULL;
1059 copy_from_user_policy(xp, p);
1061 err = copy_from_user_policy_type(&xp->type, attrs);
1062 if (err)
1063 goto error;
1065 if (!(err = copy_from_user_tmpl(xp, attrs)))
1066 err = copy_from_user_sec_ctx(xp, attrs);
1067 if (err)
1068 goto error;
1070 return xp;
1071 error:
1072 *errp = err;
1073 kfree(xp);
1074 return NULL;
1077 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1078 struct nlattr **attrs)
1080 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1081 struct xfrm_policy *xp;
1082 struct km_event c;
1083 int err;
1084 int excl;
1086 err = verify_newpolicy_info(p);
1087 if (err)
1088 return err;
1089 err = verify_sec_ctx_len(attrs);
1090 if (err)
1091 return err;
1093 xp = xfrm_policy_construct(p, attrs, &err);
1094 if (!xp)
1095 return err;
1097 /* shouldnt excl be based on nlh flags??
1098 * Aha! this is anti-netlink really i.e more pfkey derived
1099 * in netlink excl is a flag and you wouldnt need
1100 * a type XFRM_MSG_UPDPOLICY - JHS */
1101 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1102 err = xfrm_policy_insert(p->dir, xp, excl);
1103 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1104 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1106 if (err) {
1107 security_xfrm_policy_free(xp);
1108 kfree(xp);
1109 return err;
1112 c.event = nlh->nlmsg_type;
1113 c.seq = nlh->nlmsg_seq;
1114 c.pid = nlh->nlmsg_pid;
1115 km_policy_notify(xp, p->dir, &c);
1117 xfrm_pol_put(xp);
1119 return 0;
1122 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1124 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1125 int i;
1127 if (xp->xfrm_nr == 0)
1128 return 0;
1130 for (i = 0; i < xp->xfrm_nr; i++) {
1131 struct xfrm_user_tmpl *up = &vec[i];
1132 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1134 memcpy(&up->id, &kp->id, sizeof(up->id));
1135 up->family = kp->encap_family;
1136 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1137 up->reqid = kp->reqid;
1138 up->mode = kp->mode;
1139 up->share = kp->share;
1140 up->optional = kp->optional;
1141 up->aalgos = kp->aalgos;
1142 up->ealgos = kp->ealgos;
1143 up->calgos = kp->calgos;
1146 return nla_put(skb, XFRMA_TMPL,
1147 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1150 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1152 if (x->security) {
1153 return copy_sec_ctx(x->security, skb);
1155 return 0;
1158 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1160 if (xp->security) {
1161 return copy_sec_ctx(xp->security, skb);
1163 return 0;
1165 static inline size_t userpolicy_type_attrsize(void)
1167 #ifdef CONFIG_XFRM_SUB_POLICY
1168 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1169 #else
1170 return 0;
1171 #endif
1174 #ifdef CONFIG_XFRM_SUB_POLICY
1175 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1177 struct xfrm_userpolicy_type upt = {
1178 .type = type,
1181 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1184 #else
1185 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1187 return 0;
1189 #endif
1191 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1193 struct xfrm_dump_info *sp = ptr;
1194 struct xfrm_userpolicy_info *p;
1195 struct sk_buff *in_skb = sp->in_skb;
1196 struct sk_buff *skb = sp->out_skb;
1197 struct nlmsghdr *nlh;
1199 if (sp->this_idx < sp->start_idx)
1200 goto out;
1202 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1203 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1204 if (nlh == NULL)
1205 return -EMSGSIZE;
1207 p = nlmsg_data(nlh);
1208 copy_to_user_policy(xp, p, dir);
1209 if (copy_to_user_tmpl(xp, skb) < 0)
1210 goto nlmsg_failure;
1211 if (copy_to_user_sec_ctx(xp, skb))
1212 goto nlmsg_failure;
1213 if (copy_to_user_policy_type(xp->type, skb) < 0)
1214 goto nlmsg_failure;
1216 nlmsg_end(skb, nlh);
1217 out:
1218 sp->this_idx++;
1219 return 0;
1221 nlmsg_failure:
1222 nlmsg_cancel(skb, nlh);
1223 return -EMSGSIZE;
1226 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1228 struct xfrm_dump_info info;
1230 info.in_skb = cb->skb;
1231 info.out_skb = skb;
1232 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1233 info.nlmsg_flags = NLM_F_MULTI;
1234 info.this_idx = 0;
1235 info.start_idx = cb->args[0];
1236 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1237 #ifdef CONFIG_XFRM_SUB_POLICY
1238 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1239 #endif
1240 cb->args[0] = info.this_idx;
1242 return skb->len;
1245 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1246 struct xfrm_policy *xp,
1247 int dir, u32 seq)
1249 struct xfrm_dump_info info;
1250 struct sk_buff *skb;
1252 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1253 if (!skb)
1254 return ERR_PTR(-ENOMEM);
1256 info.in_skb = in_skb;
1257 info.out_skb = skb;
1258 info.nlmsg_seq = seq;
1259 info.nlmsg_flags = 0;
1260 info.this_idx = info.start_idx = 0;
1262 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1263 kfree_skb(skb);
1264 return NULL;
1267 return skb;
1270 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1271 struct nlattr **attrs)
1273 struct xfrm_policy *xp;
1274 struct xfrm_userpolicy_id *p;
1275 u8 type = XFRM_POLICY_TYPE_MAIN;
1276 int err;
1277 struct km_event c;
1278 int delete;
1280 p = nlmsg_data(nlh);
1281 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1283 err = copy_from_user_policy_type(&type, attrs);
1284 if (err)
1285 return err;
1287 err = verify_policy_dir(p->dir);
1288 if (err)
1289 return err;
1291 if (p->index)
1292 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
1293 else {
1294 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1295 struct xfrm_policy tmp;
1297 err = verify_sec_ctx_len(attrs);
1298 if (err)
1299 return err;
1301 memset(&tmp, 0, sizeof(struct xfrm_policy));
1302 if (rt) {
1303 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1305 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1306 return err;
1308 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1309 delete, &err);
1310 security_xfrm_policy_free(&tmp);
1312 if (xp == NULL)
1313 return -ENOENT;
1315 if (!delete) {
1316 struct sk_buff *resp_skb;
1318 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1319 if (IS_ERR(resp_skb)) {
1320 err = PTR_ERR(resp_skb);
1321 } else {
1322 err = nlmsg_unicast(xfrm_nl, resp_skb,
1323 NETLINK_CB(skb).pid);
1325 } else {
1326 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1327 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1329 if (err != 0)
1330 goto out;
1332 c.data.byid = p->index;
1333 c.event = nlh->nlmsg_type;
1334 c.seq = nlh->nlmsg_seq;
1335 c.pid = nlh->nlmsg_pid;
1336 km_policy_notify(xp, p->dir, &c);
1339 out:
1340 xfrm_pol_put(xp);
1341 return err;
1344 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1345 struct nlattr **attrs)
1347 struct km_event c;
1348 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1349 struct xfrm_audit audit_info;
1350 int err;
1352 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1353 audit_info.secid = NETLINK_CB(skb).sid;
1354 err = xfrm_state_flush(p->proto, &audit_info);
1355 if (err)
1356 return err;
1357 c.data.proto = p->proto;
1358 c.event = nlh->nlmsg_type;
1359 c.seq = nlh->nlmsg_seq;
1360 c.pid = nlh->nlmsg_pid;
1361 km_state_notify(NULL, &c);
1363 return 0;
1366 static inline size_t xfrm_aevent_msgsize(void)
1368 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1369 + nla_total_size(sizeof(struct xfrm_replay_state))
1370 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1371 + nla_total_size(4) /* XFRM_AE_RTHR */
1372 + nla_total_size(4); /* XFRM_AE_ETHR */
1375 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1377 struct xfrm_aevent_id *id;
1378 struct nlmsghdr *nlh;
1380 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1381 if (nlh == NULL)
1382 return -EMSGSIZE;
1384 id = nlmsg_data(nlh);
1385 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1386 id->sa_id.spi = x->id.spi;
1387 id->sa_id.family = x->props.family;
1388 id->sa_id.proto = x->id.proto;
1389 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1390 id->reqid = x->props.reqid;
1391 id->flags = c->data.aevent;
1393 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1394 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1396 if (id->flags & XFRM_AE_RTHR)
1397 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1399 if (id->flags & XFRM_AE_ETHR)
1400 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1401 x->replay_maxage * 10 / HZ);
1403 return nlmsg_end(skb, nlh);
1405 nla_put_failure:
1406 nlmsg_cancel(skb, nlh);
1407 return -EMSGSIZE;
1410 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1411 struct nlattr **attrs)
1413 struct xfrm_state *x;
1414 struct sk_buff *r_skb;
1415 int err;
1416 struct km_event c;
1417 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1418 struct xfrm_usersa_id *id = &p->sa_id;
1420 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1421 if (r_skb == NULL)
1422 return -ENOMEM;
1424 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1425 if (x == NULL) {
1426 kfree_skb(r_skb);
1427 return -ESRCH;
1431 * XXX: is this lock really needed - none of the other
1432 * gets lock (the concern is things getting updated
1433 * while we are still reading) - jhs
1435 spin_lock_bh(&x->lock);
1436 c.data.aevent = p->flags;
1437 c.seq = nlh->nlmsg_seq;
1438 c.pid = nlh->nlmsg_pid;
1440 if (build_aevent(r_skb, x, &c) < 0)
1441 BUG();
1442 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
1443 spin_unlock_bh(&x->lock);
1444 xfrm_state_put(x);
1445 return err;
1448 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1449 struct nlattr **attrs)
1451 struct xfrm_state *x;
1452 struct km_event c;
1453 int err = - EINVAL;
1454 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1455 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1456 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1458 if (!lt && !rp)
1459 return err;
1461 /* pedantic mode - thou shalt sayeth replaceth */
1462 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1463 return err;
1465 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1466 if (x == NULL)
1467 return -ESRCH;
1469 if (x->km.state != XFRM_STATE_VALID)
1470 goto out;
1472 spin_lock_bh(&x->lock);
1473 xfrm_update_ae_params(x, attrs);
1474 spin_unlock_bh(&x->lock);
1476 c.event = nlh->nlmsg_type;
1477 c.seq = nlh->nlmsg_seq;
1478 c.pid = nlh->nlmsg_pid;
1479 c.data.aevent = XFRM_AE_CU;
1480 km_state_notify(x, &c);
1481 err = 0;
1482 out:
1483 xfrm_state_put(x);
1484 return err;
1487 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1488 struct nlattr **attrs)
1490 struct km_event c;
1491 u8 type = XFRM_POLICY_TYPE_MAIN;
1492 int err;
1493 struct xfrm_audit audit_info;
1495 err = copy_from_user_policy_type(&type, attrs);
1496 if (err)
1497 return err;
1499 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1500 audit_info.secid = NETLINK_CB(skb).sid;
1501 err = xfrm_policy_flush(type, &audit_info);
1502 if (err)
1503 return err;
1504 c.data.type = type;
1505 c.event = nlh->nlmsg_type;
1506 c.seq = nlh->nlmsg_seq;
1507 c.pid = nlh->nlmsg_pid;
1508 km_policy_notify(NULL, 0, &c);
1509 return 0;
1512 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1513 struct nlattr **attrs)
1515 struct xfrm_policy *xp;
1516 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1517 struct xfrm_userpolicy_info *p = &up->pol;
1518 u8 type = XFRM_POLICY_TYPE_MAIN;
1519 int err = -ENOENT;
1521 err = copy_from_user_policy_type(&type, attrs);
1522 if (err)
1523 return err;
1525 if (p->index)
1526 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
1527 else {
1528 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1529 struct xfrm_policy tmp;
1531 err = verify_sec_ctx_len(attrs);
1532 if (err)
1533 return err;
1535 memset(&tmp, 0, sizeof(struct xfrm_policy));
1536 if (rt) {
1537 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1539 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1540 return err;
1542 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1543 0, &err);
1544 security_xfrm_policy_free(&tmp);
1547 if (xp == NULL)
1548 return -ENOENT;
1549 read_lock(&xp->lock);
1550 if (xp->dead) {
1551 read_unlock(&xp->lock);
1552 goto out;
1555 read_unlock(&xp->lock);
1556 err = 0;
1557 if (up->hard) {
1558 xfrm_policy_delete(xp, p->dir);
1559 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1560 AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
1562 } else {
1563 // reset the timers here?
1564 printk("Dont know what to do with soft policy expire\n");
1566 km_policy_expired(xp, p->dir, up->hard, current->pid);
1568 out:
1569 xfrm_pol_put(xp);
1570 return err;
1573 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1574 struct nlattr **attrs)
1576 struct xfrm_state *x;
1577 int err;
1578 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1579 struct xfrm_usersa_info *p = &ue->state;
1581 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1583 err = -ENOENT;
1584 if (x == NULL)
1585 return err;
1587 spin_lock_bh(&x->lock);
1588 err = -EINVAL;
1589 if (x->km.state != XFRM_STATE_VALID)
1590 goto out;
1591 km_state_expired(x, ue->hard, current->pid);
1593 if (ue->hard) {
1594 __xfrm_state_delete(x);
1595 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1596 AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
1598 err = 0;
1599 out:
1600 spin_unlock_bh(&x->lock);
1601 xfrm_state_put(x);
1602 return err;
1605 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1606 struct nlattr **attrs)
1608 struct xfrm_policy *xp;
1609 struct xfrm_user_tmpl *ut;
1610 int i;
1611 struct nlattr *rt = attrs[XFRMA_TMPL];
1613 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1614 struct xfrm_state *x = xfrm_state_alloc();
1615 int err = -ENOMEM;
1617 if (!x)
1618 return err;
1620 err = verify_newpolicy_info(&ua->policy);
1621 if (err) {
1622 printk("BAD policy passed\n");
1623 kfree(x);
1624 return err;
1627 /* build an XP */
1628 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
1629 if (!xp) {
1630 kfree(x);
1631 return err;
1634 memcpy(&x->id, &ua->id, sizeof(ua->id));
1635 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1636 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1638 ut = nla_data(rt);
1639 /* extract the templates and for each call km_key */
1640 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1641 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1642 memcpy(&x->id, &t->id, sizeof(x->id));
1643 x->props.mode = t->mode;
1644 x->props.reqid = t->reqid;
1645 x->props.family = ut->family;
1646 t->aalgos = ua->aalgos;
1647 t->ealgos = ua->ealgos;
1648 t->calgos = ua->calgos;
1649 err = km_query(x, t, xp);
1653 kfree(x);
1654 kfree(xp);
1656 return 0;
1659 #ifdef CONFIG_XFRM_MIGRATE
1660 static int copy_from_user_migrate(struct xfrm_migrate *ma,
1661 struct nlattr **attrs, int *num)
1663 struct nlattr *rt = attrs[XFRMA_MIGRATE];
1664 struct xfrm_user_migrate *um;
1665 int i, num_migrate;
1667 um = nla_data(rt);
1668 num_migrate = nla_len(rt) / sizeof(*um);
1670 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1671 return -EINVAL;
1673 for (i = 0; i < num_migrate; i++, um++, ma++) {
1674 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1675 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1676 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1677 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1679 ma->proto = um->proto;
1680 ma->mode = um->mode;
1681 ma->reqid = um->reqid;
1683 ma->old_family = um->old_family;
1684 ma->new_family = um->new_family;
1687 *num = i;
1688 return 0;
1691 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1692 struct nlattr **attrs)
1694 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
1695 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1696 u8 type;
1697 int err;
1698 int n = 0;
1700 if (attrs[XFRMA_MIGRATE] == NULL)
1701 return -EINVAL;
1703 err = copy_from_user_policy_type(&type, attrs);
1704 if (err)
1705 return err;
1707 err = copy_from_user_migrate((struct xfrm_migrate *)m,
1708 attrs, &n);
1709 if (err)
1710 return err;
1712 if (!n)
1713 return 0;
1715 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1717 return 0;
1719 #else
1720 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1721 struct nlattr **attrs)
1723 return -ENOPROTOOPT;
1725 #endif
1727 #ifdef CONFIG_XFRM_MIGRATE
1728 static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1730 struct xfrm_user_migrate um;
1732 memset(&um, 0, sizeof(um));
1733 um.proto = m->proto;
1734 um.mode = m->mode;
1735 um.reqid = m->reqid;
1736 um.old_family = m->old_family;
1737 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1738 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1739 um.new_family = m->new_family;
1740 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1741 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1743 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
1746 static inline size_t xfrm_migrate_msgsize(int num_migrate)
1748 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1749 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1750 + userpolicy_type_attrsize();
1753 static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1754 int num_migrate, struct xfrm_selector *sel,
1755 u8 dir, u8 type)
1757 struct xfrm_migrate *mp;
1758 struct xfrm_userpolicy_id *pol_id;
1759 struct nlmsghdr *nlh;
1760 int i;
1762 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1763 if (nlh == NULL)
1764 return -EMSGSIZE;
1766 pol_id = nlmsg_data(nlh);
1767 /* copy data from selector, dir, and type to the pol_id */
1768 memset(pol_id, 0, sizeof(*pol_id));
1769 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1770 pol_id->dir = dir;
1772 if (copy_to_user_policy_type(type, skb) < 0)
1773 goto nlmsg_failure;
1775 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1776 if (copy_to_user_migrate(mp, skb) < 0)
1777 goto nlmsg_failure;
1780 return nlmsg_end(skb, nlh);
1781 nlmsg_failure:
1782 nlmsg_cancel(skb, nlh);
1783 return -EMSGSIZE;
1786 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1787 struct xfrm_migrate *m, int num_migrate)
1789 struct sk_buff *skb;
1791 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
1792 if (skb == NULL)
1793 return -ENOMEM;
1795 /* build migrate */
1796 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1797 BUG();
1799 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
1801 #else
1802 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1803 struct xfrm_migrate *m, int num_migrate)
1805 return -ENOPROTOOPT;
1807 #endif
1809 #define XMSGSIZE(type) sizeof(struct type)
1811 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1812 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1813 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1814 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1815 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1816 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1817 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1818 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1819 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1820 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1821 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1822 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1823 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1824 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1825 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
1826 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1827 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1828 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
1829 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1830 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1831 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
1834 #undef XMSGSIZE
1836 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
1837 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1838 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1839 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1840 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1841 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1842 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1843 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1844 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1845 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1846 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1847 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1848 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1849 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1850 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1853 static struct xfrm_link {
1854 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
1855 int (*dump)(struct sk_buff *, struct netlink_callback *);
1856 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1857 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1858 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1859 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1860 .dump = xfrm_dump_sa },
1861 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1862 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1863 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1864 .dump = xfrm_dump_policy },
1865 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1866 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
1867 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1868 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1869 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1870 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1871 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1872 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
1873 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1874 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1875 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
1876 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
1877 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
1880 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1882 struct nlattr *attrs[XFRMA_MAX+1];
1883 struct xfrm_link *link;
1884 int type, err;
1886 type = nlh->nlmsg_type;
1887 if (type > XFRM_MSG_MAX)
1888 return -EINVAL;
1890 type -= XFRM_MSG_BASE;
1891 link = &xfrm_dispatch[type];
1893 /* All operations require privileges, even GET */
1894 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1895 return -EPERM;
1897 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1898 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1899 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1900 if (link->dump == NULL)
1901 return -EINVAL;
1903 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
1906 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
1907 xfrma_policy);
1908 if (err < 0)
1909 return err;
1911 if (link->doit == NULL)
1912 return -EINVAL;
1914 return link->doit(skb, nlh, attrs);
1917 static void xfrm_netlink_rcv(struct sock *sk, int len)
1919 unsigned int qlen = 0;
1921 do {
1922 mutex_lock(&xfrm_cfg_mutex);
1923 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
1924 mutex_unlock(&xfrm_cfg_mutex);
1926 } while (qlen);
1929 static inline size_t xfrm_expire_msgsize(void)
1931 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1934 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1936 struct xfrm_user_expire *ue;
1937 struct nlmsghdr *nlh;
1939 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1940 if (nlh == NULL)
1941 return -EMSGSIZE;
1943 ue = nlmsg_data(nlh);
1944 copy_to_user_state(x, &ue->state);
1945 ue->hard = (c->data.hard != 0) ? 1 : 0;
1947 return nlmsg_end(skb, nlh);
1950 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1952 struct sk_buff *skb;
1954 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
1955 if (skb == NULL)
1956 return -ENOMEM;
1958 if (build_expire(skb, x, c) < 0)
1959 BUG();
1961 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1964 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1966 struct sk_buff *skb;
1968 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1969 if (skb == NULL)
1970 return -ENOMEM;
1972 if (build_aevent(skb, x, c) < 0)
1973 BUG();
1975 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1978 static int xfrm_notify_sa_flush(struct km_event *c)
1980 struct xfrm_usersa_flush *p;
1981 struct nlmsghdr *nlh;
1982 struct sk_buff *skb;
1983 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
1985 skb = nlmsg_new(len, GFP_ATOMIC);
1986 if (skb == NULL)
1987 return -ENOMEM;
1989 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
1990 if (nlh == NULL) {
1991 kfree_skb(skb);
1992 return -EMSGSIZE;
1995 p = nlmsg_data(nlh);
1996 p->proto = c->data.proto;
1998 nlmsg_end(skb, nlh);
2000 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2003 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2005 size_t l = 0;
2006 if (x->aalg)
2007 l += nla_total_size(alg_len(x->aalg));
2008 if (x->ealg)
2009 l += nla_total_size(alg_len(x->ealg));
2010 if (x->calg)
2011 l += nla_total_size(sizeof(*x->calg));
2012 if (x->encap)
2013 l += nla_total_size(sizeof(*x->encap));
2015 return l;
2018 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2020 struct xfrm_usersa_info *p;
2021 struct xfrm_usersa_id *id;
2022 struct nlmsghdr *nlh;
2023 struct sk_buff *skb;
2024 int len = xfrm_sa_len(x);
2025 int headlen;
2027 headlen = sizeof(*p);
2028 if (c->event == XFRM_MSG_DELSA) {
2029 len += nla_total_size(headlen);
2030 headlen = sizeof(*id);
2032 len += NLMSG_ALIGN(headlen);
2034 skb = nlmsg_new(len, GFP_ATOMIC);
2035 if (skb == NULL)
2036 return -ENOMEM;
2038 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2039 if (nlh == NULL)
2040 goto nla_put_failure;
2042 p = nlmsg_data(nlh);
2043 if (c->event == XFRM_MSG_DELSA) {
2044 struct nlattr *attr;
2046 id = nlmsg_data(nlh);
2047 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2048 id->spi = x->id.spi;
2049 id->family = x->props.family;
2050 id->proto = x->id.proto;
2052 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2053 if (attr == NULL)
2054 goto nla_put_failure;
2056 p = nla_data(attr);
2059 copy_to_user_state(x, p);
2061 if (x->aalg)
2062 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
2063 if (x->ealg)
2064 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
2065 if (x->calg)
2066 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
2068 if (x->encap)
2069 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
2071 nlmsg_end(skb, nlh);
2073 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2075 nla_put_failure:
2076 kfree_skb(skb);
2077 return -1;
2080 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2083 switch (c->event) {
2084 case XFRM_MSG_EXPIRE:
2085 return xfrm_exp_state_notify(x, c);
2086 case XFRM_MSG_NEWAE:
2087 return xfrm_aevent_state_notify(x, c);
2088 case XFRM_MSG_DELSA:
2089 case XFRM_MSG_UPDSA:
2090 case XFRM_MSG_NEWSA:
2091 return xfrm_notify_sa(x, c);
2092 case XFRM_MSG_FLUSHSA:
2093 return xfrm_notify_sa_flush(c);
2094 default:
2095 printk("xfrm_user: Unknown SA event %d\n", c->event);
2096 break;
2099 return 0;
2103 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2104 struct xfrm_policy *xp)
2106 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2107 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2108 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2109 + userpolicy_type_attrsize();
2112 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2113 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2114 int dir)
2116 struct xfrm_user_acquire *ua;
2117 struct nlmsghdr *nlh;
2118 __u32 seq = xfrm_get_acqseq();
2120 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2121 if (nlh == NULL)
2122 return -EMSGSIZE;
2124 ua = nlmsg_data(nlh);
2125 memcpy(&ua->id, &x->id, sizeof(ua->id));
2126 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2127 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2128 copy_to_user_policy(xp, &ua->policy, dir);
2129 ua->aalgos = xt->aalgos;
2130 ua->ealgos = xt->ealgos;
2131 ua->calgos = xt->calgos;
2132 ua->seq = x->km.seq = seq;
2134 if (copy_to_user_tmpl(xp, skb) < 0)
2135 goto nlmsg_failure;
2136 if (copy_to_user_state_sec_ctx(x, skb))
2137 goto nlmsg_failure;
2138 if (copy_to_user_policy_type(xp->type, skb) < 0)
2139 goto nlmsg_failure;
2141 return nlmsg_end(skb, nlh);
2143 nlmsg_failure:
2144 nlmsg_cancel(skb, nlh);
2145 return -EMSGSIZE;
2148 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2149 struct xfrm_policy *xp, int dir)
2151 struct sk_buff *skb;
2153 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2154 if (skb == NULL)
2155 return -ENOMEM;
2157 if (build_acquire(skb, x, xt, xp, dir) < 0)
2158 BUG();
2160 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2163 /* User gives us xfrm_user_policy_info followed by an array of 0
2164 * or more templates.
2166 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2167 u8 *data, int len, int *dir)
2169 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2170 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2171 struct xfrm_policy *xp;
2172 int nr;
2174 switch (sk->sk_family) {
2175 case AF_INET:
2176 if (opt != IP_XFRM_POLICY) {
2177 *dir = -EOPNOTSUPP;
2178 return NULL;
2180 break;
2181 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2182 case AF_INET6:
2183 if (opt != IPV6_XFRM_POLICY) {
2184 *dir = -EOPNOTSUPP;
2185 return NULL;
2187 break;
2188 #endif
2189 default:
2190 *dir = -EINVAL;
2191 return NULL;
2194 *dir = -EINVAL;
2196 if (len < sizeof(*p) ||
2197 verify_newpolicy_info(p))
2198 return NULL;
2200 nr = ((len - sizeof(*p)) / sizeof(*ut));
2201 if (validate_tmpl(nr, ut, p->sel.family))
2202 return NULL;
2204 if (p->dir > XFRM_POLICY_OUT)
2205 return NULL;
2207 xp = xfrm_policy_alloc(GFP_KERNEL);
2208 if (xp == NULL) {
2209 *dir = -ENOBUFS;
2210 return NULL;
2213 copy_from_user_policy(xp, p);
2214 xp->type = XFRM_POLICY_TYPE_MAIN;
2215 copy_templates(xp, ut, nr);
2217 *dir = p->dir;
2219 return xp;
2222 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2224 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2225 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2226 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2227 + userpolicy_type_attrsize();
2230 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2231 int dir, struct km_event *c)
2233 struct xfrm_user_polexpire *upe;
2234 struct nlmsghdr *nlh;
2235 int hard = c->data.hard;
2237 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2238 if (nlh == NULL)
2239 return -EMSGSIZE;
2241 upe = nlmsg_data(nlh);
2242 copy_to_user_policy(xp, &upe->pol, dir);
2243 if (copy_to_user_tmpl(xp, skb) < 0)
2244 goto nlmsg_failure;
2245 if (copy_to_user_sec_ctx(xp, skb))
2246 goto nlmsg_failure;
2247 if (copy_to_user_policy_type(xp->type, skb) < 0)
2248 goto nlmsg_failure;
2249 upe->hard = !!hard;
2251 return nlmsg_end(skb, nlh);
2253 nlmsg_failure:
2254 nlmsg_cancel(skb, nlh);
2255 return -EMSGSIZE;
2258 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2260 struct sk_buff *skb;
2262 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2263 if (skb == NULL)
2264 return -ENOMEM;
2266 if (build_polexpire(skb, xp, dir, c) < 0)
2267 BUG();
2269 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2272 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2274 struct xfrm_userpolicy_info *p;
2275 struct xfrm_userpolicy_id *id;
2276 struct nlmsghdr *nlh;
2277 struct sk_buff *skb;
2278 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2279 int headlen;
2281 headlen = sizeof(*p);
2282 if (c->event == XFRM_MSG_DELPOLICY) {
2283 len += nla_total_size(headlen);
2284 headlen = sizeof(*id);
2286 len += userpolicy_type_attrsize();
2287 len += NLMSG_ALIGN(headlen);
2289 skb = nlmsg_new(len, GFP_ATOMIC);
2290 if (skb == NULL)
2291 return -ENOMEM;
2293 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2294 if (nlh == NULL)
2295 goto nlmsg_failure;
2297 p = nlmsg_data(nlh);
2298 if (c->event == XFRM_MSG_DELPOLICY) {
2299 struct nlattr *attr;
2301 id = nlmsg_data(nlh);
2302 memset(id, 0, sizeof(*id));
2303 id->dir = dir;
2304 if (c->data.byid)
2305 id->index = xp->index;
2306 else
2307 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2309 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2310 if (attr == NULL)
2311 goto nlmsg_failure;
2313 p = nla_data(attr);
2316 copy_to_user_policy(xp, p, dir);
2317 if (copy_to_user_tmpl(xp, skb) < 0)
2318 goto nlmsg_failure;
2319 if (copy_to_user_policy_type(xp->type, skb) < 0)
2320 goto nlmsg_failure;
2322 nlmsg_end(skb, nlh);
2324 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2326 nlmsg_failure:
2327 kfree_skb(skb);
2328 return -1;
2331 static int xfrm_notify_policy_flush(struct km_event *c)
2333 struct nlmsghdr *nlh;
2334 struct sk_buff *skb;
2336 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2337 if (skb == NULL)
2338 return -ENOMEM;
2340 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2341 if (nlh == NULL)
2342 goto nlmsg_failure;
2343 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2344 goto nlmsg_failure;
2346 nlmsg_end(skb, nlh);
2348 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2350 nlmsg_failure:
2351 kfree_skb(skb);
2352 return -1;
2355 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2358 switch (c->event) {
2359 case XFRM_MSG_NEWPOLICY:
2360 case XFRM_MSG_UPDPOLICY:
2361 case XFRM_MSG_DELPOLICY:
2362 return xfrm_notify_policy(xp, dir, c);
2363 case XFRM_MSG_FLUSHPOLICY:
2364 return xfrm_notify_policy_flush(c);
2365 case XFRM_MSG_POLEXPIRE:
2366 return xfrm_exp_policy_notify(xp, dir, c);
2367 default:
2368 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2371 return 0;
2375 static inline size_t xfrm_report_msgsize(void)
2377 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2380 static int build_report(struct sk_buff *skb, u8 proto,
2381 struct xfrm_selector *sel, xfrm_address_t *addr)
2383 struct xfrm_user_report *ur;
2384 struct nlmsghdr *nlh;
2386 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2387 if (nlh == NULL)
2388 return -EMSGSIZE;
2390 ur = nlmsg_data(nlh);
2391 ur->proto = proto;
2392 memcpy(&ur->sel, sel, sizeof(ur->sel));
2394 if (addr)
2395 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2397 return nlmsg_end(skb, nlh);
2399 nla_put_failure:
2400 nlmsg_cancel(skb, nlh);
2401 return -EMSGSIZE;
2404 static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2405 xfrm_address_t *addr)
2407 struct sk_buff *skb;
2409 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2410 if (skb == NULL)
2411 return -ENOMEM;
2413 if (build_report(skb, proto, sel, addr) < 0)
2414 BUG();
2416 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2419 static struct xfrm_mgr netlink_mgr = {
2420 .id = "netlink",
2421 .notify = xfrm_send_state_notify,
2422 .acquire = xfrm_send_acquire,
2423 .compile_policy = xfrm_compile_policy,
2424 .notify_policy = xfrm_send_policy_notify,
2425 .report = xfrm_send_report,
2426 .migrate = xfrm_send_migrate,
2429 static int __init xfrm_user_init(void)
2431 struct sock *nlsk;
2433 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2435 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
2436 xfrm_netlink_rcv, NULL, THIS_MODULE);
2437 if (nlsk == NULL)
2438 return -ENOMEM;
2439 rcu_assign_pointer(xfrm_nl, nlsk);
2441 xfrm_register_km(&netlink_mgr);
2443 return 0;
2446 static void __exit xfrm_user_exit(void)
2448 struct sock *nlsk = xfrm_nl;
2450 xfrm_unregister_km(&netlink_mgr);
2451 rcu_assign_pointer(xfrm_nl, NULL);
2452 synchronize_rcu();
2453 sock_release(nlsk->sk_socket);
2456 module_init(xfrm_user_init);
2457 module_exit(xfrm_user_exit);
2458 MODULE_LICENSE("GPL");
2459 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);