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