blob: 15ba08602aa14865e7f619e9c865f77e6bfb699d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
10 *
11 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/types.h>
16#include <linux/slab.h>
17#include <linux/socket.h>
18#include <linux/string.h>
19#include <linux/net.h>
20#include <linux/skbuff.h>
21#include <linux/netlink.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 <asm/uaccess.h>
30
31static struct sock *xfrm_nl;
32
33static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
34{
35 struct rtattr *rt = xfrma[type - 1];
36 struct xfrm_algo *algp;
37
38 if (!rt)
39 return 0;
40
41 if ((rt->rta_len - sizeof(*rt)) < sizeof(*algp))
42 return -EINVAL;
43
44 algp = RTA_DATA(rt);
45 switch (type) {
46 case XFRMA_ALG_AUTH:
47 if (!algp->alg_key_len &&
48 strcmp(algp->alg_name, "digest_null") != 0)
49 return -EINVAL;
50 break;
51
52 case XFRMA_ALG_CRYPT:
53 if (!algp->alg_key_len &&
54 strcmp(algp->alg_name, "cipher_null") != 0)
55 return -EINVAL;
56 break;
57
58 case XFRMA_ALG_COMP:
59 /* Zero length keys are legal. */
60 break;
61
62 default:
63 return -EINVAL;
64 };
65
66 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
67 return 0;
68}
69
70static int verify_encap_tmpl(struct rtattr **xfrma)
71{
72 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
73 struct xfrm_encap_tmpl *encap;
74
75 if (!rt)
76 return 0;
77
78 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
79 return -EINVAL;
80
81 return 0;
82}
83
84static int verify_newsa_info(struct xfrm_usersa_info *p,
85 struct rtattr **xfrma)
86{
87 int err;
88
89 err = -EINVAL;
90 switch (p->family) {
91 case AF_INET:
92 break;
93
94 case AF_INET6:
95#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
96 break;
97#else
98 err = -EAFNOSUPPORT;
99 goto out;
100#endif
101
102 default:
103 goto out;
104 };
105
106 err = -EINVAL;
107 switch (p->id.proto) {
108 case IPPROTO_AH:
109 if (!xfrma[XFRMA_ALG_AUTH-1] ||
110 xfrma[XFRMA_ALG_CRYPT-1] ||
111 xfrma[XFRMA_ALG_COMP-1])
112 goto out;
113 break;
114
115 case IPPROTO_ESP:
116 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
117 !xfrma[XFRMA_ALG_CRYPT-1]) ||
118 xfrma[XFRMA_ALG_COMP-1])
119 goto out;
120 break;
121
122 case IPPROTO_COMP:
123 if (!xfrma[XFRMA_ALG_COMP-1] ||
124 xfrma[XFRMA_ALG_AUTH-1] ||
125 xfrma[XFRMA_ALG_CRYPT-1])
126 goto out;
127 break;
128
129 default:
130 goto out;
131 };
132
133 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
134 goto out;
135 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
136 goto out;
137 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
138 goto out;
139 if ((err = verify_encap_tmpl(xfrma)))
140 goto out;
141
142 err = -EINVAL;
143 switch (p->mode) {
144 case 0:
145 case 1:
146 break;
147
148 default:
149 goto out;
150 };
151
152 err = 0;
153
154out:
155 return err;
156}
157
158static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
159 struct xfrm_algo_desc *(*get_byname)(char *, int),
160 struct rtattr *u_arg)
161{
162 struct rtattr *rta = u_arg;
163 struct xfrm_algo *p, *ualg;
164 struct xfrm_algo_desc *algo;
Herbert Xub9e9dea2005-05-19 12:39:04 -0700165 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 if (!rta)
168 return 0;
169
170 ualg = RTA_DATA(rta);
171
172 algo = get_byname(ualg->alg_name, 1);
173 if (!algo)
174 return -ENOSYS;
175 *props = algo->desc.sadb_alg_id;
176
Herbert Xub9e9dea2005-05-19 12:39:04 -0700177 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
178 p = kmalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (!p)
180 return -ENOMEM;
181
Herbert Xub9e9dea2005-05-19 12:39:04 -0700182 memcpy(p, ualg, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 *algpp = p;
184 return 0;
185}
186
187static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
188{
189 struct rtattr *rta = u_arg;
190 struct xfrm_encap_tmpl *p, *uencap;
191
192 if (!rta)
193 return 0;
194
195 uencap = RTA_DATA(rta);
196 p = kmalloc(sizeof(*p), GFP_KERNEL);
197 if (!p)
198 return -ENOMEM;
199
200 memcpy(p, uencap, sizeof(*p));
201 *encapp = p;
202 return 0;
203}
204
205static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
206{
207 memcpy(&x->id, &p->id, sizeof(x->id));
208 memcpy(&x->sel, &p->sel, sizeof(x->sel));
209 memcpy(&x->lft, &p->lft, sizeof(x->lft));
210 x->props.mode = p->mode;
211 x->props.replay_window = p->replay_window;
212 x->props.reqid = p->reqid;
213 x->props.family = p->family;
214 x->props.saddr = p->saddr;
215 x->props.flags = p->flags;
216}
217
218static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
219 struct rtattr **xfrma,
220 int *errp)
221{
222 struct xfrm_state *x = xfrm_state_alloc();
223 int err = -ENOMEM;
224
225 if (!x)
226 goto error_no_put;
227
228 copy_from_user_state(x, p);
229
230 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
231 xfrm_aalg_get_byname,
232 xfrma[XFRMA_ALG_AUTH-1])))
233 goto error;
234 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
235 xfrm_ealg_get_byname,
236 xfrma[XFRMA_ALG_CRYPT-1])))
237 goto error;
238 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
239 xfrm_calg_get_byname,
240 xfrma[XFRMA_ALG_COMP-1])))
241 goto error;
242 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
243 goto error;
244
245 err = -ENOENT;
246 x->type = xfrm_get_type(x->id.proto, x->props.family);
247 if (x->type == NULL)
248 goto error;
249
250 err = x->type->init_state(x, NULL);
251 if (err)
252 goto error;
253
254 x->curlft.add_time = (unsigned long) xtime.tv_sec;
255 x->km.state = XFRM_STATE_VALID;
256 x->km.seq = p->seq;
257
258 return x;
259
260error:
261 x->km.state = XFRM_STATE_DEAD;
262 xfrm_state_put(x);
263error_no_put:
264 *errp = err;
265 return NULL;
266}
267
268static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
269{
270 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
271 struct xfrm_state *x;
272 int err;
273
274 err = verify_newsa_info(p, (struct rtattr **) xfrma);
275 if (err)
276 return err;
277
278 x = xfrm_state_construct(p, (struct rtattr **) xfrma, &err);
279 if (!x)
280 return err;
281
282 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
283 err = xfrm_state_add(x);
284 else
285 err = xfrm_state_update(x);
286
287 if (err < 0) {
288 x->km.state = XFRM_STATE_DEAD;
289 xfrm_state_put(x);
290 }
291
292 return err;
293}
294
295static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
296{
297 struct xfrm_state *x;
298 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
299
300 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
301 if (x == NULL)
302 return -ESRCH;
303
304 if (xfrm_state_kern(x)) {
305 xfrm_state_put(x);
306 return -EPERM;
307 }
308
309 xfrm_state_delete(x);
310 xfrm_state_put(x);
311
312 return 0;
313}
314
315static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
316{
317 memcpy(&p->id, &x->id, sizeof(p->id));
318 memcpy(&p->sel, &x->sel, sizeof(p->sel));
319 memcpy(&p->lft, &x->lft, sizeof(p->lft));
320 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
321 memcpy(&p->stats, &x->stats, sizeof(p->stats));
322 p->saddr = x->props.saddr;
323 p->mode = x->props.mode;
324 p->replay_window = x->props.replay_window;
325 p->reqid = x->props.reqid;
326 p->family = x->props.family;
327 p->flags = x->props.flags;
328 p->seq = x->km.seq;
329}
330
331struct xfrm_dump_info {
332 struct sk_buff *in_skb;
333 struct sk_buff *out_skb;
334 u32 nlmsg_seq;
335 u16 nlmsg_flags;
336 int start_idx;
337 int this_idx;
338};
339
340static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
341{
342 struct xfrm_dump_info *sp = ptr;
343 struct sk_buff *in_skb = sp->in_skb;
344 struct sk_buff *skb = sp->out_skb;
345 struct xfrm_usersa_info *p;
346 struct nlmsghdr *nlh;
347 unsigned char *b = skb->tail;
348
349 if (sp->this_idx < sp->start_idx)
350 goto out;
351
352 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
353 sp->nlmsg_seq,
354 XFRM_MSG_NEWSA, sizeof(*p));
355 nlh->nlmsg_flags = sp->nlmsg_flags;
356
357 p = NLMSG_DATA(nlh);
358 copy_to_user_state(x, p);
359
360 if (x->aalg)
361 RTA_PUT(skb, XFRMA_ALG_AUTH,
362 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
363 if (x->ealg)
364 RTA_PUT(skb, XFRMA_ALG_CRYPT,
365 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
366 if (x->calg)
367 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
368
369 if (x->encap)
370 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
371
372 nlh->nlmsg_len = skb->tail - b;
373out:
374 sp->this_idx++;
375 return 0;
376
377nlmsg_failure:
378rtattr_failure:
379 skb_trim(skb, b - skb->data);
380 return -1;
381}
382
383static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
384{
385 struct xfrm_dump_info info;
386
387 info.in_skb = cb->skb;
388 info.out_skb = skb;
389 info.nlmsg_seq = cb->nlh->nlmsg_seq;
390 info.nlmsg_flags = NLM_F_MULTI;
391 info.this_idx = 0;
392 info.start_idx = cb->args[0];
393 (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
394 cb->args[0] = info.this_idx;
395
396 return skb->len;
397}
398
399static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
400 struct xfrm_state *x, u32 seq)
401{
402 struct xfrm_dump_info info;
403 struct sk_buff *skb;
404
405 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
406 if (!skb)
407 return ERR_PTR(-ENOMEM);
408
409 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
410 info.in_skb = in_skb;
411 info.out_skb = skb;
412 info.nlmsg_seq = seq;
413 info.nlmsg_flags = 0;
414 info.this_idx = info.start_idx = 0;
415
416 if (dump_one_state(x, 0, &info)) {
417 kfree_skb(skb);
418 return NULL;
419 }
420
421 return skb;
422}
423
424static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
425{
426 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
427 struct xfrm_state *x;
428 struct sk_buff *resp_skb;
429 int err;
430
431 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
432 err = -ESRCH;
433 if (x == NULL)
434 goto out_noput;
435
436 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
437 if (IS_ERR(resp_skb)) {
438 err = PTR_ERR(resp_skb);
439 } else {
440 err = netlink_unicast(xfrm_nl, resp_skb,
441 NETLINK_CB(skb).pid, MSG_DONTWAIT);
442 }
443 xfrm_state_put(x);
444out_noput:
445 return err;
446}
447
448static int verify_userspi_info(struct xfrm_userspi_info *p)
449{
450 switch (p->info.id.proto) {
451 case IPPROTO_AH:
452 case IPPROTO_ESP:
453 break;
454
455 case IPPROTO_COMP:
456 /* IPCOMP spi is 16-bits. */
457 if (p->max >= 0x10000)
458 return -EINVAL;
459 break;
460
461 default:
462 return -EINVAL;
463 };
464
465 if (p->min > p->max)
466 return -EINVAL;
467
468 return 0;
469}
470
471static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
472{
473 struct xfrm_state *x;
474 struct xfrm_userspi_info *p;
475 struct sk_buff *resp_skb;
476 xfrm_address_t *daddr;
477 int family;
478 int err;
479
480 p = NLMSG_DATA(nlh);
481 err = verify_userspi_info(p);
482 if (err)
483 goto out_noput;
484
485 family = p->info.family;
486 daddr = &p->info.id.daddr;
487
488 x = NULL;
489 if (p->info.seq) {
490 x = xfrm_find_acq_byseq(p->info.seq);
491 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
492 xfrm_state_put(x);
493 x = NULL;
494 }
495 }
496
497 if (!x)
498 x = xfrm_find_acq(p->info.mode, p->info.reqid,
499 p->info.id.proto, daddr,
500 &p->info.saddr, 1,
501 family);
502 err = -ENOENT;
503 if (x == NULL)
504 goto out_noput;
505
506 resp_skb = ERR_PTR(-ENOENT);
507
508 spin_lock_bh(&x->lock);
509 if (x->km.state != XFRM_STATE_DEAD) {
510 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
511 if (x->id.spi)
512 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
513 }
514 spin_unlock_bh(&x->lock);
515
516 if (IS_ERR(resp_skb)) {
517 err = PTR_ERR(resp_skb);
518 goto out;
519 }
520
521 err = netlink_unicast(xfrm_nl, resp_skb,
522 NETLINK_CB(skb).pid, MSG_DONTWAIT);
523
524out:
525 xfrm_state_put(x);
526out_noput:
527 return err;
528}
529
530static int verify_policy_dir(__u8 dir)
531{
532 switch (dir) {
533 case XFRM_POLICY_IN:
534 case XFRM_POLICY_OUT:
535 case XFRM_POLICY_FWD:
536 break;
537
538 default:
539 return -EINVAL;
540 };
541
542 return 0;
543}
544
545static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
546{
547 switch (p->share) {
548 case XFRM_SHARE_ANY:
549 case XFRM_SHARE_SESSION:
550 case XFRM_SHARE_USER:
551 case XFRM_SHARE_UNIQUE:
552 break;
553
554 default:
555 return -EINVAL;
556 };
557
558 switch (p->action) {
559 case XFRM_POLICY_ALLOW:
560 case XFRM_POLICY_BLOCK:
561 break;
562
563 default:
564 return -EINVAL;
565 };
566
567 switch (p->sel.family) {
568 case AF_INET:
569 break;
570
571 case AF_INET6:
572#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
573 break;
574#else
575 return -EAFNOSUPPORT;
576#endif
577
578 default:
579 return -EINVAL;
580 };
581
582 return verify_policy_dir(p->dir);
583}
584
585static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
586 int nr)
587{
588 int i;
589
590 xp->xfrm_nr = nr;
591 for (i = 0; i < nr; i++, ut++) {
592 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
593
594 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
595 memcpy(&t->saddr, &ut->saddr,
596 sizeof(xfrm_address_t));
597 t->reqid = ut->reqid;
598 t->mode = ut->mode;
599 t->share = ut->share;
600 t->optional = ut->optional;
601 t->aalgos = ut->aalgos;
602 t->ealgos = ut->ealgos;
603 t->calgos = ut->calgos;
604 }
605}
606
607static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
608{
609 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
610 struct xfrm_user_tmpl *utmpl;
611 int nr;
612
613 if (!rt) {
614 pol->xfrm_nr = 0;
615 } else {
616 nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
617
618 if (nr > XFRM_MAX_DEPTH)
619 return -EINVAL;
620
621 copy_templates(pol, RTA_DATA(rt), nr);
622 }
623 return 0;
624}
625
626static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
627{
628 xp->priority = p->priority;
629 xp->index = p->index;
630 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
631 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
632 xp->action = p->action;
633 xp->flags = p->flags;
634 xp->family = p->sel.family;
635 /* XXX xp->share = p->share; */
636}
637
638static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
639{
640 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
641 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
642 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
643 p->priority = xp->priority;
644 p->index = xp->index;
645 p->sel.family = xp->family;
646 p->dir = dir;
647 p->action = xp->action;
648 p->flags = xp->flags;
649 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
650}
651
652static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
653{
654 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
655 int err;
656
657 if (!xp) {
658 *errp = -ENOMEM;
659 return NULL;
660 }
661
662 copy_from_user_policy(xp, p);
663 err = copy_from_user_tmpl(xp, xfrma);
664 if (err) {
665 *errp = err;
666 kfree(xp);
667 xp = NULL;
668 }
669
670 return xp;
671}
672
673static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
674{
675 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
676 struct xfrm_policy *xp;
677 int err;
678 int excl;
679
680 err = verify_newpolicy_info(p);
681 if (err)
682 return err;
683
684 xp = xfrm_policy_construct(p, (struct rtattr **) xfrma, &err);
685 if (!xp)
686 return err;
687
688 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
689 err = xfrm_policy_insert(p->dir, xp, excl);
690 if (err) {
691 kfree(xp);
692 return err;
693 }
694
695 xfrm_pol_put(xp);
696
697 return 0;
698}
699
700static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
701{
702 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
703 int i;
704
705 if (xp->xfrm_nr == 0)
706 return 0;
707
708 for (i = 0; i < xp->xfrm_nr; i++) {
709 struct xfrm_user_tmpl *up = &vec[i];
710 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
711
712 memcpy(&up->id, &kp->id, sizeof(up->id));
713 up->family = xp->family;
714 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
715 up->reqid = kp->reqid;
716 up->mode = kp->mode;
717 up->share = kp->share;
718 up->optional = kp->optional;
719 up->aalgos = kp->aalgos;
720 up->ealgos = kp->ealgos;
721 up->calgos = kp->calgos;
722 }
723 RTA_PUT(skb, XFRMA_TMPL,
724 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
725 vec);
726
727 return 0;
728
729rtattr_failure:
730 return -1;
731}
732
733static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
734{
735 struct xfrm_dump_info *sp = ptr;
736 struct xfrm_userpolicy_info *p;
737 struct sk_buff *in_skb = sp->in_skb;
738 struct sk_buff *skb = sp->out_skb;
739 struct nlmsghdr *nlh;
740 unsigned char *b = skb->tail;
741
742 if (sp->this_idx < sp->start_idx)
743 goto out;
744
745 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
746 sp->nlmsg_seq,
747 XFRM_MSG_NEWPOLICY, sizeof(*p));
748 p = NLMSG_DATA(nlh);
749 nlh->nlmsg_flags = sp->nlmsg_flags;
750
751 copy_to_user_policy(xp, p, dir);
752 if (copy_to_user_tmpl(xp, skb) < 0)
753 goto nlmsg_failure;
754
755 nlh->nlmsg_len = skb->tail - b;
756out:
757 sp->this_idx++;
758 return 0;
759
760nlmsg_failure:
761 skb_trim(skb, b - skb->data);
762 return -1;
763}
764
765static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
766{
767 struct xfrm_dump_info info;
768
769 info.in_skb = cb->skb;
770 info.out_skb = skb;
771 info.nlmsg_seq = cb->nlh->nlmsg_seq;
772 info.nlmsg_flags = NLM_F_MULTI;
773 info.this_idx = 0;
774 info.start_idx = cb->args[0];
775 (void) xfrm_policy_walk(dump_one_policy, &info);
776 cb->args[0] = info.this_idx;
777
778 return skb->len;
779}
780
781static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
782 struct xfrm_policy *xp,
783 int dir, u32 seq)
784{
785 struct xfrm_dump_info info;
786 struct sk_buff *skb;
787
788 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
789 if (!skb)
790 return ERR_PTR(-ENOMEM);
791
792 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
793 info.in_skb = in_skb;
794 info.out_skb = skb;
795 info.nlmsg_seq = seq;
796 info.nlmsg_flags = 0;
797 info.this_idx = info.start_idx = 0;
798
799 if (dump_one_policy(xp, dir, 0, &info) < 0) {
800 kfree_skb(skb);
801 return NULL;
802 }
803
804 return skb;
805}
806
807static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
808{
809 struct xfrm_policy *xp;
810 struct xfrm_userpolicy_id *p;
811 int err;
812 int delete;
813
814 p = NLMSG_DATA(nlh);
815 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
816
817 err = verify_policy_dir(p->dir);
818 if (err)
819 return err;
820
821 if (p->index)
822 xp = xfrm_policy_byid(p->dir, p->index, delete);
823 else
824 xp = xfrm_policy_bysel(p->dir, &p->sel, delete);
825 if (xp == NULL)
826 return -ENOENT;
827
828 if (!delete) {
829 struct sk_buff *resp_skb;
830
831 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
832 if (IS_ERR(resp_skb)) {
833 err = PTR_ERR(resp_skb);
834 } else {
835 err = netlink_unicast(xfrm_nl, resp_skb,
836 NETLINK_CB(skb).pid,
837 MSG_DONTWAIT);
838 }
839 }
840
841 xfrm_pol_put(xp);
842
843 return err;
844}
845
846static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
847{
848 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
849
850 xfrm_state_flush(p->proto);
851 return 0;
852}
853
854static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
855{
856 xfrm_policy_flush();
857 return 0;
858}
859
Thomas Graf492b5582005-05-03 14:26:40 -0700860#define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
861
862static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
863 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
864 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
865 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
866 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
867 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
868 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
869 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
870 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
871 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
872 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
873 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
874 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
875 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
876 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877};
878
Thomas Graf492b5582005-05-03 14:26:40 -0700879#undef XMSGSIZE
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881static struct xfrm_link {
882 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
883 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -0700884} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
885 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
886 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
887 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
888 .dump = xfrm_dump_sa },
889 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
890 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
891 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
892 .dump = xfrm_dump_policy },
893 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
894 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
895 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
896 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
897 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898};
899
900static int xfrm_done(struct netlink_callback *cb)
901{
902 return 0;
903}
904
905static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
906{
907 struct rtattr *xfrma[XFRMA_MAX];
908 struct xfrm_link *link;
909 int type, min_len;
910
911 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
912 return 0;
913
914 type = nlh->nlmsg_type;
915
916 /* A control message: ignore them */
917 if (type < XFRM_MSG_BASE)
918 return 0;
919
920 /* Unknown message: reply with EINVAL */
921 if (type > XFRM_MSG_MAX)
922 goto err_einval;
923
924 type -= XFRM_MSG_BASE;
925 link = &xfrm_dispatch[type];
926
927 /* All operations require privileges, even GET */
928 if (security_netlink_recv(skb)) {
929 *errp = -EPERM;
930 return -1;
931 }
932
Thomas Graf492b5582005-05-03 14:26:40 -0700933 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
934 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
935 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 u32 rlen;
937
938 if (link->dump == NULL)
939 goto err_einval;
940
941 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
942 link->dump,
943 xfrm_done)) != 0) {
944 return -1;
945 }
946 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
947 if (rlen > skb->len)
948 rlen = skb->len;
949 skb_pull(skb, rlen);
950 return -1;
951 }
952
953 memset(xfrma, 0, sizeof(xfrma));
954
955 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
956 goto err_einval;
957
958 if (nlh->nlmsg_len > min_len) {
959 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
960 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
961
962 while (RTA_OK(attr, attrlen)) {
963 unsigned short flavor = attr->rta_type;
964 if (flavor) {
965 if (flavor > XFRMA_MAX)
966 goto err_einval;
967 xfrma[flavor - 1] = attr;
968 }
969 attr = RTA_NEXT(attr, attrlen);
970 }
971 }
972
973 if (link->doit == NULL)
974 goto err_einval;
975 *errp = link->doit(skb, nlh, (void **) &xfrma);
976
977 return *errp;
978
979err_einval:
980 *errp = -EINVAL;
981 return -1;
982}
983
984static int xfrm_user_rcv_skb(struct sk_buff *skb)
985{
986 int err;
987 struct nlmsghdr *nlh;
988
989 while (skb->len >= NLMSG_SPACE(0)) {
990 u32 rlen;
991
992 nlh = (struct nlmsghdr *) skb->data;
993 if (nlh->nlmsg_len < sizeof(*nlh) ||
994 skb->len < nlh->nlmsg_len)
995 return 0;
996 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
997 if (rlen > skb->len)
998 rlen = skb->len;
999 if (xfrm_user_rcv_msg(skb, nlh, &err) < 0) {
1000 if (err == 0)
1001 return -1;
1002 netlink_ack(skb, nlh, err);
1003 } else if (nlh->nlmsg_flags & NLM_F_ACK)
1004 netlink_ack(skb, nlh, 0);
1005 skb_pull(skb, rlen);
1006 }
1007
1008 return 0;
1009}
1010
1011static void xfrm_netlink_rcv(struct sock *sk, int len)
1012{
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001013 unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 do {
1016 struct sk_buff *skb;
1017
1018 down(&xfrm_cfg_sem);
1019
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001020 if (qlen > skb_queue_len(&sk->sk_receive_queue))
1021 qlen = skb_queue_len(&sk->sk_receive_queue);
1022
David S. Miller09e14302005-05-03 15:30:05 -07001023 for (; qlen; qlen--) {
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001024 skb = skb_dequeue(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (xfrm_user_rcv_skb(skb)) {
David S. Miller09e14302005-05-03 15:30:05 -07001026 if (skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 skb_queue_head(&sk->sk_receive_queue,
1028 skb);
David S. Miller0f4821e2005-05-03 16:15:59 -07001029 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 kfree_skb(skb);
David S. Miller0f4821e2005-05-03 16:15:59 -07001031 qlen--;
1032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 break;
1034 }
1035 kfree_skb(skb);
1036 }
1037
1038 up(&xfrm_cfg_sem);
1039
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001040 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
1043static int build_expire(struct sk_buff *skb, struct xfrm_state *x, int hard)
1044{
1045 struct xfrm_user_expire *ue;
1046 struct nlmsghdr *nlh;
1047 unsigned char *b = skb->tail;
1048
1049 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_EXPIRE,
1050 sizeof(*ue));
1051 ue = NLMSG_DATA(nlh);
1052 nlh->nlmsg_flags = 0;
1053
1054 copy_to_user_state(x, &ue->state);
1055 ue->hard = (hard != 0) ? 1 : 0;
1056
1057 nlh->nlmsg_len = skb->tail - b;
1058 return skb->len;
1059
1060nlmsg_failure:
1061 skb_trim(skb, b - skb->data);
1062 return -1;
1063}
1064
1065static int xfrm_send_state_notify(struct xfrm_state *x, int hard)
1066{
1067 struct sk_buff *skb;
1068
1069 skb = alloc_skb(sizeof(struct xfrm_user_expire) + 16, GFP_ATOMIC);
1070 if (skb == NULL)
1071 return -ENOMEM;
1072
1073 if (build_expire(skb, x, hard) < 0)
1074 BUG();
1075
1076 NETLINK_CB(skb).dst_groups = XFRMGRP_EXPIRE;
1077
1078 return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_EXPIRE, GFP_ATOMIC);
1079}
1080
1081static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1082 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1083 int dir)
1084{
1085 struct xfrm_user_acquire *ua;
1086 struct nlmsghdr *nlh;
1087 unsigned char *b = skb->tail;
1088 __u32 seq = xfrm_get_acqseq();
1089
1090 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1091 sizeof(*ua));
1092 ua = NLMSG_DATA(nlh);
1093 nlh->nlmsg_flags = 0;
1094
1095 memcpy(&ua->id, &x->id, sizeof(ua->id));
1096 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1097 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1098 copy_to_user_policy(xp, &ua->policy, dir);
1099 ua->aalgos = xt->aalgos;
1100 ua->ealgos = xt->ealgos;
1101 ua->calgos = xt->calgos;
1102 ua->seq = x->km.seq = seq;
1103
1104 if (copy_to_user_tmpl(xp, skb) < 0)
1105 goto nlmsg_failure;
1106
1107 nlh->nlmsg_len = skb->tail - b;
1108 return skb->len;
1109
1110nlmsg_failure:
1111 skb_trim(skb, b - skb->data);
1112 return -1;
1113}
1114
1115static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1116 struct xfrm_policy *xp, int dir)
1117{
1118 struct sk_buff *skb;
1119 size_t len;
1120
1121 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1122 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
1123 skb = alloc_skb(len, GFP_ATOMIC);
1124 if (skb == NULL)
1125 return -ENOMEM;
1126
1127 if (build_acquire(skb, x, xt, xp, dir) < 0)
1128 BUG();
1129
1130 NETLINK_CB(skb).dst_groups = XFRMGRP_ACQUIRE;
1131
1132 return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_ACQUIRE, GFP_ATOMIC);
1133}
1134
1135/* User gives us xfrm_user_policy_info followed by an array of 0
1136 * or more templates.
1137 */
1138static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
1139 u8 *data, int len, int *dir)
1140{
1141 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1142 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1143 struct xfrm_policy *xp;
1144 int nr;
1145
1146 switch (family) {
1147 case AF_INET:
1148 if (opt != IP_XFRM_POLICY) {
1149 *dir = -EOPNOTSUPP;
1150 return NULL;
1151 }
1152 break;
1153#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1154 case AF_INET6:
1155 if (opt != IPV6_XFRM_POLICY) {
1156 *dir = -EOPNOTSUPP;
1157 return NULL;
1158 }
1159 break;
1160#endif
1161 default:
1162 *dir = -EINVAL;
1163 return NULL;
1164 }
1165
1166 *dir = -EINVAL;
1167
1168 if (len < sizeof(*p) ||
1169 verify_newpolicy_info(p))
1170 return NULL;
1171
1172 nr = ((len - sizeof(*p)) / sizeof(*ut));
1173 if (nr > XFRM_MAX_DEPTH)
1174 return NULL;
1175
1176 xp = xfrm_policy_alloc(GFP_KERNEL);
1177 if (xp == NULL) {
1178 *dir = -ENOBUFS;
1179 return NULL;
1180 }
1181
1182 copy_from_user_policy(xp, p);
1183 copy_templates(xp, ut, nr);
1184
1185 *dir = p->dir;
1186
1187 return xp;
1188}
1189
1190static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
1191 int dir, int hard)
1192{
1193 struct xfrm_user_polexpire *upe;
1194 struct nlmsghdr *nlh;
1195 unsigned char *b = skb->tail;
1196
1197 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
1198 upe = NLMSG_DATA(nlh);
1199 nlh->nlmsg_flags = 0;
1200
1201 copy_to_user_policy(xp, &upe->pol, dir);
1202 if (copy_to_user_tmpl(xp, skb) < 0)
1203 goto nlmsg_failure;
1204 upe->hard = !!hard;
1205
1206 nlh->nlmsg_len = skb->tail - b;
1207 return skb->len;
1208
1209nlmsg_failure:
1210 skb_trim(skb, b - skb->data);
1211 return -1;
1212}
1213
1214static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, int hard)
1215{
1216 struct sk_buff *skb;
1217 size_t len;
1218
1219 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1220 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
1221 skb = alloc_skb(len, GFP_ATOMIC);
1222 if (skb == NULL)
1223 return -ENOMEM;
1224
1225 if (build_polexpire(skb, xp, dir, hard) < 0)
1226 BUG();
1227
1228 NETLINK_CB(skb).dst_groups = XFRMGRP_EXPIRE;
1229
1230 return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_EXPIRE, GFP_ATOMIC);
1231}
1232
1233static struct xfrm_mgr netlink_mgr = {
1234 .id = "netlink",
1235 .notify = xfrm_send_state_notify,
1236 .acquire = xfrm_send_acquire,
1237 .compile_policy = xfrm_compile_policy,
1238 .notify_policy = xfrm_send_policy_notify,
1239};
1240
1241static int __init xfrm_user_init(void)
1242{
1243 printk(KERN_INFO "Initializing IPsec netlink socket\n");
1244
1245 xfrm_nl = netlink_kernel_create(NETLINK_XFRM, xfrm_netlink_rcv);
1246 if (xfrm_nl == NULL)
1247 return -ENOMEM;
1248
1249 xfrm_register_km(&netlink_mgr);
1250
1251 return 0;
1252}
1253
1254static void __exit xfrm_user_exit(void)
1255{
1256 xfrm_unregister_km(&netlink_mgr);
1257 sock_release(xfrm_nl->sk_socket);
1258}
1259
1260module_init(xfrm_user_init);
1261module_exit(xfrm_user_exit);
1262MODULE_LICENSE("GPL");