[XFRM] POLICY: sub policy support.
[deliverable/linux.git] / net / xfrm / xfrm_policy.c
CommitLineData
1da177e4
LT
1/*
2 * xfrm_policy.c
3 *
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * Kazunori MIYAZAWA @USAGI
10 * YOSHIFUJI Hideaki
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
df71837d 13 *
1da177e4
LT
14 */
15
1da177e4
LT
16#include <linux/slab.h>
17#include <linux/kmod.h>
18#include <linux/list.h>
19#include <linux/spinlock.h>
20#include <linux/workqueue.h>
21#include <linux/notifier.h>
22#include <linux/netdevice.h>
eb9c7ebe 23#include <linux/netfilter.h>
1da177e4
LT
24#include <linux/module.h>
25#include <net/xfrm.h>
26#include <net/ip.h>
27
4a3e2f71
AV
28DEFINE_MUTEX(xfrm_cfg_mutex);
29EXPORT_SYMBOL(xfrm_cfg_mutex);
1da177e4
LT
30
31static DEFINE_RWLOCK(xfrm_policy_lock);
32
33struct xfrm_policy *xfrm_policy_list[XFRM_POLICY_MAX*2];
34EXPORT_SYMBOL(xfrm_policy_list);
4e81bb83
MN
35#ifdef CONFIG_XFRM_SUB_POLICY
36struct xfrm_policy *xfrm_policy_list_sub[XFRM_POLICY_MAX*2];
37EXPORT_SYMBOL(xfrm_policy_list_sub);
38
39#define XFRM_POLICY_LISTS(type) \
40 ((type == XFRM_POLICY_TYPE_SUB) ? xfrm_policy_list_sub : \
41 xfrm_policy_list)
42#define XFRM_POLICY_LISTHEAD(type, dir) \
43 ((type == XFRM_POLICY_TYPE_SUB) ? xfrm_policy_list_sub[dir] : \
44 xfrm_policy_list[dir])
45#define XFRM_POLICY_LISTHEADP(type, dir) \
46 ((type == XFRM_POLICY_TYPE_SUB) ? &xfrm_policy_list_sub[dir] : \
47 &xfrm_policy_list[dir])
48#else
49#define XFRM_POLICY_LISTS(type) xfrm_policy_list
50#define XFRM_POLICY_LISTHEAD(type, dif) xfrm_policy_list[dir]
51#define XFRM_POLICY_LISTHEADP(type, dif) &xfrm_policy_list[dir]
52#endif
1da177e4
LT
53
54static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
55static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
56
ba89966c 57static kmem_cache_t *xfrm_dst_cache __read_mostly;
1da177e4
LT
58
59static struct work_struct xfrm_policy_gc_work;
60static struct list_head xfrm_policy_gc_list =
61 LIST_HEAD_INIT(xfrm_policy_gc_list);
62static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
63
64static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
65static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
546be240
HX
66static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family);
67static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo);
1da177e4
LT
68
69int xfrm_register_type(struct xfrm_type *type, unsigned short family)
70{
546be240
HX
71 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
72 struct xfrm_type **typemap;
1da177e4
LT
73 int err = 0;
74
75 if (unlikely(afinfo == NULL))
76 return -EAFNOSUPPORT;
77 typemap = afinfo->type_map;
78
546be240
HX
79 if (likely(typemap[type->proto] == NULL))
80 typemap[type->proto] = type;
1da177e4
LT
81 else
82 err = -EEXIST;
546be240 83 xfrm_policy_unlock_afinfo(afinfo);
1da177e4
LT
84 return err;
85}
86EXPORT_SYMBOL(xfrm_register_type);
87
88int xfrm_unregister_type(struct xfrm_type *type, unsigned short family)
89{
546be240
HX
90 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
91 struct xfrm_type **typemap;
1da177e4
LT
92 int err = 0;
93
94 if (unlikely(afinfo == NULL))
95 return -EAFNOSUPPORT;
96 typemap = afinfo->type_map;
97
546be240 98 if (unlikely(typemap[type->proto] != type))
1da177e4
LT
99 err = -ENOENT;
100 else
546be240
HX
101 typemap[type->proto] = NULL;
102 xfrm_policy_unlock_afinfo(afinfo);
1da177e4
LT
103 return err;
104}
105EXPORT_SYMBOL(xfrm_unregister_type);
106
107struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
108{
109 struct xfrm_policy_afinfo *afinfo;
546be240 110 struct xfrm_type **typemap;
1da177e4
LT
111 struct xfrm_type *type;
112 int modload_attempted = 0;
113
114retry:
115 afinfo = xfrm_policy_get_afinfo(family);
116 if (unlikely(afinfo == NULL))
117 return NULL;
118 typemap = afinfo->type_map;
119
546be240 120 type = typemap[proto];
1da177e4
LT
121 if (unlikely(type && !try_module_get(type->owner)))
122 type = NULL;
1da177e4
LT
123 if (!type && !modload_attempted) {
124 xfrm_policy_put_afinfo(afinfo);
125 request_module("xfrm-type-%d-%d",
126 (int) family, (int) proto);
127 modload_attempted = 1;
128 goto retry;
129 }
130
131 xfrm_policy_put_afinfo(afinfo);
132 return type;
133}
1da177e4
LT
134
135int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
136 unsigned short family)
137{
138 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
139 int err = 0;
140
141 if (unlikely(afinfo == NULL))
142 return -EAFNOSUPPORT;
143
144 if (likely(afinfo->dst_lookup != NULL))
145 err = afinfo->dst_lookup(dst, fl);
146 else
147 err = -EINVAL;
148 xfrm_policy_put_afinfo(afinfo);
149 return err;
150}
151EXPORT_SYMBOL(xfrm_dst_lookup);
152
153void xfrm_put_type(struct xfrm_type *type)
154{
155 module_put(type->owner);
156}
157
b59f45d0
HX
158int xfrm_register_mode(struct xfrm_mode *mode, int family)
159{
160 struct xfrm_policy_afinfo *afinfo;
161 struct xfrm_mode **modemap;
162 int err;
163
164 if (unlikely(mode->encap >= XFRM_MODE_MAX))
165 return -EINVAL;
166
167 afinfo = xfrm_policy_lock_afinfo(family);
168 if (unlikely(afinfo == NULL))
169 return -EAFNOSUPPORT;
170
171 err = -EEXIST;
172 modemap = afinfo->mode_map;
173 if (likely(modemap[mode->encap] == NULL)) {
174 modemap[mode->encap] = mode;
175 err = 0;
176 }
177
178 xfrm_policy_unlock_afinfo(afinfo);
179 return err;
180}
181EXPORT_SYMBOL(xfrm_register_mode);
182
183int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
184{
185 struct xfrm_policy_afinfo *afinfo;
186 struct xfrm_mode **modemap;
187 int err;
188
189 if (unlikely(mode->encap >= XFRM_MODE_MAX))
190 return -EINVAL;
191
192 afinfo = xfrm_policy_lock_afinfo(family);
193 if (unlikely(afinfo == NULL))
194 return -EAFNOSUPPORT;
195
196 err = -ENOENT;
197 modemap = afinfo->mode_map;
198 if (likely(modemap[mode->encap] == mode)) {
199 modemap[mode->encap] = NULL;
200 err = 0;
201 }
202
203 xfrm_policy_unlock_afinfo(afinfo);
204 return err;
205}
206EXPORT_SYMBOL(xfrm_unregister_mode);
207
208struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
209{
210 struct xfrm_policy_afinfo *afinfo;
211 struct xfrm_mode *mode;
212 int modload_attempted = 0;
213
214 if (unlikely(encap >= XFRM_MODE_MAX))
215 return NULL;
216
217retry:
218 afinfo = xfrm_policy_get_afinfo(family);
219 if (unlikely(afinfo == NULL))
220 return NULL;
221
222 mode = afinfo->mode_map[encap];
223 if (unlikely(mode && !try_module_get(mode->owner)))
224 mode = NULL;
225 if (!mode && !modload_attempted) {
226 xfrm_policy_put_afinfo(afinfo);
227 request_module("xfrm-mode-%d-%d", family, encap);
228 modload_attempted = 1;
229 goto retry;
230 }
231
232 xfrm_policy_put_afinfo(afinfo);
233 return mode;
234}
235
236void xfrm_put_mode(struct xfrm_mode *mode)
237{
238 module_put(mode->owner);
239}
240
1da177e4
LT
241static inline unsigned long make_jiffies(long secs)
242{
243 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
244 return MAX_SCHEDULE_TIMEOUT-1;
245 else
246 return secs*HZ;
247}
248
249static void xfrm_policy_timer(unsigned long data)
250{
251 struct xfrm_policy *xp = (struct xfrm_policy*)data;
252 unsigned long now = (unsigned long)xtime.tv_sec;
253 long next = LONG_MAX;
254 int warn = 0;
255 int dir;
256
257 read_lock(&xp->lock);
258
259 if (xp->dead)
260 goto out;
261
77d8d7a6 262 dir = xfrm_policy_id2dir(xp->index);
1da177e4
LT
263
264 if (xp->lft.hard_add_expires_seconds) {
265 long tmo = xp->lft.hard_add_expires_seconds +
266 xp->curlft.add_time - now;
267 if (tmo <= 0)
268 goto expired;
269 if (tmo < next)
270 next = tmo;
271 }
272 if (xp->lft.hard_use_expires_seconds) {
273 long tmo = xp->lft.hard_use_expires_seconds +
274 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
275 if (tmo <= 0)
276 goto expired;
277 if (tmo < next)
278 next = tmo;
279 }
280 if (xp->lft.soft_add_expires_seconds) {
281 long tmo = xp->lft.soft_add_expires_seconds +
282 xp->curlft.add_time - now;
283 if (tmo <= 0) {
284 warn = 1;
285 tmo = XFRM_KM_TIMEOUT;
286 }
287 if (tmo < next)
288 next = tmo;
289 }
290 if (xp->lft.soft_use_expires_seconds) {
291 long tmo = xp->lft.soft_use_expires_seconds +
292 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
293 if (tmo <= 0) {
294 warn = 1;
295 tmo = XFRM_KM_TIMEOUT;
296 }
297 if (tmo < next)
298 next = tmo;
299 }
300
301 if (warn)
6c5c8ca7 302 km_policy_expired(xp, dir, 0, 0);
1da177e4
LT
303 if (next != LONG_MAX &&
304 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
305 xfrm_pol_hold(xp);
306
307out:
308 read_unlock(&xp->lock);
309 xfrm_pol_put(xp);
310 return;
311
312expired:
313 read_unlock(&xp->lock);
4666faab 314 if (!xfrm_policy_delete(xp, dir))
6c5c8ca7 315 km_policy_expired(xp, dir, 1, 0);
1da177e4
LT
316 xfrm_pol_put(xp);
317}
318
319
320/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
321 * SPD calls.
322 */
323
dd0fc66f 324struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
1da177e4
LT
325{
326 struct xfrm_policy *policy;
327
0da974f4 328 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
1da177e4
LT
329
330 if (policy) {
1da177e4
LT
331 atomic_set(&policy->refcnt, 1);
332 rwlock_init(&policy->lock);
333 init_timer(&policy->timer);
334 policy->timer.data = (unsigned long)policy;
335 policy->timer.function = xfrm_policy_timer;
336 }
337 return policy;
338}
339EXPORT_SYMBOL(xfrm_policy_alloc);
340
341/* Destroy xfrm_policy: descendant resources must be released to this moment. */
342
343void __xfrm_policy_destroy(struct xfrm_policy *policy)
344{
09a62660 345 BUG_ON(!policy->dead);
1da177e4 346
09a62660 347 BUG_ON(policy->bundles);
1da177e4
LT
348
349 if (del_timer(&policy->timer))
350 BUG();
351
df71837d 352 security_xfrm_policy_free(policy);
1da177e4
LT
353 kfree(policy);
354}
355EXPORT_SYMBOL(__xfrm_policy_destroy);
356
357static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
358{
359 struct dst_entry *dst;
360
361 while ((dst = policy->bundles) != NULL) {
362 policy->bundles = dst->next;
363 dst_free(dst);
364 }
365
366 if (del_timer(&policy->timer))
367 atomic_dec(&policy->refcnt);
368
369 if (atomic_read(&policy->refcnt) > 1)
370 flow_cache_flush();
371
372 xfrm_pol_put(policy);
373}
374
375static void xfrm_policy_gc_task(void *data)
376{
377 struct xfrm_policy *policy;
378 struct list_head *entry, *tmp;
379 struct list_head gc_list = LIST_HEAD_INIT(gc_list);
380
381 spin_lock_bh(&xfrm_policy_gc_lock);
382 list_splice_init(&xfrm_policy_gc_list, &gc_list);
383 spin_unlock_bh(&xfrm_policy_gc_lock);
384
385 list_for_each_safe(entry, tmp, &gc_list) {
386 policy = list_entry(entry, struct xfrm_policy, list);
387 xfrm_policy_gc_kill(policy);
388 }
389}
390
391/* Rule must be locked. Release descentant resources, announce
392 * entry dead. The rule must be unlinked from lists to the moment.
393 */
394
395static void xfrm_policy_kill(struct xfrm_policy *policy)
396{
397 int dead;
398
399 write_lock_bh(&policy->lock);
400 dead = policy->dead;
401 policy->dead = 1;
402 write_unlock_bh(&policy->lock);
403
404 if (unlikely(dead)) {
405 WARN_ON(1);
406 return;
407 }
408
409 spin_lock(&xfrm_policy_gc_lock);
410 list_add(&policy->list, &xfrm_policy_gc_list);
411 spin_unlock(&xfrm_policy_gc_lock);
412
413 schedule_work(&xfrm_policy_gc_work);
414}
415
416/* Generate new index... KAME seems to generate them ordered by cost
417 * of an absolute inpredictability of ordering of rules. This will not pass. */
4e81bb83 418static u32 xfrm_gen_index(u8 type, int dir)
1da177e4
LT
419{
420 u32 idx;
421 struct xfrm_policy *p;
422 static u32 idx_generator;
423
424 for (;;) {
425 idx = (idx_generator | dir);
426 idx_generator += 8;
427 if (idx == 0)
428 idx = 8;
4e81bb83 429 for (p = XFRM_POLICY_LISTHEAD(type, dir); p; p = p->next) {
1da177e4
LT
430 if (p->index == idx)
431 break;
432 }
433 if (!p)
434 return idx;
435 }
436}
437
438int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
439{
440 struct xfrm_policy *pol, **p;
441 struct xfrm_policy *delpol = NULL;
442 struct xfrm_policy **newpos = NULL;
9b78a82c 443 struct dst_entry *gc_list;
1da177e4
LT
444
445 write_lock_bh(&xfrm_policy_lock);
4e81bb83 446 for (p = XFRM_POLICY_LISTHEADP(policy->type, dir); (pol=*p)!=NULL;) {
df71837d
TJ
447 if (!delpol && memcmp(&policy->selector, &pol->selector, sizeof(pol->selector)) == 0 &&
448 xfrm_sec_ctx_match(pol->security, policy->security)) {
1da177e4
LT
449 if (excl) {
450 write_unlock_bh(&xfrm_policy_lock);
451 return -EEXIST;
452 }
453 *p = pol->next;
454 delpol = pol;
455 if (policy->priority > pol->priority)
456 continue;
457 } else if (policy->priority >= pol->priority) {
458 p = &pol->next;
459 continue;
460 }
461 if (!newpos)
462 newpos = p;
463 if (delpol)
464 break;
465 p = &pol->next;
466 }
467 if (newpos)
468 p = newpos;
469 xfrm_pol_hold(policy);
470 policy->next = *p;
471 *p = policy;
472 atomic_inc(&flow_cache_genid);
4e81bb83 473 policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
1da177e4
LT
474 policy->curlft.add_time = (unsigned long)xtime.tv_sec;
475 policy->curlft.use_time = 0;
476 if (!mod_timer(&policy->timer, jiffies + HZ))
477 xfrm_pol_hold(policy);
478 write_unlock_bh(&xfrm_policy_lock);
479
9b78a82c 480 if (delpol)
1da177e4 481 xfrm_policy_kill(delpol);
9b78a82c
DM
482
483 read_lock_bh(&xfrm_policy_lock);
484 gc_list = NULL;
485 for (policy = policy->next; policy; policy = policy->next) {
486 struct dst_entry *dst;
487
488 write_lock(&policy->lock);
489 dst = policy->bundles;
490 if (dst) {
491 struct dst_entry *tail = dst;
492 while (tail->next)
493 tail = tail->next;
494 tail->next = gc_list;
495 gc_list = dst;
496
497 policy->bundles = NULL;
498 }
499 write_unlock(&policy->lock);
1da177e4 500 }
9b78a82c
DM
501 read_unlock_bh(&xfrm_policy_lock);
502
503 while (gc_list) {
504 struct dst_entry *dst = gc_list;
505
506 gc_list = dst->next;
507 dst_free(dst);
508 }
509
1da177e4
LT
510 return 0;
511}
512EXPORT_SYMBOL(xfrm_policy_insert);
513
4e81bb83
MN
514struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
515 struct xfrm_selector *sel,
df71837d 516 struct xfrm_sec_ctx *ctx, int delete)
1da177e4
LT
517{
518 struct xfrm_policy *pol, **p;
519
520 write_lock_bh(&xfrm_policy_lock);
4e81bb83 521 for (p = XFRM_POLICY_LISTHEADP(type, dir); (pol=*p)!=NULL; p = &pol->next) {
df71837d
TJ
522 if ((memcmp(sel, &pol->selector, sizeof(*sel)) == 0) &&
523 (xfrm_sec_ctx_match(ctx, pol->security))) {
1da177e4
LT
524 xfrm_pol_hold(pol);
525 if (delete)
526 *p = pol->next;
527 break;
528 }
529 }
530 write_unlock_bh(&xfrm_policy_lock);
531
532 if (pol && delete) {
533 atomic_inc(&flow_cache_genid);
534 xfrm_policy_kill(pol);
535 }
536 return pol;
537}
df71837d 538EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
1da177e4 539
4e81bb83 540struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete)
1da177e4
LT
541{
542 struct xfrm_policy *pol, **p;
543
544 write_lock_bh(&xfrm_policy_lock);
4e81bb83 545 for (p = XFRM_POLICY_LISTHEADP(type, dir); (pol=*p)!=NULL; p = &pol->next) {
1da177e4
LT
546 if (pol->index == id) {
547 xfrm_pol_hold(pol);
548 if (delete)
549 *p = pol->next;
550 break;
551 }
552 }
553 write_unlock_bh(&xfrm_policy_lock);
554
555 if (pol && delete) {
556 atomic_inc(&flow_cache_genid);
557 xfrm_policy_kill(pol);
558 }
559 return pol;
560}
561EXPORT_SYMBOL(xfrm_policy_byid);
562
4e81bb83 563void xfrm_policy_flush(u8 type)
1da177e4
LT
564{
565 struct xfrm_policy *xp;
4e81bb83 566 struct xfrm_policy **p_list = XFRM_POLICY_LISTS(type);
1da177e4
LT
567 int dir;
568
569 write_lock_bh(&xfrm_policy_lock);
570 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
4e81bb83
MN
571 while ((xp = p_list[dir]) != NULL) {
572 p_list[dir] = xp->next;
1da177e4
LT
573 write_unlock_bh(&xfrm_policy_lock);
574
575 xfrm_policy_kill(xp);
576
577 write_lock_bh(&xfrm_policy_lock);
578 }
579 }
580 atomic_inc(&flow_cache_genid);
581 write_unlock_bh(&xfrm_policy_lock);
582}
583EXPORT_SYMBOL(xfrm_policy_flush);
584
4e81bb83 585int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
1da177e4
LT
586 void *data)
587{
588 struct xfrm_policy *xp;
589 int dir;
590 int count = 0;
591 int error = 0;
592
593 read_lock_bh(&xfrm_policy_lock);
594 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
4e81bb83 595 for (xp = XFRM_POLICY_LISTHEAD(type, dir); xp; xp = xp->next)
1da177e4
LT
596 count++;
597 }
598
599 if (count == 0) {
600 error = -ENOENT;
601 goto out;
602 }
603
604 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
4e81bb83 605 for (xp = XFRM_POLICY_LISTHEAD(type, dir); xp; xp = xp->next) {
1da177e4
LT
606 error = func(xp, dir%XFRM_POLICY_MAX, --count, data);
607 if (error)
608 goto out;
609 }
610 }
611
612out:
613 read_unlock_bh(&xfrm_policy_lock);
614 return error;
615}
616EXPORT_SYMBOL(xfrm_policy_walk);
617
618/* Find policy to apply to this flow. */
619
4e81bb83
MN
620static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
621 u16 family, u8 dir)
1da177e4
LT
622{
623 struct xfrm_policy *pol;
624
625 read_lock_bh(&xfrm_policy_lock);
4e81bb83 626 for (pol = XFRM_POLICY_LISTHEAD(type, dir); pol; pol = pol->next) {
1da177e4
LT
627 struct xfrm_selector *sel = &pol->selector;
628 int match;
629
630 if (pol->family != family)
631 continue;
632
633 match = xfrm_selector_match(sel, fl, family);
df71837d 634
1da177e4 635 if (match) {
e0d1caa7 636 if (!security_xfrm_policy_lookup(pol, fl->secid, dir)) {
df71837d
TJ
637 xfrm_pol_hold(pol);
638 break;
639 }
1da177e4
LT
640 }
641 }
642 read_unlock_bh(&xfrm_policy_lock);
4e81bb83
MN
643
644 return pol;
645}
646
647static void xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
648 void **objp, atomic_t **obj_refp)
649{
650 struct xfrm_policy *pol;
651
652#ifdef CONFIG_XFRM_SUB_POLICY
653 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
654 if (pol)
655 goto end;
656#endif
657 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
658
659#ifdef CONFIG_XFRM_SUB_POLICY
660 end:
661#endif
1da177e4
LT
662 if ((*objp = (void *) pol) != NULL)
663 *obj_refp = &pol->refcnt;
664}
665
df71837d
TJ
666static inline int policy_to_flow_dir(int dir)
667{
668 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
669 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
670 XFRM_POLICY_FWD == FLOW_DIR_FWD)
671 return dir;
672 switch (dir) {
673 default:
674 case XFRM_POLICY_IN:
675 return FLOW_DIR_IN;
676 case XFRM_POLICY_OUT:
677 return FLOW_DIR_OUT;
678 case XFRM_POLICY_FWD:
679 return FLOW_DIR_FWD;
680 };
681}
682
e0d1caa7 683static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
1da177e4
LT
684{
685 struct xfrm_policy *pol;
686
687 read_lock_bh(&xfrm_policy_lock);
688 if ((pol = sk->sk_policy[dir]) != NULL) {
df71837d 689 int match = xfrm_selector_match(&pol->selector, fl,
1da177e4 690 sk->sk_family);
df71837d
TJ
691 int err = 0;
692
1da177e4 693 if (match)
e0d1caa7 694 err = security_xfrm_policy_lookup(pol, fl->secid, policy_to_flow_dir(dir));
df71837d
TJ
695
696 if (match && !err)
1da177e4
LT
697 xfrm_pol_hold(pol);
698 else
699 pol = NULL;
700 }
701 read_unlock_bh(&xfrm_policy_lock);
702 return pol;
703}
704
705static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
706{
4e81bb83
MN
707 struct xfrm_policy **p_list = XFRM_POLICY_LISTS(pol->type);
708
709 pol->next = p_list[dir];
710 p_list[dir] = pol;
1da177e4
LT
711 xfrm_pol_hold(pol);
712}
713
714static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
715 int dir)
716{
717 struct xfrm_policy **polp;
718
4e81bb83 719 for (polp = XFRM_POLICY_LISTHEADP(pol->type, dir);
1da177e4
LT
720 *polp != NULL; polp = &(*polp)->next) {
721 if (*polp == pol) {
722 *polp = pol->next;
723 return pol;
724 }
725 }
726 return NULL;
727}
728
4666faab 729int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
1da177e4
LT
730{
731 write_lock_bh(&xfrm_policy_lock);
732 pol = __xfrm_policy_unlink(pol, dir);
733 write_unlock_bh(&xfrm_policy_lock);
734 if (pol) {
735 if (dir < XFRM_POLICY_MAX)
736 atomic_inc(&flow_cache_genid);
737 xfrm_policy_kill(pol);
4666faab 738 return 0;
1da177e4 739 }
4666faab 740 return -ENOENT;
1da177e4 741}
a70fcb0b 742EXPORT_SYMBOL(xfrm_policy_delete);
1da177e4
LT
743
744int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
745{
746 struct xfrm_policy *old_pol;
747
4e81bb83
MN
748#ifdef CONFIG_XFRM_SUB_POLICY
749 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
750 return -EINVAL;
751#endif
752
1da177e4
LT
753 write_lock_bh(&xfrm_policy_lock);
754 old_pol = sk->sk_policy[dir];
755 sk->sk_policy[dir] = pol;
756 if (pol) {
757 pol->curlft.add_time = (unsigned long)xtime.tv_sec;
4e81bb83 758 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
1da177e4
LT
759 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
760 }
761 if (old_pol)
762 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
763 write_unlock_bh(&xfrm_policy_lock);
764
765 if (old_pol) {
766 xfrm_policy_kill(old_pol);
767 }
768 return 0;
769}
770
771static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
772{
773 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
774
775 if (newp) {
776 newp->selector = old->selector;
df71837d
TJ
777 if (security_xfrm_policy_clone(old, newp)) {
778 kfree(newp);
779 return NULL; /* ENOMEM */
780 }
1da177e4
LT
781 newp->lft = old->lft;
782 newp->curlft = old->curlft;
783 newp->action = old->action;
784 newp->flags = old->flags;
785 newp->xfrm_nr = old->xfrm_nr;
786 newp->index = old->index;
4e81bb83 787 newp->type = old->type;
1da177e4
LT
788 memcpy(newp->xfrm_vec, old->xfrm_vec,
789 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
790 write_lock_bh(&xfrm_policy_lock);
791 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
792 write_unlock_bh(&xfrm_policy_lock);
793 xfrm_pol_put(newp);
794 }
795 return newp;
796}
797
798int __xfrm_sk_clone_policy(struct sock *sk)
799{
800 struct xfrm_policy *p0 = sk->sk_policy[0],
801 *p1 = sk->sk_policy[1];
802
803 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
804 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
805 return -ENOMEM;
806 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
807 return -ENOMEM;
808 return 0;
809}
810
811/* Resolve list of templates for the flow, given policy. */
812
813static int
4e81bb83
MN
814xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
815 struct xfrm_state **xfrm,
816 unsigned short family)
1da177e4
LT
817{
818 int nx;
819 int i, error;
820 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
821 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
822
823 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
824 struct xfrm_state *x;
825 xfrm_address_t *remote = daddr;
826 xfrm_address_t *local = saddr;
827 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
828
7e49e6de 829 if (tmpl->mode == XFRM_MODE_TUNNEL) {
1da177e4
LT
830 remote = &tmpl->id.daddr;
831 local = &tmpl->saddr;
832 }
833
834 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
835
836 if (x && x->km.state == XFRM_STATE_VALID) {
837 xfrm[nx++] = x;
838 daddr = remote;
839 saddr = local;
840 continue;
841 }
842 if (x) {
843 error = (x->km.state == XFRM_STATE_ERROR ?
844 -EINVAL : -EAGAIN);
845 xfrm_state_put(x);
846 }
847
848 if (!tmpl->optional)
849 goto fail;
850 }
851 return nx;
852
853fail:
854 for (nx--; nx>=0; nx--)
855 xfrm_state_put(xfrm[nx]);
856 return error;
857}
858
4e81bb83
MN
859static int
860xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
861 struct xfrm_state **xfrm,
862 unsigned short family)
863{
864 int cnx = 0;
865 int error;
866 int ret;
867 int i;
868
869 for (i = 0; i < npols; i++) {
870 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
871 error = -ENOBUFS;
872 goto fail;
873 }
874 ret = xfrm_tmpl_resolve_one(pols[i], fl, &xfrm[cnx], family);
875 if (ret < 0) {
876 error = ret;
877 goto fail;
878 } else
879 cnx += ret;
880 }
881
882 return cnx;
883
884 fail:
885 for (cnx--; cnx>=0; cnx--)
886 xfrm_state_put(xfrm[cnx]);
887 return error;
888
889}
890
1da177e4
LT
891/* Check that the bundle accepts the flow and its components are
892 * still valid.
893 */
894
895static struct dst_entry *
896xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
897{
898 struct dst_entry *x;
899 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
900 if (unlikely(afinfo == NULL))
901 return ERR_PTR(-EINVAL);
902 x = afinfo->find_bundle(fl, policy);
903 xfrm_policy_put_afinfo(afinfo);
904 return x;
905}
906
907/* Allocate chain of dst_entry's, attach known xfrm's, calculate
908 * all the metrics... Shortly, bundle a bundle.
909 */
910
911static int
912xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
913 struct flowi *fl, struct dst_entry **dst_p,
914 unsigned short family)
915{
916 int err;
917 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
918 if (unlikely(afinfo == NULL))
919 return -EINVAL;
920 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
921 xfrm_policy_put_afinfo(afinfo);
922 return err;
923}
924
1da177e4
LT
925
926static int stale_bundle(struct dst_entry *dst);
927
928/* Main function: finds/creates a bundle for given flow.
929 *
930 * At the moment we eat a raw IP route. Mostly to speed up lookups
931 * on interfaces with disabled IPsec.
932 */
933int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
934 struct sock *sk, int flags)
935{
936 struct xfrm_policy *policy;
4e81bb83
MN
937 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
938 int npols;
939 int pol_dead;
940 int xfrm_nr;
941 int pi;
1da177e4
LT
942 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
943 struct dst_entry *dst, *dst_orig = *dst_p;
944 int nx = 0;
945 int err;
946 u32 genid;
42cf93cd 947 u16 family;
df71837d 948 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
e0d1caa7 949
1da177e4
LT
950restart:
951 genid = atomic_read(&flow_cache_genid);
952 policy = NULL;
4e81bb83
MN
953 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
954 pols[pi] = NULL;
955 npols = 0;
956 pol_dead = 0;
957 xfrm_nr = 0;
958
1da177e4 959 if (sk && sk->sk_policy[1])
e0d1caa7 960 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
1da177e4
LT
961
962 if (!policy) {
963 /* To accelerate a bit... */
4e81bb83 964 if ((dst_orig->flags & DST_NOXFRM) || xfrm_policy_lists_empty(XFRM_POLICY_OUT))
1da177e4
LT
965 return 0;
966
e0d1caa7 967 policy = flow_cache_lookup(fl, dst_orig->ops->family,
42cf93cd 968 dir, xfrm_policy_lookup);
1da177e4
LT
969 }
970
971 if (!policy)
972 return 0;
973
42cf93cd 974 family = dst_orig->ops->family;
1da177e4 975 policy->curlft.use_time = (unsigned long)xtime.tv_sec;
4e81bb83
MN
976 pols[0] = policy;
977 npols ++;
978 xfrm_nr += pols[0]->xfrm_nr;
1da177e4
LT
979
980 switch (policy->action) {
981 case XFRM_POLICY_BLOCK:
982 /* Prohibit the flow */
e104411b
PM
983 err = -EPERM;
984 goto error;
1da177e4
LT
985
986 case XFRM_POLICY_ALLOW:
4e81bb83 987#ifndef CONFIG_XFRM_SUB_POLICY
1da177e4
LT
988 if (policy->xfrm_nr == 0) {
989 /* Flow passes not transformed. */
990 xfrm_pol_put(policy);
991 return 0;
992 }
4e81bb83 993#endif
1da177e4
LT
994
995 /* Try to find matching bundle.
996 *
997 * LATER: help from flow cache. It is optional, this
998 * is required only for output policy.
999 */
1000 dst = xfrm_find_bundle(fl, policy, family);
1001 if (IS_ERR(dst)) {
e104411b
PM
1002 err = PTR_ERR(dst);
1003 goto error;
1da177e4
LT
1004 }
1005
1006 if (dst)
1007 break;
1008
4e81bb83
MN
1009#ifdef CONFIG_XFRM_SUB_POLICY
1010 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1011 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1012 fl, family,
1013 XFRM_POLICY_OUT);
1014 if (pols[1]) {
1015 if (pols[1]->action == XFRM_POLICY_BLOCK) {
1016 err = -EPERM;
1017 goto error;
1018 }
1019 npols ++;
1020 xfrm_nr += pols[1]->xfrm_nr;
1021 }
1022 }
1023
1024 /*
1025 * Because neither flowi nor bundle information knows about
1026 * transformation template size. On more than one policy usage
1027 * we can realize whether all of them is bypass or not after
1028 * they are searched. See above not-transformed bypass
1029 * is surrounded by non-sub policy configuration, too.
1030 */
1031 if (xfrm_nr == 0) {
1032 /* Flow passes not transformed. */
1033 xfrm_pols_put(pols, npols);
1034 return 0;
1035 }
1036
1037#endif
1038 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
1da177e4
LT
1039
1040 if (unlikely(nx<0)) {
1041 err = nx;
1042 if (err == -EAGAIN && flags) {
1043 DECLARE_WAITQUEUE(wait, current);
1044
1045 add_wait_queue(&km_waitq, &wait);
1046 set_current_state(TASK_INTERRUPTIBLE);
1047 schedule();
1048 set_current_state(TASK_RUNNING);
1049 remove_wait_queue(&km_waitq, &wait);
1050
4e81bb83 1051 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
1da177e4
LT
1052
1053 if (nx == -EAGAIN && signal_pending(current)) {
1054 err = -ERESTART;
1055 goto error;
1056 }
1057 if (nx == -EAGAIN ||
1058 genid != atomic_read(&flow_cache_genid)) {
4e81bb83 1059 xfrm_pols_put(pols, npols);
1da177e4
LT
1060 goto restart;
1061 }
1062 err = nx;
1063 }
1064 if (err < 0)
1065 goto error;
1066 }
1067 if (nx == 0) {
1068 /* Flow passes not transformed. */
4e81bb83 1069 xfrm_pols_put(pols, npols);
1da177e4
LT
1070 return 0;
1071 }
1072
1073 dst = dst_orig;
1074 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
1075
1076 if (unlikely(err)) {
1077 int i;
1078 for (i=0; i<nx; i++)
1079 xfrm_state_put(xfrm[i]);
1080 goto error;
1081 }
1082
4e81bb83
MN
1083 for (pi = 0; pi < npols; pi++) {
1084 read_lock_bh(&pols[pi]->lock);
1085 pol_dead |= pols[pi]->dead;
1086 read_unlock_bh(&pols[pi]->lock);
1087 }
1088
1da177e4 1089 write_lock_bh(&policy->lock);
4e81bb83 1090 if (unlikely(pol_dead || stale_bundle(dst))) {
1da177e4
LT
1091 /* Wow! While we worked on resolving, this
1092 * policy has gone. Retry. It is not paranoia,
1093 * we just cannot enlist new bundle to dead object.
1094 * We can't enlist stable bundles either.
1095 */
1096 write_unlock_bh(&policy->lock);
1da177e4
LT
1097 if (dst)
1098 dst_free(dst);
00de651d
HX
1099
1100 err = -EHOSTUNREACH;
1101 goto error;
1da177e4
LT
1102 }
1103 dst->next = policy->bundles;
1104 policy->bundles = dst;
1105 dst_hold(dst);
1106 write_unlock_bh(&policy->lock);
1107 }
1108 *dst_p = dst;
1109 dst_release(dst_orig);
4e81bb83 1110 xfrm_pols_put(pols, npols);
1da177e4
LT
1111 return 0;
1112
1113error:
1114 dst_release(dst_orig);
4e81bb83 1115 xfrm_pols_put(pols, npols);
1da177e4
LT
1116 *dst_p = NULL;
1117 return err;
1118}
1119EXPORT_SYMBOL(xfrm_lookup);
1120
df0ba92a
MN
1121static inline int
1122xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1123{
1124 struct xfrm_state *x;
1125 int err;
1126
1127 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1128 return 0;
1129 x = skb->sp->xvec[idx];
1130 if (!x->type->reject)
1131 return 0;
1132 xfrm_state_hold(x);
1133 err = x->type->reject(x, skb, fl);
1134 xfrm_state_put(x);
1135 return err;
1136}
1137
1da177e4
LT
1138/* When skb is transformed back to its "native" form, we have to
1139 * check policy restrictions. At the moment we make this in maximally
1140 * stupid way. Shame on me. :-) Of course, connected sockets must
1141 * have policy cached at them.
1142 */
1143
1144static inline int
1145xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
1146 unsigned short family)
1147{
1148 if (xfrm_state_kern(x))
1149 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family);
1150 return x->id.proto == tmpl->id.proto &&
1151 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1152 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1153 x->props.mode == tmpl->mode &&
f3bd4840
MN
1154 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1155 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
7e49e6de
MN
1156 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1157 xfrm_state_addr_cmp(tmpl, x, family));
1da177e4
LT
1158}
1159
df0ba92a
MN
1160/*
1161 * 0 or more than 0 is returned when validation is succeeded (either bypass
1162 * because of optional transport mode, or next index of the mathced secpath
1163 * state with the template.
1164 * -1 is returned when no matching template is found.
1165 * Otherwise "-2 - errored_index" is returned.
1166 */
1da177e4
LT
1167static inline int
1168xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1169 unsigned short family)
1170{
1171 int idx = start;
1172
1173 if (tmpl->optional) {
7e49e6de 1174 if (tmpl->mode == XFRM_MODE_TRANSPORT)
1da177e4
LT
1175 return start;
1176 } else
1177 start = -1;
1178 for (; idx < sp->len; idx++) {
dbe5b4aa 1179 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
1da177e4 1180 return ++idx;
df0ba92a
MN
1181 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1182 if (start == -1)
1183 start = -2-idx;
1da177e4 1184 break;
df0ba92a 1185 }
1da177e4
LT
1186 }
1187 return start;
1188}
1189
3e3850e9
PM
1190int
1191xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
1da177e4
LT
1192{
1193 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
e0d1caa7 1194 int err;
1da177e4
LT
1195
1196 if (unlikely(afinfo == NULL))
1197 return -EAFNOSUPPORT;
1198
1199 afinfo->decode_session(skb, fl);
beb8d13b 1200 err = security_xfrm_decode_session(skb, &fl->secid);
1da177e4 1201 xfrm_policy_put_afinfo(afinfo);
e0d1caa7 1202 return err;
1da177e4 1203}
3e3850e9 1204EXPORT_SYMBOL(xfrm_decode_session);
1da177e4 1205
df0ba92a 1206static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
1da177e4
LT
1207{
1208 for (; k < sp->len; k++) {
df0ba92a
MN
1209 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
1210 if (idxp)
1211 *idxp = k;
1da177e4 1212 return 1;
df0ba92a 1213 }
1da177e4
LT
1214 }
1215
1216 return 0;
1217}
1218
1219int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
1220 unsigned short family)
1221{
1222 struct xfrm_policy *pol;
4e81bb83
MN
1223 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1224 int npols = 0;
1225 int xfrm_nr;
1226 int pi;
1da177e4 1227 struct flowi fl;
df71837d 1228 u8 fl_dir = policy_to_flow_dir(dir);
df0ba92a
MN
1229 int xerr_idx = -1;
1230 int *xerr_idxp = &xerr_idx;
1da177e4 1231
3e3850e9 1232 if (xfrm_decode_session(skb, &fl, family) < 0)
1da177e4 1233 return 0;
eb9c7ebe 1234 nf_nat_decode_session(skb, &fl, family);
1da177e4
LT
1235
1236 /* First, check used SA against their selectors. */
1237 if (skb->sp) {
1238 int i;
1239
1240 for (i=skb->sp->len-1; i>=0; i--) {
dbe5b4aa
HX
1241 struct xfrm_state *x = skb->sp->xvec[i];
1242 if (!xfrm_selector_match(&x->sel, &fl, family))
1da177e4 1243 return 0;
1da177e4
LT
1244 }
1245 }
1246
1247 pol = NULL;
1248 if (sk && sk->sk_policy[dir])
e0d1caa7 1249 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
1da177e4
LT
1250
1251 if (!pol)
e0d1caa7 1252 pol = flow_cache_lookup(&fl, family, fl_dir,
1da177e4
LT
1253 xfrm_policy_lookup);
1254
df0ba92a
MN
1255 if (!pol) {
1256 if (skb->sp && secpath_has_nontransport(skb->sp, 0, xerr_idxp)) {
1257 xfrm_secpath_reject(xerr_idx, skb, &fl);
1258 return 0;
1259 }
1260 return 1;
1261 }
1da177e4
LT
1262
1263 pol->curlft.use_time = (unsigned long)xtime.tv_sec;
1264
4e81bb83
MN
1265 pols[0] = pol;
1266 npols ++;
1267#ifdef CONFIG_XFRM_SUB_POLICY
1268 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1269 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1270 &fl, family,
1271 XFRM_POLICY_IN);
1272 if (pols[1]) {
1273 pols[1]->curlft.use_time = (unsigned long)xtime.tv_sec;
1274 npols ++;
1275 }
1276 }
1277#endif
1278
1da177e4
LT
1279 if (pol->action == XFRM_POLICY_ALLOW) {
1280 struct sec_path *sp;
1281 static struct sec_path dummy;
4e81bb83
MN
1282 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
1283 struct xfrm_tmpl **tpp = tp;
1284 int ti = 0;
1da177e4
LT
1285 int i, k;
1286
1287 if ((sp = skb->sp) == NULL)
1288 sp = &dummy;
1289
4e81bb83
MN
1290 for (pi = 0; pi < npols; pi++) {
1291 if (pols[pi] != pol &&
1292 pols[pi]->action != XFRM_POLICY_ALLOW)
1293 goto reject;
1294 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
1295 goto reject_error;
1296 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1297 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1298 }
1299 xfrm_nr = ti;
1300
1da177e4
LT
1301 /* For each tunnel xfrm, find the first matching tmpl.
1302 * For each tmpl before that, find corresponding xfrm.
1303 * Order is _important_. Later we will implement
1304 * some barriers, but at the moment barriers
1305 * are implied between each two transformations.
1306 */
4e81bb83
MN
1307 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1308 k = xfrm_policy_ok(tpp[i], sp, k, family);
df0ba92a
MN
1309 if (k < 0) {
1310 if (k < -1 && xerr_idxp)
1311 *xerr_idxp = -(2+k);
1da177e4 1312 goto reject;
df0ba92a 1313 }
1da177e4
LT
1314 }
1315
df0ba92a 1316 if (secpath_has_nontransport(sp, k, xerr_idxp))
1da177e4
LT
1317 goto reject;
1318
4e81bb83 1319 xfrm_pols_put(pols, npols);
1da177e4
LT
1320 return 1;
1321 }
1322
1323reject:
df0ba92a 1324 xfrm_secpath_reject(xerr_idx, skb, &fl);
4e81bb83
MN
1325reject_error:
1326 xfrm_pols_put(pols, npols);
1da177e4
LT
1327 return 0;
1328}
1329EXPORT_SYMBOL(__xfrm_policy_check);
1330
1331int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1332{
1333 struct flowi fl;
1334
3e3850e9 1335 if (xfrm_decode_session(skb, &fl, family) < 0)
1da177e4
LT
1336 return 0;
1337
1338 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1339}
1340EXPORT_SYMBOL(__xfrm_route_forward);
1341
d49c73c7
DM
1342/* Optimize later using cookies and generation ids. */
1343
1da177e4
LT
1344static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1345{
d49c73c7
DM
1346 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1347 * to "-1" to force all XFRM destinations to get validated by
1348 * dst_ops->check on every use. We do this because when a
1349 * normal route referenced by an XFRM dst is obsoleted we do
1350 * not go looking around for all parent referencing XFRM dsts
1351 * so that we can invalidate them. It is just too much work.
1352 * Instead we make the checks here on every use. For example:
1353 *
1354 * XFRM dst A --> IPv4 dst X
1355 *
1356 * X is the "xdst->route" of A (X is also the "dst->path" of A
1357 * in this example). If X is marked obsolete, "A" will not
1358 * notice. That's what we are validating here via the
1359 * stale_bundle() check.
1360 *
1361 * When a policy's bundle is pruned, we dst_free() the XFRM
1362 * dst which causes it's ->obsolete field to be set to a
1363 * positive non-zero integer. If an XFRM dst has been pruned
1364 * like this, we want to force a new route lookup.
399c180a 1365 */
d49c73c7
DM
1366 if (dst->obsolete < 0 && !stale_bundle(dst))
1367 return dst;
1368
1da177e4
LT
1369 return NULL;
1370}
1371
1372static int stale_bundle(struct dst_entry *dst)
1373{
e53820de 1374 return !xfrm_bundle_ok((struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
1da177e4
LT
1375}
1376
aabc9761 1377void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
1da177e4 1378{
1da177e4
LT
1379 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
1380 dst->dev = &loopback_dev;
1381 dev_hold(&loopback_dev);
1382 dev_put(dev);
1383 }
1384}
aabc9761 1385EXPORT_SYMBOL(xfrm_dst_ifdown);
1da177e4
LT
1386
1387static void xfrm_link_failure(struct sk_buff *skb)
1388{
1389 /* Impossible. Such dst must be popped before reaches point of failure. */
1390 return;
1391}
1392
1393static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1394{
1395 if (dst) {
1396 if (dst->obsolete) {
1397 dst_release(dst);
1398 dst = NULL;
1399 }
1400 }
1401 return dst;
1402}
1403
1404static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1405{
1406 int i;
1407 struct xfrm_policy *pol;
1408 struct dst_entry *dst, **dstp, *gc_list = NULL;
1409
1410 read_lock_bh(&xfrm_policy_lock);
1411 for (i=0; i<2*XFRM_POLICY_MAX; i++) {
4e81bb83
MN
1412#ifdef CONFIG_XFRM_SUB_POLICY
1413 for (pol = xfrm_policy_list_sub[i]; pol; pol = pol->next) {
1414 write_lock(&pol->lock);
1415 dstp = &pol->bundles;
1416 while ((dst=*dstp) != NULL) {
1417 if (func(dst)) {
1418 *dstp = dst->next;
1419 dst->next = gc_list;
1420 gc_list = dst;
1421 } else {
1422 dstp = &dst->next;
1423 }
1424 }
1425 write_unlock(&pol->lock);
1426 }
1427
1428#endif
1da177e4
LT
1429 for (pol = xfrm_policy_list[i]; pol; pol = pol->next) {
1430 write_lock(&pol->lock);
1431 dstp = &pol->bundles;
1432 while ((dst=*dstp) != NULL) {
1433 if (func(dst)) {
1434 *dstp = dst->next;
1435 dst->next = gc_list;
1436 gc_list = dst;
1437 } else {
1438 dstp = &dst->next;
1439 }
1440 }
1441 write_unlock(&pol->lock);
1442 }
1443 }
1444 read_unlock_bh(&xfrm_policy_lock);
1445
1446 while (gc_list) {
1447 dst = gc_list;
1448 gc_list = dst->next;
1449 dst_free(dst);
1450 }
1451}
1452
1453static int unused_bundle(struct dst_entry *dst)
1454{
1455 return !atomic_read(&dst->__refcnt);
1456}
1457
1458static void __xfrm_garbage_collect(void)
1459{
1460 xfrm_prune_bundles(unused_bundle);
1461}
1462
1463int xfrm_flush_bundles(void)
1464{
1465 xfrm_prune_bundles(stale_bundle);
1466 return 0;
1467}
1468
399c180a
DM
1469static int always_true(struct dst_entry *dst)
1470{
1471 return 1;
1472}
1473
1474void xfrm_flush_all_bundles(void)
1475{
1476 xfrm_prune_bundles(always_true);
1477}
1478
1da177e4
LT
1479void xfrm_init_pmtu(struct dst_entry *dst)
1480{
1481 do {
1482 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1483 u32 pmtu, route_mtu_cached;
1484
1485 pmtu = dst_mtu(dst->child);
1486 xdst->child_mtu_cached = pmtu;
1487
1488 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
1489
1490 route_mtu_cached = dst_mtu(xdst->route);
1491 xdst->route_mtu_cached = route_mtu_cached;
1492
1493 if (pmtu > route_mtu_cached)
1494 pmtu = route_mtu_cached;
1495
1496 dst->metrics[RTAX_MTU-1] = pmtu;
1497 } while ((dst = dst->next));
1498}
1499
1500EXPORT_SYMBOL(xfrm_init_pmtu);
1501
1502/* Check that the bundle accepts the flow and its components are
1503 * still valid.
1504 */
1505
e53820de 1506int xfrm_bundle_ok(struct xfrm_dst *first, struct flowi *fl, int family, int strict)
1da177e4
LT
1507{
1508 struct dst_entry *dst = &first->u.dst;
1509 struct xfrm_dst *last;
1510 u32 mtu;
1511
92d63dec 1512 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
1da177e4
LT
1513 (dst->dev && !netif_running(dst->dev)))
1514 return 0;
1515
1516 last = NULL;
1517
1518 do {
1519 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1520
1521 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
1522 return 0;
e0d1caa7
VY
1523 if (fl && !security_xfrm_flow_state_match(fl, dst->xfrm))
1524 return 0;
1da177e4
LT
1525 if (dst->xfrm->km.state != XFRM_STATE_VALID)
1526 return 0;
e53820de
MN
1527
1528 if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
1529 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
1530 return 0;
1da177e4
LT
1531
1532 mtu = dst_mtu(dst->child);
1533 if (xdst->child_mtu_cached != mtu) {
1534 last = xdst;
1535 xdst->child_mtu_cached = mtu;
1536 }
1537
92d63dec 1538 if (!dst_check(xdst->route, xdst->route_cookie))
1da177e4
LT
1539 return 0;
1540 mtu = dst_mtu(xdst->route);
1541 if (xdst->route_mtu_cached != mtu) {
1542 last = xdst;
1543 xdst->route_mtu_cached = mtu;
1544 }
1545
1546 dst = dst->child;
1547 } while (dst->xfrm);
1548
1549 if (likely(!last))
1550 return 1;
1551
1552 mtu = last->child_mtu_cached;
1553 for (;;) {
1554 dst = &last->u.dst;
1555
1556 mtu = xfrm_state_mtu(dst->xfrm, mtu);
1557 if (mtu > last->route_mtu_cached)
1558 mtu = last->route_mtu_cached;
1559 dst->metrics[RTAX_MTU-1] = mtu;
1560
1561 if (last == first)
1562 break;
1563
1564 last = last->u.next;
1565 last->child_mtu_cached = mtu;
1566 }
1567
1568 return 1;
1569}
1570
1571EXPORT_SYMBOL(xfrm_bundle_ok);
1572
1da177e4
LT
1573int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
1574{
1575 int err = 0;
1576 if (unlikely(afinfo == NULL))
1577 return -EINVAL;
1578 if (unlikely(afinfo->family >= NPROTO))
1579 return -EAFNOSUPPORT;
e959d812 1580 write_lock_bh(&xfrm_policy_afinfo_lock);
1da177e4
LT
1581 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
1582 err = -ENOBUFS;
1583 else {
1584 struct dst_ops *dst_ops = afinfo->dst_ops;
1585 if (likely(dst_ops->kmem_cachep == NULL))
1586 dst_ops->kmem_cachep = xfrm_dst_cache;
1587 if (likely(dst_ops->check == NULL))
1588 dst_ops->check = xfrm_dst_check;
1da177e4
LT
1589 if (likely(dst_ops->negative_advice == NULL))
1590 dst_ops->negative_advice = xfrm_negative_advice;
1591 if (likely(dst_ops->link_failure == NULL))
1592 dst_ops->link_failure = xfrm_link_failure;
1da177e4
LT
1593 if (likely(afinfo->garbage_collect == NULL))
1594 afinfo->garbage_collect = __xfrm_garbage_collect;
1595 xfrm_policy_afinfo[afinfo->family] = afinfo;
1596 }
e959d812 1597 write_unlock_bh(&xfrm_policy_afinfo_lock);
1da177e4
LT
1598 return err;
1599}
1600EXPORT_SYMBOL(xfrm_policy_register_afinfo);
1601
1602int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
1603{
1604 int err = 0;
1605 if (unlikely(afinfo == NULL))
1606 return -EINVAL;
1607 if (unlikely(afinfo->family >= NPROTO))
1608 return -EAFNOSUPPORT;
e959d812 1609 write_lock_bh(&xfrm_policy_afinfo_lock);
1da177e4
LT
1610 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
1611 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
1612 err = -EINVAL;
1613 else {
1614 struct dst_ops *dst_ops = afinfo->dst_ops;
1615 xfrm_policy_afinfo[afinfo->family] = NULL;
1616 dst_ops->kmem_cachep = NULL;
1617 dst_ops->check = NULL;
1da177e4
LT
1618 dst_ops->negative_advice = NULL;
1619 dst_ops->link_failure = NULL;
1da177e4
LT
1620 afinfo->garbage_collect = NULL;
1621 }
1622 }
e959d812 1623 write_unlock_bh(&xfrm_policy_afinfo_lock);
1da177e4
LT
1624 return err;
1625}
1626EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
1627
1628static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
1629{
1630 struct xfrm_policy_afinfo *afinfo;
1631 if (unlikely(family >= NPROTO))
1632 return NULL;
1633 read_lock(&xfrm_policy_afinfo_lock);
1634 afinfo = xfrm_policy_afinfo[family];
546be240
HX
1635 if (unlikely(!afinfo))
1636 read_unlock(&xfrm_policy_afinfo_lock);
1da177e4
LT
1637 return afinfo;
1638}
1639
1640static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
1641{
546be240
HX
1642 read_unlock(&xfrm_policy_afinfo_lock);
1643}
1644
1645static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
1646{
1647 struct xfrm_policy_afinfo *afinfo;
1648 if (unlikely(family >= NPROTO))
1649 return NULL;
1650 write_lock_bh(&xfrm_policy_afinfo_lock);
1651 afinfo = xfrm_policy_afinfo[family];
1652 if (unlikely(!afinfo))
1653 write_unlock_bh(&xfrm_policy_afinfo_lock);
1654 return afinfo;
1655}
1656
1657static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
1658{
1659 write_unlock_bh(&xfrm_policy_afinfo_lock);
1da177e4
LT
1660}
1661
1662static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
1663{
1664 switch (event) {
1665 case NETDEV_DOWN:
1666 xfrm_flush_bundles();
1667 }
1668 return NOTIFY_DONE;
1669}
1670
1671static struct notifier_block xfrm_dev_notifier = {
1672 xfrm_dev_event,
1673 NULL,
1674 0
1675};
1676
1677static void __init xfrm_policy_init(void)
1678{
1679 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
1680 sizeof(struct xfrm_dst),
1681 0, SLAB_HWCACHE_ALIGN,
1682 NULL, NULL);
1683 if (!xfrm_dst_cache)
1684 panic("XFRM: failed to allocate xfrm_dst_cache\n");
1685
1686 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task, NULL);
1687 register_netdevice_notifier(&xfrm_dev_notifier);
1688}
1689
1690void __init xfrm_init(void)
1691{
1692 xfrm_state_init();
1693 xfrm_policy_init();
1694 xfrm_input_init();
1695}
1696
This page took 0.278726 seconds and 5 git commands to generate.