blob: 087a5443b0514435dd9c7a2303adae43b691b7cc [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>
David S. Miller2518c7c2006-08-24 04:45:07 -070025#include <linux/bootmem.h>
26#include <linux/vmalloc.h>
27#include <linux/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <net/xfrm.h>
29#include <net/ip.h>
30
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080031DEFINE_MUTEX(xfrm_cfg_mutex);
32EXPORT_SYMBOL(xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34static DEFINE_RWLOCK(xfrm_policy_lock);
35
David S. Miller2518c7c2006-08-24 04:45:07 -070036unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
37EXPORT_SYMBOL(xfrm_policy_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
40static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
41
Eric Dumazetba899662005-08-26 12:05:31 -070042static kmem_cache_t *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44static struct work_struct xfrm_policy_gc_work;
David S. Miller2518c7c2006-08-24 04:45:07 -070045static HLIST_HEAD(xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
47
48static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
49static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
Herbert Xu546be242006-05-27 23:03:58 -070050static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family);
51static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53int xfrm_register_type(struct xfrm_type *type, unsigned short family)
54{
Herbert Xu546be242006-05-27 23:03:58 -070055 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
56 struct xfrm_type **typemap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 int err = 0;
58
59 if (unlikely(afinfo == NULL))
60 return -EAFNOSUPPORT;
61 typemap = afinfo->type_map;
62
Herbert Xu546be242006-05-27 23:03:58 -070063 if (likely(typemap[type->proto] == NULL))
64 typemap[type->proto] = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 else
66 err = -EEXIST;
Herbert Xu546be242006-05-27 23:03:58 -070067 xfrm_policy_unlock_afinfo(afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return err;
69}
70EXPORT_SYMBOL(xfrm_register_type);
71
72int xfrm_unregister_type(struct xfrm_type *type, unsigned short family)
73{
Herbert Xu546be242006-05-27 23:03:58 -070074 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
75 struct xfrm_type **typemap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 int err = 0;
77
78 if (unlikely(afinfo == NULL))
79 return -EAFNOSUPPORT;
80 typemap = afinfo->type_map;
81
Herbert Xu546be242006-05-27 23:03:58 -070082 if (unlikely(typemap[type->proto] != type))
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 err = -ENOENT;
84 else
Herbert Xu546be242006-05-27 23:03:58 -070085 typemap[type->proto] = NULL;
86 xfrm_policy_unlock_afinfo(afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return err;
88}
89EXPORT_SYMBOL(xfrm_unregister_type);
90
91struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
92{
93 struct xfrm_policy_afinfo *afinfo;
Herbert Xu546be242006-05-27 23:03:58 -070094 struct xfrm_type **typemap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct xfrm_type *type;
96 int modload_attempted = 0;
97
98retry:
99 afinfo = xfrm_policy_get_afinfo(family);
100 if (unlikely(afinfo == NULL))
101 return NULL;
102 typemap = afinfo->type_map;
103
Herbert Xu546be242006-05-27 23:03:58 -0700104 type = typemap[proto];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (unlikely(type && !try_module_get(type->owner)))
106 type = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (!type && !modload_attempted) {
108 xfrm_policy_put_afinfo(afinfo);
109 request_module("xfrm-type-%d-%d",
110 (int) family, (int) proto);
111 modload_attempted = 1;
112 goto retry;
113 }
114
115 xfrm_policy_put_afinfo(afinfo);
116 return type;
117}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
120 unsigned short family)
121{
122 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
123 int err = 0;
124
125 if (unlikely(afinfo == NULL))
126 return -EAFNOSUPPORT;
127
128 if (likely(afinfo->dst_lookup != NULL))
129 err = afinfo->dst_lookup(dst, fl);
130 else
131 err = -EINVAL;
132 xfrm_policy_put_afinfo(afinfo);
133 return err;
134}
135EXPORT_SYMBOL(xfrm_dst_lookup);
136
137void xfrm_put_type(struct xfrm_type *type)
138{
139 module_put(type->owner);
140}
141
Herbert Xub59f45d2006-05-27 23:05:54 -0700142int xfrm_register_mode(struct xfrm_mode *mode, int family)
143{
144 struct xfrm_policy_afinfo *afinfo;
145 struct xfrm_mode **modemap;
146 int err;
147
148 if (unlikely(mode->encap >= XFRM_MODE_MAX))
149 return -EINVAL;
150
151 afinfo = xfrm_policy_lock_afinfo(family);
152 if (unlikely(afinfo == NULL))
153 return -EAFNOSUPPORT;
154
155 err = -EEXIST;
156 modemap = afinfo->mode_map;
157 if (likely(modemap[mode->encap] == NULL)) {
158 modemap[mode->encap] = mode;
159 err = 0;
160 }
161
162 xfrm_policy_unlock_afinfo(afinfo);
163 return err;
164}
165EXPORT_SYMBOL(xfrm_register_mode);
166
167int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
168{
169 struct xfrm_policy_afinfo *afinfo;
170 struct xfrm_mode **modemap;
171 int err;
172
173 if (unlikely(mode->encap >= XFRM_MODE_MAX))
174 return -EINVAL;
175
176 afinfo = xfrm_policy_lock_afinfo(family);
177 if (unlikely(afinfo == NULL))
178 return -EAFNOSUPPORT;
179
180 err = -ENOENT;
181 modemap = afinfo->mode_map;
182 if (likely(modemap[mode->encap] == mode)) {
183 modemap[mode->encap] = NULL;
184 err = 0;
185 }
186
187 xfrm_policy_unlock_afinfo(afinfo);
188 return err;
189}
190EXPORT_SYMBOL(xfrm_unregister_mode);
191
192struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
193{
194 struct xfrm_policy_afinfo *afinfo;
195 struct xfrm_mode *mode;
196 int modload_attempted = 0;
197
198 if (unlikely(encap >= XFRM_MODE_MAX))
199 return NULL;
200
201retry:
202 afinfo = xfrm_policy_get_afinfo(family);
203 if (unlikely(afinfo == NULL))
204 return NULL;
205
206 mode = afinfo->mode_map[encap];
207 if (unlikely(mode && !try_module_get(mode->owner)))
208 mode = NULL;
209 if (!mode && !modload_attempted) {
210 xfrm_policy_put_afinfo(afinfo);
211 request_module("xfrm-mode-%d-%d", family, encap);
212 modload_attempted = 1;
213 goto retry;
214 }
215
216 xfrm_policy_put_afinfo(afinfo);
217 return mode;
218}
219
220void xfrm_put_mode(struct xfrm_mode *mode)
221{
222 module_put(mode->owner);
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static inline unsigned long make_jiffies(long secs)
226{
227 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
228 return MAX_SCHEDULE_TIMEOUT-1;
229 else
230 return secs*HZ;
231}
232
233static void xfrm_policy_timer(unsigned long data)
234{
235 struct xfrm_policy *xp = (struct xfrm_policy*)data;
236 unsigned long now = (unsigned long)xtime.tv_sec;
237 long next = LONG_MAX;
238 int warn = 0;
239 int dir;
240
241 read_lock(&xp->lock);
242
243 if (xp->dead)
244 goto out;
245
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700246 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 if (xp->lft.hard_add_expires_seconds) {
249 long tmo = xp->lft.hard_add_expires_seconds +
250 xp->curlft.add_time - now;
251 if (tmo <= 0)
252 goto expired;
253 if (tmo < next)
254 next = tmo;
255 }
256 if (xp->lft.hard_use_expires_seconds) {
257 long tmo = xp->lft.hard_use_expires_seconds +
258 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
259 if (tmo <= 0)
260 goto expired;
261 if (tmo < next)
262 next = tmo;
263 }
264 if (xp->lft.soft_add_expires_seconds) {
265 long tmo = xp->lft.soft_add_expires_seconds +
266 xp->curlft.add_time - now;
267 if (tmo <= 0) {
268 warn = 1;
269 tmo = XFRM_KM_TIMEOUT;
270 }
271 if (tmo < next)
272 next = tmo;
273 }
274 if (xp->lft.soft_use_expires_seconds) {
275 long tmo = xp->lft.soft_use_expires_seconds +
276 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
277 if (tmo <= 0) {
278 warn = 1;
279 tmo = XFRM_KM_TIMEOUT;
280 }
281 if (tmo < next)
282 next = tmo;
283 }
284
285 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800286 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (next != LONG_MAX &&
288 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
289 xfrm_pol_hold(xp);
290
291out:
292 read_unlock(&xp->lock);
293 xfrm_pol_put(xp);
294 return;
295
296expired:
297 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700298 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800299 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 xfrm_pol_put(xp);
301}
302
303
304/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
305 * SPD calls.
306 */
307
Al Virodd0fc662005-10-07 07:46:04 +0100308struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 struct xfrm_policy *policy;
311
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700312 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 if (policy) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700315 INIT_HLIST_NODE(&policy->bydst);
316 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700318 atomic_set(&policy->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 init_timer(&policy->timer);
320 policy->timer.data = (unsigned long)policy;
321 policy->timer.function = xfrm_policy_timer;
322 }
323 return policy;
324}
325EXPORT_SYMBOL(xfrm_policy_alloc);
326
327/* Destroy xfrm_policy: descendant resources must be released to this moment. */
328
329void __xfrm_policy_destroy(struct xfrm_policy *policy)
330{
Kris Katterjohn09a62662006-01-08 22:24:28 -0800331 BUG_ON(!policy->dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Kris Katterjohn09a62662006-01-08 22:24:28 -0800333 BUG_ON(policy->bundles);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 if (del_timer(&policy->timer))
336 BUG();
337
Trent Jaegerdf718372005-12-13 23:12:27 -0800338 security_xfrm_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 kfree(policy);
340}
341EXPORT_SYMBOL(__xfrm_policy_destroy);
342
343static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
344{
345 struct dst_entry *dst;
346
347 while ((dst = policy->bundles) != NULL) {
348 policy->bundles = dst->next;
349 dst_free(dst);
350 }
351
352 if (del_timer(&policy->timer))
353 atomic_dec(&policy->refcnt);
354
355 if (atomic_read(&policy->refcnt) > 1)
356 flow_cache_flush();
357
358 xfrm_pol_put(policy);
359}
360
361static void xfrm_policy_gc_task(void *data)
362{
363 struct xfrm_policy *policy;
David S. Miller2518c7c2006-08-24 04:45:07 -0700364 struct hlist_node *entry, *tmp;
365 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 spin_lock_bh(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700368 gc_list.first = xfrm_policy_gc_list.first;
369 INIT_HLIST_HEAD(&xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 spin_unlock_bh(&xfrm_policy_gc_lock);
371
David S. Miller2518c7c2006-08-24 04:45:07 -0700372 hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 xfrm_policy_gc_kill(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376/* Rule must be locked. Release descentant resources, announce
377 * entry dead. The rule must be unlinked from lists to the moment.
378 */
379
380static void xfrm_policy_kill(struct xfrm_policy *policy)
381{
382 int dead;
383
384 write_lock_bh(&policy->lock);
385 dead = policy->dead;
386 policy->dead = 1;
387 write_unlock_bh(&policy->lock);
388
389 if (unlikely(dead)) {
390 WARN_ON(1);
391 return;
392 }
393
394 spin_lock(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700395 hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 spin_unlock(&xfrm_policy_gc_lock);
397
398 schedule_work(&xfrm_policy_gc_work);
399}
400
David S. Miller2518c7c2006-08-24 04:45:07 -0700401struct xfrm_policy_hash {
402 struct hlist_head *table;
403 unsigned int hmask;
404};
405
406static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
407static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
408static struct hlist_head *xfrm_policy_byidx __read_mostly;
409static unsigned int xfrm_idx_hmask __read_mostly;
410static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
411
412static inline unsigned int __idx_hash(u32 index, unsigned int hmask)
413{
414 return (index ^ (index >> 8)) & hmask;
415}
416
417static inline unsigned int idx_hash(u32 index)
418{
419 return __idx_hash(index, xfrm_idx_hmask);
420}
421
422static inline unsigned int __sel_hash(struct xfrm_selector *sel, unsigned short family, unsigned int hmask)
423{
424 xfrm_address_t *daddr = &sel->daddr;
425 xfrm_address_t *saddr = &sel->saddr;
426 unsigned int h = 0;
427
428 switch (family) {
429 case AF_INET:
430 if (sel->prefixlen_d != 32 ||
431 sel->prefixlen_s != 32)
432 return hmask + 1;
433
434 h = ntohl(daddr->a4 ^ saddr->a4);
435 break;
436
437 case AF_INET6:
438 if (sel->prefixlen_d != 128 ||
439 sel->prefixlen_s != 128)
440 return hmask + 1;
441
442 h = ntohl(daddr->a6[2] ^ daddr->a6[3] ^
443 saddr->a6[2] ^ saddr->a6[3]);
444 break;
445 };
446 h ^= (h >> 16);
447 return h & hmask;
448}
449
450static inline unsigned int __addr_hash(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, unsigned int hmask)
451{
452 unsigned int h = 0;
453
454 switch (family) {
455 case AF_INET:
456 h = ntohl(daddr->a4 ^ saddr->a4);
457 break;
458
459 case AF_INET6:
460 h = ntohl(daddr->a6[2] ^ daddr->a6[3] ^
461 saddr->a6[2] ^ saddr->a6[3]);
462 break;
463 };
464 h ^= (h >> 16);
465 return h & hmask;
466}
467
468static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
469{
470 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
471 unsigned int hash = __sel_hash(sel, family, hmask);
472
473 return (hash == hmask + 1 ?
474 &xfrm_policy_inexact[dir] :
475 xfrm_policy_bydst[dir].table + hash);
476}
477
478static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
479{
480 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
481 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
482
483 return xfrm_policy_bydst[dir].table + hash;
484}
485
486static struct hlist_head *xfrm_policy_hash_alloc(unsigned int sz)
487{
488 struct hlist_head *n;
489
490 if (sz <= PAGE_SIZE)
491 n = kmalloc(sz, GFP_KERNEL);
492 else if (hashdist)
493 n = __vmalloc(sz, GFP_KERNEL, PAGE_KERNEL);
494 else
495 n = (struct hlist_head *)
496 __get_free_pages(GFP_KERNEL, get_order(sz));
497
498 if (n)
499 memset(n, 0, sz);
500
501 return n;
502}
503
504static void xfrm_policy_hash_free(struct hlist_head *n, unsigned int sz)
505{
506 if (sz <= PAGE_SIZE)
507 kfree(n);
508 else if (hashdist)
509 vfree(n);
510 else
511 free_pages((unsigned long)n, get_order(sz));
512}
513
514static void xfrm_dst_hash_transfer(struct hlist_head *list,
515 struct hlist_head *ndsttable,
516 unsigned int nhashmask)
517{
518 struct hlist_node *entry, *tmp;
519 struct xfrm_policy *pol;
520
521 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
522 unsigned int h;
523
524 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
525 pol->family, nhashmask);
526 hlist_add_head(&pol->bydst, ndsttable+h);
527 }
528}
529
530static void xfrm_idx_hash_transfer(struct hlist_head *list,
531 struct hlist_head *nidxtable,
532 unsigned int nhashmask)
533{
534 struct hlist_node *entry, *tmp;
535 struct xfrm_policy *pol;
536
537 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
538 unsigned int h;
539
540 h = __idx_hash(pol->index, nhashmask);
541 hlist_add_head(&pol->byidx, nidxtable+h);
542 }
543}
544
545static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
546{
547 return ((old_hmask + 1) << 1) - 1;
548}
549
550static void xfrm_bydst_resize(int dir)
551{
552 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
553 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
554 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
555 struct hlist_head *odst = xfrm_policy_bydst[dir].table;
556 struct hlist_head *ndst = xfrm_policy_hash_alloc(nsize);
557 int i;
558
559 if (!ndst)
560 return;
561
562 write_lock_bh(&xfrm_policy_lock);
563
564 for (i = hmask; i >= 0; i--)
565 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
566
567 xfrm_policy_bydst[dir].table = ndst;
568 xfrm_policy_bydst[dir].hmask = nhashmask;
569
570 write_unlock_bh(&xfrm_policy_lock);
571
572 xfrm_policy_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
573}
574
575static void xfrm_byidx_resize(int total)
576{
577 unsigned int hmask = xfrm_idx_hmask;
578 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
579 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
580 struct hlist_head *oidx = xfrm_policy_byidx;
581 struct hlist_head *nidx = xfrm_policy_hash_alloc(nsize);
582 int i;
583
584 if (!nidx)
585 return;
586
587 write_lock_bh(&xfrm_policy_lock);
588
589 for (i = hmask; i >= 0; i--)
590 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
591
592 xfrm_policy_byidx = nidx;
593 xfrm_idx_hmask = nhashmask;
594
595 write_unlock_bh(&xfrm_policy_lock);
596
597 xfrm_policy_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
598}
599
600static inline int xfrm_bydst_should_resize(int dir, int *total)
601{
602 unsigned int cnt = xfrm_policy_count[dir];
603 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
604
605 if (total)
606 *total += cnt;
607
608 if ((hmask + 1) < xfrm_policy_hashmax &&
609 cnt > hmask)
610 return 1;
611
612 return 0;
613}
614
615static inline int xfrm_byidx_should_resize(int total)
616{
617 unsigned int hmask = xfrm_idx_hmask;
618
619 if ((hmask + 1) < xfrm_policy_hashmax &&
620 total > hmask)
621 return 1;
622
623 return 0;
624}
625
626static DEFINE_MUTEX(hash_resize_mutex);
627
628static void xfrm_hash_resize(void *__unused)
629{
630 int dir, total;
631
632 mutex_lock(&hash_resize_mutex);
633
634 total = 0;
635 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
636 if (xfrm_bydst_should_resize(dir, &total))
637 xfrm_bydst_resize(dir);
638 }
639 if (xfrm_byidx_should_resize(total))
640 xfrm_byidx_resize(total);
641
642 mutex_unlock(&hash_resize_mutex);
643}
644
645static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize, NULL);
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647/* Generate new index... KAME seems to generate them ordered by cost
648 * of an absolute inpredictability of ordering of rules. This will not pass. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700649static u32 xfrm_gen_index(u8 type, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 static u32 idx_generator;
652
653 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700654 struct hlist_node *entry;
655 struct hlist_head *list;
656 struct xfrm_policy *p;
657 u32 idx;
658 int found;
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 idx = (idx_generator | dir);
661 idx_generator += 8;
662 if (idx == 0)
663 idx = 8;
David S. Miller2518c7c2006-08-24 04:45:07 -0700664 list = xfrm_policy_byidx + idx_hash(idx);
665 found = 0;
666 hlist_for_each_entry(p, entry, list, byidx) {
667 if (p->index == idx) {
668 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700672 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return idx;
674 }
675}
676
David S. Miller2518c7c2006-08-24 04:45:07 -0700677static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
678{
679 u32 *p1 = (u32 *) s1;
680 u32 *p2 = (u32 *) s2;
681 int len = sizeof(struct xfrm_selector) / sizeof(u32);
682 int i;
683
684 for (i = 0; i < len; i++) {
685 if (p1[i] != p2[i])
686 return 1;
687 }
688
689 return 0;
690}
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
693{
David S. Miller2518c7c2006-08-24 04:45:07 -0700694 struct xfrm_policy *pol;
695 struct xfrm_policy *delpol;
696 struct hlist_head *chain;
697 struct hlist_node *entry, *newpos, *last;
David S. Miller9b78a822005-12-22 07:39:48 -0800698 struct dst_entry *gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700701 chain = policy_hash_bysel(&policy->selector, policy->family, dir);
702 delpol = NULL;
703 newpos = NULL;
704 last = NULL;
705 hlist_for_each_entry(pol, entry, chain, bydst) {
706 if (!delpol &&
707 pol->type == policy->type &&
708 !selector_cmp(&pol->selector, &policy->selector) &&
Trent Jaegerdf718372005-12-13 23:12:27 -0800709 xfrm_sec_ctx_match(pol->security, policy->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (excl) {
711 write_unlock_bh(&xfrm_policy_lock);
712 return -EEXIST;
713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 delpol = pol;
715 if (policy->priority > pol->priority)
716 continue;
717 } else if (policy->priority >= pol->priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700718 last = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 continue;
720 }
721 if (!newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700722 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (delpol)
724 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700725 last = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700727 if (!newpos)
728 newpos = last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700730 hlist_add_after(newpos, &policy->bydst);
731 else
732 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 xfrm_pol_hold(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700734 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700736 if (delpol) {
737 hlist_del(&delpol->bydst);
738 hlist_del(&delpol->byidx);
739 xfrm_policy_count[dir]--;
740 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700741 policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700742 hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 policy->curlft.add_time = (unsigned long)xtime.tv_sec;
744 policy->curlft.use_time = 0;
745 if (!mod_timer(&policy->timer, jiffies + HZ))
746 xfrm_pol_hold(policy);
747 write_unlock_bh(&xfrm_policy_lock);
748
David S. Miller9b78a822005-12-22 07:39:48 -0800749 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 xfrm_policy_kill(delpol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700751 else if (xfrm_bydst_should_resize(dir, NULL))
752 schedule_work(&xfrm_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800753
754 read_lock_bh(&xfrm_policy_lock);
755 gc_list = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700756 entry = &policy->bydst;
757 hlist_for_each_entry_continue(policy, entry, bydst) {
David S. Miller9b78a822005-12-22 07:39:48 -0800758 struct dst_entry *dst;
759
760 write_lock(&policy->lock);
761 dst = policy->bundles;
762 if (dst) {
763 struct dst_entry *tail = dst;
764 while (tail->next)
765 tail = tail->next;
766 tail->next = gc_list;
767 gc_list = dst;
768
769 policy->bundles = NULL;
770 }
771 write_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
David S. Miller9b78a822005-12-22 07:39:48 -0800773 read_unlock_bh(&xfrm_policy_lock);
774
775 while (gc_list) {
776 struct dst_entry *dst = gc_list;
777
778 gc_list = dst->next;
779 dst_free(dst);
780 }
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return 0;
783}
784EXPORT_SYMBOL(xfrm_policy_insert);
785
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700786struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
787 struct xfrm_selector *sel,
Trent Jaegerdf718372005-12-13 23:12:27 -0800788 struct xfrm_sec_ctx *ctx, int delete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
David S. Miller2518c7c2006-08-24 04:45:07 -0700790 struct xfrm_policy *pol, *ret;
791 struct hlist_head *chain;
792 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700795 chain = policy_hash_bysel(sel, sel->family, dir);
796 ret = NULL;
797 hlist_for_each_entry(pol, entry, chain, bydst) {
798 if (pol->type == type &&
799 !selector_cmp(sel, &pol->selector) &&
800 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700802 if (delete) {
803 hlist_del(&pol->bydst);
804 hlist_del(&pol->byidx);
805 xfrm_policy_count[dir]--;
806 }
807 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 break;
809 }
810 }
811 write_unlock_bh(&xfrm_policy_lock);
812
David S. Miller2518c7c2006-08-24 04:45:07 -0700813 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700815 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700817 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
Trent Jaegerdf718372005-12-13 23:12:27 -0800819EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700821struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
David S. Miller2518c7c2006-08-24 04:45:07 -0700823 struct xfrm_policy *pol, *ret;
824 struct hlist_head *chain;
825 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700828 chain = xfrm_policy_byidx + idx_hash(id);
829 ret = NULL;
830 hlist_for_each_entry(pol, entry, chain, byidx) {
831 if (pol->type == type && pol->index == id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700833 if (delete) {
834 hlist_del(&pol->bydst);
835 hlist_del(&pol->byidx);
836 xfrm_policy_count[dir]--;
837 }
838 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 break;
840 }
841 }
842 write_unlock_bh(&xfrm_policy_lock);
843
David S. Miller2518c7c2006-08-24 04:45:07 -0700844 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700846 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700848 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850EXPORT_SYMBOL(xfrm_policy_byid);
851
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700852void xfrm_policy_flush(u8 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 int dir;
855
856 write_lock_bh(&xfrm_policy_lock);
857 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700858 struct xfrm_policy *pol;
859 struct hlist_node *entry;
860 int i;
861
862 again1:
863 hlist_for_each_entry(pol, entry,
864 &xfrm_policy_inexact[dir], bydst) {
865 if (pol->type != type)
866 continue;
867 hlist_del(&pol->bydst);
868 hlist_del(&pol->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 write_unlock_bh(&xfrm_policy_lock);
870
David S. Miller2518c7c2006-08-24 04:45:07 -0700871 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700874 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700876
877 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
878 again2:
879 hlist_for_each_entry(pol, entry,
880 xfrm_policy_bydst[dir].table + i,
881 bydst) {
882 if (pol->type != type)
883 continue;
884 hlist_del(&pol->bydst);
885 hlist_del(&pol->byidx);
886 write_unlock_bh(&xfrm_policy_lock);
887
888 xfrm_policy_kill(pol);
889
890 write_lock_bh(&xfrm_policy_lock);
891 goto again2;
892 }
893 }
894
895 xfrm_policy_count[dir] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897 atomic_inc(&flow_cache_genid);
898 write_unlock_bh(&xfrm_policy_lock);
899}
900EXPORT_SYMBOL(xfrm_policy_flush);
901
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700902int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 void *data)
904{
David S. Miller2518c7c2006-08-24 04:45:07 -0700905 struct xfrm_policy *pol;
906 struct hlist_node *entry;
907 int dir, count, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700910 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700912 struct hlist_head *table = xfrm_policy_bydst[dir].table;
913 int i;
914
915 hlist_for_each_entry(pol, entry,
916 &xfrm_policy_inexact[dir], bydst) {
917 if (pol->type == type)
918 count++;
919 }
920 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
921 hlist_for_each_entry(pol, entry, table + i, bydst) {
922 if (pol->type == type)
923 count++;
924 }
925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927
928 if (count == 0) {
929 error = -ENOENT;
930 goto out;
931 }
932
933 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700934 struct hlist_head *table = xfrm_policy_bydst[dir].table;
935 int i;
936
937 hlist_for_each_entry(pol, entry,
938 &xfrm_policy_inexact[dir], bydst) {
939 if (pol->type != type)
940 continue;
941 error = func(pol, dir % XFRM_POLICY_MAX, --count, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (error)
943 goto out;
944 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700945 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
946 hlist_for_each_entry(pol, entry, table + i, bydst) {
947 if (pol->type != type)
948 continue;
949 error = func(pol, dir % XFRM_POLICY_MAX, --count, data);
950 if (error)
951 goto out;
952 }
953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700955 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956out:
957 read_unlock_bh(&xfrm_policy_lock);
958 return error;
959}
960EXPORT_SYMBOL(xfrm_policy_walk);
961
962/* Find policy to apply to this flow. */
963
David S. Miller2518c7c2006-08-24 04:45:07 -0700964static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
965 u8 type, u16 family, int dir)
966{
967 struct xfrm_selector *sel = &pol->selector;
968 int match;
969
970 if (pol->family != family ||
971 pol->type != type)
972 return 0;
973
974 match = xfrm_selector_match(sel, fl, family);
975 if (match) {
976 if (!security_xfrm_policy_lookup(pol, fl->secid, dir))
977 return 1;
978 }
979
980 return 0;
981}
982
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700983static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
984 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
David S. Miller2518c7c2006-08-24 04:45:07 -0700986 struct xfrm_policy *pol, *ret;
987 xfrm_address_t *daddr, *saddr;
988 struct hlist_node *entry;
989 struct hlist_head *chain;
990
991 daddr = xfrm_flowi_daddr(fl, family);
992 saddr = xfrm_flowi_saddr(fl, family);
993 if (unlikely(!daddr || !saddr))
994 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700997 chain = policy_hash_direct(daddr, saddr, family, dir);
998 ret = NULL;
999 hlist_for_each_entry(pol, entry, chain, bydst) {
1000 if (xfrm_policy_match(pol, fl, type, family, dir)) {
1001 xfrm_pol_hold(pol);
1002 ret = pol;
1003 break;
1004 }
1005 }
1006 if (!ret) {
1007 chain = &xfrm_policy_inexact[dir];
1008 hlist_for_each_entry(pol, entry, chain, bydst) {
1009 if (xfrm_policy_match(pol, fl, type, family, dir)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001010 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001011 ret = pol;
Trent Jaegerdf718372005-12-13 23:12:27 -08001012 break;
1013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015 }
1016 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001017
David S. Miller2518c7c2006-08-24 04:45:07 -07001018 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001019}
1020
1021static void xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
1022 void **objp, atomic_t **obj_refp)
1023{
1024 struct xfrm_policy *pol;
1025
1026#ifdef CONFIG_XFRM_SUB_POLICY
1027 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
1028 if (pol)
1029 goto end;
1030#endif
1031 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1032
1033#ifdef CONFIG_XFRM_SUB_POLICY
David S. Miller2518c7c2006-08-24 04:45:07 -07001034end:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001035#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if ((*objp = (void *) pol) != NULL)
1037 *obj_refp = &pol->refcnt;
1038}
1039
Trent Jaegerdf718372005-12-13 23:12:27 -08001040static inline int policy_to_flow_dir(int dir)
1041{
1042 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1043 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1044 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1045 return dir;
1046 switch (dir) {
1047 default:
1048 case XFRM_POLICY_IN:
1049 return FLOW_DIR_IN;
1050 case XFRM_POLICY_OUT:
1051 return FLOW_DIR_OUT;
1052 case XFRM_POLICY_FWD:
1053 return FLOW_DIR_FWD;
1054 };
1055}
1056
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001057static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
1059 struct xfrm_policy *pol;
1060
1061 read_lock_bh(&xfrm_policy_lock);
1062 if ((pol = sk->sk_policy[dir]) != NULL) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001063 int match = xfrm_selector_match(&pol->selector, fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 sk->sk_family);
Trent Jaegerdf718372005-12-13 23:12:27 -08001065 int err = 0;
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (match)
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001068 err = security_xfrm_policy_lookup(pol, fl->secid, policy_to_flow_dir(dir));
Trent Jaegerdf718372005-12-13 23:12:27 -08001069
1070 if (match && !err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 xfrm_pol_hold(pol);
1072 else
1073 pol = NULL;
1074 }
1075 read_unlock_bh(&xfrm_policy_lock);
1076 return pol;
1077}
1078
1079static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1080{
David S. Miller2518c7c2006-08-24 04:45:07 -07001081 struct hlist_head *chain = policy_hash_bysel(&pol->selector,
1082 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001083
David S. Miller2518c7c2006-08-24 04:45:07 -07001084 hlist_add_head(&pol->bydst, chain);
1085 hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
1086 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001088
1089 if (xfrm_bydst_should_resize(dir, NULL))
1090 schedule_work(&xfrm_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
1093static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1094 int dir)
1095{
David S. Miller2518c7c2006-08-24 04:45:07 -07001096 if (hlist_unhashed(&pol->bydst))
1097 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
David S. Miller2518c7c2006-08-24 04:45:07 -07001099 hlist_del(&pol->bydst);
1100 hlist_del(&pol->byidx);
1101 xfrm_policy_count[dir]--;
1102
1103 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
Herbert Xu4666faa2005-06-18 22:43:22 -07001106int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
1108 write_lock_bh(&xfrm_policy_lock);
1109 pol = __xfrm_policy_unlink(pol, dir);
1110 write_unlock_bh(&xfrm_policy_lock);
1111 if (pol) {
1112 if (dir < XFRM_POLICY_MAX)
1113 atomic_inc(&flow_cache_genid);
1114 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001117 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
David S. Millera70fcb02006-03-20 19:18:52 -08001119EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1122{
1123 struct xfrm_policy *old_pol;
1124
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001125#ifdef CONFIG_XFRM_SUB_POLICY
1126 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1127 return -EINVAL;
1128#endif
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 write_lock_bh(&xfrm_policy_lock);
1131 old_pol = sk->sk_policy[dir];
1132 sk->sk_policy[dir] = pol;
1133 if (pol) {
1134 pol->curlft.add_time = (unsigned long)xtime.tv_sec;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001135 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1137 }
1138 if (old_pol)
1139 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1140 write_unlock_bh(&xfrm_policy_lock);
1141
1142 if (old_pol) {
1143 xfrm_policy_kill(old_pol);
1144 }
1145 return 0;
1146}
1147
1148static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1149{
1150 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
1151
1152 if (newp) {
1153 newp->selector = old->selector;
Trent Jaegerdf718372005-12-13 23:12:27 -08001154 if (security_xfrm_policy_clone(old, newp)) {
1155 kfree(newp);
1156 return NULL; /* ENOMEM */
1157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 newp->lft = old->lft;
1159 newp->curlft = old->curlft;
1160 newp->action = old->action;
1161 newp->flags = old->flags;
1162 newp->xfrm_nr = old->xfrm_nr;
1163 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001164 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 memcpy(newp->xfrm_vec, old->xfrm_vec,
1166 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1167 write_lock_bh(&xfrm_policy_lock);
1168 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1169 write_unlock_bh(&xfrm_policy_lock);
1170 xfrm_pol_put(newp);
1171 }
1172 return newp;
1173}
1174
1175int __xfrm_sk_clone_policy(struct sock *sk)
1176{
1177 struct xfrm_policy *p0 = sk->sk_policy[0],
1178 *p1 = sk->sk_policy[1];
1179
1180 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1181 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1182 return -ENOMEM;
1183 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1184 return -ENOMEM;
1185 return 0;
1186}
1187
1188/* Resolve list of templates for the flow, given policy. */
1189
1190static int
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001191xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1192 struct xfrm_state **xfrm,
1193 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 int nx;
1196 int i, error;
1197 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1198 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
1199
1200 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1201 struct xfrm_state *x;
1202 xfrm_address_t *remote = daddr;
1203 xfrm_address_t *local = saddr;
1204 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1205
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001206 if (tmpl->mode == XFRM_MODE_TUNNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 remote = &tmpl->id.daddr;
1208 local = &tmpl->saddr;
1209 }
1210
1211 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1212
1213 if (x && x->km.state == XFRM_STATE_VALID) {
1214 xfrm[nx++] = x;
1215 daddr = remote;
1216 saddr = local;
1217 continue;
1218 }
1219 if (x) {
1220 error = (x->km.state == XFRM_STATE_ERROR ?
1221 -EINVAL : -EAGAIN);
1222 xfrm_state_put(x);
1223 }
1224
1225 if (!tmpl->optional)
1226 goto fail;
1227 }
1228 return nx;
1229
1230fail:
1231 for (nx--; nx>=0; nx--)
1232 xfrm_state_put(xfrm[nx]);
1233 return error;
1234}
1235
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001236static int
1237xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1238 struct xfrm_state **xfrm,
1239 unsigned short family)
1240{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001241 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1242 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001243 int cnx = 0;
1244 int error;
1245 int ret;
1246 int i;
1247
1248 for (i = 0; i < npols; i++) {
1249 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1250 error = -ENOBUFS;
1251 goto fail;
1252 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001253
1254 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001255 if (ret < 0) {
1256 error = ret;
1257 goto fail;
1258 } else
1259 cnx += ret;
1260 }
1261
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001262 /* found states are sorted for outbound processing */
1263 if (npols > 1)
1264 xfrm_state_sort(xfrm, tpp, cnx, family);
1265
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001266 return cnx;
1267
1268 fail:
1269 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001270 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001271 return error;
1272
1273}
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275/* Check that the bundle accepts the flow and its components are
1276 * still valid.
1277 */
1278
1279static struct dst_entry *
1280xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1281{
1282 struct dst_entry *x;
1283 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1284 if (unlikely(afinfo == NULL))
1285 return ERR_PTR(-EINVAL);
1286 x = afinfo->find_bundle(fl, policy);
1287 xfrm_policy_put_afinfo(afinfo);
1288 return x;
1289}
1290
1291/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1292 * all the metrics... Shortly, bundle a bundle.
1293 */
1294
1295static int
1296xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
1297 struct flowi *fl, struct dst_entry **dst_p,
1298 unsigned short family)
1299{
1300 int err;
1301 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1302 if (unlikely(afinfo == NULL))
1303 return -EINVAL;
1304 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
1305 xfrm_policy_put_afinfo(afinfo);
1306 return err;
1307}
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310static int stale_bundle(struct dst_entry *dst);
1311
1312/* Main function: finds/creates a bundle for given flow.
1313 *
1314 * At the moment we eat a raw IP route. Mostly to speed up lookups
1315 * on interfaces with disabled IPsec.
1316 */
1317int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1318 struct sock *sk, int flags)
1319{
1320 struct xfrm_policy *policy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001321 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1322 int npols;
1323 int pol_dead;
1324 int xfrm_nr;
1325 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1327 struct dst_entry *dst, *dst_orig = *dst_p;
1328 int nx = 0;
1329 int err;
1330 u32 genid;
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001331 u16 family;
Trent Jaegerdf718372005-12-13 23:12:27 -08001332 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334restart:
1335 genid = atomic_read(&flow_cache_genid);
1336 policy = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001337 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1338 pols[pi] = NULL;
1339 npols = 0;
1340 pol_dead = 0;
1341 xfrm_nr = 0;
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (sk && sk->sk_policy[1])
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001344 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 if (!policy) {
1347 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07001348 if ((dst_orig->flags & DST_NOXFRM) ||
1349 !xfrm_policy_count[XFRM_POLICY_OUT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return 0;
1351
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001352 policy = flow_cache_lookup(fl, dst_orig->ops->family,
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001353 dir, xfrm_policy_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355
1356 if (!policy)
1357 return 0;
1358
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001359 family = dst_orig->ops->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 policy->curlft.use_time = (unsigned long)xtime.tv_sec;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001361 pols[0] = policy;
1362 npols ++;
1363 xfrm_nr += pols[0]->xfrm_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 switch (policy->action) {
1366 case XFRM_POLICY_BLOCK:
1367 /* Prohibit the flow */
Patrick McHardye1044112005-09-08 15:11:55 -07001368 err = -EPERM;
1369 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 case XFRM_POLICY_ALLOW:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001372#ifndef CONFIG_XFRM_SUB_POLICY
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (policy->xfrm_nr == 0) {
1374 /* Flow passes not transformed. */
1375 xfrm_pol_put(policy);
1376 return 0;
1377 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001378#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 /* Try to find matching bundle.
1381 *
1382 * LATER: help from flow cache. It is optional, this
1383 * is required only for output policy.
1384 */
1385 dst = xfrm_find_bundle(fl, policy, family);
1386 if (IS_ERR(dst)) {
Patrick McHardye1044112005-09-08 15:11:55 -07001387 err = PTR_ERR(dst);
1388 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390
1391 if (dst)
1392 break;
1393
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001394#ifdef CONFIG_XFRM_SUB_POLICY
1395 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1396 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1397 fl, family,
1398 XFRM_POLICY_OUT);
1399 if (pols[1]) {
1400 if (pols[1]->action == XFRM_POLICY_BLOCK) {
1401 err = -EPERM;
1402 goto error;
1403 }
1404 npols ++;
1405 xfrm_nr += pols[1]->xfrm_nr;
1406 }
1407 }
1408
1409 /*
1410 * Because neither flowi nor bundle information knows about
1411 * transformation template size. On more than one policy usage
1412 * we can realize whether all of them is bypass or not after
1413 * they are searched. See above not-transformed bypass
1414 * is surrounded by non-sub policy configuration, too.
1415 */
1416 if (xfrm_nr == 0) {
1417 /* Flow passes not transformed. */
1418 xfrm_pols_put(pols, npols);
1419 return 0;
1420 }
1421
1422#endif
1423 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 if (unlikely(nx<0)) {
1426 err = nx;
1427 if (err == -EAGAIN && flags) {
1428 DECLARE_WAITQUEUE(wait, current);
1429
1430 add_wait_queue(&km_waitq, &wait);
1431 set_current_state(TASK_INTERRUPTIBLE);
1432 schedule();
1433 set_current_state(TASK_RUNNING);
1434 remove_wait_queue(&km_waitq, &wait);
1435
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001436 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 if (nx == -EAGAIN && signal_pending(current)) {
1439 err = -ERESTART;
1440 goto error;
1441 }
1442 if (nx == -EAGAIN ||
1443 genid != atomic_read(&flow_cache_genid)) {
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001444 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 goto restart;
1446 }
1447 err = nx;
1448 }
1449 if (err < 0)
1450 goto error;
1451 }
1452 if (nx == 0) {
1453 /* Flow passes not transformed. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001454 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return 0;
1456 }
1457
1458 dst = dst_orig;
1459 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
1460
1461 if (unlikely(err)) {
1462 int i;
1463 for (i=0; i<nx; i++)
1464 xfrm_state_put(xfrm[i]);
1465 goto error;
1466 }
1467
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001468 for (pi = 0; pi < npols; pi++) {
1469 read_lock_bh(&pols[pi]->lock);
1470 pol_dead |= pols[pi]->dead;
1471 read_unlock_bh(&pols[pi]->lock);
1472 }
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 write_lock_bh(&policy->lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001475 if (unlikely(pol_dead || stale_bundle(dst))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 /* Wow! While we worked on resolving, this
1477 * policy has gone. Retry. It is not paranoia,
1478 * we just cannot enlist new bundle to dead object.
1479 * We can't enlist stable bundles either.
1480 */
1481 write_unlock_bh(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 if (dst)
1483 dst_free(dst);
Herbert Xu00de6512006-02-13 16:01:27 -08001484
1485 err = -EHOSTUNREACH;
1486 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488 dst->next = policy->bundles;
1489 policy->bundles = dst;
1490 dst_hold(dst);
1491 write_unlock_bh(&policy->lock);
1492 }
1493 *dst_p = dst;
1494 dst_release(dst_orig);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001495 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return 0;
1497
1498error:
1499 dst_release(dst_orig);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001500 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 *dst_p = NULL;
1502 return err;
1503}
1504EXPORT_SYMBOL(xfrm_lookup);
1505
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001506static inline int
1507xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1508{
1509 struct xfrm_state *x;
1510 int err;
1511
1512 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1513 return 0;
1514 x = skb->sp->xvec[idx];
1515 if (!x->type->reject)
1516 return 0;
1517 xfrm_state_hold(x);
1518 err = x->type->reject(x, skb, fl);
1519 xfrm_state_put(x);
1520 return err;
1521}
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523/* When skb is transformed back to its "native" form, we have to
1524 * check policy restrictions. At the moment we make this in maximally
1525 * stupid way. Shame on me. :-) Of course, connected sockets must
1526 * have policy cached at them.
1527 */
1528
1529static inline int
1530xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
1531 unsigned short family)
1532{
1533 if (xfrm_state_kern(x))
1534 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family);
1535 return x->id.proto == tmpl->id.proto &&
1536 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1537 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1538 x->props.mode == tmpl->mode &&
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07001539 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1540 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001541 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1542 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
1544
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001545/*
1546 * 0 or more than 0 is returned when validation is succeeded (either bypass
1547 * because of optional transport mode, or next index of the mathced secpath
1548 * state with the template.
1549 * -1 is returned when no matching template is found.
1550 * Otherwise "-2 - errored_index" is returned.
1551 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552static inline int
1553xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1554 unsigned short family)
1555{
1556 int idx = start;
1557
1558 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001559 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return start;
1561 } else
1562 start = -1;
1563 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001564 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001566 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1567 if (start == -1)
1568 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572 return start;
1573}
1574
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001575int
1576xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
1578 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001579 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 if (unlikely(afinfo == NULL))
1582 return -EAFNOSUPPORT;
1583
1584 afinfo->decode_session(skb, fl);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07001585 err = security_xfrm_decode_session(skb, &fl->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001587 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001589EXPORT_SYMBOL(xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001591static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
1593 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001594 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
1595 if (idxp)
1596 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
1600
1601 return 0;
1602}
1603
1604int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
1605 unsigned short family)
1606{
1607 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001608 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1609 int npols = 0;
1610 int xfrm_nr;
1611 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 struct flowi fl;
Trent Jaegerdf718372005-12-13 23:12:27 -08001613 u8 fl_dir = policy_to_flow_dir(dir);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001614 int xerr_idx = -1;
1615 int *xerr_idxp = &xerr_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001617 if (xfrm_decode_session(skb, &fl, family) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return 0;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08001619 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 /* First, check used SA against their selectors. */
1622 if (skb->sp) {
1623 int i;
1624
1625 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001626 struct xfrm_state *x = skb->sp->xvec[i];
1627 if (!xfrm_selector_match(&x->sel, &fl, family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
1630 }
1631
1632 pol = NULL;
1633 if (sk && sk->sk_policy[dir])
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001634 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 if (!pol)
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001637 pol = flow_cache_lookup(&fl, family, fl_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 xfrm_policy_lookup);
1639
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001640 if (!pol) {
1641 if (skb->sp && secpath_has_nontransport(skb->sp, 0, xerr_idxp)) {
1642 xfrm_secpath_reject(xerr_idx, skb, &fl);
1643 return 0;
1644 }
1645 return 1;
1646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 pol->curlft.use_time = (unsigned long)xtime.tv_sec;
1649
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001650 pols[0] = pol;
1651 npols ++;
1652#ifdef CONFIG_XFRM_SUB_POLICY
1653 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1654 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1655 &fl, family,
1656 XFRM_POLICY_IN);
1657 if (pols[1]) {
1658 pols[1]->curlft.use_time = (unsigned long)xtime.tv_sec;
1659 npols ++;
1660 }
1661 }
1662#endif
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (pol->action == XFRM_POLICY_ALLOW) {
1665 struct sec_path *sp;
1666 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001667 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001668 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001669 struct xfrm_tmpl **tpp = tp;
1670 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 int i, k;
1672
1673 if ((sp = skb->sp) == NULL)
1674 sp = &dummy;
1675
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001676 for (pi = 0; pi < npols; pi++) {
1677 if (pols[pi] != pol &&
1678 pols[pi]->action != XFRM_POLICY_ALLOW)
1679 goto reject;
1680 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
1681 goto reject_error;
1682 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1683 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1684 }
1685 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001686 if (npols > 1) {
1687 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1688 tpp = stp;
1689 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 /* For each tunnel xfrm, find the first matching tmpl.
1692 * For each tmpl before that, find corresponding xfrm.
1693 * Order is _important_. Later we will implement
1694 * some barriers, but at the moment barriers
1695 * are implied between each two transformations.
1696 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001697 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1698 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001699 if (k < 0) {
1700 if (k < -1 && xerr_idxp)
1701 *xerr_idxp = -(2+k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
1705
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001706 if (secpath_has_nontransport(sp, k, xerr_idxp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 goto reject;
1708
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001709 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return 1;
1711 }
1712
1713reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001714 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001715reject_error:
1716 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return 0;
1718}
1719EXPORT_SYMBOL(__xfrm_policy_check);
1720
1721int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1722{
1723 struct flowi fl;
1724
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001725 if (xfrm_decode_session(skb, &fl, family) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return 0;
1727
1728 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1729}
1730EXPORT_SYMBOL(__xfrm_route_forward);
1731
David S. Millerd49c73c2006-08-13 18:55:53 -07001732/* Optimize later using cookies and generation ids. */
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1735{
David S. Millerd49c73c2006-08-13 18:55:53 -07001736 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1737 * to "-1" to force all XFRM destinations to get validated by
1738 * dst_ops->check on every use. We do this because when a
1739 * normal route referenced by an XFRM dst is obsoleted we do
1740 * not go looking around for all parent referencing XFRM dsts
1741 * so that we can invalidate them. It is just too much work.
1742 * Instead we make the checks here on every use. For example:
1743 *
1744 * XFRM dst A --> IPv4 dst X
1745 *
1746 * X is the "xdst->route" of A (X is also the "dst->path" of A
1747 * in this example). If X is marked obsolete, "A" will not
1748 * notice. That's what we are validating here via the
1749 * stale_bundle() check.
1750 *
1751 * When a policy's bundle is pruned, we dst_free() the XFRM
1752 * dst which causes it's ->obsolete field to be set to a
1753 * positive non-zero integer. If an XFRM dst has been pruned
1754 * like this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08001755 */
David S. Millerd49c73c2006-08-13 18:55:53 -07001756 if (dst->obsolete < 0 && !stale_bundle(dst))
1757 return dst;
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 return NULL;
1760}
1761
1762static int stale_bundle(struct dst_entry *dst)
1763{
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07001764 return !xfrm_bundle_ok((struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766
Herbert Xuaabc9762005-05-03 16:27:10 -07001767void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
1770 dst->dev = &loopback_dev;
1771 dev_hold(&loopback_dev);
1772 dev_put(dev);
1773 }
1774}
Herbert Xuaabc9762005-05-03 16:27:10 -07001775EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777static void xfrm_link_failure(struct sk_buff *skb)
1778{
1779 /* Impossible. Such dst must be popped before reaches point of failure. */
1780 return;
1781}
1782
1783static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1784{
1785 if (dst) {
1786 if (dst->obsolete) {
1787 dst_release(dst);
1788 dst = NULL;
1789 }
1790 }
1791 return dst;
1792}
1793
David S. Miller2518c7c2006-08-24 04:45:07 -07001794static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
1795{
1796 struct dst_entry *dst, **dstp;
1797
1798 write_lock(&pol->lock);
1799 dstp = &pol->bundles;
1800 while ((dst=*dstp) != NULL) {
1801 if (func(dst)) {
1802 *dstp = dst->next;
1803 dst->next = *gc_list_p;
1804 *gc_list_p = dst;
1805 } else {
1806 dstp = &dst->next;
1807 }
1808 }
1809 write_unlock(&pol->lock);
1810}
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1813{
David S. Miller2518c7c2006-08-24 04:45:07 -07001814 struct dst_entry *gc_list = NULL;
1815 int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07001818 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
1819 struct xfrm_policy *pol;
1820 struct hlist_node *entry;
1821 struct hlist_head *table;
1822 int i;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001823
David S. Miller2518c7c2006-08-24 04:45:07 -07001824 hlist_for_each_entry(pol, entry,
1825 &xfrm_policy_inexact[dir], bydst)
1826 prune_one_bundle(pol, func, &gc_list);
1827
1828 table = xfrm_policy_bydst[dir].table;
1829 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
1830 hlist_for_each_entry(pol, entry, table + i, bydst)
1831 prune_one_bundle(pol, func, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
1833 }
1834 read_unlock_bh(&xfrm_policy_lock);
1835
1836 while (gc_list) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001837 struct dst_entry *dst = gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 gc_list = dst->next;
1839 dst_free(dst);
1840 }
1841}
1842
1843static int unused_bundle(struct dst_entry *dst)
1844{
1845 return !atomic_read(&dst->__refcnt);
1846}
1847
1848static void __xfrm_garbage_collect(void)
1849{
1850 xfrm_prune_bundles(unused_bundle);
1851}
1852
David S. Miller1c095392006-08-24 03:30:28 -07001853static int xfrm_flush_bundles(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
1855 xfrm_prune_bundles(stale_bundle);
1856 return 0;
1857}
1858
1859void xfrm_init_pmtu(struct dst_entry *dst)
1860{
1861 do {
1862 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1863 u32 pmtu, route_mtu_cached;
1864
1865 pmtu = dst_mtu(dst->child);
1866 xdst->child_mtu_cached = pmtu;
1867
1868 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
1869
1870 route_mtu_cached = dst_mtu(xdst->route);
1871 xdst->route_mtu_cached = route_mtu_cached;
1872
1873 if (pmtu > route_mtu_cached)
1874 pmtu = route_mtu_cached;
1875
1876 dst->metrics[RTAX_MTU-1] = pmtu;
1877 } while ((dst = dst->next));
1878}
1879
1880EXPORT_SYMBOL(xfrm_init_pmtu);
1881
1882/* Check that the bundle accepts the flow and its components are
1883 * still valid.
1884 */
1885
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07001886int xfrm_bundle_ok(struct xfrm_dst *first, struct flowi *fl, int family, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
1888 struct dst_entry *dst = &first->u.dst;
1889 struct xfrm_dst *last;
1890 u32 mtu;
1891
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07001892 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 (dst->dev && !netif_running(dst->dev)))
1894 return 0;
1895
1896 last = NULL;
1897
1898 do {
1899 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1900
1901 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
1902 return 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001903 if (fl && !security_xfrm_flow_state_match(fl, dst->xfrm))
1904 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (dst->xfrm->km.state != XFRM_STATE_VALID)
1906 return 0;
David S. Miller9d4a7062006-08-24 03:18:09 -07001907 if (xdst->genid != dst->xfrm->genid)
1908 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07001910 if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
1911 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
1912 return 0;
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 mtu = dst_mtu(dst->child);
1915 if (xdst->child_mtu_cached != mtu) {
1916 last = xdst;
1917 xdst->child_mtu_cached = mtu;
1918 }
1919
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07001920 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return 0;
1922 mtu = dst_mtu(xdst->route);
1923 if (xdst->route_mtu_cached != mtu) {
1924 last = xdst;
1925 xdst->route_mtu_cached = mtu;
1926 }
1927
1928 dst = dst->child;
1929 } while (dst->xfrm);
1930
1931 if (likely(!last))
1932 return 1;
1933
1934 mtu = last->child_mtu_cached;
1935 for (;;) {
1936 dst = &last->u.dst;
1937
1938 mtu = xfrm_state_mtu(dst->xfrm, mtu);
1939 if (mtu > last->route_mtu_cached)
1940 mtu = last->route_mtu_cached;
1941 dst->metrics[RTAX_MTU-1] = mtu;
1942
1943 if (last == first)
1944 break;
1945
1946 last = last->u.next;
1947 last->child_mtu_cached = mtu;
1948 }
1949
1950 return 1;
1951}
1952
1953EXPORT_SYMBOL(xfrm_bundle_ok);
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
1956{
1957 int err = 0;
1958 if (unlikely(afinfo == NULL))
1959 return -EINVAL;
1960 if (unlikely(afinfo->family >= NPROTO))
1961 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07001962 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
1964 err = -ENOBUFS;
1965 else {
1966 struct dst_ops *dst_ops = afinfo->dst_ops;
1967 if (likely(dst_ops->kmem_cachep == NULL))
1968 dst_ops->kmem_cachep = xfrm_dst_cache;
1969 if (likely(dst_ops->check == NULL))
1970 dst_ops->check = xfrm_dst_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (likely(dst_ops->negative_advice == NULL))
1972 dst_ops->negative_advice = xfrm_negative_advice;
1973 if (likely(dst_ops->link_failure == NULL))
1974 dst_ops->link_failure = xfrm_link_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (likely(afinfo->garbage_collect == NULL))
1976 afinfo->garbage_collect = __xfrm_garbage_collect;
1977 xfrm_policy_afinfo[afinfo->family] = afinfo;
1978 }
Ingo Molnare959d812006-04-28 15:32:29 -07001979 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return err;
1981}
1982EXPORT_SYMBOL(xfrm_policy_register_afinfo);
1983
1984int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
1985{
1986 int err = 0;
1987 if (unlikely(afinfo == NULL))
1988 return -EINVAL;
1989 if (unlikely(afinfo->family >= NPROTO))
1990 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07001991 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
1993 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
1994 err = -EINVAL;
1995 else {
1996 struct dst_ops *dst_ops = afinfo->dst_ops;
1997 xfrm_policy_afinfo[afinfo->family] = NULL;
1998 dst_ops->kmem_cachep = NULL;
1999 dst_ops->check = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 dst_ops->negative_advice = NULL;
2001 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 afinfo->garbage_collect = NULL;
2003 }
2004 }
Ingo Molnare959d812006-04-28 15:32:29 -07002005 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return err;
2007}
2008EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2009
2010static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
2011{
2012 struct xfrm_policy_afinfo *afinfo;
2013 if (unlikely(family >= NPROTO))
2014 return NULL;
2015 read_lock(&xfrm_policy_afinfo_lock);
2016 afinfo = xfrm_policy_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07002017 if (unlikely(!afinfo))
2018 read_unlock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return afinfo;
2020}
2021
2022static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2023{
Herbert Xu546be242006-05-27 23:03:58 -07002024 read_unlock(&xfrm_policy_afinfo_lock);
2025}
2026
2027static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
2028{
2029 struct xfrm_policy_afinfo *afinfo;
2030 if (unlikely(family >= NPROTO))
2031 return NULL;
2032 write_lock_bh(&xfrm_policy_afinfo_lock);
2033 afinfo = xfrm_policy_afinfo[family];
2034 if (unlikely(!afinfo))
2035 write_unlock_bh(&xfrm_policy_afinfo_lock);
2036 return afinfo;
2037}
2038
2039static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
2040{
2041 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
2044static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2045{
2046 switch (event) {
2047 case NETDEV_DOWN:
2048 xfrm_flush_bundles();
2049 }
2050 return NOTIFY_DONE;
2051}
2052
2053static struct notifier_block xfrm_dev_notifier = {
2054 xfrm_dev_event,
2055 NULL,
2056 0
2057};
2058
2059static void __init xfrm_policy_init(void)
2060{
David S. Miller2518c7c2006-08-24 04:45:07 -07002061 unsigned int hmask, sz;
2062 int dir;
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2065 sizeof(struct xfrm_dst),
2066 0, SLAB_HWCACHE_ALIGN,
2067 NULL, NULL);
2068 if (!xfrm_dst_cache)
2069 panic("XFRM: failed to allocate xfrm_dst_cache\n");
2070
David S. Miller2518c7c2006-08-24 04:45:07 -07002071 hmask = 8 - 1;
2072 sz = (hmask+1) * sizeof(struct hlist_head);
2073
2074 xfrm_policy_byidx = xfrm_policy_hash_alloc(sz);
2075 xfrm_idx_hmask = hmask;
2076 if (!xfrm_policy_byidx)
2077 panic("XFRM: failed to allocate byidx hash\n");
2078
2079 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2080 struct xfrm_policy_hash *htab;
2081
2082 INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
2083
2084 htab = &xfrm_policy_bydst[dir];
2085 htab->table = xfrm_policy_hash_alloc(sz);
2086 htab->hmask = hmask;
2087 if (!htab->table)
2088 panic("XFRM: failed to allocate bydst hash\n");
2089 }
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task, NULL);
2092 register_netdevice_notifier(&xfrm_dev_notifier);
2093}
2094
2095void __init xfrm_init(void)
2096{
2097 xfrm_state_init();
2098 xfrm_policy_init();
2099 xfrm_input_init();
2100}
2101