blob: d125a2649037c061ce3b0d83946673d01532be66 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Trent Jaegerdf718372005-12-13 23:12:27 -080013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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>
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -080023#include <linux/netfilter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <net/xfrm.h>
26#include <net/ip.h>
27
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080028DEFINE_MUTEX(xfrm_cfg_mutex);
29EXPORT_SYMBOL(xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31static DEFINE_RWLOCK(xfrm_policy_lock);
32
33struct xfrm_policy *xfrm_policy_list[XFRM_POLICY_MAX*2];
34EXPORT_SYMBOL(xfrm_policy_list);
35
36static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
37static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
38
Eric Dumazetba899662005-08-26 12:05:31 -070039static kmem_cache_t *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41static struct work_struct xfrm_policy_gc_work;
42static struct list_head xfrm_policy_gc_list =
43 LIST_HEAD_INIT(xfrm_policy_gc_list);
44static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
45
46static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
47static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
Herbert Xu546be242006-05-27 23:03:58 -070048static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family);
49static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51int xfrm_register_type(struct xfrm_type *type, unsigned short family)
52{
Herbert Xu546be242006-05-27 23:03:58 -070053 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
54 struct xfrm_type **typemap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 int err = 0;
56
57 if (unlikely(afinfo == NULL))
58 return -EAFNOSUPPORT;
59 typemap = afinfo->type_map;
60
Herbert Xu546be242006-05-27 23:03:58 -070061 if (likely(typemap[type->proto] == NULL))
62 typemap[type->proto] = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 else
64 err = -EEXIST;
Herbert Xu546be242006-05-27 23:03:58 -070065 xfrm_policy_unlock_afinfo(afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return err;
67}
68EXPORT_SYMBOL(xfrm_register_type);
69
70int xfrm_unregister_type(struct xfrm_type *type, unsigned short family)
71{
Herbert Xu546be242006-05-27 23:03:58 -070072 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
73 struct xfrm_type **typemap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 int err = 0;
75
76 if (unlikely(afinfo == NULL))
77 return -EAFNOSUPPORT;
78 typemap = afinfo->type_map;
79
Herbert Xu546be242006-05-27 23:03:58 -070080 if (unlikely(typemap[type->proto] != type))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 err = -ENOENT;
82 else
Herbert Xu546be242006-05-27 23:03:58 -070083 typemap[type->proto] = NULL;
84 xfrm_policy_unlock_afinfo(afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return err;
86}
87EXPORT_SYMBOL(xfrm_unregister_type);
88
89struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
90{
91 struct xfrm_policy_afinfo *afinfo;
Herbert Xu546be242006-05-27 23:03:58 -070092 struct xfrm_type **typemap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 struct xfrm_type *type;
94 int modload_attempted = 0;
95
96retry:
97 afinfo = xfrm_policy_get_afinfo(family);
98 if (unlikely(afinfo == NULL))
99 return NULL;
100 typemap = afinfo->type_map;
101
Herbert Xu546be242006-05-27 23:03:58 -0700102 type = typemap[proto];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (unlikely(type && !try_module_get(type->owner)))
104 type = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (!type && !modload_attempted) {
106 xfrm_policy_put_afinfo(afinfo);
107 request_module("xfrm-type-%d-%d",
108 (int) family, (int) proto);
109 modload_attempted = 1;
110 goto retry;
111 }
112
113 xfrm_policy_put_afinfo(afinfo);
114 return type;
115}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
118 unsigned short family)
119{
120 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
121 int err = 0;
122
123 if (unlikely(afinfo == NULL))
124 return -EAFNOSUPPORT;
125
126 if (likely(afinfo->dst_lookup != NULL))
127 err = afinfo->dst_lookup(dst, fl);
128 else
129 err = -EINVAL;
130 xfrm_policy_put_afinfo(afinfo);
131 return err;
132}
133EXPORT_SYMBOL(xfrm_dst_lookup);
134
135void xfrm_put_type(struct xfrm_type *type)
136{
137 module_put(type->owner);
138}
139
Herbert Xub59f45d2006-05-27 23:05:54 -0700140int xfrm_register_mode(struct xfrm_mode *mode, int family)
141{
142 struct xfrm_policy_afinfo *afinfo;
143 struct xfrm_mode **modemap;
144 int err;
145
146 if (unlikely(mode->encap >= XFRM_MODE_MAX))
147 return -EINVAL;
148
149 afinfo = xfrm_policy_lock_afinfo(family);
150 if (unlikely(afinfo == NULL))
151 return -EAFNOSUPPORT;
152
153 err = -EEXIST;
154 modemap = afinfo->mode_map;
155 if (likely(modemap[mode->encap] == NULL)) {
156 modemap[mode->encap] = mode;
157 err = 0;
158 }
159
160 xfrm_policy_unlock_afinfo(afinfo);
161 return err;
162}
163EXPORT_SYMBOL(xfrm_register_mode);
164
165int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
166{
167 struct xfrm_policy_afinfo *afinfo;
168 struct xfrm_mode **modemap;
169 int err;
170
171 if (unlikely(mode->encap >= XFRM_MODE_MAX))
172 return -EINVAL;
173
174 afinfo = xfrm_policy_lock_afinfo(family);
175 if (unlikely(afinfo == NULL))
176 return -EAFNOSUPPORT;
177
178 err = -ENOENT;
179 modemap = afinfo->mode_map;
180 if (likely(modemap[mode->encap] == mode)) {
181 modemap[mode->encap] = NULL;
182 err = 0;
183 }
184
185 xfrm_policy_unlock_afinfo(afinfo);
186 return err;
187}
188EXPORT_SYMBOL(xfrm_unregister_mode);
189
190struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
191{
192 struct xfrm_policy_afinfo *afinfo;
193 struct xfrm_mode *mode;
194 int modload_attempted = 0;
195
196 if (unlikely(encap >= XFRM_MODE_MAX))
197 return NULL;
198
199retry:
200 afinfo = xfrm_policy_get_afinfo(family);
201 if (unlikely(afinfo == NULL))
202 return NULL;
203
204 mode = afinfo->mode_map[encap];
205 if (unlikely(mode && !try_module_get(mode->owner)))
206 mode = NULL;
207 if (!mode && !modload_attempted) {
208 xfrm_policy_put_afinfo(afinfo);
209 request_module("xfrm-mode-%d-%d", family, encap);
210 modload_attempted = 1;
211 goto retry;
212 }
213
214 xfrm_policy_put_afinfo(afinfo);
215 return mode;
216}
217
218void xfrm_put_mode(struct xfrm_mode *mode)
219{
220 module_put(mode->owner);
221}
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static inline unsigned long make_jiffies(long secs)
224{
225 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
226 return MAX_SCHEDULE_TIMEOUT-1;
227 else
228 return secs*HZ;
229}
230
231static void xfrm_policy_timer(unsigned long data)
232{
233 struct xfrm_policy *xp = (struct xfrm_policy*)data;
234 unsigned long now = (unsigned long)xtime.tv_sec;
235 long next = LONG_MAX;
236 int warn = 0;
237 int dir;
238
239 read_lock(&xp->lock);
240
241 if (xp->dead)
242 goto out;
243
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700244 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 if (xp->lft.hard_add_expires_seconds) {
247 long tmo = xp->lft.hard_add_expires_seconds +
248 xp->curlft.add_time - now;
249 if (tmo <= 0)
250 goto expired;
251 if (tmo < next)
252 next = tmo;
253 }
254 if (xp->lft.hard_use_expires_seconds) {
255 long tmo = xp->lft.hard_use_expires_seconds +
256 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
257 if (tmo <= 0)
258 goto expired;
259 if (tmo < next)
260 next = tmo;
261 }
262 if (xp->lft.soft_add_expires_seconds) {
263 long tmo = xp->lft.soft_add_expires_seconds +
264 xp->curlft.add_time - now;
265 if (tmo <= 0) {
266 warn = 1;
267 tmo = XFRM_KM_TIMEOUT;
268 }
269 if (tmo < next)
270 next = tmo;
271 }
272 if (xp->lft.soft_use_expires_seconds) {
273 long tmo = xp->lft.soft_use_expires_seconds +
274 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
275 if (tmo <= 0) {
276 warn = 1;
277 tmo = XFRM_KM_TIMEOUT;
278 }
279 if (tmo < next)
280 next = tmo;
281 }
282
283 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800284 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (next != LONG_MAX &&
286 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
287 xfrm_pol_hold(xp);
288
289out:
290 read_unlock(&xp->lock);
291 xfrm_pol_put(xp);
292 return;
293
294expired:
295 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700296 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800297 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 xfrm_pol_put(xp);
299}
300
301
302/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
303 * SPD calls.
304 */
305
Al Virodd0fc662005-10-07 07:46:04 +0100306struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 struct xfrm_policy *policy;
309
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700310 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 if (policy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 atomic_set(&policy->refcnt, 1);
314 rwlock_init(&policy->lock);
315 init_timer(&policy->timer);
316 policy->timer.data = (unsigned long)policy;
317 policy->timer.function = xfrm_policy_timer;
318 }
319 return policy;
320}
321EXPORT_SYMBOL(xfrm_policy_alloc);
322
323/* Destroy xfrm_policy: descendant resources must be released to this moment. */
324
325void __xfrm_policy_destroy(struct xfrm_policy *policy)
326{
Kris Katterjohn09a62662006-01-08 22:24:28 -0800327 BUG_ON(!policy->dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Kris Katterjohn09a62662006-01-08 22:24:28 -0800329 BUG_ON(policy->bundles);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 if (del_timer(&policy->timer))
332 BUG();
333
Trent Jaegerdf718372005-12-13 23:12:27 -0800334 security_xfrm_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 kfree(policy);
336}
337EXPORT_SYMBOL(__xfrm_policy_destroy);
338
339static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
340{
341 struct dst_entry *dst;
342
343 while ((dst = policy->bundles) != NULL) {
344 policy->bundles = dst->next;
345 dst_free(dst);
346 }
347
348 if (del_timer(&policy->timer))
349 atomic_dec(&policy->refcnt);
350
351 if (atomic_read(&policy->refcnt) > 1)
352 flow_cache_flush();
353
354 xfrm_pol_put(policy);
355}
356
357static void xfrm_policy_gc_task(void *data)
358{
359 struct xfrm_policy *policy;
360 struct list_head *entry, *tmp;
361 struct list_head gc_list = LIST_HEAD_INIT(gc_list);
362
363 spin_lock_bh(&xfrm_policy_gc_lock);
364 list_splice_init(&xfrm_policy_gc_list, &gc_list);
365 spin_unlock_bh(&xfrm_policy_gc_lock);
366
367 list_for_each_safe(entry, tmp, &gc_list) {
368 policy = list_entry(entry, struct xfrm_policy, list);
369 xfrm_policy_gc_kill(policy);
370 }
371}
372
373/* Rule must be locked. Release descentant resources, announce
374 * entry dead. The rule must be unlinked from lists to the moment.
375 */
376
377static void xfrm_policy_kill(struct xfrm_policy *policy)
378{
379 int dead;
380
381 write_lock_bh(&policy->lock);
382 dead = policy->dead;
383 policy->dead = 1;
384 write_unlock_bh(&policy->lock);
385
386 if (unlikely(dead)) {
387 WARN_ON(1);
388 return;
389 }
390
391 spin_lock(&xfrm_policy_gc_lock);
392 list_add(&policy->list, &xfrm_policy_gc_list);
393 spin_unlock(&xfrm_policy_gc_lock);
394
395 schedule_work(&xfrm_policy_gc_work);
396}
397
398/* Generate new index... KAME seems to generate them ordered by cost
399 * of an absolute inpredictability of ordering of rules. This will not pass. */
400static u32 xfrm_gen_index(int dir)
401{
402 u32 idx;
403 struct xfrm_policy *p;
404 static u32 idx_generator;
405
406 for (;;) {
407 idx = (idx_generator | dir);
408 idx_generator += 8;
409 if (idx == 0)
410 idx = 8;
411 for (p = xfrm_policy_list[dir]; p; p = p->next) {
412 if (p->index == idx)
413 break;
414 }
415 if (!p)
416 return idx;
417 }
418}
419
420int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
421{
422 struct xfrm_policy *pol, **p;
423 struct xfrm_policy *delpol = NULL;
424 struct xfrm_policy **newpos = NULL;
David S. Miller9b78a822005-12-22 07:39:48 -0800425 struct dst_entry *gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 write_lock_bh(&xfrm_policy_lock);
428 for (p = &xfrm_policy_list[dir]; (pol=*p)!=NULL;) {
Trent Jaegerdf718372005-12-13 23:12:27 -0800429 if (!delpol && memcmp(&policy->selector, &pol->selector, sizeof(pol->selector)) == 0 &&
430 xfrm_sec_ctx_match(pol->security, policy->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (excl) {
432 write_unlock_bh(&xfrm_policy_lock);
433 return -EEXIST;
434 }
435 *p = pol->next;
436 delpol = pol;
437 if (policy->priority > pol->priority)
438 continue;
439 } else if (policy->priority >= pol->priority) {
440 p = &pol->next;
441 continue;
442 }
443 if (!newpos)
444 newpos = p;
445 if (delpol)
446 break;
447 p = &pol->next;
448 }
449 if (newpos)
450 p = newpos;
451 xfrm_pol_hold(policy);
452 policy->next = *p;
453 *p = policy;
454 atomic_inc(&flow_cache_genid);
455 policy->index = delpol ? delpol->index : xfrm_gen_index(dir);
456 policy->curlft.add_time = (unsigned long)xtime.tv_sec;
457 policy->curlft.use_time = 0;
458 if (!mod_timer(&policy->timer, jiffies + HZ))
459 xfrm_pol_hold(policy);
460 write_unlock_bh(&xfrm_policy_lock);
461
David S. Miller9b78a822005-12-22 07:39:48 -0800462 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 xfrm_policy_kill(delpol);
David S. Miller9b78a822005-12-22 07:39:48 -0800464
465 read_lock_bh(&xfrm_policy_lock);
466 gc_list = NULL;
467 for (policy = policy->next; policy; policy = policy->next) {
468 struct dst_entry *dst;
469
470 write_lock(&policy->lock);
471 dst = policy->bundles;
472 if (dst) {
473 struct dst_entry *tail = dst;
474 while (tail->next)
475 tail = tail->next;
476 tail->next = gc_list;
477 gc_list = dst;
478
479 policy->bundles = NULL;
480 }
481 write_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
David S. Miller9b78a822005-12-22 07:39:48 -0800483 read_unlock_bh(&xfrm_policy_lock);
484
485 while (gc_list) {
486 struct dst_entry *dst = gc_list;
487
488 gc_list = dst->next;
489 dst_free(dst);
490 }
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return 0;
493}
494EXPORT_SYMBOL(xfrm_policy_insert);
495
Trent Jaegerdf718372005-12-13 23:12:27 -0800496struct xfrm_policy *xfrm_policy_bysel_ctx(int dir, struct xfrm_selector *sel,
497 struct xfrm_sec_ctx *ctx, int delete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 struct xfrm_policy *pol, **p;
500
501 write_lock_bh(&xfrm_policy_lock);
502 for (p = &xfrm_policy_list[dir]; (pol=*p)!=NULL; p = &pol->next) {
Trent Jaegerdf718372005-12-13 23:12:27 -0800503 if ((memcmp(sel, &pol->selector, sizeof(*sel)) == 0) &&
504 (xfrm_sec_ctx_match(ctx, pol->security))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 xfrm_pol_hold(pol);
506 if (delete)
507 *p = pol->next;
508 break;
509 }
510 }
511 write_unlock_bh(&xfrm_policy_lock);
512
513 if (pol && delete) {
514 atomic_inc(&flow_cache_genid);
515 xfrm_policy_kill(pol);
516 }
517 return pol;
518}
Trent Jaegerdf718372005-12-13 23:12:27 -0800519EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521struct xfrm_policy *xfrm_policy_byid(int dir, u32 id, int delete)
522{
523 struct xfrm_policy *pol, **p;
524
525 write_lock_bh(&xfrm_policy_lock);
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700526 for (p = &xfrm_policy_list[dir]; (pol=*p)!=NULL; p = &pol->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (pol->index == id) {
528 xfrm_pol_hold(pol);
529 if (delete)
530 *p = pol->next;
531 break;
532 }
533 }
534 write_unlock_bh(&xfrm_policy_lock);
535
536 if (pol && delete) {
537 atomic_inc(&flow_cache_genid);
538 xfrm_policy_kill(pol);
539 }
540 return pol;
541}
542EXPORT_SYMBOL(xfrm_policy_byid);
543
544void xfrm_policy_flush(void)
545{
546 struct xfrm_policy *xp;
547 int dir;
548
549 write_lock_bh(&xfrm_policy_lock);
550 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
551 while ((xp = xfrm_policy_list[dir]) != NULL) {
552 xfrm_policy_list[dir] = xp->next;
553 write_unlock_bh(&xfrm_policy_lock);
554
555 xfrm_policy_kill(xp);
556
557 write_lock_bh(&xfrm_policy_lock);
558 }
559 }
560 atomic_inc(&flow_cache_genid);
561 write_unlock_bh(&xfrm_policy_lock);
562}
563EXPORT_SYMBOL(xfrm_policy_flush);
564
565int xfrm_policy_walk(int (*func)(struct xfrm_policy *, int, int, void*),
566 void *data)
567{
568 struct xfrm_policy *xp;
569 int dir;
570 int count = 0;
571 int error = 0;
572
573 read_lock_bh(&xfrm_policy_lock);
574 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
575 for (xp = xfrm_policy_list[dir]; xp; xp = xp->next)
576 count++;
577 }
578
579 if (count == 0) {
580 error = -ENOENT;
581 goto out;
582 }
583
584 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
585 for (xp = xfrm_policy_list[dir]; xp; xp = xp->next) {
586 error = func(xp, dir%XFRM_POLICY_MAX, --count, data);
587 if (error)
588 goto out;
589 }
590 }
591
592out:
593 read_unlock_bh(&xfrm_policy_lock);
594 return error;
595}
596EXPORT_SYMBOL(xfrm_policy_walk);
597
598/* Find policy to apply to this flow. */
599
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700600static void xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 void **objp, atomic_t **obj_refp)
602{
603 struct xfrm_policy *pol;
604
605 read_lock_bh(&xfrm_policy_lock);
606 for (pol = xfrm_policy_list[dir]; pol; pol = pol->next) {
607 struct xfrm_selector *sel = &pol->selector;
608 int match;
609
610 if (pol->family != family)
611 continue;
612
613 match = xfrm_selector_match(sel, fl, family);
Trent Jaegerdf718372005-12-13 23:12:27 -0800614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (match) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700616 if (!security_xfrm_policy_lookup(pol, fl->secid, dir)) {
Trent Jaegerdf718372005-12-13 23:12:27 -0800617 xfrm_pol_hold(pol);
618 break;
619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621 }
622 read_unlock_bh(&xfrm_policy_lock);
623 if ((*objp = (void *) pol) != NULL)
624 *obj_refp = &pol->refcnt;
625}
626
Trent Jaegerdf718372005-12-13 23:12:27 -0800627static inline int policy_to_flow_dir(int dir)
628{
629 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
630 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
631 XFRM_POLICY_FWD == FLOW_DIR_FWD)
632 return dir;
633 switch (dir) {
634 default:
635 case XFRM_POLICY_IN:
636 return FLOW_DIR_IN;
637 case XFRM_POLICY_OUT:
638 return FLOW_DIR_OUT;
639 case XFRM_POLICY_FWD:
640 return FLOW_DIR_FWD;
641 };
642}
643
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700644static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 struct xfrm_policy *pol;
647
648 read_lock_bh(&xfrm_policy_lock);
649 if ((pol = sk->sk_policy[dir]) != NULL) {
Trent Jaegerdf718372005-12-13 23:12:27 -0800650 int match = xfrm_selector_match(&pol->selector, fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 sk->sk_family);
Trent Jaegerdf718372005-12-13 23:12:27 -0800652 int err = 0;
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (match)
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700655 err = security_xfrm_policy_lookup(pol, fl->secid, policy_to_flow_dir(dir));
Trent Jaegerdf718372005-12-13 23:12:27 -0800656
657 if (match && !err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 xfrm_pol_hold(pol);
659 else
660 pol = NULL;
661 }
662 read_unlock_bh(&xfrm_policy_lock);
663 return pol;
664}
665
666static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
667{
668 pol->next = xfrm_policy_list[dir];
669 xfrm_policy_list[dir] = pol;
670 xfrm_pol_hold(pol);
671}
672
673static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
674 int dir)
675{
676 struct xfrm_policy **polp;
677
678 for (polp = &xfrm_policy_list[dir];
679 *polp != NULL; polp = &(*polp)->next) {
680 if (*polp == pol) {
681 *polp = pol->next;
682 return pol;
683 }
684 }
685 return NULL;
686}
687
Herbert Xu4666faa2005-06-18 22:43:22 -0700688int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 write_lock_bh(&xfrm_policy_lock);
691 pol = __xfrm_policy_unlink(pol, dir);
692 write_unlock_bh(&xfrm_policy_lock);
693 if (pol) {
694 if (dir < XFRM_POLICY_MAX)
695 atomic_inc(&flow_cache_genid);
696 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -0700697 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
Herbert Xu4666faa2005-06-18 22:43:22 -0700699 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
David S. Millera70fcb02006-03-20 19:18:52 -0800701EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
704{
705 struct xfrm_policy *old_pol;
706
707 write_lock_bh(&xfrm_policy_lock);
708 old_pol = sk->sk_policy[dir];
709 sk->sk_policy[dir] = pol;
710 if (pol) {
711 pol->curlft.add_time = (unsigned long)xtime.tv_sec;
712 pol->index = xfrm_gen_index(XFRM_POLICY_MAX+dir);
713 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
714 }
715 if (old_pol)
716 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
717 write_unlock_bh(&xfrm_policy_lock);
718
719 if (old_pol) {
720 xfrm_policy_kill(old_pol);
721 }
722 return 0;
723}
724
725static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
726{
727 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
728
729 if (newp) {
730 newp->selector = old->selector;
Trent Jaegerdf718372005-12-13 23:12:27 -0800731 if (security_xfrm_policy_clone(old, newp)) {
732 kfree(newp);
733 return NULL; /* ENOMEM */
734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 newp->lft = old->lft;
736 newp->curlft = old->curlft;
737 newp->action = old->action;
738 newp->flags = old->flags;
739 newp->xfrm_nr = old->xfrm_nr;
740 newp->index = old->index;
741 memcpy(newp->xfrm_vec, old->xfrm_vec,
742 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
743 write_lock_bh(&xfrm_policy_lock);
744 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
745 write_unlock_bh(&xfrm_policy_lock);
746 xfrm_pol_put(newp);
747 }
748 return newp;
749}
750
751int __xfrm_sk_clone_policy(struct sock *sk)
752{
753 struct xfrm_policy *p0 = sk->sk_policy[0],
754 *p1 = sk->sk_policy[1];
755
756 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
757 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
758 return -ENOMEM;
759 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
760 return -ENOMEM;
761 return 0;
762}
763
764/* Resolve list of templates for the flow, given policy. */
765
766static int
767xfrm_tmpl_resolve(struct xfrm_policy *policy, struct flowi *fl,
768 struct xfrm_state **xfrm,
769 unsigned short family)
770{
771 int nx;
772 int i, error;
773 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
774 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
775
776 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
777 struct xfrm_state *x;
778 xfrm_address_t *remote = daddr;
779 xfrm_address_t *local = saddr;
780 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
781
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700782 if (tmpl->mode == XFRM_MODE_TUNNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 remote = &tmpl->id.daddr;
784 local = &tmpl->saddr;
785 }
786
787 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
788
789 if (x && x->km.state == XFRM_STATE_VALID) {
790 xfrm[nx++] = x;
791 daddr = remote;
792 saddr = local;
793 continue;
794 }
795 if (x) {
796 error = (x->km.state == XFRM_STATE_ERROR ?
797 -EINVAL : -EAGAIN);
798 xfrm_state_put(x);
799 }
800
801 if (!tmpl->optional)
802 goto fail;
803 }
804 return nx;
805
806fail:
807 for (nx--; nx>=0; nx--)
808 xfrm_state_put(xfrm[nx]);
809 return error;
810}
811
812/* Check that the bundle accepts the flow and its components are
813 * still valid.
814 */
815
816static struct dst_entry *
817xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
818{
819 struct dst_entry *x;
820 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
821 if (unlikely(afinfo == NULL))
822 return ERR_PTR(-EINVAL);
823 x = afinfo->find_bundle(fl, policy);
824 xfrm_policy_put_afinfo(afinfo);
825 return x;
826}
827
828/* Allocate chain of dst_entry's, attach known xfrm's, calculate
829 * all the metrics... Shortly, bundle a bundle.
830 */
831
832static int
833xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
834 struct flowi *fl, struct dst_entry **dst_p,
835 unsigned short family)
836{
837 int err;
838 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
839 if (unlikely(afinfo == NULL))
840 return -EINVAL;
841 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
842 xfrm_policy_put_afinfo(afinfo);
843 return err;
844}
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847static int stale_bundle(struct dst_entry *dst);
848
849/* Main function: finds/creates a bundle for given flow.
850 *
851 * At the moment we eat a raw IP route. Mostly to speed up lookups
852 * on interfaces with disabled IPsec.
853 */
854int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
855 struct sock *sk, int flags)
856{
857 struct xfrm_policy *policy;
858 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
859 struct dst_entry *dst, *dst_orig = *dst_p;
860 int nx = 0;
861 int err;
862 u32 genid;
Patrick McHardy42cf93c2006-02-21 13:37:35 -0800863 u16 family;
Trent Jaegerdf718372005-12-13 23:12:27 -0800864 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866restart:
867 genid = atomic_read(&flow_cache_genid);
868 policy = NULL;
869 if (sk && sk->sk_policy[1])
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700870 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 if (!policy) {
873 /* To accelerate a bit... */
874 if ((dst_orig->flags & DST_NOXFRM) || !xfrm_policy_list[XFRM_POLICY_OUT])
875 return 0;
876
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700877 policy = flow_cache_lookup(fl, dst_orig->ops->family,
Patrick McHardy42cf93c2006-02-21 13:37:35 -0800878 dir, xfrm_policy_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
880
881 if (!policy)
882 return 0;
883
Patrick McHardy42cf93c2006-02-21 13:37:35 -0800884 family = dst_orig->ops->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 policy->curlft.use_time = (unsigned long)xtime.tv_sec;
886
887 switch (policy->action) {
888 case XFRM_POLICY_BLOCK:
889 /* Prohibit the flow */
Patrick McHardye104411b2005-09-08 15:11:55 -0700890 err = -EPERM;
891 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 case XFRM_POLICY_ALLOW:
894 if (policy->xfrm_nr == 0) {
895 /* Flow passes not transformed. */
896 xfrm_pol_put(policy);
897 return 0;
898 }
899
900 /* Try to find matching bundle.
901 *
902 * LATER: help from flow cache. It is optional, this
903 * is required only for output policy.
904 */
905 dst = xfrm_find_bundle(fl, policy, family);
906 if (IS_ERR(dst)) {
Patrick McHardye104411b2005-09-08 15:11:55 -0700907 err = PTR_ERR(dst);
908 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910
911 if (dst)
912 break;
913
914 nx = xfrm_tmpl_resolve(policy, fl, xfrm, family);
915
916 if (unlikely(nx<0)) {
917 err = nx;
918 if (err == -EAGAIN && flags) {
919 DECLARE_WAITQUEUE(wait, current);
920
921 add_wait_queue(&km_waitq, &wait);
922 set_current_state(TASK_INTERRUPTIBLE);
923 schedule();
924 set_current_state(TASK_RUNNING);
925 remove_wait_queue(&km_waitq, &wait);
926
927 nx = xfrm_tmpl_resolve(policy, fl, xfrm, family);
928
929 if (nx == -EAGAIN && signal_pending(current)) {
930 err = -ERESTART;
931 goto error;
932 }
933 if (nx == -EAGAIN ||
934 genid != atomic_read(&flow_cache_genid)) {
935 xfrm_pol_put(policy);
936 goto restart;
937 }
938 err = nx;
939 }
940 if (err < 0)
941 goto error;
942 }
943 if (nx == 0) {
944 /* Flow passes not transformed. */
945 xfrm_pol_put(policy);
946 return 0;
947 }
948
949 dst = dst_orig;
950 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
951
952 if (unlikely(err)) {
953 int i;
954 for (i=0; i<nx; i++)
955 xfrm_state_put(xfrm[i]);
956 goto error;
957 }
958
959 write_lock_bh(&policy->lock);
960 if (unlikely(policy->dead || stale_bundle(dst))) {
961 /* Wow! While we worked on resolving, this
962 * policy has gone. Retry. It is not paranoia,
963 * we just cannot enlist new bundle to dead object.
964 * We can't enlist stable bundles either.
965 */
966 write_unlock_bh(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (dst)
968 dst_free(dst);
Herbert Xu00de6512006-02-13 16:01:27 -0800969
970 err = -EHOSTUNREACH;
971 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973 dst->next = policy->bundles;
974 policy->bundles = dst;
975 dst_hold(dst);
976 write_unlock_bh(&policy->lock);
977 }
978 *dst_p = dst;
979 dst_release(dst_orig);
980 xfrm_pol_put(policy);
981 return 0;
982
983error:
984 dst_release(dst_orig);
985 xfrm_pol_put(policy);
986 *dst_p = NULL;
987 return err;
988}
989EXPORT_SYMBOL(xfrm_lookup);
990
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -0700991static inline int
992xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
993{
994 struct xfrm_state *x;
995 int err;
996
997 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
998 return 0;
999 x = skb->sp->xvec[idx];
1000 if (!x->type->reject)
1001 return 0;
1002 xfrm_state_hold(x);
1003 err = x->type->reject(x, skb, fl);
1004 xfrm_state_put(x);
1005 return err;
1006}
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008/* When skb is transformed back to its "native" form, we have to
1009 * check policy restrictions. At the moment we make this in maximally
1010 * stupid way. Shame on me. :-) Of course, connected sockets must
1011 * have policy cached at them.
1012 */
1013
1014static inline int
1015xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
1016 unsigned short family)
1017{
1018 if (xfrm_state_kern(x))
1019 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family);
1020 return x->id.proto == tmpl->id.proto &&
1021 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1022 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1023 x->props.mode == tmpl->mode &&
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07001024 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1025 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001026 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1027 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001030/*
1031 * 0 or more than 0 is returned when validation is succeeded (either bypass
1032 * because of optional transport mode, or next index of the mathced secpath
1033 * state with the template.
1034 * -1 is returned when no matching template is found.
1035 * Otherwise "-2 - errored_index" is returned.
1036 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037static inline int
1038xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1039 unsigned short family)
1040{
1041 int idx = start;
1042
1043 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001044 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return start;
1046 } else
1047 start = -1;
1048 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001049 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001051 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1052 if (start == -1)
1053 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057 return start;
1058}
1059
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001060int
1061xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001064 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 if (unlikely(afinfo == NULL))
1067 return -EAFNOSUPPORT;
1068
1069 afinfo->decode_session(skb, fl);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07001070 err = security_xfrm_decode_session(skb, &fl->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001072 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001074EXPORT_SYMBOL(xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001076static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001079 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
1080 if (idxp)
1081 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085
1086 return 0;
1087}
1088
1089int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
1090 unsigned short family)
1091{
1092 struct xfrm_policy *pol;
1093 struct flowi fl;
Trent Jaegerdf718372005-12-13 23:12:27 -08001094 u8 fl_dir = policy_to_flow_dir(dir);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001095 int xerr_idx = -1;
1096 int *xerr_idxp = &xerr_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001098 if (xfrm_decode_session(skb, &fl, family) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return 0;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08001100 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 /* First, check used SA against their selectors. */
1103 if (skb->sp) {
1104 int i;
1105
1106 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001107 struct xfrm_state *x = skb->sp->xvec[i];
1108 if (!xfrm_selector_match(&x->sel, &fl, family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
1111 }
1112
1113 pol = NULL;
1114 if (sk && sk->sk_policy[dir])
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001115 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 if (!pol)
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001118 pol = flow_cache_lookup(&fl, family, fl_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 xfrm_policy_lookup);
1120
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001121 if (!pol) {
1122 if (skb->sp && secpath_has_nontransport(skb->sp, 0, xerr_idxp)) {
1123 xfrm_secpath_reject(xerr_idx, skb, &fl);
1124 return 0;
1125 }
1126 return 1;
1127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 pol->curlft.use_time = (unsigned long)xtime.tv_sec;
1130
1131 if (pol->action == XFRM_POLICY_ALLOW) {
1132 struct sec_path *sp;
1133 static struct sec_path dummy;
1134 int i, k;
1135
1136 if ((sp = skb->sp) == NULL)
1137 sp = &dummy;
1138
1139 /* For each tunnel xfrm, find the first matching tmpl.
1140 * For each tmpl before that, find corresponding xfrm.
1141 * Order is _important_. Later we will implement
1142 * some barriers, but at the moment barriers
1143 * are implied between each two transformations.
1144 */
1145 for (i = pol->xfrm_nr-1, k = 0; i >= 0; i--) {
1146 k = xfrm_policy_ok(pol->xfrm_vec+i, sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001147 if (k < 0) {
1148 if (k < -1 && xerr_idxp)
1149 *xerr_idxp = -(2+k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
1153
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001154 if (secpath_has_nontransport(sp, k, xerr_idxp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 goto reject;
1156
1157 xfrm_pol_put(pol);
1158 return 1;
1159 }
1160
1161reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001162 xfrm_secpath_reject(xerr_idx, skb, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 xfrm_pol_put(pol);
1164 return 0;
1165}
1166EXPORT_SYMBOL(__xfrm_policy_check);
1167
1168int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1169{
1170 struct flowi fl;
1171
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001172 if (xfrm_decode_session(skb, &fl, family) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return 0;
1174
1175 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1176}
1177EXPORT_SYMBOL(__xfrm_route_forward);
1178
David S. Millerd49c73c2006-08-13 18:55:53 -07001179/* Optimize later using cookies and generation ids. */
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1182{
David S. Millerd49c73c2006-08-13 18:55:53 -07001183 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1184 * to "-1" to force all XFRM destinations to get validated by
1185 * dst_ops->check on every use. We do this because when a
1186 * normal route referenced by an XFRM dst is obsoleted we do
1187 * not go looking around for all parent referencing XFRM dsts
1188 * so that we can invalidate them. It is just too much work.
1189 * Instead we make the checks here on every use. For example:
1190 *
1191 * XFRM dst A --> IPv4 dst X
1192 *
1193 * X is the "xdst->route" of A (X is also the "dst->path" of A
1194 * in this example). If X is marked obsolete, "A" will not
1195 * notice. That's what we are validating here via the
1196 * stale_bundle() check.
1197 *
1198 * When a policy's bundle is pruned, we dst_free() the XFRM
1199 * dst which causes it's ->obsolete field to be set to a
1200 * positive non-zero integer. If an XFRM dst has been pruned
1201 * like this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08001202 */
David S. Millerd49c73c2006-08-13 18:55:53 -07001203 if (dst->obsolete < 0 && !stale_bundle(dst))
1204 return dst;
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return NULL;
1207}
1208
1209static int stale_bundle(struct dst_entry *dst)
1210{
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07001211 return !xfrm_bundle_ok((struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Herbert Xuaabc9762005-05-03 16:27:10 -07001214void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
1217 dst->dev = &loopback_dev;
1218 dev_hold(&loopback_dev);
1219 dev_put(dev);
1220 }
1221}
Herbert Xuaabc9762005-05-03 16:27:10 -07001222EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224static void xfrm_link_failure(struct sk_buff *skb)
1225{
1226 /* Impossible. Such dst must be popped before reaches point of failure. */
1227 return;
1228}
1229
1230static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1231{
1232 if (dst) {
1233 if (dst->obsolete) {
1234 dst_release(dst);
1235 dst = NULL;
1236 }
1237 }
1238 return dst;
1239}
1240
1241static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1242{
1243 int i;
1244 struct xfrm_policy *pol;
1245 struct dst_entry *dst, **dstp, *gc_list = NULL;
1246
1247 read_lock_bh(&xfrm_policy_lock);
1248 for (i=0; i<2*XFRM_POLICY_MAX; i++) {
1249 for (pol = xfrm_policy_list[i]; pol; pol = pol->next) {
1250 write_lock(&pol->lock);
1251 dstp = &pol->bundles;
1252 while ((dst=*dstp) != NULL) {
1253 if (func(dst)) {
1254 *dstp = dst->next;
1255 dst->next = gc_list;
1256 gc_list = dst;
1257 } else {
1258 dstp = &dst->next;
1259 }
1260 }
1261 write_unlock(&pol->lock);
1262 }
1263 }
1264 read_unlock_bh(&xfrm_policy_lock);
1265
1266 while (gc_list) {
1267 dst = gc_list;
1268 gc_list = dst->next;
1269 dst_free(dst);
1270 }
1271}
1272
1273static int unused_bundle(struct dst_entry *dst)
1274{
1275 return !atomic_read(&dst->__refcnt);
1276}
1277
1278static void __xfrm_garbage_collect(void)
1279{
1280 xfrm_prune_bundles(unused_bundle);
1281}
1282
1283int xfrm_flush_bundles(void)
1284{
1285 xfrm_prune_bundles(stale_bundle);
1286 return 0;
1287}
1288
David S. Miller399c1802005-12-19 14:23:23 -08001289static int always_true(struct dst_entry *dst)
1290{
1291 return 1;
1292}
1293
1294void xfrm_flush_all_bundles(void)
1295{
1296 xfrm_prune_bundles(always_true);
1297}
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299void xfrm_init_pmtu(struct dst_entry *dst)
1300{
1301 do {
1302 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1303 u32 pmtu, route_mtu_cached;
1304
1305 pmtu = dst_mtu(dst->child);
1306 xdst->child_mtu_cached = pmtu;
1307
1308 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
1309
1310 route_mtu_cached = dst_mtu(xdst->route);
1311 xdst->route_mtu_cached = route_mtu_cached;
1312
1313 if (pmtu > route_mtu_cached)
1314 pmtu = route_mtu_cached;
1315
1316 dst->metrics[RTAX_MTU-1] = pmtu;
1317 } while ((dst = dst->next));
1318}
1319
1320EXPORT_SYMBOL(xfrm_init_pmtu);
1321
1322/* Check that the bundle accepts the flow and its components are
1323 * still valid.
1324 */
1325
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07001326int xfrm_bundle_ok(struct xfrm_dst *first, struct flowi *fl, int family, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
1328 struct dst_entry *dst = &first->u.dst;
1329 struct xfrm_dst *last;
1330 u32 mtu;
1331
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07001332 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 (dst->dev && !netif_running(dst->dev)))
1334 return 0;
1335
1336 last = NULL;
1337
1338 do {
1339 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1340
1341 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
1342 return 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001343 if (fl && !security_xfrm_flow_state_match(fl, dst->xfrm))
1344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (dst->xfrm->km.state != XFRM_STATE_VALID)
1346 return 0;
1347
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07001348 if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
1349 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
1350 return 0;
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 mtu = dst_mtu(dst->child);
1353 if (xdst->child_mtu_cached != mtu) {
1354 last = xdst;
1355 xdst->child_mtu_cached = mtu;
1356 }
1357
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07001358 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return 0;
1360 mtu = dst_mtu(xdst->route);
1361 if (xdst->route_mtu_cached != mtu) {
1362 last = xdst;
1363 xdst->route_mtu_cached = mtu;
1364 }
1365
1366 dst = dst->child;
1367 } while (dst->xfrm);
1368
1369 if (likely(!last))
1370 return 1;
1371
1372 mtu = last->child_mtu_cached;
1373 for (;;) {
1374 dst = &last->u.dst;
1375
1376 mtu = xfrm_state_mtu(dst->xfrm, mtu);
1377 if (mtu > last->route_mtu_cached)
1378 mtu = last->route_mtu_cached;
1379 dst->metrics[RTAX_MTU-1] = mtu;
1380
1381 if (last == first)
1382 break;
1383
1384 last = last->u.next;
1385 last->child_mtu_cached = mtu;
1386 }
1387
1388 return 1;
1389}
1390
1391EXPORT_SYMBOL(xfrm_bundle_ok);
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
1394{
1395 int err = 0;
1396 if (unlikely(afinfo == NULL))
1397 return -EINVAL;
1398 if (unlikely(afinfo->family >= NPROTO))
1399 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07001400 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
1402 err = -ENOBUFS;
1403 else {
1404 struct dst_ops *dst_ops = afinfo->dst_ops;
1405 if (likely(dst_ops->kmem_cachep == NULL))
1406 dst_ops->kmem_cachep = xfrm_dst_cache;
1407 if (likely(dst_ops->check == NULL))
1408 dst_ops->check = xfrm_dst_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (likely(dst_ops->negative_advice == NULL))
1410 dst_ops->negative_advice = xfrm_negative_advice;
1411 if (likely(dst_ops->link_failure == NULL))
1412 dst_ops->link_failure = xfrm_link_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (likely(afinfo->garbage_collect == NULL))
1414 afinfo->garbage_collect = __xfrm_garbage_collect;
1415 xfrm_policy_afinfo[afinfo->family] = afinfo;
1416 }
Ingo Molnare959d812006-04-28 15:32:29 -07001417 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return err;
1419}
1420EXPORT_SYMBOL(xfrm_policy_register_afinfo);
1421
1422int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
1423{
1424 int err = 0;
1425 if (unlikely(afinfo == NULL))
1426 return -EINVAL;
1427 if (unlikely(afinfo->family >= NPROTO))
1428 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07001429 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
1431 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
1432 err = -EINVAL;
1433 else {
1434 struct dst_ops *dst_ops = afinfo->dst_ops;
1435 xfrm_policy_afinfo[afinfo->family] = NULL;
1436 dst_ops->kmem_cachep = NULL;
1437 dst_ops->check = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 dst_ops->negative_advice = NULL;
1439 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 afinfo->garbage_collect = NULL;
1441 }
1442 }
Ingo Molnare959d812006-04-28 15:32:29 -07001443 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return err;
1445}
1446EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
1447
1448static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
1449{
1450 struct xfrm_policy_afinfo *afinfo;
1451 if (unlikely(family >= NPROTO))
1452 return NULL;
1453 read_lock(&xfrm_policy_afinfo_lock);
1454 afinfo = xfrm_policy_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07001455 if (unlikely(!afinfo))
1456 read_unlock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return afinfo;
1458}
1459
1460static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
1461{
Herbert Xu546be242006-05-27 23:03:58 -07001462 read_unlock(&xfrm_policy_afinfo_lock);
1463}
1464
1465static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
1466{
1467 struct xfrm_policy_afinfo *afinfo;
1468 if (unlikely(family >= NPROTO))
1469 return NULL;
1470 write_lock_bh(&xfrm_policy_afinfo_lock);
1471 afinfo = xfrm_policy_afinfo[family];
1472 if (unlikely(!afinfo))
1473 write_unlock_bh(&xfrm_policy_afinfo_lock);
1474 return afinfo;
1475}
1476
1477static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
1478{
1479 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
1482static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
1483{
1484 switch (event) {
1485 case NETDEV_DOWN:
1486 xfrm_flush_bundles();
1487 }
1488 return NOTIFY_DONE;
1489}
1490
1491static struct notifier_block xfrm_dev_notifier = {
1492 xfrm_dev_event,
1493 NULL,
1494 0
1495};
1496
1497static void __init xfrm_policy_init(void)
1498{
1499 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
1500 sizeof(struct xfrm_dst),
1501 0, SLAB_HWCACHE_ALIGN,
1502 NULL, NULL);
1503 if (!xfrm_dst_cache)
1504 panic("XFRM: failed to allocate xfrm_dst_cache\n");
1505
1506 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task, NULL);
1507 register_netdevice_notifier(&xfrm_dev_notifier);
1508}
1509
1510void __init xfrm_init(void)
1511{
1512 xfrm_state_init();
1513 xfrm_policy_init();
1514 xfrm_input_init();
1515}
1516