blob: 4f04222698d9032c0a5196a3ffaeb55cc801caa0 [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
Andrew Morton77681022006-11-08 22:46:26 -080053static inline int
54__xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
55{
56 return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
57 addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
58 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
59 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
60 (fl->proto == sel->proto || !sel->proto) &&
61 (fl->oif == sel->ifindex || !sel->ifindex);
62}
63
64static inline int
65__xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
66{
67 return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
68 addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
69 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
70 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
71 (fl->proto == sel->proto || !sel->proto) &&
72 (fl->oif == sel->ifindex || !sel->ifindex);
73}
74
75int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
76 unsigned short family)
77{
78 switch (family) {
79 case AF_INET:
80 return __xfrm4_selector_match(sel, fl);
81 case AF_INET6:
82 return __xfrm6_selector_match(sel, fl);
83 }
84 return 0;
85}
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087int xfrm_register_type(struct xfrm_type *type, unsigned short family)
88{
Herbert Xu546be242006-05-27 23:03:58 -070089 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
90 struct xfrm_type **typemap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 int err = 0;
92
93 if (unlikely(afinfo == NULL))
94 return -EAFNOSUPPORT;
95 typemap = afinfo->type_map;
96
Herbert Xu546be242006-05-27 23:03:58 -070097 if (likely(typemap[type->proto] == NULL))
98 typemap[type->proto] = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 else
100 err = -EEXIST;
Herbert Xu546be242006-05-27 23:03:58 -0700101 xfrm_policy_unlock_afinfo(afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return err;
103}
104EXPORT_SYMBOL(xfrm_register_type);
105
106int xfrm_unregister_type(struct xfrm_type *type, unsigned short family)
107{
Herbert Xu546be242006-05-27 23:03:58 -0700108 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
109 struct xfrm_type **typemap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 int err = 0;
111
112 if (unlikely(afinfo == NULL))
113 return -EAFNOSUPPORT;
114 typemap = afinfo->type_map;
115
Herbert Xu546be242006-05-27 23:03:58 -0700116 if (unlikely(typemap[type->proto] != type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 err = -ENOENT;
118 else
Herbert Xu546be242006-05-27 23:03:58 -0700119 typemap[type->proto] = NULL;
120 xfrm_policy_unlock_afinfo(afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return err;
122}
123EXPORT_SYMBOL(xfrm_unregister_type);
124
125struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
126{
127 struct xfrm_policy_afinfo *afinfo;
Herbert Xu546be242006-05-27 23:03:58 -0700128 struct xfrm_type **typemap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct xfrm_type *type;
130 int modload_attempted = 0;
131
132retry:
133 afinfo = xfrm_policy_get_afinfo(family);
134 if (unlikely(afinfo == NULL))
135 return NULL;
136 typemap = afinfo->type_map;
137
Herbert Xu546be242006-05-27 23:03:58 -0700138 type = typemap[proto];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (unlikely(type && !try_module_get(type->owner)))
140 type = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (!type && !modload_attempted) {
142 xfrm_policy_put_afinfo(afinfo);
143 request_module("xfrm-type-%d-%d",
144 (int) family, (int) proto);
145 modload_attempted = 1;
146 goto retry;
147 }
148
149 xfrm_policy_put_afinfo(afinfo);
150 return type;
151}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
154 unsigned short family)
155{
156 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
157 int err = 0;
158
159 if (unlikely(afinfo == NULL))
160 return -EAFNOSUPPORT;
161
162 if (likely(afinfo->dst_lookup != NULL))
163 err = afinfo->dst_lookup(dst, fl);
164 else
165 err = -EINVAL;
166 xfrm_policy_put_afinfo(afinfo);
167 return err;
168}
169EXPORT_SYMBOL(xfrm_dst_lookup);
170
171void xfrm_put_type(struct xfrm_type *type)
172{
173 module_put(type->owner);
174}
175
Herbert Xub59f45d2006-05-27 23:05:54 -0700176int xfrm_register_mode(struct xfrm_mode *mode, int family)
177{
178 struct xfrm_policy_afinfo *afinfo;
179 struct xfrm_mode **modemap;
180 int err;
181
182 if (unlikely(mode->encap >= XFRM_MODE_MAX))
183 return -EINVAL;
184
185 afinfo = xfrm_policy_lock_afinfo(family);
186 if (unlikely(afinfo == NULL))
187 return -EAFNOSUPPORT;
188
189 err = -EEXIST;
190 modemap = afinfo->mode_map;
191 if (likely(modemap[mode->encap] == NULL)) {
192 modemap[mode->encap] = mode;
193 err = 0;
194 }
195
196 xfrm_policy_unlock_afinfo(afinfo);
197 return err;
198}
199EXPORT_SYMBOL(xfrm_register_mode);
200
201int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
202{
203 struct xfrm_policy_afinfo *afinfo;
204 struct xfrm_mode **modemap;
205 int err;
206
207 if (unlikely(mode->encap >= XFRM_MODE_MAX))
208 return -EINVAL;
209
210 afinfo = xfrm_policy_lock_afinfo(family);
211 if (unlikely(afinfo == NULL))
212 return -EAFNOSUPPORT;
213
214 err = -ENOENT;
215 modemap = afinfo->mode_map;
216 if (likely(modemap[mode->encap] == mode)) {
217 modemap[mode->encap] = NULL;
218 err = 0;
219 }
220
221 xfrm_policy_unlock_afinfo(afinfo);
222 return err;
223}
224EXPORT_SYMBOL(xfrm_unregister_mode);
225
226struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
227{
228 struct xfrm_policy_afinfo *afinfo;
229 struct xfrm_mode *mode;
230 int modload_attempted = 0;
231
232 if (unlikely(encap >= XFRM_MODE_MAX))
233 return NULL;
234
235retry:
236 afinfo = xfrm_policy_get_afinfo(family);
237 if (unlikely(afinfo == NULL))
238 return NULL;
239
240 mode = afinfo->mode_map[encap];
241 if (unlikely(mode && !try_module_get(mode->owner)))
242 mode = NULL;
243 if (!mode && !modload_attempted) {
244 xfrm_policy_put_afinfo(afinfo);
245 request_module("xfrm-mode-%d-%d", family, encap);
246 modload_attempted = 1;
247 goto retry;
248 }
249
250 xfrm_policy_put_afinfo(afinfo);
251 return mode;
252}
253
254void xfrm_put_mode(struct xfrm_mode *mode)
255{
256 module_put(mode->owner);
257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259static inline unsigned long make_jiffies(long secs)
260{
261 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
262 return MAX_SCHEDULE_TIMEOUT-1;
263 else
264 return secs*HZ;
265}
266
267static void xfrm_policy_timer(unsigned long data)
268{
269 struct xfrm_policy *xp = (struct xfrm_policy*)data;
270 unsigned long now = (unsigned long)xtime.tv_sec;
271 long next = LONG_MAX;
272 int warn = 0;
273 int dir;
274
275 read_lock(&xp->lock);
276
277 if (xp->dead)
278 goto out;
279
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700280 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 if (xp->lft.hard_add_expires_seconds) {
283 long tmo = xp->lft.hard_add_expires_seconds +
284 xp->curlft.add_time - now;
285 if (tmo <= 0)
286 goto expired;
287 if (tmo < next)
288 next = tmo;
289 }
290 if (xp->lft.hard_use_expires_seconds) {
291 long tmo = xp->lft.hard_use_expires_seconds +
292 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
293 if (tmo <= 0)
294 goto expired;
295 if (tmo < next)
296 next = tmo;
297 }
298 if (xp->lft.soft_add_expires_seconds) {
299 long tmo = xp->lft.soft_add_expires_seconds +
300 xp->curlft.add_time - now;
301 if (tmo <= 0) {
302 warn = 1;
303 tmo = XFRM_KM_TIMEOUT;
304 }
305 if (tmo < next)
306 next = tmo;
307 }
308 if (xp->lft.soft_use_expires_seconds) {
309 long tmo = xp->lft.soft_use_expires_seconds +
310 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
311 if (tmo <= 0) {
312 warn = 1;
313 tmo = XFRM_KM_TIMEOUT;
314 }
315 if (tmo < next)
316 next = tmo;
317 }
318
319 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800320 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (next != LONG_MAX &&
322 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
323 xfrm_pol_hold(xp);
324
325out:
326 read_unlock(&xp->lock);
327 xfrm_pol_put(xp);
328 return;
329
330expired:
331 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700332 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800333 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 xfrm_pol_put(xp);
335}
336
337
338/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
339 * SPD calls.
340 */
341
Al Virodd0fc662005-10-07 07:46:04 +0100342struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 struct xfrm_policy *policy;
345
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700346 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 if (policy) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700349 INIT_HLIST_NODE(&policy->bydst);
350 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700352 atomic_set(&policy->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 init_timer(&policy->timer);
354 policy->timer.data = (unsigned long)policy;
355 policy->timer.function = xfrm_policy_timer;
356 }
357 return policy;
358}
359EXPORT_SYMBOL(xfrm_policy_alloc);
360
361/* Destroy xfrm_policy: descendant resources must be released to this moment. */
362
363void __xfrm_policy_destroy(struct xfrm_policy *policy)
364{
Kris Katterjohn09a62662006-01-08 22:24:28 -0800365 BUG_ON(!policy->dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Kris Katterjohn09a62662006-01-08 22:24:28 -0800367 BUG_ON(policy->bundles);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 if (del_timer(&policy->timer))
370 BUG();
371
Trent Jaegerdf718372005-12-13 23:12:27 -0800372 security_xfrm_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 kfree(policy);
374}
375EXPORT_SYMBOL(__xfrm_policy_destroy);
376
377static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
378{
379 struct dst_entry *dst;
380
381 while ((dst = policy->bundles) != NULL) {
382 policy->bundles = dst->next;
383 dst_free(dst);
384 }
385
386 if (del_timer(&policy->timer))
387 atomic_dec(&policy->refcnt);
388
389 if (atomic_read(&policy->refcnt) > 1)
390 flow_cache_flush();
391
392 xfrm_pol_put(policy);
393}
394
David Howellsc4028952006-11-22 14:57:56 +0000395static void xfrm_policy_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 struct xfrm_policy *policy;
David S. Miller2518c7c2006-08-24 04:45:07 -0700398 struct hlist_node *entry, *tmp;
399 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 spin_lock_bh(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700402 gc_list.first = xfrm_policy_gc_list.first;
403 INIT_HLIST_HEAD(&xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 spin_unlock_bh(&xfrm_policy_gc_lock);
405
David S. Miller2518c7c2006-08-24 04:45:07 -0700406 hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 xfrm_policy_gc_kill(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410/* Rule must be locked. Release descentant resources, announce
411 * entry dead. The rule must be unlinked from lists to the moment.
412 */
413
414static void xfrm_policy_kill(struct xfrm_policy *policy)
415{
416 int dead;
417
418 write_lock_bh(&policy->lock);
419 dead = policy->dead;
420 policy->dead = 1;
421 write_unlock_bh(&policy->lock);
422
423 if (unlikely(dead)) {
424 WARN_ON(1);
425 return;
426 }
427
428 spin_lock(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700429 hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 spin_unlock(&xfrm_policy_gc_lock);
431
432 schedule_work(&xfrm_policy_gc_work);
433}
434
David S. Miller2518c7c2006-08-24 04:45:07 -0700435struct xfrm_policy_hash {
436 struct hlist_head *table;
437 unsigned int hmask;
438};
439
440static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
441static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
442static struct hlist_head *xfrm_policy_byidx __read_mostly;
443static unsigned int xfrm_idx_hmask __read_mostly;
444static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
445
David S. Miller2518c7c2006-08-24 04:45:07 -0700446static inline unsigned int idx_hash(u32 index)
447{
448 return __idx_hash(index, xfrm_idx_hmask);
449}
450
David S. Miller2518c7c2006-08-24 04:45:07 -0700451static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
452{
453 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
454 unsigned int hash = __sel_hash(sel, family, hmask);
455
456 return (hash == hmask + 1 ?
457 &xfrm_policy_inexact[dir] :
458 xfrm_policy_bydst[dir].table + hash);
459}
460
461static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
462{
463 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
464 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
465
466 return xfrm_policy_bydst[dir].table + hash;
467}
468
David S. Miller2518c7c2006-08-24 04:45:07 -0700469static void xfrm_dst_hash_transfer(struct hlist_head *list,
470 struct hlist_head *ndsttable,
471 unsigned int nhashmask)
472{
473 struct hlist_node *entry, *tmp;
474 struct xfrm_policy *pol;
475
476 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
477 unsigned int h;
478
479 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
480 pol->family, nhashmask);
481 hlist_add_head(&pol->bydst, ndsttable+h);
482 }
483}
484
485static void xfrm_idx_hash_transfer(struct hlist_head *list,
486 struct hlist_head *nidxtable,
487 unsigned int nhashmask)
488{
489 struct hlist_node *entry, *tmp;
490 struct xfrm_policy *pol;
491
492 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
493 unsigned int h;
494
495 h = __idx_hash(pol->index, nhashmask);
496 hlist_add_head(&pol->byidx, nidxtable+h);
497 }
498}
499
500static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
501{
502 return ((old_hmask + 1) << 1) - 1;
503}
504
505static void xfrm_bydst_resize(int dir)
506{
507 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
508 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
509 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
510 struct hlist_head *odst = xfrm_policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700511 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700512 int i;
513
514 if (!ndst)
515 return;
516
517 write_lock_bh(&xfrm_policy_lock);
518
519 for (i = hmask; i >= 0; i--)
520 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
521
522 xfrm_policy_bydst[dir].table = ndst;
523 xfrm_policy_bydst[dir].hmask = nhashmask;
524
525 write_unlock_bh(&xfrm_policy_lock);
526
David S. Miller44e36b42006-08-24 04:50:50 -0700527 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700528}
529
530static void xfrm_byidx_resize(int total)
531{
532 unsigned int hmask = xfrm_idx_hmask;
533 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
534 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
535 struct hlist_head *oidx = xfrm_policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700536 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700537 int i;
538
539 if (!nidx)
540 return;
541
542 write_lock_bh(&xfrm_policy_lock);
543
544 for (i = hmask; i >= 0; i--)
545 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
546
547 xfrm_policy_byidx = nidx;
548 xfrm_idx_hmask = nhashmask;
549
550 write_unlock_bh(&xfrm_policy_lock);
551
David S. Miller44e36b42006-08-24 04:50:50 -0700552 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700553}
554
555static inline int xfrm_bydst_should_resize(int dir, int *total)
556{
557 unsigned int cnt = xfrm_policy_count[dir];
558 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
559
560 if (total)
561 *total += cnt;
562
563 if ((hmask + 1) < xfrm_policy_hashmax &&
564 cnt > hmask)
565 return 1;
566
567 return 0;
568}
569
570static inline int xfrm_byidx_should_resize(int total)
571{
572 unsigned int hmask = xfrm_idx_hmask;
573
574 if ((hmask + 1) < xfrm_policy_hashmax &&
575 total > hmask)
576 return 1;
577
578 return 0;
579}
580
581static DEFINE_MUTEX(hash_resize_mutex);
582
David Howellsc4028952006-11-22 14:57:56 +0000583static void xfrm_hash_resize(struct work_struct *__unused)
David S. Miller2518c7c2006-08-24 04:45:07 -0700584{
585 int dir, total;
586
587 mutex_lock(&hash_resize_mutex);
588
589 total = 0;
590 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
591 if (xfrm_bydst_should_resize(dir, &total))
592 xfrm_bydst_resize(dir);
593 }
594 if (xfrm_byidx_should_resize(total))
595 xfrm_byidx_resize(total);
596
597 mutex_unlock(&hash_resize_mutex);
598}
599
David Howellsc4028952006-11-22 14:57:56 +0000600static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602/* Generate new index... KAME seems to generate them ordered by cost
603 * of an absolute inpredictability of ordering of rules. This will not pass. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700604static u32 xfrm_gen_index(u8 type, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 static u32 idx_generator;
607
608 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700609 struct hlist_node *entry;
610 struct hlist_head *list;
611 struct xfrm_policy *p;
612 u32 idx;
613 int found;
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 idx = (idx_generator | dir);
616 idx_generator += 8;
617 if (idx == 0)
618 idx = 8;
David S. Miller2518c7c2006-08-24 04:45:07 -0700619 list = xfrm_policy_byidx + idx_hash(idx);
620 found = 0;
621 hlist_for_each_entry(p, entry, list, byidx) {
622 if (p->index == idx) {
623 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700627 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return idx;
629 }
630}
631
David S. Miller2518c7c2006-08-24 04:45:07 -0700632static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
633{
634 u32 *p1 = (u32 *) s1;
635 u32 *p2 = (u32 *) s2;
636 int len = sizeof(struct xfrm_selector) / sizeof(u32);
637 int i;
638
639 for (i = 0; i < len; i++) {
640 if (p1[i] != p2[i])
641 return 1;
642 }
643
644 return 0;
645}
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
648{
David S. Miller2518c7c2006-08-24 04:45:07 -0700649 struct xfrm_policy *pol;
650 struct xfrm_policy *delpol;
651 struct hlist_head *chain;
652 struct hlist_node *entry, *newpos, *last;
David S. Miller9b78a822005-12-22 07:39:48 -0800653 struct dst_entry *gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700656 chain = policy_hash_bysel(&policy->selector, policy->family, dir);
657 delpol = NULL;
658 newpos = NULL;
659 last = NULL;
660 hlist_for_each_entry(pol, entry, chain, bydst) {
661 if (!delpol &&
662 pol->type == policy->type &&
663 !selector_cmp(&pol->selector, &policy->selector) &&
Trent Jaegerdf718372005-12-13 23:12:27 -0800664 xfrm_sec_ctx_match(pol->security, policy->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (excl) {
666 write_unlock_bh(&xfrm_policy_lock);
667 return -EEXIST;
668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 delpol = pol;
670 if (policy->priority > pol->priority)
671 continue;
672 } else if (policy->priority >= pol->priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700673 last = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 continue;
675 }
676 if (!newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700677 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (delpol)
679 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700680 last = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700682 if (!newpos)
683 newpos = last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700685 hlist_add_after(newpos, &policy->bydst);
686 else
687 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 xfrm_pol_hold(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700689 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700691 if (delpol) {
692 hlist_del(&delpol->bydst);
693 hlist_del(&delpol->byidx);
694 xfrm_policy_count[dir]--;
695 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700696 policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700697 hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 policy->curlft.add_time = (unsigned long)xtime.tv_sec;
699 policy->curlft.use_time = 0;
700 if (!mod_timer(&policy->timer, jiffies + HZ))
701 xfrm_pol_hold(policy);
702 write_unlock_bh(&xfrm_policy_lock);
703
David S. Miller9b78a822005-12-22 07:39:48 -0800704 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 xfrm_policy_kill(delpol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700706 else if (xfrm_bydst_should_resize(dir, NULL))
707 schedule_work(&xfrm_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800708
709 read_lock_bh(&xfrm_policy_lock);
710 gc_list = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700711 entry = &policy->bydst;
712 hlist_for_each_entry_continue(policy, entry, bydst) {
David S. Miller9b78a822005-12-22 07:39:48 -0800713 struct dst_entry *dst;
714
715 write_lock(&policy->lock);
716 dst = policy->bundles;
717 if (dst) {
718 struct dst_entry *tail = dst;
719 while (tail->next)
720 tail = tail->next;
721 tail->next = gc_list;
722 gc_list = dst;
723
724 policy->bundles = NULL;
725 }
726 write_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
David S. Miller9b78a822005-12-22 07:39:48 -0800728 read_unlock_bh(&xfrm_policy_lock);
729
730 while (gc_list) {
731 struct dst_entry *dst = gc_list;
732
733 gc_list = dst->next;
734 dst_free(dst);
735 }
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return 0;
738}
739EXPORT_SYMBOL(xfrm_policy_insert);
740
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700741struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
742 struct xfrm_selector *sel,
Trent Jaegerdf718372005-12-13 23:12:27 -0800743 struct xfrm_sec_ctx *ctx, int delete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
David S. Miller2518c7c2006-08-24 04:45:07 -0700745 struct xfrm_policy *pol, *ret;
746 struct hlist_head *chain;
747 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700750 chain = policy_hash_bysel(sel, sel->family, dir);
751 ret = NULL;
752 hlist_for_each_entry(pol, entry, chain, bydst) {
753 if (pol->type == type &&
754 !selector_cmp(sel, &pol->selector) &&
755 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700757 if (delete) {
758 hlist_del(&pol->bydst);
759 hlist_del(&pol->byidx);
760 xfrm_policy_count[dir]--;
761 }
762 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 break;
764 }
765 }
766 write_unlock_bh(&xfrm_policy_lock);
767
David S. Miller2518c7c2006-08-24 04:45:07 -0700768 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700770 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700772 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
Trent Jaegerdf718372005-12-13 23:12:27 -0800774EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700776struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
David S. Miller2518c7c2006-08-24 04:45:07 -0700778 struct xfrm_policy *pol, *ret;
779 struct hlist_head *chain;
780 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700783 chain = xfrm_policy_byidx + idx_hash(id);
784 ret = NULL;
785 hlist_for_each_entry(pol, entry, chain, byidx) {
786 if (pol->type == type && pol->index == id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700788 if (delete) {
789 hlist_del(&pol->bydst);
790 hlist_del(&pol->byidx);
791 xfrm_policy_count[dir]--;
792 }
793 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 break;
795 }
796 }
797 write_unlock_bh(&xfrm_policy_lock);
798
David S. Miller2518c7c2006-08-24 04:45:07 -0700799 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700801 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700803 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805EXPORT_SYMBOL(xfrm_policy_byid);
806
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700807void xfrm_policy_flush(u8 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 int dir;
810
811 write_lock_bh(&xfrm_policy_lock);
812 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700813 struct xfrm_policy *pol;
814 struct hlist_node *entry;
David S. Millerae8c0572006-10-03 16:00:26 -0700815 int i, killed;
David S. Miller2518c7c2006-08-24 04:45:07 -0700816
David S. Millerae8c0572006-10-03 16:00:26 -0700817 killed = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700818 again1:
819 hlist_for_each_entry(pol, entry,
820 &xfrm_policy_inexact[dir], bydst) {
821 if (pol->type != type)
822 continue;
823 hlist_del(&pol->bydst);
824 hlist_del(&pol->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 write_unlock_bh(&xfrm_policy_lock);
826
David S. Miller2518c7c2006-08-24 04:45:07 -0700827 xfrm_policy_kill(pol);
David S. Millerae8c0572006-10-03 16:00:26 -0700828 killed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700831 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700833
834 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
835 again2:
836 hlist_for_each_entry(pol, entry,
837 xfrm_policy_bydst[dir].table + i,
838 bydst) {
839 if (pol->type != type)
840 continue;
841 hlist_del(&pol->bydst);
842 hlist_del(&pol->byidx);
843 write_unlock_bh(&xfrm_policy_lock);
844
845 xfrm_policy_kill(pol);
David S. Millerae8c0572006-10-03 16:00:26 -0700846 killed++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700847
848 write_lock_bh(&xfrm_policy_lock);
849 goto again2;
850 }
851 }
852
David S. Millerae8c0572006-10-03 16:00:26 -0700853 xfrm_policy_count[dir] -= killed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855 atomic_inc(&flow_cache_genid);
856 write_unlock_bh(&xfrm_policy_lock);
857}
858EXPORT_SYMBOL(xfrm_policy_flush);
859
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700860int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 void *data)
862{
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800863 struct xfrm_policy *pol, *last = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700864 struct hlist_node *entry;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800865 int dir, last_dir = 0, count, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700868 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700871 struct hlist_head *table = xfrm_policy_bydst[dir].table;
872 int i;
873
874 hlist_for_each_entry(pol, entry,
875 &xfrm_policy_inexact[dir], bydst) {
876 if (pol->type != type)
877 continue;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800878 if (last) {
879 error = func(last, last_dir % XFRM_POLICY_MAX,
880 count, data);
881 if (error)
882 goto out;
883 }
884 last = pol;
885 last_dir = dir;
886 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700888 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
889 hlist_for_each_entry(pol, entry, table + i, bydst) {
890 if (pol->type != type)
891 continue;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800892 if (last) {
893 error = func(last, last_dir % XFRM_POLICY_MAX,
894 count, data);
895 if (error)
896 goto out;
897 }
898 last = pol;
899 last_dir = dir;
900 count++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700901 }
902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800904 if (count == 0) {
905 error = -ENOENT;
906 goto out;
907 }
908 error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909out:
910 read_unlock_bh(&xfrm_policy_lock);
911 return error;
912}
913EXPORT_SYMBOL(xfrm_policy_walk);
914
James Morris134b0fc2006-10-05 15:42:27 -0500915/*
916 * Find policy to apply to this flow.
917 *
918 * Returns 0 if policy found, else an -errno.
919 */
David S. Miller2518c7c2006-08-24 04:45:07 -0700920static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
921 u8 type, u16 family, int dir)
922{
923 struct xfrm_selector *sel = &pol->selector;
James Morris134b0fc2006-10-05 15:42:27 -0500924 int match, ret = -ESRCH;
David S. Miller2518c7c2006-08-24 04:45:07 -0700925
926 if (pol->family != family ||
927 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500928 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700929
930 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500931 if (match)
932 ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700933
James Morris134b0fc2006-10-05 15:42:27 -0500934 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700935}
936
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700937static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
938 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
James Morris134b0fc2006-10-05 15:42:27 -0500940 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700941 struct xfrm_policy *pol, *ret;
942 xfrm_address_t *daddr, *saddr;
943 struct hlist_node *entry;
944 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700945 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700946
947 daddr = xfrm_flowi_daddr(fl, family);
948 saddr = xfrm_flowi_saddr(fl, family);
949 if (unlikely(!daddr || !saddr))
950 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700953 chain = policy_hash_direct(daddr, saddr, family, dir);
954 ret = NULL;
955 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500956 err = xfrm_policy_match(pol, fl, type, family, dir);
957 if (err) {
958 if (err == -ESRCH)
959 continue;
960 else {
961 ret = ERR_PTR(err);
962 goto fail;
963 }
964 } else {
David S. Milleracba48e2006-08-25 15:46:46 -0700965 ret = pol;
966 priority = ret->priority;
967 break;
968 }
969 }
970 chain = &xfrm_policy_inexact[dir];
971 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500972 err = xfrm_policy_match(pol, fl, type, family, dir);
973 if (err) {
974 if (err == -ESRCH)
975 continue;
976 else {
977 ret = ERR_PTR(err);
978 goto fail;
979 }
980 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700981 ret = pol;
982 break;
983 }
984 }
David S. Milleracba48e2006-08-25 15:46:46 -0700985 if (ret)
986 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -0500987fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700989
David S. Miller2518c7c2006-08-24 04:45:07 -0700990 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700991}
992
James Morris134b0fc2006-10-05 15:42:27 -0500993static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700994 void **objp, atomic_t **obj_refp)
995{
996 struct xfrm_policy *pol;
James Morris134b0fc2006-10-05 15:42:27 -0500997 int err = 0;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700998
999#ifdef CONFIG_XFRM_SUB_POLICY
1000 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
James Morris134b0fc2006-10-05 15:42:27 -05001001 if (IS_ERR(pol)) {
1002 err = PTR_ERR(pol);
1003 pol = NULL;
1004 }
1005 if (pol || err)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001006 goto end;
1007#endif
1008 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
James Morris134b0fc2006-10-05 15:42:27 -05001009 if (IS_ERR(pol)) {
1010 err = PTR_ERR(pol);
1011 pol = NULL;
1012 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001013#ifdef CONFIG_XFRM_SUB_POLICY
David S. Miller2518c7c2006-08-24 04:45:07 -07001014end:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001015#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if ((*objp = (void *) pol) != NULL)
1017 *obj_refp = &pol->refcnt;
James Morris134b0fc2006-10-05 15:42:27 -05001018 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
Trent Jaegerdf718372005-12-13 23:12:27 -08001021static inline int policy_to_flow_dir(int dir)
1022{
1023 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1024 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1025 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1026 return dir;
1027 switch (dir) {
1028 default:
1029 case XFRM_POLICY_IN:
1030 return FLOW_DIR_IN;
1031 case XFRM_POLICY_OUT:
1032 return FLOW_DIR_OUT;
1033 case XFRM_POLICY_FWD:
1034 return FLOW_DIR_FWD;
1035 };
1036}
1037
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001038static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 struct xfrm_policy *pol;
1041
1042 read_lock_bh(&xfrm_policy_lock);
1043 if ((pol = sk->sk_policy[dir]) != NULL) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001044 int match = xfrm_selector_match(&pol->selector, fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 sk->sk_family);
Trent Jaegerdf718372005-12-13 23:12:27 -08001046 int err = 0;
1047
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001048 if (match) {
1049 err = security_xfrm_policy_lookup(pol, fl->secid,
1050 policy_to_flow_dir(dir));
1051 if (!err)
1052 xfrm_pol_hold(pol);
1053 else if (err == -ESRCH)
1054 pol = NULL;
1055 else
1056 pol = ERR_PTR(err);
1057 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 pol = NULL;
1059 }
1060 read_unlock_bh(&xfrm_policy_lock);
1061 return pol;
1062}
1063
1064static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1065{
David S. Miller2518c7c2006-08-24 04:45:07 -07001066 struct hlist_head *chain = policy_hash_bysel(&pol->selector,
1067 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001068
David S. Miller2518c7c2006-08-24 04:45:07 -07001069 hlist_add_head(&pol->bydst, chain);
1070 hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
1071 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001073
1074 if (xfrm_bydst_should_resize(dir, NULL))
1075 schedule_work(&xfrm_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
1078static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1079 int dir)
1080{
David S. Miller2518c7c2006-08-24 04:45:07 -07001081 if (hlist_unhashed(&pol->bydst))
1082 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
David S. Miller2518c7c2006-08-24 04:45:07 -07001084 hlist_del(&pol->bydst);
1085 hlist_del(&pol->byidx);
1086 xfrm_policy_count[dir]--;
1087
1088 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
Herbert Xu4666faa2005-06-18 22:43:22 -07001091int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 write_lock_bh(&xfrm_policy_lock);
1094 pol = __xfrm_policy_unlink(pol, dir);
1095 write_unlock_bh(&xfrm_policy_lock);
1096 if (pol) {
1097 if (dir < XFRM_POLICY_MAX)
1098 atomic_inc(&flow_cache_genid);
1099 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001102 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
David S. Millera70fcb02006-03-20 19:18:52 -08001104EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1107{
1108 struct xfrm_policy *old_pol;
1109
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001110#ifdef CONFIG_XFRM_SUB_POLICY
1111 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1112 return -EINVAL;
1113#endif
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 write_lock_bh(&xfrm_policy_lock);
1116 old_pol = sk->sk_policy[dir];
1117 sk->sk_policy[dir] = pol;
1118 if (pol) {
1119 pol->curlft.add_time = (unsigned long)xtime.tv_sec;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001120 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1122 }
1123 if (old_pol)
1124 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1125 write_unlock_bh(&xfrm_policy_lock);
1126
1127 if (old_pol) {
1128 xfrm_policy_kill(old_pol);
1129 }
1130 return 0;
1131}
1132
1133static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1134{
1135 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
1136
1137 if (newp) {
1138 newp->selector = old->selector;
Trent Jaegerdf718372005-12-13 23:12:27 -08001139 if (security_xfrm_policy_clone(old, newp)) {
1140 kfree(newp);
1141 return NULL; /* ENOMEM */
1142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 newp->lft = old->lft;
1144 newp->curlft = old->curlft;
1145 newp->action = old->action;
1146 newp->flags = old->flags;
1147 newp->xfrm_nr = old->xfrm_nr;
1148 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001149 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 memcpy(newp->xfrm_vec, old->xfrm_vec,
1151 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1152 write_lock_bh(&xfrm_policy_lock);
1153 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1154 write_unlock_bh(&xfrm_policy_lock);
1155 xfrm_pol_put(newp);
1156 }
1157 return newp;
1158}
1159
1160int __xfrm_sk_clone_policy(struct sock *sk)
1161{
1162 struct xfrm_policy *p0 = sk->sk_policy[0],
1163 *p1 = sk->sk_policy[1];
1164
1165 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1166 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1167 return -ENOMEM;
1168 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1169 return -ENOMEM;
1170 return 0;
1171}
1172
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001173static int
1174xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
1175 unsigned short family)
1176{
1177 int err;
1178 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1179
1180 if (unlikely(afinfo == NULL))
1181 return -EINVAL;
1182 err = afinfo->get_saddr(local, remote);
1183 xfrm_policy_put_afinfo(afinfo);
1184 return err;
1185}
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187/* Resolve list of templates for the flow, given policy. */
1188
1189static int
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001190xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1191 struct xfrm_state **xfrm,
1192 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 int nx;
1195 int i, error;
1196 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1197 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001198 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1201 struct xfrm_state *x;
1202 xfrm_address_t *remote = daddr;
1203 xfrm_address_t *local = saddr;
1204 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1205
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001206 if (tmpl->mode == XFRM_MODE_TUNNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 remote = &tmpl->id.daddr;
1208 local = &tmpl->saddr;
Miika Komu76b3f052006-11-30 16:40:43 -08001209 family = tmpl->encap_family;
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001210 if (xfrm_addr_any(local, family)) {
1211 error = xfrm_get_saddr(&tmp, remote, family);
1212 if (error)
1213 goto fail;
1214 local = &tmp;
1215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
1218 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1219
1220 if (x && x->km.state == XFRM_STATE_VALID) {
1221 xfrm[nx++] = x;
1222 daddr = remote;
1223 saddr = local;
1224 continue;
1225 }
1226 if (x) {
1227 error = (x->km.state == XFRM_STATE_ERROR ?
1228 -EINVAL : -EAGAIN);
1229 xfrm_state_put(x);
1230 }
1231
1232 if (!tmpl->optional)
1233 goto fail;
1234 }
1235 return nx;
1236
1237fail:
1238 for (nx--; nx>=0; nx--)
1239 xfrm_state_put(xfrm[nx]);
1240 return error;
1241}
1242
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001243static int
1244xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1245 struct xfrm_state **xfrm,
1246 unsigned short family)
1247{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001248 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1249 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001250 int cnx = 0;
1251 int error;
1252 int ret;
1253 int i;
1254
1255 for (i = 0; i < npols; i++) {
1256 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1257 error = -ENOBUFS;
1258 goto fail;
1259 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001260
1261 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001262 if (ret < 0) {
1263 error = ret;
1264 goto fail;
1265 } else
1266 cnx += ret;
1267 }
1268
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001269 /* found states are sorted for outbound processing */
1270 if (npols > 1)
1271 xfrm_state_sort(xfrm, tpp, cnx, family);
1272
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001273 return cnx;
1274
1275 fail:
1276 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001277 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001278 return error;
1279
1280}
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282/* Check that the bundle accepts the flow and its components are
1283 * still valid.
1284 */
1285
1286static struct dst_entry *
1287xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1288{
1289 struct dst_entry *x;
1290 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1291 if (unlikely(afinfo == NULL))
1292 return ERR_PTR(-EINVAL);
1293 x = afinfo->find_bundle(fl, policy);
1294 xfrm_policy_put_afinfo(afinfo);
1295 return x;
1296}
1297
1298/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1299 * all the metrics... Shortly, bundle a bundle.
1300 */
1301
1302static int
1303xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
1304 struct flowi *fl, struct dst_entry **dst_p,
1305 unsigned short family)
1306{
1307 int err;
1308 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1309 if (unlikely(afinfo == NULL))
1310 return -EINVAL;
1311 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
1312 xfrm_policy_put_afinfo(afinfo);
1313 return err;
1314}
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317static int stale_bundle(struct dst_entry *dst);
1318
1319/* Main function: finds/creates a bundle for given flow.
1320 *
1321 * At the moment we eat a raw IP route. Mostly to speed up lookups
1322 * on interfaces with disabled IPsec.
1323 */
1324int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1325 struct sock *sk, int flags)
1326{
1327 struct xfrm_policy *policy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001328 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1329 int npols;
1330 int pol_dead;
1331 int xfrm_nr;
1332 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1334 struct dst_entry *dst, *dst_orig = *dst_p;
1335 int nx = 0;
1336 int err;
1337 u32 genid;
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001338 u16 family;
Trent Jaegerdf718372005-12-13 23:12:27 -08001339 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341restart:
1342 genid = atomic_read(&flow_cache_genid);
1343 policy = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001344 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1345 pols[pi] = NULL;
1346 npols = 0;
1347 pol_dead = 0;
1348 xfrm_nr = 0;
1349
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001350 if (sk && sk->sk_policy[1]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001351 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001352 if (IS_ERR(policy))
1353 return PTR_ERR(policy);
1354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 if (!policy) {
1357 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07001358 if ((dst_orig->flags & DST_NOXFRM) ||
1359 !xfrm_policy_count[XFRM_POLICY_OUT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return 0;
1361
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001362 policy = flow_cache_lookup(fl, dst_orig->ops->family,
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001363 dir, xfrm_policy_lookup);
James Morris134b0fc2006-10-05 15:42:27 -05001364 if (IS_ERR(policy))
1365 return PTR_ERR(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367
1368 if (!policy)
1369 return 0;
1370
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001371 family = dst_orig->ops->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 policy->curlft.use_time = (unsigned long)xtime.tv_sec;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001373 pols[0] = policy;
1374 npols ++;
1375 xfrm_nr += pols[0]->xfrm_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 switch (policy->action) {
1378 case XFRM_POLICY_BLOCK:
1379 /* Prohibit the flow */
Patrick McHardye1044112005-09-08 15:11:55 -07001380 err = -EPERM;
1381 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 case XFRM_POLICY_ALLOW:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001384#ifndef CONFIG_XFRM_SUB_POLICY
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (policy->xfrm_nr == 0) {
1386 /* Flow passes not transformed. */
1387 xfrm_pol_put(policy);
1388 return 0;
1389 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001390#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /* Try to find matching bundle.
1393 *
1394 * LATER: help from flow cache. It is optional, this
1395 * is required only for output policy.
1396 */
1397 dst = xfrm_find_bundle(fl, policy, family);
1398 if (IS_ERR(dst)) {
Patrick McHardye1044112005-09-08 15:11:55 -07001399 err = PTR_ERR(dst);
1400 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402
1403 if (dst)
1404 break;
1405
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001406#ifdef CONFIG_XFRM_SUB_POLICY
1407 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1408 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1409 fl, family,
1410 XFRM_POLICY_OUT);
1411 if (pols[1]) {
James Morris134b0fc2006-10-05 15:42:27 -05001412 if (IS_ERR(pols[1])) {
1413 err = PTR_ERR(pols[1]);
1414 goto error;
1415 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001416 if (pols[1]->action == XFRM_POLICY_BLOCK) {
1417 err = -EPERM;
1418 goto error;
1419 }
1420 npols ++;
1421 xfrm_nr += pols[1]->xfrm_nr;
1422 }
1423 }
1424
1425 /*
1426 * Because neither flowi nor bundle information knows about
1427 * transformation template size. On more than one policy usage
1428 * we can realize whether all of them is bypass or not after
1429 * they are searched. See above not-transformed bypass
1430 * is surrounded by non-sub policy configuration, too.
1431 */
1432 if (xfrm_nr == 0) {
1433 /* Flow passes not transformed. */
1434 xfrm_pols_put(pols, npols);
1435 return 0;
1436 }
1437
1438#endif
1439 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 if (unlikely(nx<0)) {
1442 err = nx;
1443 if (err == -EAGAIN && flags) {
1444 DECLARE_WAITQUEUE(wait, current);
1445
1446 add_wait_queue(&km_waitq, &wait);
1447 set_current_state(TASK_INTERRUPTIBLE);
1448 schedule();
1449 set_current_state(TASK_RUNNING);
1450 remove_wait_queue(&km_waitq, &wait);
1451
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001452 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 if (nx == -EAGAIN && signal_pending(current)) {
1455 err = -ERESTART;
1456 goto error;
1457 }
1458 if (nx == -EAGAIN ||
1459 genid != atomic_read(&flow_cache_genid)) {
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001460 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 goto restart;
1462 }
1463 err = nx;
1464 }
1465 if (err < 0)
1466 goto error;
1467 }
1468 if (nx == 0) {
1469 /* Flow passes not transformed. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001470 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return 0;
1472 }
1473
1474 dst = dst_orig;
1475 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
1476
1477 if (unlikely(err)) {
1478 int i;
1479 for (i=0; i<nx; i++)
1480 xfrm_state_put(xfrm[i]);
1481 goto error;
1482 }
1483
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001484 for (pi = 0; pi < npols; pi++) {
1485 read_lock_bh(&pols[pi]->lock);
1486 pol_dead |= pols[pi]->dead;
1487 read_unlock_bh(&pols[pi]->lock);
1488 }
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 write_lock_bh(&policy->lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001491 if (unlikely(pol_dead || stale_bundle(dst))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 /* Wow! While we worked on resolving, this
1493 * policy has gone. Retry. It is not paranoia,
1494 * we just cannot enlist new bundle to dead object.
1495 * We can't enlist stable bundles either.
1496 */
1497 write_unlock_bh(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (dst)
1499 dst_free(dst);
Herbert Xu00de6512006-02-13 16:01:27 -08001500
1501 err = -EHOSTUNREACH;
1502 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
1504 dst->next = policy->bundles;
1505 policy->bundles = dst;
1506 dst_hold(dst);
1507 write_unlock_bh(&policy->lock);
1508 }
1509 *dst_p = dst;
1510 dst_release(dst_orig);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001511 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return 0;
1513
1514error:
1515 dst_release(dst_orig);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001516 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 *dst_p = NULL;
1518 return err;
1519}
1520EXPORT_SYMBOL(xfrm_lookup);
1521
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001522static inline int
1523xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1524{
1525 struct xfrm_state *x;
1526 int err;
1527
1528 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1529 return 0;
1530 x = skb->sp->xvec[idx];
1531 if (!x->type->reject)
1532 return 0;
1533 xfrm_state_hold(x);
1534 err = x->type->reject(x, skb, fl);
1535 xfrm_state_put(x);
1536 return err;
1537}
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539/* When skb is transformed back to its "native" form, we have to
1540 * check policy restrictions. At the moment we make this in maximally
1541 * stupid way. Shame on me. :-) Of course, connected sockets must
1542 * have policy cached at them.
1543 */
1544
1545static inline int
1546xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
1547 unsigned short family)
1548{
1549 if (xfrm_state_kern(x))
1550 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family);
1551 return x->id.proto == tmpl->id.proto &&
1552 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1553 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1554 x->props.mode == tmpl->mode &&
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07001555 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1556 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001557 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1558 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001561/*
1562 * 0 or more than 0 is returned when validation is succeeded (either bypass
1563 * because of optional transport mode, or next index of the mathced secpath
1564 * state with the template.
1565 * -1 is returned when no matching template is found.
1566 * Otherwise "-2 - errored_index" is returned.
1567 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568static inline int
1569xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1570 unsigned short family)
1571{
1572 int idx = start;
1573
1574 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001575 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return start;
1577 } else
1578 start = -1;
1579 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001580 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001582 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1583 if (start == -1)
1584 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
1588 return start;
1589}
1590
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001591int
1592xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
1594 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001595 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 if (unlikely(afinfo == NULL))
1598 return -EAFNOSUPPORT;
1599
1600 afinfo->decode_session(skb, fl);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07001601 err = security_xfrm_decode_session(skb, &fl->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001603 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001605EXPORT_SYMBOL(xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001607static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
1609 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001610 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001611 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
1615
1616 return 0;
1617}
1618
1619int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
1620 unsigned short family)
1621{
1622 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001623 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1624 int npols = 0;
1625 int xfrm_nr;
1626 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 struct flowi fl;
Trent Jaegerdf718372005-12-13 23:12:27 -08001628 u8 fl_dir = policy_to_flow_dir(dir);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001629 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001631 if (xfrm_decode_session(skb, &fl, family) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return 0;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08001633 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 /* First, check used SA against their selectors. */
1636 if (skb->sp) {
1637 int i;
1638
1639 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001640 struct xfrm_state *x = skb->sp->xvec[i];
1641 if (!xfrm_selector_match(&x->sel, &fl, family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644 }
1645
1646 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001647 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001648 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001649 if (IS_ERR(pol))
1650 return 0;
1651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 if (!pol)
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001654 pol = flow_cache_lookup(&fl, family, fl_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 xfrm_policy_lookup);
1656
James Morris134b0fc2006-10-05 15:42:27 -05001657 if (IS_ERR(pol))
1658 return 0;
1659
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001660 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001661 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001662 xfrm_secpath_reject(xerr_idx, skb, &fl);
1663 return 0;
1664 }
1665 return 1;
1666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 pol->curlft.use_time = (unsigned long)xtime.tv_sec;
1669
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001670 pols[0] = pol;
1671 npols ++;
1672#ifdef CONFIG_XFRM_SUB_POLICY
1673 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1674 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1675 &fl, family,
1676 XFRM_POLICY_IN);
1677 if (pols[1]) {
James Morris134b0fc2006-10-05 15:42:27 -05001678 if (IS_ERR(pols[1]))
1679 return 0;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001680 pols[1]->curlft.use_time = (unsigned long)xtime.tv_sec;
1681 npols ++;
1682 }
1683 }
1684#endif
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (pol->action == XFRM_POLICY_ALLOW) {
1687 struct sec_path *sp;
1688 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001689 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001690 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001691 struct xfrm_tmpl **tpp = tp;
1692 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 int i, k;
1694
1695 if ((sp = skb->sp) == NULL)
1696 sp = &dummy;
1697
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001698 for (pi = 0; pi < npols; pi++) {
1699 if (pols[pi] != pol &&
1700 pols[pi]->action != XFRM_POLICY_ALLOW)
1701 goto reject;
1702 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
1703 goto reject_error;
1704 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1705 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1706 }
1707 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001708 if (npols > 1) {
1709 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1710 tpp = stp;
1711 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 /* For each tunnel xfrm, find the first matching tmpl.
1714 * For each tmpl before that, find corresponding xfrm.
1715 * Order is _important_. Later we will implement
1716 * some barriers, but at the moment barriers
1717 * are implied between each two transformations.
1718 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001719 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1720 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001721 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001722 if (k < -1)
1723 /* "-2 - errored_index" returned */
1724 xerr_idx = -(2+k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 }
1728
James Morrisd1d9fac2006-09-01 00:32:12 -07001729 if (secpath_has_nontransport(sp, k, &xerr_idx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 goto reject;
1731
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001732 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 return 1;
1734 }
1735
1736reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001737 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001738reject_error:
1739 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return 0;
1741}
1742EXPORT_SYMBOL(__xfrm_policy_check);
1743
1744int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1745{
1746 struct flowi fl;
1747
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001748 if (xfrm_decode_session(skb, &fl, family) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 return 0;
1750
1751 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1752}
1753EXPORT_SYMBOL(__xfrm_route_forward);
1754
David S. Millerd49c73c2006-08-13 18:55:53 -07001755/* Optimize later using cookies and generation ids. */
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1758{
David S. Millerd49c73c2006-08-13 18:55:53 -07001759 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1760 * to "-1" to force all XFRM destinations to get validated by
1761 * dst_ops->check on every use. We do this because when a
1762 * normal route referenced by an XFRM dst is obsoleted we do
1763 * not go looking around for all parent referencing XFRM dsts
1764 * so that we can invalidate them. It is just too much work.
1765 * Instead we make the checks here on every use. For example:
1766 *
1767 * XFRM dst A --> IPv4 dst X
1768 *
1769 * X is the "xdst->route" of A (X is also the "dst->path" of A
1770 * in this example). If X is marked obsolete, "A" will not
1771 * notice. That's what we are validating here via the
1772 * stale_bundle() check.
1773 *
1774 * When a policy's bundle is pruned, we dst_free() the XFRM
1775 * dst which causes it's ->obsolete field to be set to a
1776 * positive non-zero integer. If an XFRM dst has been pruned
1777 * like this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08001778 */
David S. Millerd49c73c2006-08-13 18:55:53 -07001779 if (dst->obsolete < 0 && !stale_bundle(dst))
1780 return dst;
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return NULL;
1783}
1784
1785static int stale_bundle(struct dst_entry *dst)
1786{
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05001787 return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Herbert Xuaabc9762005-05-03 16:27:10 -07001790void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
1793 dst->dev = &loopback_dev;
1794 dev_hold(&loopback_dev);
1795 dev_put(dev);
1796 }
1797}
Herbert Xuaabc9762005-05-03 16:27:10 -07001798EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800static void xfrm_link_failure(struct sk_buff *skb)
1801{
1802 /* Impossible. Such dst must be popped before reaches point of failure. */
1803 return;
1804}
1805
1806static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1807{
1808 if (dst) {
1809 if (dst->obsolete) {
1810 dst_release(dst);
1811 dst = NULL;
1812 }
1813 }
1814 return dst;
1815}
1816
David S. Miller2518c7c2006-08-24 04:45:07 -07001817static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
1818{
1819 struct dst_entry *dst, **dstp;
1820
1821 write_lock(&pol->lock);
1822 dstp = &pol->bundles;
1823 while ((dst=*dstp) != NULL) {
1824 if (func(dst)) {
1825 *dstp = dst->next;
1826 dst->next = *gc_list_p;
1827 *gc_list_p = dst;
1828 } else {
1829 dstp = &dst->next;
1830 }
1831 }
1832 write_unlock(&pol->lock);
1833}
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1836{
David S. Miller2518c7c2006-08-24 04:45:07 -07001837 struct dst_entry *gc_list = NULL;
1838 int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07001841 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
1842 struct xfrm_policy *pol;
1843 struct hlist_node *entry;
1844 struct hlist_head *table;
1845 int i;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001846
David S. Miller2518c7c2006-08-24 04:45:07 -07001847 hlist_for_each_entry(pol, entry,
1848 &xfrm_policy_inexact[dir], bydst)
1849 prune_one_bundle(pol, func, &gc_list);
1850
1851 table = xfrm_policy_bydst[dir].table;
1852 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
1853 hlist_for_each_entry(pol, entry, table + i, bydst)
1854 prune_one_bundle(pol, func, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
1856 }
1857 read_unlock_bh(&xfrm_policy_lock);
1858
1859 while (gc_list) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001860 struct dst_entry *dst = gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 gc_list = dst->next;
1862 dst_free(dst);
1863 }
1864}
1865
1866static int unused_bundle(struct dst_entry *dst)
1867{
1868 return !atomic_read(&dst->__refcnt);
1869}
1870
1871static void __xfrm_garbage_collect(void)
1872{
1873 xfrm_prune_bundles(unused_bundle);
1874}
1875
David S. Miller1c095392006-08-24 03:30:28 -07001876static int xfrm_flush_bundles(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
1878 xfrm_prune_bundles(stale_bundle);
1879 return 0;
1880}
1881
1882void xfrm_init_pmtu(struct dst_entry *dst)
1883{
1884 do {
1885 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1886 u32 pmtu, route_mtu_cached;
1887
1888 pmtu = dst_mtu(dst->child);
1889 xdst->child_mtu_cached = pmtu;
1890
1891 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
1892
1893 route_mtu_cached = dst_mtu(xdst->route);
1894 xdst->route_mtu_cached = route_mtu_cached;
1895
1896 if (pmtu > route_mtu_cached)
1897 pmtu = route_mtu_cached;
1898
1899 dst->metrics[RTAX_MTU-1] = pmtu;
1900 } while ((dst = dst->next));
1901}
1902
1903EXPORT_SYMBOL(xfrm_init_pmtu);
1904
1905/* Check that the bundle accepts the flow and its components are
1906 * still valid.
1907 */
1908
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05001909int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
1910 struct flowi *fl, int family, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
1912 struct dst_entry *dst = &first->u.dst;
1913 struct xfrm_dst *last;
1914 u32 mtu;
1915
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07001916 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 (dst->dev && !netif_running(dst->dev)))
1918 return 0;
1919
1920 last = NULL;
1921
1922 do {
1923 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1924
1925 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
1926 return 0;
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06001927 if (fl && pol &&
1928 !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (dst->xfrm->km.state != XFRM_STATE_VALID)
1931 return 0;
David S. Miller9d4a7062006-08-24 03:18:09 -07001932 if (xdst->genid != dst->xfrm->genid)
1933 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07001935 if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
1936 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
1937 return 0;
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 mtu = dst_mtu(dst->child);
1940 if (xdst->child_mtu_cached != mtu) {
1941 last = xdst;
1942 xdst->child_mtu_cached = mtu;
1943 }
1944
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07001945 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return 0;
1947 mtu = dst_mtu(xdst->route);
1948 if (xdst->route_mtu_cached != mtu) {
1949 last = xdst;
1950 xdst->route_mtu_cached = mtu;
1951 }
1952
1953 dst = dst->child;
1954 } while (dst->xfrm);
1955
1956 if (likely(!last))
1957 return 1;
1958
1959 mtu = last->child_mtu_cached;
1960 for (;;) {
1961 dst = &last->u.dst;
1962
1963 mtu = xfrm_state_mtu(dst->xfrm, mtu);
1964 if (mtu > last->route_mtu_cached)
1965 mtu = last->route_mtu_cached;
1966 dst->metrics[RTAX_MTU-1] = mtu;
1967
1968 if (last == first)
1969 break;
1970
1971 last = last->u.next;
1972 last->child_mtu_cached = mtu;
1973 }
1974
1975 return 1;
1976}
1977
1978EXPORT_SYMBOL(xfrm_bundle_ok);
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
1981{
1982 int err = 0;
1983 if (unlikely(afinfo == NULL))
1984 return -EINVAL;
1985 if (unlikely(afinfo->family >= NPROTO))
1986 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07001987 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
1989 err = -ENOBUFS;
1990 else {
1991 struct dst_ops *dst_ops = afinfo->dst_ops;
1992 if (likely(dst_ops->kmem_cachep == NULL))
1993 dst_ops->kmem_cachep = xfrm_dst_cache;
1994 if (likely(dst_ops->check == NULL))
1995 dst_ops->check = xfrm_dst_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (likely(dst_ops->negative_advice == NULL))
1997 dst_ops->negative_advice = xfrm_negative_advice;
1998 if (likely(dst_ops->link_failure == NULL))
1999 dst_ops->link_failure = xfrm_link_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (likely(afinfo->garbage_collect == NULL))
2001 afinfo->garbage_collect = __xfrm_garbage_collect;
2002 xfrm_policy_afinfo[afinfo->family] = afinfo;
2003 }
Ingo Molnare959d812006-04-28 15:32:29 -07002004 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 return err;
2006}
2007EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2008
2009int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2010{
2011 int err = 0;
2012 if (unlikely(afinfo == NULL))
2013 return -EINVAL;
2014 if (unlikely(afinfo->family >= NPROTO))
2015 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002016 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2018 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2019 err = -EINVAL;
2020 else {
2021 struct dst_ops *dst_ops = afinfo->dst_ops;
2022 xfrm_policy_afinfo[afinfo->family] = NULL;
2023 dst_ops->kmem_cachep = NULL;
2024 dst_ops->check = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 dst_ops->negative_advice = NULL;
2026 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 afinfo->garbage_collect = NULL;
2028 }
2029 }
Ingo Molnare959d812006-04-28 15:32:29 -07002030 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 return err;
2032}
2033EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2034
2035static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
2036{
2037 struct xfrm_policy_afinfo *afinfo;
2038 if (unlikely(family >= NPROTO))
2039 return NULL;
2040 read_lock(&xfrm_policy_afinfo_lock);
2041 afinfo = xfrm_policy_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07002042 if (unlikely(!afinfo))
2043 read_unlock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return afinfo;
2045}
2046
2047static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2048{
Herbert Xu546be242006-05-27 23:03:58 -07002049 read_unlock(&xfrm_policy_afinfo_lock);
2050}
2051
2052static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
2053{
2054 struct xfrm_policy_afinfo *afinfo;
2055 if (unlikely(family >= NPROTO))
2056 return NULL;
2057 write_lock_bh(&xfrm_policy_afinfo_lock);
2058 afinfo = xfrm_policy_afinfo[family];
2059 if (unlikely(!afinfo))
2060 write_unlock_bh(&xfrm_policy_afinfo_lock);
2061 return afinfo;
2062}
2063
2064static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
2065{
2066 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
2069static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2070{
2071 switch (event) {
2072 case NETDEV_DOWN:
2073 xfrm_flush_bundles();
2074 }
2075 return NOTIFY_DONE;
2076}
2077
2078static struct notifier_block xfrm_dev_notifier = {
2079 xfrm_dev_event,
2080 NULL,
2081 0
2082};
2083
2084static void __init xfrm_policy_init(void)
2085{
David S. Miller2518c7c2006-08-24 04:45:07 -07002086 unsigned int hmask, sz;
2087 int dir;
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2090 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07002091 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
David S. Miller2518c7c2006-08-24 04:45:07 -07002094 hmask = 8 - 1;
2095 sz = (hmask+1) * sizeof(struct hlist_head);
2096
David S. Miller44e36b42006-08-24 04:50:50 -07002097 xfrm_policy_byidx = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002098 xfrm_idx_hmask = hmask;
2099 if (!xfrm_policy_byidx)
2100 panic("XFRM: failed to allocate byidx hash\n");
2101
2102 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2103 struct xfrm_policy_hash *htab;
2104
2105 INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
2106
2107 htab = &xfrm_policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002108 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002109 htab->hmask = hmask;
2110 if (!htab->table)
2111 panic("XFRM: failed to allocate bydst hash\n");
2112 }
2113
David Howellsc4028952006-11-22 14:57:56 +00002114 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 register_netdevice_notifier(&xfrm_dev_notifier);
2116}
2117
2118void __init xfrm_init(void)
2119{
2120 xfrm_state_init();
2121 xfrm_policy_init();
2122 xfrm_input_init();
2123}
2124