blob: b446ca31fecc8a2c3a823aebece2532fc7f8da6d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm_policy.c
3 *
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * Kazunori MIYAZAWA @USAGI
10 * YOSHIFUJI Hideaki
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
Trent Jaegerdf718372005-12-13 23:12:27 -080013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/slab.h>
17#include <linux/kmod.h>
18#include <linux/list.h>
19#include <linux/spinlock.h>
20#include <linux/workqueue.h>
21#include <linux/notifier.h>
22#include <linux/netdevice.h>
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -080023#include <linux/netfilter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
David S. Miller2518c7c2006-08-24 04:45:07 -070025#include <linux/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/xfrm.h>
27#include <net/ip.h>
28
David S. Miller44e36b42006-08-24 04:50:50 -070029#include "xfrm_hash.h"
30
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080031DEFINE_MUTEX(xfrm_cfg_mutex);
32EXPORT_SYMBOL(xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34static DEFINE_RWLOCK(xfrm_policy_lock);
35
David S. Miller2518c7c2006-08-24 04:45:07 -070036unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
37EXPORT_SYMBOL(xfrm_policy_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
40static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
41
Eric Dumazetba899662005-08-26 12:05:31 -070042static kmem_cache_t *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44static struct work_struct xfrm_policy_gc_work;
David S. Miller2518c7c2006-08-24 04:45:07 -070045static HLIST_HEAD(xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
47
48static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
49static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
Herbert Xu546be242006-05-27 23:03:58 -070050static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family);
51static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53int xfrm_register_type(struct xfrm_type *type, unsigned short family)
54{
Herbert Xu546be242006-05-27 23:03:58 -070055 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
56 struct xfrm_type **typemap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 int err = 0;
58
59 if (unlikely(afinfo == NULL))
60 return -EAFNOSUPPORT;
61 typemap = afinfo->type_map;
62
Herbert Xu546be242006-05-27 23:03:58 -070063 if (likely(typemap[type->proto] == NULL))
64 typemap[type->proto] = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 else
66 err = -EEXIST;
Herbert Xu546be242006-05-27 23:03:58 -070067 xfrm_policy_unlock_afinfo(afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return err;
69}
70EXPORT_SYMBOL(xfrm_register_type);
71
72int xfrm_unregister_type(struct xfrm_type *type, unsigned short family)
73{
Herbert Xu546be242006-05-27 23:03:58 -070074 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
75 struct xfrm_type **typemap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 int err = 0;
77
78 if (unlikely(afinfo == NULL))
79 return -EAFNOSUPPORT;
80 typemap = afinfo->type_map;
81
Herbert Xu546be242006-05-27 23:03:58 -070082 if (unlikely(typemap[type->proto] != type))
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 err = -ENOENT;
84 else
Herbert Xu546be242006-05-27 23:03:58 -070085 typemap[type->proto] = NULL;
86 xfrm_policy_unlock_afinfo(afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return err;
88}
89EXPORT_SYMBOL(xfrm_unregister_type);
90
91struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
92{
93 struct xfrm_policy_afinfo *afinfo;
Herbert Xu546be242006-05-27 23:03:58 -070094 struct xfrm_type **typemap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct xfrm_type *type;
96 int modload_attempted = 0;
97
98retry:
99 afinfo = xfrm_policy_get_afinfo(family);
100 if (unlikely(afinfo == NULL))
101 return NULL;
102 typemap = afinfo->type_map;
103
Herbert Xu546be242006-05-27 23:03:58 -0700104 type = typemap[proto];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (unlikely(type && !try_module_get(type->owner)))
106 type = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (!type && !modload_attempted) {
108 xfrm_policy_put_afinfo(afinfo);
109 request_module("xfrm-type-%d-%d",
110 (int) family, (int) proto);
111 modload_attempted = 1;
112 goto retry;
113 }
114
115 xfrm_policy_put_afinfo(afinfo);
116 return type;
117}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
120 unsigned short family)
121{
122 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
123 int err = 0;
124
125 if (unlikely(afinfo == NULL))
126 return -EAFNOSUPPORT;
127
128 if (likely(afinfo->dst_lookup != NULL))
129 err = afinfo->dst_lookup(dst, fl);
130 else
131 err = -EINVAL;
132 xfrm_policy_put_afinfo(afinfo);
133 return err;
134}
135EXPORT_SYMBOL(xfrm_dst_lookup);
136
137void xfrm_put_type(struct xfrm_type *type)
138{
139 module_put(type->owner);
140}
141
Herbert Xub59f45d2006-05-27 23:05:54 -0700142int xfrm_register_mode(struct xfrm_mode *mode, int family)
143{
144 struct xfrm_policy_afinfo *afinfo;
145 struct xfrm_mode **modemap;
146 int err;
147
148 if (unlikely(mode->encap >= XFRM_MODE_MAX))
149 return -EINVAL;
150
151 afinfo = xfrm_policy_lock_afinfo(family);
152 if (unlikely(afinfo == NULL))
153 return -EAFNOSUPPORT;
154
155 err = -EEXIST;
156 modemap = afinfo->mode_map;
157 if (likely(modemap[mode->encap] == NULL)) {
158 modemap[mode->encap] = mode;
159 err = 0;
160 }
161
162 xfrm_policy_unlock_afinfo(afinfo);
163 return err;
164}
165EXPORT_SYMBOL(xfrm_register_mode);
166
167int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
168{
169 struct xfrm_policy_afinfo *afinfo;
170 struct xfrm_mode **modemap;
171 int err;
172
173 if (unlikely(mode->encap >= XFRM_MODE_MAX))
174 return -EINVAL;
175
176 afinfo = xfrm_policy_lock_afinfo(family);
177 if (unlikely(afinfo == NULL))
178 return -EAFNOSUPPORT;
179
180 err = -ENOENT;
181 modemap = afinfo->mode_map;
182 if (likely(modemap[mode->encap] == mode)) {
183 modemap[mode->encap] = NULL;
184 err = 0;
185 }
186
187 xfrm_policy_unlock_afinfo(afinfo);
188 return err;
189}
190EXPORT_SYMBOL(xfrm_unregister_mode);
191
192struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
193{
194 struct xfrm_policy_afinfo *afinfo;
195 struct xfrm_mode *mode;
196 int modload_attempted = 0;
197
198 if (unlikely(encap >= XFRM_MODE_MAX))
199 return NULL;
200
201retry:
202 afinfo = xfrm_policy_get_afinfo(family);
203 if (unlikely(afinfo == NULL))
204 return NULL;
205
206 mode = afinfo->mode_map[encap];
207 if (unlikely(mode && !try_module_get(mode->owner)))
208 mode = NULL;
209 if (!mode && !modload_attempted) {
210 xfrm_policy_put_afinfo(afinfo);
211 request_module("xfrm-mode-%d-%d", family, encap);
212 modload_attempted = 1;
213 goto retry;
214 }
215
216 xfrm_policy_put_afinfo(afinfo);
217 return mode;
218}
219
220void xfrm_put_mode(struct xfrm_mode *mode)
221{
222 module_put(mode->owner);
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static inline unsigned long make_jiffies(long secs)
226{
227 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
228 return MAX_SCHEDULE_TIMEOUT-1;
229 else
230 return secs*HZ;
231}
232
233static void xfrm_policy_timer(unsigned long data)
234{
235 struct xfrm_policy *xp = (struct xfrm_policy*)data;
236 unsigned long now = (unsigned long)xtime.tv_sec;
237 long next = LONG_MAX;
238 int warn = 0;
239 int dir;
240
241 read_lock(&xp->lock);
242
243 if (xp->dead)
244 goto out;
245
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700246 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 if (xp->lft.hard_add_expires_seconds) {
249 long tmo = xp->lft.hard_add_expires_seconds +
250 xp->curlft.add_time - now;
251 if (tmo <= 0)
252 goto expired;
253 if (tmo < next)
254 next = tmo;
255 }
256 if (xp->lft.hard_use_expires_seconds) {
257 long tmo = xp->lft.hard_use_expires_seconds +
258 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
259 if (tmo <= 0)
260 goto expired;
261 if (tmo < next)
262 next = tmo;
263 }
264 if (xp->lft.soft_add_expires_seconds) {
265 long tmo = xp->lft.soft_add_expires_seconds +
266 xp->curlft.add_time - now;
267 if (tmo <= 0) {
268 warn = 1;
269 tmo = XFRM_KM_TIMEOUT;
270 }
271 if (tmo < next)
272 next = tmo;
273 }
274 if (xp->lft.soft_use_expires_seconds) {
275 long tmo = xp->lft.soft_use_expires_seconds +
276 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
277 if (tmo <= 0) {
278 warn = 1;
279 tmo = XFRM_KM_TIMEOUT;
280 }
281 if (tmo < next)
282 next = tmo;
283 }
284
285 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800286 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (next != LONG_MAX &&
288 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
289 xfrm_pol_hold(xp);
290
291out:
292 read_unlock(&xp->lock);
293 xfrm_pol_put(xp);
294 return;
295
296expired:
297 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700298 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800299 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 xfrm_pol_put(xp);
301}
302
303
304/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
305 * SPD calls.
306 */
307
Al Virodd0fc662005-10-07 07:46:04 +0100308struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 struct xfrm_policy *policy;
311
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700312 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 if (policy) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700315 INIT_HLIST_NODE(&policy->bydst);
316 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700318 atomic_set(&policy->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 init_timer(&policy->timer);
320 policy->timer.data = (unsigned long)policy;
321 policy->timer.function = xfrm_policy_timer;
322 }
323 return policy;
324}
325EXPORT_SYMBOL(xfrm_policy_alloc);
326
327/* Destroy xfrm_policy: descendant resources must be released to this moment. */
328
329void __xfrm_policy_destroy(struct xfrm_policy *policy)
330{
Kris Katterjohn09a62662006-01-08 22:24:28 -0800331 BUG_ON(!policy->dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Kris Katterjohn09a62662006-01-08 22:24:28 -0800333 BUG_ON(policy->bundles);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 if (del_timer(&policy->timer))
336 BUG();
337
Trent Jaegerdf718372005-12-13 23:12:27 -0800338 security_xfrm_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 kfree(policy);
340}
341EXPORT_SYMBOL(__xfrm_policy_destroy);
342
343static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
344{
345 struct dst_entry *dst;
346
347 while ((dst = policy->bundles) != NULL) {
348 policy->bundles = dst->next;
349 dst_free(dst);
350 }
351
352 if (del_timer(&policy->timer))
353 atomic_dec(&policy->refcnt);
354
355 if (atomic_read(&policy->refcnt) > 1)
356 flow_cache_flush();
357
358 xfrm_pol_put(policy);
359}
360
361static void xfrm_policy_gc_task(void *data)
362{
363 struct xfrm_policy *policy;
David S. Miller2518c7c2006-08-24 04:45:07 -0700364 struct hlist_node *entry, *tmp;
365 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 spin_lock_bh(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700368 gc_list.first = xfrm_policy_gc_list.first;
369 INIT_HLIST_HEAD(&xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 spin_unlock_bh(&xfrm_policy_gc_lock);
371
David S. Miller2518c7c2006-08-24 04:45:07 -0700372 hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 xfrm_policy_gc_kill(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376/* Rule must be locked. Release descentant resources, announce
377 * entry dead. The rule must be unlinked from lists to the moment.
378 */
379
380static void xfrm_policy_kill(struct xfrm_policy *policy)
381{
382 int dead;
383
384 write_lock_bh(&policy->lock);
385 dead = policy->dead;
386 policy->dead = 1;
387 write_unlock_bh(&policy->lock);
388
389 if (unlikely(dead)) {
390 WARN_ON(1);
391 return;
392 }
393
394 spin_lock(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700395 hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 spin_unlock(&xfrm_policy_gc_lock);
397
398 schedule_work(&xfrm_policy_gc_work);
399}
400
David S. Miller2518c7c2006-08-24 04:45:07 -0700401struct xfrm_policy_hash {
402 struct hlist_head *table;
403 unsigned int hmask;
404};
405
406static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
407static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
408static struct hlist_head *xfrm_policy_byidx __read_mostly;
409static unsigned int xfrm_idx_hmask __read_mostly;
410static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
411
David S. Miller2518c7c2006-08-24 04:45:07 -0700412static inline unsigned int idx_hash(u32 index)
413{
414 return __idx_hash(index, xfrm_idx_hmask);
415}
416
David S. Miller2518c7c2006-08-24 04:45:07 -0700417static 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
427static 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
David S. Miller2518c7c2006-08-24 04:45:07 -0700435static 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
451static 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
466static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
467{
468 return ((old_hmask + 1) << 1) - 1;
469}
470
471static 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;
David S. Miller44e36b42006-08-24 04:50:50 -0700477 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700478 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
David S. Miller44e36b42006-08-24 04:50:50 -0700493 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700494}
495
496static 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;
David S. Miller44e36b42006-08-24 04:50:50 -0700502 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700503 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
David S. Miller44e36b42006-08-24 04:50:50 -0700518 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700519}
520
521static 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
536static 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
547static DEFINE_MUTEX(hash_resize_mutex);
548
549static 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
566static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize, NULL);
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568/* Generate new index... KAME seems to generate them ordered by cost
569 * of an absolute inpredictability of ordering of rules. This will not pass. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700570static u32 xfrm_gen_index(u8 type, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 static u32 idx_generator;
573
574 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700575 struct hlist_node *entry;
576 struct hlist_head *list;
577 struct xfrm_policy *p;
578 u32 idx;
579 int found;
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 idx = (idx_generator | dir);
582 idx_generator += 8;
583 if (idx == 0)
584 idx = 8;
David S. Miller2518c7c2006-08-24 04:45:07 -0700585 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700593 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return idx;
595 }
596}
597
David S. Miller2518c7c2006-08-24 04:45:07 -0700598static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
614{
David S. Miller2518c7c2006-08-24 04:45:07 -0700615 struct xfrm_policy *pol;
616 struct xfrm_policy *delpol;
617 struct hlist_head *chain;
618 struct hlist_node *entry, *newpos, *last;
David S. Miller9b78a822005-12-22 07:39:48 -0800619 struct dst_entry *gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700622 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) &&
Trent Jaegerdf718372005-12-13 23:12:27 -0800630 xfrm_sec_ctx_match(pol->security, policy->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (excl) {
632 write_unlock_bh(&xfrm_policy_lock);
633 return -EEXIST;
634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 delpol = pol;
636 if (policy->priority > pol->priority)
637 continue;
638 } else if (policy->priority >= pol->priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700639 last = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 continue;
641 }
642 if (!newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700643 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (delpol)
645 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700646 last = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700648 if (!newpos)
649 newpos = last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700651 hlist_add_after(newpos, &policy->bydst);
652 else
653 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 xfrm_pol_hold(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700655 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700657 if (delpol) {
658 hlist_del(&delpol->bydst);
659 hlist_del(&delpol->byidx);
660 xfrm_policy_count[dir]--;
661 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700662 policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700663 hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 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
David S. Miller9b78a822005-12-22 07:39:48 -0800670 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 xfrm_policy_kill(delpol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700672 else if (xfrm_bydst_should_resize(dir, NULL))
673 schedule_work(&xfrm_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800674
675 read_lock_bh(&xfrm_policy_lock);
676 gc_list = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700677 entry = &policy->bydst;
678 hlist_for_each_entry_continue(policy, entry, bydst) {
David S. Miller9b78a822005-12-22 07:39:48 -0800679 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
David S. Miller9b78a822005-12-22 07:39:48 -0800694 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return 0;
704}
705EXPORT_SYMBOL(xfrm_policy_insert);
706
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700707struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
708 struct xfrm_selector *sel,
Trent Jaegerdf718372005-12-13 23:12:27 -0800709 struct xfrm_sec_ctx *ctx, int delete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
David S. Miller2518c7c2006-08-24 04:45:07 -0700711 struct xfrm_policy *pol, *ret;
712 struct hlist_head *chain;
713 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700716 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)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700723 if (delete) {
724 hlist_del(&pol->bydst);
725 hlist_del(&pol->byidx);
726 xfrm_policy_count[dir]--;
727 }
728 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 break;
730 }
731 }
732 write_unlock_bh(&xfrm_policy_lock);
733
David S. Miller2518c7c2006-08-24 04:45:07 -0700734 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700736 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700738 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
Trent Jaegerdf718372005-12-13 23:12:27 -0800740EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700742struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
David S. Miller2518c7c2006-08-24 04:45:07 -0700744 struct xfrm_policy *pol, *ret;
745 struct hlist_head *chain;
746 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700749 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700754 if (delete) {
755 hlist_del(&pol->bydst);
756 hlist_del(&pol->byidx);
757 xfrm_policy_count[dir]--;
758 }
759 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 break;
761 }
762 }
763 write_unlock_bh(&xfrm_policy_lock);
764
David S. Miller2518c7c2006-08-24 04:45:07 -0700765 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700767 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700769 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771EXPORT_SYMBOL(xfrm_policy_byid);
772
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700773void xfrm_policy_flush(u8 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 int dir;
776
777 write_lock_bh(&xfrm_policy_lock);
778 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700779 struct xfrm_policy *pol;
780 struct hlist_node *entry;
781 int i;
782
783 again1:
784 hlist_for_each_entry(pol, entry,
785 &xfrm_policy_inexact[dir], bydst) {
786 if (pol->type != type)
787 continue;
788 hlist_del(&pol->bydst);
789 hlist_del(&pol->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 write_unlock_bh(&xfrm_policy_lock);
791
David S. Miller2518c7c2006-08-24 04:45:07 -0700792 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700795 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700797
798 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
799 again2:
800 hlist_for_each_entry(pol, entry,
801 xfrm_policy_bydst[dir].table + i,
802 bydst) {
803 if (pol->type != type)
804 continue;
805 hlist_del(&pol->bydst);
806 hlist_del(&pol->byidx);
807 write_unlock_bh(&xfrm_policy_lock);
808
809 xfrm_policy_kill(pol);
810
811 write_lock_bh(&xfrm_policy_lock);
812 goto again2;
813 }
814 }
815
816 xfrm_policy_count[dir] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818 atomic_inc(&flow_cache_genid);
819 write_unlock_bh(&xfrm_policy_lock);
820}
821EXPORT_SYMBOL(xfrm_policy_flush);
822
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700823int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 void *data)
825{
David S. Miller2518c7c2006-08-24 04:45:07 -0700826 struct xfrm_policy *pol;
827 struct hlist_node *entry;
828 int dir, count, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700831 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700833 struct hlist_head *table = xfrm_policy_bydst[dir].table;
834 int i;
835
836 hlist_for_each_entry(pol, entry,
837 &xfrm_policy_inexact[dir], bydst) {
838 if (pol->type == type)
839 count++;
840 }
841 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
842 hlist_for_each_entry(pol, entry, table + i, bydst) {
843 if (pol->type == type)
844 count++;
845 }
846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848
849 if (count == 0) {
850 error = -ENOENT;
851 goto out;
852 }
853
854 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700855 struct hlist_head *table = xfrm_policy_bydst[dir].table;
856 int i;
857
858 hlist_for_each_entry(pol, entry,
859 &xfrm_policy_inexact[dir], bydst) {
860 if (pol->type != type)
861 continue;
862 error = func(pol, dir % XFRM_POLICY_MAX, --count, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (error)
864 goto out;
865 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700866 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
867 hlist_for_each_entry(pol, entry, table + i, bydst) {
868 if (pol->type != type)
869 continue;
870 error = func(pol, dir % XFRM_POLICY_MAX, --count, data);
871 if (error)
872 goto out;
873 }
874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700876 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877out:
878 read_unlock_bh(&xfrm_policy_lock);
879 return error;
880}
881EXPORT_SYMBOL(xfrm_policy_walk);
882
883/* Find policy to apply to this flow. */
884
David S. Miller2518c7c2006-08-24 04:45:07 -0700885static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
886 u8 type, u16 family, int dir)
887{
888 struct xfrm_selector *sel = &pol->selector;
889 int match;
890
891 if (pol->family != family ||
892 pol->type != type)
893 return 0;
894
895 match = xfrm_selector_match(sel, fl, family);
896 if (match) {
897 if (!security_xfrm_policy_lookup(pol, fl->secid, dir))
898 return 1;
899 }
900
901 return 0;
902}
903
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700904static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
905 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
David S. Miller2518c7c2006-08-24 04:45:07 -0700907 struct xfrm_policy *pol, *ret;
908 xfrm_address_t *daddr, *saddr;
909 struct hlist_node *entry;
910 struct hlist_head *chain;
911
912 daddr = xfrm_flowi_daddr(fl, family);
913 saddr = xfrm_flowi_saddr(fl, family);
914 if (unlikely(!daddr || !saddr))
915 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700918 chain = policy_hash_direct(daddr, saddr, family, dir);
919 ret = NULL;
920 hlist_for_each_entry(pol, entry, chain, bydst) {
921 if (xfrm_policy_match(pol, fl, type, family, dir)) {
922 xfrm_pol_hold(pol);
923 ret = pol;
924 break;
925 }
926 }
927 if (!ret) {
928 chain = &xfrm_policy_inexact[dir];
929 hlist_for_each_entry(pol, entry, chain, bydst) {
930 if (xfrm_policy_match(pol, fl, type, family, dir)) {
Trent Jaegerdf718372005-12-13 23:12:27 -0800931 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700932 ret = pol;
Trent Jaegerdf718372005-12-13 23:12:27 -0800933 break;
934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936 }
937 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700938
David S. Miller2518c7c2006-08-24 04:45:07 -0700939 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700940}
941
942static void xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
943 void **objp, atomic_t **obj_refp)
944{
945 struct xfrm_policy *pol;
946
947#ifdef CONFIG_XFRM_SUB_POLICY
948 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
949 if (pol)
950 goto end;
951#endif
952 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
953
954#ifdef CONFIG_XFRM_SUB_POLICY
David S. Miller2518c7c2006-08-24 04:45:07 -0700955end:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700956#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if ((*objp = (void *) pol) != NULL)
958 *obj_refp = &pol->refcnt;
959}
960
Trent Jaegerdf718372005-12-13 23:12:27 -0800961static inline int policy_to_flow_dir(int dir)
962{
963 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
964 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
965 XFRM_POLICY_FWD == FLOW_DIR_FWD)
966 return dir;
967 switch (dir) {
968 default:
969 case XFRM_POLICY_IN:
970 return FLOW_DIR_IN;
971 case XFRM_POLICY_OUT:
972 return FLOW_DIR_OUT;
973 case XFRM_POLICY_FWD:
974 return FLOW_DIR_FWD;
975 };
976}
977
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700978static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 struct xfrm_policy *pol;
981
982 read_lock_bh(&xfrm_policy_lock);
983 if ((pol = sk->sk_policy[dir]) != NULL) {
Trent Jaegerdf718372005-12-13 23:12:27 -0800984 int match = xfrm_selector_match(&pol->selector, fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 sk->sk_family);
Trent Jaegerdf718372005-12-13 23:12:27 -0800986 int err = 0;
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (match)
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700989 err = security_xfrm_policy_lookup(pol, fl->secid, policy_to_flow_dir(dir));
Trent Jaegerdf718372005-12-13 23:12:27 -0800990
991 if (match && !err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 xfrm_pol_hold(pol);
993 else
994 pol = NULL;
995 }
996 read_unlock_bh(&xfrm_policy_lock);
997 return pol;
998}
999
1000static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1001{
David S. Miller2518c7c2006-08-24 04:45:07 -07001002 struct hlist_head *chain = policy_hash_bysel(&pol->selector,
1003 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001004
David S. Miller2518c7c2006-08-24 04:45:07 -07001005 hlist_add_head(&pol->bydst, chain);
1006 hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
1007 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001009
1010 if (xfrm_bydst_should_resize(dir, NULL))
1011 schedule_work(&xfrm_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1015 int dir)
1016{
David S. Miller2518c7c2006-08-24 04:45:07 -07001017 if (hlist_unhashed(&pol->bydst))
1018 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
David S. Miller2518c7c2006-08-24 04:45:07 -07001020 hlist_del(&pol->bydst);
1021 hlist_del(&pol->byidx);
1022 xfrm_policy_count[dir]--;
1023
1024 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
Herbert Xu4666faa2005-06-18 22:43:22 -07001027int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029 write_lock_bh(&xfrm_policy_lock);
1030 pol = __xfrm_policy_unlink(pol, dir);
1031 write_unlock_bh(&xfrm_policy_lock);
1032 if (pol) {
1033 if (dir < XFRM_POLICY_MAX)
1034 atomic_inc(&flow_cache_genid);
1035 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001036 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001038 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
David S. Millera70fcb02006-03-20 19:18:52 -08001040EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1043{
1044 struct xfrm_policy *old_pol;
1045
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001046#ifdef CONFIG_XFRM_SUB_POLICY
1047 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1048 return -EINVAL;
1049#endif
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 write_lock_bh(&xfrm_policy_lock);
1052 old_pol = sk->sk_policy[dir];
1053 sk->sk_policy[dir] = pol;
1054 if (pol) {
1055 pol->curlft.add_time = (unsigned long)xtime.tv_sec;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001056 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1058 }
1059 if (old_pol)
1060 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1061 write_unlock_bh(&xfrm_policy_lock);
1062
1063 if (old_pol) {
1064 xfrm_policy_kill(old_pol);
1065 }
1066 return 0;
1067}
1068
1069static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1070{
1071 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
1072
1073 if (newp) {
1074 newp->selector = old->selector;
Trent Jaegerdf718372005-12-13 23:12:27 -08001075 if (security_xfrm_policy_clone(old, newp)) {
1076 kfree(newp);
1077 return NULL; /* ENOMEM */
1078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 newp->lft = old->lft;
1080 newp->curlft = old->curlft;
1081 newp->action = old->action;
1082 newp->flags = old->flags;
1083 newp->xfrm_nr = old->xfrm_nr;
1084 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001085 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 memcpy(newp->xfrm_vec, old->xfrm_vec,
1087 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1088 write_lock_bh(&xfrm_policy_lock);
1089 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1090 write_unlock_bh(&xfrm_policy_lock);
1091 xfrm_pol_put(newp);
1092 }
1093 return newp;
1094}
1095
1096int __xfrm_sk_clone_policy(struct sock *sk)
1097{
1098 struct xfrm_policy *p0 = sk->sk_policy[0],
1099 *p1 = sk->sk_policy[1];
1100
1101 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1102 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1103 return -ENOMEM;
1104 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1105 return -ENOMEM;
1106 return 0;
1107}
1108
1109/* Resolve list of templates for the flow, given policy. */
1110
1111static int
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001112xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1113 struct xfrm_state **xfrm,
1114 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
1116 int nx;
1117 int i, error;
1118 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1119 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
1120
1121 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1122 struct xfrm_state *x;
1123 xfrm_address_t *remote = daddr;
1124 xfrm_address_t *local = saddr;
1125 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1126
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001127 if (tmpl->mode == XFRM_MODE_TUNNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 remote = &tmpl->id.daddr;
1129 local = &tmpl->saddr;
1130 }
1131
1132 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1133
1134 if (x && x->km.state == XFRM_STATE_VALID) {
1135 xfrm[nx++] = x;
1136 daddr = remote;
1137 saddr = local;
1138 continue;
1139 }
1140 if (x) {
1141 error = (x->km.state == XFRM_STATE_ERROR ?
1142 -EINVAL : -EAGAIN);
1143 xfrm_state_put(x);
1144 }
1145
1146 if (!tmpl->optional)
1147 goto fail;
1148 }
1149 return nx;
1150
1151fail:
1152 for (nx--; nx>=0; nx--)
1153 xfrm_state_put(xfrm[nx]);
1154 return error;
1155}
1156
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001157static int
1158xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1159 struct xfrm_state **xfrm,
1160 unsigned short family)
1161{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001162 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1163 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001164 int cnx = 0;
1165 int error;
1166 int ret;
1167 int i;
1168
1169 for (i = 0; i < npols; i++) {
1170 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1171 error = -ENOBUFS;
1172 goto fail;
1173 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001174
1175 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001176 if (ret < 0) {
1177 error = ret;
1178 goto fail;
1179 } else
1180 cnx += ret;
1181 }
1182
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001183 /* found states are sorted for outbound processing */
1184 if (npols > 1)
1185 xfrm_state_sort(xfrm, tpp, cnx, family);
1186
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001187 return cnx;
1188
1189 fail:
1190 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001191 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001192 return error;
1193
1194}
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196/* Check that the bundle accepts the flow and its components are
1197 * still valid.
1198 */
1199
1200static struct dst_entry *
1201xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1202{
1203 struct dst_entry *x;
1204 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1205 if (unlikely(afinfo == NULL))
1206 return ERR_PTR(-EINVAL);
1207 x = afinfo->find_bundle(fl, policy);
1208 xfrm_policy_put_afinfo(afinfo);
1209 return x;
1210}
1211
1212/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1213 * all the metrics... Shortly, bundle a bundle.
1214 */
1215
1216static int
1217xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
1218 struct flowi *fl, struct dst_entry **dst_p,
1219 unsigned short family)
1220{
1221 int err;
1222 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1223 if (unlikely(afinfo == NULL))
1224 return -EINVAL;
1225 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
1226 xfrm_policy_put_afinfo(afinfo);
1227 return err;
1228}
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231static int stale_bundle(struct dst_entry *dst);
1232
1233/* Main function: finds/creates a bundle for given flow.
1234 *
1235 * At the moment we eat a raw IP route. Mostly to speed up lookups
1236 * on interfaces with disabled IPsec.
1237 */
1238int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1239 struct sock *sk, int flags)
1240{
1241 struct xfrm_policy *policy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001242 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1243 int npols;
1244 int pol_dead;
1245 int xfrm_nr;
1246 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1248 struct dst_entry *dst, *dst_orig = *dst_p;
1249 int nx = 0;
1250 int err;
1251 u32 genid;
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001252 u16 family;
Trent Jaegerdf718372005-12-13 23:12:27 -08001253 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255restart:
1256 genid = atomic_read(&flow_cache_genid);
1257 policy = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001258 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1259 pols[pi] = NULL;
1260 npols = 0;
1261 pol_dead = 0;
1262 xfrm_nr = 0;
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (sk && sk->sk_policy[1])
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001265 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 if (!policy) {
1268 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07001269 if ((dst_orig->flags & DST_NOXFRM) ||
1270 !xfrm_policy_count[XFRM_POLICY_OUT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return 0;
1272
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001273 policy = flow_cache_lookup(fl, dst_orig->ops->family,
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001274 dir, xfrm_policy_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
1276
1277 if (!policy)
1278 return 0;
1279
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001280 family = dst_orig->ops->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 policy->curlft.use_time = (unsigned long)xtime.tv_sec;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001282 pols[0] = policy;
1283 npols ++;
1284 xfrm_nr += pols[0]->xfrm_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 switch (policy->action) {
1287 case XFRM_POLICY_BLOCK:
1288 /* Prohibit the flow */
Patrick McHardye1044112005-09-08 15:11:55 -07001289 err = -EPERM;
1290 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 case XFRM_POLICY_ALLOW:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001293#ifndef CONFIG_XFRM_SUB_POLICY
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (policy->xfrm_nr == 0) {
1295 /* Flow passes not transformed. */
1296 xfrm_pol_put(policy);
1297 return 0;
1298 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001299#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 /* Try to find matching bundle.
1302 *
1303 * LATER: help from flow cache. It is optional, this
1304 * is required only for output policy.
1305 */
1306 dst = xfrm_find_bundle(fl, policy, family);
1307 if (IS_ERR(dst)) {
Patrick McHardye1044112005-09-08 15:11:55 -07001308 err = PTR_ERR(dst);
1309 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311
1312 if (dst)
1313 break;
1314
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001315#ifdef CONFIG_XFRM_SUB_POLICY
1316 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1317 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1318 fl, family,
1319 XFRM_POLICY_OUT);
1320 if (pols[1]) {
1321 if (pols[1]->action == XFRM_POLICY_BLOCK) {
1322 err = -EPERM;
1323 goto error;
1324 }
1325 npols ++;
1326 xfrm_nr += pols[1]->xfrm_nr;
1327 }
1328 }
1329
1330 /*
1331 * Because neither flowi nor bundle information knows about
1332 * transformation template size. On more than one policy usage
1333 * we can realize whether all of them is bypass or not after
1334 * they are searched. See above not-transformed bypass
1335 * is surrounded by non-sub policy configuration, too.
1336 */
1337 if (xfrm_nr == 0) {
1338 /* Flow passes not transformed. */
1339 xfrm_pols_put(pols, npols);
1340 return 0;
1341 }
1342
1343#endif
1344 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 if (unlikely(nx<0)) {
1347 err = nx;
1348 if (err == -EAGAIN && flags) {
1349 DECLARE_WAITQUEUE(wait, current);
1350
1351 add_wait_queue(&km_waitq, &wait);
1352 set_current_state(TASK_INTERRUPTIBLE);
1353 schedule();
1354 set_current_state(TASK_RUNNING);
1355 remove_wait_queue(&km_waitq, &wait);
1356
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001357 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 if (nx == -EAGAIN && signal_pending(current)) {
1360 err = -ERESTART;
1361 goto error;
1362 }
1363 if (nx == -EAGAIN ||
1364 genid != atomic_read(&flow_cache_genid)) {
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001365 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 goto restart;
1367 }
1368 err = nx;
1369 }
1370 if (err < 0)
1371 goto error;
1372 }
1373 if (nx == 0) {
1374 /* Flow passes not transformed. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001375 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return 0;
1377 }
1378
1379 dst = dst_orig;
1380 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
1381
1382 if (unlikely(err)) {
1383 int i;
1384 for (i=0; i<nx; i++)
1385 xfrm_state_put(xfrm[i]);
1386 goto error;
1387 }
1388
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001389 for (pi = 0; pi < npols; pi++) {
1390 read_lock_bh(&pols[pi]->lock);
1391 pol_dead |= pols[pi]->dead;
1392 read_unlock_bh(&pols[pi]->lock);
1393 }
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 write_lock_bh(&policy->lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001396 if (unlikely(pol_dead || stale_bundle(dst))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 /* Wow! While we worked on resolving, this
1398 * policy has gone. Retry. It is not paranoia,
1399 * we just cannot enlist new bundle to dead object.
1400 * We can't enlist stable bundles either.
1401 */
1402 write_unlock_bh(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (dst)
1404 dst_free(dst);
Herbert Xu00de6512006-02-13 16:01:27 -08001405
1406 err = -EHOSTUNREACH;
1407 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409 dst->next = policy->bundles;
1410 policy->bundles = dst;
1411 dst_hold(dst);
1412 write_unlock_bh(&policy->lock);
1413 }
1414 *dst_p = dst;
1415 dst_release(dst_orig);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001416 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return 0;
1418
1419error:
1420 dst_release(dst_orig);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001421 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 *dst_p = NULL;
1423 return err;
1424}
1425EXPORT_SYMBOL(xfrm_lookup);
1426
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001427static inline int
1428xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1429{
1430 struct xfrm_state *x;
1431 int err;
1432
1433 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1434 return 0;
1435 x = skb->sp->xvec[idx];
1436 if (!x->type->reject)
1437 return 0;
1438 xfrm_state_hold(x);
1439 err = x->type->reject(x, skb, fl);
1440 xfrm_state_put(x);
1441 return err;
1442}
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444/* When skb is transformed back to its "native" form, we have to
1445 * check policy restrictions. At the moment we make this in maximally
1446 * stupid way. Shame on me. :-) Of course, connected sockets must
1447 * have policy cached at them.
1448 */
1449
1450static inline int
1451xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
1452 unsigned short family)
1453{
1454 if (xfrm_state_kern(x))
1455 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family);
1456 return x->id.proto == tmpl->id.proto &&
1457 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1458 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1459 x->props.mode == tmpl->mode &&
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07001460 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1461 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001462 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1463 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001466/*
1467 * 0 or more than 0 is returned when validation is succeeded (either bypass
1468 * because of optional transport mode, or next index of the mathced secpath
1469 * state with the template.
1470 * -1 is returned when no matching template is found.
1471 * Otherwise "-2 - errored_index" is returned.
1472 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473static inline int
1474xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1475 unsigned short family)
1476{
1477 int idx = start;
1478
1479 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001480 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return start;
1482 } else
1483 start = -1;
1484 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001485 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001487 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1488 if (start == -1)
1489 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
1493 return start;
1494}
1495
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001496int
1497xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001500 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 if (unlikely(afinfo == NULL))
1503 return -EAFNOSUPPORT;
1504
1505 afinfo->decode_session(skb, fl);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07001506 err = security_xfrm_decode_session(skb, &fl->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001508 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001510EXPORT_SYMBOL(xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001512static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
1514 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001515 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
1516 if (idxp)
1517 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
1521
1522 return 0;
1523}
1524
1525int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
1526 unsigned short family)
1527{
1528 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001529 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1530 int npols = 0;
1531 int xfrm_nr;
1532 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 struct flowi fl;
Trent Jaegerdf718372005-12-13 23:12:27 -08001534 u8 fl_dir = policy_to_flow_dir(dir);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001535 int xerr_idx = -1;
1536 int *xerr_idxp = &xerr_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001538 if (xfrm_decode_session(skb, &fl, family) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return 0;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08001540 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 /* First, check used SA against their selectors. */
1543 if (skb->sp) {
1544 int i;
1545
1546 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001547 struct xfrm_state *x = skb->sp->xvec[i];
1548 if (!xfrm_selector_match(&x->sel, &fl, family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
1551 }
1552
1553 pol = NULL;
1554 if (sk && sk->sk_policy[dir])
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001555 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 if (!pol)
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001558 pol = flow_cache_lookup(&fl, family, fl_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 xfrm_policy_lookup);
1560
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001561 if (!pol) {
1562 if (skb->sp && secpath_has_nontransport(skb->sp, 0, xerr_idxp)) {
1563 xfrm_secpath_reject(xerr_idx, skb, &fl);
1564 return 0;
1565 }
1566 return 1;
1567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 pol->curlft.use_time = (unsigned long)xtime.tv_sec;
1570
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001571 pols[0] = pol;
1572 npols ++;
1573#ifdef CONFIG_XFRM_SUB_POLICY
1574 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1575 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1576 &fl, family,
1577 XFRM_POLICY_IN);
1578 if (pols[1]) {
1579 pols[1]->curlft.use_time = (unsigned long)xtime.tv_sec;
1580 npols ++;
1581 }
1582 }
1583#endif
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (pol->action == XFRM_POLICY_ALLOW) {
1586 struct sec_path *sp;
1587 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001588 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001589 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001590 struct xfrm_tmpl **tpp = tp;
1591 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 int i, k;
1593
1594 if ((sp = skb->sp) == NULL)
1595 sp = &dummy;
1596
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001597 for (pi = 0; pi < npols; pi++) {
1598 if (pols[pi] != pol &&
1599 pols[pi]->action != XFRM_POLICY_ALLOW)
1600 goto reject;
1601 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
1602 goto reject_error;
1603 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1604 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1605 }
1606 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001607 if (npols > 1) {
1608 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1609 tpp = stp;
1610 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 /* For each tunnel xfrm, find the first matching tmpl.
1613 * For each tmpl before that, find corresponding xfrm.
1614 * Order is _important_. Later we will implement
1615 * some barriers, but at the moment barriers
1616 * are implied between each two transformations.
1617 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001618 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1619 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001620 if (k < 0) {
1621 if (k < -1 && xerr_idxp)
1622 *xerr_idxp = -(2+k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
1626
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001627 if (secpath_has_nontransport(sp, k, xerr_idxp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 goto reject;
1629
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001630 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return 1;
1632 }
1633
1634reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001635 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001636reject_error:
1637 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return 0;
1639}
1640EXPORT_SYMBOL(__xfrm_policy_check);
1641
1642int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1643{
1644 struct flowi fl;
1645
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001646 if (xfrm_decode_session(skb, &fl, family) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 return 0;
1648
1649 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1650}
1651EXPORT_SYMBOL(__xfrm_route_forward);
1652
David S. Millerd49c73c2006-08-13 18:55:53 -07001653/* Optimize later using cookies and generation ids. */
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1656{
David S. Millerd49c73c2006-08-13 18:55:53 -07001657 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1658 * to "-1" to force all XFRM destinations to get validated by
1659 * dst_ops->check on every use. We do this because when a
1660 * normal route referenced by an XFRM dst is obsoleted we do
1661 * not go looking around for all parent referencing XFRM dsts
1662 * so that we can invalidate them. It is just too much work.
1663 * Instead we make the checks here on every use. For example:
1664 *
1665 * XFRM dst A --> IPv4 dst X
1666 *
1667 * X is the "xdst->route" of A (X is also the "dst->path" of A
1668 * in this example). If X is marked obsolete, "A" will not
1669 * notice. That's what we are validating here via the
1670 * stale_bundle() check.
1671 *
1672 * When a policy's bundle is pruned, we dst_free() the XFRM
1673 * dst which causes it's ->obsolete field to be set to a
1674 * positive non-zero integer. If an XFRM dst has been pruned
1675 * like this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08001676 */
David S. Millerd49c73c2006-08-13 18:55:53 -07001677 if (dst->obsolete < 0 && !stale_bundle(dst))
1678 return dst;
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return NULL;
1681}
1682
1683static int stale_bundle(struct dst_entry *dst)
1684{
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07001685 return !xfrm_bundle_ok((struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
Herbert Xuaabc9762005-05-03 16:27:10 -07001688void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
1691 dst->dev = &loopback_dev;
1692 dev_hold(&loopback_dev);
1693 dev_put(dev);
1694 }
1695}
Herbert Xuaabc9762005-05-03 16:27:10 -07001696EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698static void xfrm_link_failure(struct sk_buff *skb)
1699{
1700 /* Impossible. Such dst must be popped before reaches point of failure. */
1701 return;
1702}
1703
1704static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1705{
1706 if (dst) {
1707 if (dst->obsolete) {
1708 dst_release(dst);
1709 dst = NULL;
1710 }
1711 }
1712 return dst;
1713}
1714
David S. Miller2518c7c2006-08-24 04:45:07 -07001715static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
1716{
1717 struct dst_entry *dst, **dstp;
1718
1719 write_lock(&pol->lock);
1720 dstp = &pol->bundles;
1721 while ((dst=*dstp) != NULL) {
1722 if (func(dst)) {
1723 *dstp = dst->next;
1724 dst->next = *gc_list_p;
1725 *gc_list_p = dst;
1726 } else {
1727 dstp = &dst->next;
1728 }
1729 }
1730 write_unlock(&pol->lock);
1731}
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1734{
David S. Miller2518c7c2006-08-24 04:45:07 -07001735 struct dst_entry *gc_list = NULL;
1736 int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07001739 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
1740 struct xfrm_policy *pol;
1741 struct hlist_node *entry;
1742 struct hlist_head *table;
1743 int i;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001744
David S. Miller2518c7c2006-08-24 04:45:07 -07001745 hlist_for_each_entry(pol, entry,
1746 &xfrm_policy_inexact[dir], bydst)
1747 prune_one_bundle(pol, func, &gc_list);
1748
1749 table = xfrm_policy_bydst[dir].table;
1750 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
1751 hlist_for_each_entry(pol, entry, table + i, bydst)
1752 prune_one_bundle(pol, func, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754 }
1755 read_unlock_bh(&xfrm_policy_lock);
1756
1757 while (gc_list) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001758 struct dst_entry *dst = gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 gc_list = dst->next;
1760 dst_free(dst);
1761 }
1762}
1763
1764static int unused_bundle(struct dst_entry *dst)
1765{
1766 return !atomic_read(&dst->__refcnt);
1767}
1768
1769static void __xfrm_garbage_collect(void)
1770{
1771 xfrm_prune_bundles(unused_bundle);
1772}
1773
David S. Miller1c095392006-08-24 03:30:28 -07001774static int xfrm_flush_bundles(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
1776 xfrm_prune_bundles(stale_bundle);
1777 return 0;
1778}
1779
1780void xfrm_init_pmtu(struct dst_entry *dst)
1781{
1782 do {
1783 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1784 u32 pmtu, route_mtu_cached;
1785
1786 pmtu = dst_mtu(dst->child);
1787 xdst->child_mtu_cached = pmtu;
1788
1789 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
1790
1791 route_mtu_cached = dst_mtu(xdst->route);
1792 xdst->route_mtu_cached = route_mtu_cached;
1793
1794 if (pmtu > route_mtu_cached)
1795 pmtu = route_mtu_cached;
1796
1797 dst->metrics[RTAX_MTU-1] = pmtu;
1798 } while ((dst = dst->next));
1799}
1800
1801EXPORT_SYMBOL(xfrm_init_pmtu);
1802
1803/* Check that the bundle accepts the flow and its components are
1804 * still valid.
1805 */
1806
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07001807int xfrm_bundle_ok(struct xfrm_dst *first, struct flowi *fl, int family, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
1809 struct dst_entry *dst = &first->u.dst;
1810 struct xfrm_dst *last;
1811 u32 mtu;
1812
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07001813 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 (dst->dev && !netif_running(dst->dev)))
1815 return 0;
1816
1817 last = NULL;
1818
1819 do {
1820 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1821
1822 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
1823 return 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001824 if (fl && !security_xfrm_flow_state_match(fl, dst->xfrm))
1825 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (dst->xfrm->km.state != XFRM_STATE_VALID)
1827 return 0;
David S. Miller9d4a7062006-08-24 03:18:09 -07001828 if (xdst->genid != dst->xfrm->genid)
1829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07001831 if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
1832 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
1833 return 0;
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 mtu = dst_mtu(dst->child);
1836 if (xdst->child_mtu_cached != mtu) {
1837 last = xdst;
1838 xdst->child_mtu_cached = mtu;
1839 }
1840
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07001841 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return 0;
1843 mtu = dst_mtu(xdst->route);
1844 if (xdst->route_mtu_cached != mtu) {
1845 last = xdst;
1846 xdst->route_mtu_cached = mtu;
1847 }
1848
1849 dst = dst->child;
1850 } while (dst->xfrm);
1851
1852 if (likely(!last))
1853 return 1;
1854
1855 mtu = last->child_mtu_cached;
1856 for (;;) {
1857 dst = &last->u.dst;
1858
1859 mtu = xfrm_state_mtu(dst->xfrm, mtu);
1860 if (mtu > last->route_mtu_cached)
1861 mtu = last->route_mtu_cached;
1862 dst->metrics[RTAX_MTU-1] = mtu;
1863
1864 if (last == first)
1865 break;
1866
1867 last = last->u.next;
1868 last->child_mtu_cached = mtu;
1869 }
1870
1871 return 1;
1872}
1873
1874EXPORT_SYMBOL(xfrm_bundle_ok);
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
1877{
1878 int err = 0;
1879 if (unlikely(afinfo == NULL))
1880 return -EINVAL;
1881 if (unlikely(afinfo->family >= NPROTO))
1882 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07001883 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
1885 err = -ENOBUFS;
1886 else {
1887 struct dst_ops *dst_ops = afinfo->dst_ops;
1888 if (likely(dst_ops->kmem_cachep == NULL))
1889 dst_ops->kmem_cachep = xfrm_dst_cache;
1890 if (likely(dst_ops->check == NULL))
1891 dst_ops->check = xfrm_dst_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (likely(dst_ops->negative_advice == NULL))
1893 dst_ops->negative_advice = xfrm_negative_advice;
1894 if (likely(dst_ops->link_failure == NULL))
1895 dst_ops->link_failure = xfrm_link_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (likely(afinfo->garbage_collect == NULL))
1897 afinfo->garbage_collect = __xfrm_garbage_collect;
1898 xfrm_policy_afinfo[afinfo->family] = afinfo;
1899 }
Ingo Molnare959d812006-04-28 15:32:29 -07001900 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return err;
1902}
1903EXPORT_SYMBOL(xfrm_policy_register_afinfo);
1904
1905int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
1906{
1907 int err = 0;
1908 if (unlikely(afinfo == NULL))
1909 return -EINVAL;
1910 if (unlikely(afinfo->family >= NPROTO))
1911 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07001912 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
1914 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
1915 err = -EINVAL;
1916 else {
1917 struct dst_ops *dst_ops = afinfo->dst_ops;
1918 xfrm_policy_afinfo[afinfo->family] = NULL;
1919 dst_ops->kmem_cachep = NULL;
1920 dst_ops->check = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 dst_ops->negative_advice = NULL;
1922 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 afinfo->garbage_collect = NULL;
1924 }
1925 }
Ingo Molnare959d812006-04-28 15:32:29 -07001926 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return err;
1928}
1929EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
1930
1931static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
1932{
1933 struct xfrm_policy_afinfo *afinfo;
1934 if (unlikely(family >= NPROTO))
1935 return NULL;
1936 read_lock(&xfrm_policy_afinfo_lock);
1937 afinfo = xfrm_policy_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07001938 if (unlikely(!afinfo))
1939 read_unlock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return afinfo;
1941}
1942
1943static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
1944{
Herbert Xu546be242006-05-27 23:03:58 -07001945 read_unlock(&xfrm_policy_afinfo_lock);
1946}
1947
1948static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
1949{
1950 struct xfrm_policy_afinfo *afinfo;
1951 if (unlikely(family >= NPROTO))
1952 return NULL;
1953 write_lock_bh(&xfrm_policy_afinfo_lock);
1954 afinfo = xfrm_policy_afinfo[family];
1955 if (unlikely(!afinfo))
1956 write_unlock_bh(&xfrm_policy_afinfo_lock);
1957 return afinfo;
1958}
1959
1960static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
1961{
1962 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
1965static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
1966{
1967 switch (event) {
1968 case NETDEV_DOWN:
1969 xfrm_flush_bundles();
1970 }
1971 return NOTIFY_DONE;
1972}
1973
1974static struct notifier_block xfrm_dev_notifier = {
1975 xfrm_dev_event,
1976 NULL,
1977 0
1978};
1979
1980static void __init xfrm_policy_init(void)
1981{
David S. Miller2518c7c2006-08-24 04:45:07 -07001982 unsigned int hmask, sz;
1983 int dir;
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
1986 sizeof(struct xfrm_dst),
1987 0, SLAB_HWCACHE_ALIGN,
1988 NULL, NULL);
1989 if (!xfrm_dst_cache)
1990 panic("XFRM: failed to allocate xfrm_dst_cache\n");
1991
David S. Miller2518c7c2006-08-24 04:45:07 -07001992 hmask = 8 - 1;
1993 sz = (hmask+1) * sizeof(struct hlist_head);
1994
David S. Miller44e36b42006-08-24 04:50:50 -07001995 xfrm_policy_byidx = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07001996 xfrm_idx_hmask = hmask;
1997 if (!xfrm_policy_byidx)
1998 panic("XFRM: failed to allocate byidx hash\n");
1999
2000 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2001 struct xfrm_policy_hash *htab;
2002
2003 INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
2004
2005 htab = &xfrm_policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002006 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002007 htab->hmask = hmask;
2008 if (!htab->table)
2009 panic("XFRM: failed to allocate bydst hash\n");
2010 }
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task, NULL);
2013 register_netdevice_notifier(&xfrm_dev_notifier);
2014}
2015
2016void __init xfrm_init(void)
2017{
2018 xfrm_state_init();
2019 xfrm_policy_init();
2020 xfrm_input_init();
2021}
2022