blob: 07b71680cde0d4c574c0c133fb50beeff258de80 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/types.h>
16#include <linux/slab.h>
17#include <linux/socket.h>
18#include <linux/string.h>
19#include <linux/net.h>
20#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/rtnetlink.h>
22#include <linux/pfkeyv2.h>
23#include <linux/ipsec.h>
24#include <linux/init.h>
25#include <linux/security.h>
26#include <net/sock.h>
27#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010028#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
32{
33 struct rtattr *rt = xfrma[type - 1];
34 struct xfrm_algo *algp;
Herbert Xu31c26852005-05-19 12:39:49 -070035 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37 if (!rt)
38 return 0;
39
Herbert Xu31c26852005-05-19 12:39:49 -070040 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
41 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 return -EINVAL;
43
44 algp = RTA_DATA(rt);
Herbert Xu31c26852005-05-19 12:39:49 -070045
46 len -= (algp->alg_key_len + 7U) / 8;
47 if (len < 0)
48 return -EINVAL;
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 switch (type) {
51 case XFRMA_ALG_AUTH:
52 if (!algp->alg_key_len &&
53 strcmp(algp->alg_name, "digest_null") != 0)
54 return -EINVAL;
55 break;
56
57 case XFRMA_ALG_CRYPT:
58 if (!algp->alg_key_len &&
59 strcmp(algp->alg_name, "cipher_null") != 0)
60 return -EINVAL;
61 break;
62
63 case XFRMA_ALG_COMP:
64 /* Zero length keys are legal. */
65 break;
66
67 default:
68 return -EINVAL;
69 };
70
71 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
72 return 0;
73}
74
75static int verify_encap_tmpl(struct rtattr **xfrma)
76{
77 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
78 struct xfrm_encap_tmpl *encap;
79
80 if (!rt)
81 return 0;
82
83 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
84 return -EINVAL;
85
86 return 0;
87}
88
Trent Jaegerdf718372005-12-13 23:12:27 -080089
90static inline int verify_sec_ctx_len(struct rtattr **xfrma)
91{
92 struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
93 struct xfrm_user_sec_ctx *uctx;
94 int len = 0;
95
96 if (!rt)
97 return 0;
98
99 if (rt->rta_len < sizeof(*uctx))
100 return -EINVAL;
101
102 uctx = RTA_DATA(rt);
103
104 if (uctx->ctx_len > PAGE_SIZE)
105 return -EINVAL;
106
107 len += sizeof(struct xfrm_user_sec_ctx);
108 len += uctx->ctx_len;
109
110 if (uctx->len != len)
111 return -EINVAL;
112
113 return 0;
114}
115
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static int verify_newsa_info(struct xfrm_usersa_info *p,
118 struct rtattr **xfrma)
119{
120 int err;
121
122 err = -EINVAL;
123 switch (p->family) {
124 case AF_INET:
125 break;
126
127 case AF_INET6:
128#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
129 break;
130#else
131 err = -EAFNOSUPPORT;
132 goto out;
133#endif
134
135 default:
136 goto out;
137 };
138
139 err = -EINVAL;
140 switch (p->id.proto) {
141 case IPPROTO_AH:
142 if (!xfrma[XFRMA_ALG_AUTH-1] ||
143 xfrma[XFRMA_ALG_CRYPT-1] ||
144 xfrma[XFRMA_ALG_COMP-1])
145 goto out;
146 break;
147
148 case IPPROTO_ESP:
149 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
150 !xfrma[XFRMA_ALG_CRYPT-1]) ||
151 xfrma[XFRMA_ALG_COMP-1])
152 goto out;
153 break;
154
155 case IPPROTO_COMP:
156 if (!xfrma[XFRMA_ALG_COMP-1] ||
157 xfrma[XFRMA_ALG_AUTH-1] ||
158 xfrma[XFRMA_ALG_CRYPT-1])
159 goto out;
160 break;
161
162 default:
163 goto out;
164 };
165
166 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
167 goto out;
168 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
169 goto out;
170 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
171 goto out;
172 if ((err = verify_encap_tmpl(xfrma)))
173 goto out;
Trent Jaegerdf718372005-12-13 23:12:27 -0800174 if ((err = verify_sec_ctx_len(xfrma)))
175 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 err = -EINVAL;
178 switch (p->mode) {
179 case 0:
180 case 1:
181 break;
182
183 default:
184 goto out;
185 };
186
187 err = 0;
188
189out:
190 return err;
191}
192
193static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
194 struct xfrm_algo_desc *(*get_byname)(char *, int),
195 struct rtattr *u_arg)
196{
197 struct rtattr *rta = u_arg;
198 struct xfrm_algo *p, *ualg;
199 struct xfrm_algo_desc *algo;
Herbert Xub9e9dea2005-05-19 12:39:04 -0700200 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 if (!rta)
203 return 0;
204
205 ualg = RTA_DATA(rta);
206
207 algo = get_byname(ualg->alg_name, 1);
208 if (!algo)
209 return -ENOSYS;
210 *props = algo->desc.sadb_alg_id;
211
Herbert Xub9e9dea2005-05-19 12:39:04 -0700212 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
213 p = kmalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (!p)
215 return -ENOMEM;
216
Herbert Xub9e9dea2005-05-19 12:39:04 -0700217 memcpy(p, ualg, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 *algpp = p;
219 return 0;
220}
221
222static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
223{
224 struct rtattr *rta = u_arg;
225 struct xfrm_encap_tmpl *p, *uencap;
226
227 if (!rta)
228 return 0;
229
230 uencap = RTA_DATA(rta);
231 p = kmalloc(sizeof(*p), GFP_KERNEL);
232 if (!p)
233 return -ENOMEM;
234
235 memcpy(p, uencap, sizeof(*p));
236 *encapp = p;
237 return 0;
238}
239
Trent Jaegerdf718372005-12-13 23:12:27 -0800240
241static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
242{
243 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
244 int len = 0;
245
246 if (xfrm_ctx) {
247 len += sizeof(struct xfrm_user_sec_ctx);
248 len += xfrm_ctx->ctx_len;
249 }
250 return len;
251}
252
253static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
254{
255 struct xfrm_user_sec_ctx *uctx;
256
257 if (!u_arg)
258 return 0;
259
260 uctx = RTA_DATA(u_arg);
261 return security_xfrm_state_alloc(x, uctx);
262}
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
265{
266 memcpy(&x->id, &p->id, sizeof(x->id));
267 memcpy(&x->sel, &p->sel, sizeof(x->sel));
268 memcpy(&x->lft, &p->lft, sizeof(x->lft));
269 x->props.mode = p->mode;
270 x->props.replay_window = p->replay_window;
271 x->props.reqid = p->reqid;
272 x->props.family = p->family;
273 x->props.saddr = p->saddr;
274 x->props.flags = p->flags;
275}
276
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800277/*
278 * someday when pfkey also has support, we could have the code
279 * somehow made shareable and move it to xfrm_state.c - JHS
280 *
281*/
282static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
283{
284 int err = - EINVAL;
285 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
286 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
287 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
288 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
289
290 if (rp) {
291 struct xfrm_replay_state *replay;
292 if (RTA_PAYLOAD(rp) < sizeof(*replay))
293 goto error;
294 replay = RTA_DATA(rp);
295 memcpy(&x->replay, replay, sizeof(*replay));
296 memcpy(&x->preplay, replay, sizeof(*replay));
297 }
298
299 if (lt) {
300 struct xfrm_lifetime_cur *ltime;
301 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
302 goto error;
303 ltime = RTA_DATA(lt);
304 x->curlft.bytes = ltime->bytes;
305 x->curlft.packets = ltime->packets;
306 x->curlft.add_time = ltime->add_time;
307 x->curlft.use_time = ltime->use_time;
308 }
309
310 if (et) {
311 if (RTA_PAYLOAD(et) < sizeof(u32))
312 goto error;
313 x->replay_maxage = *(u32*)RTA_DATA(et);
314 }
315
316 if (rt) {
317 if (RTA_PAYLOAD(rt) < sizeof(u32))
318 goto error;
319 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
320 }
321
322 return 0;
323error:
324 return err;
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
328 struct rtattr **xfrma,
329 int *errp)
330{
331 struct xfrm_state *x = xfrm_state_alloc();
332 int err = -ENOMEM;
333
334 if (!x)
335 goto error_no_put;
336
337 copy_from_user_state(x, p);
338
339 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
340 xfrm_aalg_get_byname,
341 xfrma[XFRMA_ALG_AUTH-1])))
342 goto error;
343 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
344 xfrm_ealg_get_byname,
345 xfrma[XFRMA_ALG_CRYPT-1])))
346 goto error;
347 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
348 xfrm_calg_get_byname,
349 xfrma[XFRMA_ALG_COMP-1])))
350 goto error;
351 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
352 goto error;
353
Herbert Xu72cb6962005-06-20 13:18:08 -0700354 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (err)
356 goto error;
357
Trent Jaegerdf718372005-12-13 23:12:27 -0800358 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
359 goto error;
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800362 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
363 /* sysctl_xfrm_aevent_etime is in 100ms units */
364 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
365 x->preplay.bitmap = 0;
366 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
367 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
368
369 /* override default values from above */
370
371 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
372 if (err < 0)
373 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 return x;
376
377error:
378 x->km.state = XFRM_STATE_DEAD;
379 xfrm_state_put(x);
380error_no_put:
381 *errp = err;
382 return NULL;
383}
384
385static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
386{
387 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
388 struct xfrm_state *x;
389 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700390 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Trent Jaegerdf718372005-12-13 23:12:27 -0800392 err = verify_newsa_info(p, (struct rtattr **)xfrma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (err)
394 return err;
395
Trent Jaegerdf718372005-12-13 23:12:27 -0800396 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (!x)
398 return err;
399
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700400 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
402 err = xfrm_state_add(x);
403 else
404 err = xfrm_state_update(x);
405
406 if (err < 0) {
407 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800408 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700409 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700412 c.seq = nlh->nlmsg_seq;
413 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700414 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700415
416 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700417out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700418 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return err;
420}
421
422static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
423{
424 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700425 int err;
426 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
428
429 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
430 if (x == NULL)
431 return -ESRCH;
432
433 if (xfrm_state_kern(x)) {
434 xfrm_state_put(x);
435 return -EPERM;
436 }
437
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700438 err = xfrm_state_delete(x);
439 if (err < 0) {
440 xfrm_state_put(x);
441 return err;
442 }
443
444 c.seq = nlh->nlmsg_seq;
445 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700446 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700447 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 xfrm_state_put(x);
449
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700450 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
454{
455 memcpy(&p->id, &x->id, sizeof(p->id));
456 memcpy(&p->sel, &x->sel, sizeof(p->sel));
457 memcpy(&p->lft, &x->lft, sizeof(p->lft));
458 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
459 memcpy(&p->stats, &x->stats, sizeof(p->stats));
460 p->saddr = x->props.saddr;
461 p->mode = x->props.mode;
462 p->replay_window = x->props.replay_window;
463 p->reqid = x->props.reqid;
464 p->family = x->props.family;
465 p->flags = x->props.flags;
466 p->seq = x->km.seq;
467}
468
469struct xfrm_dump_info {
470 struct sk_buff *in_skb;
471 struct sk_buff *out_skb;
472 u32 nlmsg_seq;
473 u16 nlmsg_flags;
474 int start_idx;
475 int this_idx;
476};
477
478static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
479{
480 struct xfrm_dump_info *sp = ptr;
481 struct sk_buff *in_skb = sp->in_skb;
482 struct sk_buff *skb = sp->out_skb;
483 struct xfrm_usersa_info *p;
484 struct nlmsghdr *nlh;
485 unsigned char *b = skb->tail;
486
487 if (sp->this_idx < sp->start_idx)
488 goto out;
489
490 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
491 sp->nlmsg_seq,
492 XFRM_MSG_NEWSA, sizeof(*p));
493 nlh->nlmsg_flags = sp->nlmsg_flags;
494
495 p = NLMSG_DATA(nlh);
496 copy_to_user_state(x, p);
497
498 if (x->aalg)
499 RTA_PUT(skb, XFRMA_ALG_AUTH,
500 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
501 if (x->ealg)
502 RTA_PUT(skb, XFRMA_ALG_CRYPT,
503 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
504 if (x->calg)
505 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
506
507 if (x->encap)
508 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
509
Trent Jaegerdf718372005-12-13 23:12:27 -0800510 if (x->security) {
511 int ctx_size = sizeof(struct xfrm_sec_ctx) +
512 x->security->ctx_len;
513 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
514 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
515
516 uctx->exttype = XFRMA_SEC_CTX;
517 uctx->len = ctx_size;
518 uctx->ctx_doi = x->security->ctx_doi;
519 uctx->ctx_alg = x->security->ctx_alg;
520 uctx->ctx_len = x->security->ctx_len;
521 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 nlh->nlmsg_len = skb->tail - b;
524out:
525 sp->this_idx++;
526 return 0;
527
528nlmsg_failure:
529rtattr_failure:
530 skb_trim(skb, b - skb->data);
531 return -1;
532}
533
534static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
535{
536 struct xfrm_dump_info info;
537
538 info.in_skb = cb->skb;
539 info.out_skb = skb;
540 info.nlmsg_seq = cb->nlh->nlmsg_seq;
541 info.nlmsg_flags = NLM_F_MULTI;
542 info.this_idx = 0;
543 info.start_idx = cb->args[0];
544 (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
545 cb->args[0] = info.this_idx;
546
547 return skb->len;
548}
549
550static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
551 struct xfrm_state *x, u32 seq)
552{
553 struct xfrm_dump_info info;
554 struct sk_buff *skb;
555
556 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
557 if (!skb)
558 return ERR_PTR(-ENOMEM);
559
560 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
561 info.in_skb = in_skb;
562 info.out_skb = skb;
563 info.nlmsg_seq = seq;
564 info.nlmsg_flags = 0;
565 info.this_idx = info.start_idx = 0;
566
567 if (dump_one_state(x, 0, &info)) {
568 kfree_skb(skb);
569 return NULL;
570 }
571
572 return skb;
573}
574
575static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
576{
577 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
578 struct xfrm_state *x;
579 struct sk_buff *resp_skb;
580 int err;
581
582 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
583 err = -ESRCH;
584 if (x == NULL)
585 goto out_noput;
586
587 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
588 if (IS_ERR(resp_skb)) {
589 err = PTR_ERR(resp_skb);
590 } else {
591 err = netlink_unicast(xfrm_nl, resp_skb,
592 NETLINK_CB(skb).pid, MSG_DONTWAIT);
593 }
594 xfrm_state_put(x);
595out_noput:
596 return err;
597}
598
599static int verify_userspi_info(struct xfrm_userspi_info *p)
600{
601 switch (p->info.id.proto) {
602 case IPPROTO_AH:
603 case IPPROTO_ESP:
604 break;
605
606 case IPPROTO_COMP:
607 /* IPCOMP spi is 16-bits. */
608 if (p->max >= 0x10000)
609 return -EINVAL;
610 break;
611
612 default:
613 return -EINVAL;
614 };
615
616 if (p->min > p->max)
617 return -EINVAL;
618
619 return 0;
620}
621
622static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
623{
624 struct xfrm_state *x;
625 struct xfrm_userspi_info *p;
626 struct sk_buff *resp_skb;
627 xfrm_address_t *daddr;
628 int family;
629 int err;
630
631 p = NLMSG_DATA(nlh);
632 err = verify_userspi_info(p);
633 if (err)
634 goto out_noput;
635
636 family = p->info.family;
637 daddr = &p->info.id.daddr;
638
639 x = NULL;
640 if (p->info.seq) {
641 x = xfrm_find_acq_byseq(p->info.seq);
642 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
643 xfrm_state_put(x);
644 x = NULL;
645 }
646 }
647
648 if (!x)
649 x = xfrm_find_acq(p->info.mode, p->info.reqid,
650 p->info.id.proto, daddr,
651 &p->info.saddr, 1,
652 family);
653 err = -ENOENT;
654 if (x == NULL)
655 goto out_noput;
656
657 resp_skb = ERR_PTR(-ENOENT);
658
659 spin_lock_bh(&x->lock);
660 if (x->km.state != XFRM_STATE_DEAD) {
661 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
662 if (x->id.spi)
663 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
664 }
665 spin_unlock_bh(&x->lock);
666
667 if (IS_ERR(resp_skb)) {
668 err = PTR_ERR(resp_skb);
669 goto out;
670 }
671
672 err = netlink_unicast(xfrm_nl, resp_skb,
673 NETLINK_CB(skb).pid, MSG_DONTWAIT);
674
675out:
676 xfrm_state_put(x);
677out_noput:
678 return err;
679}
680
681static int verify_policy_dir(__u8 dir)
682{
683 switch (dir) {
684 case XFRM_POLICY_IN:
685 case XFRM_POLICY_OUT:
686 case XFRM_POLICY_FWD:
687 break;
688
689 default:
690 return -EINVAL;
691 };
692
693 return 0;
694}
695
696static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
697{
698 switch (p->share) {
699 case XFRM_SHARE_ANY:
700 case XFRM_SHARE_SESSION:
701 case XFRM_SHARE_USER:
702 case XFRM_SHARE_UNIQUE:
703 break;
704
705 default:
706 return -EINVAL;
707 };
708
709 switch (p->action) {
710 case XFRM_POLICY_ALLOW:
711 case XFRM_POLICY_BLOCK:
712 break;
713
714 default:
715 return -EINVAL;
716 };
717
718 switch (p->sel.family) {
719 case AF_INET:
720 break;
721
722 case AF_INET6:
723#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
724 break;
725#else
726 return -EAFNOSUPPORT;
727#endif
728
729 default:
730 return -EINVAL;
731 };
732
733 return verify_policy_dir(p->dir);
734}
735
Trent Jaegerdf718372005-12-13 23:12:27 -0800736static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
737{
738 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
739 struct xfrm_user_sec_ctx *uctx;
740
741 if (!rt)
742 return 0;
743
744 uctx = RTA_DATA(rt);
745 return security_xfrm_policy_alloc(pol, uctx);
746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
749 int nr)
750{
751 int i;
752
753 xp->xfrm_nr = nr;
754 for (i = 0; i < nr; i++, ut++) {
755 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
756
757 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
758 memcpy(&t->saddr, &ut->saddr,
759 sizeof(xfrm_address_t));
760 t->reqid = ut->reqid;
761 t->mode = ut->mode;
762 t->share = ut->share;
763 t->optional = ut->optional;
764 t->aalgos = ut->aalgos;
765 t->ealgos = ut->ealgos;
766 t->calgos = ut->calgos;
767 }
768}
769
770static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
771{
772 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
773 struct xfrm_user_tmpl *utmpl;
774 int nr;
775
776 if (!rt) {
777 pol->xfrm_nr = 0;
778 } else {
779 nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
780
781 if (nr > XFRM_MAX_DEPTH)
782 return -EINVAL;
783
784 copy_templates(pol, RTA_DATA(rt), nr);
785 }
786 return 0;
787}
788
789static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
790{
791 xp->priority = p->priority;
792 xp->index = p->index;
793 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
794 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
795 xp->action = p->action;
796 xp->flags = p->flags;
797 xp->family = p->sel.family;
798 /* XXX xp->share = p->share; */
799}
800
801static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
802{
803 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
804 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
805 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
806 p->priority = xp->priority;
807 p->index = xp->index;
808 p->sel.family = xp->family;
809 p->dir = dir;
810 p->action = xp->action;
811 p->flags = xp->flags;
812 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
813}
814
815static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
816{
817 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
818 int err;
819
820 if (!xp) {
821 *errp = -ENOMEM;
822 return NULL;
823 }
824
825 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -0800826
827 if (!(err = copy_from_user_tmpl(xp, xfrma)))
828 err = copy_from_user_sec_ctx(xp, xfrma);
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (err) {
831 *errp = err;
832 kfree(xp);
833 xp = NULL;
834 }
835
836 return xp;
837}
838
839static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
840{
841 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
842 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700843 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 int err;
845 int excl;
846
847 err = verify_newpolicy_info(p);
848 if (err)
849 return err;
Trent Jaegerdf718372005-12-13 23:12:27 -0800850 err = verify_sec_ctx_len((struct rtattr **)xfrma);
851 if (err)
852 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Trent Jaegerdf718372005-12-13 23:12:27 -0800854 xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (!xp)
856 return err;
857
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700858 /* shouldnt excl be based on nlh flags??
859 * Aha! this is anti-netlink really i.e more pfkey derived
860 * in netlink excl is a flag and you wouldnt need
861 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
863 err = xfrm_policy_insert(p->dir, xp, excl);
864 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -0800865 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 kfree(xp);
867 return err;
868 }
869
Herbert Xuf60f6b82005-06-18 22:44:37 -0700870 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700871 c.seq = nlh->nlmsg_seq;
872 c.pid = nlh->nlmsg_pid;
873 km_policy_notify(xp, p->dir, &c);
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 xfrm_pol_put(xp);
876
877 return 0;
878}
879
880static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
881{
882 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
883 int i;
884
885 if (xp->xfrm_nr == 0)
886 return 0;
887
888 for (i = 0; i < xp->xfrm_nr; i++) {
889 struct xfrm_user_tmpl *up = &vec[i];
890 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
891
892 memcpy(&up->id, &kp->id, sizeof(up->id));
893 up->family = xp->family;
894 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
895 up->reqid = kp->reqid;
896 up->mode = kp->mode;
897 up->share = kp->share;
898 up->optional = kp->optional;
899 up->aalgos = kp->aalgos;
900 up->ealgos = kp->ealgos;
901 up->calgos = kp->calgos;
902 }
903 RTA_PUT(skb, XFRMA_TMPL,
904 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
905 vec);
906
907 return 0;
908
909rtattr_failure:
910 return -1;
911}
912
Trent Jaegerdf718372005-12-13 23:12:27 -0800913static int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
914{
915 if (xp->security) {
916 int ctx_size = sizeof(struct xfrm_sec_ctx) +
917 xp->security->ctx_len;
918 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
919 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
920
921 uctx->exttype = XFRMA_SEC_CTX;
922 uctx->len = ctx_size;
923 uctx->ctx_doi = xp->security->ctx_doi;
924 uctx->ctx_alg = xp->security->ctx_alg;
925 uctx->ctx_len = xp->security->ctx_len;
926 memcpy(uctx + 1, xp->security->ctx_str, xp->security->ctx_len);
927 }
928 return 0;
929
930 rtattr_failure:
931 return -1;
932}
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
935{
936 struct xfrm_dump_info *sp = ptr;
937 struct xfrm_userpolicy_info *p;
938 struct sk_buff *in_skb = sp->in_skb;
939 struct sk_buff *skb = sp->out_skb;
940 struct nlmsghdr *nlh;
941 unsigned char *b = skb->tail;
942
943 if (sp->this_idx < sp->start_idx)
944 goto out;
945
946 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
947 sp->nlmsg_seq,
948 XFRM_MSG_NEWPOLICY, sizeof(*p));
949 p = NLMSG_DATA(nlh);
950 nlh->nlmsg_flags = sp->nlmsg_flags;
951
952 copy_to_user_policy(xp, p, dir);
953 if (copy_to_user_tmpl(xp, skb) < 0)
954 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -0800955 if (copy_to_user_sec_ctx(xp, skb))
956 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 nlh->nlmsg_len = skb->tail - b;
959out:
960 sp->this_idx++;
961 return 0;
962
963nlmsg_failure:
964 skb_trim(skb, b - skb->data);
965 return -1;
966}
967
968static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
969{
970 struct xfrm_dump_info info;
971
972 info.in_skb = cb->skb;
973 info.out_skb = skb;
974 info.nlmsg_seq = cb->nlh->nlmsg_seq;
975 info.nlmsg_flags = NLM_F_MULTI;
976 info.this_idx = 0;
977 info.start_idx = cb->args[0];
978 (void) xfrm_policy_walk(dump_one_policy, &info);
979 cb->args[0] = info.this_idx;
980
981 return skb->len;
982}
983
984static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
985 struct xfrm_policy *xp,
986 int dir, u32 seq)
987{
988 struct xfrm_dump_info info;
989 struct sk_buff *skb;
990
991 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
992 if (!skb)
993 return ERR_PTR(-ENOMEM);
994
995 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
996 info.in_skb = in_skb;
997 info.out_skb = skb;
998 info.nlmsg_seq = seq;
999 info.nlmsg_flags = 0;
1000 info.this_idx = info.start_idx = 0;
1001
1002 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1003 kfree_skb(skb);
1004 return NULL;
1005 }
1006
1007 return skb;
1008}
1009
1010static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1011{
1012 struct xfrm_policy *xp;
1013 struct xfrm_userpolicy_id *p;
1014 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001015 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 int delete;
1017
1018 p = NLMSG_DATA(nlh);
1019 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1020
1021 err = verify_policy_dir(p->dir);
1022 if (err)
1023 return err;
1024
1025 if (p->index)
1026 xp = xfrm_policy_byid(p->dir, p->index, delete);
Trent Jaegerdf718372005-12-13 23:12:27 -08001027 else {
1028 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1029 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1030 struct xfrm_policy tmp;
1031
1032 err = verify_sec_ctx_len(rtattrs);
1033 if (err)
1034 return err;
1035
1036 memset(&tmp, 0, sizeof(struct xfrm_policy));
1037 if (rt) {
1038 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1039
1040 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1041 return err;
1042 }
1043 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, delete);
1044 security_xfrm_policy_free(&tmp);
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (xp == NULL)
1047 return -ENOENT;
1048
1049 if (!delete) {
1050 struct sk_buff *resp_skb;
1051
1052 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1053 if (IS_ERR(resp_skb)) {
1054 err = PTR_ERR(resp_skb);
1055 } else {
1056 err = netlink_unicast(xfrm_nl, resp_skb,
1057 NETLINK_CB(skb).pid,
1058 MSG_DONTWAIT);
1059 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001060 } else {
Herbert Xue7443892005-06-18 22:44:18 -07001061 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001062 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001063 c.seq = nlh->nlmsg_seq;
1064 c.pid = nlh->nlmsg_pid;
1065 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067
1068 xfrm_pol_put(xp);
1069
1070 return err;
1071}
1072
1073static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1074{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001075 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1077
1078 xfrm_state_flush(p->proto);
Herbert Xubf088672005-06-18 22:44:00 -07001079 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001080 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001081 c.seq = nlh->nlmsg_seq;
1082 c.pid = nlh->nlmsg_pid;
1083 km_state_notify(NULL, &c);
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return 0;
1086}
1087
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001088
1089static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1090{
1091 struct xfrm_aevent_id *id;
1092 struct nlmsghdr *nlh;
1093 struct xfrm_lifetime_cur ltime;
1094 unsigned char *b = skb->tail;
1095
1096 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1097 id = NLMSG_DATA(nlh);
1098 nlh->nlmsg_flags = 0;
1099
1100 id->sa_id.daddr = x->id.daddr;
1101 id->sa_id.spi = x->id.spi;
1102 id->sa_id.family = x->props.family;
1103 id->sa_id.proto = x->id.proto;
1104 id->flags = c->data.aevent;
1105
1106 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1107
1108 ltime.bytes = x->curlft.bytes;
1109 ltime.packets = x->curlft.packets;
1110 ltime.add_time = x->curlft.add_time;
1111 ltime.use_time = x->curlft.use_time;
1112
1113 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
1114
1115 if (id->flags&XFRM_AE_RTHR) {
1116 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1117 }
1118
1119 if (id->flags&XFRM_AE_ETHR) {
1120 u32 etimer = x->replay_maxage*10/HZ;
1121 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1122 }
1123
1124 nlh->nlmsg_len = skb->tail - b;
1125 return skb->len;
1126
1127rtattr_failure:
1128nlmsg_failure:
1129 skb_trim(skb, b - skb->data);
1130 return -1;
1131}
1132
1133static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1134{
1135 struct xfrm_state *x;
1136 struct sk_buff *r_skb;
1137 int err;
1138 struct km_event c;
1139 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1140 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1141 struct xfrm_usersa_id *id = &p->sa_id;
1142
1143 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1144 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1145
1146 if (p->flags&XFRM_AE_RTHR)
1147 len+=RTA_SPACE(sizeof(u32));
1148
1149 if (p->flags&XFRM_AE_ETHR)
1150 len+=RTA_SPACE(sizeof(u32));
1151
1152 r_skb = alloc_skb(len, GFP_ATOMIC);
1153 if (r_skb == NULL)
1154 return -ENOMEM;
1155
1156 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1157 if (x == NULL) {
1158 kfree(r_skb);
1159 return -ESRCH;
1160 }
1161
1162 /*
1163 * XXX: is this lock really needed - none of the other
1164 * gets lock (the concern is things getting updated
1165 * while we are still reading) - jhs
1166 */
1167 spin_lock_bh(&x->lock);
1168 c.data.aevent = p->flags;
1169 c.seq = nlh->nlmsg_seq;
1170 c.pid = nlh->nlmsg_pid;
1171
1172 if (build_aevent(r_skb, x, &c) < 0)
1173 BUG();
1174 err = netlink_unicast(xfrm_nl, r_skb,
1175 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1176 spin_unlock_bh(&x->lock);
1177 xfrm_state_put(x);
1178 return err;
1179}
1180
1181static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1182{
1183 struct xfrm_state *x;
1184 struct km_event c;
1185 int err = - EINVAL;
1186 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1187 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1188 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1189
1190 if (!lt && !rp)
1191 return err;
1192
1193 /* pedantic mode - thou shalt sayeth replaceth */
1194 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1195 return err;
1196
1197 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1198 if (x == NULL)
1199 return -ESRCH;
1200
1201 if (x->km.state != XFRM_STATE_VALID)
1202 goto out;
1203
1204 spin_lock_bh(&x->lock);
1205 err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
1206 spin_unlock_bh(&x->lock);
1207 if (err < 0)
1208 goto out;
1209
1210 c.event = nlh->nlmsg_type;
1211 c.seq = nlh->nlmsg_seq;
1212 c.pid = nlh->nlmsg_pid;
1213 c.data.aevent = XFRM_AE_CU;
1214 km_state_notify(x, &c);
1215 err = 0;
1216out:
1217 xfrm_state_put(x);
1218 return err;
1219}
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1222{
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001223struct km_event c;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 xfrm_policy_flush();
Herbert Xuf60f6b82005-06-18 22:44:37 -07001226 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001227 c.seq = nlh->nlmsg_seq;
1228 c.pid = nlh->nlmsg_pid;
1229 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return 0;
1231}
1232
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001233static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1234{
1235 struct xfrm_policy *xp;
1236 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1237 struct xfrm_userpolicy_info *p = &up->pol;
1238 int err = -ENOENT;
1239
1240 if (p->index)
1241 xp = xfrm_policy_byid(p->dir, p->index, 0);
1242 else {
1243 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1244 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1245 struct xfrm_policy tmp;
1246
1247 err = verify_sec_ctx_len(rtattrs);
1248 if (err)
1249 return err;
1250
1251 memset(&tmp, 0, sizeof(struct xfrm_policy));
1252 if (rt) {
1253 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1254
1255 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1256 return err;
1257 }
1258 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, 0);
1259 security_xfrm_policy_free(&tmp);
1260 }
1261
1262 if (xp == NULL)
1263 return err;
1264 read_lock(&xp->lock);
1265 if (xp->dead) {
1266 read_unlock(&xp->lock);
1267 goto out;
1268 }
1269
1270 read_unlock(&xp->lock);
1271 err = 0;
1272 if (up->hard) {
1273 xfrm_policy_delete(xp, p->dir);
1274 } else {
1275 // reset the timers here?
1276 printk("Dont know what to do with soft policy expire\n");
1277 }
1278 km_policy_expired(xp, p->dir, up->hard, current->pid);
1279
1280out:
1281 xfrm_pol_put(xp);
1282 return err;
1283}
1284
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001285static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1286{
1287 struct xfrm_state *x;
1288 int err;
1289 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1290 struct xfrm_usersa_info *p = &ue->state;
1291
1292 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1293 err = -ENOENT;
1294
1295 if (x == NULL)
1296 return err;
1297
1298 err = -EINVAL;
1299
1300 spin_lock_bh(&x->lock);
1301 if (x->km.state != XFRM_STATE_VALID)
1302 goto out;
1303 km_state_expired(x, ue->hard, current->pid);
1304
1305 if (ue->hard)
1306 __xfrm_state_delete(x);
1307out:
1308 spin_unlock_bh(&x->lock);
1309 xfrm_state_put(x);
1310 return err;
1311}
1312
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001313static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1314{
1315 struct xfrm_policy *xp;
1316 struct xfrm_user_tmpl *ut;
1317 int i;
1318 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1319
1320 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1321 struct xfrm_state *x = xfrm_state_alloc();
1322 int err = -ENOMEM;
1323
1324 if (!x)
1325 return err;
1326
1327 err = verify_newpolicy_info(&ua->policy);
1328 if (err) {
1329 printk("BAD policy passed\n");
1330 kfree(x);
1331 return err;
1332 }
1333
1334 /* build an XP */
1335 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err); if (!xp) {
1336 kfree(x);
1337 return err;
1338 }
1339
1340 memcpy(&x->id, &ua->id, sizeof(ua->id));
1341 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1342 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1343
1344 ut = RTA_DATA(rt);
1345 /* extract the templates and for each call km_key */
1346 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1347 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1348 memcpy(&x->id, &t->id, sizeof(x->id));
1349 x->props.mode = t->mode;
1350 x->props.reqid = t->reqid;
1351 x->props.family = ut->family;
1352 t->aalgos = ua->aalgos;
1353 t->ealgos = ua->ealgos;
1354 t->calgos = ua->calgos;
1355 err = km_query(x, t, xp);
1356
1357 }
1358
1359 kfree(x);
1360 kfree(xp);
1361
1362 return 0;
1363}
1364
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001365
Thomas Graf492b5582005-05-03 14:26:40 -07001366#define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1367
1368static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1369 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1370 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1371 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1372 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1373 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1374 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1375 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001376 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001377 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001378 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1379 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001380 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001381 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1382 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001383 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1384 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385};
1386
Thomas Graf492b5582005-05-03 14:26:40 -07001387#undef XMSGSIZE
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389static struct xfrm_link {
1390 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1391 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001392} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1393 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1394 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1395 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1396 .dump = xfrm_dump_sa },
1397 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1398 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1399 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1400 .dump = xfrm_dump_policy },
1401 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001402 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001403 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001404 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1405 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001406 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001407 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1408 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001409 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1410 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411};
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1414{
1415 struct rtattr *xfrma[XFRMA_MAX];
1416 struct xfrm_link *link;
1417 int type, min_len;
1418
1419 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1420 return 0;
1421
1422 type = nlh->nlmsg_type;
1423
1424 /* A control message: ignore them */
1425 if (type < XFRM_MSG_BASE)
1426 return 0;
1427
1428 /* Unknown message: reply with EINVAL */
1429 if (type > XFRM_MSG_MAX)
1430 goto err_einval;
1431
1432 type -= XFRM_MSG_BASE;
1433 link = &xfrm_dispatch[type];
1434
1435 /* All operations require privileges, even GET */
1436 if (security_netlink_recv(skb)) {
1437 *errp = -EPERM;
1438 return -1;
1439 }
1440
Thomas Graf492b5582005-05-03 14:26:40 -07001441 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1442 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1443 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (link->dump == NULL)
1445 goto err_einval;
1446
1447 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
Thomas Grafa8f74b22005-11-10 02:25:52 +01001448 link->dump, NULL)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return -1;
1450 }
Thomas Graf88fc2c82005-11-10 02:25:54 +01001451
1452 netlink_queue_skip(nlh, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return -1;
1454 }
1455
1456 memset(xfrma, 0, sizeof(xfrma));
1457
1458 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1459 goto err_einval;
1460
1461 if (nlh->nlmsg_len > min_len) {
1462 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1463 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1464
1465 while (RTA_OK(attr, attrlen)) {
1466 unsigned short flavor = attr->rta_type;
1467 if (flavor) {
1468 if (flavor > XFRMA_MAX)
1469 goto err_einval;
1470 xfrma[flavor - 1] = attr;
1471 }
1472 attr = RTA_NEXT(attr, attrlen);
1473 }
1474 }
1475
1476 if (link->doit == NULL)
1477 goto err_einval;
1478 *errp = link->doit(skb, nlh, (void **) &xfrma);
1479
1480 return *errp;
1481
1482err_einval:
1483 *errp = -EINVAL;
1484 return -1;
1485}
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487static void xfrm_netlink_rcv(struct sock *sk, int len)
1488{
Thomas Graf88fc2c82005-11-10 02:25:54 +01001489 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 down(&xfrm_cfg_sem);
Thomas Graf88fc2c82005-11-10 02:25:54 +01001493 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 up(&xfrm_cfg_sem);
1495
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001496 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001499static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
1501 struct xfrm_user_expire *ue;
1502 struct nlmsghdr *nlh;
1503 unsigned char *b = skb->tail;
1504
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001505 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 sizeof(*ue));
1507 ue = NLMSG_DATA(nlh);
1508 nlh->nlmsg_flags = 0;
1509
1510 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001511 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 nlh->nlmsg_len = skb->tail - b;
1514 return skb->len;
1515
1516nlmsg_failure:
1517 skb_trim(skb, b - skb->data);
1518 return -1;
1519}
1520
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001521static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
1523 struct sk_buff *skb;
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001524 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001526 skb = alloc_skb(len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (skb == NULL)
1528 return -ENOMEM;
1529
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001530 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 BUG();
1532
Patrick McHardyac6d4392005-08-14 19:29:52 -07001533 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1534 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001537static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1538{
1539 struct sk_buff *skb;
1540 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1541
1542 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1543 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1544 skb = alloc_skb(len, GFP_ATOMIC);
1545 if (skb == NULL)
1546 return -ENOMEM;
1547
1548 if (build_aevent(skb, x, c) < 0)
1549 BUG();
1550
1551 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1552 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1553}
1554
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001555static int xfrm_notify_sa_flush(struct km_event *c)
1556{
1557 struct xfrm_usersa_flush *p;
1558 struct nlmsghdr *nlh;
1559 struct sk_buff *skb;
1560 unsigned char *b;
1561 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1562
1563 skb = alloc_skb(len, GFP_ATOMIC);
1564 if (skb == NULL)
1565 return -ENOMEM;
1566 b = skb->tail;
1567
1568 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1569 XFRM_MSG_FLUSHSA, sizeof(*p));
1570 nlh->nlmsg_flags = 0;
1571
1572 p = NLMSG_DATA(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07001573 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001574
1575 nlh->nlmsg_len = skb->tail - b;
1576
Patrick McHardyac6d4392005-08-14 19:29:52 -07001577 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1578 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001579
1580nlmsg_failure:
1581 kfree_skb(skb);
1582 return -1;
1583}
1584
1585static int inline xfrm_sa_len(struct xfrm_state *x)
1586{
Herbert Xu0603eac2005-06-18 22:54:36 -07001587 int l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001588 if (x->aalg)
1589 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1590 if (x->ealg)
1591 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1592 if (x->calg)
1593 l += RTA_SPACE(sizeof(*x->calg));
1594 if (x->encap)
1595 l += RTA_SPACE(sizeof(*x->encap));
1596
1597 return l;
1598}
1599
1600static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1601{
1602 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07001603 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001604 struct nlmsghdr *nlh;
1605 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001606 unsigned char *b;
1607 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07001608 int headlen;
1609
1610 headlen = sizeof(*p);
1611 if (c->event == XFRM_MSG_DELSA) {
1612 len += RTA_SPACE(headlen);
1613 headlen = sizeof(*id);
1614 }
1615 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001616
1617 skb = alloc_skb(len, GFP_ATOMIC);
1618 if (skb == NULL)
1619 return -ENOMEM;
1620 b = skb->tail;
1621
Herbert Xu0603eac2005-06-18 22:54:36 -07001622 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001623 nlh->nlmsg_flags = 0;
1624
1625 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07001626 if (c->event == XFRM_MSG_DELSA) {
1627 id = NLMSG_DATA(nlh);
1628 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1629 id->spi = x->id.spi;
1630 id->family = x->props.family;
1631 id->proto = x->id.proto;
1632
1633 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1634 }
1635
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001636 copy_to_user_state(x, p);
1637
1638 if (x->aalg)
1639 RTA_PUT(skb, XFRMA_ALG_AUTH,
1640 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1641 if (x->ealg)
1642 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1643 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1644 if (x->calg)
1645 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1646
1647 if (x->encap)
1648 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1649
1650 nlh->nlmsg_len = skb->tail - b;
1651
Patrick McHardyac6d4392005-08-14 19:29:52 -07001652 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1653 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001654
1655nlmsg_failure:
1656rtattr_failure:
1657 kfree_skb(skb);
1658 return -1;
1659}
1660
1661static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1662{
1663
1664 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001665 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001666 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001667 case XFRM_MSG_NEWAE:
1668 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001669 case XFRM_MSG_DELSA:
1670 case XFRM_MSG_UPDSA:
1671 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001672 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001673 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001674 return xfrm_notify_sa_flush(c);
1675 default:
1676 printk("xfrm_user: Unknown SA event %d\n", c->event);
1677 break;
1678 }
1679
1680 return 0;
1681
1682}
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1685 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1686 int dir)
1687{
1688 struct xfrm_user_acquire *ua;
1689 struct nlmsghdr *nlh;
1690 unsigned char *b = skb->tail;
1691 __u32 seq = xfrm_get_acqseq();
1692
1693 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1694 sizeof(*ua));
1695 ua = NLMSG_DATA(nlh);
1696 nlh->nlmsg_flags = 0;
1697
1698 memcpy(&ua->id, &x->id, sizeof(ua->id));
1699 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1700 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1701 copy_to_user_policy(xp, &ua->policy, dir);
1702 ua->aalgos = xt->aalgos;
1703 ua->ealgos = xt->ealgos;
1704 ua->calgos = xt->calgos;
1705 ua->seq = x->km.seq = seq;
1706
1707 if (copy_to_user_tmpl(xp, skb) < 0)
1708 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001709 if (copy_to_user_sec_ctx(xp, skb))
1710 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 nlh->nlmsg_len = skb->tail - b;
1713 return skb->len;
1714
1715nlmsg_failure:
1716 skb_trim(skb, b - skb->data);
1717 return -1;
1718}
1719
1720static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1721 struct xfrm_policy *xp, int dir)
1722{
1723 struct sk_buff *skb;
1724 size_t len;
1725
1726 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1727 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
Trent Jaegerdf718372005-12-13 23:12:27 -08001728 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 skb = alloc_skb(len, GFP_ATOMIC);
1730 if (skb == NULL)
1731 return -ENOMEM;
1732
1733 if (build_acquire(skb, x, xt, xp, dir) < 0)
1734 BUG();
1735
Patrick McHardyac6d4392005-08-14 19:29:52 -07001736 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
1737 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
1739
1740/* User gives us xfrm_user_policy_info followed by an array of 0
1741 * or more templates.
1742 */
1743static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
1744 u8 *data, int len, int *dir)
1745{
1746 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1747 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1748 struct xfrm_policy *xp;
1749 int nr;
1750
1751 switch (family) {
1752 case AF_INET:
1753 if (opt != IP_XFRM_POLICY) {
1754 *dir = -EOPNOTSUPP;
1755 return NULL;
1756 }
1757 break;
1758#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1759 case AF_INET6:
1760 if (opt != IPV6_XFRM_POLICY) {
1761 *dir = -EOPNOTSUPP;
1762 return NULL;
1763 }
1764 break;
1765#endif
1766 default:
1767 *dir = -EINVAL;
1768 return NULL;
1769 }
1770
1771 *dir = -EINVAL;
1772
1773 if (len < sizeof(*p) ||
1774 verify_newpolicy_info(p))
1775 return NULL;
1776
1777 nr = ((len - sizeof(*p)) / sizeof(*ut));
1778 if (nr > XFRM_MAX_DEPTH)
1779 return NULL;
1780
Herbert Xua4f1bac2005-07-26 15:43:17 -07001781 if (p->dir > XFRM_POLICY_OUT)
1782 return NULL;
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 xp = xfrm_policy_alloc(GFP_KERNEL);
1785 if (xp == NULL) {
1786 *dir = -ENOBUFS;
1787 return NULL;
1788 }
1789
1790 copy_from_user_policy(xp, p);
1791 copy_templates(xp, ut, nr);
1792
1793 *dir = p->dir;
1794
1795 return xp;
1796}
1797
1798static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001799 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
1801 struct xfrm_user_polexpire *upe;
1802 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001803 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 unsigned char *b = skb->tail;
1805
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001806 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 upe = NLMSG_DATA(nlh);
1808 nlh->nlmsg_flags = 0;
1809
1810 copy_to_user_policy(xp, &upe->pol, dir);
1811 if (copy_to_user_tmpl(xp, skb) < 0)
1812 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001813 if (copy_to_user_sec_ctx(xp, skb))
1814 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 upe->hard = !!hard;
1816
1817 nlh->nlmsg_len = skb->tail - b;
1818 return skb->len;
1819
1820nlmsg_failure:
1821 skb_trim(skb, b - skb->data);
1822 return -1;
1823}
1824
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001825static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
1827 struct sk_buff *skb;
1828 size_t len;
1829
1830 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1831 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
Trent Jaegerdf718372005-12-13 23:12:27 -08001832 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 skb = alloc_skb(len, GFP_ATOMIC);
1834 if (skb == NULL)
1835 return -ENOMEM;
1836
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001837 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 BUG();
1839
Patrick McHardyac6d4392005-08-14 19:29:52 -07001840 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1841 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001844static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
1845{
1846 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07001847 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001848 struct nlmsghdr *nlh;
1849 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001850 unsigned char *b;
1851 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07001852 int headlen;
1853
1854 headlen = sizeof(*p);
1855 if (c->event == XFRM_MSG_DELPOLICY) {
1856 len += RTA_SPACE(headlen);
1857 headlen = sizeof(*id);
1858 }
1859 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001860
1861 skb = alloc_skb(len, GFP_ATOMIC);
1862 if (skb == NULL)
1863 return -ENOMEM;
1864 b = skb->tail;
1865
Herbert Xu0603eac2005-06-18 22:54:36 -07001866 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001867
1868 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07001869 if (c->event == XFRM_MSG_DELPOLICY) {
1870 id = NLMSG_DATA(nlh);
1871 memset(id, 0, sizeof(*id));
1872 id->dir = dir;
1873 if (c->data.byid)
1874 id->index = xp->index;
1875 else
1876 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
1877
1878 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
1879 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001880
1881 nlh->nlmsg_flags = 0;
1882
1883 copy_to_user_policy(xp, p, dir);
1884 if (copy_to_user_tmpl(xp, skb) < 0)
1885 goto nlmsg_failure;
1886
1887 nlh->nlmsg_len = skb->tail - b;
1888
Patrick McHardyac6d4392005-08-14 19:29:52 -07001889 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1890 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001891
1892nlmsg_failure:
Herbert Xu0603eac2005-06-18 22:54:36 -07001893rtattr_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001894 kfree_skb(skb);
1895 return -1;
1896}
1897
1898static int xfrm_notify_policy_flush(struct km_event *c)
1899{
1900 struct nlmsghdr *nlh;
1901 struct sk_buff *skb;
1902 unsigned char *b;
1903 int len = NLMSG_LENGTH(0);
1904
1905 skb = alloc_skb(len, GFP_ATOMIC);
1906 if (skb == NULL)
1907 return -ENOMEM;
1908 b = skb->tail;
1909
1910
1911 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
1912
1913 nlh->nlmsg_len = skb->tail - b;
1914
Patrick McHardyac6d4392005-08-14 19:29:52 -07001915 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1916 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001917
1918nlmsg_failure:
1919 kfree_skb(skb);
1920 return -1;
1921}
1922
1923static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1924{
1925
1926 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001927 case XFRM_MSG_NEWPOLICY:
1928 case XFRM_MSG_UPDPOLICY:
1929 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001930 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001931 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001932 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001933 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001934 return xfrm_exp_policy_notify(xp, dir, c);
1935 default:
1936 printk("xfrm_user: Unknown Policy event %d\n", c->event);
1937 }
1938
1939 return 0;
1940
1941}
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943static struct xfrm_mgr netlink_mgr = {
1944 .id = "netlink",
1945 .notify = xfrm_send_state_notify,
1946 .acquire = xfrm_send_acquire,
1947 .compile_policy = xfrm_compile_policy,
1948 .notify_policy = xfrm_send_policy_notify,
1949};
1950
1951static int __init xfrm_user_init(void)
1952{
1953 printk(KERN_INFO "Initializing IPsec netlink socket\n");
1954
Patrick McHardy06628602005-08-15 12:33:26 -07001955 xfrm_nl = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
1956 xfrm_netlink_rcv, THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (xfrm_nl == NULL)
1958 return -ENOMEM;
1959
1960 xfrm_register_km(&netlink_mgr);
1961
1962 return 0;
1963}
1964
1965static void __exit xfrm_user_exit(void)
1966{
1967 xfrm_unregister_km(&netlink_mgr);
1968 sock_release(xfrm_nl->sk_socket);
1969}
1970
1971module_init(xfrm_user_init);
1972module_exit(xfrm_user_exit);
1973MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001974MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001975