]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/xfrm/xfrm_policy.c
[PATCH] slab: remove kmem_cache_t
[mirror_ubuntu-artful-kernel.git] / net / xfrm / xfrm_policy.c
CommitLineData
1da177e4
LT
1/*
2 * xfrm_policy.c
3 *
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * Kazunori MIYAZAWA @USAGI
10 * YOSHIFUJI Hideaki
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
df71837d 13 *
1da177e4
LT
14 */
15
1da177e4
LT
16#include <linux/slab.h>
17#include <linux/kmod.h>
18#include <linux/list.h>
19#include <linux/spinlock.h>
20#include <linux/workqueue.h>
21#include <linux/notifier.h>
22#include <linux/netdevice.h>
eb9c7ebe 23#include <linux/netfilter.h>
1da177e4 24#include <linux/module.h>
2518c7c2 25#include <linux/cache.h>
1da177e4
LT
26#include <net/xfrm.h>
27#include <net/ip.h>
28
44e36b42
DM
29#include "xfrm_hash.h"
30
4a3e2f71
AV
31DEFINE_MUTEX(xfrm_cfg_mutex);
32EXPORT_SYMBOL(xfrm_cfg_mutex);
1da177e4
LT
33
34static DEFINE_RWLOCK(xfrm_policy_lock);
35
2518c7c2
DM
36unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
37EXPORT_SYMBOL(xfrm_policy_count);
1da177e4
LT
38
39static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
40static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
41
e18b890b 42static struct kmem_cache *xfrm_dst_cache __read_mostly;
1da177e4
LT
43
44static struct work_struct xfrm_policy_gc_work;
2518c7c2 45static HLIST_HEAD(xfrm_policy_gc_list);
1da177e4
LT
46static 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);
546be240
HX
50static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family);
51static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo);
1da177e4 52
77681021
AM
53static 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
1da177e4
LT
87int xfrm_register_type(struct xfrm_type *type, unsigned short family)
88{
546be240
HX
89 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
90 struct xfrm_type **typemap;
1da177e4
LT
91 int err = 0;
92
93 if (unlikely(afinfo == NULL))
94 return -EAFNOSUPPORT;
95 typemap = afinfo->type_map;
96
546be240
HX
97 if (likely(typemap[type->proto] == NULL))
98 typemap[type->proto] = type;
1da177e4
LT
99 else
100 err = -EEXIST;
546be240 101 xfrm_policy_unlock_afinfo(afinfo);
1da177e4
LT
102 return err;
103}
104EXPORT_SYMBOL(xfrm_register_type);
105
106int xfrm_unregister_type(struct xfrm_type *type, unsigned short family)
107{
546be240
HX
108 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
109 struct xfrm_type **typemap;
1da177e4
LT
110 int err = 0;
111
112 if (unlikely(afinfo == NULL))
113 return -EAFNOSUPPORT;
114 typemap = afinfo->type_map;
115
546be240 116 if (unlikely(typemap[type->proto] != type))
1da177e4
LT
117 err = -ENOENT;
118 else
546be240
HX
119 typemap[type->proto] = NULL;
120 xfrm_policy_unlock_afinfo(afinfo);
1da177e4
LT
121 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;
546be240 128 struct xfrm_type **typemap;
1da177e4
LT
129 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
546be240 138 type = typemap[proto];
1da177e4
LT
139 if (unlikely(type && !try_module_get(type->owner)))
140 type = NULL;
1da177e4
LT
141 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}
1da177e4
LT
152
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
b59f45d0
HX
176int 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
1da177e4
LT
259static 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
77d8d7a6 280 dir = xfrm_policy_id2dir(xp->index);
1da177e4
LT
281
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)
6c5c8ca7 320 km_policy_expired(xp, dir, 0, 0);
1da177e4
LT
321 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);
4666faab 332 if (!xfrm_policy_delete(xp, dir))
6c5c8ca7 333 km_policy_expired(xp, dir, 1, 0);
1da177e4
LT
334 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
dd0fc66f 342struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
1da177e4
LT
343{
344 struct xfrm_policy *policy;
345
0da974f4 346 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
1da177e4
LT
347
348 if (policy) {
2518c7c2
DM
349 INIT_HLIST_NODE(&policy->bydst);
350 INIT_HLIST_NODE(&policy->byidx);
1da177e4 351 rwlock_init(&policy->lock);
2518c7c2 352 atomic_set(&policy->refcnt, 1);
1da177e4
LT
353 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{
09a62660 365 BUG_ON(!policy->dead);
1da177e4 366
09a62660 367 BUG_ON(policy->bundles);
1da177e4
LT
368
369 if (del_timer(&policy->timer))
370 BUG();
371
df71837d 372 security_xfrm_policy_free(policy);
1da177e4
LT
373 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
c4028958 395static void xfrm_policy_gc_task(struct work_struct *work)
1da177e4
LT
396{
397 struct xfrm_policy *policy;
2518c7c2
DM
398 struct hlist_node *entry, *tmp;
399 struct hlist_head gc_list;
1da177e4
LT
400
401 spin_lock_bh(&xfrm_policy_gc_lock);
2518c7c2
DM
402 gc_list.first = xfrm_policy_gc_list.first;
403 INIT_HLIST_HEAD(&xfrm_policy_gc_list);
1da177e4
LT
404 spin_unlock_bh(&xfrm_policy_gc_lock);
405
2518c7c2 406 hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
1da177e4 407 xfrm_policy_gc_kill(policy);
1da177e4
LT
408}
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);
2518c7c2 429 hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
1da177e4
LT
430 spin_unlock(&xfrm_policy_gc_lock);
431
432 schedule_work(&xfrm_policy_gc_work);
433}
434
2518c7c2
DM
435struct 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
2518c7c2
DM
446static inline unsigned int idx_hash(u32 index)
447{
448 return __idx_hash(index, xfrm_idx_hmask);
449}
450
2518c7c2
DM
451static 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
2518c7c2
DM
469static 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;
44e36b42 511 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
2518c7c2
DM
512 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
44e36b42 527 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
2518c7c2
DM
528}
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;
44e36b42 536 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
2518c7c2
DM
537 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
44e36b42 552 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
2518c7c2
DM
553}
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
c4028958 583static void xfrm_hash_resize(struct work_struct *__unused)
2518c7c2
DM
584{
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
c4028958 600static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
2518c7c2 601
1da177e4
LT
602/* Generate new index... KAME seems to generate them ordered by cost
603 * of an absolute inpredictability of ordering of rules. This will not pass. */
4e81bb83 604static u32 xfrm_gen_index(u8 type, int dir)
1da177e4 605{
1da177e4
LT
606 static u32 idx_generator;
607
608 for (;;) {
2518c7c2
DM
609 struct hlist_node *entry;
610 struct hlist_head *list;
611 struct xfrm_policy *p;
612 u32 idx;
613 int found;
614
1da177e4
LT
615 idx = (idx_generator | dir);
616 idx_generator += 8;
617 if (idx == 0)
618 idx = 8;
2518c7c2
DM
619 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;
1da177e4 624 break;
2518c7c2 625 }
1da177e4 626 }
2518c7c2 627 if (!found)
1da177e4
LT
628 return idx;
629 }
630}
631
2518c7c2
DM
632static 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
1da177e4
LT
647int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
648{
2518c7c2
DM
649 struct xfrm_policy *pol;
650 struct xfrm_policy *delpol;
651 struct hlist_head *chain;
652 struct hlist_node *entry, *newpos, *last;
9b78a82c 653 struct dst_entry *gc_list;
1da177e4
LT
654
655 write_lock_bh(&xfrm_policy_lock);
2518c7c2
DM
656 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) &&
df71837d 664 xfrm_sec_ctx_match(pol->security, policy->security)) {
1da177e4
LT
665 if (excl) {
666 write_unlock_bh(&xfrm_policy_lock);
667 return -EEXIST;
668 }
1da177e4
LT
669 delpol = pol;
670 if (policy->priority > pol->priority)
671 continue;
672 } else if (policy->priority >= pol->priority) {
2518c7c2 673 last = &pol->bydst;
1da177e4
LT
674 continue;
675 }
676 if (!newpos)
2518c7c2 677 newpos = &pol->bydst;
1da177e4
LT
678 if (delpol)
679 break;
2518c7c2 680 last = &pol->bydst;
1da177e4 681 }
2518c7c2
DM
682 if (!newpos)
683 newpos = last;
1da177e4 684 if (newpos)
2518c7c2
DM
685 hlist_add_after(newpos, &policy->bydst);
686 else
687 hlist_add_head(&policy->bydst, chain);
1da177e4 688 xfrm_pol_hold(policy);
2518c7c2 689 xfrm_policy_count[dir]++;
1da177e4 690 atomic_inc(&flow_cache_genid);
2518c7c2
DM
691 if (delpol) {
692 hlist_del(&delpol->bydst);
693 hlist_del(&delpol->byidx);
694 xfrm_policy_count[dir]--;
695 }
4e81bb83 696 policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
2518c7c2 697 hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
1da177e4
LT
698 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
9b78a82c 704 if (delpol)
1da177e4 705 xfrm_policy_kill(delpol);
2518c7c2
DM
706 else if (xfrm_bydst_should_resize(dir, NULL))
707 schedule_work(&xfrm_hash_work);
9b78a82c
DM
708
709 read_lock_bh(&xfrm_policy_lock);
710 gc_list = NULL;
2518c7c2
DM
711 entry = &policy->bydst;
712 hlist_for_each_entry_continue(policy, entry, bydst) {
9b78a82c
DM
713 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);
1da177e4 727 }
9b78a82c
DM
728 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
1da177e4
LT
737 return 0;
738}
739EXPORT_SYMBOL(xfrm_policy_insert);
740
4e81bb83
MN
741struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
742 struct xfrm_selector *sel,
df71837d 743 struct xfrm_sec_ctx *ctx, int delete)
1da177e4 744{
2518c7c2
DM
745 struct xfrm_policy *pol, *ret;
746 struct hlist_head *chain;
747 struct hlist_node *entry;
1da177e4
LT
748
749 write_lock_bh(&xfrm_policy_lock);
2518c7c2
DM
750 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)) {
1da177e4 756 xfrm_pol_hold(pol);
2518c7c2
DM
757 if (delete) {
758 hlist_del(&pol->bydst);
759 hlist_del(&pol->byidx);
760 xfrm_policy_count[dir]--;
761 }
762 ret = pol;
1da177e4
LT
763 break;
764 }
765 }
766 write_unlock_bh(&xfrm_policy_lock);
767
2518c7c2 768 if (ret && delete) {
1da177e4 769 atomic_inc(&flow_cache_genid);
2518c7c2 770 xfrm_policy_kill(ret);
1da177e4 771 }
2518c7c2 772 return ret;
1da177e4 773}
df71837d 774EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
1da177e4 775
4e81bb83 776struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete)
1da177e4 777{
2518c7c2
DM
778 struct xfrm_policy *pol, *ret;
779 struct hlist_head *chain;
780 struct hlist_node *entry;
1da177e4
LT
781
782 write_lock_bh(&xfrm_policy_lock);
2518c7c2
DM
783 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) {
1da177e4 787 xfrm_pol_hold(pol);
2518c7c2
DM
788 if (delete) {
789 hlist_del(&pol->bydst);
790 hlist_del(&pol->byidx);
791 xfrm_policy_count[dir]--;
792 }
793 ret = pol;
1da177e4
LT
794 break;
795 }
796 }
797 write_unlock_bh(&xfrm_policy_lock);
798
2518c7c2 799 if (ret && delete) {
1da177e4 800 atomic_inc(&flow_cache_genid);
2518c7c2 801 xfrm_policy_kill(ret);
1da177e4 802 }
2518c7c2 803 return ret;
1da177e4
LT
804}
805EXPORT_SYMBOL(xfrm_policy_byid);
806
4e81bb83 807void xfrm_policy_flush(u8 type)
1da177e4 808{
1da177e4
LT
809 int dir;
810
811 write_lock_bh(&xfrm_policy_lock);
812 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
2518c7c2
DM
813 struct xfrm_policy *pol;
814 struct hlist_node *entry;
ae8c0577 815 int i, killed;
2518c7c2 816
ae8c0577 817 killed = 0;
2518c7c2
DM
818 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);
1da177e4
LT
825 write_unlock_bh(&xfrm_policy_lock);
826
2518c7c2 827 xfrm_policy_kill(pol);
ae8c0577 828 killed++;
1da177e4
LT
829
830 write_lock_bh(&xfrm_policy_lock);
2518c7c2
DM
831 goto again1;
832 }
833
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);
ae8c0577 846 killed++;
2518c7c2
DM
847
848 write_lock_bh(&xfrm_policy_lock);
849 goto again2;
850 }
1da177e4 851 }
2518c7c2 852
ae8c0577 853 xfrm_policy_count[dir] -= killed;
1da177e4
LT
854 }
855 atomic_inc(&flow_cache_genid);
856 write_unlock_bh(&xfrm_policy_lock);
857}
858EXPORT_SYMBOL(xfrm_policy_flush);
859
4e81bb83 860int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
1da177e4
LT
861 void *data)
862{
2518c7c2
DM
863 struct xfrm_policy *pol;
864 struct hlist_node *entry;
865 int dir, count, error;
1da177e4
LT
866
867 read_lock_bh(&xfrm_policy_lock);
2518c7c2 868 count = 0;
1da177e4 869 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
2518c7c2
DM
870 struct hlist_head *table = xfrm_policy_bydst[dir].table;
871 int i;
872
873 hlist_for_each_entry(pol, entry,
874 &xfrm_policy_inexact[dir], bydst) {
875 if (pol->type == type)
876 count++;
877 }
878 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
879 hlist_for_each_entry(pol, entry, table + i, bydst) {
880 if (pol->type == type)
881 count++;
882 }
883 }
1da177e4
LT
884 }
885
886 if (count == 0) {
887 error = -ENOENT;
888 goto out;
889 }
890
891 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
2518c7c2
DM
892 struct hlist_head *table = xfrm_policy_bydst[dir].table;
893 int i;
894
895 hlist_for_each_entry(pol, entry,
896 &xfrm_policy_inexact[dir], bydst) {
897 if (pol->type != type)
898 continue;
899 error = func(pol, dir % XFRM_POLICY_MAX, --count, data);
1da177e4
LT
900 if (error)
901 goto out;
902 }
2518c7c2
DM
903 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
904 hlist_for_each_entry(pol, entry, table + i, bydst) {
905 if (pol->type != type)
906 continue;
907 error = func(pol, dir % XFRM_POLICY_MAX, --count, data);
908 if (error)
909 goto out;
910 }
911 }
1da177e4 912 }
2518c7c2 913 error = 0;
1da177e4
LT
914out:
915 read_unlock_bh(&xfrm_policy_lock);
916 return error;
917}
918EXPORT_SYMBOL(xfrm_policy_walk);
919
134b0fc5
JM
920/*
921 * Find policy to apply to this flow.
922 *
923 * Returns 0 if policy found, else an -errno.
924 */
2518c7c2
DM
925static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
926 u8 type, u16 family, int dir)
1da177e4 927{
2518c7c2 928 struct xfrm_selector *sel = &pol->selector;
134b0fc5 929 int match, ret = -ESRCH;
1da177e4 930
2518c7c2
DM
931 if (pol->family != family ||
932 pol->type != type)
134b0fc5 933 return ret;
1da177e4 934
2518c7c2 935 match = xfrm_selector_match(sel, fl, family);
134b0fc5
JM
936 if (match)
937 ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
2518c7c2 938
134b0fc5 939 return ret;
2518c7c2 940}
1da177e4 941
2518c7c2
DM
942static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
943 u16 family, u8 dir)
944{
134b0fc5 945 int err;
2518c7c2
DM
946 struct xfrm_policy *pol, *ret;
947 xfrm_address_t *daddr, *saddr;
948 struct hlist_node *entry;
949 struct hlist_head *chain;
acba48e1 950 u32 priority = ~0U;
df71837d 951
2518c7c2
DM
952 daddr = xfrm_flowi_daddr(fl, family);
953 saddr = xfrm_flowi_saddr(fl, family);
954 if (unlikely(!daddr || !saddr))
955 return NULL;
956
957 read_lock_bh(&xfrm_policy_lock);
958 chain = policy_hash_direct(daddr, saddr, family, dir);
959 ret = NULL;
960 hlist_for_each_entry(pol, entry, chain, bydst) {
134b0fc5
JM
961 err = xfrm_policy_match(pol, fl, type, family, dir);
962 if (err) {
963 if (err == -ESRCH)
964 continue;
965 else {
966 ret = ERR_PTR(err);
967 goto fail;
968 }
969 } else {
2518c7c2 970 ret = pol;
acba48e1 971 priority = ret->priority;
2518c7c2
DM
972 break;
973 }
974 }
acba48e1
DM
975 chain = &xfrm_policy_inexact[dir];
976 hlist_for_each_entry(pol, entry, chain, bydst) {
134b0fc5
JM
977 err = xfrm_policy_match(pol, fl, type, family, dir);
978 if (err) {
979 if (err == -ESRCH)
980 continue;
981 else {
982 ret = ERR_PTR(err);
983 goto fail;
984 }
985 } else if (pol->priority < priority) {
acba48e1
DM
986 ret = pol;
987 break;
1da177e4
LT
988 }
989 }
acba48e1
DM
990 if (ret)
991 xfrm_pol_hold(ret);
134b0fc5 992fail:
1da177e4 993 read_unlock_bh(&xfrm_policy_lock);
4e81bb83 994
2518c7c2 995 return ret;
4e81bb83
MN
996}
997
134b0fc5 998static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
4e81bb83
MN
999 void **objp, atomic_t **obj_refp)
1000{
1001 struct xfrm_policy *pol;
134b0fc5 1002 int err = 0;
4e81bb83
MN
1003
1004#ifdef CONFIG_XFRM_SUB_POLICY
1005 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
134b0fc5
JM
1006 if (IS_ERR(pol)) {
1007 err = PTR_ERR(pol);
1008 pol = NULL;
1009 }
1010 if (pol || err)
4e81bb83
MN
1011 goto end;
1012#endif
1013 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
134b0fc5
JM
1014 if (IS_ERR(pol)) {
1015 err = PTR_ERR(pol);
1016 pol = NULL;
1017 }
4e81bb83 1018#ifdef CONFIG_XFRM_SUB_POLICY
2518c7c2 1019end:
4e81bb83 1020#endif
1da177e4
LT
1021 if ((*objp = (void *) pol) != NULL)
1022 *obj_refp = &pol->refcnt;
134b0fc5 1023 return err;
1da177e4
LT
1024}
1025
df71837d
TJ
1026static inline int policy_to_flow_dir(int dir)
1027{
1028 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1029 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1030 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1031 return dir;
1032 switch (dir) {
1033 default:
1034 case XFRM_POLICY_IN:
1035 return FLOW_DIR_IN;
1036 case XFRM_POLICY_OUT:
1037 return FLOW_DIR_OUT;
1038 case XFRM_POLICY_FWD:
1039 return FLOW_DIR_FWD;
1040 };
1041}
1042
e0d1caa7 1043static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
1da177e4
LT
1044{
1045 struct xfrm_policy *pol;
1046
1047 read_lock_bh(&xfrm_policy_lock);
1048 if ((pol = sk->sk_policy[dir]) != NULL) {
df71837d 1049 int match = xfrm_selector_match(&pol->selector, fl,
1da177e4 1050 sk->sk_family);
df71837d
TJ
1051 int err = 0;
1052
3bccfbc7
VY
1053 if (match) {
1054 err = security_xfrm_policy_lookup(pol, fl->secid,
1055 policy_to_flow_dir(dir));
1056 if (!err)
1057 xfrm_pol_hold(pol);
1058 else if (err == -ESRCH)
1059 pol = NULL;
1060 else
1061 pol = ERR_PTR(err);
1062 } else
1da177e4
LT
1063 pol = NULL;
1064 }
1065 read_unlock_bh(&xfrm_policy_lock);
1066 return pol;
1067}
1068
1069static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1070{
2518c7c2
DM
1071 struct hlist_head *chain = policy_hash_bysel(&pol->selector,
1072 pol->family, dir);
4e81bb83 1073
2518c7c2
DM
1074 hlist_add_head(&pol->bydst, chain);
1075 hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
1076 xfrm_policy_count[dir]++;
1da177e4 1077 xfrm_pol_hold(pol);
2518c7c2
DM
1078
1079 if (xfrm_bydst_should_resize(dir, NULL))
1080 schedule_work(&xfrm_hash_work);
1da177e4
LT
1081}
1082
1083static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1084 int dir)
1085{
2518c7c2
DM
1086 if (hlist_unhashed(&pol->bydst))
1087 return NULL;
1da177e4 1088
2518c7c2
DM
1089 hlist_del(&pol->bydst);
1090 hlist_del(&pol->byidx);
1091 xfrm_policy_count[dir]--;
1092
1093 return pol;
1da177e4
LT
1094}
1095
4666faab 1096int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
1da177e4
LT
1097{
1098 write_lock_bh(&xfrm_policy_lock);
1099 pol = __xfrm_policy_unlink(pol, dir);
1100 write_unlock_bh(&xfrm_policy_lock);
1101 if (pol) {
1102 if (dir < XFRM_POLICY_MAX)
1103 atomic_inc(&flow_cache_genid);
1104 xfrm_policy_kill(pol);
4666faab 1105 return 0;
1da177e4 1106 }
4666faab 1107 return -ENOENT;
1da177e4 1108}
a70fcb0b 1109EXPORT_SYMBOL(xfrm_policy_delete);
1da177e4
LT
1110
1111int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1112{
1113 struct xfrm_policy *old_pol;
1114
4e81bb83
MN
1115#ifdef CONFIG_XFRM_SUB_POLICY
1116 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1117 return -EINVAL;
1118#endif
1119
1da177e4
LT
1120 write_lock_bh(&xfrm_policy_lock);
1121 old_pol = sk->sk_policy[dir];
1122 sk->sk_policy[dir] = pol;
1123 if (pol) {
1124 pol->curlft.add_time = (unsigned long)xtime.tv_sec;
4e81bb83 1125 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
1da177e4
LT
1126 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1127 }
1128 if (old_pol)
1129 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1130 write_unlock_bh(&xfrm_policy_lock);
1131
1132 if (old_pol) {
1133 xfrm_policy_kill(old_pol);
1134 }
1135 return 0;
1136}
1137
1138static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1139{
1140 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
1141
1142 if (newp) {
1143 newp->selector = old->selector;
df71837d
TJ
1144 if (security_xfrm_policy_clone(old, newp)) {
1145 kfree(newp);
1146 return NULL; /* ENOMEM */
1147 }
1da177e4
LT
1148 newp->lft = old->lft;
1149 newp->curlft = old->curlft;
1150 newp->action = old->action;
1151 newp->flags = old->flags;
1152 newp->xfrm_nr = old->xfrm_nr;
1153 newp->index = old->index;
4e81bb83 1154 newp->type = old->type;
1da177e4
LT
1155 memcpy(newp->xfrm_vec, old->xfrm_vec,
1156 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1157 write_lock_bh(&xfrm_policy_lock);
1158 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1159 write_unlock_bh(&xfrm_policy_lock);
1160 xfrm_pol_put(newp);
1161 }
1162 return newp;
1163}
1164
1165int __xfrm_sk_clone_policy(struct sock *sk)
1166{
1167 struct xfrm_policy *p0 = sk->sk_policy[0],
1168 *p1 = sk->sk_policy[1];
1169
1170 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1171 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1172 return -ENOMEM;
1173 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1174 return -ENOMEM;
1175 return 0;
1176}
1177
a1e59abf
PM
1178static int
1179xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
1180 unsigned short family)
1181{
1182 int err;
1183 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1184
1185 if (unlikely(afinfo == NULL))
1186 return -EINVAL;
1187 err = afinfo->get_saddr(local, remote);
1188 xfrm_policy_put_afinfo(afinfo);
1189 return err;
1190}
1191
1da177e4
LT
1192/* Resolve list of templates for the flow, given policy. */
1193
1194static int
4e81bb83
MN
1195xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1196 struct xfrm_state **xfrm,
1197 unsigned short family)
1da177e4
LT
1198{
1199 int nx;
1200 int i, error;
1201 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1202 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
a1e59abf 1203 xfrm_address_t tmp;
1da177e4
LT
1204
1205 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1206 struct xfrm_state *x;
1207 xfrm_address_t *remote = daddr;
1208 xfrm_address_t *local = saddr;
1209 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1210
7e49e6de 1211 if (tmpl->mode == XFRM_MODE_TUNNEL) {
1da177e4
LT
1212 remote = &tmpl->id.daddr;
1213 local = &tmpl->saddr;
76b3f055 1214 family = tmpl->encap_family;
a1e59abf
PM
1215 if (xfrm_addr_any(local, family)) {
1216 error = xfrm_get_saddr(&tmp, remote, family);
1217 if (error)
1218 goto fail;
1219 local = &tmp;
1220 }
1da177e4
LT
1221 }
1222
1223 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1224
1225 if (x && x->km.state == XFRM_STATE_VALID) {
1226 xfrm[nx++] = x;
1227 daddr = remote;
1228 saddr = local;
1229 continue;
1230 }
1231 if (x) {
1232 error = (x->km.state == XFRM_STATE_ERROR ?
1233 -EINVAL : -EAGAIN);
1234 xfrm_state_put(x);
1235 }
1236
1237 if (!tmpl->optional)
1238 goto fail;
1239 }
1240 return nx;
1241
1242fail:
1243 for (nx--; nx>=0; nx--)
1244 xfrm_state_put(xfrm[nx]);
1245 return error;
1246}
1247
4e81bb83
MN
1248static int
1249xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1250 struct xfrm_state **xfrm,
1251 unsigned short family)
1252{
41a49cc3
MN
1253 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1254 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
4e81bb83
MN
1255 int cnx = 0;
1256 int error;
1257 int ret;
1258 int i;
1259
1260 for (i = 0; i < npols; i++) {
1261 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1262 error = -ENOBUFS;
1263 goto fail;
1264 }
41a49cc3
MN
1265
1266 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
4e81bb83
MN
1267 if (ret < 0) {
1268 error = ret;
1269 goto fail;
1270 } else
1271 cnx += ret;
1272 }
1273
41a49cc3
MN
1274 /* found states are sorted for outbound processing */
1275 if (npols > 1)
1276 xfrm_state_sort(xfrm, tpp, cnx, family);
1277
4e81bb83
MN
1278 return cnx;
1279
1280 fail:
1281 for (cnx--; cnx>=0; cnx--)
41a49cc3 1282 xfrm_state_put(tpp[cnx]);
4e81bb83
MN
1283 return error;
1284
1285}
1286
1da177e4
LT
1287/* Check that the bundle accepts the flow and its components are
1288 * still valid.
1289 */
1290
1291static struct dst_entry *
1292xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1293{
1294 struct dst_entry *x;
1295 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1296 if (unlikely(afinfo == NULL))
1297 return ERR_PTR(-EINVAL);
1298 x = afinfo->find_bundle(fl, policy);
1299 xfrm_policy_put_afinfo(afinfo);
1300 return x;
1301}
1302
1303/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1304 * all the metrics... Shortly, bundle a bundle.
1305 */
1306
1307static int
1308xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
1309 struct flowi *fl, struct dst_entry **dst_p,
1310 unsigned short family)
1311{
1312 int err;
1313 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1314 if (unlikely(afinfo == NULL))
1315 return -EINVAL;
1316 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
1317 xfrm_policy_put_afinfo(afinfo);
1318 return err;
1319}
1320
1da177e4
LT
1321
1322static int stale_bundle(struct dst_entry *dst);
1323
1324/* Main function: finds/creates a bundle for given flow.
1325 *
1326 * At the moment we eat a raw IP route. Mostly to speed up lookups
1327 * on interfaces with disabled IPsec.
1328 */
1329int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1330 struct sock *sk, int flags)
1331{
1332 struct xfrm_policy *policy;
4e81bb83
MN
1333 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1334 int npols;
1335 int pol_dead;
1336 int xfrm_nr;
1337 int pi;
1da177e4
LT
1338 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1339 struct dst_entry *dst, *dst_orig = *dst_p;
1340 int nx = 0;
1341 int err;
1342 u32 genid;
42cf93cd 1343 u16 family;
df71837d 1344 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
e0d1caa7 1345
1da177e4
LT
1346restart:
1347 genid = atomic_read(&flow_cache_genid);
1348 policy = NULL;
4e81bb83
MN
1349 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1350 pols[pi] = NULL;
1351 npols = 0;
1352 pol_dead = 0;
1353 xfrm_nr = 0;
1354
3bccfbc7 1355 if (sk && sk->sk_policy[1]) {
e0d1caa7 1356 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
3bccfbc7
VY
1357 if (IS_ERR(policy))
1358 return PTR_ERR(policy);
1359 }
1da177e4
LT
1360
1361 if (!policy) {
1362 /* To accelerate a bit... */
2518c7c2
DM
1363 if ((dst_orig->flags & DST_NOXFRM) ||
1364 !xfrm_policy_count[XFRM_POLICY_OUT])
1da177e4
LT
1365 return 0;
1366
e0d1caa7 1367 policy = flow_cache_lookup(fl, dst_orig->ops->family,
42cf93cd 1368 dir, xfrm_policy_lookup);
134b0fc5
JM
1369 if (IS_ERR(policy))
1370 return PTR_ERR(policy);
1da177e4
LT
1371 }
1372
1373 if (!policy)
1374 return 0;
1375
42cf93cd 1376 family = dst_orig->ops->family;
1da177e4 1377 policy->curlft.use_time = (unsigned long)xtime.tv_sec;
4e81bb83
MN
1378 pols[0] = policy;
1379 npols ++;
1380 xfrm_nr += pols[0]->xfrm_nr;
1da177e4
LT
1381
1382 switch (policy->action) {
1383 case XFRM_POLICY_BLOCK:
1384 /* Prohibit the flow */
e104411b
PM
1385 err = -EPERM;
1386 goto error;
1da177e4
LT
1387
1388 case XFRM_POLICY_ALLOW:
4e81bb83 1389#ifndef CONFIG_XFRM_SUB_POLICY
1da177e4
LT
1390 if (policy->xfrm_nr == 0) {
1391 /* Flow passes not transformed. */
1392 xfrm_pol_put(policy);
1393 return 0;
1394 }
4e81bb83 1395#endif
1da177e4
LT
1396
1397 /* Try to find matching bundle.
1398 *
1399 * LATER: help from flow cache. It is optional, this
1400 * is required only for output policy.
1401 */
1402 dst = xfrm_find_bundle(fl, policy, family);
1403 if (IS_ERR(dst)) {
e104411b
PM
1404 err = PTR_ERR(dst);
1405 goto error;
1da177e4
LT
1406 }
1407
1408 if (dst)
1409 break;
1410
4e81bb83
MN
1411#ifdef CONFIG_XFRM_SUB_POLICY
1412 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1413 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1414 fl, family,
1415 XFRM_POLICY_OUT);
1416 if (pols[1]) {
134b0fc5
JM
1417 if (IS_ERR(pols[1])) {
1418 err = PTR_ERR(pols[1]);
1419 goto error;
1420 }
4e81bb83
MN
1421 if (pols[1]->action == XFRM_POLICY_BLOCK) {
1422 err = -EPERM;
1423 goto error;
1424 }
1425 npols ++;
1426 xfrm_nr += pols[1]->xfrm_nr;
1427 }
1428 }
1429
1430 /*
1431 * Because neither flowi nor bundle information knows about
1432 * transformation template size. On more than one policy usage
1433 * we can realize whether all of them is bypass or not after
1434 * they are searched. See above not-transformed bypass
1435 * is surrounded by non-sub policy configuration, too.
1436 */
1437 if (xfrm_nr == 0) {
1438 /* Flow passes not transformed. */
1439 xfrm_pols_put(pols, npols);
1440 return 0;
1441 }
1442
1443#endif
1444 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
1da177e4
LT
1445
1446 if (unlikely(nx<0)) {
1447 err = nx;
1448 if (err == -EAGAIN && flags) {
1449 DECLARE_WAITQUEUE(wait, current);
1450
1451 add_wait_queue(&km_waitq, &wait);
1452 set_current_state(TASK_INTERRUPTIBLE);
1453 schedule();
1454 set_current_state(TASK_RUNNING);
1455 remove_wait_queue(&km_waitq, &wait);
1456
4e81bb83 1457 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
1da177e4
LT
1458
1459 if (nx == -EAGAIN && signal_pending(current)) {
1460 err = -ERESTART;
1461 goto error;
1462 }
1463 if (nx == -EAGAIN ||
1464 genid != atomic_read(&flow_cache_genid)) {
4e81bb83 1465 xfrm_pols_put(pols, npols);
1da177e4
LT
1466 goto restart;
1467 }
1468 err = nx;
1469 }
1470 if (err < 0)
1471 goto error;
1472 }
1473 if (nx == 0) {
1474 /* Flow passes not transformed. */
4e81bb83 1475 xfrm_pols_put(pols, npols);
1da177e4
LT
1476 return 0;
1477 }
1478
1479 dst = dst_orig;
1480 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
1481
1482 if (unlikely(err)) {
1483 int i;
1484 for (i=0; i<nx; i++)
1485 xfrm_state_put(xfrm[i]);
1486 goto error;
1487 }
1488
4e81bb83
MN
1489 for (pi = 0; pi < npols; pi++) {
1490 read_lock_bh(&pols[pi]->lock);
1491 pol_dead |= pols[pi]->dead;
1492 read_unlock_bh(&pols[pi]->lock);
1493 }
1494
1da177e4 1495 write_lock_bh(&policy->lock);
4e81bb83 1496 if (unlikely(pol_dead || stale_bundle(dst))) {
1da177e4
LT
1497 /* Wow! While we worked on resolving, this
1498 * policy has gone. Retry. It is not paranoia,
1499 * we just cannot enlist new bundle to dead object.
1500 * We can't enlist stable bundles either.
1501 */
1502 write_unlock_bh(&policy->lock);
1da177e4
LT
1503 if (dst)
1504 dst_free(dst);
00de651d
HX
1505
1506 err = -EHOSTUNREACH;
1507 goto error;
1da177e4
LT
1508 }
1509 dst->next = policy->bundles;
1510 policy->bundles = dst;
1511 dst_hold(dst);
1512 write_unlock_bh(&policy->lock);
1513 }
1514 *dst_p = dst;
1515 dst_release(dst_orig);
4e81bb83 1516 xfrm_pols_put(pols, npols);
1da177e4
LT
1517 return 0;
1518
1519error:
1520 dst_release(dst_orig);
4e81bb83 1521 xfrm_pols_put(pols, npols);
1da177e4
LT
1522 *dst_p = NULL;
1523 return err;
1524}
1525EXPORT_SYMBOL(xfrm_lookup);
1526
df0ba92a
MN
1527static inline int
1528xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1529{
1530 struct xfrm_state *x;
1531 int err;
1532
1533 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1534 return 0;
1535 x = skb->sp->xvec[idx];
1536 if (!x->type->reject)
1537 return 0;
1538 xfrm_state_hold(x);
1539 err = x->type->reject(x, skb, fl);
1540 xfrm_state_put(x);
1541 return err;
1542}
1543
1da177e4
LT
1544/* When skb is transformed back to its "native" form, we have to
1545 * check policy restrictions. At the moment we make this in maximally
1546 * stupid way. Shame on me. :-) Of course, connected sockets must
1547 * have policy cached at them.
1548 */
1549
1550static inline int
1551xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
1552 unsigned short family)
1553{
1554 if (xfrm_state_kern(x))
1555 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family);
1556 return x->id.proto == tmpl->id.proto &&
1557 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1558 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1559 x->props.mode == tmpl->mode &&
f3bd4840
MN
1560 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1561 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
7e49e6de
MN
1562 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1563 xfrm_state_addr_cmp(tmpl, x, family));
1da177e4
LT
1564}
1565
df0ba92a
MN
1566/*
1567 * 0 or more than 0 is returned when validation is succeeded (either bypass
1568 * because of optional transport mode, or next index of the mathced secpath
1569 * state with the template.
1570 * -1 is returned when no matching template is found.
1571 * Otherwise "-2 - errored_index" is returned.
1572 */
1da177e4
LT
1573static inline int
1574xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1575 unsigned short family)
1576{
1577 int idx = start;
1578
1579 if (tmpl->optional) {
7e49e6de 1580 if (tmpl->mode == XFRM_MODE_TRANSPORT)
1da177e4
LT
1581 return start;
1582 } else
1583 start = -1;
1584 for (; idx < sp->len; idx++) {
dbe5b4aa 1585 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
1da177e4 1586 return ++idx;
df0ba92a
MN
1587 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1588 if (start == -1)
1589 start = -2-idx;
1da177e4 1590 break;
df0ba92a 1591 }
1da177e4
LT
1592 }
1593 return start;
1594}
1595
3e3850e9
PM
1596int
1597xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
1da177e4
LT
1598{
1599 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
e0d1caa7 1600 int err;
1da177e4
LT
1601
1602 if (unlikely(afinfo == NULL))
1603 return -EAFNOSUPPORT;
1604
1605 afinfo->decode_session(skb, fl);
beb8d13b 1606 err = security_xfrm_decode_session(skb, &fl->secid);
1da177e4 1607 xfrm_policy_put_afinfo(afinfo);
e0d1caa7 1608 return err;
1da177e4 1609}
3e3850e9 1610EXPORT_SYMBOL(xfrm_decode_session);
1da177e4 1611
df0ba92a 1612static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
1da177e4
LT
1613{
1614 for (; k < sp->len; k++) {
df0ba92a 1615 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
d1d9facf 1616 *idxp = k;
1da177e4 1617 return 1;
df0ba92a 1618 }
1da177e4
LT
1619 }
1620
1621 return 0;
1622}
1623
1624int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
1625 unsigned short family)
1626{
1627 struct xfrm_policy *pol;
4e81bb83
MN
1628 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1629 int npols = 0;
1630 int xfrm_nr;
1631 int pi;
1da177e4 1632 struct flowi fl;
df71837d 1633 u8 fl_dir = policy_to_flow_dir(dir);
df0ba92a 1634 int xerr_idx = -1;
1da177e4 1635
3e3850e9 1636 if (xfrm_decode_session(skb, &fl, family) < 0)
1da177e4 1637 return 0;
eb9c7ebe 1638 nf_nat_decode_session(skb, &fl, family);
1da177e4
LT
1639
1640 /* First, check used SA against their selectors. */
1641 if (skb->sp) {
1642 int i;
1643
1644 for (i=skb->sp->len-1; i>=0; i--) {
dbe5b4aa
HX
1645 struct xfrm_state *x = skb->sp->xvec[i];
1646 if (!xfrm_selector_match(&x->sel, &fl, family))
1da177e4 1647 return 0;
1da177e4
LT
1648 }
1649 }
1650
1651 pol = NULL;
3bccfbc7 1652 if (sk && sk->sk_policy[dir]) {
e0d1caa7 1653 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
3bccfbc7
VY
1654 if (IS_ERR(pol))
1655 return 0;
1656 }
1da177e4
LT
1657
1658 if (!pol)
e0d1caa7 1659 pol = flow_cache_lookup(&fl, family, fl_dir,
1da177e4
LT
1660 xfrm_policy_lookup);
1661
134b0fc5
JM
1662 if (IS_ERR(pol))
1663 return 0;
1664
df0ba92a 1665 if (!pol) {
d1d9facf 1666 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
df0ba92a
MN
1667 xfrm_secpath_reject(xerr_idx, skb, &fl);
1668 return 0;
1669 }
1670 return 1;
1671 }
1da177e4
LT
1672
1673 pol->curlft.use_time = (unsigned long)xtime.tv_sec;
1674
4e81bb83
MN
1675 pols[0] = pol;
1676 npols ++;
1677#ifdef CONFIG_XFRM_SUB_POLICY
1678 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1679 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1680 &fl, family,
1681 XFRM_POLICY_IN);
1682 if (pols[1]) {
134b0fc5
JM
1683 if (IS_ERR(pols[1]))
1684 return 0;
4e81bb83
MN
1685 pols[1]->curlft.use_time = (unsigned long)xtime.tv_sec;
1686 npols ++;
1687 }
1688 }
1689#endif
1690
1da177e4
LT
1691 if (pol->action == XFRM_POLICY_ALLOW) {
1692 struct sec_path *sp;
1693 static struct sec_path dummy;
4e81bb83 1694 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
41a49cc3 1695 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
4e81bb83
MN
1696 struct xfrm_tmpl **tpp = tp;
1697 int ti = 0;
1da177e4
LT
1698 int i, k;
1699
1700 if ((sp = skb->sp) == NULL)
1701 sp = &dummy;
1702
4e81bb83
MN
1703 for (pi = 0; pi < npols; pi++) {
1704 if (pols[pi] != pol &&
1705 pols[pi]->action != XFRM_POLICY_ALLOW)
1706 goto reject;
1707 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
1708 goto reject_error;
1709 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1710 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1711 }
1712 xfrm_nr = ti;
41a49cc3
MN
1713 if (npols > 1) {
1714 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1715 tpp = stp;
1716 }
4e81bb83 1717
1da177e4
LT
1718 /* For each tunnel xfrm, find the first matching tmpl.
1719 * For each tmpl before that, find corresponding xfrm.
1720 * Order is _important_. Later we will implement
1721 * some barriers, but at the moment barriers
1722 * are implied between each two transformations.
1723 */
4e81bb83
MN
1724 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1725 k = xfrm_policy_ok(tpp[i], sp, k, family);
df0ba92a 1726 if (k < 0) {
d1d9facf
JM
1727 if (k < -1)
1728 /* "-2 - errored_index" returned */
1729 xerr_idx = -(2+k);
1da177e4 1730 goto reject;
df0ba92a 1731 }
1da177e4
LT
1732 }
1733
d1d9facf 1734 if (secpath_has_nontransport(sp, k, &xerr_idx))
1da177e4
LT
1735 goto reject;
1736
4e81bb83 1737 xfrm_pols_put(pols, npols);
1da177e4
LT
1738 return 1;
1739 }
1740
1741reject:
df0ba92a 1742 xfrm_secpath_reject(xerr_idx, skb, &fl);
4e81bb83
MN
1743reject_error:
1744 xfrm_pols_put(pols, npols);
1da177e4
LT
1745 return 0;
1746}
1747EXPORT_SYMBOL(__xfrm_policy_check);
1748
1749int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1750{
1751 struct flowi fl;
1752
3e3850e9 1753 if (xfrm_decode_session(skb, &fl, family) < 0)
1da177e4
LT
1754 return 0;
1755
1756 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1757}
1758EXPORT_SYMBOL(__xfrm_route_forward);
1759
d49c73c7
DM
1760/* Optimize later using cookies and generation ids. */
1761
1da177e4
LT
1762static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1763{
d49c73c7
DM
1764 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1765 * to "-1" to force all XFRM destinations to get validated by
1766 * dst_ops->check on every use. We do this because when a
1767 * normal route referenced by an XFRM dst is obsoleted we do
1768 * not go looking around for all parent referencing XFRM dsts
1769 * so that we can invalidate them. It is just too much work.
1770 * Instead we make the checks here on every use. For example:
1771 *
1772 * XFRM dst A --> IPv4 dst X
1773 *
1774 * X is the "xdst->route" of A (X is also the "dst->path" of A
1775 * in this example). If X is marked obsolete, "A" will not
1776 * notice. That's what we are validating here via the
1777 * stale_bundle() check.
1778 *
1779 * When a policy's bundle is pruned, we dst_free() the XFRM
1780 * dst which causes it's ->obsolete field to be set to a
1781 * positive non-zero integer. If an XFRM dst has been pruned
1782 * like this, we want to force a new route lookup.
399c180a 1783 */
d49c73c7
DM
1784 if (dst->obsolete < 0 && !stale_bundle(dst))
1785 return dst;
1786
1da177e4
LT
1787 return NULL;
1788}
1789
1790static int stale_bundle(struct dst_entry *dst)
1791{
5b368e61 1792 return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
1da177e4
LT
1793}
1794
aabc9761 1795void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
1da177e4 1796{
1da177e4
LT
1797 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
1798 dst->dev = &loopback_dev;
1799 dev_hold(&loopback_dev);
1800 dev_put(dev);
1801 }
1802}
aabc9761 1803EXPORT_SYMBOL(xfrm_dst_ifdown);
1da177e4
LT
1804
1805static void xfrm_link_failure(struct sk_buff *skb)
1806{
1807 /* Impossible. Such dst must be popped before reaches point of failure. */
1808 return;
1809}
1810
1811static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1812{
1813 if (dst) {
1814 if (dst->obsolete) {
1815 dst_release(dst);
1816 dst = NULL;
1817 }
1818 }
1819 return dst;
1820}
1821
2518c7c2
DM
1822static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
1823{
1824 struct dst_entry *dst, **dstp;
1825
1826 write_lock(&pol->lock);
1827 dstp = &pol->bundles;
1828 while ((dst=*dstp) != NULL) {
1829 if (func(dst)) {
1830 *dstp = dst->next;
1831 dst->next = *gc_list_p;
1832 *gc_list_p = dst;
1833 } else {
1834 dstp = &dst->next;
1835 }
1836 }
1837 write_unlock(&pol->lock);
1838}
1839
1da177e4
LT
1840static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1841{
2518c7c2
DM
1842 struct dst_entry *gc_list = NULL;
1843 int dir;
1da177e4
LT
1844
1845 read_lock_bh(&xfrm_policy_lock);
2518c7c2
DM
1846 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
1847 struct xfrm_policy *pol;
1848 struct hlist_node *entry;
1849 struct hlist_head *table;
1850 int i;
4e81bb83 1851
2518c7c2
DM
1852 hlist_for_each_entry(pol, entry,
1853 &xfrm_policy_inexact[dir], bydst)
1854 prune_one_bundle(pol, func, &gc_list);
1855
1856 table = xfrm_policy_bydst[dir].table;
1857 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
1858 hlist_for_each_entry(pol, entry, table + i, bydst)
1859 prune_one_bundle(pol, func, &gc_list);
1da177e4
LT
1860 }
1861 }
1862 read_unlock_bh(&xfrm_policy_lock);
1863
1864 while (gc_list) {
2518c7c2 1865 struct dst_entry *dst = gc_list;
1da177e4
LT
1866 gc_list = dst->next;
1867 dst_free(dst);
1868 }
1869}
1870
1871static int unused_bundle(struct dst_entry *dst)
1872{
1873 return !atomic_read(&dst->__refcnt);
1874}
1875
1876static void __xfrm_garbage_collect(void)
1877{
1878 xfrm_prune_bundles(unused_bundle);
1879}
1880
1c095399 1881static int xfrm_flush_bundles(void)
1da177e4
LT
1882{
1883 xfrm_prune_bundles(stale_bundle);
1884 return 0;
1885}
1886
1887void xfrm_init_pmtu(struct dst_entry *dst)
1888{
1889 do {
1890 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1891 u32 pmtu, route_mtu_cached;
1892
1893 pmtu = dst_mtu(dst->child);
1894 xdst->child_mtu_cached = pmtu;
1895
1896 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
1897
1898 route_mtu_cached = dst_mtu(xdst->route);
1899 xdst->route_mtu_cached = route_mtu_cached;
1900
1901 if (pmtu > route_mtu_cached)
1902 pmtu = route_mtu_cached;
1903
1904 dst->metrics[RTAX_MTU-1] = pmtu;
1905 } while ((dst = dst->next));
1906}
1907
1908EXPORT_SYMBOL(xfrm_init_pmtu);
1909
1910/* Check that the bundle accepts the flow and its components are
1911 * still valid.
1912 */
1913
5b368e61
VY
1914int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
1915 struct flowi *fl, int family, int strict)
1da177e4
LT
1916{
1917 struct dst_entry *dst = &first->u.dst;
1918 struct xfrm_dst *last;
1919 u32 mtu;
1920
92d63dec 1921 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
1da177e4
LT
1922 (dst->dev && !netif_running(dst->dev)))
1923 return 0;
1924
1925 last = NULL;
1926
1927 do {
1928 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1929
1930 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
1931 return 0;
67f83cbf
VY
1932 if (fl && pol &&
1933 !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
e0d1caa7 1934 return 0;
1da177e4
LT
1935 if (dst->xfrm->km.state != XFRM_STATE_VALID)
1936 return 0;
9d4a706d
DM
1937 if (xdst->genid != dst->xfrm->genid)
1938 return 0;
e53820de
MN
1939
1940 if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
1941 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
1942 return 0;
1da177e4
LT
1943
1944 mtu = dst_mtu(dst->child);
1945 if (xdst->child_mtu_cached != mtu) {
1946 last = xdst;
1947 xdst->child_mtu_cached = mtu;
1948 }
1949
92d63dec 1950 if (!dst_check(xdst->route, xdst->route_cookie))
1da177e4
LT
1951 return 0;
1952 mtu = dst_mtu(xdst->route);
1953 if (xdst->route_mtu_cached != mtu) {
1954 last = xdst;
1955 xdst->route_mtu_cached = mtu;
1956 }
1957
1958 dst = dst->child;
1959 } while (dst->xfrm);
1960
1961 if (likely(!last))
1962 return 1;
1963
1964 mtu = last->child_mtu_cached;
1965 for (;;) {
1966 dst = &last->u.dst;
1967
1968 mtu = xfrm_state_mtu(dst->xfrm, mtu);
1969 if (mtu > last->route_mtu_cached)
1970 mtu = last->route_mtu_cached;
1971 dst->metrics[RTAX_MTU-1] = mtu;
1972
1973 if (last == first)
1974 break;
1975
1976 last = last->u.next;
1977 last->child_mtu_cached = mtu;
1978 }
1979
1980 return 1;
1981}
1982
1983EXPORT_SYMBOL(xfrm_bundle_ok);
1984
1da177e4
LT
1985int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
1986{
1987 int err = 0;
1988 if (unlikely(afinfo == NULL))
1989 return -EINVAL;
1990 if (unlikely(afinfo->family >= NPROTO))
1991 return -EAFNOSUPPORT;
e959d812 1992 write_lock_bh(&xfrm_policy_afinfo_lock);
1da177e4
LT
1993 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
1994 err = -ENOBUFS;
1995 else {
1996 struct dst_ops *dst_ops = afinfo->dst_ops;
1997 if (likely(dst_ops->kmem_cachep == NULL))
1998 dst_ops->kmem_cachep = xfrm_dst_cache;
1999 if (likely(dst_ops->check == NULL))
2000 dst_ops->check = xfrm_dst_check;
1da177e4
LT
2001 if (likely(dst_ops->negative_advice == NULL))
2002 dst_ops->negative_advice = xfrm_negative_advice;
2003 if (likely(dst_ops->link_failure == NULL))
2004 dst_ops->link_failure = xfrm_link_failure;
1da177e4
LT
2005 if (likely(afinfo->garbage_collect == NULL))
2006 afinfo->garbage_collect = __xfrm_garbage_collect;
2007 xfrm_policy_afinfo[afinfo->family] = afinfo;
2008 }
e959d812 2009 write_unlock_bh(&xfrm_policy_afinfo_lock);
1da177e4
LT
2010 return err;
2011}
2012EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2013
2014int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2015{
2016 int err = 0;
2017 if (unlikely(afinfo == NULL))
2018 return -EINVAL;
2019 if (unlikely(afinfo->family >= NPROTO))
2020 return -EAFNOSUPPORT;
e959d812 2021 write_lock_bh(&xfrm_policy_afinfo_lock);
1da177e4
LT
2022 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2023 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2024 err = -EINVAL;
2025 else {
2026 struct dst_ops *dst_ops = afinfo->dst_ops;
2027 xfrm_policy_afinfo[afinfo->family] = NULL;
2028 dst_ops->kmem_cachep = NULL;
2029 dst_ops->check = NULL;
1da177e4
LT
2030 dst_ops->negative_advice = NULL;
2031 dst_ops->link_failure = NULL;
1da177e4
LT
2032 afinfo->garbage_collect = NULL;
2033 }
2034 }
e959d812 2035 write_unlock_bh(&xfrm_policy_afinfo_lock);
1da177e4
LT
2036 return err;
2037}
2038EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2039
2040static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
2041{
2042 struct xfrm_policy_afinfo *afinfo;
2043 if (unlikely(family >= NPROTO))
2044 return NULL;
2045 read_lock(&xfrm_policy_afinfo_lock);
2046 afinfo = xfrm_policy_afinfo[family];
546be240
HX
2047 if (unlikely(!afinfo))
2048 read_unlock(&xfrm_policy_afinfo_lock);
1da177e4
LT
2049 return afinfo;
2050}
2051
2052static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2053{
546be240
HX
2054 read_unlock(&xfrm_policy_afinfo_lock);
2055}
2056
2057static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
2058{
2059 struct xfrm_policy_afinfo *afinfo;
2060 if (unlikely(family >= NPROTO))
2061 return NULL;
2062 write_lock_bh(&xfrm_policy_afinfo_lock);
2063 afinfo = xfrm_policy_afinfo[family];
2064 if (unlikely(!afinfo))
2065 write_unlock_bh(&xfrm_policy_afinfo_lock);
2066 return afinfo;
2067}
2068
2069static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
2070{
2071 write_unlock_bh(&xfrm_policy_afinfo_lock);
1da177e4
LT
2072}
2073
2074static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2075{
2076 switch (event) {
2077 case NETDEV_DOWN:
2078 xfrm_flush_bundles();
2079 }
2080 return NOTIFY_DONE;
2081}
2082
2083static struct notifier_block xfrm_dev_notifier = {
2084 xfrm_dev_event,
2085 NULL,
2086 0
2087};
2088
2089static void __init xfrm_policy_init(void)
2090{
2518c7c2
DM
2091 unsigned int hmask, sz;
2092 int dir;
2093
1da177e4
LT
2094 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2095 sizeof(struct xfrm_dst),
e5d679f3 2096 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1da177e4 2097 NULL, NULL);
1da177e4 2098
2518c7c2
DM
2099 hmask = 8 - 1;
2100 sz = (hmask+1) * sizeof(struct hlist_head);
2101
44e36b42 2102 xfrm_policy_byidx = xfrm_hash_alloc(sz);
2518c7c2
DM
2103 xfrm_idx_hmask = hmask;
2104 if (!xfrm_policy_byidx)
2105 panic("XFRM: failed to allocate byidx hash\n");
2106
2107 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2108 struct xfrm_policy_hash *htab;
2109
2110 INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
2111
2112 htab = &xfrm_policy_bydst[dir];
44e36b42 2113 htab->table = xfrm_hash_alloc(sz);
2518c7c2
DM
2114 htab->hmask = hmask;
2115 if (!htab->table)
2116 panic("XFRM: failed to allocate bydst hash\n");
2117 }
2118
c4028958 2119 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
1da177e4
LT
2120 register_netdevice_notifier(&xfrm_dev_notifier);
2121}
2122
2123void __init xfrm_init(void)
2124{
2125 xfrm_state_init();
2126 xfrm_policy_init();
2127 xfrm_input_init();
2128}
2129