RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / xfrm / xfrm_policy.c
blob1c86a23c3230be6f535cd1211eaaa59ddbb3325e
1 /*
2 * xfrm_policy.c
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
16 #include <linux/slab.h>
17 #include <linux/kmod.h>
18 #include <linux/list.h>
19 #include <linux/spinlock.h>
20 #include <linux/workqueue.h>
21 #include <linux/notifier.h>
22 #include <linux/netdevice.h>
23 #include <linux/netfilter.h>
24 #include <linux/module.h>
25 #include <linux/cache.h>
26 #include <net/xfrm.h>
27 #include <net/ip.h>
28 #include <linux/audit.h>
29 #include <linux/cache.h>
31 #include "xfrm_hash.h"
33 int sysctl_xfrm_larval_drop __read_mostly;
35 DEFINE_MUTEX(xfrm_cfg_mutex);
36 EXPORT_SYMBOL(xfrm_cfg_mutex);
38 static DEFINE_RWLOCK(xfrm_policy_lock);
40 unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
41 EXPORT_SYMBOL(xfrm_policy_count);
43 static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
44 static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
46 static struct kmem_cache *xfrm_dst_cache __read_mostly;
48 static struct work_struct xfrm_policy_gc_work;
49 static HLIST_HEAD(xfrm_policy_gc_list);
50 static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
52 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
53 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
54 static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family);
55 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo);
57 static inline int
58 __xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
60 return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
61 addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
62 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
63 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
64 (fl->proto == sel->proto || !sel->proto) &&
65 (fl->oif == sel->ifindex || !sel->ifindex);
68 static inline int
69 __xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
71 return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
72 addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
73 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
74 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
75 (fl->proto == sel->proto || !sel->proto) &&
76 (fl->oif == sel->ifindex || !sel->ifindex);
79 int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
80 unsigned short family)
82 switch (family) {
83 case AF_INET:
84 return __xfrm4_selector_match(sel, fl);
85 case AF_INET6:
86 return __xfrm6_selector_match(sel, fl);
88 return 0;
91 int xfrm_register_type(struct xfrm_type *type, unsigned short family)
93 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
94 struct xfrm_type **typemap;
95 int err = 0;
97 if (unlikely(afinfo == NULL))
98 return -EAFNOSUPPORT;
99 typemap = afinfo->type_map;
101 if (likely(typemap[type->proto] == NULL))
102 typemap[type->proto] = type;
103 else
104 err = -EEXIST;
105 xfrm_policy_unlock_afinfo(afinfo);
106 return err;
108 EXPORT_SYMBOL(xfrm_register_type);
110 int xfrm_unregister_type(struct xfrm_type *type, unsigned short family)
112 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
113 struct xfrm_type **typemap;
114 int err = 0;
116 if (unlikely(afinfo == NULL))
117 return -EAFNOSUPPORT;
118 typemap = afinfo->type_map;
120 if (unlikely(typemap[type->proto] != type))
121 err = -ENOENT;
122 else
123 typemap[type->proto] = NULL;
124 xfrm_policy_unlock_afinfo(afinfo);
125 return err;
127 EXPORT_SYMBOL(xfrm_unregister_type);
129 struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
131 struct xfrm_policy_afinfo *afinfo;
132 struct xfrm_type **typemap;
133 struct xfrm_type *type;
134 int modload_attempted = 0;
136 retry:
137 afinfo = xfrm_policy_get_afinfo(family);
138 if (unlikely(afinfo == NULL))
139 return NULL;
140 typemap = afinfo->type_map;
142 type = typemap[proto];
143 if (unlikely(type && !try_module_get(type->owner)))
144 type = NULL;
145 if (!type && !modload_attempted) {
146 xfrm_policy_put_afinfo(afinfo);
147 request_module("xfrm-type-%d-%d",
148 (int) family, (int) proto);
149 modload_attempted = 1;
150 goto retry;
153 xfrm_policy_put_afinfo(afinfo);
154 return type;
157 int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
158 unsigned short family)
160 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
161 int err = 0;
163 if (unlikely(afinfo == NULL))
164 return -EAFNOSUPPORT;
166 if (likely(afinfo->dst_lookup != NULL))
167 err = afinfo->dst_lookup(dst, fl);
168 else
169 err = -EINVAL;
170 xfrm_policy_put_afinfo(afinfo);
171 return err;
173 EXPORT_SYMBOL(xfrm_dst_lookup);
175 void xfrm_put_type(struct xfrm_type *type)
177 module_put(type->owner);
180 int xfrm_register_mode(struct xfrm_mode *mode, int family)
182 struct xfrm_policy_afinfo *afinfo;
183 struct xfrm_mode **modemap;
184 int err;
186 if (unlikely(mode->encap >= XFRM_MODE_MAX))
187 return -EINVAL;
189 afinfo = xfrm_policy_lock_afinfo(family);
190 if (unlikely(afinfo == NULL))
191 return -EAFNOSUPPORT;
193 err = -EEXIST;
194 modemap = afinfo->mode_map;
195 if (likely(modemap[mode->encap] == NULL)) {
196 modemap[mode->encap] = mode;
197 err = 0;
200 xfrm_policy_unlock_afinfo(afinfo);
201 return err;
203 EXPORT_SYMBOL(xfrm_register_mode);
205 int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
207 struct xfrm_policy_afinfo *afinfo;
208 struct xfrm_mode **modemap;
209 int err;
211 if (unlikely(mode->encap >= XFRM_MODE_MAX))
212 return -EINVAL;
214 afinfo = xfrm_policy_lock_afinfo(family);
215 if (unlikely(afinfo == NULL))
216 return -EAFNOSUPPORT;
218 err = -ENOENT;
219 modemap = afinfo->mode_map;
220 if (likely(modemap[mode->encap] == mode)) {
221 modemap[mode->encap] = NULL;
222 err = 0;
225 xfrm_policy_unlock_afinfo(afinfo);
226 return err;
228 EXPORT_SYMBOL(xfrm_unregister_mode);
230 struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
232 struct xfrm_policy_afinfo *afinfo;
233 struct xfrm_mode *mode;
234 int modload_attempted = 0;
236 if (unlikely(encap >= XFRM_MODE_MAX))
237 return NULL;
239 retry:
240 afinfo = xfrm_policy_get_afinfo(family);
241 if (unlikely(afinfo == NULL))
242 return NULL;
244 mode = afinfo->mode_map[encap];
245 if (unlikely(mode && !try_module_get(mode->owner)))
246 mode = NULL;
247 if (!mode && !modload_attempted) {
248 xfrm_policy_put_afinfo(afinfo);
249 request_module("xfrm-mode-%d-%d", family, encap);
250 modload_attempted = 1;
251 goto retry;
254 xfrm_policy_put_afinfo(afinfo);
255 return mode;
258 void xfrm_put_mode(struct xfrm_mode *mode)
260 module_put(mode->owner);
263 static inline unsigned long make_jiffies(long secs)
265 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
266 return MAX_SCHEDULE_TIMEOUT-1;
267 else
268 return secs*HZ;
271 static void xfrm_policy_timer(unsigned long data)
273 struct xfrm_policy *xp = (struct xfrm_policy*)data;
274 unsigned long now = get_seconds();
275 long next = LONG_MAX;
276 int warn = 0;
277 int dir;
279 read_lock(&xp->lock);
281 if (xp->dead)
282 goto out;
284 dir = xfrm_policy_id2dir(xp->index);
286 if (xp->lft.hard_add_expires_seconds) {
287 long tmo = xp->lft.hard_add_expires_seconds +
288 xp->curlft.add_time - now;
289 if (tmo <= 0)
290 goto expired;
291 if (tmo < next)
292 next = tmo;
294 if (xp->lft.hard_use_expires_seconds) {
295 long tmo = xp->lft.hard_use_expires_seconds +
296 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
297 if (tmo <= 0)
298 goto expired;
299 if (tmo < next)
300 next = tmo;
302 if (xp->lft.soft_add_expires_seconds) {
303 long tmo = xp->lft.soft_add_expires_seconds +
304 xp->curlft.add_time - now;
305 if (tmo <= 0) {
306 warn = 1;
307 tmo = XFRM_KM_TIMEOUT;
309 if (tmo < next)
310 next = tmo;
312 if (xp->lft.soft_use_expires_seconds) {
313 long tmo = xp->lft.soft_use_expires_seconds +
314 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
315 if (tmo <= 0) {
316 warn = 1;
317 tmo = XFRM_KM_TIMEOUT;
319 if (tmo < next)
320 next = tmo;
323 if (warn)
324 km_policy_expired(xp, dir, 0, 0);
325 if (next != LONG_MAX &&
326 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
327 xfrm_pol_hold(xp);
329 out:
330 read_unlock(&xp->lock);
331 xfrm_pol_put(xp);
332 return;
334 expired:
335 read_unlock(&xp->lock);
336 if (!xfrm_policy_delete(xp, dir))
337 km_policy_expired(xp, dir, 1, 0);
338 xfrm_pol_put(xp);
342 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
343 * SPD calls.
346 struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
348 struct xfrm_policy *policy;
350 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
352 if (policy) {
353 INIT_HLIST_NODE(&policy->bydst);
354 INIT_HLIST_NODE(&policy->byidx);
355 rwlock_init(&policy->lock);
356 atomic_set(&policy->refcnt, 1);
357 init_timer(&policy->timer);
358 policy->timer.data = (unsigned long)policy;
359 policy->timer.function = xfrm_policy_timer;
361 return policy;
363 EXPORT_SYMBOL(xfrm_policy_alloc);
365 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
367 void __xfrm_policy_destroy(struct xfrm_policy *policy)
369 BUG_ON(!policy->dead);
371 BUG_ON(policy->bundles);
373 if (del_timer(&policy->timer))
374 BUG();
376 security_xfrm_policy_free(policy);
377 kfree(policy);
379 EXPORT_SYMBOL(__xfrm_policy_destroy);
381 static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
383 struct dst_entry *dst;
385 while ((dst = policy->bundles) != NULL) {
386 policy->bundles = dst->next;
387 dst_free(dst);
390 if (del_timer(&policy->timer))
391 atomic_dec(&policy->refcnt);
393 if (atomic_read(&policy->refcnt) > 1)
394 flow_cache_flush();
396 xfrm_pol_put(policy);
399 static void xfrm_policy_gc_task(struct work_struct *work)
401 struct xfrm_policy *policy;
402 struct hlist_node *entry, *tmp;
403 struct hlist_head gc_list;
405 spin_lock_bh(&xfrm_policy_gc_lock);
406 gc_list.first = xfrm_policy_gc_list.first;
407 INIT_HLIST_HEAD(&xfrm_policy_gc_list);
408 spin_unlock_bh(&xfrm_policy_gc_lock);
410 hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
411 xfrm_policy_gc_kill(policy);
414 /* Rule must be locked. Release descentant resources, announce
415 * entry dead. The rule must be unlinked from lists to the moment.
418 static void xfrm_policy_kill(struct xfrm_policy *policy)
420 int dead;
422 write_lock_bh(&policy->lock);
423 dead = policy->dead;
424 policy->dead = 1;
425 write_unlock_bh(&policy->lock);
427 if (unlikely(dead)) {
428 WARN_ON(1);
429 return;
432 spin_lock(&xfrm_policy_gc_lock);
433 hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
434 spin_unlock(&xfrm_policy_gc_lock);
436 schedule_work(&xfrm_policy_gc_work);
439 struct xfrm_policy_hash {
440 struct hlist_head *table;
441 unsigned int hmask;
444 static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
445 static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
446 static struct hlist_head *xfrm_policy_byidx __read_mostly;
447 static unsigned int xfrm_idx_hmask __read_mostly;
448 static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
450 static inline unsigned int idx_hash(u32 index)
452 return __idx_hash(index, xfrm_idx_hmask);
455 static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
457 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
458 unsigned int hash = __sel_hash(sel, family, hmask);
460 return (hash == hmask + 1 ?
461 &xfrm_policy_inexact[dir] :
462 xfrm_policy_bydst[dir].table + hash);
465 static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
467 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
468 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
470 return xfrm_policy_bydst[dir].table + hash;
473 static void xfrm_dst_hash_transfer(struct hlist_head *list,
474 struct hlist_head *ndsttable,
475 unsigned int nhashmask)
477 struct hlist_node *entry, *tmp;
478 struct xfrm_policy *pol;
480 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
481 unsigned int h;
483 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
484 pol->family, nhashmask);
485 hlist_add_head(&pol->bydst, ndsttable+h);
489 static void xfrm_idx_hash_transfer(struct hlist_head *list,
490 struct hlist_head *nidxtable,
491 unsigned int nhashmask)
493 struct hlist_node *entry, *tmp;
494 struct xfrm_policy *pol;
496 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
497 unsigned int h;
499 h = __idx_hash(pol->index, nhashmask);
500 hlist_add_head(&pol->byidx, nidxtable+h);
504 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
506 return ((old_hmask + 1) << 1) - 1;
509 static void xfrm_bydst_resize(int dir)
511 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
512 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
513 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
514 struct hlist_head *odst = xfrm_policy_bydst[dir].table;
515 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
516 int i;
518 if (!ndst)
519 return;
521 write_lock_bh(&xfrm_policy_lock);
523 for (i = hmask; i >= 0; i--)
524 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
526 xfrm_policy_bydst[dir].table = ndst;
527 xfrm_policy_bydst[dir].hmask = nhashmask;
529 write_unlock_bh(&xfrm_policy_lock);
531 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
534 static void xfrm_byidx_resize(int total)
536 unsigned int hmask = xfrm_idx_hmask;
537 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
538 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
539 struct hlist_head *oidx = xfrm_policy_byidx;
540 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
541 int i;
543 if (!nidx)
544 return;
546 write_lock_bh(&xfrm_policy_lock);
548 for (i = hmask; i >= 0; i--)
549 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
551 xfrm_policy_byidx = nidx;
552 xfrm_idx_hmask = nhashmask;
554 write_unlock_bh(&xfrm_policy_lock);
556 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
559 static inline int xfrm_bydst_should_resize(int dir, int *total)
561 unsigned int cnt = xfrm_policy_count[dir];
562 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
564 if (total)
565 *total += cnt;
567 if ((hmask + 1) < xfrm_policy_hashmax &&
568 cnt > hmask)
569 return 1;
571 return 0;
574 static inline int xfrm_byidx_should_resize(int total)
576 unsigned int hmask = xfrm_idx_hmask;
578 if ((hmask + 1) < xfrm_policy_hashmax &&
579 total > hmask)
580 return 1;
582 return 0;
585 void xfrm_spd_getinfo(struct xfrmk_spdinfo *si)
587 read_lock_bh(&xfrm_policy_lock);
588 si->incnt = xfrm_policy_count[XFRM_POLICY_IN];
589 si->outcnt = xfrm_policy_count[XFRM_POLICY_OUT];
590 si->fwdcnt = xfrm_policy_count[XFRM_POLICY_FWD];
591 si->inscnt = xfrm_policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
592 si->outscnt = xfrm_policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
593 si->fwdscnt = xfrm_policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
594 si->spdhcnt = xfrm_idx_hmask;
595 si->spdhmcnt = xfrm_policy_hashmax;
596 read_unlock_bh(&xfrm_policy_lock);
598 EXPORT_SYMBOL(xfrm_spd_getinfo);
600 static DEFINE_MUTEX(hash_resize_mutex);
601 static void xfrm_hash_resize(struct work_struct *__unused)
603 int dir, total;
605 mutex_lock(&hash_resize_mutex);
607 total = 0;
608 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
609 if (xfrm_bydst_should_resize(dir, &total))
610 xfrm_bydst_resize(dir);
612 if (xfrm_byidx_should_resize(total))
613 xfrm_byidx_resize(total);
615 mutex_unlock(&hash_resize_mutex);
618 static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
620 /* Generate new index... KAME seems to generate them ordered by cost
621 * of an absolute inpredictability of ordering of rules. This will not pass. */
622 static u32 xfrm_gen_index(u8 type, int dir)
624 static u32 idx_generator;
626 for (;;) {
627 struct hlist_node *entry;
628 struct hlist_head *list;
629 struct xfrm_policy *p;
630 u32 idx;
631 int found;
633 idx = (idx_generator | dir);
634 idx_generator += 8;
635 if (idx == 0)
636 idx = 8;
637 list = xfrm_policy_byidx + idx_hash(idx);
638 found = 0;
639 hlist_for_each_entry(p, entry, list, byidx) {
640 if (p->index == idx) {
641 found = 1;
642 break;
645 if (!found)
646 return idx;
650 static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
652 u32 *p1 = (u32 *) s1;
653 u32 *p2 = (u32 *) s2;
654 int len = sizeof(struct xfrm_selector) / sizeof(u32);
655 int i;
657 for (i = 0; i < len; i++) {
658 if (p1[i] != p2[i])
659 return 1;
662 return 0;
665 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
667 struct xfrm_policy *pol;
668 struct xfrm_policy *delpol;
669 struct hlist_head *chain;
670 struct hlist_node *entry, *newpos;
671 struct dst_entry *gc_list;
673 write_lock_bh(&xfrm_policy_lock);
674 chain = policy_hash_bysel(&policy->selector, policy->family, dir);
675 delpol = NULL;
676 newpos = NULL;
677 hlist_for_each_entry(pol, entry, chain, bydst) {
678 if (pol->type == policy->type &&
679 !selector_cmp(&pol->selector, &policy->selector) &&
680 xfrm_sec_ctx_match(pol->security, policy->security) &&
681 !WARN_ON(delpol)) {
682 if (excl) {
683 write_unlock_bh(&xfrm_policy_lock);
684 return -EEXIST;
686 delpol = pol;
687 if (policy->priority > pol->priority)
688 continue;
689 } else if (policy->priority >= pol->priority) {
690 newpos = &pol->bydst;
691 continue;
693 if (delpol)
694 break;
696 if (newpos)
697 hlist_add_after(newpos, &policy->bydst);
698 else
699 hlist_add_head(&policy->bydst, chain);
700 xfrm_pol_hold(policy);
701 xfrm_policy_count[dir]++;
702 atomic_inc(&flow_cache_genid);
703 if (delpol) {
704 hlist_del(&delpol->bydst);
705 hlist_del(&delpol->byidx);
706 xfrm_policy_count[dir]--;
708 policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
709 hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
710 policy->curlft.add_time = get_seconds();
711 policy->curlft.use_time = 0;
712 if (!mod_timer(&policy->timer, jiffies + HZ))
713 xfrm_pol_hold(policy);
714 write_unlock_bh(&xfrm_policy_lock);
716 if (delpol)
717 xfrm_policy_kill(delpol);
718 else if (xfrm_bydst_should_resize(dir, NULL))
719 schedule_work(&xfrm_hash_work);
721 read_lock_bh(&xfrm_policy_lock);
722 gc_list = NULL;
723 entry = &policy->bydst;
724 hlist_for_each_entry_continue(policy, entry, bydst) {
725 struct dst_entry *dst;
727 write_lock(&policy->lock);
728 dst = policy->bundles;
729 if (dst) {
730 struct dst_entry *tail = dst;
731 while (tail->next)
732 tail = tail->next;
733 tail->next = gc_list;
734 gc_list = dst;
736 policy->bundles = NULL;
738 write_unlock(&policy->lock);
740 read_unlock_bh(&xfrm_policy_lock);
742 while (gc_list) {
743 struct dst_entry *dst = gc_list;
745 gc_list = dst->next;
746 dst_free(dst);
749 return 0;
751 EXPORT_SYMBOL(xfrm_policy_insert);
753 struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
754 struct xfrm_selector *sel,
755 struct xfrm_sec_ctx *ctx, int delete,
756 int *err)
758 struct xfrm_policy *pol, *ret;
759 struct hlist_head *chain;
760 struct hlist_node *entry;
762 *err = 0;
763 write_lock_bh(&xfrm_policy_lock);
764 chain = policy_hash_bysel(sel, sel->family, dir);
765 ret = NULL;
766 hlist_for_each_entry(pol, entry, chain, bydst) {
767 if (pol->type == type &&
768 !selector_cmp(sel, &pol->selector) &&
769 xfrm_sec_ctx_match(ctx, pol->security)) {
770 xfrm_pol_hold(pol);
771 if (delete) {
772 *err = security_xfrm_policy_delete(pol);
773 if (*err) {
774 write_unlock_bh(&xfrm_policy_lock);
775 return pol;
777 hlist_del(&pol->bydst);
778 hlist_del(&pol->byidx);
779 xfrm_policy_count[dir]--;
781 ret = pol;
782 break;
785 write_unlock_bh(&xfrm_policy_lock);
787 if (ret && delete) {
788 atomic_inc(&flow_cache_genid);
789 xfrm_policy_kill(ret);
791 return ret;
793 EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
795 struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete,
796 int *err)
798 struct xfrm_policy *pol, *ret;
799 struct hlist_head *chain;
800 struct hlist_node *entry;
802 *err = -ENOENT;
803 if (xfrm_policy_id2dir(id) != dir)
804 return NULL;
806 *err = 0;
807 write_lock_bh(&xfrm_policy_lock);
808 chain = xfrm_policy_byidx + idx_hash(id);
809 ret = NULL;
810 hlist_for_each_entry(pol, entry, chain, byidx) {
811 if (pol->type == type && pol->index == id) {
812 xfrm_pol_hold(pol);
813 if (delete) {
814 *err = security_xfrm_policy_delete(pol);
815 if (*err) {
816 write_unlock_bh(&xfrm_policy_lock);
817 return pol;
819 hlist_del(&pol->bydst);
820 hlist_del(&pol->byidx);
821 xfrm_policy_count[dir]--;
823 ret = pol;
824 break;
827 write_unlock_bh(&xfrm_policy_lock);
829 if (ret && delete) {
830 atomic_inc(&flow_cache_genid);
831 xfrm_policy_kill(ret);
833 return ret;
835 EXPORT_SYMBOL(xfrm_policy_byid);
837 #ifdef CONFIG_SECURITY_NETWORK_XFRM
838 static inline int
839 xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
841 int dir, err = 0;
843 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
844 struct xfrm_policy *pol;
845 struct hlist_node *entry;
846 int i;
848 hlist_for_each_entry(pol, entry,
849 &xfrm_policy_inexact[dir], bydst) {
850 if (pol->type != type)
851 continue;
852 err = security_xfrm_policy_delete(pol);
853 if (err) {
854 xfrm_audit_log(audit_info->loginuid,
855 audit_info->secid,
856 AUDIT_MAC_IPSEC_DELSPD, 0,
857 pol, NULL);
858 return err;
861 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
862 hlist_for_each_entry(pol, entry,
863 xfrm_policy_bydst[dir].table + i,
864 bydst) {
865 if (pol->type != type)
866 continue;
867 err = security_xfrm_policy_delete(pol);
868 if (err) {
869 xfrm_audit_log(audit_info->loginuid,
870 audit_info->secid,
871 AUDIT_MAC_IPSEC_DELSPD,
872 0, pol, NULL);
873 return err;
878 return err;
880 #else
881 static inline int
882 xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
884 return 0;
886 #endif
888 int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
890 int dir, err = 0;
892 write_lock_bh(&xfrm_policy_lock);
894 err = xfrm_policy_flush_secctx_check(type, audit_info);
895 if (err)
896 goto out;
898 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
899 struct xfrm_policy *pol;
900 struct hlist_node *entry;
901 int i, killed;
903 killed = 0;
904 again1:
905 hlist_for_each_entry(pol, entry,
906 &xfrm_policy_inexact[dir], bydst) {
907 if (pol->type != type)
908 continue;
909 hlist_del(&pol->bydst);
910 hlist_del(&pol->byidx);
911 write_unlock_bh(&xfrm_policy_lock);
913 xfrm_audit_log(audit_info->loginuid, audit_info->secid,
914 AUDIT_MAC_IPSEC_DELSPD, 1, pol, NULL);
916 xfrm_policy_kill(pol);
917 killed++;
919 write_lock_bh(&xfrm_policy_lock);
920 goto again1;
923 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
924 again2:
925 hlist_for_each_entry(pol, entry,
926 xfrm_policy_bydst[dir].table + i,
927 bydst) {
928 if (pol->type != type)
929 continue;
930 hlist_del(&pol->bydst);
931 hlist_del(&pol->byidx);
932 write_unlock_bh(&xfrm_policy_lock);
934 xfrm_audit_log(audit_info->loginuid,
935 audit_info->secid,
936 AUDIT_MAC_IPSEC_DELSPD, 1,
937 pol, NULL);
939 xfrm_policy_kill(pol);
940 killed++;
942 write_lock_bh(&xfrm_policy_lock);
943 goto again2;
947 xfrm_policy_count[dir] -= killed;
949 atomic_inc(&flow_cache_genid);
950 out:
951 write_unlock_bh(&xfrm_policy_lock);
952 return err;
954 EXPORT_SYMBOL(xfrm_policy_flush);
956 int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
957 void *data)
959 struct xfrm_policy *pol, *last = NULL;
960 struct hlist_node *entry;
961 int dir, last_dir = 0, count, error;
963 read_lock_bh(&xfrm_policy_lock);
964 count = 0;
966 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
967 struct hlist_head *table = xfrm_policy_bydst[dir].table;
968 int i;
970 hlist_for_each_entry(pol, entry,
971 &xfrm_policy_inexact[dir], bydst) {
972 if (pol->type != type)
973 continue;
974 if (last) {
975 error = func(last, last_dir % XFRM_POLICY_MAX,
976 count, data);
977 if (error)
978 goto out;
980 last = pol;
981 last_dir = dir;
982 count++;
984 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
985 hlist_for_each_entry(pol, entry, table + i, bydst) {
986 if (pol->type != type)
987 continue;
988 if (last) {
989 error = func(last, last_dir % XFRM_POLICY_MAX,
990 count, data);
991 if (error)
992 goto out;
994 last = pol;
995 last_dir = dir;
996 count++;
1000 if (count == 0) {
1001 error = -ENOENT;
1002 goto out;
1004 error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
1005 out:
1006 read_unlock_bh(&xfrm_policy_lock);
1007 return error;
1009 EXPORT_SYMBOL(xfrm_policy_walk);
1012 * Find policy to apply to this flow.
1014 * Returns 0 if policy found, else an -errno.
1016 static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
1017 u8 type, u16 family, int dir)
1019 struct xfrm_selector *sel = &pol->selector;
1020 int match, ret = -ESRCH;
1022 if (pol->family != family ||
1023 pol->type != type)
1024 return ret;
1026 match = xfrm_selector_match(sel, fl, family);
1027 if (match)
1028 ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
1030 return ret;
1033 static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
1034 u16 family, u8 dir)
1036 int err;
1037 struct xfrm_policy *pol, *ret;
1038 xfrm_address_t *daddr, *saddr;
1039 struct hlist_node *entry;
1040 struct hlist_head *chain;
1041 u32 priority = ~0U;
1043 daddr = xfrm_flowi_daddr(fl, family);
1044 saddr = xfrm_flowi_saddr(fl, family);
1045 if (unlikely(!daddr || !saddr))
1046 return NULL;
1048 read_lock_bh(&xfrm_policy_lock);
1049 chain = policy_hash_direct(daddr, saddr, family, dir);
1050 ret = NULL;
1051 hlist_for_each_entry(pol, entry, chain, bydst) {
1052 err = xfrm_policy_match(pol, fl, type, family, dir);
1053 if (err) {
1054 if (err == -ESRCH)
1055 continue;
1056 else {
1057 ret = ERR_PTR(err);
1058 goto fail;
1060 } else {
1061 ret = pol;
1062 priority = ret->priority;
1063 break;
1066 chain = &xfrm_policy_inexact[dir];
1067 hlist_for_each_entry(pol, entry, chain, bydst) {
1068 err = xfrm_policy_match(pol, fl, type, family, dir);
1069 if (err) {
1070 if (err == -ESRCH)
1071 continue;
1072 else {
1073 ret = ERR_PTR(err);
1074 goto fail;
1076 } else if (pol->priority < priority) {
1077 ret = pol;
1078 break;
1081 if (ret)
1082 xfrm_pol_hold(ret);
1083 fail:
1084 read_unlock_bh(&xfrm_policy_lock);
1086 return ret;
1089 static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
1090 void **objp, atomic_t **obj_refp)
1092 struct xfrm_policy *pol;
1093 int err = 0;
1095 #ifdef CONFIG_XFRM_SUB_POLICY
1096 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
1097 if (IS_ERR(pol)) {
1098 err = PTR_ERR(pol);
1099 pol = NULL;
1101 if (pol || err)
1102 goto end;
1103 #endif
1104 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1105 if (IS_ERR(pol)) {
1106 err = PTR_ERR(pol);
1107 pol = NULL;
1109 #ifdef CONFIG_XFRM_SUB_POLICY
1110 end:
1111 #endif
1112 if ((*objp = (void *) pol) != NULL)
1113 *obj_refp = &pol->refcnt;
1114 return err;
1117 static inline int policy_to_flow_dir(int dir)
1119 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1120 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1121 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1122 return dir;
1123 switch (dir) {
1124 default:
1125 case XFRM_POLICY_IN:
1126 return FLOW_DIR_IN;
1127 case XFRM_POLICY_OUT:
1128 return FLOW_DIR_OUT;
1129 case XFRM_POLICY_FWD:
1130 return FLOW_DIR_FWD;
1134 static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
1136 struct xfrm_policy *pol;
1138 read_lock_bh(&xfrm_policy_lock);
1139 if ((pol = sk->sk_policy[dir]) != NULL) {
1140 int match = xfrm_selector_match(&pol->selector, fl,
1141 sk->sk_family);
1142 int err = 0;
1144 if (match) {
1145 err = security_xfrm_policy_lookup(pol, fl->secid,
1146 policy_to_flow_dir(dir));
1147 if (!err)
1148 xfrm_pol_hold(pol);
1149 else if (err == -ESRCH)
1150 pol = NULL;
1151 else
1152 pol = ERR_PTR(err);
1153 } else
1154 pol = NULL;
1156 read_unlock_bh(&xfrm_policy_lock);
1157 return pol;
1160 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1162 struct hlist_head *chain = policy_hash_bysel(&pol->selector,
1163 pol->family, dir);
1165 hlist_add_head(&pol->bydst, chain);
1166 hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
1167 xfrm_policy_count[dir]++;
1168 xfrm_pol_hold(pol);
1170 if (xfrm_bydst_should_resize(dir, NULL))
1171 schedule_work(&xfrm_hash_work);
1174 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1175 int dir)
1177 if (hlist_unhashed(&pol->bydst))
1178 return NULL;
1180 hlist_del(&pol->bydst);
1181 hlist_del(&pol->byidx);
1182 xfrm_policy_count[dir]--;
1184 return pol;
1187 int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
1189 write_lock_bh(&xfrm_policy_lock);
1190 pol = __xfrm_policy_unlink(pol, dir);
1191 write_unlock_bh(&xfrm_policy_lock);
1192 if (pol) {
1193 if (dir < XFRM_POLICY_MAX)
1194 atomic_inc(&flow_cache_genid);
1195 xfrm_policy_kill(pol);
1196 return 0;
1198 return -ENOENT;
1200 EXPORT_SYMBOL(xfrm_policy_delete);
1202 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1204 struct xfrm_policy *old_pol;
1206 #ifdef CONFIG_XFRM_SUB_POLICY
1207 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1208 return -EINVAL;
1209 #endif
1211 write_lock_bh(&xfrm_policy_lock);
1212 old_pol = sk->sk_policy[dir];
1213 sk->sk_policy[dir] = pol;
1214 if (pol) {
1215 pol->curlft.add_time = get_seconds();
1216 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
1217 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1219 if (old_pol)
1220 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1221 write_unlock_bh(&xfrm_policy_lock);
1223 if (old_pol) {
1224 xfrm_policy_kill(old_pol);
1226 return 0;
1229 static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1231 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
1233 if (newp) {
1234 newp->selector = old->selector;
1235 if (security_xfrm_policy_clone(old, newp)) {
1236 kfree(newp);
1237 return NULL; /* ENOMEM */
1239 newp->lft = old->lft;
1240 newp->curlft = old->curlft;
1241 newp->action = old->action;
1242 newp->flags = old->flags;
1243 newp->xfrm_nr = old->xfrm_nr;
1244 newp->index = old->index;
1245 newp->type = old->type;
1246 memcpy(newp->xfrm_vec, old->xfrm_vec,
1247 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1248 write_lock_bh(&xfrm_policy_lock);
1249 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1250 write_unlock_bh(&xfrm_policy_lock);
1251 xfrm_pol_put(newp);
1253 return newp;
1256 int __xfrm_sk_clone_policy(struct sock *sk)
1258 struct xfrm_policy *p0 = sk->sk_policy[0],
1259 *p1 = sk->sk_policy[1];
1261 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1262 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1263 return -ENOMEM;
1264 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1265 return -ENOMEM;
1266 return 0;
1269 static int
1270 xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
1271 unsigned short family)
1273 int err;
1274 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1276 if (unlikely(afinfo == NULL))
1277 return -EINVAL;
1278 err = afinfo->get_saddr(local, remote);
1279 xfrm_policy_put_afinfo(afinfo);
1280 return err;
1283 /* Resolve list of templates for the flow, given policy. */
1285 static int
1286 xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1287 struct xfrm_state **xfrm,
1288 unsigned short family)
1290 int nx;
1291 int i, error;
1292 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1293 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
1294 xfrm_address_t tmp;
1296 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1297 struct xfrm_state *x;
1298 xfrm_address_t *remote = daddr;
1299 xfrm_address_t *local = saddr;
1300 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1302 if (tmpl->mode == XFRM_MODE_TUNNEL) {
1303 remote = &tmpl->id.daddr;
1304 local = &tmpl->saddr;
1305 family = tmpl->encap_family;
1306 if (xfrm_addr_any(local, family)) {
1307 error = xfrm_get_saddr(&tmp, remote, family);
1308 if (error)
1309 goto fail;
1310 local = &tmp;
1314 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1316 if (x && x->km.state == XFRM_STATE_VALID) {
1317 xfrm[nx++] = x;
1318 daddr = remote;
1319 saddr = local;
1320 continue;
1322 if (x) {
1323 error = (x->km.state == XFRM_STATE_ERROR ?
1324 -EINVAL : -EAGAIN);
1325 xfrm_state_put(x);
1328 if (!tmpl->optional)
1329 goto fail;
1331 return nx;
1333 fail:
1334 for (nx--; nx>=0; nx--)
1335 xfrm_state_put(xfrm[nx]);
1336 return error;
1339 static int
1340 xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1341 struct xfrm_state **xfrm,
1342 unsigned short family)
1344 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1345 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
1346 int cnx = 0;
1347 int error;
1348 int ret;
1349 int i;
1351 for (i = 0; i < npols; i++) {
1352 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1353 error = -ENOBUFS;
1354 goto fail;
1357 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
1358 if (ret < 0) {
1359 error = ret;
1360 goto fail;
1361 } else
1362 cnx += ret;
1365 /* found states are sorted for outbound processing */
1366 if (npols > 1)
1367 xfrm_state_sort(xfrm, tpp, cnx, family);
1369 return cnx;
1371 fail:
1372 for (cnx--; cnx>=0; cnx--)
1373 xfrm_state_put(tpp[cnx]);
1374 return error;
1378 /* Check that the bundle accepts the flow and its components are
1379 * still valid.
1382 static struct dst_entry *
1383 xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1385 struct dst_entry *x;
1386 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1387 if (unlikely(afinfo == NULL))
1388 return ERR_PTR(-EINVAL);
1389 x = afinfo->find_bundle(fl, policy);
1390 xfrm_policy_put_afinfo(afinfo);
1391 return x;
1394 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1395 * all the metrics... Shortly, bundle a bundle.
1398 static int
1399 xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
1400 struct flowi *fl, struct dst_entry **dst_p,
1401 unsigned short family)
1403 int err;
1404 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1405 if (unlikely(afinfo == NULL))
1406 return -EINVAL;
1407 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
1408 xfrm_policy_put_afinfo(afinfo);
1409 return err;
1412 static int inline
1413 xfrm_dst_alloc_copy(void **target, void *src, int size)
1415 if (!*target) {
1416 *target = kmalloc(size, GFP_ATOMIC);
1417 if (!*target)
1418 return -ENOMEM;
1420 memcpy(*target, src, size);
1421 return 0;
1424 static int inline
1425 xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
1427 #ifdef CONFIG_XFRM_SUB_POLICY
1428 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1429 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1430 sel, sizeof(*sel));
1431 #else
1432 return 0;
1433 #endif
1436 static int inline
1437 xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
1439 #ifdef CONFIG_XFRM_SUB_POLICY
1440 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1441 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1442 #else
1443 return 0;
1444 #endif
1447 static int stale_bundle(struct dst_entry *dst);
1449 /* Main function: finds/creates a bundle for given flow.
1451 * At the moment we eat a raw IP route. Mostly to speed up lookups
1452 * on interfaces with disabled IPsec.
1454 int __xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1455 struct sock *sk, int flags)
1457 struct xfrm_policy *policy;
1458 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1459 int npols;
1460 int pol_dead;
1461 int xfrm_nr;
1462 int pi;
1463 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1464 struct dst_entry *dst, *dst_orig = *dst_p;
1465 int nx = 0;
1466 int err;
1467 u32 genid;
1468 u16 family;
1469 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
1471 restart:
1472 genid = atomic_read(&flow_cache_genid);
1473 policy = NULL;
1474 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1475 pols[pi] = NULL;
1476 npols = 0;
1477 pol_dead = 0;
1478 xfrm_nr = 0;
1480 if (sk && sk->sk_policy[1]) {
1481 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
1482 err = PTR_ERR(policy);
1483 if (IS_ERR(policy))
1484 goto dropdst;
1487 if (!policy) {
1488 /* To accelerate a bit... */
1489 if ((dst_orig->flags & DST_NOXFRM) ||
1490 !xfrm_policy_count[XFRM_POLICY_OUT])
1491 return 0;
1493 policy = flow_cache_lookup(fl, dst_orig->ops->family,
1494 dir, xfrm_policy_lookup);
1495 err = PTR_ERR(policy);
1496 if (IS_ERR(policy))
1497 goto dropdst;
1500 if (!policy)
1501 return 0;
1503 family = dst_orig->ops->family;
1504 policy->curlft.use_time = get_seconds();
1505 pols[0] = policy;
1506 npols ++;
1507 xfrm_nr += pols[0]->xfrm_nr;
1509 switch (policy->action) {
1510 case XFRM_POLICY_BLOCK:
1511 /* Prohibit the flow */
1512 err = -EPERM;
1513 goto error;
1515 case XFRM_POLICY_ALLOW:
1516 #ifndef CONFIG_XFRM_SUB_POLICY
1517 if (policy->xfrm_nr == 0) {
1518 /* Flow passes not transformed. */
1519 xfrm_pol_put(policy);
1520 return 0;
1522 #endif
1524 /* Try to find matching bundle.
1526 * LATER: help from flow cache. It is optional, this
1527 * is required only for output policy.
1529 dst = xfrm_find_bundle(fl, policy, family);
1530 if (IS_ERR(dst)) {
1531 err = PTR_ERR(dst);
1532 goto error;
1535 if (dst)
1536 break;
1538 #ifdef CONFIG_XFRM_SUB_POLICY
1539 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1540 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1541 fl, family,
1542 XFRM_POLICY_OUT);
1543 if (pols[1]) {
1544 if (IS_ERR(pols[1])) {
1545 err = PTR_ERR(pols[1]);
1546 goto error;
1548 if (pols[1]->action == XFRM_POLICY_BLOCK) {
1549 err = -EPERM;
1550 goto error;
1552 npols ++;
1553 xfrm_nr += pols[1]->xfrm_nr;
1558 * Because neither flowi nor bundle information knows about
1559 * transformation template size. On more than one policy usage
1560 * we can realize whether all of them is bypass or not after
1561 * they are searched. See above not-transformed bypass
1562 * is surrounded by non-sub policy configuration, too.
1564 if (xfrm_nr == 0) {
1565 /* Flow passes not transformed. */
1566 xfrm_pols_put(pols, npols);
1567 return 0;
1570 #endif
1571 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
1573 if (unlikely(nx<0)) {
1574 err = nx;
1575 if (err == -EAGAIN && sysctl_xfrm_larval_drop) {
1576 /* EREMOTE tells the caller to generate
1577 * a one-shot blackhole route.
1579 xfrm_pol_put(policy);
1580 return -EREMOTE;
1582 if (err == -EAGAIN && flags) {
1583 DECLARE_WAITQUEUE(wait, current);
1585 add_wait_queue(&km_waitq, &wait);
1586 set_current_state(TASK_INTERRUPTIBLE);
1587 schedule();
1588 set_current_state(TASK_RUNNING);
1589 remove_wait_queue(&km_waitq, &wait);
1591 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
1593 if (nx == -EAGAIN && signal_pending(current)) {
1594 err = -ERESTART;
1595 goto error;
1597 if (nx == -EAGAIN ||
1598 genid != atomic_read(&flow_cache_genid)) {
1599 xfrm_pols_put(pols, npols);
1600 goto restart;
1602 err = nx;
1604 if (err < 0)
1605 goto error;
1607 if (nx == 0) {
1608 /* Flow passes not transformed. */
1609 xfrm_pols_put(pols, npols);
1610 return 0;
1613 dst = dst_orig;
1614 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
1616 if (unlikely(err)) {
1617 int i;
1618 for (i=0; i<nx; i++)
1619 xfrm_state_put(xfrm[i]);
1620 goto error;
1623 for (pi = 0; pi < npols; pi++) {
1624 read_lock_bh(&pols[pi]->lock);
1625 pol_dead |= pols[pi]->dead;
1626 read_unlock_bh(&pols[pi]->lock);
1629 write_lock_bh(&policy->lock);
1630 if (unlikely(pol_dead || stale_bundle(dst))) {
1631 /* Wow! While we worked on resolving, this
1632 * policy has gone. Retry. It is not paranoia,
1633 * we just cannot enlist new bundle to dead object.
1634 * We can't enlist stable bundles either.
1636 write_unlock_bh(&policy->lock);
1637 if (dst)
1638 dst_free(dst);
1640 err = -EHOSTUNREACH;
1641 goto error;
1644 if (npols > 1)
1645 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1646 else
1647 err = xfrm_dst_update_origin(dst, fl);
1648 if (unlikely(err)) {
1649 write_unlock_bh(&policy->lock);
1650 if (dst)
1651 dst_free(dst);
1652 goto error;
1655 dst->next = policy->bundles;
1656 policy->bundles = dst;
1657 dst_hold(dst);
1658 write_unlock_bh(&policy->lock);
1660 *dst_p = dst;
1661 dst_release(dst_orig);
1662 xfrm_pols_put(pols, npols);
1663 return 0;
1665 error:
1666 xfrm_pols_put(pols, npols);
1667 dropdst:
1668 dst_release(dst_orig);
1669 *dst_p = NULL;
1670 return err;
1672 EXPORT_SYMBOL(__xfrm_lookup);
1674 int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1675 struct sock *sk, int flags)
1677 int err = __xfrm_lookup(dst_p, fl, sk, flags);
1679 if (err == -EREMOTE) {
1680 dst_release(*dst_p);
1681 *dst_p = NULL;
1682 err = -EAGAIN;
1685 return err;
1687 EXPORT_SYMBOL(xfrm_lookup);
1689 static inline int
1690 xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1692 struct xfrm_state *x;
1693 int err;
1695 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1696 return 0;
1697 x = skb->sp->xvec[idx];
1698 if (!x->type->reject)
1699 return 0;
1700 xfrm_state_hold(x);
1701 err = x->type->reject(x, skb, fl);
1702 xfrm_state_put(x);
1703 return err;
1706 /* When skb is transformed back to its "native" form, we have to
1707 * check policy restrictions. At the moment we make this in maximally
1708 * stupid way. Shame on me. :-) Of course, connected sockets must
1709 * have policy cached at them.
1712 static inline int
1713 xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
1714 unsigned short family)
1716 if (xfrm_state_kern(x))
1717 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
1718 return x->id.proto == tmpl->id.proto &&
1719 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1720 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1721 x->props.mode == tmpl->mode &&
1722 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1723 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
1724 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1725 xfrm_state_addr_cmp(tmpl, x, family));
1729 * 0 or more than 0 is returned when validation is succeeded (either bypass
1730 * because of optional transport mode, or next index of the mathced secpath
1731 * state with the template.
1732 * -1 is returned when no matching template is found.
1733 * Otherwise "-2 - errored_index" is returned.
1735 static inline int
1736 xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1737 unsigned short family)
1739 int idx = start;
1741 if (tmpl->optional) {
1742 if (tmpl->mode == XFRM_MODE_TRANSPORT)
1743 return start;
1744 } else
1745 start = -1;
1746 for (; idx < sp->len; idx++) {
1747 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
1748 return ++idx;
1749 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1750 if (start == -1)
1751 start = -2-idx;
1752 break;
1755 return start;
1759 xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
1761 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1762 int err;
1764 if (unlikely(afinfo == NULL))
1765 return -EAFNOSUPPORT;
1767 afinfo->decode_session(skb, fl);
1768 err = security_xfrm_decode_session(skb, &fl->secid);
1769 xfrm_policy_put_afinfo(afinfo);
1770 return err;
1772 EXPORT_SYMBOL(xfrm_decode_session);
1774 static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
1776 for (; k < sp->len; k++) {
1777 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
1778 *idxp = k;
1779 return 1;
1783 return 0;
1786 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
1787 unsigned short family)
1789 struct xfrm_policy *pol;
1790 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1791 int npols = 0;
1792 int xfrm_nr;
1793 int pi;
1794 struct flowi fl;
1795 u8 fl_dir = policy_to_flow_dir(dir);
1796 int xerr_idx = -1;
1798 if (xfrm_decode_session(skb, &fl, family) < 0)
1799 return 0;
1800 nf_nat_decode_session(skb, &fl, family);
1802 /* First, check used SA against their selectors. */
1803 if (skb->sp) {
1804 int i;
1806 for (i=skb->sp->len-1; i>=0; i--) {
1807 struct xfrm_state *x = skb->sp->xvec[i];
1808 if (!xfrm_selector_match(&x->sel, &fl, family))
1809 return 0;
1813 pol = NULL;
1814 if (sk && sk->sk_policy[dir]) {
1815 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
1816 if (IS_ERR(pol))
1817 return 0;
1820 if (!pol)
1821 pol = flow_cache_lookup(&fl, family, fl_dir,
1822 xfrm_policy_lookup);
1824 if (IS_ERR(pol))
1825 return 0;
1827 if (!pol) {
1828 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
1829 xfrm_secpath_reject(xerr_idx, skb, &fl);
1830 return 0;
1832 return 1;
1835 pol->curlft.use_time = get_seconds();
1837 pols[0] = pol;
1838 npols ++;
1839 #ifdef CONFIG_XFRM_SUB_POLICY
1840 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1841 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1842 &fl, family,
1843 XFRM_POLICY_IN);
1844 if (pols[1]) {
1845 if (IS_ERR(pols[1]))
1846 return 0;
1847 pols[1]->curlft.use_time = get_seconds();
1848 npols ++;
1851 #endif
1853 if (pol->action == XFRM_POLICY_ALLOW) {
1854 struct sec_path *sp;
1855 static struct sec_path dummy;
1856 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
1857 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
1858 struct xfrm_tmpl **tpp = tp;
1859 int ti = 0;
1860 int i, k;
1862 if ((sp = skb->sp) == NULL)
1863 sp = &dummy;
1865 for (pi = 0; pi < npols; pi++) {
1866 if (pols[pi] != pol &&
1867 pols[pi]->action != XFRM_POLICY_ALLOW)
1868 goto reject;
1869 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
1870 goto reject_error;
1871 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1872 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1874 xfrm_nr = ti;
1875 if (npols > 1) {
1876 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1877 tpp = stp;
1880 /* For each tunnel xfrm, find the first matching tmpl.
1881 * For each tmpl before that, find corresponding xfrm.
1882 * Order is _important_. Later we will implement
1883 * some barriers, but at the moment barriers
1884 * are implied between each two transformations.
1886 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1887 k = xfrm_policy_ok(tpp[i], sp, k, family);
1888 if (k < 0) {
1889 if (k < -1)
1890 /* "-2 - errored_index" returned */
1891 xerr_idx = -(2+k);
1892 goto reject;
1896 if (secpath_has_nontransport(sp, k, &xerr_idx))
1897 goto reject;
1899 xfrm_pols_put(pols, npols);
1900 return 1;
1903 reject:
1904 xfrm_secpath_reject(xerr_idx, skb, &fl);
1905 reject_error:
1906 xfrm_pols_put(pols, npols);
1907 return 0;
1909 EXPORT_SYMBOL(__xfrm_policy_check);
1911 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1913 struct flowi fl;
1915 if (xfrm_decode_session(skb, &fl, family) < 0)
1916 return 0;
1918 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1920 EXPORT_SYMBOL(__xfrm_route_forward);
1922 /* Optimize later using cookies and generation ids. */
1924 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1926 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1927 * to "-1" to force all XFRM destinations to get validated by
1928 * dst_ops->check on every use. We do this because when a
1929 * normal route referenced by an XFRM dst is obsoleted we do
1930 * not go looking around for all parent referencing XFRM dsts
1931 * so that we can invalidate them. It is just too much work.
1932 * Instead we make the checks here on every use. For example:
1934 * XFRM dst A --> IPv4 dst X
1936 * X is the "xdst->route" of A (X is also the "dst->path" of A
1937 * in this example). If X is marked obsolete, "A" will not
1938 * notice. That's what we are validating here via the
1939 * stale_bundle() check.
1941 * When a policy's bundle is pruned, we dst_free() the XFRM
1942 * dst which causes it's ->obsolete field to be set to a
1943 * positive non-zero integer. If an XFRM dst has been pruned
1944 * like this, we want to force a new route lookup.
1946 if (dst->obsolete < 0 && !stale_bundle(dst))
1947 return dst;
1949 return NULL;
1952 static int stale_bundle(struct dst_entry *dst)
1954 return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
1957 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
1959 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
1960 dst->dev = &loopback_dev;
1961 dev_hold(&loopback_dev);
1962 dev_put(dev);
1965 EXPORT_SYMBOL(xfrm_dst_ifdown);
1967 static void xfrm_link_failure(struct sk_buff *skb)
1969 /* Impossible. Such dst must be popped before reaches point of failure. */
1970 return;
1973 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1975 if (dst) {
1976 if (dst->obsolete) {
1977 dst_release(dst);
1978 dst = NULL;
1981 return dst;
1984 static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
1986 struct dst_entry *dst, **dstp;
1988 write_lock(&pol->lock);
1989 dstp = &pol->bundles;
1990 while ((dst=*dstp) != NULL) {
1991 if (func(dst)) {
1992 *dstp = dst->next;
1993 dst->next = *gc_list_p;
1994 *gc_list_p = dst;
1995 } else {
1996 dstp = &dst->next;
1999 write_unlock(&pol->lock);
2002 static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
2004 struct dst_entry *gc_list = NULL;
2005 int dir;
2007 read_lock_bh(&xfrm_policy_lock);
2008 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2009 struct xfrm_policy *pol;
2010 struct hlist_node *entry;
2011 struct hlist_head *table;
2012 int i;
2014 hlist_for_each_entry(pol, entry,
2015 &xfrm_policy_inexact[dir], bydst)
2016 prune_one_bundle(pol, func, &gc_list);
2018 table = xfrm_policy_bydst[dir].table;
2019 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
2020 hlist_for_each_entry(pol, entry, table + i, bydst)
2021 prune_one_bundle(pol, func, &gc_list);
2024 read_unlock_bh(&xfrm_policy_lock);
2026 while (gc_list) {
2027 struct dst_entry *dst = gc_list;
2028 gc_list = dst->next;
2029 dst_free(dst);
2033 static int unused_bundle(struct dst_entry *dst)
2035 return !atomic_read(&dst->__refcnt);
2038 static void __xfrm_garbage_collect(void)
2040 xfrm_prune_bundles(unused_bundle);
2043 static int xfrm_flush_bundles(void)
2045 xfrm_prune_bundles(stale_bundle);
2046 return 0;
2049 void xfrm_init_pmtu(struct dst_entry *dst)
2051 do {
2052 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2053 u32 pmtu, route_mtu_cached;
2055 pmtu = dst_mtu(dst->child);
2056 xdst->child_mtu_cached = pmtu;
2058 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2060 route_mtu_cached = dst_mtu(xdst->route);
2061 xdst->route_mtu_cached = route_mtu_cached;
2063 if (pmtu > route_mtu_cached)
2064 pmtu = route_mtu_cached;
2066 dst->metrics[RTAX_MTU-1] = pmtu;
2067 } while ((dst = dst->next));
2070 EXPORT_SYMBOL(xfrm_init_pmtu);
2072 /* Check that the bundle accepts the flow and its components are
2073 * still valid.
2076 int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
2077 struct flowi *fl, int family, int strict)
2079 struct dst_entry *dst = &first->u.dst;
2080 struct xfrm_dst *last;
2081 u32 mtu;
2083 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
2084 (dst->dev && !netif_running(dst->dev)))
2085 return 0;
2086 #ifdef CONFIG_XFRM_SUB_POLICY
2087 if (fl) {
2088 if (first->origin && !flow_cache_uli_match(first->origin, fl))
2089 return 0;
2090 if (first->partner &&
2091 !xfrm_selector_match(first->partner, fl, family))
2092 return 0;
2094 #endif
2096 last = NULL;
2098 do {
2099 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2101 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
2102 return 0;
2103 if (fl && pol &&
2104 !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
2105 return 0;
2106 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2107 return 0;
2108 if (xdst->genid != dst->xfrm->genid)
2109 return 0;
2111 if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
2112 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
2113 return 0;
2115 mtu = dst_mtu(dst->child);
2116 if (xdst->child_mtu_cached != mtu) {
2117 last = xdst;
2118 xdst->child_mtu_cached = mtu;
2121 if (!dst_check(xdst->route, xdst->route_cookie))
2122 return 0;
2123 mtu = dst_mtu(xdst->route);
2124 if (xdst->route_mtu_cached != mtu) {
2125 last = xdst;
2126 xdst->route_mtu_cached = mtu;
2129 dst = dst->child;
2130 } while (dst->xfrm);
2132 if (likely(!last))
2133 return 1;
2135 mtu = last->child_mtu_cached;
2136 for (;;) {
2137 dst = &last->u.dst;
2139 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2140 if (mtu > last->route_mtu_cached)
2141 mtu = last->route_mtu_cached;
2142 dst->metrics[RTAX_MTU-1] = mtu;
2144 if (last == first)
2145 break;
2147 last = (struct xfrm_dst *)last->u.dst.next;
2148 last->child_mtu_cached = mtu;
2151 return 1;
2154 EXPORT_SYMBOL(xfrm_bundle_ok);
2156 #ifdef CONFIG_AUDITSYSCALL
2157 /* Audit addition and deletion of SAs and ipsec policy */
2159 void xfrm_audit_log(uid_t auid, u32 sid, int type, int result,
2160 struct xfrm_policy *xp, struct xfrm_state *x)
2163 char *secctx;
2164 u32 secctx_len;
2165 struct xfrm_sec_ctx *sctx = NULL;
2166 struct audit_buffer *audit_buf;
2167 int family;
2168 extern int audit_enabled;
2170 if (audit_enabled == 0)
2171 return;
2173 BUG_ON((type == AUDIT_MAC_IPSEC_ADDSA ||
2174 type == AUDIT_MAC_IPSEC_DELSA) && !x);
2175 BUG_ON((type == AUDIT_MAC_IPSEC_ADDSPD ||
2176 type == AUDIT_MAC_IPSEC_DELSPD) && !xp);
2178 audit_buf = audit_log_start(current->audit_context, GFP_ATOMIC, type);
2179 if (audit_buf == NULL)
2180 return;
2182 switch(type) {
2183 case AUDIT_MAC_IPSEC_ADDSA:
2184 audit_log_format(audit_buf, "SAD add: auid=%u", auid);
2185 break;
2186 case AUDIT_MAC_IPSEC_DELSA:
2187 audit_log_format(audit_buf, "SAD delete: auid=%u", auid);
2188 break;
2189 case AUDIT_MAC_IPSEC_ADDSPD:
2190 audit_log_format(audit_buf, "SPD add: auid=%u", auid);
2191 break;
2192 case AUDIT_MAC_IPSEC_DELSPD:
2193 audit_log_format(audit_buf, "SPD delete: auid=%u", auid);
2194 break;
2195 default:
2196 return;
2199 if (sid != 0 &&
2200 security_secid_to_secctx(sid, &secctx, &secctx_len) == 0)
2201 audit_log_format(audit_buf, " subj=%s", secctx);
2202 else
2203 audit_log_task_context(audit_buf);
2205 if (xp) {
2206 family = xp->selector.family;
2207 if (xp->security)
2208 sctx = xp->security;
2209 } else {
2210 family = x->props.family;
2211 if (x->security)
2212 sctx = x->security;
2215 if (sctx)
2216 audit_log_format(audit_buf,
2217 " sec_alg=%u sec_doi=%u sec_obj=%s",
2218 sctx->ctx_alg, sctx->ctx_doi, sctx->ctx_str);
2220 switch(family) {
2221 case AF_INET:
2223 struct in_addr saddr, daddr;
2224 if (xp) {
2225 saddr.s_addr = xp->selector.saddr.a4;
2226 daddr.s_addr = xp->selector.daddr.a4;
2227 } else {
2228 saddr.s_addr = x->props.saddr.a4;
2229 daddr.s_addr = x->id.daddr.a4;
2231 audit_log_format(audit_buf,
2232 " src=%u.%u.%u.%u dst=%u.%u.%u.%u",
2233 NIPQUAD(saddr), NIPQUAD(daddr));
2235 break;
2236 case AF_INET6:
2238 struct in6_addr saddr6, daddr6;
2239 if (xp) {
2240 memcpy(&saddr6, xp->selector.saddr.a6,
2241 sizeof(struct in6_addr));
2242 memcpy(&daddr6, xp->selector.daddr.a6,
2243 sizeof(struct in6_addr));
2244 } else {
2245 memcpy(&saddr6, x->props.saddr.a6,
2246 sizeof(struct in6_addr));
2247 memcpy(&daddr6, x->id.daddr.a6,
2248 sizeof(struct in6_addr));
2250 audit_log_format(audit_buf,
2251 " src=" NIP6_FMT " dst=" NIP6_FMT,
2252 NIP6(saddr6), NIP6(daddr6));
2254 break;
2257 if (x)
2258 audit_log_format(audit_buf, " spi=%lu(0x%lx) protocol=%s",
2259 (unsigned long)ntohl(x->id.spi),
2260 (unsigned long)ntohl(x->id.spi),
2261 x->id.proto == IPPROTO_AH ? "AH" :
2262 (x->id.proto == IPPROTO_ESP ?
2263 "ESP" : "IPCOMP"));
2265 audit_log_format(audit_buf, " res=%u", result);
2266 audit_log_end(audit_buf);
2269 EXPORT_SYMBOL(xfrm_audit_log);
2270 #endif /* CONFIG_AUDITSYSCALL */
2272 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2274 int err = 0;
2275 if (unlikely(afinfo == NULL))
2276 return -EINVAL;
2277 if (unlikely(afinfo->family >= NPROTO))
2278 return -EAFNOSUPPORT;
2279 write_lock_bh(&xfrm_policy_afinfo_lock);
2280 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2281 err = -ENOBUFS;
2282 else {
2283 struct dst_ops *dst_ops = afinfo->dst_ops;
2284 if (likely(dst_ops->kmem_cachep == NULL))
2285 dst_ops->kmem_cachep = xfrm_dst_cache;
2286 if (likely(dst_ops->check == NULL))
2287 dst_ops->check = xfrm_dst_check;
2288 if (likely(dst_ops->negative_advice == NULL))
2289 dst_ops->negative_advice = xfrm_negative_advice;
2290 if (likely(dst_ops->link_failure == NULL))
2291 dst_ops->link_failure = xfrm_link_failure;
2292 if (likely(afinfo->garbage_collect == NULL))
2293 afinfo->garbage_collect = __xfrm_garbage_collect;
2294 xfrm_policy_afinfo[afinfo->family] = afinfo;
2296 write_unlock_bh(&xfrm_policy_afinfo_lock);
2297 return err;
2299 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2301 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2303 int err = 0;
2304 if (unlikely(afinfo == NULL))
2305 return -EINVAL;
2306 if (unlikely(afinfo->family >= NPROTO))
2307 return -EAFNOSUPPORT;
2308 write_lock_bh(&xfrm_policy_afinfo_lock);
2309 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2310 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2311 err = -EINVAL;
2312 else {
2313 struct dst_ops *dst_ops = afinfo->dst_ops;
2314 xfrm_policy_afinfo[afinfo->family] = NULL;
2315 dst_ops->kmem_cachep = NULL;
2316 dst_ops->check = NULL;
2317 dst_ops->negative_advice = NULL;
2318 dst_ops->link_failure = NULL;
2319 afinfo->garbage_collect = NULL;
2322 write_unlock_bh(&xfrm_policy_afinfo_lock);
2323 return err;
2325 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2327 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
2329 struct xfrm_policy_afinfo *afinfo;
2330 if (unlikely(family >= NPROTO))
2331 return NULL;
2332 read_lock(&xfrm_policy_afinfo_lock);
2333 afinfo = xfrm_policy_afinfo[family];
2334 if (unlikely(!afinfo))
2335 read_unlock(&xfrm_policy_afinfo_lock);
2336 return afinfo;
2339 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2341 read_unlock(&xfrm_policy_afinfo_lock);
2344 static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
2346 struct xfrm_policy_afinfo *afinfo;
2347 if (unlikely(family >= NPROTO))
2348 return NULL;
2349 write_lock_bh(&xfrm_policy_afinfo_lock);
2350 afinfo = xfrm_policy_afinfo[family];
2351 if (unlikely(!afinfo))
2352 write_unlock_bh(&xfrm_policy_afinfo_lock);
2353 return afinfo;
2356 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
2358 write_unlock_bh(&xfrm_policy_afinfo_lock);
2361 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2363 switch (event) {
2364 case NETDEV_DOWN:
2365 xfrm_flush_bundles();
2367 return NOTIFY_DONE;
2370 static struct notifier_block xfrm_dev_notifier = {
2371 xfrm_dev_event,
2372 NULL,
2376 static void __init xfrm_policy_init(void)
2378 unsigned int hmask, sz;
2379 int dir;
2381 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2382 sizeof(struct xfrm_dst),
2383 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2384 NULL, NULL);
2386 hmask = 8 - 1;
2387 sz = (hmask+1) * sizeof(struct hlist_head);
2389 xfrm_policy_byidx = xfrm_hash_alloc(sz);
2390 xfrm_idx_hmask = hmask;
2391 if (!xfrm_policy_byidx)
2392 panic("XFRM: failed to allocate byidx hash\n");
2394 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2395 struct xfrm_policy_hash *htab;
2397 INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
2399 htab = &xfrm_policy_bydst[dir];
2400 htab->table = xfrm_hash_alloc(sz);
2401 htab->hmask = hmask;
2402 if (!htab->table)
2403 panic("XFRM: failed to allocate bydst hash\n");
2406 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
2407 register_netdevice_notifier(&xfrm_dev_notifier);
2410 void __init xfrm_init(void)
2412 xfrm_state_init();
2413 xfrm_policy_init();
2414 xfrm_input_init();
2417 #ifdef CONFIG_XFRM_MIGRATE
2418 static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
2419 struct xfrm_selector *sel_tgt)
2421 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
2422 if (sel_tgt->family == sel_cmp->family &&
2423 xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
2424 sel_cmp->family) == 0 &&
2425 xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
2426 sel_cmp->family) == 0 &&
2427 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
2428 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
2429 return 1;
2431 } else {
2432 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
2433 return 1;
2436 return 0;
2439 static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
2440 u8 dir, u8 type)
2442 struct xfrm_policy *pol, *ret = NULL;
2443 struct hlist_node *entry;
2444 struct hlist_head *chain;
2445 u32 priority = ~0U;
2447 read_lock_bh(&xfrm_policy_lock);
2448 chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
2449 hlist_for_each_entry(pol, entry, chain, bydst) {
2450 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2451 pol->type == type) {
2452 ret = pol;
2453 priority = ret->priority;
2454 break;
2457 chain = &xfrm_policy_inexact[dir];
2458 hlist_for_each_entry(pol, entry, chain, bydst) {
2459 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2460 pol->type == type &&
2461 pol->priority < priority) {
2462 ret = pol;
2463 break;
2467 if (ret)
2468 xfrm_pol_hold(ret);
2470 read_unlock_bh(&xfrm_policy_lock);
2472 return ret;
2475 static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
2477 int match = 0;
2479 if (t->mode == m->mode && t->id.proto == m->proto &&
2480 (m->reqid == 0 || t->reqid == m->reqid)) {
2481 switch (t->mode) {
2482 case XFRM_MODE_TUNNEL:
2483 case XFRM_MODE_BEET:
2484 if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
2485 m->old_family) == 0 &&
2486 xfrm_addr_cmp(&t->saddr, &m->old_saddr,
2487 m->old_family) == 0) {
2488 match = 1;
2490 break;
2491 case XFRM_MODE_TRANSPORT:
2492 /* in case of transport mode, template does not store
2493 any IP addresses, hence we just compare mode and
2494 protocol */
2495 match = 1;
2496 break;
2497 default:
2498 break;
2501 return match;
2504 /* update endpoint address(es) of template(s) */
2505 static int xfrm_policy_migrate(struct xfrm_policy *pol,
2506 struct xfrm_migrate *m, int num_migrate)
2508 struct xfrm_migrate *mp;
2509 struct dst_entry *dst;
2510 int i, j, n = 0;
2512 write_lock_bh(&pol->lock);
2513 if (unlikely(pol->dead)) {
2514 /* target policy has been deleted */
2515 write_unlock_bh(&pol->lock);
2516 return -ENOENT;
2519 for (i = 0; i < pol->xfrm_nr; i++) {
2520 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
2521 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
2522 continue;
2523 n++;
2524 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL)
2525 continue;
2526 /* update endpoints */
2527 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
2528 sizeof(pol->xfrm_vec[i].id.daddr));
2529 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
2530 sizeof(pol->xfrm_vec[i].saddr));
2531 pol->xfrm_vec[i].encap_family = mp->new_family;
2532 /* flush bundles */
2533 while ((dst = pol->bundles) != NULL) {
2534 pol->bundles = dst->next;
2535 dst_free(dst);
2540 write_unlock_bh(&pol->lock);
2542 if (!n)
2543 return -ENODATA;
2545 return 0;
2548 static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
2550 int i, j;
2552 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
2553 return -EINVAL;
2555 for (i = 0; i < num_migrate; i++) {
2556 if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
2557 m[i].old_family) == 0) &&
2558 (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
2559 m[i].old_family) == 0))
2560 return -EINVAL;
2561 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
2562 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
2563 return -EINVAL;
2565 /* check if there is any duplicated entry */
2566 for (j = i + 1; j < num_migrate; j++) {
2567 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
2568 sizeof(m[i].old_daddr)) &&
2569 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
2570 sizeof(m[i].old_saddr)) &&
2571 m[i].proto == m[j].proto &&
2572 m[i].mode == m[j].mode &&
2573 m[i].reqid == m[j].reqid &&
2574 m[i].old_family == m[j].old_family)
2575 return -EINVAL;
2579 return 0;
2582 int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
2583 struct xfrm_migrate *m, int num_migrate)
2585 int i, err, nx_cur = 0, nx_new = 0;
2586 struct xfrm_policy *pol = NULL;
2587 struct xfrm_state *x, *xc;
2588 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
2589 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
2590 struct xfrm_migrate *mp;
2592 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
2593 goto out;
2595 /* Stage 1 - find policy */
2596 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
2597 err = -ENOENT;
2598 goto out;
2601 /* Stage 2 - find and update state(s) */
2602 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
2603 if ((x = xfrm_migrate_state_find(mp))) {
2604 x_cur[nx_cur] = x;
2605 nx_cur++;
2606 if ((xc = xfrm_state_migrate(x, mp))) {
2607 x_new[nx_new] = xc;
2608 nx_new++;
2609 } else {
2610 err = -ENODATA;
2611 goto restore_state;
2616 /* Stage 3 - update policy */
2617 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
2618 goto restore_state;
2620 /* Stage 4 - delete old state(s) */
2621 if (nx_cur) {
2622 xfrm_states_put(x_cur, nx_cur);
2623 xfrm_states_delete(x_cur, nx_cur);
2626 /* Stage 5 - announce */
2627 km_migrate(sel, dir, type, m, num_migrate);
2629 xfrm_pol_put(pol);
2631 return 0;
2632 out:
2633 return err;
2635 restore_state:
2636 if (pol)
2637 xfrm_pol_put(pol);
2638 if (nx_cur)
2639 xfrm_states_put(x_cur, nx_cur);
2640 if (nx_new)
2641 xfrm_states_delete(x_new, nx_new);
2643 return err;
2645 EXPORT_SYMBOL(xfrm_migrate);
2646 #endif