blob: 1bcaae4adf3ae6a222053c77a7b758eab64fd1d8 [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>
25#include <net/xfrm.h>
26#include <net/ip.h>
27
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080028DEFINE_MUTEX(xfrm_cfg_mutex);
29EXPORT_SYMBOL(xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31static DEFINE_RWLOCK(xfrm_policy_lock);
32
33struct xfrm_policy *xfrm_policy_list[XFRM_POLICY_MAX*2];
34EXPORT_SYMBOL(xfrm_policy_list);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -070035#ifdef CONFIG_XFRM_SUB_POLICY
36struct xfrm_policy *xfrm_policy_list_sub[XFRM_POLICY_MAX*2];
37EXPORT_SYMBOL(xfrm_policy_list_sub);
38
39#define XFRM_POLICY_LISTS(type) \
40 ((type == XFRM_POLICY_TYPE_SUB) ? xfrm_policy_list_sub : \
41 xfrm_policy_list)
42#define XFRM_POLICY_LISTHEAD(type, dir) \
43 ((type == XFRM_POLICY_TYPE_SUB) ? xfrm_policy_list_sub[dir] : \
44 xfrm_policy_list[dir])
45#define XFRM_POLICY_LISTHEADP(type, dir) \
46 ((type == XFRM_POLICY_TYPE_SUB) ? &xfrm_policy_list_sub[dir] : \
47 &xfrm_policy_list[dir])
48#else
49#define XFRM_POLICY_LISTS(type) xfrm_policy_list
50#define XFRM_POLICY_LISTHEAD(type, dif) xfrm_policy_list[dir]
51#define XFRM_POLICY_LISTHEADP(type, dif) &xfrm_policy_list[dir]
52#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
55static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
56
Eric Dumazetba899662005-08-26 12:05:31 -070057static kmem_cache_t *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59static struct work_struct xfrm_policy_gc_work;
60static struct list_head xfrm_policy_gc_list =
61 LIST_HEAD_INIT(xfrm_policy_gc_list);
62static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
63
64static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
65static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
Herbert Xu546be242006-05-27 23:03:58 -070066static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family);
67static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69int xfrm_register_type(struct xfrm_type *type, unsigned short family)
70{
Herbert Xu546be242006-05-27 23:03:58 -070071 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
72 struct xfrm_type **typemap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 int err = 0;
74
75 if (unlikely(afinfo == NULL))
76 return -EAFNOSUPPORT;
77 typemap = afinfo->type_map;
78
Herbert Xu546be242006-05-27 23:03:58 -070079 if (likely(typemap[type->proto] == NULL))
80 typemap[type->proto] = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 else
82 err = -EEXIST;
Herbert Xu546be242006-05-27 23:03:58 -070083 xfrm_policy_unlock_afinfo(afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return err;
85}
86EXPORT_SYMBOL(xfrm_register_type);
87
88int xfrm_unregister_type(struct xfrm_type *type, unsigned short family)
89{
Herbert Xu546be242006-05-27 23:03:58 -070090 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
91 struct xfrm_type **typemap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 int err = 0;
93
94 if (unlikely(afinfo == NULL))
95 return -EAFNOSUPPORT;
96 typemap = afinfo->type_map;
97
Herbert Xu546be242006-05-27 23:03:58 -070098 if (unlikely(typemap[type->proto] != type))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 err = -ENOENT;
100 else
Herbert Xu546be242006-05-27 23:03:58 -0700101 typemap[type->proto] = NULL;
102 xfrm_policy_unlock_afinfo(afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return err;
104}
105EXPORT_SYMBOL(xfrm_unregister_type);
106
107struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
108{
109 struct xfrm_policy_afinfo *afinfo;
Herbert Xu546be242006-05-27 23:03:58 -0700110 struct xfrm_type **typemap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 struct xfrm_type *type;
112 int modload_attempted = 0;
113
114retry:
115 afinfo = xfrm_policy_get_afinfo(family);
116 if (unlikely(afinfo == NULL))
117 return NULL;
118 typemap = afinfo->type_map;
119
Herbert Xu546be242006-05-27 23:03:58 -0700120 type = typemap[proto];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (unlikely(type && !try_module_get(type->owner)))
122 type = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (!type && !modload_attempted) {
124 xfrm_policy_put_afinfo(afinfo);
125 request_module("xfrm-type-%d-%d",
126 (int) family, (int) proto);
127 modload_attempted = 1;
128 goto retry;
129 }
130
131 xfrm_policy_put_afinfo(afinfo);
132 return type;
133}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
136 unsigned short family)
137{
138 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
139 int err = 0;
140
141 if (unlikely(afinfo == NULL))
142 return -EAFNOSUPPORT;
143
144 if (likely(afinfo->dst_lookup != NULL))
145 err = afinfo->dst_lookup(dst, fl);
146 else
147 err = -EINVAL;
148 xfrm_policy_put_afinfo(afinfo);
149 return err;
150}
151EXPORT_SYMBOL(xfrm_dst_lookup);
152
153void xfrm_put_type(struct xfrm_type *type)
154{
155 module_put(type->owner);
156}
157
Herbert Xub59f45d2006-05-27 23:05:54 -0700158int xfrm_register_mode(struct xfrm_mode *mode, int family)
159{
160 struct xfrm_policy_afinfo *afinfo;
161 struct xfrm_mode **modemap;
162 int err;
163
164 if (unlikely(mode->encap >= XFRM_MODE_MAX))
165 return -EINVAL;
166
167 afinfo = xfrm_policy_lock_afinfo(family);
168 if (unlikely(afinfo == NULL))
169 return -EAFNOSUPPORT;
170
171 err = -EEXIST;
172 modemap = afinfo->mode_map;
173 if (likely(modemap[mode->encap] == NULL)) {
174 modemap[mode->encap] = mode;
175 err = 0;
176 }
177
178 xfrm_policy_unlock_afinfo(afinfo);
179 return err;
180}
181EXPORT_SYMBOL(xfrm_register_mode);
182
183int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
184{
185 struct xfrm_policy_afinfo *afinfo;
186 struct xfrm_mode **modemap;
187 int err;
188
189 if (unlikely(mode->encap >= XFRM_MODE_MAX))
190 return -EINVAL;
191
192 afinfo = xfrm_policy_lock_afinfo(family);
193 if (unlikely(afinfo == NULL))
194 return -EAFNOSUPPORT;
195
196 err = -ENOENT;
197 modemap = afinfo->mode_map;
198 if (likely(modemap[mode->encap] == mode)) {
199 modemap[mode->encap] = NULL;
200 err = 0;
201 }
202
203 xfrm_policy_unlock_afinfo(afinfo);
204 return err;
205}
206EXPORT_SYMBOL(xfrm_unregister_mode);
207
208struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
209{
210 struct xfrm_policy_afinfo *afinfo;
211 struct xfrm_mode *mode;
212 int modload_attempted = 0;
213
214 if (unlikely(encap >= XFRM_MODE_MAX))
215 return NULL;
216
217retry:
218 afinfo = xfrm_policy_get_afinfo(family);
219 if (unlikely(afinfo == NULL))
220 return NULL;
221
222 mode = afinfo->mode_map[encap];
223 if (unlikely(mode && !try_module_get(mode->owner)))
224 mode = NULL;
225 if (!mode && !modload_attempted) {
226 xfrm_policy_put_afinfo(afinfo);
227 request_module("xfrm-mode-%d-%d", family, encap);
228 modload_attempted = 1;
229 goto retry;
230 }
231
232 xfrm_policy_put_afinfo(afinfo);
233 return mode;
234}
235
236void xfrm_put_mode(struct xfrm_mode *mode)
237{
238 module_put(mode->owner);
239}
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241static inline unsigned long make_jiffies(long secs)
242{
243 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
244 return MAX_SCHEDULE_TIMEOUT-1;
245 else
246 return secs*HZ;
247}
248
249static void xfrm_policy_timer(unsigned long data)
250{
251 struct xfrm_policy *xp = (struct xfrm_policy*)data;
252 unsigned long now = (unsigned long)xtime.tv_sec;
253 long next = LONG_MAX;
254 int warn = 0;
255 int dir;
256
257 read_lock(&xp->lock);
258
259 if (xp->dead)
260 goto out;
261
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700262 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 if (xp->lft.hard_add_expires_seconds) {
265 long tmo = xp->lft.hard_add_expires_seconds +
266 xp->curlft.add_time - now;
267 if (tmo <= 0)
268 goto expired;
269 if (tmo < next)
270 next = tmo;
271 }
272 if (xp->lft.hard_use_expires_seconds) {
273 long tmo = xp->lft.hard_use_expires_seconds +
274 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
275 if (tmo <= 0)
276 goto expired;
277 if (tmo < next)
278 next = tmo;
279 }
280 if (xp->lft.soft_add_expires_seconds) {
281 long tmo = xp->lft.soft_add_expires_seconds +
282 xp->curlft.add_time - now;
283 if (tmo <= 0) {
284 warn = 1;
285 tmo = XFRM_KM_TIMEOUT;
286 }
287 if (tmo < next)
288 next = tmo;
289 }
290 if (xp->lft.soft_use_expires_seconds) {
291 long tmo = xp->lft.soft_use_expires_seconds +
292 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
293 if (tmo <= 0) {
294 warn = 1;
295 tmo = XFRM_KM_TIMEOUT;
296 }
297 if (tmo < next)
298 next = tmo;
299 }
300
301 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800302 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (next != LONG_MAX &&
304 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
305 xfrm_pol_hold(xp);
306
307out:
308 read_unlock(&xp->lock);
309 xfrm_pol_put(xp);
310 return;
311
312expired:
313 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700314 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800315 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 xfrm_pol_put(xp);
317}
318
319
320/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
321 * SPD calls.
322 */
323
Al Virodd0fc662005-10-07 07:46:04 +0100324struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 struct xfrm_policy *policy;
327
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700328 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 if (policy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 atomic_set(&policy->refcnt, 1);
332 rwlock_init(&policy->lock);
333 init_timer(&policy->timer);
334 policy->timer.data = (unsigned long)policy;
335 policy->timer.function = xfrm_policy_timer;
336 }
337 return policy;
338}
339EXPORT_SYMBOL(xfrm_policy_alloc);
340
341/* Destroy xfrm_policy: descendant resources must be released to this moment. */
342
343void __xfrm_policy_destroy(struct xfrm_policy *policy)
344{
Kris Katterjohn09a62662006-01-08 22:24:28 -0800345 BUG_ON(!policy->dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Kris Katterjohn09a62662006-01-08 22:24:28 -0800347 BUG_ON(policy->bundles);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 if (del_timer(&policy->timer))
350 BUG();
351
Trent Jaegerdf718372005-12-13 23:12:27 -0800352 security_xfrm_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 kfree(policy);
354}
355EXPORT_SYMBOL(__xfrm_policy_destroy);
356
357static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
358{
359 struct dst_entry *dst;
360
361 while ((dst = policy->bundles) != NULL) {
362 policy->bundles = dst->next;
363 dst_free(dst);
364 }
365
366 if (del_timer(&policy->timer))
367 atomic_dec(&policy->refcnt);
368
369 if (atomic_read(&policy->refcnt) > 1)
370 flow_cache_flush();
371
372 xfrm_pol_put(policy);
373}
374
375static void xfrm_policy_gc_task(void *data)
376{
377 struct xfrm_policy *policy;
378 struct list_head *entry, *tmp;
379 struct list_head gc_list = LIST_HEAD_INIT(gc_list);
380
381 spin_lock_bh(&xfrm_policy_gc_lock);
382 list_splice_init(&xfrm_policy_gc_list, &gc_list);
383 spin_unlock_bh(&xfrm_policy_gc_lock);
384
385 list_for_each_safe(entry, tmp, &gc_list) {
386 policy = list_entry(entry, struct xfrm_policy, list);
387 xfrm_policy_gc_kill(policy);
388 }
389}
390
391/* Rule must be locked. Release descentant resources, announce
392 * entry dead. The rule must be unlinked from lists to the moment.
393 */
394
395static void xfrm_policy_kill(struct xfrm_policy *policy)
396{
397 int dead;
398
399 write_lock_bh(&policy->lock);
400 dead = policy->dead;
401 policy->dead = 1;
402 write_unlock_bh(&policy->lock);
403
404 if (unlikely(dead)) {
405 WARN_ON(1);
406 return;
407 }
408
409 spin_lock(&xfrm_policy_gc_lock);
410 list_add(&policy->list, &xfrm_policy_gc_list);
411 spin_unlock(&xfrm_policy_gc_lock);
412
413 schedule_work(&xfrm_policy_gc_work);
414}
415
416/* Generate new index... KAME seems to generate them ordered by cost
417 * of an absolute inpredictability of ordering of rules. This will not pass. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700418static u32 xfrm_gen_index(u8 type, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 u32 idx;
421 struct xfrm_policy *p;
422 static u32 idx_generator;
423
424 for (;;) {
425 idx = (idx_generator | dir);
426 idx_generator += 8;
427 if (idx == 0)
428 idx = 8;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700429 for (p = XFRM_POLICY_LISTHEAD(type, dir); p; p = p->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (p->index == idx)
431 break;
432 }
433 if (!p)
434 return idx;
435 }
436}
437
438int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
439{
440 struct xfrm_policy *pol, **p;
441 struct xfrm_policy *delpol = NULL;
442 struct xfrm_policy **newpos = NULL;
David S. Miller9b78a822005-12-22 07:39:48 -0800443 struct dst_entry *gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 write_lock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700446 for (p = XFRM_POLICY_LISTHEADP(policy->type, dir); (pol=*p)!=NULL;) {
Trent Jaegerdf718372005-12-13 23:12:27 -0800447 if (!delpol && memcmp(&policy->selector, &pol->selector, sizeof(pol->selector)) == 0 &&
448 xfrm_sec_ctx_match(pol->security, policy->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (excl) {
450 write_unlock_bh(&xfrm_policy_lock);
451 return -EEXIST;
452 }
453 *p = pol->next;
454 delpol = pol;
455 if (policy->priority > pol->priority)
456 continue;
457 } else if (policy->priority >= pol->priority) {
458 p = &pol->next;
459 continue;
460 }
461 if (!newpos)
462 newpos = p;
463 if (delpol)
464 break;
465 p = &pol->next;
466 }
467 if (newpos)
468 p = newpos;
469 xfrm_pol_hold(policy);
470 policy->next = *p;
471 *p = policy;
472 atomic_inc(&flow_cache_genid);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700473 policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 policy->curlft.add_time = (unsigned long)xtime.tv_sec;
475 policy->curlft.use_time = 0;
476 if (!mod_timer(&policy->timer, jiffies + HZ))
477 xfrm_pol_hold(policy);
478 write_unlock_bh(&xfrm_policy_lock);
479
David S. Miller9b78a822005-12-22 07:39:48 -0800480 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 xfrm_policy_kill(delpol);
David S. Miller9b78a822005-12-22 07:39:48 -0800482
483 read_lock_bh(&xfrm_policy_lock);
484 gc_list = NULL;
485 for (policy = policy->next; policy; policy = policy->next) {
486 struct dst_entry *dst;
487
488 write_lock(&policy->lock);
489 dst = policy->bundles;
490 if (dst) {
491 struct dst_entry *tail = dst;
492 while (tail->next)
493 tail = tail->next;
494 tail->next = gc_list;
495 gc_list = dst;
496
497 policy->bundles = NULL;
498 }
499 write_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
David S. Miller9b78a822005-12-22 07:39:48 -0800501 read_unlock_bh(&xfrm_policy_lock);
502
503 while (gc_list) {
504 struct dst_entry *dst = gc_list;
505
506 gc_list = dst->next;
507 dst_free(dst);
508 }
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return 0;
511}
512EXPORT_SYMBOL(xfrm_policy_insert);
513
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700514struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
515 struct xfrm_selector *sel,
Trent Jaegerdf718372005-12-13 23:12:27 -0800516 struct xfrm_sec_ctx *ctx, int delete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 struct xfrm_policy *pol, **p;
519
520 write_lock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700521 for (p = XFRM_POLICY_LISTHEADP(type, dir); (pol=*p)!=NULL; p = &pol->next) {
Trent Jaegerdf718372005-12-13 23:12:27 -0800522 if ((memcmp(sel, &pol->selector, sizeof(*sel)) == 0) &&
523 (xfrm_sec_ctx_match(ctx, pol->security))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 xfrm_pol_hold(pol);
525 if (delete)
526 *p = pol->next;
527 break;
528 }
529 }
530 write_unlock_bh(&xfrm_policy_lock);
531
532 if (pol && delete) {
533 atomic_inc(&flow_cache_genid);
534 xfrm_policy_kill(pol);
535 }
536 return pol;
537}
Trent Jaegerdf718372005-12-13 23:12:27 -0800538EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700540struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct xfrm_policy *pol, **p;
543
544 write_lock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700545 for (p = XFRM_POLICY_LISTHEADP(type, dir); (pol=*p)!=NULL; p = &pol->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (pol->index == id) {
547 xfrm_pol_hold(pol);
548 if (delete)
549 *p = pol->next;
550 break;
551 }
552 }
553 write_unlock_bh(&xfrm_policy_lock);
554
555 if (pol && delete) {
556 atomic_inc(&flow_cache_genid);
557 xfrm_policy_kill(pol);
558 }
559 return pol;
560}
561EXPORT_SYMBOL(xfrm_policy_byid);
562
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700563void xfrm_policy_flush(u8 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 struct xfrm_policy *xp;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700566 struct xfrm_policy **p_list = XFRM_POLICY_LISTS(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 int dir;
568
569 write_lock_bh(&xfrm_policy_lock);
570 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700571 while ((xp = p_list[dir]) != NULL) {
572 p_list[dir] = xp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 write_unlock_bh(&xfrm_policy_lock);
574
575 xfrm_policy_kill(xp);
576
577 write_lock_bh(&xfrm_policy_lock);
578 }
579 }
580 atomic_inc(&flow_cache_genid);
581 write_unlock_bh(&xfrm_policy_lock);
582}
583EXPORT_SYMBOL(xfrm_policy_flush);
584
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700585int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 void *data)
587{
588 struct xfrm_policy *xp;
589 int dir;
590 int count = 0;
591 int error = 0;
592
593 read_lock_bh(&xfrm_policy_lock);
594 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700595 for (xp = XFRM_POLICY_LISTHEAD(type, dir); xp; xp = xp->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 count++;
597 }
598
599 if (count == 0) {
600 error = -ENOENT;
601 goto out;
602 }
603
604 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700605 for (xp = XFRM_POLICY_LISTHEAD(type, dir); xp; xp = xp->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 error = func(xp, dir%XFRM_POLICY_MAX, --count, data);
607 if (error)
608 goto out;
609 }
610 }
611
612out:
613 read_unlock_bh(&xfrm_policy_lock);
614 return error;
615}
616EXPORT_SYMBOL(xfrm_policy_walk);
617
618/* Find policy to apply to this flow. */
619
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700620static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
621 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 struct xfrm_policy *pol;
624
625 read_lock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700626 for (pol = XFRM_POLICY_LISTHEAD(type, dir); pol; pol = pol->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 struct xfrm_selector *sel = &pol->selector;
628 int match;
629
630 if (pol->family != family)
631 continue;
632
633 match = xfrm_selector_match(sel, fl, family);
Trent Jaegerdf718372005-12-13 23:12:27 -0800634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (match) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700636 if (!security_xfrm_policy_lookup(pol, fl->secid, dir)) {
Trent Jaegerdf718372005-12-13 23:12:27 -0800637 xfrm_pol_hold(pol);
638 break;
639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641 }
642 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700643
644 return pol;
645}
646
647static void xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
648 void **objp, atomic_t **obj_refp)
649{
650 struct xfrm_policy *pol;
651
652#ifdef CONFIG_XFRM_SUB_POLICY
653 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
654 if (pol)
655 goto end;
656#endif
657 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
658
659#ifdef CONFIG_XFRM_SUB_POLICY
660 end:
661#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if ((*objp = (void *) pol) != NULL)
663 *obj_refp = &pol->refcnt;
664}
665
Trent Jaegerdf718372005-12-13 23:12:27 -0800666static inline int policy_to_flow_dir(int dir)
667{
668 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
669 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
670 XFRM_POLICY_FWD == FLOW_DIR_FWD)
671 return dir;
672 switch (dir) {
673 default:
674 case XFRM_POLICY_IN:
675 return FLOW_DIR_IN;
676 case XFRM_POLICY_OUT:
677 return FLOW_DIR_OUT;
678 case XFRM_POLICY_FWD:
679 return FLOW_DIR_FWD;
680 };
681}
682
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700683static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 struct xfrm_policy *pol;
686
687 read_lock_bh(&xfrm_policy_lock);
688 if ((pol = sk->sk_policy[dir]) != NULL) {
Trent Jaegerdf718372005-12-13 23:12:27 -0800689 int match = xfrm_selector_match(&pol->selector, fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 sk->sk_family);
Trent Jaegerdf718372005-12-13 23:12:27 -0800691 int err = 0;
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (match)
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700694 err = security_xfrm_policy_lookup(pol, fl->secid, policy_to_flow_dir(dir));
Trent Jaegerdf718372005-12-13 23:12:27 -0800695
696 if (match && !err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 xfrm_pol_hold(pol);
698 else
699 pol = NULL;
700 }
701 read_unlock_bh(&xfrm_policy_lock);
702 return pol;
703}
704
705static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
706{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700707 struct xfrm_policy **p_list = XFRM_POLICY_LISTS(pol->type);
708
709 pol->next = p_list[dir];
710 p_list[dir] = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 xfrm_pol_hold(pol);
712}
713
714static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
715 int dir)
716{
717 struct xfrm_policy **polp;
718
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700719 for (polp = XFRM_POLICY_LISTHEADP(pol->type, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 *polp != NULL; polp = &(*polp)->next) {
721 if (*polp == pol) {
722 *polp = pol->next;
723 return pol;
724 }
725 }
726 return NULL;
727}
728
Herbert Xu4666faa2005-06-18 22:43:22 -0700729int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 write_lock_bh(&xfrm_policy_lock);
732 pol = __xfrm_policy_unlink(pol, dir);
733 write_unlock_bh(&xfrm_policy_lock);
734 if (pol) {
735 if (dir < XFRM_POLICY_MAX)
736 atomic_inc(&flow_cache_genid);
737 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -0700738 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
Herbert Xu4666faa2005-06-18 22:43:22 -0700740 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
David S. Millera70fcb02006-03-20 19:18:52 -0800742EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
745{
746 struct xfrm_policy *old_pol;
747
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700748#ifdef CONFIG_XFRM_SUB_POLICY
749 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
750 return -EINVAL;
751#endif
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 write_lock_bh(&xfrm_policy_lock);
754 old_pol = sk->sk_policy[dir];
755 sk->sk_policy[dir] = pol;
756 if (pol) {
757 pol->curlft.add_time = (unsigned long)xtime.tv_sec;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700758 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
760 }
761 if (old_pol)
762 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
763 write_unlock_bh(&xfrm_policy_lock);
764
765 if (old_pol) {
766 xfrm_policy_kill(old_pol);
767 }
768 return 0;
769}
770
771static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
772{
773 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
774
775 if (newp) {
776 newp->selector = old->selector;
Trent Jaegerdf718372005-12-13 23:12:27 -0800777 if (security_xfrm_policy_clone(old, newp)) {
778 kfree(newp);
779 return NULL; /* ENOMEM */
780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 newp->lft = old->lft;
782 newp->curlft = old->curlft;
783 newp->action = old->action;
784 newp->flags = old->flags;
785 newp->xfrm_nr = old->xfrm_nr;
786 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700787 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 memcpy(newp->xfrm_vec, old->xfrm_vec,
789 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
790 write_lock_bh(&xfrm_policy_lock);
791 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
792 write_unlock_bh(&xfrm_policy_lock);
793 xfrm_pol_put(newp);
794 }
795 return newp;
796}
797
798int __xfrm_sk_clone_policy(struct sock *sk)
799{
800 struct xfrm_policy *p0 = sk->sk_policy[0],
801 *p1 = sk->sk_policy[1];
802
803 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
804 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
805 return -ENOMEM;
806 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
807 return -ENOMEM;
808 return 0;
809}
810
811/* Resolve list of templates for the flow, given policy. */
812
813static int
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700814xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
815 struct xfrm_state **xfrm,
816 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
818 int nx;
819 int i, error;
820 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
821 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
822
823 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
824 struct xfrm_state *x;
825 xfrm_address_t *remote = daddr;
826 xfrm_address_t *local = saddr;
827 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
828
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700829 if (tmpl->mode == XFRM_MODE_TUNNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 remote = &tmpl->id.daddr;
831 local = &tmpl->saddr;
832 }
833
834 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
835
836 if (x && x->km.state == XFRM_STATE_VALID) {
837 xfrm[nx++] = x;
838 daddr = remote;
839 saddr = local;
840 continue;
841 }
842 if (x) {
843 error = (x->km.state == XFRM_STATE_ERROR ?
844 -EINVAL : -EAGAIN);
845 xfrm_state_put(x);
846 }
847
848 if (!tmpl->optional)
849 goto fail;
850 }
851 return nx;
852
853fail:
854 for (nx--; nx>=0; nx--)
855 xfrm_state_put(xfrm[nx]);
856 return error;
857}
858
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700859static int
860xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
861 struct xfrm_state **xfrm,
862 unsigned short family)
863{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -0700864 struct xfrm_state *tp[XFRM_MAX_DEPTH];
865 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700866 int cnx = 0;
867 int error;
868 int ret;
869 int i;
870
871 for (i = 0; i < npols; i++) {
872 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
873 error = -ENOBUFS;
874 goto fail;
875 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -0700876
877 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700878 if (ret < 0) {
879 error = ret;
880 goto fail;
881 } else
882 cnx += ret;
883 }
884
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -0700885 /* found states are sorted for outbound processing */
886 if (npols > 1)
887 xfrm_state_sort(xfrm, tpp, cnx, family);
888
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700889 return cnx;
890
891 fail:
892 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -0700893 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700894 return error;
895
896}
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898/* Check that the bundle accepts the flow and its components are
899 * still valid.
900 */
901
902static struct dst_entry *
903xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
904{
905 struct dst_entry *x;
906 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
907 if (unlikely(afinfo == NULL))
908 return ERR_PTR(-EINVAL);
909 x = afinfo->find_bundle(fl, policy);
910 xfrm_policy_put_afinfo(afinfo);
911 return x;
912}
913
914/* Allocate chain of dst_entry's, attach known xfrm's, calculate
915 * all the metrics... Shortly, bundle a bundle.
916 */
917
918static int
919xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
920 struct flowi *fl, struct dst_entry **dst_p,
921 unsigned short family)
922{
923 int err;
924 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
925 if (unlikely(afinfo == NULL))
926 return -EINVAL;
927 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
928 xfrm_policy_put_afinfo(afinfo);
929 return err;
930}
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933static int stale_bundle(struct dst_entry *dst);
934
935/* Main function: finds/creates a bundle for given flow.
936 *
937 * At the moment we eat a raw IP route. Mostly to speed up lookups
938 * on interfaces with disabled IPsec.
939 */
940int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
941 struct sock *sk, int flags)
942{
943 struct xfrm_policy *policy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700944 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
945 int npols;
946 int pol_dead;
947 int xfrm_nr;
948 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
950 struct dst_entry *dst, *dst_orig = *dst_p;
951 int nx = 0;
952 int err;
953 u32 genid;
Patrick McHardy42cf93c2006-02-21 13:37:35 -0800954 u16 family;
Trent Jaegerdf718372005-12-13 23:12:27 -0800955 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957restart:
958 genid = atomic_read(&flow_cache_genid);
959 policy = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700960 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
961 pols[pi] = NULL;
962 npols = 0;
963 pol_dead = 0;
964 xfrm_nr = 0;
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (sk && sk->sk_policy[1])
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700967 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 if (!policy) {
970 /* To accelerate a bit... */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700971 if ((dst_orig->flags & DST_NOXFRM) || xfrm_policy_lists_empty(XFRM_POLICY_OUT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return 0;
973
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700974 policy = flow_cache_lookup(fl, dst_orig->ops->family,
Patrick McHardy42cf93c2006-02-21 13:37:35 -0800975 dir, xfrm_policy_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977
978 if (!policy)
979 return 0;
980
Patrick McHardy42cf93c2006-02-21 13:37:35 -0800981 family = dst_orig->ops->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 policy->curlft.use_time = (unsigned long)xtime.tv_sec;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700983 pols[0] = policy;
984 npols ++;
985 xfrm_nr += pols[0]->xfrm_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 switch (policy->action) {
988 case XFRM_POLICY_BLOCK:
989 /* Prohibit the flow */
Patrick McHardye104411b2005-09-08 15:11:55 -0700990 err = -EPERM;
991 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 case XFRM_POLICY_ALLOW:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700994#ifndef CONFIG_XFRM_SUB_POLICY
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (policy->xfrm_nr == 0) {
996 /* Flow passes not transformed. */
997 xfrm_pol_put(policy);
998 return 0;
999 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001000#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 /* Try to find matching bundle.
1003 *
1004 * LATER: help from flow cache. It is optional, this
1005 * is required only for output policy.
1006 */
1007 dst = xfrm_find_bundle(fl, policy, family);
1008 if (IS_ERR(dst)) {
Patrick McHardye104411b2005-09-08 15:11:55 -07001009 err = PTR_ERR(dst);
1010 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012
1013 if (dst)
1014 break;
1015
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001016#ifdef CONFIG_XFRM_SUB_POLICY
1017 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1018 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1019 fl, family,
1020 XFRM_POLICY_OUT);
1021 if (pols[1]) {
1022 if (pols[1]->action == XFRM_POLICY_BLOCK) {
1023 err = -EPERM;
1024 goto error;
1025 }
1026 npols ++;
1027 xfrm_nr += pols[1]->xfrm_nr;
1028 }
1029 }
1030
1031 /*
1032 * Because neither flowi nor bundle information knows about
1033 * transformation template size. On more than one policy usage
1034 * we can realize whether all of them is bypass or not after
1035 * they are searched. See above not-transformed bypass
1036 * is surrounded by non-sub policy configuration, too.
1037 */
1038 if (xfrm_nr == 0) {
1039 /* Flow passes not transformed. */
1040 xfrm_pols_put(pols, npols);
1041 return 0;
1042 }
1043
1044#endif
1045 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 if (unlikely(nx<0)) {
1048 err = nx;
1049 if (err == -EAGAIN && flags) {
1050 DECLARE_WAITQUEUE(wait, current);
1051
1052 add_wait_queue(&km_waitq, &wait);
1053 set_current_state(TASK_INTERRUPTIBLE);
1054 schedule();
1055 set_current_state(TASK_RUNNING);
1056 remove_wait_queue(&km_waitq, &wait);
1057
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001058 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 if (nx == -EAGAIN && signal_pending(current)) {
1061 err = -ERESTART;
1062 goto error;
1063 }
1064 if (nx == -EAGAIN ||
1065 genid != atomic_read(&flow_cache_genid)) {
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001066 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 goto restart;
1068 }
1069 err = nx;
1070 }
1071 if (err < 0)
1072 goto error;
1073 }
1074 if (nx == 0) {
1075 /* Flow passes not transformed. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001076 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return 0;
1078 }
1079
1080 dst = dst_orig;
1081 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
1082
1083 if (unlikely(err)) {
1084 int i;
1085 for (i=0; i<nx; i++)
1086 xfrm_state_put(xfrm[i]);
1087 goto error;
1088 }
1089
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001090 for (pi = 0; pi < npols; pi++) {
1091 read_lock_bh(&pols[pi]->lock);
1092 pol_dead |= pols[pi]->dead;
1093 read_unlock_bh(&pols[pi]->lock);
1094 }
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 write_lock_bh(&policy->lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001097 if (unlikely(pol_dead || stale_bundle(dst))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /* Wow! While we worked on resolving, this
1099 * policy has gone. Retry. It is not paranoia,
1100 * we just cannot enlist new bundle to dead object.
1101 * We can't enlist stable bundles either.
1102 */
1103 write_unlock_bh(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (dst)
1105 dst_free(dst);
Herbert Xu00de6512006-02-13 16:01:27 -08001106
1107 err = -EHOSTUNREACH;
1108 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110 dst->next = policy->bundles;
1111 policy->bundles = dst;
1112 dst_hold(dst);
1113 write_unlock_bh(&policy->lock);
1114 }
1115 *dst_p = dst;
1116 dst_release(dst_orig);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001117 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return 0;
1119
1120error:
1121 dst_release(dst_orig);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001122 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 *dst_p = NULL;
1124 return err;
1125}
1126EXPORT_SYMBOL(xfrm_lookup);
1127
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001128static inline int
1129xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1130{
1131 struct xfrm_state *x;
1132 int err;
1133
1134 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1135 return 0;
1136 x = skb->sp->xvec[idx];
1137 if (!x->type->reject)
1138 return 0;
1139 xfrm_state_hold(x);
1140 err = x->type->reject(x, skb, fl);
1141 xfrm_state_put(x);
1142 return err;
1143}
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145/* When skb is transformed back to its "native" form, we have to
1146 * check policy restrictions. At the moment we make this in maximally
1147 * stupid way. Shame on me. :-) Of course, connected sockets must
1148 * have policy cached at them.
1149 */
1150
1151static inline int
1152xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
1153 unsigned short family)
1154{
1155 if (xfrm_state_kern(x))
1156 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family);
1157 return x->id.proto == tmpl->id.proto &&
1158 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1159 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1160 x->props.mode == tmpl->mode &&
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07001161 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1162 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001163 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1164 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001167/*
1168 * 0 or more than 0 is returned when validation is succeeded (either bypass
1169 * because of optional transport mode, or next index of the mathced secpath
1170 * state with the template.
1171 * -1 is returned when no matching template is found.
1172 * Otherwise "-2 - errored_index" is returned.
1173 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174static inline int
1175xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1176 unsigned short family)
1177{
1178 int idx = start;
1179
1180 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001181 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return start;
1183 } else
1184 start = -1;
1185 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001186 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001188 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1189 if (start == -1)
1190 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
1194 return start;
1195}
1196
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001197int
1198xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
1200 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001201 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 if (unlikely(afinfo == NULL))
1204 return -EAFNOSUPPORT;
1205
1206 afinfo->decode_session(skb, fl);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07001207 err = security_xfrm_decode_session(skb, &fl->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001209 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001211EXPORT_SYMBOL(xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001213static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
1215 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001216 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
1217 if (idxp)
1218 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222
1223 return 0;
1224}
1225
1226int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
1227 unsigned short family)
1228{
1229 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001230 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1231 int npols = 0;
1232 int xfrm_nr;
1233 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 struct flowi fl;
Trent Jaegerdf718372005-12-13 23:12:27 -08001235 u8 fl_dir = policy_to_flow_dir(dir);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001236 int xerr_idx = -1;
1237 int *xerr_idxp = &xerr_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001239 if (xfrm_decode_session(skb, &fl, family) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return 0;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08001241 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 /* First, check used SA against their selectors. */
1244 if (skb->sp) {
1245 int i;
1246
1247 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001248 struct xfrm_state *x = skb->sp->xvec[i];
1249 if (!xfrm_selector_match(&x->sel, &fl, family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252 }
1253
1254 pol = NULL;
1255 if (sk && sk->sk_policy[dir])
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001256 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 if (!pol)
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001259 pol = flow_cache_lookup(&fl, family, fl_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 xfrm_policy_lookup);
1261
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001262 if (!pol) {
1263 if (skb->sp && secpath_has_nontransport(skb->sp, 0, xerr_idxp)) {
1264 xfrm_secpath_reject(xerr_idx, skb, &fl);
1265 return 0;
1266 }
1267 return 1;
1268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 pol->curlft.use_time = (unsigned long)xtime.tv_sec;
1271
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001272 pols[0] = pol;
1273 npols ++;
1274#ifdef CONFIG_XFRM_SUB_POLICY
1275 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1276 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1277 &fl, family,
1278 XFRM_POLICY_IN);
1279 if (pols[1]) {
1280 pols[1]->curlft.use_time = (unsigned long)xtime.tv_sec;
1281 npols ++;
1282 }
1283 }
1284#endif
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (pol->action == XFRM_POLICY_ALLOW) {
1287 struct sec_path *sp;
1288 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001289 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001290 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001291 struct xfrm_tmpl **tpp = tp;
1292 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 int i, k;
1294
1295 if ((sp = skb->sp) == NULL)
1296 sp = &dummy;
1297
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001298 for (pi = 0; pi < npols; pi++) {
1299 if (pols[pi] != pol &&
1300 pols[pi]->action != XFRM_POLICY_ALLOW)
1301 goto reject;
1302 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
1303 goto reject_error;
1304 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1305 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1306 }
1307 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001308 if (npols > 1) {
1309 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1310 tpp = stp;
1311 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /* For each tunnel xfrm, find the first matching tmpl.
1314 * For each tmpl before that, find corresponding xfrm.
1315 * Order is _important_. Later we will implement
1316 * some barriers, but at the moment barriers
1317 * are implied between each two transformations.
1318 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001319 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1320 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001321 if (k < 0) {
1322 if (k < -1 && xerr_idxp)
1323 *xerr_idxp = -(2+k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001328 if (secpath_has_nontransport(sp, k, xerr_idxp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 goto reject;
1330
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001331 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return 1;
1333 }
1334
1335reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001336 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001337reject_error:
1338 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return 0;
1340}
1341EXPORT_SYMBOL(__xfrm_policy_check);
1342
1343int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1344{
1345 struct flowi fl;
1346
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001347 if (xfrm_decode_session(skb, &fl, family) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return 0;
1349
1350 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1351}
1352EXPORT_SYMBOL(__xfrm_route_forward);
1353
David S. Millerd49c73c2006-08-13 18:55:53 -07001354/* Optimize later using cookies and generation ids. */
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1357{
David S. Millerd49c73c2006-08-13 18:55:53 -07001358 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1359 * to "-1" to force all XFRM destinations to get validated by
1360 * dst_ops->check on every use. We do this because when a
1361 * normal route referenced by an XFRM dst is obsoleted we do
1362 * not go looking around for all parent referencing XFRM dsts
1363 * so that we can invalidate them. It is just too much work.
1364 * Instead we make the checks here on every use. For example:
1365 *
1366 * XFRM dst A --> IPv4 dst X
1367 *
1368 * X is the "xdst->route" of A (X is also the "dst->path" of A
1369 * in this example). If X is marked obsolete, "A" will not
1370 * notice. That's what we are validating here via the
1371 * stale_bundle() check.
1372 *
1373 * When a policy's bundle is pruned, we dst_free() the XFRM
1374 * dst which causes it's ->obsolete field to be set to a
1375 * positive non-zero integer. If an XFRM dst has been pruned
1376 * like this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08001377 */
David S. Millerd49c73c2006-08-13 18:55:53 -07001378 if (dst->obsolete < 0 && !stale_bundle(dst))
1379 return dst;
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return NULL;
1382}
1383
1384static int stale_bundle(struct dst_entry *dst)
1385{
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07001386 return !xfrm_bundle_ok((struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
Herbert Xuaabc9762005-05-03 16:27:10 -07001389void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
1392 dst->dev = &loopback_dev;
1393 dev_hold(&loopback_dev);
1394 dev_put(dev);
1395 }
1396}
Herbert Xuaabc9762005-05-03 16:27:10 -07001397EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399static void xfrm_link_failure(struct sk_buff *skb)
1400{
1401 /* Impossible. Such dst must be popped before reaches point of failure. */
1402 return;
1403}
1404
1405static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1406{
1407 if (dst) {
1408 if (dst->obsolete) {
1409 dst_release(dst);
1410 dst = NULL;
1411 }
1412 }
1413 return dst;
1414}
1415
1416static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1417{
1418 int i;
1419 struct xfrm_policy *pol;
1420 struct dst_entry *dst, **dstp, *gc_list = NULL;
1421
1422 read_lock_bh(&xfrm_policy_lock);
1423 for (i=0; i<2*XFRM_POLICY_MAX; i++) {
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001424#ifdef CONFIG_XFRM_SUB_POLICY
1425 for (pol = xfrm_policy_list_sub[i]; pol; pol = pol->next) {
1426 write_lock(&pol->lock);
1427 dstp = &pol->bundles;
1428 while ((dst=*dstp) != NULL) {
1429 if (func(dst)) {
1430 *dstp = dst->next;
1431 dst->next = gc_list;
1432 gc_list = dst;
1433 } else {
1434 dstp = &dst->next;
1435 }
1436 }
1437 write_unlock(&pol->lock);
1438 }
1439
1440#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 for (pol = xfrm_policy_list[i]; pol; pol = pol->next) {
1442 write_lock(&pol->lock);
1443 dstp = &pol->bundles;
1444 while ((dst=*dstp) != NULL) {
1445 if (func(dst)) {
1446 *dstp = dst->next;
1447 dst->next = gc_list;
1448 gc_list = dst;
1449 } else {
1450 dstp = &dst->next;
1451 }
1452 }
1453 write_unlock(&pol->lock);
1454 }
1455 }
1456 read_unlock_bh(&xfrm_policy_lock);
1457
1458 while (gc_list) {
1459 dst = gc_list;
1460 gc_list = dst->next;
1461 dst_free(dst);
1462 }
1463}
1464
1465static int unused_bundle(struct dst_entry *dst)
1466{
1467 return !atomic_read(&dst->__refcnt);
1468}
1469
1470static void __xfrm_garbage_collect(void)
1471{
1472 xfrm_prune_bundles(unused_bundle);
1473}
1474
David S. Miller1c095392006-08-24 03:30:28 -07001475static int xfrm_flush_bundles(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
1477 xfrm_prune_bundles(stale_bundle);
1478 return 0;
1479}
1480
1481void xfrm_init_pmtu(struct dst_entry *dst)
1482{
1483 do {
1484 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1485 u32 pmtu, route_mtu_cached;
1486
1487 pmtu = dst_mtu(dst->child);
1488 xdst->child_mtu_cached = pmtu;
1489
1490 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
1491
1492 route_mtu_cached = dst_mtu(xdst->route);
1493 xdst->route_mtu_cached = route_mtu_cached;
1494
1495 if (pmtu > route_mtu_cached)
1496 pmtu = route_mtu_cached;
1497
1498 dst->metrics[RTAX_MTU-1] = pmtu;
1499 } while ((dst = dst->next));
1500}
1501
1502EXPORT_SYMBOL(xfrm_init_pmtu);
1503
1504/* Check that the bundle accepts the flow and its components are
1505 * still valid.
1506 */
1507
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07001508int xfrm_bundle_ok(struct xfrm_dst *first, struct flowi *fl, int family, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 struct dst_entry *dst = &first->u.dst;
1511 struct xfrm_dst *last;
1512 u32 mtu;
1513
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07001514 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 (dst->dev && !netif_running(dst->dev)))
1516 return 0;
1517
1518 last = NULL;
1519
1520 do {
1521 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1522
1523 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
1524 return 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001525 if (fl && !security_xfrm_flow_state_match(fl, dst->xfrm))
1526 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (dst->xfrm->km.state != XFRM_STATE_VALID)
1528 return 0;
David S. Miller9d4a7062006-08-24 03:18:09 -07001529 if (xdst->genid != dst->xfrm->genid)
1530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07001532 if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
1533 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
1534 return 0;
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 mtu = dst_mtu(dst->child);
1537 if (xdst->child_mtu_cached != mtu) {
1538 last = xdst;
1539 xdst->child_mtu_cached = mtu;
1540 }
1541
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07001542 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return 0;
1544 mtu = dst_mtu(xdst->route);
1545 if (xdst->route_mtu_cached != mtu) {
1546 last = xdst;
1547 xdst->route_mtu_cached = mtu;
1548 }
1549
1550 dst = dst->child;
1551 } while (dst->xfrm);
1552
1553 if (likely(!last))
1554 return 1;
1555
1556 mtu = last->child_mtu_cached;
1557 for (;;) {
1558 dst = &last->u.dst;
1559
1560 mtu = xfrm_state_mtu(dst->xfrm, mtu);
1561 if (mtu > last->route_mtu_cached)
1562 mtu = last->route_mtu_cached;
1563 dst->metrics[RTAX_MTU-1] = mtu;
1564
1565 if (last == first)
1566 break;
1567
1568 last = last->u.next;
1569 last->child_mtu_cached = mtu;
1570 }
1571
1572 return 1;
1573}
1574
1575EXPORT_SYMBOL(xfrm_bundle_ok);
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
1578{
1579 int err = 0;
1580 if (unlikely(afinfo == NULL))
1581 return -EINVAL;
1582 if (unlikely(afinfo->family >= NPROTO))
1583 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07001584 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
1586 err = -ENOBUFS;
1587 else {
1588 struct dst_ops *dst_ops = afinfo->dst_ops;
1589 if (likely(dst_ops->kmem_cachep == NULL))
1590 dst_ops->kmem_cachep = xfrm_dst_cache;
1591 if (likely(dst_ops->check == NULL))
1592 dst_ops->check = xfrm_dst_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (likely(dst_ops->negative_advice == NULL))
1594 dst_ops->negative_advice = xfrm_negative_advice;
1595 if (likely(dst_ops->link_failure == NULL))
1596 dst_ops->link_failure = xfrm_link_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (likely(afinfo->garbage_collect == NULL))
1598 afinfo->garbage_collect = __xfrm_garbage_collect;
1599 xfrm_policy_afinfo[afinfo->family] = afinfo;
1600 }
Ingo Molnare959d812006-04-28 15:32:29 -07001601 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return err;
1603}
1604EXPORT_SYMBOL(xfrm_policy_register_afinfo);
1605
1606int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
1607{
1608 int err = 0;
1609 if (unlikely(afinfo == NULL))
1610 return -EINVAL;
1611 if (unlikely(afinfo->family >= NPROTO))
1612 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07001613 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
1615 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
1616 err = -EINVAL;
1617 else {
1618 struct dst_ops *dst_ops = afinfo->dst_ops;
1619 xfrm_policy_afinfo[afinfo->family] = NULL;
1620 dst_ops->kmem_cachep = NULL;
1621 dst_ops->check = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 dst_ops->negative_advice = NULL;
1623 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 afinfo->garbage_collect = NULL;
1625 }
1626 }
Ingo Molnare959d812006-04-28 15:32:29 -07001627 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return err;
1629}
1630EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
1631
1632static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
1633{
1634 struct xfrm_policy_afinfo *afinfo;
1635 if (unlikely(family >= NPROTO))
1636 return NULL;
1637 read_lock(&xfrm_policy_afinfo_lock);
1638 afinfo = xfrm_policy_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07001639 if (unlikely(!afinfo))
1640 read_unlock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return afinfo;
1642}
1643
1644static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
1645{
Herbert Xu546be242006-05-27 23:03:58 -07001646 read_unlock(&xfrm_policy_afinfo_lock);
1647}
1648
1649static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
1650{
1651 struct xfrm_policy_afinfo *afinfo;
1652 if (unlikely(family >= NPROTO))
1653 return NULL;
1654 write_lock_bh(&xfrm_policy_afinfo_lock);
1655 afinfo = xfrm_policy_afinfo[family];
1656 if (unlikely(!afinfo))
1657 write_unlock_bh(&xfrm_policy_afinfo_lock);
1658 return afinfo;
1659}
1660
1661static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
1662{
1663 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
1666static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
1667{
1668 switch (event) {
1669 case NETDEV_DOWN:
1670 xfrm_flush_bundles();
1671 }
1672 return NOTIFY_DONE;
1673}
1674
1675static struct notifier_block xfrm_dev_notifier = {
1676 xfrm_dev_event,
1677 NULL,
1678 0
1679};
1680
1681static void __init xfrm_policy_init(void)
1682{
1683 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
1684 sizeof(struct xfrm_dst),
1685 0, SLAB_HWCACHE_ALIGN,
1686 NULL, NULL);
1687 if (!xfrm_dst_cache)
1688 panic("XFRM: failed to allocate xfrm_dst_cache\n");
1689
1690 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task, NULL);
1691 register_netdevice_notifier(&xfrm_dev_notifier);
1692}
1693
1694void __init xfrm_init(void)
1695{
1696 xfrm_state_init();
1697 xfrm_policy_init();
1698 xfrm_input_init();
1699}
1700