blob: 7d18ca03c80d385a24da56cd1599e74755ca1384 [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
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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>
Thomas Graf88fc2c82005-11-10 02:25:54 +010029#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static 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;
Herbert Xu31c26852005-05-19 12:39:49 -070036 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 if (!rt)
39 return 0;
40
Herbert Xu31c26852005-05-19 12:39:49 -070041 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
42 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 return -EINVAL;
44
45 algp = RTA_DATA(rt);
Herbert Xu31c26852005-05-19 12:39:49 -070046
47 len -= (algp->alg_key_len + 7U) / 8;
48 if (len < 0)
49 return -EINVAL;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 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
Trent Jaegerdf718372005-12-13 23:12:27 -080090
91static inline int verify_sec_ctx_len(struct rtattr **xfrma)
92{
93 struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
94 struct xfrm_user_sec_ctx *uctx;
95 int len = 0;
96
97 if (!rt)
98 return 0;
99
100 if (rt->rta_len < sizeof(*uctx))
101 return -EINVAL;
102
103 uctx = RTA_DATA(rt);
104
Trent Jaegerdf718372005-12-13 23:12:27 -0800105 len += sizeof(struct xfrm_user_sec_ctx);
106 len += uctx->ctx_len;
107
108 if (uctx->len != len)
109 return -EINVAL;
110
111 return 0;
112}
113
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115static int verify_newsa_info(struct xfrm_usersa_info *p,
116 struct rtattr **xfrma)
117{
118 int err;
119
120 err = -EINVAL;
121 switch (p->family) {
122 case AF_INET:
123 break;
124
125 case AF_INET6:
126#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
127 break;
128#else
129 err = -EAFNOSUPPORT;
130 goto out;
131#endif
132
133 default:
134 goto out;
135 };
136
137 err = -EINVAL;
138 switch (p->id.proto) {
139 case IPPROTO_AH:
140 if (!xfrma[XFRMA_ALG_AUTH-1] ||
141 xfrma[XFRMA_ALG_CRYPT-1] ||
142 xfrma[XFRMA_ALG_COMP-1])
143 goto out;
144 break;
145
146 case IPPROTO_ESP:
147 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
148 !xfrma[XFRMA_ALG_CRYPT-1]) ||
149 xfrma[XFRMA_ALG_COMP-1])
150 goto out;
151 break;
152
153 case IPPROTO_COMP:
154 if (!xfrma[XFRMA_ALG_COMP-1] ||
155 xfrma[XFRMA_ALG_AUTH-1] ||
156 xfrma[XFRMA_ALG_CRYPT-1])
157 goto out;
158 break;
159
160 default:
161 goto out;
162 };
163
164 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
165 goto out;
166 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
167 goto out;
168 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
169 goto out;
170 if ((err = verify_encap_tmpl(xfrma)))
171 goto out;
Trent Jaegerdf718372005-12-13 23:12:27 -0800172 if ((err = verify_sec_ctx_len(xfrma)))
173 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 err = -EINVAL;
176 switch (p->mode) {
177 case 0:
178 case 1:
179 break;
180
181 default:
182 goto out;
183 };
184
185 err = 0;
186
187out:
188 return err;
189}
190
191static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
192 struct xfrm_algo_desc *(*get_byname)(char *, int),
193 struct rtattr *u_arg)
194{
195 struct rtattr *rta = u_arg;
196 struct xfrm_algo *p, *ualg;
197 struct xfrm_algo_desc *algo;
Herbert Xub9e9dea2005-05-19 12:39:04 -0700198 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 if (!rta)
201 return 0;
202
203 ualg = RTA_DATA(rta);
204
205 algo = get_byname(ualg->alg_name, 1);
206 if (!algo)
207 return -ENOSYS;
208 *props = algo->desc.sadb_alg_id;
209
Herbert Xub9e9dea2005-05-19 12:39:04 -0700210 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
211 p = kmalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (!p)
213 return -ENOMEM;
214
Herbert Xub9e9dea2005-05-19 12:39:04 -0700215 memcpy(p, ualg, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 *algpp = p;
217 return 0;
218}
219
220static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
221{
222 struct rtattr *rta = u_arg;
223 struct xfrm_encap_tmpl *p, *uencap;
224
225 if (!rta)
226 return 0;
227
228 uencap = RTA_DATA(rta);
229 p = kmalloc(sizeof(*p), GFP_KERNEL);
230 if (!p)
231 return -ENOMEM;
232
233 memcpy(p, uencap, sizeof(*p));
234 *encapp = p;
235 return 0;
236}
237
Trent Jaegerdf718372005-12-13 23:12:27 -0800238
239static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
240{
241 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
242 int len = 0;
243
244 if (xfrm_ctx) {
245 len += sizeof(struct xfrm_user_sec_ctx);
246 len += xfrm_ctx->ctx_len;
247 }
248 return len;
249}
250
251static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
252{
253 struct xfrm_user_sec_ctx *uctx;
254
255 if (!u_arg)
256 return 0;
257
258 uctx = RTA_DATA(u_arg);
259 return security_xfrm_state_alloc(x, uctx);
260}
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
263{
264 memcpy(&x->id, &p->id, sizeof(x->id));
265 memcpy(&x->sel, &p->sel, sizeof(x->sel));
266 memcpy(&x->lft, &p->lft, sizeof(x->lft));
267 x->props.mode = p->mode;
268 x->props.replay_window = p->replay_window;
269 x->props.reqid = p->reqid;
270 x->props.family = p->family;
271 x->props.saddr = p->saddr;
272 x->props.flags = p->flags;
273}
274
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800275/*
276 * someday when pfkey also has support, we could have the code
277 * somehow made shareable and move it to xfrm_state.c - JHS
278 *
279*/
280static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
281{
282 int err = - EINVAL;
283 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
284 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
285 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
286 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
287
288 if (rp) {
289 struct xfrm_replay_state *replay;
290 if (RTA_PAYLOAD(rp) < sizeof(*replay))
291 goto error;
292 replay = RTA_DATA(rp);
293 memcpy(&x->replay, replay, sizeof(*replay));
294 memcpy(&x->preplay, replay, sizeof(*replay));
295 }
296
297 if (lt) {
298 struct xfrm_lifetime_cur *ltime;
299 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
300 goto error;
301 ltime = RTA_DATA(lt);
302 x->curlft.bytes = ltime->bytes;
303 x->curlft.packets = ltime->packets;
304 x->curlft.add_time = ltime->add_time;
305 x->curlft.use_time = ltime->use_time;
306 }
307
308 if (et) {
309 if (RTA_PAYLOAD(et) < sizeof(u32))
310 goto error;
311 x->replay_maxage = *(u32*)RTA_DATA(et);
312 }
313
314 if (rt) {
315 if (RTA_PAYLOAD(rt) < sizeof(u32))
316 goto error;
317 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
318 }
319
320 return 0;
321error:
322 return err;
323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
326 struct rtattr **xfrma,
327 int *errp)
328{
329 struct xfrm_state *x = xfrm_state_alloc();
330 int err = -ENOMEM;
331
332 if (!x)
333 goto error_no_put;
334
335 copy_from_user_state(x, p);
336
337 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
338 xfrm_aalg_get_byname,
339 xfrma[XFRMA_ALG_AUTH-1])))
340 goto error;
341 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
342 xfrm_ealg_get_byname,
343 xfrma[XFRMA_ALG_CRYPT-1])))
344 goto error;
345 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
346 xfrm_calg_get_byname,
347 xfrma[XFRMA_ALG_COMP-1])))
348 goto error;
349 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
350 goto error;
351
Herbert Xu72cb6962005-06-20 13:18:08 -0700352 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (err)
354 goto error;
355
Trent Jaegerdf718372005-12-13 23:12:27 -0800356 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
357 goto error;
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800360 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
361 /* sysctl_xfrm_aevent_etime is in 100ms units */
362 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
363 x->preplay.bitmap = 0;
364 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
365 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
366
367 /* override default values from above */
368
369 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
370 if (err < 0)
371 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 return x;
374
375error:
376 x->km.state = XFRM_STATE_DEAD;
377 xfrm_state_put(x);
378error_no_put:
379 *errp = err;
380 return NULL;
381}
382
383static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
384{
385 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
386 struct xfrm_state *x;
387 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700388 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Trent Jaegerdf718372005-12-13 23:12:27 -0800390 err = verify_newsa_info(p, (struct rtattr **)xfrma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (err)
392 return err;
393
Trent Jaegerdf718372005-12-13 23:12:27 -0800394 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (!x)
396 return err;
397
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700398 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
400 err = xfrm_state_add(x);
401 else
402 err = xfrm_state_update(x);
403
404 if (err < 0) {
405 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800406 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700407 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700410 c.seq = nlh->nlmsg_seq;
411 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700412 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700413
414 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700415out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700416 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return err;
418}
419
420static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
421{
422 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700423 int err;
424 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
426
427 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
428 if (x == NULL)
429 return -ESRCH;
430
David S. Miller6f68dc32006-06-08 23:58:52 -0700431 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700432 goto out;
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700435 err = -EPERM;
436 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700439 err = xfrm_state_delete(x);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700440 if (err < 0)
441 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700442
443 c.seq = nlh->nlmsg_seq;
444 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700445 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700446 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700448out:
449 xfrm_state_put(x);
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 {
David S. Miller6f68dc32006-06-08 23:58:52 -07001061 if ((err = security_xfrm_policy_delete(xp)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001062 goto out;
Herbert Xue7443892005-06-18 22:44:18 -07001063 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001064 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001065 c.seq = nlh->nlmsg_seq;
1066 c.pid = nlh->nlmsg_pid;
1067 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069
1070 xfrm_pol_put(xp);
1071
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001072out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return err;
1074}
1075
1076static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1077{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001078 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1080
1081 xfrm_state_flush(p->proto);
Herbert Xubf088672005-06-18 22:44:00 -07001082 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001083 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001084 c.seq = nlh->nlmsg_seq;
1085 c.pid = nlh->nlmsg_pid;
1086 km_state_notify(NULL, &c);
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return 0;
1089}
1090
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001091
1092static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1093{
1094 struct xfrm_aevent_id *id;
1095 struct nlmsghdr *nlh;
1096 struct xfrm_lifetime_cur ltime;
1097 unsigned char *b = skb->tail;
1098
1099 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1100 id = NLMSG_DATA(nlh);
1101 nlh->nlmsg_flags = 0;
1102
1103 id->sa_id.daddr = x->id.daddr;
1104 id->sa_id.spi = x->id.spi;
1105 id->sa_id.family = x->props.family;
1106 id->sa_id.proto = x->id.proto;
1107 id->flags = c->data.aevent;
1108
1109 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1110
1111 ltime.bytes = x->curlft.bytes;
1112 ltime.packets = x->curlft.packets;
1113 ltime.add_time = x->curlft.add_time;
1114 ltime.use_time = x->curlft.use_time;
1115
1116 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
1117
1118 if (id->flags&XFRM_AE_RTHR) {
1119 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1120 }
1121
1122 if (id->flags&XFRM_AE_ETHR) {
1123 u32 etimer = x->replay_maxage*10/HZ;
1124 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1125 }
1126
1127 nlh->nlmsg_len = skb->tail - b;
1128 return skb->len;
1129
1130rtattr_failure:
1131nlmsg_failure:
1132 skb_trim(skb, b - skb->data);
1133 return -1;
1134}
1135
1136static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1137{
1138 struct xfrm_state *x;
1139 struct sk_buff *r_skb;
1140 int err;
1141 struct km_event c;
1142 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1143 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1144 struct xfrm_usersa_id *id = &p->sa_id;
1145
1146 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1147 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1148
1149 if (p->flags&XFRM_AE_RTHR)
1150 len+=RTA_SPACE(sizeof(u32));
1151
1152 if (p->flags&XFRM_AE_ETHR)
1153 len+=RTA_SPACE(sizeof(u32));
1154
1155 r_skb = alloc_skb(len, GFP_ATOMIC);
1156 if (r_skb == NULL)
1157 return -ENOMEM;
1158
1159 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1160 if (x == NULL) {
1161 kfree(r_skb);
1162 return -ESRCH;
1163 }
1164
1165 /*
1166 * XXX: is this lock really needed - none of the other
1167 * gets lock (the concern is things getting updated
1168 * while we are still reading) - jhs
1169 */
1170 spin_lock_bh(&x->lock);
1171 c.data.aevent = p->flags;
1172 c.seq = nlh->nlmsg_seq;
1173 c.pid = nlh->nlmsg_pid;
1174
1175 if (build_aevent(r_skb, x, &c) < 0)
1176 BUG();
1177 err = netlink_unicast(xfrm_nl, r_skb,
1178 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1179 spin_unlock_bh(&x->lock);
1180 xfrm_state_put(x);
1181 return err;
1182}
1183
1184static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1185{
1186 struct xfrm_state *x;
1187 struct km_event c;
1188 int err = - EINVAL;
1189 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1190 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1191 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1192
1193 if (!lt && !rp)
1194 return err;
1195
1196 /* pedantic mode - thou shalt sayeth replaceth */
1197 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1198 return err;
1199
1200 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1201 if (x == NULL)
1202 return -ESRCH;
1203
1204 if (x->km.state != XFRM_STATE_VALID)
1205 goto out;
1206
1207 spin_lock_bh(&x->lock);
1208 err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
1209 spin_unlock_bh(&x->lock);
1210 if (err < 0)
1211 goto out;
1212
1213 c.event = nlh->nlmsg_type;
1214 c.seq = nlh->nlmsg_seq;
1215 c.pid = nlh->nlmsg_pid;
1216 c.data.aevent = XFRM_AE_CU;
1217 km_state_notify(x, &c);
1218 err = 0;
1219out:
1220 xfrm_state_put(x);
1221 return err;
1222}
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1225{
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001226struct km_event c;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 xfrm_policy_flush();
Herbert Xuf60f6b82005-06-18 22:44:37 -07001229 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001230 c.seq = nlh->nlmsg_seq;
1231 c.pid = nlh->nlmsg_pid;
1232 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return 0;
1234}
1235
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001236static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1237{
1238 struct xfrm_policy *xp;
1239 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1240 struct xfrm_userpolicy_info *p = &up->pol;
1241 int err = -ENOENT;
1242
1243 if (p->index)
1244 xp = xfrm_policy_byid(p->dir, p->index, 0);
1245 else {
1246 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1247 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1248 struct xfrm_policy tmp;
1249
1250 err = verify_sec_ctx_len(rtattrs);
1251 if (err)
1252 return err;
1253
1254 memset(&tmp, 0, sizeof(struct xfrm_policy));
1255 if (rt) {
1256 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1257
1258 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1259 return err;
1260 }
1261 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, 0);
1262 security_xfrm_policy_free(&tmp);
1263 }
1264
1265 if (xp == NULL)
1266 return err;
1267 read_lock(&xp->lock);
1268 if (xp->dead) {
1269 read_unlock(&xp->lock);
1270 goto out;
1271 }
1272
1273 read_unlock(&xp->lock);
1274 err = 0;
1275 if (up->hard) {
1276 xfrm_policy_delete(xp, p->dir);
1277 } else {
1278 // reset the timers here?
1279 printk("Dont know what to do with soft policy expire\n");
1280 }
1281 km_policy_expired(xp, p->dir, up->hard, current->pid);
1282
1283out:
1284 xfrm_pol_put(xp);
1285 return err;
1286}
1287
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001288static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1289{
1290 struct xfrm_state *x;
1291 int err;
1292 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1293 struct xfrm_usersa_info *p = &ue->state;
1294
1295 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1296 err = -ENOENT;
1297
1298 if (x == NULL)
1299 return err;
1300
1301 err = -EINVAL;
1302
1303 spin_lock_bh(&x->lock);
1304 if (x->km.state != XFRM_STATE_VALID)
1305 goto out;
1306 km_state_expired(x, ue->hard, current->pid);
1307
1308 if (ue->hard)
1309 __xfrm_state_delete(x);
1310out:
1311 spin_unlock_bh(&x->lock);
1312 xfrm_state_put(x);
1313 return err;
1314}
1315
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001316static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1317{
1318 struct xfrm_policy *xp;
1319 struct xfrm_user_tmpl *ut;
1320 int i;
1321 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1322
1323 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1324 struct xfrm_state *x = xfrm_state_alloc();
1325 int err = -ENOMEM;
1326
1327 if (!x)
1328 return err;
1329
1330 err = verify_newpolicy_info(&ua->policy);
1331 if (err) {
1332 printk("BAD policy passed\n");
1333 kfree(x);
1334 return err;
1335 }
1336
1337 /* build an XP */
1338 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err); if (!xp) {
1339 kfree(x);
1340 return err;
1341 }
1342
1343 memcpy(&x->id, &ua->id, sizeof(ua->id));
1344 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1345 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1346
1347 ut = RTA_DATA(rt);
1348 /* extract the templates and for each call km_key */
1349 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1350 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1351 memcpy(&x->id, &t->id, sizeof(x->id));
1352 x->props.mode = t->mode;
1353 x->props.reqid = t->reqid;
1354 x->props.family = ut->family;
1355 t->aalgos = ua->aalgos;
1356 t->ealgos = ua->ealgos;
1357 t->calgos = ua->calgos;
1358 err = km_query(x, t, xp);
1359
1360 }
1361
1362 kfree(x);
1363 kfree(xp);
1364
1365 return 0;
1366}
1367
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001368
Thomas Graf492b5582005-05-03 14:26:40 -07001369#define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1370
1371static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1372 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1373 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1374 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1375 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1376 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1377 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1378 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001379 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001380 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001381 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1382 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001383 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001384 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1385 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001386 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1387 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388};
1389
Thomas Graf492b5582005-05-03 14:26:40 -07001390#undef XMSGSIZE
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392static struct xfrm_link {
1393 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1394 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001395} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1396 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1397 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1398 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1399 .dump = xfrm_dump_sa },
1400 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1401 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1402 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1403 .dump = xfrm_dump_policy },
1404 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001405 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001406 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001407 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1408 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001409 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001410 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1411 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001412 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1413 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414};
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1417{
1418 struct rtattr *xfrma[XFRMA_MAX];
1419 struct xfrm_link *link;
1420 int type, min_len;
1421
1422 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1423 return 0;
1424
1425 type = nlh->nlmsg_type;
1426
1427 /* A control message: ignore them */
1428 if (type < XFRM_MSG_BASE)
1429 return 0;
1430
1431 /* Unknown message: reply with EINVAL */
1432 if (type > XFRM_MSG_MAX)
1433 goto err_einval;
1434
1435 type -= XFRM_MSG_BASE;
1436 link = &xfrm_dispatch[type];
1437
1438 /* All operations require privileges, even GET */
Darrel Goeddelc7bdb542006-06-27 13:26:11 -07001439 if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 *errp = -EPERM;
1441 return -1;
1442 }
1443
Thomas Graf492b5582005-05-03 14:26:40 -07001444 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1445 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1446 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (link->dump == NULL)
1448 goto err_einval;
1449
1450 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
Thomas Grafa8f74b22005-11-10 02:25:52 +01001451 link->dump, NULL)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return -1;
1453 }
Thomas Graf88fc2c82005-11-10 02:25:54 +01001454
1455 netlink_queue_skip(nlh, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return -1;
1457 }
1458
1459 memset(xfrma, 0, sizeof(xfrma));
1460
1461 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1462 goto err_einval;
1463
1464 if (nlh->nlmsg_len > min_len) {
1465 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1466 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1467
1468 while (RTA_OK(attr, attrlen)) {
1469 unsigned short flavor = attr->rta_type;
1470 if (flavor) {
1471 if (flavor > XFRMA_MAX)
1472 goto err_einval;
1473 xfrma[flavor - 1] = attr;
1474 }
1475 attr = RTA_NEXT(attr, attrlen);
1476 }
1477 }
1478
1479 if (link->doit == NULL)
1480 goto err_einval;
1481 *errp = link->doit(skb, nlh, (void **) &xfrma);
1482
1483 return *errp;
1484
1485err_einval:
1486 *errp = -EINVAL;
1487 return -1;
1488}
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490static void xfrm_netlink_rcv(struct sock *sk, int len)
1491{
Thomas Graf88fc2c82005-11-10 02:25:54 +01001492 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 do {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001495 mutex_lock(&xfrm_cfg_mutex);
Thomas Graf88fc2c82005-11-10 02:25:54 +01001496 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001497 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001499 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001502static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 struct xfrm_user_expire *ue;
1505 struct nlmsghdr *nlh;
1506 unsigned char *b = skb->tail;
1507
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001508 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 sizeof(*ue));
1510 ue = NLMSG_DATA(nlh);
1511 nlh->nlmsg_flags = 0;
1512
1513 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001514 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 nlh->nlmsg_len = skb->tail - b;
1517 return skb->len;
1518
1519nlmsg_failure:
1520 skb_trim(skb, b - skb->data);
1521 return -1;
1522}
1523
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001524static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526 struct sk_buff *skb;
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001527 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001529 skb = alloc_skb(len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (skb == NULL)
1531 return -ENOMEM;
1532
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001533 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 BUG();
1535
Patrick McHardyac6d4392005-08-14 19:29:52 -07001536 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1537 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001540static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1541{
1542 struct sk_buff *skb;
1543 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1544
1545 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1546 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1547 skb = alloc_skb(len, GFP_ATOMIC);
1548 if (skb == NULL)
1549 return -ENOMEM;
1550
1551 if (build_aevent(skb, x, c) < 0)
1552 BUG();
1553
1554 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1555 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1556}
1557
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001558static int xfrm_notify_sa_flush(struct km_event *c)
1559{
1560 struct xfrm_usersa_flush *p;
1561 struct nlmsghdr *nlh;
1562 struct sk_buff *skb;
1563 unsigned char *b;
1564 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1565
1566 skb = alloc_skb(len, GFP_ATOMIC);
1567 if (skb == NULL)
1568 return -ENOMEM;
1569 b = skb->tail;
1570
1571 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1572 XFRM_MSG_FLUSHSA, sizeof(*p));
1573 nlh->nlmsg_flags = 0;
1574
1575 p = NLMSG_DATA(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07001576 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001577
1578 nlh->nlmsg_len = skb->tail - b;
1579
Patrick McHardyac6d4392005-08-14 19:29:52 -07001580 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1581 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001582
1583nlmsg_failure:
1584 kfree_skb(skb);
1585 return -1;
1586}
1587
1588static int inline xfrm_sa_len(struct xfrm_state *x)
1589{
Herbert Xu0603eac2005-06-18 22:54:36 -07001590 int l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001591 if (x->aalg)
1592 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1593 if (x->ealg)
1594 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1595 if (x->calg)
1596 l += RTA_SPACE(sizeof(*x->calg));
1597 if (x->encap)
1598 l += RTA_SPACE(sizeof(*x->encap));
1599
1600 return l;
1601}
1602
1603static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1604{
1605 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07001606 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001607 struct nlmsghdr *nlh;
1608 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001609 unsigned char *b;
1610 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07001611 int headlen;
1612
1613 headlen = sizeof(*p);
1614 if (c->event == XFRM_MSG_DELSA) {
1615 len += RTA_SPACE(headlen);
1616 headlen = sizeof(*id);
1617 }
1618 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001619
1620 skb = alloc_skb(len, GFP_ATOMIC);
1621 if (skb == NULL)
1622 return -ENOMEM;
1623 b = skb->tail;
1624
Herbert Xu0603eac2005-06-18 22:54:36 -07001625 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001626 nlh->nlmsg_flags = 0;
1627
1628 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07001629 if (c->event == XFRM_MSG_DELSA) {
1630 id = NLMSG_DATA(nlh);
1631 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1632 id->spi = x->id.spi;
1633 id->family = x->props.family;
1634 id->proto = x->id.proto;
1635
1636 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1637 }
1638
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001639 copy_to_user_state(x, p);
1640
1641 if (x->aalg)
1642 RTA_PUT(skb, XFRMA_ALG_AUTH,
1643 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1644 if (x->ealg)
1645 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1646 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1647 if (x->calg)
1648 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1649
1650 if (x->encap)
1651 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1652
1653 nlh->nlmsg_len = skb->tail - b;
1654
Patrick McHardyac6d4392005-08-14 19:29:52 -07001655 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1656 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001657
1658nlmsg_failure:
1659rtattr_failure:
1660 kfree_skb(skb);
1661 return -1;
1662}
1663
1664static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1665{
1666
1667 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001668 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001669 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001670 case XFRM_MSG_NEWAE:
1671 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001672 case XFRM_MSG_DELSA:
1673 case XFRM_MSG_UPDSA:
1674 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001675 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001676 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001677 return xfrm_notify_sa_flush(c);
1678 default:
1679 printk("xfrm_user: Unknown SA event %d\n", c->event);
1680 break;
1681 }
1682
1683 return 0;
1684
1685}
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1688 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1689 int dir)
1690{
1691 struct xfrm_user_acquire *ua;
1692 struct nlmsghdr *nlh;
1693 unsigned char *b = skb->tail;
1694 __u32 seq = xfrm_get_acqseq();
1695
1696 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1697 sizeof(*ua));
1698 ua = NLMSG_DATA(nlh);
1699 nlh->nlmsg_flags = 0;
1700
1701 memcpy(&ua->id, &x->id, sizeof(ua->id));
1702 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1703 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1704 copy_to_user_policy(xp, &ua->policy, dir);
1705 ua->aalgos = xt->aalgos;
1706 ua->ealgos = xt->ealgos;
1707 ua->calgos = xt->calgos;
1708 ua->seq = x->km.seq = seq;
1709
1710 if (copy_to_user_tmpl(xp, skb) < 0)
1711 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001712 if (copy_to_user_sec_ctx(xp, skb))
1713 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 nlh->nlmsg_len = skb->tail - b;
1716 return skb->len;
1717
1718nlmsg_failure:
1719 skb_trim(skb, b - skb->data);
1720 return -1;
1721}
1722
1723static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1724 struct xfrm_policy *xp, int dir)
1725{
1726 struct sk_buff *skb;
1727 size_t len;
1728
1729 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1730 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
Trent Jaegerdf718372005-12-13 23:12:27 -08001731 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 skb = alloc_skb(len, GFP_ATOMIC);
1733 if (skb == NULL)
1734 return -ENOMEM;
1735
1736 if (build_acquire(skb, x, xt, xp, dir) < 0)
1737 BUG();
1738
Patrick McHardyac6d4392005-08-14 19:29:52 -07001739 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
1740 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
1743/* User gives us xfrm_user_policy_info followed by an array of 0
1744 * or more templates.
1745 */
1746static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
1747 u8 *data, int len, int *dir)
1748{
1749 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1750 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1751 struct xfrm_policy *xp;
1752 int nr;
1753
1754 switch (family) {
1755 case AF_INET:
1756 if (opt != IP_XFRM_POLICY) {
1757 *dir = -EOPNOTSUPP;
1758 return NULL;
1759 }
1760 break;
1761#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1762 case AF_INET6:
1763 if (opt != IPV6_XFRM_POLICY) {
1764 *dir = -EOPNOTSUPP;
1765 return NULL;
1766 }
1767 break;
1768#endif
1769 default:
1770 *dir = -EINVAL;
1771 return NULL;
1772 }
1773
1774 *dir = -EINVAL;
1775
1776 if (len < sizeof(*p) ||
1777 verify_newpolicy_info(p))
1778 return NULL;
1779
1780 nr = ((len - sizeof(*p)) / sizeof(*ut));
1781 if (nr > XFRM_MAX_DEPTH)
1782 return NULL;
1783
Herbert Xua4f1bac2005-07-26 15:43:17 -07001784 if (p->dir > XFRM_POLICY_OUT)
1785 return NULL;
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 xp = xfrm_policy_alloc(GFP_KERNEL);
1788 if (xp == NULL) {
1789 *dir = -ENOBUFS;
1790 return NULL;
1791 }
1792
1793 copy_from_user_policy(xp, p);
1794 copy_templates(xp, ut, nr);
1795
1796 *dir = p->dir;
1797
1798 return xp;
1799}
1800
1801static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001802 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
1804 struct xfrm_user_polexpire *upe;
1805 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001806 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 unsigned char *b = skb->tail;
1808
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001809 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 upe = NLMSG_DATA(nlh);
1811 nlh->nlmsg_flags = 0;
1812
1813 copy_to_user_policy(xp, &upe->pol, dir);
1814 if (copy_to_user_tmpl(xp, skb) < 0)
1815 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001816 if (copy_to_user_sec_ctx(xp, skb))
1817 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 upe->hard = !!hard;
1819
1820 nlh->nlmsg_len = skb->tail - b;
1821 return skb->len;
1822
1823nlmsg_failure:
1824 skb_trim(skb, b - skb->data);
1825 return -1;
1826}
1827
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001828static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
1830 struct sk_buff *skb;
1831 size_t len;
1832
1833 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1834 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
Trent Jaegerdf718372005-12-13 23:12:27 -08001835 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 skb = alloc_skb(len, GFP_ATOMIC);
1837 if (skb == NULL)
1838 return -ENOMEM;
1839
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001840 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 BUG();
1842
Patrick McHardyac6d4392005-08-14 19:29:52 -07001843 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1844 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001847static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
1848{
1849 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07001850 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001851 struct nlmsghdr *nlh;
1852 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001853 unsigned char *b;
1854 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07001855 int headlen;
1856
1857 headlen = sizeof(*p);
1858 if (c->event == XFRM_MSG_DELPOLICY) {
1859 len += RTA_SPACE(headlen);
1860 headlen = sizeof(*id);
1861 }
1862 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001863
1864 skb = alloc_skb(len, GFP_ATOMIC);
1865 if (skb == NULL)
1866 return -ENOMEM;
1867 b = skb->tail;
1868
Herbert Xu0603eac2005-06-18 22:54:36 -07001869 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001870
1871 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07001872 if (c->event == XFRM_MSG_DELPOLICY) {
1873 id = NLMSG_DATA(nlh);
1874 memset(id, 0, sizeof(*id));
1875 id->dir = dir;
1876 if (c->data.byid)
1877 id->index = xp->index;
1878 else
1879 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
1880
1881 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
1882 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001883
1884 nlh->nlmsg_flags = 0;
1885
1886 copy_to_user_policy(xp, p, dir);
1887 if (copy_to_user_tmpl(xp, skb) < 0)
1888 goto nlmsg_failure;
1889
1890 nlh->nlmsg_len = skb->tail - b;
1891
Patrick McHardyac6d4392005-08-14 19:29:52 -07001892 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1893 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001894
1895nlmsg_failure:
Herbert Xu0603eac2005-06-18 22:54:36 -07001896rtattr_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001897 kfree_skb(skb);
1898 return -1;
1899}
1900
1901static int xfrm_notify_policy_flush(struct km_event *c)
1902{
1903 struct nlmsghdr *nlh;
1904 struct sk_buff *skb;
1905 unsigned char *b;
1906 int len = NLMSG_LENGTH(0);
1907
1908 skb = alloc_skb(len, GFP_ATOMIC);
1909 if (skb == NULL)
1910 return -ENOMEM;
1911 b = skb->tail;
1912
1913
1914 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
1915
1916 nlh->nlmsg_len = skb->tail - b;
1917
Patrick McHardyac6d4392005-08-14 19:29:52 -07001918 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1919 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001920
1921nlmsg_failure:
1922 kfree_skb(skb);
1923 return -1;
1924}
1925
1926static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1927{
1928
1929 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001930 case XFRM_MSG_NEWPOLICY:
1931 case XFRM_MSG_UPDPOLICY:
1932 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001933 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001934 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001935 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001936 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001937 return xfrm_exp_policy_notify(xp, dir, c);
1938 default:
1939 printk("xfrm_user: Unknown Policy event %d\n", c->event);
1940 }
1941
1942 return 0;
1943
1944}
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946static struct xfrm_mgr netlink_mgr = {
1947 .id = "netlink",
1948 .notify = xfrm_send_state_notify,
1949 .acquire = xfrm_send_acquire,
1950 .compile_policy = xfrm_compile_policy,
1951 .notify_policy = xfrm_send_policy_notify,
1952};
1953
1954static int __init xfrm_user_init(void)
1955{
Patrick McHardybe336902006-03-20 22:40:54 -08001956 struct sock *nlsk;
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 printk(KERN_INFO "Initializing IPsec netlink socket\n");
1959
Patrick McHardybe336902006-03-20 22:40:54 -08001960 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
1961 xfrm_netlink_rcv, THIS_MODULE);
1962 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08001964 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 xfrm_register_km(&netlink_mgr);
1967
1968 return 0;
1969}
1970
1971static void __exit xfrm_user_exit(void)
1972{
Patrick McHardybe336902006-03-20 22:40:54 -08001973 struct sock *nlsk = xfrm_nl;
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08001976 rcu_assign_pointer(xfrm_nl, NULL);
1977 synchronize_rcu();
1978 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
1981module_init(xfrm_user_init);
1982module_exit(xfrm_user_exit);
1983MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001984MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001985