blob: 3e6a722d072ed8b75c0561b82617abd5a2d3adcd [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
Trent Jaegerdf718372005-12-13 23:12:27 -0800104 len += sizeof(struct xfrm_user_sec_ctx);
105 len += uctx->ctx_len;
106
107 if (uctx->len != len)
108 return -EINVAL;
109
110 return 0;
111}
112
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static int verify_newsa_info(struct xfrm_usersa_info *p,
115 struct rtattr **xfrma)
116{
117 int err;
118
119 err = -EINVAL;
120 switch (p->family) {
121 case AF_INET:
122 break;
123
124 case AF_INET6:
125#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
126 break;
127#else
128 err = -EAFNOSUPPORT;
129 goto out;
130#endif
131
132 default:
133 goto out;
134 };
135
136 err = -EINVAL;
137 switch (p->id.proto) {
138 case IPPROTO_AH:
139 if (!xfrma[XFRMA_ALG_AUTH-1] ||
140 xfrma[XFRMA_ALG_CRYPT-1] ||
141 xfrma[XFRMA_ALG_COMP-1])
142 goto out;
143 break;
144
145 case IPPROTO_ESP:
146 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
147 !xfrma[XFRMA_ALG_CRYPT-1]) ||
148 xfrma[XFRMA_ALG_COMP-1])
149 goto out;
150 break;
151
152 case IPPROTO_COMP:
153 if (!xfrma[XFRMA_ALG_COMP-1] ||
154 xfrma[XFRMA_ALG_AUTH-1] ||
155 xfrma[XFRMA_ALG_CRYPT-1])
156 goto out;
157 break;
158
159 default:
160 goto out;
161 };
162
163 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
164 goto out;
165 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
166 goto out;
167 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
168 goto out;
169 if ((err = verify_encap_tmpl(xfrma)))
170 goto out;
Trent Jaegerdf718372005-12-13 23:12:27 -0800171 if ((err = verify_sec_ctx_len(xfrma)))
172 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 err = -EINVAL;
175 switch (p->mode) {
176 case 0:
177 case 1:
178 break;
179
180 default:
181 goto out;
182 };
183
184 err = 0;
185
186out:
187 return err;
188}
189
190static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
191 struct xfrm_algo_desc *(*get_byname)(char *, int),
192 struct rtattr *u_arg)
193{
194 struct rtattr *rta = u_arg;
195 struct xfrm_algo *p, *ualg;
196 struct xfrm_algo_desc *algo;
Herbert Xub9e9dea2005-05-19 12:39:04 -0700197 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 if (!rta)
200 return 0;
201
202 ualg = RTA_DATA(rta);
203
204 algo = get_byname(ualg->alg_name, 1);
205 if (!algo)
206 return -ENOSYS;
207 *props = algo->desc.sadb_alg_id;
208
Herbert Xub9e9dea2005-05-19 12:39:04 -0700209 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
210 p = kmalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (!p)
212 return -ENOMEM;
213
Herbert Xub9e9dea2005-05-19 12:39:04 -0700214 memcpy(p, ualg, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 *algpp = p;
216 return 0;
217}
218
219static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
220{
221 struct rtattr *rta = u_arg;
222 struct xfrm_encap_tmpl *p, *uencap;
223
224 if (!rta)
225 return 0;
226
227 uencap = RTA_DATA(rta);
228 p = kmalloc(sizeof(*p), GFP_KERNEL);
229 if (!p)
230 return -ENOMEM;
231
232 memcpy(p, uencap, sizeof(*p));
233 *encapp = p;
234 return 0;
235}
236
Trent Jaegerdf718372005-12-13 23:12:27 -0800237
238static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
239{
240 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
241 int len = 0;
242
243 if (xfrm_ctx) {
244 len += sizeof(struct xfrm_user_sec_ctx);
245 len += xfrm_ctx->ctx_len;
246 }
247 return len;
248}
249
250static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
251{
252 struct xfrm_user_sec_ctx *uctx;
253
254 if (!u_arg)
255 return 0;
256
257 uctx = RTA_DATA(u_arg);
258 return security_xfrm_state_alloc(x, uctx);
259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
262{
263 memcpy(&x->id, &p->id, sizeof(x->id));
264 memcpy(&x->sel, &p->sel, sizeof(x->sel));
265 memcpy(&x->lft, &p->lft, sizeof(x->lft));
266 x->props.mode = p->mode;
267 x->props.replay_window = p->replay_window;
268 x->props.reqid = p->reqid;
269 x->props.family = p->family;
270 x->props.saddr = p->saddr;
271 x->props.flags = p->flags;
272}
273
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800274/*
275 * someday when pfkey also has support, we could have the code
276 * somehow made shareable and move it to xfrm_state.c - JHS
277 *
278*/
279static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
280{
281 int err = - EINVAL;
282 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
283 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
284 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
285 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
286
287 if (rp) {
288 struct xfrm_replay_state *replay;
289 if (RTA_PAYLOAD(rp) < sizeof(*replay))
290 goto error;
291 replay = RTA_DATA(rp);
292 memcpy(&x->replay, replay, sizeof(*replay));
293 memcpy(&x->preplay, replay, sizeof(*replay));
294 }
295
296 if (lt) {
297 struct xfrm_lifetime_cur *ltime;
298 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
299 goto error;
300 ltime = RTA_DATA(lt);
301 x->curlft.bytes = ltime->bytes;
302 x->curlft.packets = ltime->packets;
303 x->curlft.add_time = ltime->add_time;
304 x->curlft.use_time = ltime->use_time;
305 }
306
307 if (et) {
308 if (RTA_PAYLOAD(et) < sizeof(u32))
309 goto error;
310 x->replay_maxage = *(u32*)RTA_DATA(et);
311 }
312
313 if (rt) {
314 if (RTA_PAYLOAD(rt) < sizeof(u32))
315 goto error;
316 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
317 }
318
319 return 0;
320error:
321 return err;
322}
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
325 struct rtattr **xfrma,
326 int *errp)
327{
328 struct xfrm_state *x = xfrm_state_alloc();
329 int err = -ENOMEM;
330
331 if (!x)
332 goto error_no_put;
333
334 copy_from_user_state(x, p);
335
336 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
337 xfrm_aalg_get_byname,
338 xfrma[XFRMA_ALG_AUTH-1])))
339 goto error;
340 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
341 xfrm_ealg_get_byname,
342 xfrma[XFRMA_ALG_CRYPT-1])))
343 goto error;
344 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
345 xfrm_calg_get_byname,
346 xfrma[XFRMA_ALG_COMP-1])))
347 goto error;
348 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
349 goto error;
350
Herbert Xu72cb6962005-06-20 13:18:08 -0700351 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (err)
353 goto error;
354
Trent Jaegerdf718372005-12-13 23:12:27 -0800355 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
356 goto error;
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800359 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
360 /* sysctl_xfrm_aevent_etime is in 100ms units */
361 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
362 x->preplay.bitmap = 0;
363 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
364 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
365
366 /* override default values from above */
367
368 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
369 if (err < 0)
370 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 return x;
373
374error:
375 x->km.state = XFRM_STATE_DEAD;
376 xfrm_state_put(x);
377error_no_put:
378 *errp = err;
379 return NULL;
380}
381
382static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
383{
384 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
385 struct xfrm_state *x;
386 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700387 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Trent Jaegerdf718372005-12-13 23:12:27 -0800389 err = verify_newsa_info(p, (struct rtattr **)xfrma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (err)
391 return err;
392
Trent Jaegerdf718372005-12-13 23:12:27 -0800393 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (!x)
395 return err;
396
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700397 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
399 err = xfrm_state_add(x);
400 else
401 err = xfrm_state_update(x);
402
403 if (err < 0) {
404 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800405 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700406 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700409 c.seq = nlh->nlmsg_seq;
410 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700411 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700412
413 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700414out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700415 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return err;
417}
418
419static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
420{
421 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700422 int err;
423 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
425
426 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
427 if (x == NULL)
428 return -ESRCH;
429
David S. Miller6f68dc32006-06-08 23:58:52 -0700430 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700431 goto out;
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700434 err = -EPERM;
435 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700438 err = xfrm_state_delete(x);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700439 if (err < 0)
440 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700441
442 c.seq = nlh->nlmsg_seq;
443 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700444 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700445 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700447out:
448 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700449 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
453{
454 memcpy(&p->id, &x->id, sizeof(p->id));
455 memcpy(&p->sel, &x->sel, sizeof(p->sel));
456 memcpy(&p->lft, &x->lft, sizeof(p->lft));
457 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
458 memcpy(&p->stats, &x->stats, sizeof(p->stats));
459 p->saddr = x->props.saddr;
460 p->mode = x->props.mode;
461 p->replay_window = x->props.replay_window;
462 p->reqid = x->props.reqid;
463 p->family = x->props.family;
464 p->flags = x->props.flags;
465 p->seq = x->km.seq;
466}
467
468struct xfrm_dump_info {
469 struct sk_buff *in_skb;
470 struct sk_buff *out_skb;
471 u32 nlmsg_seq;
472 u16 nlmsg_flags;
473 int start_idx;
474 int this_idx;
475};
476
477static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
478{
479 struct xfrm_dump_info *sp = ptr;
480 struct sk_buff *in_skb = sp->in_skb;
481 struct sk_buff *skb = sp->out_skb;
482 struct xfrm_usersa_info *p;
483 struct nlmsghdr *nlh;
484 unsigned char *b = skb->tail;
485
486 if (sp->this_idx < sp->start_idx)
487 goto out;
488
489 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
490 sp->nlmsg_seq,
491 XFRM_MSG_NEWSA, sizeof(*p));
492 nlh->nlmsg_flags = sp->nlmsg_flags;
493
494 p = NLMSG_DATA(nlh);
495 copy_to_user_state(x, p);
496
497 if (x->aalg)
498 RTA_PUT(skb, XFRMA_ALG_AUTH,
499 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
500 if (x->ealg)
501 RTA_PUT(skb, XFRMA_ALG_CRYPT,
502 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
503 if (x->calg)
504 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
505
506 if (x->encap)
507 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
508
Trent Jaegerdf718372005-12-13 23:12:27 -0800509 if (x->security) {
510 int ctx_size = sizeof(struct xfrm_sec_ctx) +
511 x->security->ctx_len;
512 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
513 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
514
515 uctx->exttype = XFRMA_SEC_CTX;
516 uctx->len = ctx_size;
517 uctx->ctx_doi = x->security->ctx_doi;
518 uctx->ctx_alg = x->security->ctx_alg;
519 uctx->ctx_len = x->security->ctx_len;
520 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 nlh->nlmsg_len = skb->tail - b;
523out:
524 sp->this_idx++;
525 return 0;
526
527nlmsg_failure:
528rtattr_failure:
529 skb_trim(skb, b - skb->data);
530 return -1;
531}
532
533static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
534{
535 struct xfrm_dump_info info;
536
537 info.in_skb = cb->skb;
538 info.out_skb = skb;
539 info.nlmsg_seq = cb->nlh->nlmsg_seq;
540 info.nlmsg_flags = NLM_F_MULTI;
541 info.this_idx = 0;
542 info.start_idx = cb->args[0];
543 (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
544 cb->args[0] = info.this_idx;
545
546 return skb->len;
547}
548
549static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
550 struct xfrm_state *x, u32 seq)
551{
552 struct xfrm_dump_info info;
553 struct sk_buff *skb;
554
555 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
556 if (!skb)
557 return ERR_PTR(-ENOMEM);
558
559 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
560 info.in_skb = in_skb;
561 info.out_skb = skb;
562 info.nlmsg_seq = seq;
563 info.nlmsg_flags = 0;
564 info.this_idx = info.start_idx = 0;
565
566 if (dump_one_state(x, 0, &info)) {
567 kfree_skb(skb);
568 return NULL;
569 }
570
571 return skb;
572}
573
574static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
575{
576 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
577 struct xfrm_state *x;
578 struct sk_buff *resp_skb;
579 int err;
580
581 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
582 err = -ESRCH;
583 if (x == NULL)
584 goto out_noput;
585
586 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
587 if (IS_ERR(resp_skb)) {
588 err = PTR_ERR(resp_skb);
589 } else {
590 err = netlink_unicast(xfrm_nl, resp_skb,
591 NETLINK_CB(skb).pid, MSG_DONTWAIT);
592 }
593 xfrm_state_put(x);
594out_noput:
595 return err;
596}
597
598static int verify_userspi_info(struct xfrm_userspi_info *p)
599{
600 switch (p->info.id.proto) {
601 case IPPROTO_AH:
602 case IPPROTO_ESP:
603 break;
604
605 case IPPROTO_COMP:
606 /* IPCOMP spi is 16-bits. */
607 if (p->max >= 0x10000)
608 return -EINVAL;
609 break;
610
611 default:
612 return -EINVAL;
613 };
614
615 if (p->min > p->max)
616 return -EINVAL;
617
618 return 0;
619}
620
621static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
622{
623 struct xfrm_state *x;
624 struct xfrm_userspi_info *p;
625 struct sk_buff *resp_skb;
626 xfrm_address_t *daddr;
627 int family;
628 int err;
629
630 p = NLMSG_DATA(nlh);
631 err = verify_userspi_info(p);
632 if (err)
633 goto out_noput;
634
635 family = p->info.family;
636 daddr = &p->info.id.daddr;
637
638 x = NULL;
639 if (p->info.seq) {
640 x = xfrm_find_acq_byseq(p->info.seq);
641 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
642 xfrm_state_put(x);
643 x = NULL;
644 }
645 }
646
647 if (!x)
648 x = xfrm_find_acq(p->info.mode, p->info.reqid,
649 p->info.id.proto, daddr,
650 &p->info.saddr, 1,
651 family);
652 err = -ENOENT;
653 if (x == NULL)
654 goto out_noput;
655
656 resp_skb = ERR_PTR(-ENOENT);
657
658 spin_lock_bh(&x->lock);
659 if (x->km.state != XFRM_STATE_DEAD) {
660 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
661 if (x->id.spi)
662 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
663 }
664 spin_unlock_bh(&x->lock);
665
666 if (IS_ERR(resp_skb)) {
667 err = PTR_ERR(resp_skb);
668 goto out;
669 }
670
671 err = netlink_unicast(xfrm_nl, resp_skb,
672 NETLINK_CB(skb).pid, MSG_DONTWAIT);
673
674out:
675 xfrm_state_put(x);
676out_noput:
677 return err;
678}
679
680static int verify_policy_dir(__u8 dir)
681{
682 switch (dir) {
683 case XFRM_POLICY_IN:
684 case XFRM_POLICY_OUT:
685 case XFRM_POLICY_FWD:
686 break;
687
688 default:
689 return -EINVAL;
690 };
691
692 return 0;
693}
694
695static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
696{
697 switch (p->share) {
698 case XFRM_SHARE_ANY:
699 case XFRM_SHARE_SESSION:
700 case XFRM_SHARE_USER:
701 case XFRM_SHARE_UNIQUE:
702 break;
703
704 default:
705 return -EINVAL;
706 };
707
708 switch (p->action) {
709 case XFRM_POLICY_ALLOW:
710 case XFRM_POLICY_BLOCK:
711 break;
712
713 default:
714 return -EINVAL;
715 };
716
717 switch (p->sel.family) {
718 case AF_INET:
719 break;
720
721 case AF_INET6:
722#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
723 break;
724#else
725 return -EAFNOSUPPORT;
726#endif
727
728 default:
729 return -EINVAL;
730 };
731
732 return verify_policy_dir(p->dir);
733}
734
Trent Jaegerdf718372005-12-13 23:12:27 -0800735static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
736{
737 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
738 struct xfrm_user_sec_ctx *uctx;
739
740 if (!rt)
741 return 0;
742
743 uctx = RTA_DATA(rt);
744 return security_xfrm_policy_alloc(pol, uctx);
745}
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
748 int nr)
749{
750 int i;
751
752 xp->xfrm_nr = nr;
753 for (i = 0; i < nr; i++, ut++) {
754 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
755
756 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
757 memcpy(&t->saddr, &ut->saddr,
758 sizeof(xfrm_address_t));
759 t->reqid = ut->reqid;
760 t->mode = ut->mode;
761 t->share = ut->share;
762 t->optional = ut->optional;
763 t->aalgos = ut->aalgos;
764 t->ealgos = ut->ealgos;
765 t->calgos = ut->calgos;
766 }
767}
768
769static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
770{
771 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
772 struct xfrm_user_tmpl *utmpl;
773 int nr;
774
775 if (!rt) {
776 pol->xfrm_nr = 0;
777 } else {
778 nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
779
780 if (nr > XFRM_MAX_DEPTH)
781 return -EINVAL;
782
783 copy_templates(pol, RTA_DATA(rt), nr);
784 }
785 return 0;
786}
787
788static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
789{
790 xp->priority = p->priority;
791 xp->index = p->index;
792 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
793 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
794 xp->action = p->action;
795 xp->flags = p->flags;
796 xp->family = p->sel.family;
797 /* XXX xp->share = p->share; */
798}
799
800static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
801{
802 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
803 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
804 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
805 p->priority = xp->priority;
806 p->index = xp->index;
807 p->sel.family = xp->family;
808 p->dir = dir;
809 p->action = xp->action;
810 p->flags = xp->flags;
811 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
812}
813
814static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
815{
816 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
817 int err;
818
819 if (!xp) {
820 *errp = -ENOMEM;
821 return NULL;
822 }
823
824 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -0800825
826 if (!(err = copy_from_user_tmpl(xp, xfrma)))
827 err = copy_from_user_sec_ctx(xp, xfrma);
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (err) {
830 *errp = err;
831 kfree(xp);
832 xp = NULL;
833 }
834
835 return xp;
836}
837
838static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
839{
840 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
841 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700842 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 int err;
844 int excl;
845
846 err = verify_newpolicy_info(p);
847 if (err)
848 return err;
Trent Jaegerdf718372005-12-13 23:12:27 -0800849 err = verify_sec_ctx_len((struct rtattr **)xfrma);
850 if (err)
851 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Trent Jaegerdf718372005-12-13 23:12:27 -0800853 xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (!xp)
855 return err;
856
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700857 /* shouldnt excl be based on nlh flags??
858 * Aha! this is anti-netlink really i.e more pfkey derived
859 * in netlink excl is a flag and you wouldnt need
860 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
862 err = xfrm_policy_insert(p->dir, xp, excl);
863 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -0800864 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 kfree(xp);
866 return err;
867 }
868
Herbert Xuf60f6b82005-06-18 22:44:37 -0700869 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700870 c.seq = nlh->nlmsg_seq;
871 c.pid = nlh->nlmsg_pid;
872 km_policy_notify(xp, p->dir, &c);
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 xfrm_pol_put(xp);
875
876 return 0;
877}
878
879static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
880{
881 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
882 int i;
883
884 if (xp->xfrm_nr == 0)
885 return 0;
886
887 for (i = 0; i < xp->xfrm_nr; i++) {
888 struct xfrm_user_tmpl *up = &vec[i];
889 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
890
891 memcpy(&up->id, &kp->id, sizeof(up->id));
892 up->family = xp->family;
893 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
894 up->reqid = kp->reqid;
895 up->mode = kp->mode;
896 up->share = kp->share;
897 up->optional = kp->optional;
898 up->aalgos = kp->aalgos;
899 up->ealgos = kp->ealgos;
900 up->calgos = kp->calgos;
901 }
902 RTA_PUT(skb, XFRMA_TMPL,
903 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
904 vec);
905
906 return 0;
907
908rtattr_failure:
909 return -1;
910}
911
Trent Jaegerdf718372005-12-13 23:12:27 -0800912static int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
913{
914 if (xp->security) {
915 int ctx_size = sizeof(struct xfrm_sec_ctx) +
916 xp->security->ctx_len;
917 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
918 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
919
920 uctx->exttype = XFRMA_SEC_CTX;
921 uctx->len = ctx_size;
922 uctx->ctx_doi = xp->security->ctx_doi;
923 uctx->ctx_alg = xp->security->ctx_alg;
924 uctx->ctx_len = xp->security->ctx_len;
925 memcpy(uctx + 1, xp->security->ctx_str, xp->security->ctx_len);
926 }
927 return 0;
928
929 rtattr_failure:
930 return -1;
931}
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
934{
935 struct xfrm_dump_info *sp = ptr;
936 struct xfrm_userpolicy_info *p;
937 struct sk_buff *in_skb = sp->in_skb;
938 struct sk_buff *skb = sp->out_skb;
939 struct nlmsghdr *nlh;
940 unsigned char *b = skb->tail;
941
942 if (sp->this_idx < sp->start_idx)
943 goto out;
944
945 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
946 sp->nlmsg_seq,
947 XFRM_MSG_NEWPOLICY, sizeof(*p));
948 p = NLMSG_DATA(nlh);
949 nlh->nlmsg_flags = sp->nlmsg_flags;
950
951 copy_to_user_policy(xp, p, dir);
952 if (copy_to_user_tmpl(xp, skb) < 0)
953 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -0800954 if (copy_to_user_sec_ctx(xp, skb))
955 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 nlh->nlmsg_len = skb->tail - b;
958out:
959 sp->this_idx++;
960 return 0;
961
962nlmsg_failure:
963 skb_trim(skb, b - skb->data);
964 return -1;
965}
966
967static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
968{
969 struct xfrm_dump_info info;
970
971 info.in_skb = cb->skb;
972 info.out_skb = skb;
973 info.nlmsg_seq = cb->nlh->nlmsg_seq;
974 info.nlmsg_flags = NLM_F_MULTI;
975 info.this_idx = 0;
976 info.start_idx = cb->args[0];
977 (void) xfrm_policy_walk(dump_one_policy, &info);
978 cb->args[0] = info.this_idx;
979
980 return skb->len;
981}
982
983static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
984 struct xfrm_policy *xp,
985 int dir, u32 seq)
986{
987 struct xfrm_dump_info info;
988 struct sk_buff *skb;
989
990 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
991 if (!skb)
992 return ERR_PTR(-ENOMEM);
993
994 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
995 info.in_skb = in_skb;
996 info.out_skb = skb;
997 info.nlmsg_seq = seq;
998 info.nlmsg_flags = 0;
999 info.this_idx = info.start_idx = 0;
1000
1001 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1002 kfree_skb(skb);
1003 return NULL;
1004 }
1005
1006 return skb;
1007}
1008
1009static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1010{
1011 struct xfrm_policy *xp;
1012 struct xfrm_userpolicy_id *p;
1013 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001014 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 int delete;
1016
1017 p = NLMSG_DATA(nlh);
1018 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1019
1020 err = verify_policy_dir(p->dir);
1021 if (err)
1022 return err;
1023
1024 if (p->index)
1025 xp = xfrm_policy_byid(p->dir, p->index, delete);
Trent Jaegerdf718372005-12-13 23:12:27 -08001026 else {
1027 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1028 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1029 struct xfrm_policy tmp;
1030
1031 err = verify_sec_ctx_len(rtattrs);
1032 if (err)
1033 return err;
1034
1035 memset(&tmp, 0, sizeof(struct xfrm_policy));
1036 if (rt) {
1037 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1038
1039 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1040 return err;
1041 }
1042 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, delete);
1043 security_xfrm_policy_free(&tmp);
1044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (xp == NULL)
1046 return -ENOENT;
1047
1048 if (!delete) {
1049 struct sk_buff *resp_skb;
1050
1051 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1052 if (IS_ERR(resp_skb)) {
1053 err = PTR_ERR(resp_skb);
1054 } else {
1055 err = netlink_unicast(xfrm_nl, resp_skb,
1056 NETLINK_CB(skb).pid,
1057 MSG_DONTWAIT);
1058 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001059 } else {
David S. Miller6f68dc32006-06-08 23:58:52 -07001060 if ((err = security_xfrm_policy_delete(xp)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001061 goto out;
Herbert Xue7443892005-06-18 22:44:18 -07001062 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001063 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001064 c.seq = nlh->nlmsg_seq;
1065 c.pid = nlh->nlmsg_pid;
1066 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068
1069 xfrm_pol_put(xp);
1070
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001071out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return err;
1073}
1074
1075static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1076{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001077 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1079
1080 xfrm_state_flush(p->proto);
Herbert Xubf088672005-06-18 22:44:00 -07001081 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001082 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001083 c.seq = nlh->nlmsg_seq;
1084 c.pid = nlh->nlmsg_pid;
1085 km_state_notify(NULL, &c);
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return 0;
1088}
1089
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001090
1091static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1092{
1093 struct xfrm_aevent_id *id;
1094 struct nlmsghdr *nlh;
1095 struct xfrm_lifetime_cur ltime;
1096 unsigned char *b = skb->tail;
1097
1098 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1099 id = NLMSG_DATA(nlh);
1100 nlh->nlmsg_flags = 0;
1101
1102 id->sa_id.daddr = x->id.daddr;
1103 id->sa_id.spi = x->id.spi;
1104 id->sa_id.family = x->props.family;
1105 id->sa_id.proto = x->id.proto;
1106 id->flags = c->data.aevent;
1107
1108 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1109
1110 ltime.bytes = x->curlft.bytes;
1111 ltime.packets = x->curlft.packets;
1112 ltime.add_time = x->curlft.add_time;
1113 ltime.use_time = x->curlft.use_time;
1114
1115 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
1116
1117 if (id->flags&XFRM_AE_RTHR) {
1118 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1119 }
1120
1121 if (id->flags&XFRM_AE_ETHR) {
1122 u32 etimer = x->replay_maxage*10/HZ;
1123 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1124 }
1125
1126 nlh->nlmsg_len = skb->tail - b;
1127 return skb->len;
1128
1129rtattr_failure:
1130nlmsg_failure:
1131 skb_trim(skb, b - skb->data);
1132 return -1;
1133}
1134
1135static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1136{
1137 struct xfrm_state *x;
1138 struct sk_buff *r_skb;
1139 int err;
1140 struct km_event c;
1141 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1142 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1143 struct xfrm_usersa_id *id = &p->sa_id;
1144
1145 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1146 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1147
1148 if (p->flags&XFRM_AE_RTHR)
1149 len+=RTA_SPACE(sizeof(u32));
1150
1151 if (p->flags&XFRM_AE_ETHR)
1152 len+=RTA_SPACE(sizeof(u32));
1153
1154 r_skb = alloc_skb(len, GFP_ATOMIC);
1155 if (r_skb == NULL)
1156 return -ENOMEM;
1157
1158 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1159 if (x == NULL) {
1160 kfree(r_skb);
1161 return -ESRCH;
1162 }
1163
1164 /*
1165 * XXX: is this lock really needed - none of the other
1166 * gets lock (the concern is things getting updated
1167 * while we are still reading) - jhs
1168 */
1169 spin_lock_bh(&x->lock);
1170 c.data.aevent = p->flags;
1171 c.seq = nlh->nlmsg_seq;
1172 c.pid = nlh->nlmsg_pid;
1173
1174 if (build_aevent(r_skb, x, &c) < 0)
1175 BUG();
1176 err = netlink_unicast(xfrm_nl, r_skb,
1177 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1178 spin_unlock_bh(&x->lock);
1179 xfrm_state_put(x);
1180 return err;
1181}
1182
1183static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1184{
1185 struct xfrm_state *x;
1186 struct km_event c;
1187 int err = - EINVAL;
1188 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1189 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1190 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1191
1192 if (!lt && !rp)
1193 return err;
1194
1195 /* pedantic mode - thou shalt sayeth replaceth */
1196 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1197 return err;
1198
1199 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1200 if (x == NULL)
1201 return -ESRCH;
1202
1203 if (x->km.state != XFRM_STATE_VALID)
1204 goto out;
1205
1206 spin_lock_bh(&x->lock);
1207 err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
1208 spin_unlock_bh(&x->lock);
1209 if (err < 0)
1210 goto out;
1211
1212 c.event = nlh->nlmsg_type;
1213 c.seq = nlh->nlmsg_seq;
1214 c.pid = nlh->nlmsg_pid;
1215 c.data.aevent = XFRM_AE_CU;
1216 km_state_notify(x, &c);
1217 err = 0;
1218out:
1219 xfrm_state_put(x);
1220 return err;
1221}
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1224{
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001225struct km_event c;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 xfrm_policy_flush();
Herbert Xuf60f6b82005-06-18 22:44:37 -07001228 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001229 c.seq = nlh->nlmsg_seq;
1230 c.pid = nlh->nlmsg_pid;
1231 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return 0;
1233}
1234
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001235static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1236{
1237 struct xfrm_policy *xp;
1238 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1239 struct xfrm_userpolicy_info *p = &up->pol;
1240 int err = -ENOENT;
1241
1242 if (p->index)
1243 xp = xfrm_policy_byid(p->dir, p->index, 0);
1244 else {
1245 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1246 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1247 struct xfrm_policy tmp;
1248
1249 err = verify_sec_ctx_len(rtattrs);
1250 if (err)
1251 return err;
1252
1253 memset(&tmp, 0, sizeof(struct xfrm_policy));
1254 if (rt) {
1255 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1256
1257 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1258 return err;
1259 }
1260 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, 0);
1261 security_xfrm_policy_free(&tmp);
1262 }
1263
1264 if (xp == NULL)
1265 return err;
1266 read_lock(&xp->lock);
1267 if (xp->dead) {
1268 read_unlock(&xp->lock);
1269 goto out;
1270 }
1271
1272 read_unlock(&xp->lock);
1273 err = 0;
1274 if (up->hard) {
1275 xfrm_policy_delete(xp, p->dir);
1276 } else {
1277 // reset the timers here?
1278 printk("Dont know what to do with soft policy expire\n");
1279 }
1280 km_policy_expired(xp, p->dir, up->hard, current->pid);
1281
1282out:
1283 xfrm_pol_put(xp);
1284 return err;
1285}
1286
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001287static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1288{
1289 struct xfrm_state *x;
1290 int err;
1291 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1292 struct xfrm_usersa_info *p = &ue->state;
1293
1294 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1295 err = -ENOENT;
1296
1297 if (x == NULL)
1298 return err;
1299
1300 err = -EINVAL;
1301
1302 spin_lock_bh(&x->lock);
1303 if (x->km.state != XFRM_STATE_VALID)
1304 goto out;
1305 km_state_expired(x, ue->hard, current->pid);
1306
1307 if (ue->hard)
1308 __xfrm_state_delete(x);
1309out:
1310 spin_unlock_bh(&x->lock);
1311 xfrm_state_put(x);
1312 return err;
1313}
1314
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001315static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1316{
1317 struct xfrm_policy *xp;
1318 struct xfrm_user_tmpl *ut;
1319 int i;
1320 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1321
1322 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1323 struct xfrm_state *x = xfrm_state_alloc();
1324 int err = -ENOMEM;
1325
1326 if (!x)
1327 return err;
1328
1329 err = verify_newpolicy_info(&ua->policy);
1330 if (err) {
1331 printk("BAD policy passed\n");
1332 kfree(x);
1333 return err;
1334 }
1335
1336 /* build an XP */
1337 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err); if (!xp) {
1338 kfree(x);
1339 return err;
1340 }
1341
1342 memcpy(&x->id, &ua->id, sizeof(ua->id));
1343 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1344 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1345
1346 ut = RTA_DATA(rt);
1347 /* extract the templates and for each call km_key */
1348 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1349 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1350 memcpy(&x->id, &t->id, sizeof(x->id));
1351 x->props.mode = t->mode;
1352 x->props.reqid = t->reqid;
1353 x->props.family = ut->family;
1354 t->aalgos = ua->aalgos;
1355 t->ealgos = ua->ealgos;
1356 t->calgos = ua->calgos;
1357 err = km_query(x, t, xp);
1358
1359 }
1360
1361 kfree(x);
1362 kfree(xp);
1363
1364 return 0;
1365}
1366
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001367
Thomas Graf492b5582005-05-03 14:26:40 -07001368#define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1369
1370static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1371 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1372 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1373 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1374 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1375 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1376 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1377 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001378 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001379 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001380 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1381 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001382 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001383 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1384 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001385 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1386 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387};
1388
Thomas Graf492b5582005-05-03 14:26:40 -07001389#undef XMSGSIZE
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391static struct xfrm_link {
1392 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1393 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001394} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1395 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1396 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1397 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1398 .dump = xfrm_dump_sa },
1399 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1400 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1401 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1402 .dump = xfrm_dump_policy },
1403 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001404 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001405 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001406 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1407 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001408 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001409 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1410 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001411 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1412 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413};
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1416{
1417 struct rtattr *xfrma[XFRMA_MAX];
1418 struct xfrm_link *link;
1419 int type, min_len;
1420
1421 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1422 return 0;
1423
1424 type = nlh->nlmsg_type;
1425
1426 /* A control message: ignore them */
1427 if (type < XFRM_MSG_BASE)
1428 return 0;
1429
1430 /* Unknown message: reply with EINVAL */
1431 if (type > XFRM_MSG_MAX)
1432 goto err_einval;
1433
1434 type -= XFRM_MSG_BASE;
1435 link = &xfrm_dispatch[type];
1436
1437 /* All operations require privileges, even GET */
Darrel Goeddelc7bdb542006-06-27 13:26:11 -07001438 if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 *errp = -EPERM;
1440 return -1;
1441 }
1442
Thomas Graf492b5582005-05-03 14:26:40 -07001443 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1444 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1445 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (link->dump == NULL)
1447 goto err_einval;
1448
1449 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
Thomas Grafa8f74b22005-11-10 02:25:52 +01001450 link->dump, NULL)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return -1;
1452 }
Thomas Graf88fc2c82005-11-10 02:25:54 +01001453
1454 netlink_queue_skip(nlh, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return -1;
1456 }
1457
1458 memset(xfrma, 0, sizeof(xfrma));
1459
1460 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1461 goto err_einval;
1462
1463 if (nlh->nlmsg_len > min_len) {
1464 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1465 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1466
1467 while (RTA_OK(attr, attrlen)) {
1468 unsigned short flavor = attr->rta_type;
1469 if (flavor) {
1470 if (flavor > XFRMA_MAX)
1471 goto err_einval;
1472 xfrma[flavor - 1] = attr;
1473 }
1474 attr = RTA_NEXT(attr, attrlen);
1475 }
1476 }
1477
1478 if (link->doit == NULL)
1479 goto err_einval;
1480 *errp = link->doit(skb, nlh, (void **) &xfrma);
1481
1482 return *errp;
1483
1484err_einval:
1485 *errp = -EINVAL;
1486 return -1;
1487}
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489static void xfrm_netlink_rcv(struct sock *sk, int len)
1490{
Thomas Graf88fc2c82005-11-10 02:25:54 +01001491 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 do {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001494 mutex_lock(&xfrm_cfg_mutex);
Thomas Graf88fc2c82005-11-10 02:25:54 +01001495 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001496 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001498 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001501static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
1503 struct xfrm_user_expire *ue;
1504 struct nlmsghdr *nlh;
1505 unsigned char *b = skb->tail;
1506
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001507 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 sizeof(*ue));
1509 ue = NLMSG_DATA(nlh);
1510 nlh->nlmsg_flags = 0;
1511
1512 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001513 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 nlh->nlmsg_len = skb->tail - b;
1516 return skb->len;
1517
1518nlmsg_failure:
1519 skb_trim(skb, b - skb->data);
1520 return -1;
1521}
1522
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001523static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
1525 struct sk_buff *skb;
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001526 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001528 skb = alloc_skb(len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (skb == NULL)
1530 return -ENOMEM;
1531
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001532 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 BUG();
1534
Patrick McHardyac6d4392005-08-14 19:29:52 -07001535 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1536 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001539static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1540{
1541 struct sk_buff *skb;
1542 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1543
1544 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1545 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1546 skb = alloc_skb(len, GFP_ATOMIC);
1547 if (skb == NULL)
1548 return -ENOMEM;
1549
1550 if (build_aevent(skb, x, c) < 0)
1551 BUG();
1552
1553 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1554 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1555}
1556
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001557static int xfrm_notify_sa_flush(struct km_event *c)
1558{
1559 struct xfrm_usersa_flush *p;
1560 struct nlmsghdr *nlh;
1561 struct sk_buff *skb;
1562 unsigned char *b;
1563 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1564
1565 skb = alloc_skb(len, GFP_ATOMIC);
1566 if (skb == NULL)
1567 return -ENOMEM;
1568 b = skb->tail;
1569
1570 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1571 XFRM_MSG_FLUSHSA, sizeof(*p));
1572 nlh->nlmsg_flags = 0;
1573
1574 p = NLMSG_DATA(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07001575 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001576
1577 nlh->nlmsg_len = skb->tail - b;
1578
Patrick McHardyac6d4392005-08-14 19:29:52 -07001579 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1580 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001581
1582nlmsg_failure:
1583 kfree_skb(skb);
1584 return -1;
1585}
1586
1587static int inline xfrm_sa_len(struct xfrm_state *x)
1588{
Herbert Xu0603eac2005-06-18 22:54:36 -07001589 int l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001590 if (x->aalg)
1591 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1592 if (x->ealg)
1593 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1594 if (x->calg)
1595 l += RTA_SPACE(sizeof(*x->calg));
1596 if (x->encap)
1597 l += RTA_SPACE(sizeof(*x->encap));
1598
1599 return l;
1600}
1601
1602static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1603{
1604 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07001605 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001606 struct nlmsghdr *nlh;
1607 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001608 unsigned char *b;
1609 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07001610 int headlen;
1611
1612 headlen = sizeof(*p);
1613 if (c->event == XFRM_MSG_DELSA) {
1614 len += RTA_SPACE(headlen);
1615 headlen = sizeof(*id);
1616 }
1617 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001618
1619 skb = alloc_skb(len, GFP_ATOMIC);
1620 if (skb == NULL)
1621 return -ENOMEM;
1622 b = skb->tail;
1623
Herbert Xu0603eac2005-06-18 22:54:36 -07001624 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001625 nlh->nlmsg_flags = 0;
1626
1627 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07001628 if (c->event == XFRM_MSG_DELSA) {
1629 id = NLMSG_DATA(nlh);
1630 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1631 id->spi = x->id.spi;
1632 id->family = x->props.family;
1633 id->proto = x->id.proto;
1634
1635 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1636 }
1637
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001638 copy_to_user_state(x, p);
1639
1640 if (x->aalg)
1641 RTA_PUT(skb, XFRMA_ALG_AUTH,
1642 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1643 if (x->ealg)
1644 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1645 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1646 if (x->calg)
1647 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1648
1649 if (x->encap)
1650 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1651
1652 nlh->nlmsg_len = skb->tail - b;
1653
Patrick McHardyac6d4392005-08-14 19:29:52 -07001654 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1655 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001656
1657nlmsg_failure:
1658rtattr_failure:
1659 kfree_skb(skb);
1660 return -1;
1661}
1662
1663static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1664{
1665
1666 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001667 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001668 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001669 case XFRM_MSG_NEWAE:
1670 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001671 case XFRM_MSG_DELSA:
1672 case XFRM_MSG_UPDSA:
1673 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001674 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001675 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001676 return xfrm_notify_sa_flush(c);
1677 default:
1678 printk("xfrm_user: Unknown SA event %d\n", c->event);
1679 break;
1680 }
1681
1682 return 0;
1683
1684}
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1687 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1688 int dir)
1689{
1690 struct xfrm_user_acquire *ua;
1691 struct nlmsghdr *nlh;
1692 unsigned char *b = skb->tail;
1693 __u32 seq = xfrm_get_acqseq();
1694
1695 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1696 sizeof(*ua));
1697 ua = NLMSG_DATA(nlh);
1698 nlh->nlmsg_flags = 0;
1699
1700 memcpy(&ua->id, &x->id, sizeof(ua->id));
1701 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1702 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1703 copy_to_user_policy(xp, &ua->policy, dir);
1704 ua->aalgos = xt->aalgos;
1705 ua->ealgos = xt->ealgos;
1706 ua->calgos = xt->calgos;
1707 ua->seq = x->km.seq = seq;
1708
1709 if (copy_to_user_tmpl(xp, skb) < 0)
1710 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001711 if (copy_to_user_sec_ctx(xp, skb))
1712 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 nlh->nlmsg_len = skb->tail - b;
1715 return skb->len;
1716
1717nlmsg_failure:
1718 skb_trim(skb, b - skb->data);
1719 return -1;
1720}
1721
1722static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1723 struct xfrm_policy *xp, int dir)
1724{
1725 struct sk_buff *skb;
1726 size_t len;
1727
1728 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1729 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
Trent Jaegerdf718372005-12-13 23:12:27 -08001730 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 skb = alloc_skb(len, GFP_ATOMIC);
1732 if (skb == NULL)
1733 return -ENOMEM;
1734
1735 if (build_acquire(skb, x, xt, xp, dir) < 0)
1736 BUG();
1737
Patrick McHardyac6d4392005-08-14 19:29:52 -07001738 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
1739 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740}
1741
1742/* User gives us xfrm_user_policy_info followed by an array of 0
1743 * or more templates.
1744 */
1745static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
1746 u8 *data, int len, int *dir)
1747{
1748 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1749 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1750 struct xfrm_policy *xp;
1751 int nr;
1752
1753 switch (family) {
1754 case AF_INET:
1755 if (opt != IP_XFRM_POLICY) {
1756 *dir = -EOPNOTSUPP;
1757 return NULL;
1758 }
1759 break;
1760#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1761 case AF_INET6:
1762 if (opt != IPV6_XFRM_POLICY) {
1763 *dir = -EOPNOTSUPP;
1764 return NULL;
1765 }
1766 break;
1767#endif
1768 default:
1769 *dir = -EINVAL;
1770 return NULL;
1771 }
1772
1773 *dir = -EINVAL;
1774
1775 if (len < sizeof(*p) ||
1776 verify_newpolicy_info(p))
1777 return NULL;
1778
1779 nr = ((len - sizeof(*p)) / sizeof(*ut));
1780 if (nr > XFRM_MAX_DEPTH)
1781 return NULL;
1782
Herbert Xua4f1bac2005-07-26 15:43:17 -07001783 if (p->dir > XFRM_POLICY_OUT)
1784 return NULL;
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 xp = xfrm_policy_alloc(GFP_KERNEL);
1787 if (xp == NULL) {
1788 *dir = -ENOBUFS;
1789 return NULL;
1790 }
1791
1792 copy_from_user_policy(xp, p);
1793 copy_templates(xp, ut, nr);
1794
1795 *dir = p->dir;
1796
1797 return xp;
1798}
1799
1800static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001801 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
1803 struct xfrm_user_polexpire *upe;
1804 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001805 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 unsigned char *b = skb->tail;
1807
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001808 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 upe = NLMSG_DATA(nlh);
1810 nlh->nlmsg_flags = 0;
1811
1812 copy_to_user_policy(xp, &upe->pol, dir);
1813 if (copy_to_user_tmpl(xp, skb) < 0)
1814 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001815 if (copy_to_user_sec_ctx(xp, skb))
1816 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 upe->hard = !!hard;
1818
1819 nlh->nlmsg_len = skb->tail - b;
1820 return skb->len;
1821
1822nlmsg_failure:
1823 skb_trim(skb, b - skb->data);
1824 return -1;
1825}
1826
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001827static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
1829 struct sk_buff *skb;
1830 size_t len;
1831
1832 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1833 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
Trent Jaegerdf718372005-12-13 23:12:27 -08001834 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 skb = alloc_skb(len, GFP_ATOMIC);
1836 if (skb == NULL)
1837 return -ENOMEM;
1838
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001839 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 BUG();
1841
Patrick McHardyac6d4392005-08-14 19:29:52 -07001842 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1843 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001846static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
1847{
1848 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07001849 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001850 struct nlmsghdr *nlh;
1851 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001852 unsigned char *b;
1853 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07001854 int headlen;
1855
1856 headlen = sizeof(*p);
1857 if (c->event == XFRM_MSG_DELPOLICY) {
1858 len += RTA_SPACE(headlen);
1859 headlen = sizeof(*id);
1860 }
1861 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001862
1863 skb = alloc_skb(len, GFP_ATOMIC);
1864 if (skb == NULL)
1865 return -ENOMEM;
1866 b = skb->tail;
1867
Herbert Xu0603eac2005-06-18 22:54:36 -07001868 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001869
1870 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07001871 if (c->event == XFRM_MSG_DELPOLICY) {
1872 id = NLMSG_DATA(nlh);
1873 memset(id, 0, sizeof(*id));
1874 id->dir = dir;
1875 if (c->data.byid)
1876 id->index = xp->index;
1877 else
1878 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
1879
1880 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
1881 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001882
1883 nlh->nlmsg_flags = 0;
1884
1885 copy_to_user_policy(xp, p, dir);
1886 if (copy_to_user_tmpl(xp, skb) < 0)
1887 goto nlmsg_failure;
1888
1889 nlh->nlmsg_len = skb->tail - b;
1890
Patrick McHardyac6d4392005-08-14 19:29:52 -07001891 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1892 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001893
1894nlmsg_failure:
Herbert Xu0603eac2005-06-18 22:54:36 -07001895rtattr_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001896 kfree_skb(skb);
1897 return -1;
1898}
1899
1900static int xfrm_notify_policy_flush(struct km_event *c)
1901{
1902 struct nlmsghdr *nlh;
1903 struct sk_buff *skb;
1904 unsigned char *b;
1905 int len = NLMSG_LENGTH(0);
1906
1907 skb = alloc_skb(len, GFP_ATOMIC);
1908 if (skb == NULL)
1909 return -ENOMEM;
1910 b = skb->tail;
1911
1912
1913 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
1914
1915 nlh->nlmsg_len = skb->tail - b;
1916
Patrick McHardyac6d4392005-08-14 19:29:52 -07001917 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1918 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001919
1920nlmsg_failure:
1921 kfree_skb(skb);
1922 return -1;
1923}
1924
1925static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1926{
1927
1928 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001929 case XFRM_MSG_NEWPOLICY:
1930 case XFRM_MSG_UPDPOLICY:
1931 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001932 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001933 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001934 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001935 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001936 return xfrm_exp_policy_notify(xp, dir, c);
1937 default:
1938 printk("xfrm_user: Unknown Policy event %d\n", c->event);
1939 }
1940
1941 return 0;
1942
1943}
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945static struct xfrm_mgr netlink_mgr = {
1946 .id = "netlink",
1947 .notify = xfrm_send_state_notify,
1948 .acquire = xfrm_send_acquire,
1949 .compile_policy = xfrm_compile_policy,
1950 .notify_policy = xfrm_send_policy_notify,
1951};
1952
1953static int __init xfrm_user_init(void)
1954{
Patrick McHardybe336902006-03-20 22:40:54 -08001955 struct sock *nlsk;
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 printk(KERN_INFO "Initializing IPsec netlink socket\n");
1958
Patrick McHardybe336902006-03-20 22:40:54 -08001959 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
1960 xfrm_netlink_rcv, THIS_MODULE);
1961 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08001963 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 xfrm_register_km(&netlink_mgr);
1966
1967 return 0;
1968}
1969
1970static void __exit xfrm_user_exit(void)
1971{
Patrick McHardybe336902006-03-20 22:40:54 -08001972 struct sock *nlsk = xfrm_nl;
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08001975 rcu_assign_pointer(xfrm_nl, NULL);
1976 synchronize_rcu();
1977 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
1979
1980module_init(xfrm_user_init);
1981module_exit(xfrm_user_exit);
1982MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001983MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001984