blob: 92e2b804c6061a9442b951b99b0d99f8067a657a [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
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/rtnetlink.h>
22#include <linux/pfkeyv2.h>
23#include <linux/ipsec.h>
24#include <linux/init.h>
25#include <linux/security.h>
26#include <net/sock.h>
27#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010028#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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;
Herbert Xu31c26852005-05-19 12:39:49 -070037 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39 if (!rt)
40 return 0;
41
Herbert Xu31c26852005-05-19 12:39:49 -070042 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
43 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 return -EINVAL;
45
46 algp = RTA_DATA(rt);
Herbert Xu31c26852005-05-19 12:39:49 -070047
48 len -= (algp->alg_key_len + 7U) / 8;
49 if (len < 0)
50 return -EINVAL;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 switch (type) {
53 case XFRMA_ALG_AUTH:
54 if (!algp->alg_key_len &&
55 strcmp(algp->alg_name, "digest_null") != 0)
56 return -EINVAL;
57 break;
58
59 case XFRMA_ALG_CRYPT:
60 if (!algp->alg_key_len &&
61 strcmp(algp->alg_name, "cipher_null") != 0)
62 return -EINVAL;
63 break;
64
65 case XFRMA_ALG_COMP:
66 /* Zero length keys are legal. */
67 break;
68
69 default:
70 return -EINVAL;
71 };
72
73 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
74 return 0;
75}
76
77static int verify_encap_tmpl(struct rtattr **xfrma)
78{
79 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
80 struct xfrm_encap_tmpl *encap;
81
82 if (!rt)
83 return 0;
84
85 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
86 return -EINVAL;
87
88 return 0;
89}
90
Trent Jaegerdf718372005-12-13 23:12:27 -080091
92static inline int verify_sec_ctx_len(struct rtattr **xfrma)
93{
94 struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
95 struct xfrm_user_sec_ctx *uctx;
96 int len = 0;
97
98 if (!rt)
99 return 0;
100
101 if (rt->rta_len < sizeof(*uctx))
102 return -EINVAL;
103
104 uctx = RTA_DATA(rt);
105
106 if (uctx->ctx_len > PAGE_SIZE)
107 return -EINVAL;
108
109 len += sizeof(struct xfrm_user_sec_ctx);
110 len += uctx->ctx_len;
111
112 if (uctx->len != len)
113 return -EINVAL;
114
115 return 0;
116}
117
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static int verify_newsa_info(struct xfrm_usersa_info *p,
120 struct rtattr **xfrma)
121{
122 int err;
123
124 err = -EINVAL;
125 switch (p->family) {
126 case AF_INET:
127 break;
128
129 case AF_INET6:
130#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
131 break;
132#else
133 err = -EAFNOSUPPORT;
134 goto out;
135#endif
136
137 default:
138 goto out;
139 };
140
141 err = -EINVAL;
142 switch (p->id.proto) {
143 case IPPROTO_AH:
144 if (!xfrma[XFRMA_ALG_AUTH-1] ||
145 xfrma[XFRMA_ALG_CRYPT-1] ||
146 xfrma[XFRMA_ALG_COMP-1])
147 goto out;
148 break;
149
150 case IPPROTO_ESP:
151 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
152 !xfrma[XFRMA_ALG_CRYPT-1]) ||
153 xfrma[XFRMA_ALG_COMP-1])
154 goto out;
155 break;
156
157 case IPPROTO_COMP:
158 if (!xfrma[XFRMA_ALG_COMP-1] ||
159 xfrma[XFRMA_ALG_AUTH-1] ||
160 xfrma[XFRMA_ALG_CRYPT-1])
161 goto out;
162 break;
163
164 default:
165 goto out;
166 };
167
168 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
169 goto out;
170 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
171 goto out;
172 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
173 goto out;
174 if ((err = verify_encap_tmpl(xfrma)))
175 goto out;
Trent Jaegerdf718372005-12-13 23:12:27 -0800176 if ((err = verify_sec_ctx_len(xfrma)))
177 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 err = -EINVAL;
180 switch (p->mode) {
181 case 0:
182 case 1:
183 break;
184
185 default:
186 goto out;
187 };
188
189 err = 0;
190
191out:
192 return err;
193}
194
195static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
196 struct xfrm_algo_desc *(*get_byname)(char *, int),
197 struct rtattr *u_arg)
198{
199 struct rtattr *rta = u_arg;
200 struct xfrm_algo *p, *ualg;
201 struct xfrm_algo_desc *algo;
Herbert Xub9e9dea2005-05-19 12:39:04 -0700202 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 if (!rta)
205 return 0;
206
207 ualg = RTA_DATA(rta);
208
209 algo = get_byname(ualg->alg_name, 1);
210 if (!algo)
211 return -ENOSYS;
212 *props = algo->desc.sadb_alg_id;
213
Herbert Xub9e9dea2005-05-19 12:39:04 -0700214 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
215 p = kmalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (!p)
217 return -ENOMEM;
218
Herbert Xub9e9dea2005-05-19 12:39:04 -0700219 memcpy(p, ualg, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 *algpp = p;
221 return 0;
222}
223
224static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
225{
226 struct rtattr *rta = u_arg;
227 struct xfrm_encap_tmpl *p, *uencap;
228
229 if (!rta)
230 return 0;
231
232 uencap = RTA_DATA(rta);
233 p = kmalloc(sizeof(*p), GFP_KERNEL);
234 if (!p)
235 return -ENOMEM;
236
237 memcpy(p, uencap, sizeof(*p));
238 *encapp = p;
239 return 0;
240}
241
Trent Jaegerdf718372005-12-13 23:12:27 -0800242
243static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
244{
245 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
246 int len = 0;
247
248 if (xfrm_ctx) {
249 len += sizeof(struct xfrm_user_sec_ctx);
250 len += xfrm_ctx->ctx_len;
251 }
252 return len;
253}
254
255static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
256{
257 struct xfrm_user_sec_ctx *uctx;
258
259 if (!u_arg)
260 return 0;
261
262 uctx = RTA_DATA(u_arg);
263 return security_xfrm_state_alloc(x, uctx);
264}
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
267{
268 memcpy(&x->id, &p->id, sizeof(x->id));
269 memcpy(&x->sel, &p->sel, sizeof(x->sel));
270 memcpy(&x->lft, &p->lft, sizeof(x->lft));
271 x->props.mode = p->mode;
272 x->props.replay_window = p->replay_window;
273 x->props.reqid = p->reqid;
274 x->props.family = p->family;
275 x->props.saddr = p->saddr;
276 x->props.flags = p->flags;
277}
278
279static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
280 struct rtattr **xfrma,
281 int *errp)
282{
283 struct xfrm_state *x = xfrm_state_alloc();
284 int err = -ENOMEM;
285
286 if (!x)
287 goto error_no_put;
288
289 copy_from_user_state(x, p);
290
291 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
292 xfrm_aalg_get_byname,
293 xfrma[XFRMA_ALG_AUTH-1])))
294 goto error;
295 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
296 xfrm_ealg_get_byname,
297 xfrma[XFRMA_ALG_CRYPT-1])))
298 goto error;
299 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
300 xfrm_calg_get_byname,
301 xfrma[XFRMA_ALG_COMP-1])))
302 goto error;
303 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
304 goto error;
305
Herbert Xu72cb6962005-06-20 13:18:08 -0700306 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (err)
308 goto error;
309
Trent Jaegerdf718372005-12-13 23:12:27 -0800310 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
311 goto error;
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 x->km.seq = p->seq;
314
315 return x;
316
317error:
318 x->km.state = XFRM_STATE_DEAD;
319 xfrm_state_put(x);
320error_no_put:
321 *errp = err;
322 return NULL;
323}
324
325static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
326{
327 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
328 struct xfrm_state *x;
329 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700330 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Trent Jaegerdf718372005-12-13 23:12:27 -0800332 err = verify_newsa_info(p, (struct rtattr **)xfrma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (err)
334 return err;
335
Trent Jaegerdf718372005-12-13 23:12:27 -0800336 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (!x)
338 return err;
339
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700340 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
342 err = xfrm_state_add(x);
343 else
344 err = xfrm_state_update(x);
345
346 if (err < 0) {
347 x->km.state = XFRM_STATE_DEAD;
348 xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700349 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700352 c.seq = nlh->nlmsg_seq;
353 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700354 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700355
356 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700357out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700358 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return err;
360}
361
362static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
363{
364 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700365 int err;
366 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
368
369 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
370 if (x == NULL)
371 return -ESRCH;
372
373 if (xfrm_state_kern(x)) {
374 xfrm_state_put(x);
375 return -EPERM;
376 }
377
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700378 err = xfrm_state_delete(x);
379 if (err < 0) {
380 xfrm_state_put(x);
381 return err;
382 }
383
384 c.seq = nlh->nlmsg_seq;
385 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700386 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700387 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 xfrm_state_put(x);
389
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700390 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
394{
395 memcpy(&p->id, &x->id, sizeof(p->id));
396 memcpy(&p->sel, &x->sel, sizeof(p->sel));
397 memcpy(&p->lft, &x->lft, sizeof(p->lft));
398 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
399 memcpy(&p->stats, &x->stats, sizeof(p->stats));
400 p->saddr = x->props.saddr;
401 p->mode = x->props.mode;
402 p->replay_window = x->props.replay_window;
403 p->reqid = x->props.reqid;
404 p->family = x->props.family;
405 p->flags = x->props.flags;
406 p->seq = x->km.seq;
407}
408
409struct xfrm_dump_info {
410 struct sk_buff *in_skb;
411 struct sk_buff *out_skb;
412 u32 nlmsg_seq;
413 u16 nlmsg_flags;
414 int start_idx;
415 int this_idx;
416};
417
418static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
419{
420 struct xfrm_dump_info *sp = ptr;
421 struct sk_buff *in_skb = sp->in_skb;
422 struct sk_buff *skb = sp->out_skb;
423 struct xfrm_usersa_info *p;
424 struct nlmsghdr *nlh;
425 unsigned char *b = skb->tail;
426
427 if (sp->this_idx < sp->start_idx)
428 goto out;
429
430 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
431 sp->nlmsg_seq,
432 XFRM_MSG_NEWSA, sizeof(*p));
433 nlh->nlmsg_flags = sp->nlmsg_flags;
434
435 p = NLMSG_DATA(nlh);
436 copy_to_user_state(x, p);
437
438 if (x->aalg)
439 RTA_PUT(skb, XFRMA_ALG_AUTH,
440 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
441 if (x->ealg)
442 RTA_PUT(skb, XFRMA_ALG_CRYPT,
443 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
444 if (x->calg)
445 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
446
447 if (x->encap)
448 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
449
Trent Jaegerdf718372005-12-13 23:12:27 -0800450 if (x->security) {
451 int ctx_size = sizeof(struct xfrm_sec_ctx) +
452 x->security->ctx_len;
453 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
454 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
455
456 uctx->exttype = XFRMA_SEC_CTX;
457 uctx->len = ctx_size;
458 uctx->ctx_doi = x->security->ctx_doi;
459 uctx->ctx_alg = x->security->ctx_alg;
460 uctx->ctx_len = x->security->ctx_len;
461 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 nlh->nlmsg_len = skb->tail - b;
464out:
465 sp->this_idx++;
466 return 0;
467
468nlmsg_failure:
469rtattr_failure:
470 skb_trim(skb, b - skb->data);
471 return -1;
472}
473
474static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
475{
476 struct xfrm_dump_info info;
477
478 info.in_skb = cb->skb;
479 info.out_skb = skb;
480 info.nlmsg_seq = cb->nlh->nlmsg_seq;
481 info.nlmsg_flags = NLM_F_MULTI;
482 info.this_idx = 0;
483 info.start_idx = cb->args[0];
484 (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
485 cb->args[0] = info.this_idx;
486
487 return skb->len;
488}
489
490static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
491 struct xfrm_state *x, u32 seq)
492{
493 struct xfrm_dump_info info;
494 struct sk_buff *skb;
495
496 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
497 if (!skb)
498 return ERR_PTR(-ENOMEM);
499
500 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
501 info.in_skb = in_skb;
502 info.out_skb = skb;
503 info.nlmsg_seq = seq;
504 info.nlmsg_flags = 0;
505 info.this_idx = info.start_idx = 0;
506
507 if (dump_one_state(x, 0, &info)) {
508 kfree_skb(skb);
509 return NULL;
510 }
511
512 return skb;
513}
514
515static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
516{
517 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
518 struct xfrm_state *x;
519 struct sk_buff *resp_skb;
520 int err;
521
522 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
523 err = -ESRCH;
524 if (x == NULL)
525 goto out_noput;
526
527 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
528 if (IS_ERR(resp_skb)) {
529 err = PTR_ERR(resp_skb);
530 } else {
531 err = netlink_unicast(xfrm_nl, resp_skb,
532 NETLINK_CB(skb).pid, MSG_DONTWAIT);
533 }
534 xfrm_state_put(x);
535out_noput:
536 return err;
537}
538
539static int verify_userspi_info(struct xfrm_userspi_info *p)
540{
541 switch (p->info.id.proto) {
542 case IPPROTO_AH:
543 case IPPROTO_ESP:
544 break;
545
546 case IPPROTO_COMP:
547 /* IPCOMP spi is 16-bits. */
548 if (p->max >= 0x10000)
549 return -EINVAL;
550 break;
551
552 default:
553 return -EINVAL;
554 };
555
556 if (p->min > p->max)
557 return -EINVAL;
558
559 return 0;
560}
561
562static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
563{
564 struct xfrm_state *x;
565 struct xfrm_userspi_info *p;
566 struct sk_buff *resp_skb;
567 xfrm_address_t *daddr;
568 int family;
569 int err;
570
571 p = NLMSG_DATA(nlh);
572 err = verify_userspi_info(p);
573 if (err)
574 goto out_noput;
575
576 family = p->info.family;
577 daddr = &p->info.id.daddr;
578
579 x = NULL;
580 if (p->info.seq) {
581 x = xfrm_find_acq_byseq(p->info.seq);
582 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
583 xfrm_state_put(x);
584 x = NULL;
585 }
586 }
587
588 if (!x)
589 x = xfrm_find_acq(p->info.mode, p->info.reqid,
590 p->info.id.proto, daddr,
591 &p->info.saddr, 1,
592 family);
593 err = -ENOENT;
594 if (x == NULL)
595 goto out_noput;
596
597 resp_skb = ERR_PTR(-ENOENT);
598
599 spin_lock_bh(&x->lock);
600 if (x->km.state != XFRM_STATE_DEAD) {
601 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
602 if (x->id.spi)
603 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
604 }
605 spin_unlock_bh(&x->lock);
606
607 if (IS_ERR(resp_skb)) {
608 err = PTR_ERR(resp_skb);
609 goto out;
610 }
611
612 err = netlink_unicast(xfrm_nl, resp_skb,
613 NETLINK_CB(skb).pid, MSG_DONTWAIT);
614
615out:
616 xfrm_state_put(x);
617out_noput:
618 return err;
619}
620
621static int verify_policy_dir(__u8 dir)
622{
623 switch (dir) {
624 case XFRM_POLICY_IN:
625 case XFRM_POLICY_OUT:
626 case XFRM_POLICY_FWD:
627 break;
628
629 default:
630 return -EINVAL;
631 };
632
633 return 0;
634}
635
636static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
637{
638 switch (p->share) {
639 case XFRM_SHARE_ANY:
640 case XFRM_SHARE_SESSION:
641 case XFRM_SHARE_USER:
642 case XFRM_SHARE_UNIQUE:
643 break;
644
645 default:
646 return -EINVAL;
647 };
648
649 switch (p->action) {
650 case XFRM_POLICY_ALLOW:
651 case XFRM_POLICY_BLOCK:
652 break;
653
654 default:
655 return -EINVAL;
656 };
657
658 switch (p->sel.family) {
659 case AF_INET:
660 break;
661
662 case AF_INET6:
663#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
664 break;
665#else
666 return -EAFNOSUPPORT;
667#endif
668
669 default:
670 return -EINVAL;
671 };
672
673 return verify_policy_dir(p->dir);
674}
675
Trent Jaegerdf718372005-12-13 23:12:27 -0800676static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
677{
678 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
679 struct xfrm_user_sec_ctx *uctx;
680
681 if (!rt)
682 return 0;
683
684 uctx = RTA_DATA(rt);
685 return security_xfrm_policy_alloc(pol, uctx);
686}
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
689 int nr)
690{
691 int i;
692
693 xp->xfrm_nr = nr;
694 for (i = 0; i < nr; i++, ut++) {
695 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
696
697 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
698 memcpy(&t->saddr, &ut->saddr,
699 sizeof(xfrm_address_t));
700 t->reqid = ut->reqid;
701 t->mode = ut->mode;
702 t->share = ut->share;
703 t->optional = ut->optional;
704 t->aalgos = ut->aalgos;
705 t->ealgos = ut->ealgos;
706 t->calgos = ut->calgos;
707 }
708}
709
710static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
711{
712 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
713 struct xfrm_user_tmpl *utmpl;
714 int nr;
715
716 if (!rt) {
717 pol->xfrm_nr = 0;
718 } else {
719 nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
720
721 if (nr > XFRM_MAX_DEPTH)
722 return -EINVAL;
723
724 copy_templates(pol, RTA_DATA(rt), nr);
725 }
726 return 0;
727}
728
729static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
730{
731 xp->priority = p->priority;
732 xp->index = p->index;
733 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
734 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
735 xp->action = p->action;
736 xp->flags = p->flags;
737 xp->family = p->sel.family;
738 /* XXX xp->share = p->share; */
739}
740
741static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
742{
743 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
744 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
745 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
746 p->priority = xp->priority;
747 p->index = xp->index;
748 p->sel.family = xp->family;
749 p->dir = dir;
750 p->action = xp->action;
751 p->flags = xp->flags;
752 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
753}
754
755static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
756{
757 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
758 int err;
759
760 if (!xp) {
761 *errp = -ENOMEM;
762 return NULL;
763 }
764
765 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -0800766
767 if (!(err = copy_from_user_tmpl(xp, xfrma)))
768 err = copy_from_user_sec_ctx(xp, xfrma);
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (err) {
771 *errp = err;
772 kfree(xp);
773 xp = NULL;
774 }
775
776 return xp;
777}
778
779static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
780{
781 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
782 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700783 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 int err;
785 int excl;
786
787 err = verify_newpolicy_info(p);
788 if (err)
789 return err;
Trent Jaegerdf718372005-12-13 23:12:27 -0800790 err = verify_sec_ctx_len((struct rtattr **)xfrma);
791 if (err)
792 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Trent Jaegerdf718372005-12-13 23:12:27 -0800794 xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (!xp)
796 return err;
797
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700798 /* shouldnt excl be based on nlh flags??
799 * Aha! this is anti-netlink really i.e more pfkey derived
800 * in netlink excl is a flag and you wouldnt need
801 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
803 err = xfrm_policy_insert(p->dir, xp, excl);
804 if (err) {
805 kfree(xp);
806 return err;
807 }
808
Herbert Xuf60f6b82005-06-18 22:44:37 -0700809 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700810 c.seq = nlh->nlmsg_seq;
811 c.pid = nlh->nlmsg_pid;
812 km_policy_notify(xp, p->dir, &c);
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 xfrm_pol_put(xp);
815
816 return 0;
817}
818
819static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
820{
821 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
822 int i;
823
824 if (xp->xfrm_nr == 0)
825 return 0;
826
827 for (i = 0; i < xp->xfrm_nr; i++) {
828 struct xfrm_user_tmpl *up = &vec[i];
829 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
830
831 memcpy(&up->id, &kp->id, sizeof(up->id));
832 up->family = xp->family;
833 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
834 up->reqid = kp->reqid;
835 up->mode = kp->mode;
836 up->share = kp->share;
837 up->optional = kp->optional;
838 up->aalgos = kp->aalgos;
839 up->ealgos = kp->ealgos;
840 up->calgos = kp->calgos;
841 }
842 RTA_PUT(skb, XFRMA_TMPL,
843 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
844 vec);
845
846 return 0;
847
848rtattr_failure:
849 return -1;
850}
851
Trent Jaegerdf718372005-12-13 23:12:27 -0800852static int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
853{
854 if (xp->security) {
855 int ctx_size = sizeof(struct xfrm_sec_ctx) +
856 xp->security->ctx_len;
857 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
858 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
859
860 uctx->exttype = XFRMA_SEC_CTX;
861 uctx->len = ctx_size;
862 uctx->ctx_doi = xp->security->ctx_doi;
863 uctx->ctx_alg = xp->security->ctx_alg;
864 uctx->ctx_len = xp->security->ctx_len;
865 memcpy(uctx + 1, xp->security->ctx_str, xp->security->ctx_len);
866 }
867 return 0;
868
869 rtattr_failure:
870 return -1;
871}
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
874{
875 struct xfrm_dump_info *sp = ptr;
876 struct xfrm_userpolicy_info *p;
877 struct sk_buff *in_skb = sp->in_skb;
878 struct sk_buff *skb = sp->out_skb;
879 struct nlmsghdr *nlh;
880 unsigned char *b = skb->tail;
881
882 if (sp->this_idx < sp->start_idx)
883 goto out;
884
885 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
886 sp->nlmsg_seq,
887 XFRM_MSG_NEWPOLICY, sizeof(*p));
888 p = NLMSG_DATA(nlh);
889 nlh->nlmsg_flags = sp->nlmsg_flags;
890
891 copy_to_user_policy(xp, p, dir);
892 if (copy_to_user_tmpl(xp, skb) < 0)
893 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -0800894 if (copy_to_user_sec_ctx(xp, skb))
895 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 nlh->nlmsg_len = skb->tail - b;
898out:
899 sp->this_idx++;
900 return 0;
901
902nlmsg_failure:
903 skb_trim(skb, b - skb->data);
904 return -1;
905}
906
907static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
908{
909 struct xfrm_dump_info info;
910
911 info.in_skb = cb->skb;
912 info.out_skb = skb;
913 info.nlmsg_seq = cb->nlh->nlmsg_seq;
914 info.nlmsg_flags = NLM_F_MULTI;
915 info.this_idx = 0;
916 info.start_idx = cb->args[0];
917 (void) xfrm_policy_walk(dump_one_policy, &info);
918 cb->args[0] = info.this_idx;
919
920 return skb->len;
921}
922
923static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
924 struct xfrm_policy *xp,
925 int dir, u32 seq)
926{
927 struct xfrm_dump_info info;
928 struct sk_buff *skb;
929
930 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
931 if (!skb)
932 return ERR_PTR(-ENOMEM);
933
934 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
935 info.in_skb = in_skb;
936 info.out_skb = skb;
937 info.nlmsg_seq = seq;
938 info.nlmsg_flags = 0;
939 info.this_idx = info.start_idx = 0;
940
941 if (dump_one_policy(xp, dir, 0, &info) < 0) {
942 kfree_skb(skb);
943 return NULL;
944 }
945
946 return skb;
947}
948
949static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
950{
951 struct xfrm_policy *xp;
952 struct xfrm_userpolicy_id *p;
953 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700954 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int delete;
956
957 p = NLMSG_DATA(nlh);
958 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
959
960 err = verify_policy_dir(p->dir);
961 if (err)
962 return err;
963
964 if (p->index)
965 xp = xfrm_policy_byid(p->dir, p->index, delete);
Trent Jaegerdf718372005-12-13 23:12:27 -0800966 else {
967 struct rtattr **rtattrs = (struct rtattr **)xfrma;
968 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
969 struct xfrm_policy tmp;
970
971 err = verify_sec_ctx_len(rtattrs);
972 if (err)
973 return err;
974
975 memset(&tmp, 0, sizeof(struct xfrm_policy));
976 if (rt) {
977 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
978
979 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
980 return err;
981 }
982 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, delete);
983 security_xfrm_policy_free(&tmp);
984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (xp == NULL)
986 return -ENOENT;
987
988 if (!delete) {
989 struct sk_buff *resp_skb;
990
991 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
992 if (IS_ERR(resp_skb)) {
993 err = PTR_ERR(resp_skb);
994 } else {
995 err = netlink_unicast(xfrm_nl, resp_skb,
996 NETLINK_CB(skb).pid,
997 MSG_DONTWAIT);
998 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700999 } else {
Herbert Xue7443892005-06-18 22:44:18 -07001000 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001001 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001002 c.seq = nlh->nlmsg_seq;
1003 c.pid = nlh->nlmsg_pid;
1004 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006
1007 xfrm_pol_put(xp);
1008
1009 return err;
1010}
1011
1012static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1013{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001014 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1016
1017 xfrm_state_flush(p->proto);
Herbert Xubf088672005-06-18 22:44:00 -07001018 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001019 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001020 c.seq = nlh->nlmsg_seq;
1021 c.pid = nlh->nlmsg_pid;
1022 km_state_notify(NULL, &c);
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return 0;
1025}
1026
1027static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1028{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001029 struct km_event c;
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 xfrm_policy_flush();
Herbert Xuf60f6b82005-06-18 22:44:37 -07001032 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001033 c.seq = nlh->nlmsg_seq;
1034 c.pid = nlh->nlmsg_pid;
1035 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return 0;
1037}
1038
Thomas Graf492b5582005-05-03 14:26:40 -07001039#define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1040
1041static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1042 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1043 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1044 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1045 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1046 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1047 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1048 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1049 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1050 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1051 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1052 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1053 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1054 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1055 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056};
1057
Thomas Graf492b5582005-05-03 14:26:40 -07001058#undef XMSGSIZE
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060static struct xfrm_link {
1061 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1062 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001063} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1064 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1065 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1066 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1067 .dump = xfrm_dump_sa },
1068 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1069 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1070 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1071 .dump = xfrm_dump_policy },
1072 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1073 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1074 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1075 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1076 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077};
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1080{
1081 struct rtattr *xfrma[XFRMA_MAX];
1082 struct xfrm_link *link;
1083 int type, min_len;
1084
1085 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1086 return 0;
1087
1088 type = nlh->nlmsg_type;
1089
1090 /* A control message: ignore them */
1091 if (type < XFRM_MSG_BASE)
1092 return 0;
1093
1094 /* Unknown message: reply with EINVAL */
1095 if (type > XFRM_MSG_MAX)
1096 goto err_einval;
1097
1098 type -= XFRM_MSG_BASE;
1099 link = &xfrm_dispatch[type];
1100
1101 /* All operations require privileges, even GET */
1102 if (security_netlink_recv(skb)) {
1103 *errp = -EPERM;
1104 return -1;
1105 }
1106
Thomas Graf492b5582005-05-03 14:26:40 -07001107 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1108 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1109 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (link->dump == NULL)
1111 goto err_einval;
1112
1113 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
Thomas Grafa8f74b22005-11-10 02:25:52 +01001114 link->dump, NULL)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return -1;
1116 }
Thomas Graf88fc2c82005-11-10 02:25:54 +01001117
1118 netlink_queue_skip(nlh, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return -1;
1120 }
1121
1122 memset(xfrma, 0, sizeof(xfrma));
1123
1124 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1125 goto err_einval;
1126
1127 if (nlh->nlmsg_len > min_len) {
1128 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1129 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1130
1131 while (RTA_OK(attr, attrlen)) {
1132 unsigned short flavor = attr->rta_type;
1133 if (flavor) {
1134 if (flavor > XFRMA_MAX)
1135 goto err_einval;
1136 xfrma[flavor - 1] = attr;
1137 }
1138 attr = RTA_NEXT(attr, attrlen);
1139 }
1140 }
1141
1142 if (link->doit == NULL)
1143 goto err_einval;
1144 *errp = link->doit(skb, nlh, (void **) &xfrma);
1145
1146 return *errp;
1147
1148err_einval:
1149 *errp = -EINVAL;
1150 return -1;
1151}
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153static void xfrm_netlink_rcv(struct sock *sk, int len)
1154{
Thomas Graf88fc2c82005-11-10 02:25:54 +01001155 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 down(&xfrm_cfg_sem);
Thomas Graf88fc2c82005-11-10 02:25:54 +01001159 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 up(&xfrm_cfg_sem);
1161
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001162 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
1165static int build_expire(struct sk_buff *skb, struct xfrm_state *x, int hard)
1166{
1167 struct xfrm_user_expire *ue;
1168 struct nlmsghdr *nlh;
1169 unsigned char *b = skb->tail;
1170
1171 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_EXPIRE,
1172 sizeof(*ue));
1173 ue = NLMSG_DATA(nlh);
1174 nlh->nlmsg_flags = 0;
1175
1176 copy_to_user_state(x, &ue->state);
1177 ue->hard = (hard != 0) ? 1 : 0;
1178
1179 nlh->nlmsg_len = skb->tail - b;
1180 return skb->len;
1181
1182nlmsg_failure:
1183 skb_trim(skb, b - skb->data);
1184 return -1;
1185}
1186
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001187static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 struct sk_buff *skb;
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001190 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001192 skb = alloc_skb(len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (skb == NULL)
1194 return -ENOMEM;
1195
Herbert Xubf088672005-06-18 22:44:00 -07001196 if (build_expire(skb, x, c->data.hard) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 BUG();
1198
Patrick McHardyac6d4392005-08-14 19:29:52 -07001199 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1200 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001203static int xfrm_notify_sa_flush(struct km_event *c)
1204{
1205 struct xfrm_usersa_flush *p;
1206 struct nlmsghdr *nlh;
1207 struct sk_buff *skb;
1208 unsigned char *b;
1209 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1210
1211 skb = alloc_skb(len, GFP_ATOMIC);
1212 if (skb == NULL)
1213 return -ENOMEM;
1214 b = skb->tail;
1215
1216 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1217 XFRM_MSG_FLUSHSA, sizeof(*p));
1218 nlh->nlmsg_flags = 0;
1219
1220 p = NLMSG_DATA(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07001221 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001222
1223 nlh->nlmsg_len = skb->tail - b;
1224
Patrick McHardyac6d4392005-08-14 19:29:52 -07001225 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1226 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001227
1228nlmsg_failure:
1229 kfree_skb(skb);
1230 return -1;
1231}
1232
1233static int inline xfrm_sa_len(struct xfrm_state *x)
1234{
Herbert Xu0603eac2005-06-18 22:54:36 -07001235 int l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001236 if (x->aalg)
1237 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1238 if (x->ealg)
1239 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1240 if (x->calg)
1241 l += RTA_SPACE(sizeof(*x->calg));
1242 if (x->encap)
1243 l += RTA_SPACE(sizeof(*x->encap));
1244
1245 return l;
1246}
1247
1248static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1249{
1250 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07001251 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001252 struct nlmsghdr *nlh;
1253 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001254 unsigned char *b;
1255 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07001256 int headlen;
1257
1258 headlen = sizeof(*p);
1259 if (c->event == XFRM_MSG_DELSA) {
1260 len += RTA_SPACE(headlen);
1261 headlen = sizeof(*id);
1262 }
1263 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001264
1265 skb = alloc_skb(len, GFP_ATOMIC);
1266 if (skb == NULL)
1267 return -ENOMEM;
1268 b = skb->tail;
1269
Herbert Xu0603eac2005-06-18 22:54:36 -07001270 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001271 nlh->nlmsg_flags = 0;
1272
1273 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07001274 if (c->event == XFRM_MSG_DELSA) {
1275 id = NLMSG_DATA(nlh);
1276 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1277 id->spi = x->id.spi;
1278 id->family = x->props.family;
1279 id->proto = x->id.proto;
1280
1281 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1282 }
1283
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001284 copy_to_user_state(x, p);
1285
1286 if (x->aalg)
1287 RTA_PUT(skb, XFRMA_ALG_AUTH,
1288 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1289 if (x->ealg)
1290 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1291 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1292 if (x->calg)
1293 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1294
1295 if (x->encap)
1296 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1297
1298 nlh->nlmsg_len = skb->tail - b;
1299
Patrick McHardyac6d4392005-08-14 19:29:52 -07001300 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1301 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001302
1303nlmsg_failure:
1304rtattr_failure:
1305 kfree_skb(skb);
1306 return -1;
1307}
1308
1309static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1310{
1311
1312 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001313 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001314 return xfrm_exp_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001315 case XFRM_MSG_DELSA:
1316 case XFRM_MSG_UPDSA:
1317 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001318 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001319 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001320 return xfrm_notify_sa_flush(c);
1321 default:
1322 printk("xfrm_user: Unknown SA event %d\n", c->event);
1323 break;
1324 }
1325
1326 return 0;
1327
1328}
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1331 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1332 int dir)
1333{
1334 struct xfrm_user_acquire *ua;
1335 struct nlmsghdr *nlh;
1336 unsigned char *b = skb->tail;
1337 __u32 seq = xfrm_get_acqseq();
1338
1339 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1340 sizeof(*ua));
1341 ua = NLMSG_DATA(nlh);
1342 nlh->nlmsg_flags = 0;
1343
1344 memcpy(&ua->id, &x->id, sizeof(ua->id));
1345 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1346 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1347 copy_to_user_policy(xp, &ua->policy, dir);
1348 ua->aalgos = xt->aalgos;
1349 ua->ealgos = xt->ealgos;
1350 ua->calgos = xt->calgos;
1351 ua->seq = x->km.seq = seq;
1352
1353 if (copy_to_user_tmpl(xp, skb) < 0)
1354 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001355 if (copy_to_user_sec_ctx(xp, skb))
1356 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 nlh->nlmsg_len = skb->tail - b;
1359 return skb->len;
1360
1361nlmsg_failure:
1362 skb_trim(skb, b - skb->data);
1363 return -1;
1364}
1365
1366static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1367 struct xfrm_policy *xp, int dir)
1368{
1369 struct sk_buff *skb;
1370 size_t len;
1371
1372 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1373 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
Trent Jaegerdf718372005-12-13 23:12:27 -08001374 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 skb = alloc_skb(len, GFP_ATOMIC);
1376 if (skb == NULL)
1377 return -ENOMEM;
1378
1379 if (build_acquire(skb, x, xt, xp, dir) < 0)
1380 BUG();
1381
Patrick McHardyac6d4392005-08-14 19:29:52 -07001382 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
1383 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
1386/* User gives us xfrm_user_policy_info followed by an array of 0
1387 * or more templates.
1388 */
1389static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
1390 u8 *data, int len, int *dir)
1391{
1392 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1393 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1394 struct xfrm_policy *xp;
1395 int nr;
1396
1397 switch (family) {
1398 case AF_INET:
1399 if (opt != IP_XFRM_POLICY) {
1400 *dir = -EOPNOTSUPP;
1401 return NULL;
1402 }
1403 break;
1404#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1405 case AF_INET6:
1406 if (opt != IPV6_XFRM_POLICY) {
1407 *dir = -EOPNOTSUPP;
1408 return NULL;
1409 }
1410 break;
1411#endif
1412 default:
1413 *dir = -EINVAL;
1414 return NULL;
1415 }
1416
1417 *dir = -EINVAL;
1418
1419 if (len < sizeof(*p) ||
1420 verify_newpolicy_info(p))
1421 return NULL;
1422
1423 nr = ((len - sizeof(*p)) / sizeof(*ut));
1424 if (nr > XFRM_MAX_DEPTH)
1425 return NULL;
1426
Herbert Xua4f1bac2005-07-26 15:43:17 -07001427 if (p->dir > XFRM_POLICY_OUT)
1428 return NULL;
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 xp = xfrm_policy_alloc(GFP_KERNEL);
1431 if (xp == NULL) {
1432 *dir = -ENOBUFS;
1433 return NULL;
1434 }
1435
1436 copy_from_user_policy(xp, p);
1437 copy_templates(xp, ut, nr);
1438
1439 *dir = p->dir;
1440
1441 return xp;
1442}
1443
1444static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
1445 int dir, int hard)
1446{
1447 struct xfrm_user_polexpire *upe;
1448 struct nlmsghdr *nlh;
1449 unsigned char *b = skb->tail;
1450
1451 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
1452 upe = NLMSG_DATA(nlh);
1453 nlh->nlmsg_flags = 0;
1454
1455 copy_to_user_policy(xp, &upe->pol, dir);
1456 if (copy_to_user_tmpl(xp, skb) < 0)
1457 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001458 if (copy_to_user_sec_ctx(xp, skb))
1459 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 upe->hard = !!hard;
1461
1462 nlh->nlmsg_len = skb->tail - b;
1463 return skb->len;
1464
1465nlmsg_failure:
1466 skb_trim(skb, b - skb->data);
1467 return -1;
1468}
1469
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001470static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 struct sk_buff *skb;
1473 size_t len;
1474
1475 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1476 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
Trent Jaegerdf718372005-12-13 23:12:27 -08001477 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 skb = alloc_skb(len, GFP_ATOMIC);
1479 if (skb == NULL)
1480 return -ENOMEM;
1481
Herbert Xubf088672005-06-18 22:44:00 -07001482 if (build_polexpire(skb, xp, dir, c->data.hard) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 BUG();
1484
Patrick McHardyac6d4392005-08-14 19:29:52 -07001485 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1486 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001489static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
1490{
1491 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07001492 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001493 struct nlmsghdr *nlh;
1494 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001495 unsigned char *b;
1496 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07001497 int headlen;
1498
1499 headlen = sizeof(*p);
1500 if (c->event == XFRM_MSG_DELPOLICY) {
1501 len += RTA_SPACE(headlen);
1502 headlen = sizeof(*id);
1503 }
1504 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001505
1506 skb = alloc_skb(len, GFP_ATOMIC);
1507 if (skb == NULL)
1508 return -ENOMEM;
1509 b = skb->tail;
1510
Herbert Xu0603eac2005-06-18 22:54:36 -07001511 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001512
1513 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07001514 if (c->event == XFRM_MSG_DELPOLICY) {
1515 id = NLMSG_DATA(nlh);
1516 memset(id, 0, sizeof(*id));
1517 id->dir = dir;
1518 if (c->data.byid)
1519 id->index = xp->index;
1520 else
1521 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
1522
1523 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
1524 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001525
1526 nlh->nlmsg_flags = 0;
1527
1528 copy_to_user_policy(xp, p, dir);
1529 if (copy_to_user_tmpl(xp, skb) < 0)
1530 goto nlmsg_failure;
1531
1532 nlh->nlmsg_len = skb->tail - b;
1533
Patrick McHardyac6d4392005-08-14 19:29:52 -07001534 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1535 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001536
1537nlmsg_failure:
Herbert Xu0603eac2005-06-18 22:54:36 -07001538rtattr_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001539 kfree_skb(skb);
1540 return -1;
1541}
1542
1543static int xfrm_notify_policy_flush(struct km_event *c)
1544{
1545 struct nlmsghdr *nlh;
1546 struct sk_buff *skb;
1547 unsigned char *b;
1548 int len = NLMSG_LENGTH(0);
1549
1550 skb = alloc_skb(len, GFP_ATOMIC);
1551 if (skb == NULL)
1552 return -ENOMEM;
1553 b = skb->tail;
1554
1555
1556 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
1557
1558 nlh->nlmsg_len = skb->tail - b;
1559
Patrick McHardyac6d4392005-08-14 19:29:52 -07001560 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1561 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001562
1563nlmsg_failure:
1564 kfree_skb(skb);
1565 return -1;
1566}
1567
1568static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1569{
1570
1571 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001572 case XFRM_MSG_NEWPOLICY:
1573 case XFRM_MSG_UPDPOLICY:
1574 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001575 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001576 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001577 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001578 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001579 return xfrm_exp_policy_notify(xp, dir, c);
1580 default:
1581 printk("xfrm_user: Unknown Policy event %d\n", c->event);
1582 }
1583
1584 return 0;
1585
1586}
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588static struct xfrm_mgr netlink_mgr = {
1589 .id = "netlink",
1590 .notify = xfrm_send_state_notify,
1591 .acquire = xfrm_send_acquire,
1592 .compile_policy = xfrm_compile_policy,
1593 .notify_policy = xfrm_send_policy_notify,
1594};
1595
1596static int __init xfrm_user_init(void)
1597{
1598 printk(KERN_INFO "Initializing IPsec netlink socket\n");
1599
Patrick McHardy06628602005-08-15 12:33:26 -07001600 xfrm_nl = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
1601 xfrm_netlink_rcv, THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (xfrm_nl == NULL)
1603 return -ENOMEM;
1604
1605 xfrm_register_km(&netlink_mgr);
1606
1607 return 0;
1608}
1609
1610static void __exit xfrm_user_exit(void)
1611{
1612 xfrm_unregister_km(&netlink_mgr);
1613 sock_release(xfrm_nl->sk_socket);
1614}
1615
1616module_init(xfrm_user_init);
1617module_exit(xfrm_user_exit);
1618MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001619MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);