]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/xfrm/xfrm_user.c
[IPV6] ROUTE: Unify RT6_F_xxx and RT6_SELECT_F_xxx flags
[net-next-2.6.git] / net / xfrm / xfrm_user.c
CommitLineData
1da177e4
LT
1/* 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
df71837d 10 *
1da177e4
LT
11 */
12
9409f38a 13#include <linux/crypto.h>
1da177e4
LT
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>
1da177e4
LT
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>
88fc2c84 29#include <net/netlink.h>
1da177e4
LT
30#include <asm/uaccess.h>
31
1da177e4
LT
32static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
33{
34 struct rtattr *rt = xfrma[type - 1];
35 struct xfrm_algo *algp;
31c26852 36 int len;
1da177e4
LT
37
38 if (!rt)
39 return 0;
40
31c26852
HX
41 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
42 if (len < 0)
1da177e4
LT
43 return -EINVAL;
44
45 algp = RTA_DATA(rt);
31c26852
HX
46
47 len -= (algp->alg_key_len + 7U) / 8;
48 if (len < 0)
49 return -EINVAL;
50
1da177e4
LT
51 switch (type) {
52 case XFRMA_ALG_AUTH:
53 if (!algp->alg_key_len &&
54 strcmp(algp->alg_name, "digest_null") != 0)
55 return -EINVAL;
56 break;
57
58 case XFRMA_ALG_CRYPT:
59 if (!algp->alg_key_len &&
60 strcmp(algp->alg_name, "cipher_null") != 0)
61 return -EINVAL;
62 break;
63
64 case XFRMA_ALG_COMP:
65 /* Zero length keys are legal. */
66 break;
67
68 default:
69 return -EINVAL;
70 };
71
72 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
73 return 0;
74}
75
76static int verify_encap_tmpl(struct rtattr **xfrma)
77{
78 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
79 struct xfrm_encap_tmpl *encap;
80
81 if (!rt)
82 return 0;
83
84 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
85 return -EINVAL;
86
87 return 0;
88}
89
df71837d
TJ
90
91static inline int verify_sec_ctx_len(struct rtattr **xfrma)
92{
93 struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
94 struct xfrm_user_sec_ctx *uctx;
95 int len = 0;
96
97 if (!rt)
98 return 0;
99
100 if (rt->rta_len < sizeof(*uctx))
101 return -EINVAL;
102
103 uctx = RTA_DATA(rt);
104
df71837d
TJ
105 len += sizeof(struct xfrm_user_sec_ctx);
106 len += uctx->ctx_len;
107
108 if (uctx->len != len)
109 return -EINVAL;
110
111 return 0;
112}
113
114
1da177e4
LT
115static int verify_newsa_info(struct xfrm_usersa_info *p,
116 struct rtattr **xfrma)
117{
118 int err;
119
120 err = -EINVAL;
121 switch (p->family) {
122 case AF_INET:
123 break;
124
125 case AF_INET6:
126#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
127 break;
128#else
129 err = -EAFNOSUPPORT;
130 goto out;
131#endif
132
133 default:
134 goto out;
135 };
136
137 err = -EINVAL;
138 switch (p->id.proto) {
139 case IPPROTO_AH:
140 if (!xfrma[XFRMA_ALG_AUTH-1] ||
141 xfrma[XFRMA_ALG_CRYPT-1] ||
142 xfrma[XFRMA_ALG_COMP-1])
143 goto out;
144 break;
145
146 case IPPROTO_ESP:
147 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
148 !xfrma[XFRMA_ALG_CRYPT-1]) ||
149 xfrma[XFRMA_ALG_COMP-1])
150 goto out;
151 break;
152
153 case IPPROTO_COMP:
154 if (!xfrma[XFRMA_ALG_COMP-1] ||
155 xfrma[XFRMA_ALG_AUTH-1] ||
156 xfrma[XFRMA_ALG_CRYPT-1])
157 goto out;
158 break;
159
160 default:
161 goto out;
162 };
163
164 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
165 goto out;
166 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
167 goto out;
168 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
169 goto out;
170 if ((err = verify_encap_tmpl(xfrma)))
171 goto out;
df71837d
TJ
172 if ((err = verify_sec_ctx_len(xfrma)))
173 goto out;
1da177e4
LT
174
175 err = -EINVAL;
176 switch (p->mode) {
177 case 0:
178 case 1:
179 break;
180
181 default:
182 goto out;
183 };
184
185 err = 0;
186
187out:
188 return err;
189}
190
191static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
192 struct xfrm_algo_desc *(*get_byname)(char *, int),
193 struct rtattr *u_arg)
194{
195 struct rtattr *rta = u_arg;
196 struct xfrm_algo *p, *ualg;
197 struct xfrm_algo_desc *algo;
b9e9dead 198 int len;
1da177e4
LT
199
200 if (!rta)
201 return 0;
202
203 ualg = RTA_DATA(rta);
204
205 algo = get_byname(ualg->alg_name, 1);
206 if (!algo)
207 return -ENOSYS;
208 *props = algo->desc.sadb_alg_id;
209
b9e9dead
HX
210 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
211 p = kmalloc(len, GFP_KERNEL);
1da177e4
LT
212 if (!p)
213 return -ENOMEM;
214
b9e9dead 215 memcpy(p, ualg, len);
04ff1260 216 strcpy(p->alg_name, algo->name);
1da177e4
LT
217 *algpp = p;
218 return 0;
219}
220
221static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
222{
223 struct rtattr *rta = u_arg;
224 struct xfrm_encap_tmpl *p, *uencap;
225
226 if (!rta)
227 return 0;
228
229 uencap = RTA_DATA(rta);
230 p = kmalloc(sizeof(*p), GFP_KERNEL);
231 if (!p)
232 return -ENOMEM;
233
234 memcpy(p, uencap, sizeof(*p));
235 *encapp = p;
236 return 0;
237}
238
df71837d
TJ
239
240static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
241{
242 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
243 int len = 0;
244
245 if (xfrm_ctx) {
246 len += sizeof(struct xfrm_user_sec_ctx);
247 len += xfrm_ctx->ctx_len;
248 }
249 return len;
250}
251
252static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
253{
254 struct xfrm_user_sec_ctx *uctx;
255
256 if (!u_arg)
257 return 0;
258
259 uctx = RTA_DATA(u_arg);
260 return security_xfrm_state_alloc(x, uctx);
261}
262
1da177e4
LT
263static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
264{
265 memcpy(&x->id, &p->id, sizeof(x->id));
266 memcpy(&x->sel, &p->sel, sizeof(x->sel));
267 memcpy(&x->lft, &p->lft, sizeof(x->lft));
268 x->props.mode = p->mode;
269 x->props.replay_window = p->replay_window;
270 x->props.reqid = p->reqid;
271 x->props.family = p->family;
272 x->props.saddr = p->saddr;
273 x->props.flags = p->flags;
274}
275
d51d081d
JHS
276/*
277 * someday when pfkey also has support, we could have the code
278 * somehow made shareable and move it to xfrm_state.c - JHS
279 *
280*/
281static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
282{
283 int err = - EINVAL;
284 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
285 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
286 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
287 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
288
289 if (rp) {
290 struct xfrm_replay_state *replay;
291 if (RTA_PAYLOAD(rp) < sizeof(*replay))
292 goto error;
293 replay = RTA_DATA(rp);
294 memcpy(&x->replay, replay, sizeof(*replay));
295 memcpy(&x->preplay, replay, sizeof(*replay));
296 }
297
298 if (lt) {
299 struct xfrm_lifetime_cur *ltime;
300 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
301 goto error;
302 ltime = RTA_DATA(lt);
303 x->curlft.bytes = ltime->bytes;
304 x->curlft.packets = ltime->packets;
305 x->curlft.add_time = ltime->add_time;
306 x->curlft.use_time = ltime->use_time;
307 }
308
309 if (et) {
310 if (RTA_PAYLOAD(et) < sizeof(u32))
311 goto error;
312 x->replay_maxage = *(u32*)RTA_DATA(et);
313 }
314
315 if (rt) {
316 if (RTA_PAYLOAD(rt) < sizeof(u32))
317 goto error;
318 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
319 }
320
321 return 0;
322error:
323 return err;
324}
325
1da177e4
LT
326static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
327 struct rtattr **xfrma,
328 int *errp)
329{
330 struct xfrm_state *x = xfrm_state_alloc();
331 int err = -ENOMEM;
332
333 if (!x)
334 goto error_no_put;
335
336 copy_from_user_state(x, p);
337
338 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
339 xfrm_aalg_get_byname,
340 xfrma[XFRMA_ALG_AUTH-1])))
341 goto error;
342 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
343 xfrm_ealg_get_byname,
344 xfrma[XFRMA_ALG_CRYPT-1])))
345 goto error;
346 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
347 xfrm_calg_get_byname,
348 xfrma[XFRMA_ALG_COMP-1])))
349 goto error;
350 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
351 goto error;
352
72cb6962 353 err = xfrm_init_state(x);
1da177e4
LT
354 if (err)
355 goto error;
356
df71837d
TJ
357 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
358 goto error;
359
1da177e4 360 x->km.seq = p->seq;
d51d081d
JHS
361 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
362 /* sysctl_xfrm_aevent_etime is in 100ms units */
363 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
364 x->preplay.bitmap = 0;
365 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
366 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
367
368 /* override default values from above */
369
370 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
371 if (err < 0)
372 goto error;
1da177e4
LT
373
374 return x;
375
376error:
377 x->km.state = XFRM_STATE_DEAD;
378 xfrm_state_put(x);
379error_no_put:
380 *errp = err;
381 return NULL;
382}
383
384static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
385{
386 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
387 struct xfrm_state *x;
388 int err;
26b15dad 389 struct km_event c;
1da177e4 390
df71837d 391 err = verify_newsa_info(p, (struct rtattr **)xfrma);
1da177e4
LT
392 if (err)
393 return err;
394
df71837d 395 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
1da177e4
LT
396 if (!x)
397 return err;
398
26b15dad 399 xfrm_state_hold(x);
1da177e4
LT
400 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
401 err = xfrm_state_add(x);
402 else
403 err = xfrm_state_update(x);
404
405 if (err < 0) {
406 x->km.state = XFRM_STATE_DEAD;
21380b81 407 __xfrm_state_put(x);
7d6dfe1f 408 goto out;
1da177e4
LT
409 }
410
26b15dad
JHS
411 c.seq = nlh->nlmsg_seq;
412 c.pid = nlh->nlmsg_pid;
f60f6b8f 413 c.event = nlh->nlmsg_type;
26b15dad
JHS
414
415 km_state_notify(x, &c);
7d6dfe1f 416out:
26b15dad 417 xfrm_state_put(x);
1da177e4
LT
418 return err;
419}
420
421static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
422{
423 struct xfrm_state *x;
26b15dad
JHS
424 int err;
425 struct km_event c;
1da177e4
LT
426 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
427
428 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
429 if (x == NULL)
430 return -ESRCH;
431
6f68dc37 432 if ((err = security_xfrm_state_delete(x)) != 0)
c8c05a8e
CZ
433 goto out;
434
1da177e4 435 if (xfrm_state_kern(x)) {
c8c05a8e
CZ
436 err = -EPERM;
437 goto out;
1da177e4
LT
438 }
439
26b15dad 440 err = xfrm_state_delete(x);
c8c05a8e
CZ
441 if (err < 0)
442 goto out;
26b15dad
JHS
443
444 c.seq = nlh->nlmsg_seq;
445 c.pid = nlh->nlmsg_pid;
f60f6b8f 446 c.event = nlh->nlmsg_type;
26b15dad 447 km_state_notify(x, &c);
1da177e4 448
c8c05a8e
CZ
449out:
450 xfrm_state_put(x);
26b15dad 451 return err;
1da177e4
LT
452}
453
454static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
455{
456 memcpy(&p->id, &x->id, sizeof(p->id));
457 memcpy(&p->sel, &x->sel, sizeof(p->sel));
458 memcpy(&p->lft, &x->lft, sizeof(p->lft));
459 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
460 memcpy(&p->stats, &x->stats, sizeof(p->stats));
461 p->saddr = x->props.saddr;
462 p->mode = x->props.mode;
463 p->replay_window = x->props.replay_window;
464 p->reqid = x->props.reqid;
465 p->family = x->props.family;
466 p->flags = x->props.flags;
467 p->seq = x->km.seq;
468}
469
470struct xfrm_dump_info {
471 struct sk_buff *in_skb;
472 struct sk_buff *out_skb;
473 u32 nlmsg_seq;
474 u16 nlmsg_flags;
475 int start_idx;
476 int this_idx;
477};
478
479static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
480{
481 struct xfrm_dump_info *sp = ptr;
482 struct sk_buff *in_skb = sp->in_skb;
483 struct sk_buff *skb = sp->out_skb;
484 struct xfrm_usersa_info *p;
485 struct nlmsghdr *nlh;
486 unsigned char *b = skb->tail;
487
488 if (sp->this_idx < sp->start_idx)
489 goto out;
490
491 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
492 sp->nlmsg_seq,
493 XFRM_MSG_NEWSA, sizeof(*p));
494 nlh->nlmsg_flags = sp->nlmsg_flags;
495
496 p = NLMSG_DATA(nlh);
497 copy_to_user_state(x, p);
498
499 if (x->aalg)
500 RTA_PUT(skb, XFRMA_ALG_AUTH,
501 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
502 if (x->ealg)
503 RTA_PUT(skb, XFRMA_ALG_CRYPT,
504 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
505 if (x->calg)
506 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
507
508 if (x->encap)
509 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
510
df71837d
TJ
511 if (x->security) {
512 int ctx_size = sizeof(struct xfrm_sec_ctx) +
513 x->security->ctx_len;
514 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
515 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
516
517 uctx->exttype = XFRMA_SEC_CTX;
518 uctx->len = ctx_size;
519 uctx->ctx_doi = x->security->ctx_doi;
520 uctx->ctx_alg = x->security->ctx_alg;
521 uctx->ctx_len = x->security->ctx_len;
522 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
523 }
1da177e4
LT
524 nlh->nlmsg_len = skb->tail - b;
525out:
526 sp->this_idx++;
527 return 0;
528
529nlmsg_failure:
530rtattr_failure:
531 skb_trim(skb, b - skb->data);
532 return -1;
533}
534
535static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
536{
537 struct xfrm_dump_info info;
538
539 info.in_skb = cb->skb;
540 info.out_skb = skb;
541 info.nlmsg_seq = cb->nlh->nlmsg_seq;
542 info.nlmsg_flags = NLM_F_MULTI;
543 info.this_idx = 0;
544 info.start_idx = cb->args[0];
545 (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
546 cb->args[0] = info.this_idx;
547
548 return skb->len;
549}
550
551static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
552 struct xfrm_state *x, u32 seq)
553{
554 struct xfrm_dump_info info;
555 struct sk_buff *skb;
556
557 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
558 if (!skb)
559 return ERR_PTR(-ENOMEM);
560
561 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
562 info.in_skb = in_skb;
563 info.out_skb = skb;
564 info.nlmsg_seq = seq;
565 info.nlmsg_flags = 0;
566 info.this_idx = info.start_idx = 0;
567
568 if (dump_one_state(x, 0, &info)) {
569 kfree_skb(skb);
570 return NULL;
571 }
572
573 return skb;
574}
575
576static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
577{
578 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
579 struct xfrm_state *x;
580 struct sk_buff *resp_skb;
581 int err;
582
583 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
584 err = -ESRCH;
585 if (x == NULL)
586 goto out_noput;
587
588 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
589 if (IS_ERR(resp_skb)) {
590 err = PTR_ERR(resp_skb);
591 } else {
592 err = netlink_unicast(xfrm_nl, resp_skb,
593 NETLINK_CB(skb).pid, MSG_DONTWAIT);
594 }
595 xfrm_state_put(x);
596out_noput:
597 return err;
598}
599
600static int verify_userspi_info(struct xfrm_userspi_info *p)
601{
602 switch (p->info.id.proto) {
603 case IPPROTO_AH:
604 case IPPROTO_ESP:
605 break;
606
607 case IPPROTO_COMP:
608 /* IPCOMP spi is 16-bits. */
609 if (p->max >= 0x10000)
610 return -EINVAL;
611 break;
612
613 default:
614 return -EINVAL;
615 };
616
617 if (p->min > p->max)
618 return -EINVAL;
619
620 return 0;
621}
622
623static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
624{
625 struct xfrm_state *x;
626 struct xfrm_userspi_info *p;
627 struct sk_buff *resp_skb;
628 xfrm_address_t *daddr;
629 int family;
630 int err;
631
632 p = NLMSG_DATA(nlh);
633 err = verify_userspi_info(p);
634 if (err)
635 goto out_noput;
636
637 family = p->info.family;
638 daddr = &p->info.id.daddr;
639
640 x = NULL;
641 if (p->info.seq) {
642 x = xfrm_find_acq_byseq(p->info.seq);
643 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
644 xfrm_state_put(x);
645 x = NULL;
646 }
647 }
648
649 if (!x)
650 x = xfrm_find_acq(p->info.mode, p->info.reqid,
651 p->info.id.proto, daddr,
652 &p->info.saddr, 1,
653 family);
654 err = -ENOENT;
655 if (x == NULL)
656 goto out_noput;
657
658 resp_skb = ERR_PTR(-ENOENT);
659
660 spin_lock_bh(&x->lock);
661 if (x->km.state != XFRM_STATE_DEAD) {
662 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
663 if (x->id.spi)
664 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
665 }
666 spin_unlock_bh(&x->lock);
667
668 if (IS_ERR(resp_skb)) {
669 err = PTR_ERR(resp_skb);
670 goto out;
671 }
672
673 err = netlink_unicast(xfrm_nl, resp_skb,
674 NETLINK_CB(skb).pid, MSG_DONTWAIT);
675
676out:
677 xfrm_state_put(x);
678out_noput:
679 return err;
680}
681
682static int verify_policy_dir(__u8 dir)
683{
684 switch (dir) {
685 case XFRM_POLICY_IN:
686 case XFRM_POLICY_OUT:
687 case XFRM_POLICY_FWD:
688 break;
689
690 default:
691 return -EINVAL;
692 };
693
694 return 0;
695}
696
697static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
698{
699 switch (p->share) {
700 case XFRM_SHARE_ANY:
701 case XFRM_SHARE_SESSION:
702 case XFRM_SHARE_USER:
703 case XFRM_SHARE_UNIQUE:
704 break;
705
706 default:
707 return -EINVAL;
708 };
709
710 switch (p->action) {
711 case XFRM_POLICY_ALLOW:
712 case XFRM_POLICY_BLOCK:
713 break;
714
715 default:
716 return -EINVAL;
717 };
718
719 switch (p->sel.family) {
720 case AF_INET:
721 break;
722
723 case AF_INET6:
724#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
725 break;
726#else
727 return -EAFNOSUPPORT;
728#endif
729
730 default:
731 return -EINVAL;
732 };
733
734 return verify_policy_dir(p->dir);
735}
736
df71837d
TJ
737static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
738{
739 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
740 struct xfrm_user_sec_ctx *uctx;
741
742 if (!rt)
743 return 0;
744
745 uctx = RTA_DATA(rt);
746 return security_xfrm_policy_alloc(pol, uctx);
747}
748
1da177e4
LT
749static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
750 int nr)
751{
752 int i;
753
754 xp->xfrm_nr = nr;
755 for (i = 0; i < nr; i++, ut++) {
756 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
757
758 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
759 memcpy(&t->saddr, &ut->saddr,
760 sizeof(xfrm_address_t));
761 t->reqid = ut->reqid;
762 t->mode = ut->mode;
763 t->share = ut->share;
764 t->optional = ut->optional;
765 t->aalgos = ut->aalgos;
766 t->ealgos = ut->ealgos;
767 t->calgos = ut->calgos;
768 }
769}
770
771static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
772{
773 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
774 struct xfrm_user_tmpl *utmpl;
775 int nr;
776
777 if (!rt) {
778 pol->xfrm_nr = 0;
779 } else {
780 nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
781
782 if (nr > XFRM_MAX_DEPTH)
783 return -EINVAL;
784
785 copy_templates(pol, RTA_DATA(rt), nr);
786 }
787 return 0;
788}
789
790static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
791{
792 xp->priority = p->priority;
793 xp->index = p->index;
794 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
795 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
796 xp->action = p->action;
797 xp->flags = p->flags;
798 xp->family = p->sel.family;
799 /* XXX xp->share = p->share; */
800}
801
802static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
803{
804 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
805 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
806 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
807 p->priority = xp->priority;
808 p->index = xp->index;
809 p->sel.family = xp->family;
810 p->dir = dir;
811 p->action = xp->action;
812 p->flags = xp->flags;
813 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
814}
815
816static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
817{
818 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
819 int err;
820
821 if (!xp) {
822 *errp = -ENOMEM;
823 return NULL;
824 }
825
826 copy_from_user_policy(xp, p);
df71837d
TJ
827
828 if (!(err = copy_from_user_tmpl(xp, xfrma)))
829 err = copy_from_user_sec_ctx(xp, xfrma);
830
1da177e4
LT
831 if (err) {
832 *errp = err;
833 kfree(xp);
834 xp = NULL;
835 }
836
837 return xp;
838}
839
840static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
841{
842 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
843 struct xfrm_policy *xp;
26b15dad 844 struct km_event c;
1da177e4
LT
845 int err;
846 int excl;
847
848 err = verify_newpolicy_info(p);
df71837d
TJ
849 if (err)
850 return err;
851 err = verify_sec_ctx_len((struct rtattr **)xfrma);
1da177e4
LT
852 if (err)
853 return err;
854
df71837d 855 xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
1da177e4
LT
856 if (!xp)
857 return err;
858
26b15dad
JHS
859 /* shouldnt excl be based on nlh flags??
860 * Aha! this is anti-netlink really i.e more pfkey derived
861 * in netlink excl is a flag and you wouldnt need
862 * a type XFRM_MSG_UPDPOLICY - JHS */
1da177e4
LT
863 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
864 err = xfrm_policy_insert(p->dir, xp, excl);
865 if (err) {
5f8ac64b 866 security_xfrm_policy_free(xp);
1da177e4
LT
867 kfree(xp);
868 return err;
869 }
870
f60f6b8f 871 c.event = nlh->nlmsg_type;
26b15dad
JHS
872 c.seq = nlh->nlmsg_seq;
873 c.pid = nlh->nlmsg_pid;
874 km_policy_notify(xp, p->dir, &c);
875
1da177e4
LT
876 xfrm_pol_put(xp);
877
878 return 0;
879}
880
881static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
882{
883 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
884 int i;
885
886 if (xp->xfrm_nr == 0)
887 return 0;
888
889 for (i = 0; i < xp->xfrm_nr; i++) {
890 struct xfrm_user_tmpl *up = &vec[i];
891 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
892
893 memcpy(&up->id, &kp->id, sizeof(up->id));
894 up->family = xp->family;
895 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
896 up->reqid = kp->reqid;
897 up->mode = kp->mode;
898 up->share = kp->share;
899 up->optional = kp->optional;
900 up->aalgos = kp->aalgos;
901 up->ealgos = kp->ealgos;
902 up->calgos = kp->calgos;
903 }
904 RTA_PUT(skb, XFRMA_TMPL,
905 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
906 vec);
907
908 return 0;
909
910rtattr_failure:
911 return -1;
912}
913
0d681623 914static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
df71837d 915{
0d681623
SH
916 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
917 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
918 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
919
920 uctx->exttype = XFRMA_SEC_CTX;
921 uctx->len = ctx_size;
922 uctx->ctx_doi = s->ctx_doi;
923 uctx->ctx_alg = s->ctx_alg;
924 uctx->ctx_len = s->ctx_len;
925 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
926 return 0;
df71837d 927
0d681623
SH
928 rtattr_failure:
929 return -1;
930}
931
932static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
933{
934 if (x->security) {
935 return copy_sec_ctx(x->security, skb);
df71837d
TJ
936 }
937 return 0;
0d681623 938}
df71837d 939
0d681623
SH
940static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
941{
942 if (xp->security) {
943 return copy_sec_ctx(xp->security, skb);
944 }
945 return 0;
df71837d
TJ
946}
947
1da177e4
LT
948static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
949{
950 struct xfrm_dump_info *sp = ptr;
951 struct xfrm_userpolicy_info *p;
952 struct sk_buff *in_skb = sp->in_skb;
953 struct sk_buff *skb = sp->out_skb;
954 struct nlmsghdr *nlh;
955 unsigned char *b = skb->tail;
956
957 if (sp->this_idx < sp->start_idx)
958 goto out;
959
960 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
961 sp->nlmsg_seq,
962 XFRM_MSG_NEWPOLICY, sizeof(*p));
963 p = NLMSG_DATA(nlh);
964 nlh->nlmsg_flags = sp->nlmsg_flags;
965
966 copy_to_user_policy(xp, p, dir);
967 if (copy_to_user_tmpl(xp, skb) < 0)
968 goto nlmsg_failure;
df71837d
TJ
969 if (copy_to_user_sec_ctx(xp, skb))
970 goto nlmsg_failure;
1da177e4
LT
971
972 nlh->nlmsg_len = skb->tail - b;
973out:
974 sp->this_idx++;
975 return 0;
976
977nlmsg_failure:
978 skb_trim(skb, b - skb->data);
979 return -1;
980}
981
982static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
983{
984 struct xfrm_dump_info info;
985
986 info.in_skb = cb->skb;
987 info.out_skb = skb;
988 info.nlmsg_seq = cb->nlh->nlmsg_seq;
989 info.nlmsg_flags = NLM_F_MULTI;
990 info.this_idx = 0;
991 info.start_idx = cb->args[0];
992 (void) xfrm_policy_walk(dump_one_policy, &info);
993 cb->args[0] = info.this_idx;
994
995 return skb->len;
996}
997
998static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
999 struct xfrm_policy *xp,
1000 int dir, u32 seq)
1001{
1002 struct xfrm_dump_info info;
1003 struct sk_buff *skb;
1004
1005 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1006 if (!skb)
1007 return ERR_PTR(-ENOMEM);
1008
1009 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
1010 info.in_skb = in_skb;
1011 info.out_skb = skb;
1012 info.nlmsg_seq = seq;
1013 info.nlmsg_flags = 0;
1014 info.this_idx = info.start_idx = 0;
1015
1016 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1017 kfree_skb(skb);
1018 return NULL;
1019 }
1020
1021 return skb;
1022}
1023
1024static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1025{
1026 struct xfrm_policy *xp;
1027 struct xfrm_userpolicy_id *p;
1028 int err;
26b15dad 1029 struct km_event c;
1da177e4
LT
1030 int delete;
1031
1032 p = NLMSG_DATA(nlh);
1033 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1034
1035 err = verify_policy_dir(p->dir);
1036 if (err)
1037 return err;
1038
1039 if (p->index)
1040 xp = xfrm_policy_byid(p->dir, p->index, delete);
df71837d
TJ
1041 else {
1042 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1043 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1044 struct xfrm_policy tmp;
1045
1046 err = verify_sec_ctx_len(rtattrs);
1047 if (err)
1048 return err;
1049
1050 memset(&tmp, 0, sizeof(struct xfrm_policy));
1051 if (rt) {
1052 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1053
1054 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1055 return err;
1056 }
1057 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, delete);
1058 security_xfrm_policy_free(&tmp);
1059 }
1da177e4
LT
1060 if (xp == NULL)
1061 return -ENOENT;
1062
1063 if (!delete) {
1064 struct sk_buff *resp_skb;
1065
1066 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1067 if (IS_ERR(resp_skb)) {
1068 err = PTR_ERR(resp_skb);
1069 } else {
1070 err = netlink_unicast(xfrm_nl, resp_skb,
1071 NETLINK_CB(skb).pid,
1072 MSG_DONTWAIT);
1073 }
26b15dad 1074 } else {
6f68dc37 1075 if ((err = security_xfrm_policy_delete(xp)) != 0)
c8c05a8e 1076 goto out;
e7443892 1077 c.data.byid = p->index;
f60f6b8f 1078 c.event = nlh->nlmsg_type;
26b15dad
JHS
1079 c.seq = nlh->nlmsg_seq;
1080 c.pid = nlh->nlmsg_pid;
1081 km_policy_notify(xp, p->dir, &c);
1da177e4
LT
1082 }
1083
1084 xfrm_pol_put(xp);
1085
c8c05a8e 1086out:
1da177e4
LT
1087 return err;
1088}
1089
1090static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1091{
26b15dad 1092 struct km_event c;
1da177e4
LT
1093 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1094
1095 xfrm_state_flush(p->proto);
bf08867f 1096 c.data.proto = p->proto;
f60f6b8f 1097 c.event = nlh->nlmsg_type;
26b15dad
JHS
1098 c.seq = nlh->nlmsg_seq;
1099 c.pid = nlh->nlmsg_pid;
1100 km_state_notify(NULL, &c);
1101
1da177e4
LT
1102 return 0;
1103}
1104
d51d081d
JHS
1105
1106static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1107{
1108 struct xfrm_aevent_id *id;
1109 struct nlmsghdr *nlh;
1110 struct xfrm_lifetime_cur ltime;
1111 unsigned char *b = skb->tail;
1112
1113 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1114 id = NLMSG_DATA(nlh);
1115 nlh->nlmsg_flags = 0;
1116
1117 id->sa_id.daddr = x->id.daddr;
1118 id->sa_id.spi = x->id.spi;
1119 id->sa_id.family = x->props.family;
1120 id->sa_id.proto = x->id.proto;
1121 id->flags = c->data.aevent;
1122
1123 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1124
1125 ltime.bytes = x->curlft.bytes;
1126 ltime.packets = x->curlft.packets;
1127 ltime.add_time = x->curlft.add_time;
1128 ltime.use_time = x->curlft.use_time;
1129
1130 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
1131
1132 if (id->flags&XFRM_AE_RTHR) {
1133 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1134 }
1135
1136 if (id->flags&XFRM_AE_ETHR) {
1137 u32 etimer = x->replay_maxage*10/HZ;
1138 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1139 }
1140
1141 nlh->nlmsg_len = skb->tail - b;
1142 return skb->len;
1143
1144rtattr_failure:
1145nlmsg_failure:
1146 skb_trim(skb, b - skb->data);
1147 return -1;
1148}
1149
1150static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1151{
1152 struct xfrm_state *x;
1153 struct sk_buff *r_skb;
1154 int err;
1155 struct km_event c;
1156 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1157 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1158 struct xfrm_usersa_id *id = &p->sa_id;
1159
1160 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1161 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1162
1163 if (p->flags&XFRM_AE_RTHR)
1164 len+=RTA_SPACE(sizeof(u32));
1165
1166 if (p->flags&XFRM_AE_ETHR)
1167 len+=RTA_SPACE(sizeof(u32));
1168
1169 r_skb = alloc_skb(len, GFP_ATOMIC);
1170 if (r_skb == NULL)
1171 return -ENOMEM;
1172
1173 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1174 if (x == NULL) {
1175 kfree(r_skb);
1176 return -ESRCH;
1177 }
1178
1179 /*
1180 * XXX: is this lock really needed - none of the other
1181 * gets lock (the concern is things getting updated
1182 * while we are still reading) - jhs
1183 */
1184 spin_lock_bh(&x->lock);
1185 c.data.aevent = p->flags;
1186 c.seq = nlh->nlmsg_seq;
1187 c.pid = nlh->nlmsg_pid;
1188
1189 if (build_aevent(r_skb, x, &c) < 0)
1190 BUG();
1191 err = netlink_unicast(xfrm_nl, r_skb,
1192 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1193 spin_unlock_bh(&x->lock);
1194 xfrm_state_put(x);
1195 return err;
1196}
1197
1198static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1199{
1200 struct xfrm_state *x;
1201 struct km_event c;
1202 int err = - EINVAL;
1203 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1204 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1205 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1206
1207 if (!lt && !rp)
1208 return err;
1209
1210 /* pedantic mode - thou shalt sayeth replaceth */
1211 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1212 return err;
1213
1214 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1215 if (x == NULL)
1216 return -ESRCH;
1217
1218 if (x->km.state != XFRM_STATE_VALID)
1219 goto out;
1220
1221 spin_lock_bh(&x->lock);
1222 err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
1223 spin_unlock_bh(&x->lock);
1224 if (err < 0)
1225 goto out;
1226
1227 c.event = nlh->nlmsg_type;
1228 c.seq = nlh->nlmsg_seq;
1229 c.pid = nlh->nlmsg_pid;
1230 c.data.aevent = XFRM_AE_CU;
1231 km_state_notify(x, &c);
1232 err = 0;
1233out:
1234 xfrm_state_put(x);
1235 return err;
1236}
1237
1da177e4
LT
1238static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1239{
6c5c8ca7 1240struct km_event c;
26b15dad 1241
1da177e4 1242 xfrm_policy_flush();
f60f6b8f 1243 c.event = nlh->nlmsg_type;
26b15dad
JHS
1244 c.seq = nlh->nlmsg_seq;
1245 c.pid = nlh->nlmsg_pid;
1246 km_policy_notify(NULL, 0, &c);
1da177e4
LT
1247 return 0;
1248}
1249
6c5c8ca7
JHS
1250static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1251{
1252 struct xfrm_policy *xp;
1253 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1254 struct xfrm_userpolicy_info *p = &up->pol;
1255 int err = -ENOENT;
1256
1257 if (p->index)
1258 xp = xfrm_policy_byid(p->dir, p->index, 0);
1259 else {
1260 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1261 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1262 struct xfrm_policy tmp;
1263
1264 err = verify_sec_ctx_len(rtattrs);
1265 if (err)
1266 return err;
1267
1268 memset(&tmp, 0, sizeof(struct xfrm_policy));
1269 if (rt) {
1270 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1271
1272 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1273 return err;
1274 }
1275 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, 0);
1276 security_xfrm_policy_free(&tmp);
1277 }
1278
1279 if (xp == NULL)
1280 return err;
1281 read_lock(&xp->lock);
1282 if (xp->dead) {
1283 read_unlock(&xp->lock);
1284 goto out;
1285 }
1286
1287 read_unlock(&xp->lock);
1288 err = 0;
1289 if (up->hard) {
1290 xfrm_policy_delete(xp, p->dir);
1291 } else {
1292 // reset the timers here?
1293 printk("Dont know what to do with soft policy expire\n");
1294 }
1295 km_policy_expired(xp, p->dir, up->hard, current->pid);
1296
1297out:
1298 xfrm_pol_put(xp);
1299 return err;
1300}
1301
53bc6b4d
JHS
1302static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1303{
1304 struct xfrm_state *x;
1305 int err;
1306 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1307 struct xfrm_usersa_info *p = &ue->state;
1308
1309 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1310 err = -ENOENT;
1311
1312 if (x == NULL)
1313 return err;
1314
1315 err = -EINVAL;
1316
1317 spin_lock_bh(&x->lock);
1318 if (x->km.state != XFRM_STATE_VALID)
1319 goto out;
1320 km_state_expired(x, ue->hard, current->pid);
1321
1322 if (ue->hard)
1323 __xfrm_state_delete(x);
1324out:
1325 spin_unlock_bh(&x->lock);
1326 xfrm_state_put(x);
1327 return err;
1328}
1329
980ebd25
JHS
1330static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1331{
1332 struct xfrm_policy *xp;
1333 struct xfrm_user_tmpl *ut;
1334 int i;
1335 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1336
1337 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1338 struct xfrm_state *x = xfrm_state_alloc();
1339 int err = -ENOMEM;
1340
1341 if (!x)
1342 return err;
1343
1344 err = verify_newpolicy_info(&ua->policy);
1345 if (err) {
1346 printk("BAD policy passed\n");
1347 kfree(x);
1348 return err;
1349 }
1350
1351 /* build an XP */
1352 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err); if (!xp) {
1353 kfree(x);
1354 return err;
1355 }
1356
1357 memcpy(&x->id, &ua->id, sizeof(ua->id));
1358 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1359 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1360
1361 ut = RTA_DATA(rt);
1362 /* extract the templates and for each call km_key */
1363 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1364 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1365 memcpy(&x->id, &t->id, sizeof(x->id));
1366 x->props.mode = t->mode;
1367 x->props.reqid = t->reqid;
1368 x->props.family = ut->family;
1369 t->aalgos = ua->aalgos;
1370 t->ealgos = ua->ealgos;
1371 t->calgos = ua->calgos;
1372 err = km_query(x, t, xp);
1373
1374 }
1375
1376 kfree(x);
1377 kfree(xp);
1378
1379 return 0;
1380}
1381
d51d081d 1382
492b558b
TG
1383#define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1384
1385static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1386 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1387 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1388 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1389 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1390 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1391 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1392 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
980ebd25 1393 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
53bc6b4d 1394 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
492b558b
TG
1395 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1396 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
6c5c8ca7 1397 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
492b558b
TG
1398 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1399 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
d51d081d
JHS
1400 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1401 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1da177e4
LT
1402};
1403
492b558b
TG
1404#undef XMSGSIZE
1405
1da177e4
LT
1406static struct xfrm_link {
1407 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1408 int (*dump)(struct sk_buff *, struct netlink_callback *);
492b558b
TG
1409} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1410 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1411 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1412 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1413 .dump = xfrm_dump_sa },
1414 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1415 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1416 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1417 .dump = xfrm_dump_policy },
1418 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
980ebd25 1419 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
53bc6b4d 1420 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
492b558b
TG
1421 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1422 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
6c5c8ca7 1423 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
492b558b
TG
1424 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1425 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
d51d081d
JHS
1426 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1427 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1da177e4
LT
1428};
1429
1da177e4
LT
1430static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1431{
1432 struct rtattr *xfrma[XFRMA_MAX];
1433 struct xfrm_link *link;
1434 int type, min_len;
1435
1436 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1437 return 0;
1438
1439 type = nlh->nlmsg_type;
1440
1441 /* A control message: ignore them */
1442 if (type < XFRM_MSG_BASE)
1443 return 0;
1444
1445 /* Unknown message: reply with EINVAL */
1446 if (type > XFRM_MSG_MAX)
1447 goto err_einval;
1448
1449 type -= XFRM_MSG_BASE;
1450 link = &xfrm_dispatch[type];
1451
1452 /* All operations require privileges, even GET */
c7bdb545 1453 if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
1da177e4
LT
1454 *errp = -EPERM;
1455 return -1;
1456 }
1457
492b558b
TG
1458 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1459 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1460 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1da177e4
LT
1461 if (link->dump == NULL)
1462 goto err_einval;
1463
1464 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
a8f74b22 1465 link->dump, NULL)) != 0) {
1da177e4
LT
1466 return -1;
1467 }
88fc2c84
TG
1468
1469 netlink_queue_skip(nlh, skb);
1da177e4
LT
1470 return -1;
1471 }
1472
1473 memset(xfrma, 0, sizeof(xfrma));
1474
1475 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1476 goto err_einval;
1477
1478 if (nlh->nlmsg_len > min_len) {
1479 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1480 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1481
1482 while (RTA_OK(attr, attrlen)) {
1483 unsigned short flavor = attr->rta_type;
1484 if (flavor) {
1485 if (flavor > XFRMA_MAX)
1486 goto err_einval;
1487 xfrma[flavor - 1] = attr;
1488 }
1489 attr = RTA_NEXT(attr, attrlen);
1490 }
1491 }
1492
1493 if (link->doit == NULL)
1494 goto err_einval;
1495 *errp = link->doit(skb, nlh, (void **) &xfrma);
1496
1497 return *errp;
1498
1499err_einval:
1500 *errp = -EINVAL;
1501 return -1;
1502}
1503
1da177e4
LT
1504static void xfrm_netlink_rcv(struct sock *sk, int len)
1505{
88fc2c84 1506 unsigned int qlen = 0;
2a0a6ebe 1507
1da177e4 1508 do {
4a3e2f71 1509 mutex_lock(&xfrm_cfg_mutex);
88fc2c84 1510 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
4a3e2f71 1511 mutex_unlock(&xfrm_cfg_mutex);
1da177e4 1512
2a0a6ebe 1513 } while (qlen);
1da177e4
LT
1514}
1515
d51d081d 1516static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1da177e4
LT
1517{
1518 struct xfrm_user_expire *ue;
1519 struct nlmsghdr *nlh;
1520 unsigned char *b = skb->tail;
1521
d51d081d 1522 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
1da177e4
LT
1523 sizeof(*ue));
1524 ue = NLMSG_DATA(nlh);
1525 nlh->nlmsg_flags = 0;
1526
1527 copy_to_user_state(x, &ue->state);
d51d081d 1528 ue->hard = (c->data.hard != 0) ? 1 : 0;
1da177e4
LT
1529
1530 nlh->nlmsg_len = skb->tail - b;
1531 return skb->len;
1532
1533nlmsg_failure:
1534 skb_trim(skb, b - skb->data);
1535 return -1;
1536}
1537
26b15dad 1538static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1da177e4
LT
1539{
1540 struct sk_buff *skb;
ee57eef9 1541 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
1da177e4 1542
ee57eef9 1543 skb = alloc_skb(len, GFP_ATOMIC);
1da177e4
LT
1544 if (skb == NULL)
1545 return -ENOMEM;
1546
d51d081d 1547 if (build_expire(skb, x, c) < 0)
1da177e4
LT
1548 BUG();
1549
ac6d439d
PM
1550 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1551 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1da177e4
LT
1552}
1553
d51d081d
JHS
1554static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1555{
1556 struct sk_buff *skb;
1557 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1558
1559 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1560 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1561 skb = alloc_skb(len, GFP_ATOMIC);
1562 if (skb == NULL)
1563 return -ENOMEM;
1564
1565 if (build_aevent(skb, x, c) < 0)
1566 BUG();
1567
1568 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1569 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1570}
1571
26b15dad
JHS
1572static int xfrm_notify_sa_flush(struct km_event *c)
1573{
1574 struct xfrm_usersa_flush *p;
1575 struct nlmsghdr *nlh;
1576 struct sk_buff *skb;
1577 unsigned char *b;
1578 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1579
1580 skb = alloc_skb(len, GFP_ATOMIC);
1581 if (skb == NULL)
1582 return -ENOMEM;
1583 b = skb->tail;
1584
1585 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1586 XFRM_MSG_FLUSHSA, sizeof(*p));
1587 nlh->nlmsg_flags = 0;
1588
1589 p = NLMSG_DATA(nlh);
bf08867f 1590 p->proto = c->data.proto;
26b15dad
JHS
1591
1592 nlh->nlmsg_len = skb->tail - b;
1593
ac6d439d
PM
1594 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1595 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
26b15dad
JHS
1596
1597nlmsg_failure:
1598 kfree_skb(skb);
1599 return -1;
1600}
1601
1602static int inline xfrm_sa_len(struct xfrm_state *x)
1603{
0603eac0 1604 int l = 0;
26b15dad
JHS
1605 if (x->aalg)
1606 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1607 if (x->ealg)
1608 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1609 if (x->calg)
1610 l += RTA_SPACE(sizeof(*x->calg));
1611 if (x->encap)
1612 l += RTA_SPACE(sizeof(*x->encap));
1613
1614 return l;
1615}
1616
1617static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1618{
1619 struct xfrm_usersa_info *p;
0603eac0 1620 struct xfrm_usersa_id *id;
26b15dad
JHS
1621 struct nlmsghdr *nlh;
1622 struct sk_buff *skb;
26b15dad
JHS
1623 unsigned char *b;
1624 int len = xfrm_sa_len(x);
0603eac0
HX
1625 int headlen;
1626
1627 headlen = sizeof(*p);
1628 if (c->event == XFRM_MSG_DELSA) {
1629 len += RTA_SPACE(headlen);
1630 headlen = sizeof(*id);
1631 }
1632 len += NLMSG_SPACE(headlen);
26b15dad
JHS
1633
1634 skb = alloc_skb(len, GFP_ATOMIC);
1635 if (skb == NULL)
1636 return -ENOMEM;
1637 b = skb->tail;
1638
0603eac0 1639 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
26b15dad
JHS
1640 nlh->nlmsg_flags = 0;
1641
1642 p = NLMSG_DATA(nlh);
0603eac0
HX
1643 if (c->event == XFRM_MSG_DELSA) {
1644 id = NLMSG_DATA(nlh);
1645 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1646 id->spi = x->id.spi;
1647 id->family = x->props.family;
1648 id->proto = x->id.proto;
1649
1650 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1651 }
1652
26b15dad
JHS
1653 copy_to_user_state(x, p);
1654
1655 if (x->aalg)
1656 RTA_PUT(skb, XFRMA_ALG_AUTH,
1657 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1658 if (x->ealg)
1659 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1660 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1661 if (x->calg)
1662 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1663
1664 if (x->encap)
1665 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1666
1667 nlh->nlmsg_len = skb->tail - b;
1668
ac6d439d
PM
1669 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1670 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
26b15dad
JHS
1671
1672nlmsg_failure:
1673rtattr_failure:
1674 kfree_skb(skb);
1675 return -1;
1676}
1677
1678static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1679{
1680
1681 switch (c->event) {
f60f6b8f 1682 case XFRM_MSG_EXPIRE:
26b15dad 1683 return xfrm_exp_state_notify(x, c);
d51d081d
JHS
1684 case XFRM_MSG_NEWAE:
1685 return xfrm_aevent_state_notify(x, c);
f60f6b8f
HX
1686 case XFRM_MSG_DELSA:
1687 case XFRM_MSG_UPDSA:
1688 case XFRM_MSG_NEWSA:
26b15dad 1689 return xfrm_notify_sa(x, c);
f60f6b8f 1690 case XFRM_MSG_FLUSHSA:
26b15dad
JHS
1691 return xfrm_notify_sa_flush(c);
1692 default:
1693 printk("xfrm_user: Unknown SA event %d\n", c->event);
1694 break;
1695 }
1696
1697 return 0;
1698
1699}
1700
1da177e4
LT
1701static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1702 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1703 int dir)
1704{
1705 struct xfrm_user_acquire *ua;
1706 struct nlmsghdr *nlh;
1707 unsigned char *b = skb->tail;
1708 __u32 seq = xfrm_get_acqseq();
1709
1710 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1711 sizeof(*ua));
1712 ua = NLMSG_DATA(nlh);
1713 nlh->nlmsg_flags = 0;
1714
1715 memcpy(&ua->id, &x->id, sizeof(ua->id));
1716 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1717 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1718 copy_to_user_policy(xp, &ua->policy, dir);
1719 ua->aalgos = xt->aalgos;
1720 ua->ealgos = xt->ealgos;
1721 ua->calgos = xt->calgos;
1722 ua->seq = x->km.seq = seq;
1723
1724 if (copy_to_user_tmpl(xp, skb) < 0)
1725 goto nlmsg_failure;
0d681623 1726 if (copy_to_user_state_sec_ctx(x, skb))
df71837d 1727 goto nlmsg_failure;
1da177e4
LT
1728
1729 nlh->nlmsg_len = skb->tail - b;
1730 return skb->len;
1731
1732nlmsg_failure:
1733 skb_trim(skb, b - skb->data);
1734 return -1;
1735}
1736
1737static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1738 struct xfrm_policy *xp, int dir)
1739{
1740 struct sk_buff *skb;
1741 size_t len;
1742
1743 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1744 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
df71837d 1745 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1da177e4
LT
1746 skb = alloc_skb(len, GFP_ATOMIC);
1747 if (skb == NULL)
1748 return -ENOMEM;
1749
1750 if (build_acquire(skb, x, xt, xp, dir) < 0)
1751 BUG();
1752
ac6d439d
PM
1753 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
1754 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
1da177e4
LT
1755}
1756
1757/* User gives us xfrm_user_policy_info followed by an array of 0
1758 * or more templates.
1759 */
cb969f07 1760static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
1da177e4
LT
1761 u8 *data, int len, int *dir)
1762{
1763 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1764 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1765 struct xfrm_policy *xp;
1766 int nr;
1767
cb969f07 1768 switch (sk->sk_family) {
1da177e4
LT
1769 case AF_INET:
1770 if (opt != IP_XFRM_POLICY) {
1771 *dir = -EOPNOTSUPP;
1772 return NULL;
1773 }
1774 break;
1775#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1776 case AF_INET6:
1777 if (opt != IPV6_XFRM_POLICY) {
1778 *dir = -EOPNOTSUPP;
1779 return NULL;
1780 }
1781 break;
1782#endif
1783 default:
1784 *dir = -EINVAL;
1785 return NULL;
1786 }
1787
1788 *dir = -EINVAL;
1789
1790 if (len < sizeof(*p) ||
1791 verify_newpolicy_info(p))
1792 return NULL;
1793
1794 nr = ((len - sizeof(*p)) / sizeof(*ut));
1795 if (nr > XFRM_MAX_DEPTH)
1796 return NULL;
1797
a4f1bac6
HX
1798 if (p->dir > XFRM_POLICY_OUT)
1799 return NULL;
1800
1da177e4
LT
1801 xp = xfrm_policy_alloc(GFP_KERNEL);
1802 if (xp == NULL) {
1803 *dir = -ENOBUFS;
1804 return NULL;
1805 }
1806
1807 copy_from_user_policy(xp, p);
1808 copy_templates(xp, ut, nr);
1809
cb969f07
VY
1810 if (!xp->security) {
1811 int err = security_xfrm_sock_policy_alloc(xp, sk);
1812 if (err) {
1813 kfree(xp);
1814 *dir = err;
1815 return NULL;
1816 }
1817 }
1818
1da177e4
LT
1819 *dir = p->dir;
1820
1821 return xp;
1822}
1823
1824static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
d51d081d 1825 int dir, struct km_event *c)
1da177e4
LT
1826{
1827 struct xfrm_user_polexpire *upe;
1828 struct nlmsghdr *nlh;
d51d081d 1829 int hard = c->data.hard;
1da177e4
LT
1830 unsigned char *b = skb->tail;
1831
d51d081d 1832 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
1da177e4
LT
1833 upe = NLMSG_DATA(nlh);
1834 nlh->nlmsg_flags = 0;
1835
1836 copy_to_user_policy(xp, &upe->pol, dir);
1837 if (copy_to_user_tmpl(xp, skb) < 0)
1838 goto nlmsg_failure;
df71837d
TJ
1839 if (copy_to_user_sec_ctx(xp, skb))
1840 goto nlmsg_failure;
1da177e4
LT
1841 upe->hard = !!hard;
1842
1843 nlh->nlmsg_len = skb->tail - b;
1844 return skb->len;
1845
1846nlmsg_failure:
1847 skb_trim(skb, b - skb->data);
1848 return -1;
1849}
1850
26b15dad 1851static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1da177e4
LT
1852{
1853 struct sk_buff *skb;
1854 size_t len;
1855
1856 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1857 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
df71837d 1858 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1da177e4
LT
1859 skb = alloc_skb(len, GFP_ATOMIC);
1860 if (skb == NULL)
1861 return -ENOMEM;
1862
d51d081d 1863 if (build_polexpire(skb, xp, dir, c) < 0)
1da177e4
LT
1864 BUG();
1865
ac6d439d
PM
1866 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1867 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1da177e4
LT
1868}
1869
26b15dad
JHS
1870static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
1871{
1872 struct xfrm_userpolicy_info *p;
0603eac0 1873 struct xfrm_userpolicy_id *id;
26b15dad
JHS
1874 struct nlmsghdr *nlh;
1875 struct sk_buff *skb;
26b15dad
JHS
1876 unsigned char *b;
1877 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
0603eac0
HX
1878 int headlen;
1879
1880 headlen = sizeof(*p);
1881 if (c->event == XFRM_MSG_DELPOLICY) {
1882 len += RTA_SPACE(headlen);
1883 headlen = sizeof(*id);
1884 }
1885 len += NLMSG_SPACE(headlen);
26b15dad
JHS
1886
1887 skb = alloc_skb(len, GFP_ATOMIC);
1888 if (skb == NULL)
1889 return -ENOMEM;
1890 b = skb->tail;
1891
0603eac0 1892 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
26b15dad
JHS
1893
1894 p = NLMSG_DATA(nlh);
0603eac0
HX
1895 if (c->event == XFRM_MSG_DELPOLICY) {
1896 id = NLMSG_DATA(nlh);
1897 memset(id, 0, sizeof(*id));
1898 id->dir = dir;
1899 if (c->data.byid)
1900 id->index = xp->index;
1901 else
1902 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
1903
1904 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
1905 }
26b15dad
JHS
1906
1907 nlh->nlmsg_flags = 0;
1908
1909 copy_to_user_policy(xp, p, dir);
1910 if (copy_to_user_tmpl(xp, skb) < 0)
1911 goto nlmsg_failure;
1912
1913 nlh->nlmsg_len = skb->tail - b;
1914
ac6d439d
PM
1915 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1916 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
26b15dad
JHS
1917
1918nlmsg_failure:
0603eac0 1919rtattr_failure:
26b15dad
JHS
1920 kfree_skb(skb);
1921 return -1;
1922}
1923
1924static int xfrm_notify_policy_flush(struct km_event *c)
1925{
1926 struct nlmsghdr *nlh;
1927 struct sk_buff *skb;
1928 unsigned char *b;
1929 int len = NLMSG_LENGTH(0);
1930
1931 skb = alloc_skb(len, GFP_ATOMIC);
1932 if (skb == NULL)
1933 return -ENOMEM;
1934 b = skb->tail;
1935
1936
1937 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
1938
1939 nlh->nlmsg_len = skb->tail - b;
1940
ac6d439d
PM
1941 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1942 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
26b15dad
JHS
1943
1944nlmsg_failure:
1945 kfree_skb(skb);
1946 return -1;
1947}
1948
1949static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1950{
1951
1952 switch (c->event) {
f60f6b8f
HX
1953 case XFRM_MSG_NEWPOLICY:
1954 case XFRM_MSG_UPDPOLICY:
1955 case XFRM_MSG_DELPOLICY:
26b15dad 1956 return xfrm_notify_policy(xp, dir, c);
f60f6b8f 1957 case XFRM_MSG_FLUSHPOLICY:
26b15dad 1958 return xfrm_notify_policy_flush(c);
f60f6b8f 1959 case XFRM_MSG_POLEXPIRE:
26b15dad
JHS
1960 return xfrm_exp_policy_notify(xp, dir, c);
1961 default:
1962 printk("xfrm_user: Unknown Policy event %d\n", c->event);
1963 }
1964
1965 return 0;
1966
1967}
1968
1da177e4
LT
1969static struct xfrm_mgr netlink_mgr = {
1970 .id = "netlink",
1971 .notify = xfrm_send_state_notify,
1972 .acquire = xfrm_send_acquire,
1973 .compile_policy = xfrm_compile_policy,
1974 .notify_policy = xfrm_send_policy_notify,
1975};
1976
1977static int __init xfrm_user_init(void)
1978{
be33690d
PM
1979 struct sock *nlsk;
1980
1da177e4
LT
1981 printk(KERN_INFO "Initializing IPsec netlink socket\n");
1982
be33690d
PM
1983 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
1984 xfrm_netlink_rcv, THIS_MODULE);
1985 if (nlsk == NULL)
1da177e4 1986 return -ENOMEM;
be33690d 1987 rcu_assign_pointer(xfrm_nl, nlsk);
1da177e4
LT
1988
1989 xfrm_register_km(&netlink_mgr);
1990
1991 return 0;
1992}
1993
1994static void __exit xfrm_user_exit(void)
1995{
be33690d
PM
1996 struct sock *nlsk = xfrm_nl;
1997
1da177e4 1998 xfrm_unregister_km(&netlink_mgr);
be33690d
PM
1999 rcu_assign_pointer(xfrm_nl, NULL);
2000 synchronize_rcu();
2001 sock_release(nlsk->sk_socket);
1da177e4
LT
2002}
2003
2004module_init(xfrm_user_init);
2005module_exit(xfrm_user_exit);
2006MODULE_LICENSE("GPL");
4fdb3bb7 2007MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
f8cd5488 2008