]> err.no Git - linux-2.6/blob - net/xfrm/xfrm_policy.c
087a5443b0514435dd9c7a2303adae43b691b7cc
[linux-2.6] / net / xfrm / xfrm_policy.c
1 /* 
2  * xfrm_policy.c
3  *
4  * Changes:
5  *      Mitsuru KANDA @USAGI
6  *      Kazunori MIYAZAWA @USAGI
7  *      Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8  *              IPv6 support
9  *      Kazunori MIYAZAWA @USAGI
10  *      YOSHIFUJI Hideaki
11  *              Split up af-specific portion
12  *      Derek Atkins <derek@ihtfp.com>          Add the post_input processor
13  *
14  */
15
16 #include <linux/slab.h>
17 #include <linux/kmod.h>
18 #include <linux/list.h>
19 #include <linux/spinlock.h>
20 #include <linux/workqueue.h>
21 #include <linux/notifier.h>
22 #include <linux/netdevice.h>
23 #include <linux/netfilter.h>
24 #include <linux/module.h>
25 #include <linux/bootmem.h>
26 #include <linux/vmalloc.h>
27 #include <linux/cache.h>
28 #include <net/xfrm.h>
29 #include <net/ip.h>
30
31 DEFINE_MUTEX(xfrm_cfg_mutex);
32 EXPORT_SYMBOL(xfrm_cfg_mutex);
33
34 static DEFINE_RWLOCK(xfrm_policy_lock);
35
36 unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
37 EXPORT_SYMBOL(xfrm_policy_count);
38
39 static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
40 static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
41
42 static kmem_cache_t *xfrm_dst_cache __read_mostly;
43
44 static struct work_struct xfrm_policy_gc_work;
45 static HLIST_HEAD(xfrm_policy_gc_list);
46 static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
47
48 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
49 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
50 static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family);
51 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo);
52
53 int xfrm_register_type(struct xfrm_type *type, unsigned short family)
54 {
55         struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
56         struct xfrm_type **typemap;
57         int err = 0;
58
59         if (unlikely(afinfo == NULL))
60                 return -EAFNOSUPPORT;
61         typemap = afinfo->type_map;
62
63         if (likely(typemap[type->proto] == NULL))
64                 typemap[type->proto] = type;
65         else
66                 err = -EEXIST;
67         xfrm_policy_unlock_afinfo(afinfo);
68         return err;
69 }
70 EXPORT_SYMBOL(xfrm_register_type);
71
72 int xfrm_unregister_type(struct xfrm_type *type, unsigned short family)
73 {
74         struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
75         struct xfrm_type **typemap;
76         int err = 0;
77
78         if (unlikely(afinfo == NULL))
79                 return -EAFNOSUPPORT;
80         typemap = afinfo->type_map;
81
82         if (unlikely(typemap[type->proto] != type))
83                 err = -ENOENT;
84         else
85                 typemap[type->proto] = NULL;
86         xfrm_policy_unlock_afinfo(afinfo);
87         return err;
88 }
89 EXPORT_SYMBOL(xfrm_unregister_type);
90
91 struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
92 {
93         struct xfrm_policy_afinfo *afinfo;
94         struct xfrm_type **typemap;
95         struct xfrm_type *type;
96         int modload_attempted = 0;
97
98 retry:
99         afinfo = xfrm_policy_get_afinfo(family);
100         if (unlikely(afinfo == NULL))
101                 return NULL;
102         typemap = afinfo->type_map;
103
104         type = typemap[proto];
105         if (unlikely(type && !try_module_get(type->owner)))
106                 type = NULL;
107         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 }
118
119 int 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 }
135 EXPORT_SYMBOL(xfrm_dst_lookup);
136
137 void xfrm_put_type(struct xfrm_type *type)
138 {
139         module_put(type->owner);
140 }
141
142 int 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 }
165 EXPORT_SYMBOL(xfrm_register_mode);
166
167 int 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 }
190 EXPORT_SYMBOL(xfrm_unregister_mode);
191
192 struct 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
201 retry:
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
220 void xfrm_put_mode(struct xfrm_mode *mode)
221 {
222         module_put(mode->owner);
223 }
224
225 static 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
233 static 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
246         dir = xfrm_policy_id2dir(xp->index);
247
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)
286                 km_policy_expired(xp, dir, 0, 0);
287         if (next != LONG_MAX &&
288             !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
289                 xfrm_pol_hold(xp);
290
291 out:
292         read_unlock(&xp->lock);
293         xfrm_pol_put(xp);
294         return;
295
296 expired:
297         read_unlock(&xp->lock);
298         if (!xfrm_policy_delete(xp, dir))
299                 km_policy_expired(xp, dir, 1, 0);
300         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
308 struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
309 {
310         struct xfrm_policy *policy;
311
312         policy = kzalloc(sizeof(struct xfrm_policy), gfp);
313
314         if (policy) {
315                 INIT_HLIST_NODE(&policy->bydst);
316                 INIT_HLIST_NODE(&policy->byidx);
317                 rwlock_init(&policy->lock);
318                 atomic_set(&policy->refcnt, 1);
319                 init_timer(&policy->timer);
320                 policy->timer.data = (unsigned long)policy;
321                 policy->timer.function = xfrm_policy_timer;
322         }
323         return policy;
324 }
325 EXPORT_SYMBOL(xfrm_policy_alloc);
326
327 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
328
329 void __xfrm_policy_destroy(struct xfrm_policy *policy)
330 {
331         BUG_ON(!policy->dead);
332
333         BUG_ON(policy->bundles);
334
335         if (del_timer(&policy->timer))
336                 BUG();
337
338         security_xfrm_policy_free(policy);
339         kfree(policy);
340 }
341 EXPORT_SYMBOL(__xfrm_policy_destroy);
342
343 static 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
361 static void xfrm_policy_gc_task(void *data)
362 {
363         struct xfrm_policy *policy;
364         struct hlist_node *entry, *tmp;
365         struct hlist_head gc_list;
366
367         spin_lock_bh(&xfrm_policy_gc_lock);
368         gc_list.first = xfrm_policy_gc_list.first;
369         INIT_HLIST_HEAD(&xfrm_policy_gc_list);
370         spin_unlock_bh(&xfrm_policy_gc_lock);
371
372         hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
373                 xfrm_policy_gc_kill(policy);
374 }
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
380 static 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);
395         hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
396         spin_unlock(&xfrm_policy_gc_lock);
397
398         schedule_work(&xfrm_policy_gc_work);
399 }
400
401 struct xfrm_policy_hash {
402         struct hlist_head       *table;
403         unsigned int            hmask;
404 };
405
406 static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
407 static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
408 static struct hlist_head *xfrm_policy_byidx __read_mostly;
409 static unsigned int xfrm_idx_hmask __read_mostly;
410 static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
411
412 static inline unsigned int __idx_hash(u32 index, unsigned int hmask)
413 {
414         return (index ^ (index >> 8)) & hmask;
415 }
416
417 static inline unsigned int idx_hash(u32 index)
418 {
419         return __idx_hash(index, xfrm_idx_hmask);
420 }
421
422 static 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
450 static 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
468 static 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
478 static 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
486 static 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
504 static 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
514 static 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
530 static 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
545 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
546 {
547         return ((old_hmask + 1) << 1) - 1;
548 }
549
550 static 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
575 static 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
600 static 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
615 static 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
626 static DEFINE_MUTEX(hash_resize_mutex);
627
628 static 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
645 static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize, NULL);
646
647 /* Generate new index... KAME seems to generate them ordered by cost
648  * of an absolute inpredictability of ordering of rules. This will not pass. */
649 static u32 xfrm_gen_index(u8 type, int dir)
650 {
651         static u32 idx_generator;
652
653         for (;;) {
654                 struct hlist_node *entry;
655                 struct hlist_head *list;
656                 struct xfrm_policy *p;
657                 u32 idx;
658                 int found;
659
660                 idx = (idx_generator | dir);
661                 idx_generator += 8;
662                 if (idx == 0)
663                         idx = 8;
664                 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;
669                                 break;
670                         }
671                 }
672                 if (!found)
673                         return idx;
674         }
675 }
676
677 static 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
692 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
693 {
694         struct xfrm_policy *pol;
695         struct xfrm_policy *delpol;
696         struct hlist_head *chain;
697         struct hlist_node *entry, *newpos, *last;
698         struct dst_entry *gc_list;
699
700         write_lock_bh(&xfrm_policy_lock);
701         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) &&
709                     xfrm_sec_ctx_match(pol->security, policy->security)) {
710                         if (excl) {
711                                 write_unlock_bh(&xfrm_policy_lock);
712                                 return -EEXIST;
713                         }
714                         delpol = pol;
715                         if (policy->priority > pol->priority)
716                                 continue;
717                 } else if (policy->priority >= pol->priority) {
718                         last = &pol->bydst;
719                         continue;
720                 }
721                 if (!newpos)
722                         newpos = &pol->bydst;
723                 if (delpol)
724                         break;
725                 last = &pol->bydst;
726         }
727         if (!newpos)
728                 newpos = last;
729         if (newpos)
730                 hlist_add_after(newpos, &policy->bydst);
731         else
732                 hlist_add_head(&policy->bydst, chain);
733         xfrm_pol_hold(policy);
734         xfrm_policy_count[dir]++;
735         atomic_inc(&flow_cache_genid);
736         if (delpol) {
737                 hlist_del(&delpol->bydst);
738                 hlist_del(&delpol->byidx);
739                 xfrm_policy_count[dir]--;
740         }
741         policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
742         hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
743         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
749         if (delpol)
750                 xfrm_policy_kill(delpol);
751         else if (xfrm_bydst_should_resize(dir, NULL))
752                 schedule_work(&xfrm_hash_work);
753
754         read_lock_bh(&xfrm_policy_lock);
755         gc_list = NULL;
756         entry = &policy->bydst;
757         hlist_for_each_entry_continue(policy, entry, bydst) {
758                 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);
772         }
773         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
782         return 0;
783 }
784 EXPORT_SYMBOL(xfrm_policy_insert);
785
786 struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
787                                           struct xfrm_selector *sel,
788                                           struct xfrm_sec_ctx *ctx, int delete)
789 {
790         struct xfrm_policy *pol, *ret;
791         struct hlist_head *chain;
792         struct hlist_node *entry;
793
794         write_lock_bh(&xfrm_policy_lock);
795         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)) {
801                         xfrm_pol_hold(pol);
802                         if (delete) {
803                                 hlist_del(&pol->bydst);
804                                 hlist_del(&pol->byidx);
805                                 xfrm_policy_count[dir]--;
806                         }
807                         ret = pol;
808                         break;
809                 }
810         }
811         write_unlock_bh(&xfrm_policy_lock);
812
813         if (ret && delete) {
814                 atomic_inc(&flow_cache_genid);
815                 xfrm_policy_kill(ret);
816         }
817         return ret;
818 }
819 EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
820
821 struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete)
822 {
823         struct xfrm_policy *pol, *ret;
824         struct hlist_head *chain;
825         struct hlist_node *entry;
826
827         write_lock_bh(&xfrm_policy_lock);
828         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) {
832                         xfrm_pol_hold(pol);
833                         if (delete) {
834                                 hlist_del(&pol->bydst);
835                                 hlist_del(&pol->byidx);
836                                 xfrm_policy_count[dir]--;
837                         }
838                         ret = pol;
839                         break;
840                 }
841         }
842         write_unlock_bh(&xfrm_policy_lock);
843
844         if (ret && delete) {
845                 atomic_inc(&flow_cache_genid);
846                 xfrm_policy_kill(ret);
847         }
848         return ret;
849 }
850 EXPORT_SYMBOL(xfrm_policy_byid);
851
852 void xfrm_policy_flush(u8 type)
853 {
854         int dir;
855
856         write_lock_bh(&xfrm_policy_lock);
857         for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
858                 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);
869                         write_unlock_bh(&xfrm_policy_lock);
870
871                         xfrm_policy_kill(pol);
872
873                         write_lock_bh(&xfrm_policy_lock);
874                         goto again1;
875                 }
876
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;
896         }
897         atomic_inc(&flow_cache_genid);
898         write_unlock_bh(&xfrm_policy_lock);
899 }
900 EXPORT_SYMBOL(xfrm_policy_flush);
901
902 int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
903                      void *data)
904 {
905         struct xfrm_policy *pol;
906         struct hlist_node *entry;
907         int dir, count, error;
908
909         read_lock_bh(&xfrm_policy_lock);
910         count = 0;
911         for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
912                 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                 }
926         }
927
928         if (count == 0) {
929                 error = -ENOENT;
930                 goto out;
931         }
932
933         for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
934                 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);
942                         if (error)
943                                 goto out;
944                 }
945                 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                 }
954         }
955         error = 0;
956 out:
957         read_unlock_bh(&xfrm_policy_lock);
958         return error;
959 }
960 EXPORT_SYMBOL(xfrm_policy_walk);
961
962 /* Find policy to apply to this flow. */
963
964 static 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
983 static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
984                                                      u16 family, u8 dir)
985 {
986         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;
995
996         read_lock_bh(&xfrm_policy_lock);
997         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)) {
1010                                 xfrm_pol_hold(pol);
1011                                 ret = pol;
1012                                 break;
1013                         }
1014                 }
1015         }
1016         read_unlock_bh(&xfrm_policy_lock);
1017
1018         return ret;
1019 }
1020
1021 static 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
1034 end:
1035 #endif
1036         if ((*objp = (void *) pol) != NULL)
1037                 *obj_refp = &pol->refcnt;
1038 }
1039
1040 static 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
1057 static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
1058 {
1059         struct xfrm_policy *pol;
1060
1061         read_lock_bh(&xfrm_policy_lock);
1062         if ((pol = sk->sk_policy[dir]) != NULL) {
1063                 int match = xfrm_selector_match(&pol->selector, fl,
1064                                                 sk->sk_family);
1065                 int err = 0;
1066
1067                 if (match)
1068                   err = security_xfrm_policy_lookup(pol, fl->secid, policy_to_flow_dir(dir));
1069
1070                 if (match && !err)
1071                         xfrm_pol_hold(pol);
1072                 else
1073                         pol = NULL;
1074         }
1075         read_unlock_bh(&xfrm_policy_lock);
1076         return pol;
1077 }
1078
1079 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1080 {
1081         struct hlist_head *chain = policy_hash_bysel(&pol->selector,
1082                                                      pol->family, dir);
1083
1084         hlist_add_head(&pol->bydst, chain);
1085         hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
1086         xfrm_policy_count[dir]++;
1087         xfrm_pol_hold(pol);
1088
1089         if (xfrm_bydst_should_resize(dir, NULL))
1090                 schedule_work(&xfrm_hash_work);
1091 }
1092
1093 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1094                                                 int dir)
1095 {
1096         if (hlist_unhashed(&pol->bydst))
1097                 return NULL;
1098
1099         hlist_del(&pol->bydst);
1100         hlist_del(&pol->byidx);
1101         xfrm_policy_count[dir]--;
1102
1103         return pol;
1104 }
1105
1106 int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
1107 {
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);
1115                 return 0;
1116         }
1117         return -ENOENT;
1118 }
1119 EXPORT_SYMBOL(xfrm_policy_delete);
1120
1121 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1122 {
1123         struct xfrm_policy *old_pol;
1124
1125 #ifdef CONFIG_XFRM_SUB_POLICY
1126         if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1127                 return -EINVAL;
1128 #endif
1129
1130         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;
1135                 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
1136                 __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
1148 static 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;
1154                 if (security_xfrm_policy_clone(old, newp)) {
1155                         kfree(newp);
1156                         return NULL;  /* ENOMEM */
1157                 }
1158                 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;
1164                 newp->type = old->type;
1165                 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
1175 int __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
1190 static int
1191 xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1192                       struct xfrm_state **xfrm,
1193                       unsigned short family)
1194 {
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
1206                 if (tmpl->mode == XFRM_MODE_TUNNEL) {
1207                         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
1230 fail:
1231         for (nx--; nx>=0; nx--)
1232                 xfrm_state_put(xfrm[nx]);
1233         return error;
1234 }
1235
1236 static int
1237 xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1238                   struct xfrm_state **xfrm,
1239                   unsigned short family)
1240 {
1241         struct xfrm_state *tp[XFRM_MAX_DEPTH];
1242         struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
1243         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                 }
1253
1254                 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
1255                 if (ret < 0) {
1256                         error = ret;
1257                         goto fail;
1258                 } else
1259                         cnx += ret;
1260         }
1261
1262         /* found states are sorted for outbound processing */
1263         if (npols > 1)
1264                 xfrm_state_sort(xfrm, tpp, cnx, family);
1265
1266         return cnx;
1267
1268  fail:
1269         for (cnx--; cnx>=0; cnx--)
1270                 xfrm_state_put(tpp[cnx]);
1271         return error;
1272
1273 }
1274
1275 /* Check that the bundle accepts the flow and its components are
1276  * still valid.
1277  */
1278
1279 static struct dst_entry *
1280 xfrm_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
1295 static int
1296 xfrm_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
1309
1310 static 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  */
1317 int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1318                 struct sock *sk, int flags)
1319 {
1320         struct xfrm_policy *policy;
1321         struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1322         int npols;
1323         int pol_dead;
1324         int xfrm_nr;
1325         int pi;
1326         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;
1331         u16 family;
1332         u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
1333
1334 restart:
1335         genid = atomic_read(&flow_cache_genid);
1336         policy = NULL;
1337         for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1338                 pols[pi] = NULL;
1339         npols = 0;
1340         pol_dead = 0;
1341         xfrm_nr = 0;
1342
1343         if (sk && sk->sk_policy[1])
1344                 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
1345
1346         if (!policy) {
1347                 /* To accelerate a bit...  */
1348                 if ((dst_orig->flags & DST_NOXFRM) ||
1349                     !xfrm_policy_count[XFRM_POLICY_OUT])
1350                         return 0;
1351
1352                 policy = flow_cache_lookup(fl, dst_orig->ops->family,
1353                                            dir, xfrm_policy_lookup);
1354         }
1355
1356         if (!policy)
1357                 return 0;
1358
1359         family = dst_orig->ops->family;
1360         policy->curlft.use_time = (unsigned long)xtime.tv_sec;
1361         pols[0] = policy;
1362         npols ++;
1363         xfrm_nr += pols[0]->xfrm_nr;
1364
1365         switch (policy->action) {
1366         case XFRM_POLICY_BLOCK:
1367                 /* Prohibit the flow */
1368                 err = -EPERM;
1369                 goto error;
1370
1371         case XFRM_POLICY_ALLOW:
1372 #ifndef CONFIG_XFRM_SUB_POLICY
1373                 if (policy->xfrm_nr == 0) {
1374                         /* Flow passes not transformed. */
1375                         xfrm_pol_put(policy);
1376                         return 0;
1377                 }
1378 #endif
1379
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)) {
1387                         err = PTR_ERR(dst);
1388                         goto error;
1389                 }
1390
1391                 if (dst)
1392                         break;
1393
1394 #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);
1424
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
1436                                 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
1437
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)) {
1444                                         xfrm_pols_put(pols, npols);
1445                                         goto restart;
1446                                 }
1447                                 err = nx;
1448                         }
1449                         if (err < 0)
1450                                 goto error;
1451                 }
1452                 if (nx == 0) {
1453                         /* Flow passes not transformed. */
1454                         xfrm_pols_put(pols, npols);
1455                         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
1468                 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
1474                 write_lock_bh(&policy->lock);
1475                 if (unlikely(pol_dead || stale_bundle(dst))) {
1476                         /* 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);
1482                         if (dst)
1483                                 dst_free(dst);
1484
1485                         err = -EHOSTUNREACH;
1486                         goto error;
1487                 }
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);
1495         xfrm_pols_put(pols, npols);
1496         return 0;
1497
1498 error:
1499         dst_release(dst_orig);
1500         xfrm_pols_put(pols, npols);
1501         *dst_p = NULL;
1502         return err;
1503 }
1504 EXPORT_SYMBOL(xfrm_lookup);
1505
1506 static inline int
1507 xfrm_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
1523 /* 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
1529 static inline int
1530 xfrm_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 &&
1539                 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1540                  !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
1541                 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1542                   xfrm_state_addr_cmp(tmpl, x, family));
1543 }
1544
1545 /*
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  */
1552 static inline int
1553 xfrm_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) {
1559                 if (tmpl->mode == XFRM_MODE_TRANSPORT)
1560                         return start;
1561         } else
1562                 start = -1;
1563         for (; idx < sp->len; idx++) {
1564                 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
1565                         return ++idx;
1566                 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1567                         if (start == -1)
1568                                 start = -2-idx;
1569                         break;
1570                 }
1571         }
1572         return start;
1573 }
1574
1575 int
1576 xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
1577 {
1578         struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1579         int err;
1580
1581         if (unlikely(afinfo == NULL))
1582                 return -EAFNOSUPPORT;
1583
1584         afinfo->decode_session(skb, fl);
1585         err = security_xfrm_decode_session(skb, &fl->secid);
1586         xfrm_policy_put_afinfo(afinfo);
1587         return err;
1588 }
1589 EXPORT_SYMBOL(xfrm_decode_session);
1590
1591 static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
1592 {
1593         for (; k < sp->len; k++) {
1594                 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
1595                         if (idxp)
1596                                 *idxp = k;
1597                         return 1;
1598                 }
1599         }
1600
1601         return 0;
1602 }
1603
1604 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, 
1605                         unsigned short family)
1606 {
1607         struct xfrm_policy *pol;
1608         struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1609         int npols = 0;
1610         int xfrm_nr;
1611         int pi;
1612         struct flowi fl;
1613         u8 fl_dir = policy_to_flow_dir(dir);
1614         int xerr_idx = -1;
1615         int *xerr_idxp = &xerr_idx;
1616
1617         if (xfrm_decode_session(skb, &fl, family) < 0)
1618                 return 0;
1619         nf_nat_decode_session(skb, &fl, family);
1620
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--) {
1626                         struct xfrm_state *x = skb->sp->xvec[i];
1627                         if (!xfrm_selector_match(&x->sel, &fl, family))
1628                                 return 0;
1629                 }
1630         }
1631
1632         pol = NULL;
1633         if (sk && sk->sk_policy[dir])
1634                 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
1635
1636         if (!pol)
1637                 pol = flow_cache_lookup(&fl, family, fl_dir,
1638                                         xfrm_policy_lookup);
1639
1640         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         }
1647
1648         pol->curlft.use_time = (unsigned long)xtime.tv_sec;
1649
1650         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
1664         if (pol->action == XFRM_POLICY_ALLOW) {
1665                 struct sec_path *sp;
1666                 static struct sec_path dummy;
1667                 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
1668                 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
1669                 struct xfrm_tmpl **tpp = tp;
1670                 int ti = 0;
1671                 int i, k;
1672
1673                 if ((sp = skb->sp) == NULL)
1674                         sp = &dummy;
1675
1676                 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;
1686                 if (npols > 1) {
1687                         xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1688                         tpp = stp;
1689                 }
1690
1691                 /* 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                  */
1697                 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1698                         k = xfrm_policy_ok(tpp[i], sp, k, family);
1699                         if (k < 0) {
1700                                 if (k < -1 && xerr_idxp)
1701                                         *xerr_idxp = -(2+k);
1702                                 goto reject;
1703                         }
1704                 }
1705
1706                 if (secpath_has_nontransport(sp, k, xerr_idxp))
1707                         goto reject;
1708
1709                 xfrm_pols_put(pols, npols);
1710                 return 1;
1711         }
1712
1713 reject:
1714         xfrm_secpath_reject(xerr_idx, skb, &fl);
1715 reject_error:
1716         xfrm_pols_put(pols, npols);
1717         return 0;
1718 }
1719 EXPORT_SYMBOL(__xfrm_policy_check);
1720
1721 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1722 {
1723         struct flowi fl;
1724
1725         if (xfrm_decode_session(skb, &fl, family) < 0)
1726                 return 0;
1727
1728         return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1729 }
1730 EXPORT_SYMBOL(__xfrm_route_forward);
1731
1732 /* Optimize later using cookies and generation ids. */
1733
1734 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1735 {
1736         /* 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.
1755          */
1756         if (dst->obsolete < 0 && !stale_bundle(dst))
1757                 return dst;
1758
1759         return NULL;
1760 }
1761
1762 static int stale_bundle(struct dst_entry *dst)
1763 {
1764         return !xfrm_bundle_ok((struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
1765 }
1766
1767 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
1768 {
1769         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 }
1775 EXPORT_SYMBOL(xfrm_dst_ifdown);
1776
1777 static 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
1783 static 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
1794 static 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
1812 static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1813 {
1814         struct dst_entry *gc_list = NULL;
1815         int dir;
1816
1817         read_lock_bh(&xfrm_policy_lock);
1818         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;
1823
1824                 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);
1832                 }
1833         }
1834         read_unlock_bh(&xfrm_policy_lock);
1835
1836         while (gc_list) {
1837                 struct dst_entry *dst = gc_list;
1838                 gc_list = dst->next;
1839                 dst_free(dst);
1840         }
1841 }
1842
1843 static int unused_bundle(struct dst_entry *dst)
1844 {
1845         return !atomic_read(&dst->__refcnt);
1846 }
1847
1848 static void __xfrm_garbage_collect(void)
1849 {
1850         xfrm_prune_bundles(unused_bundle);
1851 }
1852
1853 static int xfrm_flush_bundles(void)
1854 {
1855         xfrm_prune_bundles(stale_bundle);
1856         return 0;
1857 }
1858
1859 void 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
1880 EXPORT_SYMBOL(xfrm_init_pmtu);
1881
1882 /* Check that the bundle accepts the flow and its components are
1883  * still valid.
1884  */
1885
1886 int xfrm_bundle_ok(struct xfrm_dst *first, struct flowi *fl, int family, int strict)
1887 {
1888         struct dst_entry *dst = &first->u.dst;
1889         struct xfrm_dst *last;
1890         u32 mtu;
1891
1892         if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
1893             (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;
1903                 if (fl && !security_xfrm_flow_state_match(fl, dst->xfrm))
1904                         return 0;
1905                 if (dst->xfrm->km.state != XFRM_STATE_VALID)
1906                         return 0;
1907                 if (xdst->genid != dst->xfrm->genid)
1908                         return 0;
1909
1910                 if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
1911                     !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
1912                         return 0;
1913
1914                 mtu = dst_mtu(dst->child);
1915                 if (xdst->child_mtu_cached != mtu) {
1916                         last = xdst;
1917                         xdst->child_mtu_cached = mtu;
1918                 }
1919
1920                 if (!dst_check(xdst->route, xdst->route_cookie))
1921                         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
1953 EXPORT_SYMBOL(xfrm_bundle_ok);
1954
1955 int 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;
1962         write_lock_bh(&xfrm_policy_afinfo_lock);
1963         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;
1971                 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;
1975                 if (likely(afinfo->garbage_collect == NULL))
1976                         afinfo->garbage_collect = __xfrm_garbage_collect;
1977                 xfrm_policy_afinfo[afinfo->family] = afinfo;
1978         }
1979         write_unlock_bh(&xfrm_policy_afinfo_lock);
1980         return err;
1981 }
1982 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
1983
1984 int 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;
1991         write_lock_bh(&xfrm_policy_afinfo_lock);
1992         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;
2000                         dst_ops->negative_advice = NULL;
2001                         dst_ops->link_failure = NULL;
2002                         afinfo->garbage_collect = NULL;
2003                 }
2004         }
2005         write_unlock_bh(&xfrm_policy_afinfo_lock);
2006         return err;
2007 }
2008 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2009
2010 static 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];
2017         if (unlikely(!afinfo))
2018                 read_unlock(&xfrm_policy_afinfo_lock);
2019         return afinfo;
2020 }
2021
2022 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2023 {
2024         read_unlock(&xfrm_policy_afinfo_lock);
2025 }
2026
2027 static 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
2039 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
2040 {
2041         write_unlock_bh(&xfrm_policy_afinfo_lock);
2042 }
2043
2044 static 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
2053 static struct notifier_block xfrm_dev_notifier = {
2054         xfrm_dev_event,
2055         NULL,
2056         0
2057 };
2058
2059 static void __init xfrm_policy_init(void)
2060 {
2061         unsigned int hmask, sz;
2062         int dir;
2063
2064         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
2071         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
2091         INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task, NULL);
2092         register_netdevice_notifier(&xfrm_dev_notifier);
2093 }
2094
2095 void __init xfrm_init(void)
2096 {
2097         xfrm_state_init();
2098         xfrm_policy_init();
2099         xfrm_input_init();
2100 }
2101