Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[pandora-kernel.git] / 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/cache.h>
26 #include <net/xfrm.h>
27 #include <net/ip.h>
28
29 #include "xfrm_hash.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)
413 {
414         return __idx_hash(index, xfrm_idx_hmask);
415 }
416
417 static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
418 {
419         unsigned int hmask = xfrm_policy_bydst[dir].hmask;
420         unsigned int hash = __sel_hash(sel, family, hmask);
421
422         return (hash == hmask + 1 ?
423                 &xfrm_policy_inexact[dir] :
424                 xfrm_policy_bydst[dir].table + hash);
425 }
426
427 static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
428 {
429         unsigned int hmask = xfrm_policy_bydst[dir].hmask;
430         unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
431
432         return xfrm_policy_bydst[dir].table + hash;
433 }
434
435 static void xfrm_dst_hash_transfer(struct hlist_head *list,
436                                    struct hlist_head *ndsttable,
437                                    unsigned int nhashmask)
438 {
439         struct hlist_node *entry, *tmp;
440         struct xfrm_policy *pol;
441
442         hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
443                 unsigned int h;
444
445                 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
446                                 pol->family, nhashmask);
447                 hlist_add_head(&pol->bydst, ndsttable+h);
448         }
449 }
450
451 static void xfrm_idx_hash_transfer(struct hlist_head *list,
452                                    struct hlist_head *nidxtable,
453                                    unsigned int nhashmask)
454 {
455         struct hlist_node *entry, *tmp;
456         struct xfrm_policy *pol;
457
458         hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
459                 unsigned int h;
460
461                 h = __idx_hash(pol->index, nhashmask);
462                 hlist_add_head(&pol->byidx, nidxtable+h);
463         }
464 }
465
466 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
467 {
468         return ((old_hmask + 1) << 1) - 1;
469 }
470
471 static void xfrm_bydst_resize(int dir)
472 {
473         unsigned int hmask = xfrm_policy_bydst[dir].hmask;
474         unsigned int nhashmask = xfrm_new_hash_mask(hmask);
475         unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
476         struct hlist_head *odst = xfrm_policy_bydst[dir].table;
477         struct hlist_head *ndst = xfrm_hash_alloc(nsize);
478         int i;
479
480         if (!ndst)
481                 return;
482
483         write_lock_bh(&xfrm_policy_lock);
484
485         for (i = hmask; i >= 0; i--)
486                 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
487
488         xfrm_policy_bydst[dir].table = ndst;
489         xfrm_policy_bydst[dir].hmask = nhashmask;
490
491         write_unlock_bh(&xfrm_policy_lock);
492
493         xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
494 }
495
496 static void xfrm_byidx_resize(int total)
497 {
498         unsigned int hmask = xfrm_idx_hmask;
499         unsigned int nhashmask = xfrm_new_hash_mask(hmask);
500         unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
501         struct hlist_head *oidx = xfrm_policy_byidx;
502         struct hlist_head *nidx = xfrm_hash_alloc(nsize);
503         int i;
504
505         if (!nidx)
506                 return;
507
508         write_lock_bh(&xfrm_policy_lock);
509
510         for (i = hmask; i >= 0; i--)
511                 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
512
513         xfrm_policy_byidx = nidx;
514         xfrm_idx_hmask = nhashmask;
515
516         write_unlock_bh(&xfrm_policy_lock);
517
518         xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
519 }
520
521 static inline int xfrm_bydst_should_resize(int dir, int *total)
522 {
523         unsigned int cnt = xfrm_policy_count[dir];
524         unsigned int hmask = xfrm_policy_bydst[dir].hmask;
525
526         if (total)
527                 *total += cnt;
528
529         if ((hmask + 1) < xfrm_policy_hashmax &&
530             cnt > hmask)
531                 return 1;
532
533         return 0;
534 }
535
536 static inline int xfrm_byidx_should_resize(int total)
537 {
538         unsigned int hmask = xfrm_idx_hmask;
539
540         if ((hmask + 1) < xfrm_policy_hashmax &&
541             total > hmask)
542                 return 1;
543
544         return 0;
545 }
546
547 static DEFINE_MUTEX(hash_resize_mutex);
548
549 static void xfrm_hash_resize(void *__unused)
550 {
551         int dir, total;
552
553         mutex_lock(&hash_resize_mutex);
554
555         total = 0;
556         for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
557                 if (xfrm_bydst_should_resize(dir, &total))
558                         xfrm_bydst_resize(dir);
559         }
560         if (xfrm_byidx_should_resize(total))
561                 xfrm_byidx_resize(total);
562
563         mutex_unlock(&hash_resize_mutex);
564 }
565
566 static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize, NULL);
567
568 /* Generate new index... KAME seems to generate them ordered by cost
569  * of an absolute inpredictability of ordering of rules. This will not pass. */
570 static u32 xfrm_gen_index(u8 type, int dir)
571 {
572         static u32 idx_generator;
573
574         for (;;) {
575                 struct hlist_node *entry;
576                 struct hlist_head *list;
577                 struct xfrm_policy *p;
578                 u32 idx;
579                 int found;
580
581                 idx = (idx_generator | dir);
582                 idx_generator += 8;
583                 if (idx == 0)
584                         idx = 8;
585                 list = xfrm_policy_byidx + idx_hash(idx);
586                 found = 0;
587                 hlist_for_each_entry(p, entry, list, byidx) {
588                         if (p->index == idx) {
589                                 found = 1;
590                                 break;
591                         }
592                 }
593                 if (!found)
594                         return idx;
595         }
596 }
597
598 static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
599 {
600         u32 *p1 = (u32 *) s1;
601         u32 *p2 = (u32 *) s2;
602         int len = sizeof(struct xfrm_selector) / sizeof(u32);
603         int i;
604
605         for (i = 0; i < len; i++) {
606                 if (p1[i] != p2[i])
607                         return 1;
608         }
609
610         return 0;
611 }
612
613 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
614 {
615         struct xfrm_policy *pol;
616         struct xfrm_policy *delpol;
617         struct hlist_head *chain;
618         struct hlist_node *entry, *newpos, *last;
619         struct dst_entry *gc_list;
620
621         write_lock_bh(&xfrm_policy_lock);
622         chain = policy_hash_bysel(&policy->selector, policy->family, dir);
623         delpol = NULL;
624         newpos = NULL;
625         last = NULL;
626         hlist_for_each_entry(pol, entry, chain, bydst) {
627                 if (!delpol &&
628                     pol->type == policy->type &&
629                     !selector_cmp(&pol->selector, &policy->selector) &&
630                     xfrm_sec_ctx_match(pol->security, policy->security)) {
631                         if (excl) {
632                                 write_unlock_bh(&xfrm_policy_lock);
633                                 return -EEXIST;
634                         }
635                         delpol = pol;
636                         if (policy->priority > pol->priority)
637                                 continue;
638                 } else if (policy->priority >= pol->priority) {
639                         last = &pol->bydst;
640                         continue;
641                 }
642                 if (!newpos)
643                         newpos = &pol->bydst;
644                 if (delpol)
645                         break;
646                 last = &pol->bydst;
647         }
648         if (!newpos)
649                 newpos = last;
650         if (newpos)
651                 hlist_add_after(newpos, &policy->bydst);
652         else
653                 hlist_add_head(&policy->bydst, chain);
654         xfrm_pol_hold(policy);
655         xfrm_policy_count[dir]++;
656         atomic_inc(&flow_cache_genid);
657         if (delpol) {
658                 hlist_del(&delpol->bydst);
659                 hlist_del(&delpol->byidx);
660                 xfrm_policy_count[dir]--;
661         }
662         policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
663         hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
664         policy->curlft.add_time = (unsigned long)xtime.tv_sec;
665         policy->curlft.use_time = 0;
666         if (!mod_timer(&policy->timer, jiffies + HZ))
667                 xfrm_pol_hold(policy);
668         write_unlock_bh(&xfrm_policy_lock);
669
670         if (delpol)
671                 xfrm_policy_kill(delpol);
672         else if (xfrm_bydst_should_resize(dir, NULL))
673                 schedule_work(&xfrm_hash_work);
674
675         read_lock_bh(&xfrm_policy_lock);
676         gc_list = NULL;
677         entry = &policy->bydst;
678         hlist_for_each_entry_continue(policy, entry, bydst) {
679                 struct dst_entry *dst;
680
681                 write_lock(&policy->lock);
682                 dst = policy->bundles;
683                 if (dst) {
684                         struct dst_entry *tail = dst;
685                         while (tail->next)
686                                 tail = tail->next;
687                         tail->next = gc_list;
688                         gc_list = dst;
689
690                         policy->bundles = NULL;
691                 }
692                 write_unlock(&policy->lock);
693         }
694         read_unlock_bh(&xfrm_policy_lock);
695
696         while (gc_list) {
697                 struct dst_entry *dst = gc_list;
698
699                 gc_list = dst->next;
700                 dst_free(dst);
701         }
702
703         return 0;
704 }
705 EXPORT_SYMBOL(xfrm_policy_insert);
706
707 struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
708                                           struct xfrm_selector *sel,
709                                           struct xfrm_sec_ctx *ctx, int delete)
710 {
711         struct xfrm_policy *pol, *ret;
712         struct hlist_head *chain;
713         struct hlist_node *entry;
714
715         write_lock_bh(&xfrm_policy_lock);
716         chain = policy_hash_bysel(sel, sel->family, dir);
717         ret = NULL;
718         hlist_for_each_entry(pol, entry, chain, bydst) {
719                 if (pol->type == type &&
720                     !selector_cmp(sel, &pol->selector) &&
721                     xfrm_sec_ctx_match(ctx, pol->security)) {
722                         xfrm_pol_hold(pol);
723                         if (delete) {
724                                 hlist_del(&pol->bydst);
725                                 hlist_del(&pol->byidx);
726                                 xfrm_policy_count[dir]--;
727                         }
728                         ret = pol;
729                         break;
730                 }
731         }
732         write_unlock_bh(&xfrm_policy_lock);
733
734         if (ret && delete) {
735                 atomic_inc(&flow_cache_genid);
736                 xfrm_policy_kill(ret);
737         }
738         return ret;
739 }
740 EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
741
742 struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete)
743 {
744         struct xfrm_policy *pol, *ret;
745         struct hlist_head *chain;
746         struct hlist_node *entry;
747
748         write_lock_bh(&xfrm_policy_lock);
749         chain = xfrm_policy_byidx + idx_hash(id);
750         ret = NULL;
751         hlist_for_each_entry(pol, entry, chain, byidx) {
752                 if (pol->type == type && pol->index == id) {
753                         xfrm_pol_hold(pol);
754                         if (delete) {
755                                 hlist_del(&pol->bydst);
756                                 hlist_del(&pol->byidx);
757                                 xfrm_policy_count[dir]--;
758                         }
759                         ret = pol;
760                         break;
761                 }
762         }
763         write_unlock_bh(&xfrm_policy_lock);
764
765         if (ret && delete) {
766                 atomic_inc(&flow_cache_genid);
767                 xfrm_policy_kill(ret);
768         }
769         return ret;
770 }
771 EXPORT_SYMBOL(xfrm_policy_byid);
772
773 void xfrm_policy_flush(u8 type)
774 {
775         int dir;
776
777         write_lock_bh(&xfrm_policy_lock);
778         for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
779                 struct xfrm_policy *pol;
780                 struct hlist_node *entry;
781                 int i, killed;
782
783                 killed = 0;
784         again1:
785                 hlist_for_each_entry(pol, entry,
786                                      &xfrm_policy_inexact[dir], bydst) {
787                         if (pol->type != type)
788                                 continue;
789                         hlist_del(&pol->bydst);
790                         hlist_del(&pol->byidx);
791                         write_unlock_bh(&xfrm_policy_lock);
792
793                         xfrm_policy_kill(pol);
794                         killed++;
795
796                         write_lock_bh(&xfrm_policy_lock);
797                         goto again1;
798                 }
799
800                 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
801         again2:
802                         hlist_for_each_entry(pol, entry,
803                                              xfrm_policy_bydst[dir].table + i,
804                                              bydst) {
805                                 if (pol->type != type)
806                                         continue;
807                                 hlist_del(&pol->bydst);
808                                 hlist_del(&pol->byidx);
809                                 write_unlock_bh(&xfrm_policy_lock);
810
811                                 xfrm_policy_kill(pol);
812                                 killed++;
813
814                                 write_lock_bh(&xfrm_policy_lock);
815                                 goto again2;
816                         }
817                 }
818
819                 xfrm_policy_count[dir] -= killed;
820         }
821         atomic_inc(&flow_cache_genid);
822         write_unlock_bh(&xfrm_policy_lock);
823 }
824 EXPORT_SYMBOL(xfrm_policy_flush);
825
826 int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
827                      void *data)
828 {
829         struct xfrm_policy *pol;
830         struct hlist_node *entry;
831         int dir, count, error;
832
833         read_lock_bh(&xfrm_policy_lock);
834         count = 0;
835         for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
836                 struct hlist_head *table = xfrm_policy_bydst[dir].table;
837                 int i;
838
839                 hlist_for_each_entry(pol, entry,
840                                      &xfrm_policy_inexact[dir], bydst) {
841                         if (pol->type == type)
842                                 count++;
843                 }
844                 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
845                         hlist_for_each_entry(pol, entry, table + i, bydst) {
846                                 if (pol->type == type)
847                                         count++;
848                         }
849                 }
850         }
851
852         if (count == 0) {
853                 error = -ENOENT;
854                 goto out;
855         }
856
857         for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
858                 struct hlist_head *table = xfrm_policy_bydst[dir].table;
859                 int i;
860
861                 hlist_for_each_entry(pol, entry,
862                                      &xfrm_policy_inexact[dir], bydst) {
863                         if (pol->type != type)
864                                 continue;
865                         error = func(pol, dir % XFRM_POLICY_MAX, --count, data);
866                         if (error)
867                                 goto out;
868                 }
869                 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
870                         hlist_for_each_entry(pol, entry, table + i, bydst) {
871                                 if (pol->type != type)
872                                         continue;
873                                 error = func(pol, dir % XFRM_POLICY_MAX, --count, data);
874                                 if (error)
875                                         goto out;
876                         }
877                 }
878         }
879         error = 0;
880 out:
881         read_unlock_bh(&xfrm_policy_lock);
882         return error;
883 }
884 EXPORT_SYMBOL(xfrm_policy_walk);
885
886 /* Find policy to apply to this flow. */
887
888 static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
889                              u8 type, u16 family, int dir)
890 {
891         struct xfrm_selector *sel = &pol->selector;
892         int match;
893
894         if (pol->family != family ||
895             pol->type != type)
896                 return 0;
897
898         match = xfrm_selector_match(sel, fl, family);
899         if (match) {
900                 if (!security_xfrm_policy_lookup(pol, fl->secid, dir))
901                         return 1;
902         }
903
904         return 0;
905 }
906
907 static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
908                                                      u16 family, u8 dir)
909 {
910         struct xfrm_policy *pol, *ret;
911         xfrm_address_t *daddr, *saddr;
912         struct hlist_node *entry;
913         struct hlist_head *chain;
914         u32 priority = ~0U;
915
916         daddr = xfrm_flowi_daddr(fl, family);
917         saddr = xfrm_flowi_saddr(fl, family);
918         if (unlikely(!daddr || !saddr))
919                 return NULL;
920
921         read_lock_bh(&xfrm_policy_lock);
922         chain = policy_hash_direct(daddr, saddr, family, dir);
923         ret = NULL;
924         hlist_for_each_entry(pol, entry, chain, bydst) {
925                 if (xfrm_policy_match(pol, fl, type, family, dir)) {
926                         ret = pol;
927                         priority = ret->priority;
928                         break;
929                 }
930         }
931         chain = &xfrm_policy_inexact[dir];
932         hlist_for_each_entry(pol, entry, chain, bydst) {
933                 if (xfrm_policy_match(pol, fl, type, family, dir) &&
934                     pol->priority < priority) {
935                         ret = pol;
936                         break;
937                 }
938         }
939         if (ret)
940                 xfrm_pol_hold(ret);
941         read_unlock_bh(&xfrm_policy_lock);
942
943         return ret;
944 }
945
946 static void xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
947                                void **objp, atomic_t **obj_refp)
948 {
949         struct xfrm_policy *pol;
950
951 #ifdef CONFIG_XFRM_SUB_POLICY
952         pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
953         if (pol)
954                 goto end;
955 #endif
956         pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
957
958 #ifdef CONFIG_XFRM_SUB_POLICY
959 end:
960 #endif
961         if ((*objp = (void *) pol) != NULL)
962                 *obj_refp = &pol->refcnt;
963 }
964
965 static inline int policy_to_flow_dir(int dir)
966 {
967         if (XFRM_POLICY_IN == FLOW_DIR_IN &&
968             XFRM_POLICY_OUT == FLOW_DIR_OUT &&
969             XFRM_POLICY_FWD == FLOW_DIR_FWD)
970                 return dir;
971         switch (dir) {
972         default:
973         case XFRM_POLICY_IN:
974                 return FLOW_DIR_IN;
975         case XFRM_POLICY_OUT:
976                 return FLOW_DIR_OUT;
977         case XFRM_POLICY_FWD:
978                 return FLOW_DIR_FWD;
979         };
980 }
981
982 static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
983 {
984         struct xfrm_policy *pol;
985
986         read_lock_bh(&xfrm_policy_lock);
987         if ((pol = sk->sk_policy[dir]) != NULL) {
988                 int match = xfrm_selector_match(&pol->selector, fl,
989                                                 sk->sk_family);
990                 int err = 0;
991
992                 if (match)
993                   err = security_xfrm_policy_lookup(pol, fl->secid, policy_to_flow_dir(dir));
994
995                 if (match && !err)
996                         xfrm_pol_hold(pol);
997                 else
998                         pol = NULL;
999         }
1000         read_unlock_bh(&xfrm_policy_lock);
1001         return pol;
1002 }
1003
1004 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1005 {
1006         struct hlist_head *chain = policy_hash_bysel(&pol->selector,
1007                                                      pol->family, dir);
1008
1009         hlist_add_head(&pol->bydst, chain);
1010         hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
1011         xfrm_policy_count[dir]++;
1012         xfrm_pol_hold(pol);
1013
1014         if (xfrm_bydst_should_resize(dir, NULL))
1015                 schedule_work(&xfrm_hash_work);
1016 }
1017
1018 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1019                                                 int dir)
1020 {
1021         if (hlist_unhashed(&pol->bydst))
1022                 return NULL;
1023
1024         hlist_del(&pol->bydst);
1025         hlist_del(&pol->byidx);
1026         xfrm_policy_count[dir]--;
1027
1028         return pol;
1029 }
1030
1031 int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
1032 {
1033         write_lock_bh(&xfrm_policy_lock);
1034         pol = __xfrm_policy_unlink(pol, dir);
1035         write_unlock_bh(&xfrm_policy_lock);
1036         if (pol) {
1037                 if (dir < XFRM_POLICY_MAX)
1038                         atomic_inc(&flow_cache_genid);
1039                 xfrm_policy_kill(pol);
1040                 return 0;
1041         }
1042         return -ENOENT;
1043 }
1044 EXPORT_SYMBOL(xfrm_policy_delete);
1045
1046 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1047 {
1048         struct xfrm_policy *old_pol;
1049
1050 #ifdef CONFIG_XFRM_SUB_POLICY
1051         if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1052                 return -EINVAL;
1053 #endif
1054
1055         write_lock_bh(&xfrm_policy_lock);
1056         old_pol = sk->sk_policy[dir];
1057         sk->sk_policy[dir] = pol;
1058         if (pol) {
1059                 pol->curlft.add_time = (unsigned long)xtime.tv_sec;
1060                 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
1061                 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1062         }
1063         if (old_pol)
1064                 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1065         write_unlock_bh(&xfrm_policy_lock);
1066
1067         if (old_pol) {
1068                 xfrm_policy_kill(old_pol);
1069         }
1070         return 0;
1071 }
1072
1073 static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1074 {
1075         struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
1076
1077         if (newp) {
1078                 newp->selector = old->selector;
1079                 if (security_xfrm_policy_clone(old, newp)) {
1080                         kfree(newp);
1081                         return NULL;  /* ENOMEM */
1082                 }
1083                 newp->lft = old->lft;
1084                 newp->curlft = old->curlft;
1085                 newp->action = old->action;
1086                 newp->flags = old->flags;
1087                 newp->xfrm_nr = old->xfrm_nr;
1088                 newp->index = old->index;
1089                 newp->type = old->type;
1090                 memcpy(newp->xfrm_vec, old->xfrm_vec,
1091                        newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1092                 write_lock_bh(&xfrm_policy_lock);
1093                 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1094                 write_unlock_bh(&xfrm_policy_lock);
1095                 xfrm_pol_put(newp);
1096         }
1097         return newp;
1098 }
1099
1100 int __xfrm_sk_clone_policy(struct sock *sk)
1101 {
1102         struct xfrm_policy *p0 = sk->sk_policy[0],
1103                            *p1 = sk->sk_policy[1];
1104
1105         sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1106         if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1107                 return -ENOMEM;
1108         if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1109                 return -ENOMEM;
1110         return 0;
1111 }
1112
1113 static int
1114 xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
1115                unsigned short family)
1116 {
1117         int err;
1118         struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1119
1120         if (unlikely(afinfo == NULL))
1121                 return -EINVAL;
1122         err = afinfo->get_saddr(local, remote);
1123         xfrm_policy_put_afinfo(afinfo);
1124         return err;
1125 }
1126
1127 /* Resolve list of templates for the flow, given policy. */
1128
1129 static int
1130 xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1131                       struct xfrm_state **xfrm,
1132                       unsigned short family)
1133 {
1134         int nx;
1135         int i, error;
1136         xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1137         xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
1138         xfrm_address_t tmp;
1139
1140         for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1141                 struct xfrm_state *x;
1142                 xfrm_address_t *remote = daddr;
1143                 xfrm_address_t *local  = saddr;
1144                 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1145
1146                 if (tmpl->mode == XFRM_MODE_TUNNEL) {
1147                         remote = &tmpl->id.daddr;
1148                         local = &tmpl->saddr;
1149                         if (xfrm_addr_any(local, family)) {
1150                                 error = xfrm_get_saddr(&tmp, remote, family);
1151                                 if (error)
1152                                         goto fail;
1153                                 local = &tmp;
1154                         }
1155                 }
1156
1157                 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1158
1159                 if (x && x->km.state == XFRM_STATE_VALID) {
1160                         xfrm[nx++] = x;
1161                         daddr = remote;
1162                         saddr = local;
1163                         continue;
1164                 }
1165                 if (x) {
1166                         error = (x->km.state == XFRM_STATE_ERROR ?
1167                                  -EINVAL : -EAGAIN);
1168                         xfrm_state_put(x);
1169                 }
1170
1171                 if (!tmpl->optional)
1172                         goto fail;
1173         }
1174         return nx;
1175
1176 fail:
1177         for (nx--; nx>=0; nx--)
1178                 xfrm_state_put(xfrm[nx]);
1179         return error;
1180 }
1181
1182 static int
1183 xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1184                   struct xfrm_state **xfrm,
1185                   unsigned short family)
1186 {
1187         struct xfrm_state *tp[XFRM_MAX_DEPTH];
1188         struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
1189         int cnx = 0;
1190         int error;
1191         int ret;
1192         int i;
1193
1194         for (i = 0; i < npols; i++) {
1195                 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1196                         error = -ENOBUFS;
1197                         goto fail;
1198                 }
1199
1200                 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
1201                 if (ret < 0) {
1202                         error = ret;
1203                         goto fail;
1204                 } else
1205                         cnx += ret;
1206         }
1207
1208         /* found states are sorted for outbound processing */
1209         if (npols > 1)
1210                 xfrm_state_sort(xfrm, tpp, cnx, family);
1211
1212         return cnx;
1213
1214  fail:
1215         for (cnx--; cnx>=0; cnx--)
1216                 xfrm_state_put(tpp[cnx]);
1217         return error;
1218
1219 }
1220
1221 /* Check that the bundle accepts the flow and its components are
1222  * still valid.
1223  */
1224
1225 static struct dst_entry *
1226 xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1227 {
1228         struct dst_entry *x;
1229         struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1230         if (unlikely(afinfo == NULL))
1231                 return ERR_PTR(-EINVAL);
1232         x = afinfo->find_bundle(fl, policy);
1233         xfrm_policy_put_afinfo(afinfo);
1234         return x;
1235 }
1236
1237 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1238  * all the metrics... Shortly, bundle a bundle.
1239  */
1240
1241 static int
1242 xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
1243                    struct flowi *fl, struct dst_entry **dst_p,
1244                    unsigned short family)
1245 {
1246         int err;
1247         struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1248         if (unlikely(afinfo == NULL))
1249                 return -EINVAL;
1250         err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
1251         xfrm_policy_put_afinfo(afinfo);
1252         return err;
1253 }
1254
1255
1256 static int stale_bundle(struct dst_entry *dst);
1257
1258 /* Main function: finds/creates a bundle for given flow.
1259  *
1260  * At the moment we eat a raw IP route. Mostly to speed up lookups
1261  * on interfaces with disabled IPsec.
1262  */
1263 int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1264                 struct sock *sk, int flags)
1265 {
1266         struct xfrm_policy *policy;
1267         struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1268         int npols;
1269         int pol_dead;
1270         int xfrm_nr;
1271         int pi;
1272         struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1273         struct dst_entry *dst, *dst_orig = *dst_p;
1274         int nx = 0;
1275         int err;
1276         u32 genid;
1277         u16 family;
1278         u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
1279
1280 restart:
1281         genid = atomic_read(&flow_cache_genid);
1282         policy = NULL;
1283         for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1284                 pols[pi] = NULL;
1285         npols = 0;
1286         pol_dead = 0;
1287         xfrm_nr = 0;
1288
1289         if (sk && sk->sk_policy[1])
1290                 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
1291
1292         if (!policy) {
1293                 /* To accelerate a bit...  */
1294                 if ((dst_orig->flags & DST_NOXFRM) ||
1295                     !xfrm_policy_count[XFRM_POLICY_OUT])
1296                         return 0;
1297
1298                 policy = flow_cache_lookup(fl, dst_orig->ops->family,
1299                                            dir, xfrm_policy_lookup);
1300         }
1301
1302         if (!policy)
1303                 return 0;
1304
1305         family = dst_orig->ops->family;
1306         policy->curlft.use_time = (unsigned long)xtime.tv_sec;
1307         pols[0] = policy;
1308         npols ++;
1309         xfrm_nr += pols[0]->xfrm_nr;
1310
1311         switch (policy->action) {
1312         case XFRM_POLICY_BLOCK:
1313                 /* Prohibit the flow */
1314                 err = -EPERM;
1315                 goto error;
1316
1317         case XFRM_POLICY_ALLOW:
1318 #ifndef CONFIG_XFRM_SUB_POLICY
1319                 if (policy->xfrm_nr == 0) {
1320                         /* Flow passes not transformed. */
1321                         xfrm_pol_put(policy);
1322                         return 0;
1323                 }
1324 #endif
1325
1326                 /* Try to find matching bundle.
1327                  *
1328                  * LATER: help from flow cache. It is optional, this
1329                  * is required only for output policy.
1330                  */
1331                 dst = xfrm_find_bundle(fl, policy, family);
1332                 if (IS_ERR(dst)) {
1333                         err = PTR_ERR(dst);
1334                         goto error;
1335                 }
1336
1337                 if (dst)
1338                         break;
1339
1340 #ifdef CONFIG_XFRM_SUB_POLICY
1341                 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1342                         pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1343                                                             fl, family,
1344                                                             XFRM_POLICY_OUT);
1345                         if (pols[1]) {
1346                                 if (pols[1]->action == XFRM_POLICY_BLOCK) {
1347                                         err = -EPERM;
1348                                         goto error;
1349                                 }
1350                                 npols ++;
1351                                 xfrm_nr += pols[1]->xfrm_nr;
1352                         }
1353                 }
1354
1355                 /*
1356                  * Because neither flowi nor bundle information knows about
1357                  * transformation template size. On more than one policy usage
1358                  * we can realize whether all of them is bypass or not after
1359                  * they are searched. See above not-transformed bypass
1360                  * is surrounded by non-sub policy configuration, too.
1361                  */
1362                 if (xfrm_nr == 0) {
1363                         /* Flow passes not transformed. */
1364                         xfrm_pols_put(pols, npols);
1365                         return 0;
1366                 }
1367
1368 #endif
1369                 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
1370
1371                 if (unlikely(nx<0)) {
1372                         err = nx;
1373                         if (err == -EAGAIN && flags) {
1374                                 DECLARE_WAITQUEUE(wait, current);
1375
1376                                 add_wait_queue(&km_waitq, &wait);
1377                                 set_current_state(TASK_INTERRUPTIBLE);
1378                                 schedule();
1379                                 set_current_state(TASK_RUNNING);
1380                                 remove_wait_queue(&km_waitq, &wait);
1381
1382                                 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
1383
1384                                 if (nx == -EAGAIN && signal_pending(current)) {
1385                                         err = -ERESTART;
1386                                         goto error;
1387                                 }
1388                                 if (nx == -EAGAIN ||
1389                                     genid != atomic_read(&flow_cache_genid)) {
1390                                         xfrm_pols_put(pols, npols);
1391                                         goto restart;
1392                                 }
1393                                 err = nx;
1394                         }
1395                         if (err < 0)
1396                                 goto error;
1397                 }
1398                 if (nx == 0) {
1399                         /* Flow passes not transformed. */
1400                         xfrm_pols_put(pols, npols);
1401                         return 0;
1402                 }
1403
1404                 dst = dst_orig;
1405                 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
1406
1407                 if (unlikely(err)) {
1408                         int i;
1409                         for (i=0; i<nx; i++)
1410                                 xfrm_state_put(xfrm[i]);
1411                         goto error;
1412                 }
1413
1414                 for (pi = 0; pi < npols; pi++) {
1415                         read_lock_bh(&pols[pi]->lock);
1416                         pol_dead |= pols[pi]->dead;
1417                         read_unlock_bh(&pols[pi]->lock);
1418                 }
1419
1420                 write_lock_bh(&policy->lock);
1421                 if (unlikely(pol_dead || stale_bundle(dst))) {
1422                         /* Wow! While we worked on resolving, this
1423                          * policy has gone. Retry. It is not paranoia,
1424                          * we just cannot enlist new bundle to dead object.
1425                          * We can't enlist stable bundles either.
1426                          */
1427                         write_unlock_bh(&policy->lock);
1428                         if (dst)
1429                                 dst_free(dst);
1430
1431                         err = -EHOSTUNREACH;
1432                         goto error;
1433                 }
1434                 dst->next = policy->bundles;
1435                 policy->bundles = dst;
1436                 dst_hold(dst);
1437                 write_unlock_bh(&policy->lock);
1438         }
1439         *dst_p = dst;
1440         dst_release(dst_orig);
1441         xfrm_pols_put(pols, npols);
1442         return 0;
1443
1444 error:
1445         dst_release(dst_orig);
1446         xfrm_pols_put(pols, npols);
1447         *dst_p = NULL;
1448         return err;
1449 }
1450 EXPORT_SYMBOL(xfrm_lookup);
1451
1452 static inline int
1453 xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1454 {
1455         struct xfrm_state *x;
1456         int err;
1457
1458         if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1459                 return 0;
1460         x = skb->sp->xvec[idx];
1461         if (!x->type->reject)
1462                 return 0;
1463         xfrm_state_hold(x);
1464         err = x->type->reject(x, skb, fl);
1465         xfrm_state_put(x);
1466         return err;
1467 }
1468
1469 /* When skb is transformed back to its "native" form, we have to
1470  * check policy restrictions. At the moment we make this in maximally
1471  * stupid way. Shame on me. :-) Of course, connected sockets must
1472  * have policy cached at them.
1473  */
1474
1475 static inline int
1476 xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x, 
1477               unsigned short family)
1478 {
1479         if (xfrm_state_kern(x))
1480                 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family);
1481         return  x->id.proto == tmpl->id.proto &&
1482                 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1483                 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1484                 x->props.mode == tmpl->mode &&
1485                 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1486                  !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
1487                 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1488                   xfrm_state_addr_cmp(tmpl, x, family));
1489 }
1490
1491 /*
1492  * 0 or more than 0 is returned when validation is succeeded (either bypass
1493  * because of optional transport mode, or next index of the mathced secpath
1494  * state with the template.
1495  * -1 is returned when no matching template is found.
1496  * Otherwise "-2 - errored_index" is returned.
1497  */
1498 static inline int
1499 xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1500                unsigned short family)
1501 {
1502         int idx = start;
1503
1504         if (tmpl->optional) {
1505                 if (tmpl->mode == XFRM_MODE_TRANSPORT)
1506                         return start;
1507         } else
1508                 start = -1;
1509         for (; idx < sp->len; idx++) {
1510                 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
1511                         return ++idx;
1512                 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1513                         if (start == -1)
1514                                 start = -2-idx;
1515                         break;
1516                 }
1517         }
1518         return start;
1519 }
1520
1521 int
1522 xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
1523 {
1524         struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1525         int err;
1526
1527         if (unlikely(afinfo == NULL))
1528                 return -EAFNOSUPPORT;
1529
1530         afinfo->decode_session(skb, fl);
1531         err = security_xfrm_decode_session(skb, &fl->secid);
1532         xfrm_policy_put_afinfo(afinfo);
1533         return err;
1534 }
1535 EXPORT_SYMBOL(xfrm_decode_session);
1536
1537 static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
1538 {
1539         for (; k < sp->len; k++) {
1540                 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
1541                         *idxp = k;
1542                         return 1;
1543                 }
1544         }
1545
1546         return 0;
1547 }
1548
1549 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, 
1550                         unsigned short family)
1551 {
1552         struct xfrm_policy *pol;
1553         struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1554         int npols = 0;
1555         int xfrm_nr;
1556         int pi;
1557         struct flowi fl;
1558         u8 fl_dir = policy_to_flow_dir(dir);
1559         int xerr_idx = -1;
1560
1561         if (xfrm_decode_session(skb, &fl, family) < 0)
1562                 return 0;
1563         nf_nat_decode_session(skb, &fl, family);
1564
1565         /* First, check used SA against their selectors. */
1566         if (skb->sp) {
1567                 int i;
1568
1569                 for (i=skb->sp->len-1; i>=0; i--) {
1570                         struct xfrm_state *x = skb->sp->xvec[i];
1571                         if (!xfrm_selector_match(&x->sel, &fl, family))
1572                                 return 0;
1573                 }
1574         }
1575
1576         pol = NULL;
1577         if (sk && sk->sk_policy[dir])
1578                 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
1579
1580         if (!pol)
1581                 pol = flow_cache_lookup(&fl, family, fl_dir,
1582                                         xfrm_policy_lookup);
1583
1584         if (!pol) {
1585                 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
1586                         xfrm_secpath_reject(xerr_idx, skb, &fl);
1587                         return 0;
1588                 }
1589                 return 1;
1590         }
1591
1592         pol->curlft.use_time = (unsigned long)xtime.tv_sec;
1593
1594         pols[0] = pol;
1595         npols ++;
1596 #ifdef CONFIG_XFRM_SUB_POLICY
1597         if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1598                 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1599                                                     &fl, family,
1600                                                     XFRM_POLICY_IN);
1601                 if (pols[1]) {
1602                         pols[1]->curlft.use_time = (unsigned long)xtime.tv_sec;
1603                         npols ++;
1604                 }
1605         }
1606 #endif
1607
1608         if (pol->action == XFRM_POLICY_ALLOW) {
1609                 struct sec_path *sp;
1610                 static struct sec_path dummy;
1611                 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
1612                 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
1613                 struct xfrm_tmpl **tpp = tp;
1614                 int ti = 0;
1615                 int i, k;
1616
1617                 if ((sp = skb->sp) == NULL)
1618                         sp = &dummy;
1619
1620                 for (pi = 0; pi < npols; pi++) {
1621                         if (pols[pi] != pol &&
1622                             pols[pi]->action != XFRM_POLICY_ALLOW)
1623                                 goto reject;
1624                         if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
1625                                 goto reject_error;
1626                         for (i = 0; i < pols[pi]->xfrm_nr; i++)
1627                                 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1628                 }
1629                 xfrm_nr = ti;
1630                 if (npols > 1) {
1631                         xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1632                         tpp = stp;
1633                 }
1634
1635                 /* For each tunnel xfrm, find the first matching tmpl.
1636                  * For each tmpl before that, find corresponding xfrm.
1637                  * Order is _important_. Later we will implement
1638                  * some barriers, but at the moment barriers
1639                  * are implied between each two transformations.
1640                  */
1641                 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1642                         k = xfrm_policy_ok(tpp[i], sp, k, family);
1643                         if (k < 0) {
1644                                 if (k < -1)
1645                                         /* "-2 - errored_index" returned */
1646                                         xerr_idx = -(2+k);
1647                                 goto reject;
1648                         }
1649                 }
1650
1651                 if (secpath_has_nontransport(sp, k, &xerr_idx))
1652                         goto reject;
1653
1654                 xfrm_pols_put(pols, npols);
1655                 return 1;
1656         }
1657
1658 reject:
1659         xfrm_secpath_reject(xerr_idx, skb, &fl);
1660 reject_error:
1661         xfrm_pols_put(pols, npols);
1662         return 0;
1663 }
1664 EXPORT_SYMBOL(__xfrm_policy_check);
1665
1666 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1667 {
1668         struct flowi fl;
1669
1670         if (xfrm_decode_session(skb, &fl, family) < 0)
1671                 return 0;
1672
1673         return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1674 }
1675 EXPORT_SYMBOL(__xfrm_route_forward);
1676
1677 /* Optimize later using cookies and generation ids. */
1678
1679 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1680 {
1681         /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1682          * to "-1" to force all XFRM destinations to get validated by
1683          * dst_ops->check on every use.  We do this because when a
1684          * normal route referenced by an XFRM dst is obsoleted we do
1685          * not go looking around for all parent referencing XFRM dsts
1686          * so that we can invalidate them.  It is just too much work.
1687          * Instead we make the checks here on every use.  For example:
1688          *
1689          *      XFRM dst A --> IPv4 dst X
1690          *
1691          * X is the "xdst->route" of A (X is also the "dst->path" of A
1692          * in this example).  If X is marked obsolete, "A" will not
1693          * notice.  That's what we are validating here via the
1694          * stale_bundle() check.
1695          *
1696          * When a policy's bundle is pruned, we dst_free() the XFRM
1697          * dst which causes it's ->obsolete field to be set to a
1698          * positive non-zero integer.  If an XFRM dst has been pruned
1699          * like this, we want to force a new route lookup.
1700          */
1701         if (dst->obsolete < 0 && !stale_bundle(dst))
1702                 return dst;
1703
1704         return NULL;
1705 }
1706
1707 static int stale_bundle(struct dst_entry *dst)
1708 {
1709         return !xfrm_bundle_ok((struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
1710 }
1711
1712 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
1713 {
1714         while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
1715                 dst->dev = &loopback_dev;
1716                 dev_hold(&loopback_dev);
1717                 dev_put(dev);
1718         }
1719 }
1720 EXPORT_SYMBOL(xfrm_dst_ifdown);
1721
1722 static void xfrm_link_failure(struct sk_buff *skb)
1723 {
1724         /* Impossible. Such dst must be popped before reaches point of failure. */
1725         return;
1726 }
1727
1728 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1729 {
1730         if (dst) {
1731                 if (dst->obsolete) {
1732                         dst_release(dst);
1733                         dst = NULL;
1734                 }
1735         }
1736         return dst;
1737 }
1738
1739 static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
1740 {
1741         struct dst_entry *dst, **dstp;
1742
1743         write_lock(&pol->lock);
1744         dstp = &pol->bundles;
1745         while ((dst=*dstp) != NULL) {
1746                 if (func(dst)) {
1747                         *dstp = dst->next;
1748                         dst->next = *gc_list_p;
1749                         *gc_list_p = dst;
1750                 } else {
1751                         dstp = &dst->next;
1752                 }
1753         }
1754         write_unlock(&pol->lock);
1755 }
1756
1757 static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1758 {
1759         struct dst_entry *gc_list = NULL;
1760         int dir;
1761
1762         read_lock_bh(&xfrm_policy_lock);
1763         for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
1764                 struct xfrm_policy *pol;
1765                 struct hlist_node *entry;
1766                 struct hlist_head *table;
1767                 int i;
1768
1769                 hlist_for_each_entry(pol, entry,
1770                                      &xfrm_policy_inexact[dir], bydst)
1771                         prune_one_bundle(pol, func, &gc_list);
1772
1773                 table = xfrm_policy_bydst[dir].table;
1774                 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
1775                         hlist_for_each_entry(pol, entry, table + i, bydst)
1776                                 prune_one_bundle(pol, func, &gc_list);
1777                 }
1778         }
1779         read_unlock_bh(&xfrm_policy_lock);
1780
1781         while (gc_list) {
1782                 struct dst_entry *dst = gc_list;
1783                 gc_list = dst->next;
1784                 dst_free(dst);
1785         }
1786 }
1787
1788 static int unused_bundle(struct dst_entry *dst)
1789 {
1790         return !atomic_read(&dst->__refcnt);
1791 }
1792
1793 static void __xfrm_garbage_collect(void)
1794 {
1795         xfrm_prune_bundles(unused_bundle);
1796 }
1797
1798 static int xfrm_flush_bundles(void)
1799 {
1800         xfrm_prune_bundles(stale_bundle);
1801         return 0;
1802 }
1803
1804 void xfrm_init_pmtu(struct dst_entry *dst)
1805 {
1806         do {
1807                 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1808                 u32 pmtu, route_mtu_cached;
1809
1810                 pmtu = dst_mtu(dst->child);
1811                 xdst->child_mtu_cached = pmtu;
1812
1813                 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
1814
1815                 route_mtu_cached = dst_mtu(xdst->route);
1816                 xdst->route_mtu_cached = route_mtu_cached;
1817
1818                 if (pmtu > route_mtu_cached)
1819                         pmtu = route_mtu_cached;
1820
1821                 dst->metrics[RTAX_MTU-1] = pmtu;
1822         } while ((dst = dst->next));
1823 }
1824
1825 EXPORT_SYMBOL(xfrm_init_pmtu);
1826
1827 /* Check that the bundle accepts the flow and its components are
1828  * still valid.
1829  */
1830
1831 int xfrm_bundle_ok(struct xfrm_dst *first, struct flowi *fl, int family, int strict)
1832 {
1833         struct dst_entry *dst = &first->u.dst;
1834         struct xfrm_dst *last;
1835         u32 mtu;
1836
1837         if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
1838             (dst->dev && !netif_running(dst->dev)))
1839                 return 0;
1840
1841         last = NULL;
1842
1843         do {
1844                 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1845
1846                 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
1847                         return 0;
1848                 if (fl && !security_xfrm_flow_state_match(fl, dst->xfrm))
1849                         return 0;
1850                 if (dst->xfrm->km.state != XFRM_STATE_VALID)
1851                         return 0;
1852                 if (xdst->genid != dst->xfrm->genid)
1853                         return 0;
1854
1855                 if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
1856                     !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
1857                         return 0;
1858
1859                 mtu = dst_mtu(dst->child);
1860                 if (xdst->child_mtu_cached != mtu) {
1861                         last = xdst;
1862                         xdst->child_mtu_cached = mtu;
1863                 }
1864
1865                 if (!dst_check(xdst->route, xdst->route_cookie))
1866                         return 0;
1867                 mtu = dst_mtu(xdst->route);
1868                 if (xdst->route_mtu_cached != mtu) {
1869                         last = xdst;
1870                         xdst->route_mtu_cached = mtu;
1871                 }
1872
1873                 dst = dst->child;
1874         } while (dst->xfrm);
1875
1876         if (likely(!last))
1877                 return 1;
1878
1879         mtu = last->child_mtu_cached;
1880         for (;;) {
1881                 dst = &last->u.dst;
1882
1883                 mtu = xfrm_state_mtu(dst->xfrm, mtu);
1884                 if (mtu > last->route_mtu_cached)
1885                         mtu = last->route_mtu_cached;
1886                 dst->metrics[RTAX_MTU-1] = mtu;
1887
1888                 if (last == first)
1889                         break;
1890
1891                 last = last->u.next;
1892                 last->child_mtu_cached = mtu;
1893         }
1894
1895         return 1;
1896 }
1897
1898 EXPORT_SYMBOL(xfrm_bundle_ok);
1899
1900 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
1901 {
1902         int err = 0;
1903         if (unlikely(afinfo == NULL))
1904                 return -EINVAL;
1905         if (unlikely(afinfo->family >= NPROTO))
1906                 return -EAFNOSUPPORT;
1907         write_lock_bh(&xfrm_policy_afinfo_lock);
1908         if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
1909                 err = -ENOBUFS;
1910         else {
1911                 struct dst_ops *dst_ops = afinfo->dst_ops;
1912                 if (likely(dst_ops->kmem_cachep == NULL))
1913                         dst_ops->kmem_cachep = xfrm_dst_cache;
1914                 if (likely(dst_ops->check == NULL))
1915                         dst_ops->check = xfrm_dst_check;
1916                 if (likely(dst_ops->negative_advice == NULL))
1917                         dst_ops->negative_advice = xfrm_negative_advice;
1918                 if (likely(dst_ops->link_failure == NULL))
1919                         dst_ops->link_failure = xfrm_link_failure;
1920                 if (likely(afinfo->garbage_collect == NULL))
1921                         afinfo->garbage_collect = __xfrm_garbage_collect;
1922                 xfrm_policy_afinfo[afinfo->family] = afinfo;
1923         }
1924         write_unlock_bh(&xfrm_policy_afinfo_lock);
1925         return err;
1926 }
1927 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
1928
1929 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
1930 {
1931         int err = 0;
1932         if (unlikely(afinfo == NULL))
1933                 return -EINVAL;
1934         if (unlikely(afinfo->family >= NPROTO))
1935                 return -EAFNOSUPPORT;
1936         write_lock_bh(&xfrm_policy_afinfo_lock);
1937         if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
1938                 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
1939                         err = -EINVAL;
1940                 else {
1941                         struct dst_ops *dst_ops = afinfo->dst_ops;
1942                         xfrm_policy_afinfo[afinfo->family] = NULL;
1943                         dst_ops->kmem_cachep = NULL;
1944                         dst_ops->check = NULL;
1945                         dst_ops->negative_advice = NULL;
1946                         dst_ops->link_failure = NULL;
1947                         afinfo->garbage_collect = NULL;
1948                 }
1949         }
1950         write_unlock_bh(&xfrm_policy_afinfo_lock);
1951         return err;
1952 }
1953 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
1954
1955 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
1956 {
1957         struct xfrm_policy_afinfo *afinfo;
1958         if (unlikely(family >= NPROTO))
1959                 return NULL;
1960         read_lock(&xfrm_policy_afinfo_lock);
1961         afinfo = xfrm_policy_afinfo[family];
1962         if (unlikely(!afinfo))
1963                 read_unlock(&xfrm_policy_afinfo_lock);
1964         return afinfo;
1965 }
1966
1967 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
1968 {
1969         read_unlock(&xfrm_policy_afinfo_lock);
1970 }
1971
1972 static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
1973 {
1974         struct xfrm_policy_afinfo *afinfo;
1975         if (unlikely(family >= NPROTO))
1976                 return NULL;
1977         write_lock_bh(&xfrm_policy_afinfo_lock);
1978         afinfo = xfrm_policy_afinfo[family];
1979         if (unlikely(!afinfo))
1980                 write_unlock_bh(&xfrm_policy_afinfo_lock);
1981         return afinfo;
1982 }
1983
1984 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
1985 {
1986         write_unlock_bh(&xfrm_policy_afinfo_lock);
1987 }
1988
1989 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
1990 {
1991         switch (event) {
1992         case NETDEV_DOWN:
1993                 xfrm_flush_bundles();
1994         }
1995         return NOTIFY_DONE;
1996 }
1997
1998 static struct notifier_block xfrm_dev_notifier = {
1999         xfrm_dev_event,
2000         NULL,
2001         0
2002 };
2003
2004 static void __init xfrm_policy_init(void)
2005 {
2006         unsigned int hmask, sz;
2007         int dir;
2008
2009         xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2010                                            sizeof(struct xfrm_dst),
2011                                            0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2012                                            NULL, NULL);
2013
2014         hmask = 8 - 1;
2015         sz = (hmask+1) * sizeof(struct hlist_head);
2016
2017         xfrm_policy_byidx = xfrm_hash_alloc(sz);
2018         xfrm_idx_hmask = hmask;
2019         if (!xfrm_policy_byidx)
2020                 panic("XFRM: failed to allocate byidx hash\n");
2021
2022         for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2023                 struct xfrm_policy_hash *htab;
2024
2025                 INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
2026
2027                 htab = &xfrm_policy_bydst[dir];
2028                 htab->table = xfrm_hash_alloc(sz);
2029                 htab->hmask = hmask;
2030                 if (!htab->table)
2031                         panic("XFRM: failed to allocate bydst hash\n");
2032         }
2033
2034         INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task, NULL);
2035         register_netdevice_notifier(&xfrm_dev_notifier);
2036 }
2037
2038 void __init xfrm_init(void)
2039 {
2040         xfrm_state_init();
2041         xfrm_policy_init();
2042         xfrm_input_init();
2043 }
2044