blob: df5bfa837eb363d2a4caac212d9ea1543cb74bd7 [file] [log] [blame]
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * xfrm_policy.c
3 *
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * Kazunori MIYAZAWA @USAGI
10 * YOSHIFUJI Hideaki
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
Trent Jaegerdf718372005-12-13 23:12:27 -080013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/slab.h>
17#include <linux/kmod.h>
18#include <linux/list.h>
19#include <linux/spinlock.h>
20#include <linux/workqueue.h>
21#include <linux/notifier.h>
22#include <linux/netdevice.h>
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -080023#include <linux/netfilter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
David S. Miller2518c7c2006-08-24 04:45:07 -070025#include <linux/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/xfrm.h>
27#include <net/ip.h>
28
David S. Miller44e36b42006-08-24 04:50:50 -070029#include "xfrm_hash.h"
30
David S. Milleraad0e0b2007-05-25 00:42:49 -070031int sysctl_xfrm_larval_drop __read_mostly;
David S. Miller14e50e52007-05-24 18:17:54 -070032
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080033DEFINE_MUTEX(xfrm_cfg_mutex);
34EXPORT_SYMBOL(xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36static DEFINE_RWLOCK(xfrm_policy_lock);
37
David S. Miller2518c7c2006-08-24 04:45:07 -070038unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
39EXPORT_SYMBOL(xfrm_policy_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
42static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
43
Christoph Lametere18b8902006-12-06 20:33:20 -080044static struct kmem_cache *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46static struct work_struct xfrm_policy_gc_work;
David S. Miller2518c7c2006-08-24 04:45:07 -070047static HLIST_HEAD(xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
49
50static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
51static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
52
Andrew Morton77681022006-11-08 22:46:26 -080053static inline int
54__xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
55{
56 return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
57 addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
58 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
59 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
60 (fl->proto == sel->proto || !sel->proto) &&
61 (fl->oif == sel->ifindex || !sel->ifindex);
62}
63
64static inline int
65__xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
66{
67 return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
68 addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
69 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
70 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
71 (fl->proto == sel->proto || !sel->proto) &&
72 (fl->oif == sel->ifindex || !sel->ifindex);
73}
74
75int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
76 unsigned short family)
77{
78 switch (family) {
79 case AF_INET:
80 return __xfrm4_selector_match(sel, fl);
81 case AF_INET6:
82 return __xfrm6_selector_match(sel, fl);
83 }
84 return 0;
85}
86
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +090087int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 unsigned short family)
89{
90 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
91 int err = 0;
92
93 if (unlikely(afinfo == NULL))
94 return -EAFNOSUPPORT;
95
96 if (likely(afinfo->dst_lookup != NULL))
97 err = afinfo->dst_lookup(dst, fl);
98 else
99 err = -EINVAL;
100 xfrm_policy_put_afinfo(afinfo);
101 return err;
102}
103EXPORT_SYMBOL(xfrm_dst_lookup);
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static inline unsigned long make_jiffies(long secs)
106{
107 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
108 return MAX_SCHEDULE_TIMEOUT-1;
109 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900110 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113static void xfrm_policy_timer(unsigned long data)
114{
115 struct xfrm_policy *xp = (struct xfrm_policy*)data;
James Morris9d729f72007-03-04 16:12:44 -0800116 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 long next = LONG_MAX;
118 int warn = 0;
119 int dir;
120
121 read_lock(&xp->lock);
122
123 if (xp->dead)
124 goto out;
125
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700126 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 if (xp->lft.hard_add_expires_seconds) {
129 long tmo = xp->lft.hard_add_expires_seconds +
130 xp->curlft.add_time - now;
131 if (tmo <= 0)
132 goto expired;
133 if (tmo < next)
134 next = tmo;
135 }
136 if (xp->lft.hard_use_expires_seconds) {
137 long tmo = xp->lft.hard_use_expires_seconds +
138 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
139 if (tmo <= 0)
140 goto expired;
141 if (tmo < next)
142 next = tmo;
143 }
144 if (xp->lft.soft_add_expires_seconds) {
145 long tmo = xp->lft.soft_add_expires_seconds +
146 xp->curlft.add_time - now;
147 if (tmo <= 0) {
148 warn = 1;
149 tmo = XFRM_KM_TIMEOUT;
150 }
151 if (tmo < next)
152 next = tmo;
153 }
154 if (xp->lft.soft_use_expires_seconds) {
155 long tmo = xp->lft.soft_use_expires_seconds +
156 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
157 if (tmo <= 0) {
158 warn = 1;
159 tmo = XFRM_KM_TIMEOUT;
160 }
161 if (tmo < next)
162 next = tmo;
163 }
164
165 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800166 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (next != LONG_MAX &&
168 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
169 xfrm_pol_hold(xp);
170
171out:
172 read_unlock(&xp->lock);
173 xfrm_pol_put(xp);
174 return;
175
176expired:
177 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700178 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800179 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 xfrm_pol_put(xp);
181}
182
183
184/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
185 * SPD calls.
186 */
187
Al Virodd0fc662005-10-07 07:46:04 +0100188struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 struct xfrm_policy *policy;
191
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700192 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 if (policy) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700195 INIT_HLIST_NODE(&policy->bydst);
196 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700198 atomic_set(&policy->refcnt, 1);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800199 setup_timer(&policy->timer, xfrm_policy_timer,
200 (unsigned long)policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202 return policy;
203}
204EXPORT_SYMBOL(xfrm_policy_alloc);
205
206/* Destroy xfrm_policy: descendant resources must be released to this moment. */
207
208void __xfrm_policy_destroy(struct xfrm_policy *policy)
209{
Kris Katterjohn09a62662006-01-08 22:24:28 -0800210 BUG_ON(!policy->dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Kris Katterjohn09a62662006-01-08 22:24:28 -0800212 BUG_ON(policy->bundles);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 if (del_timer(&policy->timer))
215 BUG();
216
Trent Jaegerdf718372005-12-13 23:12:27 -0800217 security_xfrm_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 kfree(policy);
219}
220EXPORT_SYMBOL(__xfrm_policy_destroy);
221
222static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
223{
224 struct dst_entry *dst;
225
226 while ((dst = policy->bundles) != NULL) {
227 policy->bundles = dst->next;
228 dst_free(dst);
229 }
230
231 if (del_timer(&policy->timer))
232 atomic_dec(&policy->refcnt);
233
234 if (atomic_read(&policy->refcnt) > 1)
235 flow_cache_flush();
236
237 xfrm_pol_put(policy);
238}
239
David Howellsc4028952006-11-22 14:57:56 +0000240static void xfrm_policy_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct xfrm_policy *policy;
David S. Miller2518c7c2006-08-24 04:45:07 -0700243 struct hlist_node *entry, *tmp;
244 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 spin_lock_bh(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700247 gc_list.first = xfrm_policy_gc_list.first;
248 INIT_HLIST_HEAD(&xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 spin_unlock_bh(&xfrm_policy_gc_lock);
250
David S. Miller2518c7c2006-08-24 04:45:07 -0700251 hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 xfrm_policy_gc_kill(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255/* Rule must be locked. Release descentant resources, announce
256 * entry dead. The rule must be unlinked from lists to the moment.
257 */
258
259static void xfrm_policy_kill(struct xfrm_policy *policy)
260{
261 int dead;
262
263 write_lock_bh(&policy->lock);
264 dead = policy->dead;
265 policy->dead = 1;
266 write_unlock_bh(&policy->lock);
267
268 if (unlikely(dead)) {
269 WARN_ON(1);
270 return;
271 }
272
273 spin_lock(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700274 hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 spin_unlock(&xfrm_policy_gc_lock);
276
277 schedule_work(&xfrm_policy_gc_work);
278}
279
David S. Miller2518c7c2006-08-24 04:45:07 -0700280struct xfrm_policy_hash {
281 struct hlist_head *table;
282 unsigned int hmask;
283};
284
285static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
286static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
287static struct hlist_head *xfrm_policy_byidx __read_mostly;
288static unsigned int xfrm_idx_hmask __read_mostly;
289static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
290
David S. Miller2518c7c2006-08-24 04:45:07 -0700291static inline unsigned int idx_hash(u32 index)
292{
293 return __idx_hash(index, xfrm_idx_hmask);
294}
295
David S. Miller2518c7c2006-08-24 04:45:07 -0700296static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
297{
298 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
299 unsigned int hash = __sel_hash(sel, family, hmask);
300
301 return (hash == hmask + 1 ?
302 &xfrm_policy_inexact[dir] :
303 xfrm_policy_bydst[dir].table + hash);
304}
305
306static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
307{
308 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
309 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
310
311 return xfrm_policy_bydst[dir].table + hash;
312}
313
David S. Miller2518c7c2006-08-24 04:45:07 -0700314static void xfrm_dst_hash_transfer(struct hlist_head *list,
315 struct hlist_head *ndsttable,
316 unsigned int nhashmask)
317{
318 struct hlist_node *entry, *tmp;
319 struct xfrm_policy *pol;
320
321 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
322 unsigned int h;
323
324 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
325 pol->family, nhashmask);
326 hlist_add_head(&pol->bydst, ndsttable+h);
327 }
328}
329
330static void xfrm_idx_hash_transfer(struct hlist_head *list,
331 struct hlist_head *nidxtable,
332 unsigned int nhashmask)
333{
334 struct hlist_node *entry, *tmp;
335 struct xfrm_policy *pol;
336
337 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
338 unsigned int h;
339
340 h = __idx_hash(pol->index, nhashmask);
341 hlist_add_head(&pol->byidx, nidxtable+h);
342 }
343}
344
345static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
346{
347 return ((old_hmask + 1) << 1) - 1;
348}
349
350static void xfrm_bydst_resize(int dir)
351{
352 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
353 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
354 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
355 struct hlist_head *odst = xfrm_policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700356 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700357 int i;
358
359 if (!ndst)
360 return;
361
362 write_lock_bh(&xfrm_policy_lock);
363
364 for (i = hmask; i >= 0; i--)
365 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
366
367 xfrm_policy_bydst[dir].table = ndst;
368 xfrm_policy_bydst[dir].hmask = nhashmask;
369
370 write_unlock_bh(&xfrm_policy_lock);
371
David S. Miller44e36b42006-08-24 04:50:50 -0700372 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700373}
374
375static void xfrm_byidx_resize(int total)
376{
377 unsigned int hmask = xfrm_idx_hmask;
378 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
379 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
380 struct hlist_head *oidx = xfrm_policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700381 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700382 int i;
383
384 if (!nidx)
385 return;
386
387 write_lock_bh(&xfrm_policy_lock);
388
389 for (i = hmask; i >= 0; i--)
390 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
391
392 xfrm_policy_byidx = nidx;
393 xfrm_idx_hmask = nhashmask;
394
395 write_unlock_bh(&xfrm_policy_lock);
396
David S. Miller44e36b42006-08-24 04:50:50 -0700397 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700398}
399
400static inline int xfrm_bydst_should_resize(int dir, int *total)
401{
402 unsigned int cnt = xfrm_policy_count[dir];
403 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
404
405 if (total)
406 *total += cnt;
407
408 if ((hmask + 1) < xfrm_policy_hashmax &&
409 cnt > hmask)
410 return 1;
411
412 return 0;
413}
414
415static inline int xfrm_byidx_should_resize(int total)
416{
417 unsigned int hmask = xfrm_idx_hmask;
418
419 if ((hmask + 1) < xfrm_policy_hashmax &&
420 total > hmask)
421 return 1;
422
423 return 0;
424}
425
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700426void xfrm_spd_getinfo(struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700427{
428 read_lock_bh(&xfrm_policy_lock);
429 si->incnt = xfrm_policy_count[XFRM_POLICY_IN];
430 si->outcnt = xfrm_policy_count[XFRM_POLICY_OUT];
431 si->fwdcnt = xfrm_policy_count[XFRM_POLICY_FWD];
432 si->inscnt = xfrm_policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
433 si->outscnt = xfrm_policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
434 si->fwdscnt = xfrm_policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
435 si->spdhcnt = xfrm_idx_hmask;
436 si->spdhmcnt = xfrm_policy_hashmax;
437 read_unlock_bh(&xfrm_policy_lock);
438}
439EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700440
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700441static DEFINE_MUTEX(hash_resize_mutex);
David Howellsc4028952006-11-22 14:57:56 +0000442static void xfrm_hash_resize(struct work_struct *__unused)
David S. Miller2518c7c2006-08-24 04:45:07 -0700443{
444 int dir, total;
445
446 mutex_lock(&hash_resize_mutex);
447
448 total = 0;
449 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
450 if (xfrm_bydst_should_resize(dir, &total))
451 xfrm_bydst_resize(dir);
452 }
453 if (xfrm_byidx_should_resize(total))
454 xfrm_byidx_resize(total);
455
456 mutex_unlock(&hash_resize_mutex);
457}
458
David Howellsc4028952006-11-22 14:57:56 +0000459static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461/* Generate new index... KAME seems to generate them ordered by cost
462 * of an absolute inpredictability of ordering of rules. This will not pass. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700463static u32 xfrm_gen_index(u8 type, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 static u32 idx_generator;
466
467 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700468 struct hlist_node *entry;
469 struct hlist_head *list;
470 struct xfrm_policy *p;
471 u32 idx;
472 int found;
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 idx = (idx_generator | dir);
475 idx_generator += 8;
476 if (idx == 0)
477 idx = 8;
David S. Miller2518c7c2006-08-24 04:45:07 -0700478 list = xfrm_policy_byidx + idx_hash(idx);
479 found = 0;
480 hlist_for_each_entry(p, entry, list, byidx) {
481 if (p->index == idx) {
482 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700486 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return idx;
488 }
489}
490
David S. Miller2518c7c2006-08-24 04:45:07 -0700491static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
492{
493 u32 *p1 = (u32 *) s1;
494 u32 *p2 = (u32 *) s2;
495 int len = sizeof(struct xfrm_selector) / sizeof(u32);
496 int i;
497
498 for (i = 0; i < len; i++) {
499 if (p1[i] != p2[i])
500 return 1;
501 }
502
503 return 0;
504}
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
507{
David S. Miller2518c7c2006-08-24 04:45:07 -0700508 struct xfrm_policy *pol;
509 struct xfrm_policy *delpol;
510 struct hlist_head *chain;
Herbert Xua6c7ab52007-01-16 16:52:02 -0800511 struct hlist_node *entry, *newpos;
David S. Miller9b78a822005-12-22 07:39:48 -0800512 struct dst_entry *gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700515 chain = policy_hash_bysel(&policy->selector, policy->family, dir);
516 delpol = NULL;
517 newpos = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700518 hlist_for_each_entry(pol, entry, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800519 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700520 !selector_cmp(&pol->selector, &policy->selector) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800521 xfrm_sec_ctx_match(pol->security, policy->security) &&
522 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (excl) {
524 write_unlock_bh(&xfrm_policy_lock);
525 return -EEXIST;
526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 delpol = pol;
528 if (policy->priority > pol->priority)
529 continue;
530 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800531 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 continue;
533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (delpol)
535 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700538 hlist_add_after(newpos, &policy->bydst);
539 else
540 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 xfrm_pol_hold(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700542 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700544 if (delpol) {
545 hlist_del(&delpol->bydst);
546 hlist_del(&delpol->byidx);
547 xfrm_policy_count[dir]--;
548 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700549 policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700550 hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800551 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 policy->curlft.use_time = 0;
553 if (!mod_timer(&policy->timer, jiffies + HZ))
554 xfrm_pol_hold(policy);
555 write_unlock_bh(&xfrm_policy_lock);
556
David S. Miller9b78a822005-12-22 07:39:48 -0800557 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 xfrm_policy_kill(delpol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700559 else if (xfrm_bydst_should_resize(dir, NULL))
560 schedule_work(&xfrm_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800561
562 read_lock_bh(&xfrm_policy_lock);
563 gc_list = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700564 entry = &policy->bydst;
565 hlist_for_each_entry_continue(policy, entry, bydst) {
David S. Miller9b78a822005-12-22 07:39:48 -0800566 struct dst_entry *dst;
567
568 write_lock(&policy->lock);
569 dst = policy->bundles;
570 if (dst) {
571 struct dst_entry *tail = dst;
572 while (tail->next)
573 tail = tail->next;
574 tail->next = gc_list;
575 gc_list = dst;
576
577 policy->bundles = NULL;
578 }
579 write_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
David S. Miller9b78a822005-12-22 07:39:48 -0800581 read_unlock_bh(&xfrm_policy_lock);
582
583 while (gc_list) {
584 struct dst_entry *dst = gc_list;
585
586 gc_list = dst->next;
587 dst_free(dst);
588 }
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return 0;
591}
592EXPORT_SYMBOL(xfrm_policy_insert);
593
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700594struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
595 struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800596 struct xfrm_sec_ctx *ctx, int delete,
597 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
David S. Miller2518c7c2006-08-24 04:45:07 -0700599 struct xfrm_policy *pol, *ret;
600 struct hlist_head *chain;
601 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Eric Parisef41aaa2007-03-07 15:37:58 -0800603 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700605 chain = policy_hash_bysel(sel, sel->family, dir);
606 ret = NULL;
607 hlist_for_each_entry(pol, entry, chain, bydst) {
608 if (pol->type == type &&
609 !selector_cmp(sel, &pol->selector) &&
610 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700612 if (delete) {
Eric Parisef41aaa2007-03-07 15:37:58 -0800613 *err = security_xfrm_policy_delete(pol);
614 if (*err) {
615 write_unlock_bh(&xfrm_policy_lock);
616 return pol;
617 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700618 hlist_del(&pol->bydst);
619 hlist_del(&pol->byidx);
620 xfrm_policy_count[dir]--;
621 }
622 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 break;
624 }
625 }
626 write_unlock_bh(&xfrm_policy_lock);
627
David S. Miller2518c7c2006-08-24 04:45:07 -0700628 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700630 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700632 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
Trent Jaegerdf718372005-12-13 23:12:27 -0800634EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Eric Parisef41aaa2007-03-07 15:37:58 -0800636struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete,
637 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
David S. Miller2518c7c2006-08-24 04:45:07 -0700639 struct xfrm_policy *pol, *ret;
640 struct hlist_head *chain;
641 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Herbert Xub5505c62007-05-14 02:15:47 -0700643 *err = -ENOENT;
644 if (xfrm_policy_id2dir(id) != dir)
645 return NULL;
646
Eric Parisef41aaa2007-03-07 15:37:58 -0800647 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700649 chain = xfrm_policy_byidx + idx_hash(id);
650 ret = NULL;
651 hlist_for_each_entry(pol, entry, chain, byidx) {
652 if (pol->type == type && pol->index == id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700654 if (delete) {
Eric Parisef41aaa2007-03-07 15:37:58 -0800655 *err = security_xfrm_policy_delete(pol);
656 if (*err) {
657 write_unlock_bh(&xfrm_policy_lock);
658 return pol;
659 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700660 hlist_del(&pol->bydst);
661 hlist_del(&pol->byidx);
662 xfrm_policy_count[dir]--;
663 }
664 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 break;
666 }
667 }
668 write_unlock_bh(&xfrm_policy_lock);
669
David S. Miller2518c7c2006-08-24 04:45:07 -0700670 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700672 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700674 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676EXPORT_SYMBOL(xfrm_policy_byid);
677
Joy Latten4aa2e622007-06-04 19:05:57 -0400678#ifdef CONFIG_SECURITY_NETWORK_XFRM
679static inline int
680xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Joy Latten4aa2e622007-06-04 19:05:57 -0400682 int dir, err = 0;
683
684 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
685 struct xfrm_policy *pol;
686 struct hlist_node *entry;
687 int i;
688
689 hlist_for_each_entry(pol, entry,
690 &xfrm_policy_inexact[dir], bydst) {
691 if (pol->type != type)
692 continue;
693 err = security_xfrm_policy_delete(pol);
694 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700695 xfrm_audit_policy_delete(pol, 0,
696 audit_info->loginuid,
697 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400698 return err;
699 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900700 }
Joy Latten4aa2e622007-06-04 19:05:57 -0400701 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
702 hlist_for_each_entry(pol, entry,
703 xfrm_policy_bydst[dir].table + i,
704 bydst) {
705 if (pol->type != type)
706 continue;
707 err = security_xfrm_policy_delete(pol);
708 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700709 xfrm_audit_policy_delete(pol, 0,
710 audit_info->loginuid,
711 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400712 return err;
713 }
714 }
715 }
716 }
717 return err;
718}
719#else
720static inline int
721xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
722{
723 return 0;
724}
725#endif
726
727int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
728{
729 int dir, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 write_lock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400732
733 err = xfrm_policy_flush_secctx_check(type, audit_info);
734 if (err)
735 goto out;
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700738 struct xfrm_policy *pol;
739 struct hlist_node *entry;
David S. Millerae8c0572006-10-03 16:00:26 -0700740 int i, killed;
David S. Miller2518c7c2006-08-24 04:45:07 -0700741
David S. Millerae8c0572006-10-03 16:00:26 -0700742 killed = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700743 again1:
744 hlist_for_each_entry(pol, entry,
745 &xfrm_policy_inexact[dir], bydst) {
746 if (pol->type != type)
747 continue;
748 hlist_del(&pol->bydst);
749 hlist_del(&pol->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 write_unlock_bh(&xfrm_policy_lock);
751
Joy Lattenab5f5e82007-09-17 11:51:22 -0700752 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
753 audit_info->secid);
Joy Latten161a09e2006-11-27 13:11:54 -0600754
David S. Miller2518c7c2006-08-24 04:45:07 -0700755 xfrm_policy_kill(pol);
David S. Millerae8c0572006-10-03 16:00:26 -0700756 killed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700759 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700761
762 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
763 again2:
764 hlist_for_each_entry(pol, entry,
765 xfrm_policy_bydst[dir].table + i,
766 bydst) {
767 if (pol->type != type)
768 continue;
769 hlist_del(&pol->bydst);
770 hlist_del(&pol->byidx);
771 write_unlock_bh(&xfrm_policy_lock);
772
Joy Lattenab5f5e82007-09-17 11:51:22 -0700773 xfrm_audit_policy_delete(pol, 1,
774 audit_info->loginuid,
775 audit_info->secid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700776 xfrm_policy_kill(pol);
David S. Millerae8c0572006-10-03 16:00:26 -0700777 killed++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700778
779 write_lock_bh(&xfrm_policy_lock);
780 goto again2;
781 }
782 }
783
David S. Millerae8c0572006-10-03 16:00:26 -0700784 xfrm_policy_count[dir] -= killed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786 atomic_inc(&flow_cache_genid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400787out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 write_unlock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400789 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791EXPORT_SYMBOL(xfrm_policy_flush);
792
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700793int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 void *data)
795{
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800796 struct xfrm_policy *pol, *last = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700797 struct hlist_node *entry;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800798 int dir, last_dir = 0, count, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700801 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700804 struct hlist_head *table = xfrm_policy_bydst[dir].table;
805 int i;
806
807 hlist_for_each_entry(pol, entry,
808 &xfrm_policy_inexact[dir], bydst) {
809 if (pol->type != type)
810 continue;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800811 if (last) {
812 error = func(last, last_dir % XFRM_POLICY_MAX,
813 count, data);
814 if (error)
815 goto out;
816 }
817 last = pol;
818 last_dir = dir;
819 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700821 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
822 hlist_for_each_entry(pol, entry, table + i, bydst) {
823 if (pol->type != type)
824 continue;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800825 if (last) {
826 error = func(last, last_dir % XFRM_POLICY_MAX,
827 count, data);
828 if (error)
829 goto out;
830 }
831 last = pol;
832 last_dir = dir;
833 count++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700834 }
835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800837 if (count == 0) {
838 error = -ENOENT;
839 goto out;
840 }
841 error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842out:
843 read_unlock_bh(&xfrm_policy_lock);
844 return error;
845}
846EXPORT_SYMBOL(xfrm_policy_walk);
847
James Morris134b0fc2006-10-05 15:42:27 -0500848/*
849 * Find policy to apply to this flow.
850 *
851 * Returns 0 if policy found, else an -errno.
852 */
David S. Miller2518c7c2006-08-24 04:45:07 -0700853static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
854 u8 type, u16 family, int dir)
855{
856 struct xfrm_selector *sel = &pol->selector;
James Morris134b0fc2006-10-05 15:42:27 -0500857 int match, ret = -ESRCH;
David S. Miller2518c7c2006-08-24 04:45:07 -0700858
859 if (pol->family != family ||
860 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500861 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700862
863 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500864 if (match)
865 ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700866
James Morris134b0fc2006-10-05 15:42:27 -0500867 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700868}
869
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700870static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
871 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
James Morris134b0fc2006-10-05 15:42:27 -0500873 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700874 struct xfrm_policy *pol, *ret;
875 xfrm_address_t *daddr, *saddr;
876 struct hlist_node *entry;
877 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700878 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700879
880 daddr = xfrm_flowi_daddr(fl, family);
881 saddr = xfrm_flowi_saddr(fl, family);
882 if (unlikely(!daddr || !saddr))
883 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700886 chain = policy_hash_direct(daddr, saddr, family, dir);
887 ret = NULL;
888 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500889 err = xfrm_policy_match(pol, fl, type, family, dir);
890 if (err) {
891 if (err == -ESRCH)
892 continue;
893 else {
894 ret = ERR_PTR(err);
895 goto fail;
896 }
897 } else {
David S. Milleracba48e2006-08-25 15:46:46 -0700898 ret = pol;
899 priority = ret->priority;
900 break;
901 }
902 }
903 chain = &xfrm_policy_inexact[dir];
904 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500905 err = xfrm_policy_match(pol, fl, type, family, dir);
906 if (err) {
907 if (err == -ESRCH)
908 continue;
909 else {
910 ret = ERR_PTR(err);
911 goto fail;
912 }
913 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700914 ret = pol;
915 break;
916 }
917 }
David S. Milleracba48e2006-08-25 15:46:46 -0700918 if (ret)
919 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -0500920fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700922
David S. Miller2518c7c2006-08-24 04:45:07 -0700923 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700924}
925
James Morris134b0fc2006-10-05 15:42:27 -0500926static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700927 void **objp, atomic_t **obj_refp)
928{
929 struct xfrm_policy *pol;
James Morris134b0fc2006-10-05 15:42:27 -0500930 int err = 0;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700931
932#ifdef CONFIG_XFRM_SUB_POLICY
933 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
James Morris134b0fc2006-10-05 15:42:27 -0500934 if (IS_ERR(pol)) {
935 err = PTR_ERR(pol);
936 pol = NULL;
937 }
938 if (pol || err)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700939 goto end;
940#endif
941 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
James Morris134b0fc2006-10-05 15:42:27 -0500942 if (IS_ERR(pol)) {
943 err = PTR_ERR(pol);
944 pol = NULL;
945 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700946#ifdef CONFIG_XFRM_SUB_POLICY
David S. Miller2518c7c2006-08-24 04:45:07 -0700947end:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700948#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if ((*objp = (void *) pol) != NULL)
950 *obj_refp = &pol->refcnt;
James Morris134b0fc2006-10-05 15:42:27 -0500951 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Trent Jaegerdf718372005-12-13 23:12:27 -0800954static inline int policy_to_flow_dir(int dir)
955{
956 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900957 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
958 XFRM_POLICY_FWD == FLOW_DIR_FWD)
959 return dir;
960 switch (dir) {
961 default:
962 case XFRM_POLICY_IN:
963 return FLOW_DIR_IN;
964 case XFRM_POLICY_OUT:
965 return FLOW_DIR_OUT;
966 case XFRM_POLICY_FWD:
967 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700968 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800969}
970
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700971static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 struct xfrm_policy *pol;
974
975 read_lock_bh(&xfrm_policy_lock);
976 if ((pol = sk->sk_policy[dir]) != NULL) {
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900977 int match = xfrm_selector_match(&pol->selector, fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900979 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -0800980
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -0500981 if (match) {
982 err = security_xfrm_policy_lookup(pol, fl->secid,
983 policy_to_flow_dir(dir));
984 if (!err)
985 xfrm_pol_hold(pol);
986 else if (err == -ESRCH)
987 pol = NULL;
988 else
989 pol = ERR_PTR(err);
990 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 pol = NULL;
992 }
993 read_unlock_bh(&xfrm_policy_lock);
994 return pol;
995}
996
997static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
998{
David S. Miller2518c7c2006-08-24 04:45:07 -0700999 struct hlist_head *chain = policy_hash_bysel(&pol->selector,
1000 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001001
David S. Miller2518c7c2006-08-24 04:45:07 -07001002 hlist_add_head(&pol->bydst, chain);
1003 hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
1004 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001006
1007 if (xfrm_bydst_should_resize(dir, NULL))
1008 schedule_work(&xfrm_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
1011static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1012 int dir)
1013{
David S. Miller2518c7c2006-08-24 04:45:07 -07001014 if (hlist_unhashed(&pol->bydst))
1015 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
David S. Miller2518c7c2006-08-24 04:45:07 -07001017 hlist_del(&pol->bydst);
1018 hlist_del(&pol->byidx);
1019 xfrm_policy_count[dir]--;
1020
1021 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Herbert Xu4666faa2005-06-18 22:43:22 -07001024int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 write_lock_bh(&xfrm_policy_lock);
1027 pol = __xfrm_policy_unlink(pol, dir);
1028 write_unlock_bh(&xfrm_policy_lock);
1029 if (pol) {
1030 if (dir < XFRM_POLICY_MAX)
1031 atomic_inc(&flow_cache_genid);
1032 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001033 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001035 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
David S. Millera70fcb02006-03-20 19:18:52 -08001037EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1040{
1041 struct xfrm_policy *old_pol;
1042
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001043#ifdef CONFIG_XFRM_SUB_POLICY
1044 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1045 return -EINVAL;
1046#endif
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 write_lock_bh(&xfrm_policy_lock);
1049 old_pol = sk->sk_policy[dir];
1050 sk->sk_policy[dir] = pol;
1051 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001052 pol->curlft.add_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001053 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1055 }
1056 if (old_pol)
1057 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1058 write_unlock_bh(&xfrm_policy_lock);
1059
1060 if (old_pol) {
1061 xfrm_policy_kill(old_pol);
1062 }
1063 return 0;
1064}
1065
1066static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1067{
1068 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
1069
1070 if (newp) {
1071 newp->selector = old->selector;
Trent Jaegerdf718372005-12-13 23:12:27 -08001072 if (security_xfrm_policy_clone(old, newp)) {
1073 kfree(newp);
1074 return NULL; /* ENOMEM */
1075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 newp->lft = old->lft;
1077 newp->curlft = old->curlft;
1078 newp->action = old->action;
1079 newp->flags = old->flags;
1080 newp->xfrm_nr = old->xfrm_nr;
1081 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001082 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 memcpy(newp->xfrm_vec, old->xfrm_vec,
1084 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1085 write_lock_bh(&xfrm_policy_lock);
1086 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1087 write_unlock_bh(&xfrm_policy_lock);
1088 xfrm_pol_put(newp);
1089 }
1090 return newp;
1091}
1092
1093int __xfrm_sk_clone_policy(struct sock *sk)
1094{
1095 struct xfrm_policy *p0 = sk->sk_policy[0],
1096 *p1 = sk->sk_policy[1];
1097
1098 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1099 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1100 return -ENOMEM;
1101 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1102 return -ENOMEM;
1103 return 0;
1104}
1105
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001106static int
1107xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
1108 unsigned short family)
1109{
1110 int err;
1111 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1112
1113 if (unlikely(afinfo == NULL))
1114 return -EINVAL;
1115 err = afinfo->get_saddr(local, remote);
1116 xfrm_policy_put_afinfo(afinfo);
1117 return err;
1118}
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120/* Resolve list of templates for the flow, given policy. */
1121
1122static int
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001123xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1124 struct xfrm_state **xfrm,
1125 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 int nx;
1128 int i, error;
1129 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1130 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001131 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1134 struct xfrm_state *x;
1135 xfrm_address_t *remote = daddr;
1136 xfrm_address_t *local = saddr;
1137 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1138
Joakim Koskela48b8d782007-07-26 00:08:42 -07001139 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1140 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 remote = &tmpl->id.daddr;
1142 local = &tmpl->saddr;
Miika Komu76b3f052006-11-30 16:40:43 -08001143 family = tmpl->encap_family;
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001144 if (xfrm_addr_any(local, family)) {
1145 error = xfrm_get_saddr(&tmp, remote, family);
1146 if (error)
1147 goto fail;
1148 local = &tmp;
1149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151
1152 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1153
1154 if (x && x->km.state == XFRM_STATE_VALID) {
1155 xfrm[nx++] = x;
1156 daddr = remote;
1157 saddr = local;
1158 continue;
1159 }
1160 if (x) {
1161 error = (x->km.state == XFRM_STATE_ERROR ?
1162 -EINVAL : -EAGAIN);
1163 xfrm_state_put(x);
1164 }
1165
1166 if (!tmpl->optional)
1167 goto fail;
1168 }
1169 return nx;
1170
1171fail:
1172 for (nx--; nx>=0; nx--)
1173 xfrm_state_put(xfrm[nx]);
1174 return error;
1175}
1176
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001177static int
1178xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1179 struct xfrm_state **xfrm,
1180 unsigned short family)
1181{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001182 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1183 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001184 int cnx = 0;
1185 int error;
1186 int ret;
1187 int i;
1188
1189 for (i = 0; i < npols; i++) {
1190 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1191 error = -ENOBUFS;
1192 goto fail;
1193 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001194
1195 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001196 if (ret < 0) {
1197 error = ret;
1198 goto fail;
1199 } else
1200 cnx += ret;
1201 }
1202
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001203 /* found states are sorted for outbound processing */
1204 if (npols > 1)
1205 xfrm_state_sort(xfrm, tpp, cnx, family);
1206
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001207 return cnx;
1208
1209 fail:
1210 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001211 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001212 return error;
1213
1214}
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216/* Check that the bundle accepts the flow and its components are
1217 * still valid.
1218 */
1219
1220static struct dst_entry *
1221xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1222{
1223 struct dst_entry *x;
1224 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1225 if (unlikely(afinfo == NULL))
1226 return ERR_PTR(-EINVAL);
1227 x = afinfo->find_bundle(fl, policy);
1228 xfrm_policy_put_afinfo(afinfo);
1229 return x;
1230}
1231
1232/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1233 * all the metrics... Shortly, bundle a bundle.
1234 */
1235
1236static int
1237xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
1238 struct flowi *fl, struct dst_entry **dst_p,
1239 unsigned short family)
1240{
1241 int err;
1242 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1243 if (unlikely(afinfo == NULL))
1244 return -EINVAL;
1245 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
1246 xfrm_policy_put_afinfo(afinfo);
1247 return err;
1248}
1249
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001250static int inline
1251xfrm_dst_alloc_copy(void **target, void *src, int size)
1252{
1253 if (!*target) {
1254 *target = kmalloc(size, GFP_ATOMIC);
1255 if (!*target)
1256 return -ENOMEM;
1257 }
1258 memcpy(*target, src, size);
1259 return 0;
1260}
1261
1262static int inline
1263xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
1264{
1265#ifdef CONFIG_XFRM_SUB_POLICY
1266 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1267 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1268 sel, sizeof(*sel));
1269#else
1270 return 0;
1271#endif
1272}
1273
1274static int inline
1275xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
1276{
1277#ifdef CONFIG_XFRM_SUB_POLICY
1278 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1279 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1280#else
1281 return 0;
1282#endif
1283}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285static int stale_bundle(struct dst_entry *dst);
1286
1287/* Main function: finds/creates a bundle for given flow.
1288 *
1289 * At the moment we eat a raw IP route. Mostly to speed up lookups
1290 * on interfaces with disabled IPsec.
1291 */
David S. Miller14e50e52007-05-24 18:17:54 -07001292int __xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1293 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
1295 struct xfrm_policy *policy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001296 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1297 int npols;
1298 int pol_dead;
1299 int xfrm_nr;
1300 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1302 struct dst_entry *dst, *dst_orig = *dst_p;
1303 int nx = 0;
1304 int err;
1305 u32 genid;
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001306 u16 family;
Trent Jaegerdf718372005-12-13 23:12:27 -08001307 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309restart:
1310 genid = atomic_read(&flow_cache_genid);
1311 policy = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001312 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1313 pols[pi] = NULL;
1314 npols = 0;
1315 pol_dead = 0;
1316 xfrm_nr = 0;
1317
Thomas Graff7944fb2007-08-25 13:46:55 -07001318 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001319 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
Herbert Xu75b8c132007-12-11 04:38:08 -08001320 err = PTR_ERR(policy);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001321 if (IS_ERR(policy))
Herbert Xu75b8c132007-12-11 04:38:08 -08001322 goto dropdst;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 if (!policy) {
1326 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07001327 if ((dst_orig->flags & DST_NOXFRM) ||
1328 !xfrm_policy_count[XFRM_POLICY_OUT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return 0;
1330
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001331 policy = flow_cache_lookup(fl, dst_orig->ops->family,
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001332 dir, xfrm_policy_lookup);
Herbert Xu75b8c132007-12-11 04:38:08 -08001333 err = PTR_ERR(policy);
James Morris134b0fc2006-10-05 15:42:27 -05001334 if (IS_ERR(policy))
Herbert Xu75b8c132007-12-11 04:38:08 -08001335 goto dropdst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337
1338 if (!policy)
1339 return 0;
1340
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001341 family = dst_orig->ops->family;
James Morris9d729f72007-03-04 16:12:44 -08001342 policy->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001343 pols[0] = policy;
1344 npols ++;
1345 xfrm_nr += pols[0]->xfrm_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 switch (policy->action) {
Herbert Xu5e5234f2007-11-30 00:50:31 +11001348 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 case XFRM_POLICY_BLOCK:
1350 /* Prohibit the flow */
Patrick McHardye104411b2005-09-08 15:11:55 -07001351 err = -EPERM;
1352 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 case XFRM_POLICY_ALLOW:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001355#ifndef CONFIG_XFRM_SUB_POLICY
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (policy->xfrm_nr == 0) {
1357 /* Flow passes not transformed. */
1358 xfrm_pol_put(policy);
1359 return 0;
1360 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001361#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 /* Try to find matching bundle.
1364 *
1365 * LATER: help from flow cache. It is optional, this
1366 * is required only for output policy.
1367 */
1368 dst = xfrm_find_bundle(fl, policy, family);
1369 if (IS_ERR(dst)) {
Patrick McHardye104411b2005-09-08 15:11:55 -07001370 err = PTR_ERR(dst);
1371 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
1373
1374 if (dst)
1375 break;
1376
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001377#ifdef CONFIG_XFRM_SUB_POLICY
1378 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1379 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1380 fl, family,
1381 XFRM_POLICY_OUT);
1382 if (pols[1]) {
James Morris134b0fc2006-10-05 15:42:27 -05001383 if (IS_ERR(pols[1])) {
1384 err = PTR_ERR(pols[1]);
1385 goto error;
1386 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001387 if (pols[1]->action == XFRM_POLICY_BLOCK) {
1388 err = -EPERM;
1389 goto error;
1390 }
1391 npols ++;
1392 xfrm_nr += pols[1]->xfrm_nr;
1393 }
1394 }
1395
1396 /*
1397 * Because neither flowi nor bundle information knows about
1398 * transformation template size. On more than one policy usage
1399 * we can realize whether all of them is bypass or not after
1400 * they are searched. See above not-transformed bypass
1401 * is surrounded by non-sub policy configuration, too.
1402 */
1403 if (xfrm_nr == 0) {
1404 /* Flow passes not transformed. */
1405 xfrm_pols_put(pols, npols);
1406 return 0;
1407 }
1408
1409#endif
1410 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 if (unlikely(nx<0)) {
1413 err = nx;
David S. Miller14e50e52007-05-24 18:17:54 -07001414 if (err == -EAGAIN && sysctl_xfrm_larval_drop) {
1415 /* EREMOTE tells the caller to generate
1416 * a one-shot blackhole route.
1417 */
1418 xfrm_pol_put(policy);
1419 return -EREMOTE;
1420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (err == -EAGAIN && flags) {
1422 DECLARE_WAITQUEUE(wait, current);
1423
1424 add_wait_queue(&km_waitq, &wait);
1425 set_current_state(TASK_INTERRUPTIBLE);
1426 schedule();
1427 set_current_state(TASK_RUNNING);
1428 remove_wait_queue(&km_waitq, &wait);
1429
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001430 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 if (nx == -EAGAIN && signal_pending(current)) {
1433 err = -ERESTART;
1434 goto error;
1435 }
1436 if (nx == -EAGAIN ||
1437 genid != atomic_read(&flow_cache_genid)) {
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001438 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 goto restart;
1440 }
1441 err = nx;
1442 }
1443 if (err < 0)
1444 goto error;
1445 }
1446 if (nx == 0) {
1447 /* Flow passes not transformed. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001448 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return 0;
1450 }
1451
1452 dst = dst_orig;
1453 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
1454
1455 if (unlikely(err)) {
1456 int i;
1457 for (i=0; i<nx; i++)
1458 xfrm_state_put(xfrm[i]);
1459 goto error;
1460 }
1461
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001462 for (pi = 0; pi < npols; pi++) {
1463 read_lock_bh(&pols[pi]->lock);
1464 pol_dead |= pols[pi]->dead;
1465 read_unlock_bh(&pols[pi]->lock);
1466 }
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 write_lock_bh(&policy->lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001469 if (unlikely(pol_dead || stale_bundle(dst))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 /* Wow! While we worked on resolving, this
1471 * policy has gone. Retry. It is not paranoia,
1472 * we just cannot enlist new bundle to dead object.
1473 * We can't enlist stable bundles either.
1474 */
1475 write_unlock_bh(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (dst)
1477 dst_free(dst);
Herbert Xu00de6512006-02-13 16:01:27 -08001478
1479 err = -EHOSTUNREACH;
1480 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001482
1483 if (npols > 1)
1484 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1485 else
1486 err = xfrm_dst_update_origin(dst, fl);
1487 if (unlikely(err)) {
1488 write_unlock_bh(&policy->lock);
1489 if (dst)
1490 dst_free(dst);
1491 goto error;
1492 }
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 dst->next = policy->bundles;
1495 policy->bundles = dst;
1496 dst_hold(dst);
1497 write_unlock_bh(&policy->lock);
1498 }
1499 *dst_p = dst;
1500 dst_release(dst_orig);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001501 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return 0;
1503
1504error:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001505 xfrm_pols_put(pols, npols);
Herbert Xu75b8c132007-12-11 04:38:08 -08001506dropdst:
1507 dst_release(dst_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 *dst_p = NULL;
1509 return err;
1510}
David S. Miller14e50e52007-05-24 18:17:54 -07001511EXPORT_SYMBOL(__xfrm_lookup);
1512
1513int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1514 struct sock *sk, int flags)
1515{
1516 int err = __xfrm_lookup(dst_p, fl, sk, flags);
1517
1518 if (err == -EREMOTE) {
1519 dst_release(*dst_p);
1520 *dst_p = NULL;
1521 err = -EAGAIN;
1522 }
1523
1524 return err;
1525}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526EXPORT_SYMBOL(xfrm_lookup);
1527
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001528static inline int
1529xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1530{
1531 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001532
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;
Herbert Xu1ecafed2007-10-09 13:24:07 -07001538 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001539}
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541/* When skb is transformed back to its "native" form, we have to
1542 * check policy restrictions. At the moment we make this in maximally
1543 * stupid way. Shame on me. :-) Of course, connected sockets must
1544 * have policy cached at them.
1545 */
1546
1547static inline int
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001548xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 unsigned short family)
1550{
1551 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08001552 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return x->id.proto == tmpl->id.proto &&
1554 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1555 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1556 x->props.mode == tmpl->mode &&
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07001557 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1558 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001559 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1560 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001563/*
1564 * 0 or more than 0 is returned when validation is succeeded (either bypass
1565 * because of optional transport mode, or next index of the mathced secpath
1566 * state with the template.
1567 * -1 is returned when no matching template is found.
1568 * Otherwise "-2 - errored_index" is returned.
1569 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570static inline int
1571xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1572 unsigned short family)
1573{
1574 int idx = start;
1575
1576 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001577 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return start;
1579 } else
1580 start = -1;
1581 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001582 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001584 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1585 if (start == -1)
1586 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
1590 return start;
1591}
1592
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001593int
1594xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
1596 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001597 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 if (unlikely(afinfo == NULL))
1600 return -EAFNOSUPPORT;
1601
1602 afinfo->decode_session(skb, fl);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07001603 err = security_xfrm_decode_session(skb, &fl->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001605 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001607EXPORT_SYMBOL(xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001609static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
1611 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001612 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001613 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617
1618 return 0;
1619}
1620
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001621int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 unsigned short family)
1623{
1624 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001625 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1626 int npols = 0;
1627 int xfrm_nr;
1628 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 struct flowi fl;
Trent Jaegerdf718372005-12-13 23:12:27 -08001630 u8 fl_dir = policy_to_flow_dir(dir);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001631 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001633 if (xfrm_decode_session(skb, &fl, family) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 return 0;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08001635 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 /* First, check used SA against their selectors. */
1638 if (skb->sp) {
1639 int i;
1640
1641 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001642 struct xfrm_state *x = skb->sp->xvec[i];
1643 if (!xfrm_selector_match(&x->sel, &fl, family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
1646 }
1647
1648 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001649 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001650 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001651 if (IS_ERR(pol))
1652 return 0;
1653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 if (!pol)
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001656 pol = flow_cache_lookup(&fl, family, fl_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 xfrm_policy_lookup);
1658
James Morris134b0fc2006-10-05 15:42:27 -05001659 if (IS_ERR(pol))
1660 return 0;
1661
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001662 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001663 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001664 xfrm_secpath_reject(xerr_idx, skb, &fl);
1665 return 0;
1666 }
1667 return 1;
1668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
James Morris9d729f72007-03-04 16:12:44 -08001670 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001672 pols[0] = pol;
1673 npols ++;
1674#ifdef CONFIG_XFRM_SUB_POLICY
1675 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1676 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1677 &fl, family,
1678 XFRM_POLICY_IN);
1679 if (pols[1]) {
James Morris134b0fc2006-10-05 15:42:27 -05001680 if (IS_ERR(pols[1]))
1681 return 0;
James Morris9d729f72007-03-04 16:12:44 -08001682 pols[1]->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001683 npols ++;
1684 }
1685 }
1686#endif
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (pol->action == XFRM_POLICY_ALLOW) {
1689 struct sec_path *sp;
1690 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001691 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001692 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001693 struct xfrm_tmpl **tpp = tp;
1694 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 int i, k;
1696
1697 if ((sp = skb->sp) == NULL)
1698 sp = &dummy;
1699
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001700 for (pi = 0; pi < npols; pi++) {
1701 if (pols[pi] != pol &&
1702 pols[pi]->action != XFRM_POLICY_ALLOW)
1703 goto reject;
1704 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
1705 goto reject_error;
1706 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1707 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1708 }
1709 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001710 if (npols > 1) {
1711 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1712 tpp = stp;
1713 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 /* For each tunnel xfrm, find the first matching tmpl.
1716 * For each tmpl before that, find corresponding xfrm.
1717 * Order is _important_. Later we will implement
1718 * some barriers, but at the moment barriers
1719 * are implied between each two transformations.
1720 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001721 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1722 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001723 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001724 if (k < -1)
1725 /* "-2 - errored_index" returned */
1726 xerr_idx = -(2+k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
1730
James Morrisd1d9fac2006-09-01 00:32:12 -07001731 if (secpath_has_nontransport(sp, k, &xerr_idx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 goto reject;
1733
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001734 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return 1;
1736 }
1737
1738reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001739 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001740reject_error:
1741 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return 0;
1743}
1744EXPORT_SYMBOL(__xfrm_policy_check);
1745
1746int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1747{
1748 struct flowi fl;
1749
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001750 if (xfrm_decode_session(skb, &fl, family) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return 0;
1752
1753 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1754}
1755EXPORT_SYMBOL(__xfrm_route_forward);
1756
David S. Millerd49c73c2006-08-13 18:55:53 -07001757/* Optimize later using cookies and generation ids. */
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1760{
David S. Millerd49c73c2006-08-13 18:55:53 -07001761 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1762 * to "-1" to force all XFRM destinations to get validated by
1763 * dst_ops->check on every use. We do this because when a
1764 * normal route referenced by an XFRM dst is obsoleted we do
1765 * not go looking around for all parent referencing XFRM dsts
1766 * so that we can invalidate them. It is just too much work.
1767 * Instead we make the checks here on every use. For example:
1768 *
1769 * XFRM dst A --> IPv4 dst X
1770 *
1771 * X is the "xdst->route" of A (X is also the "dst->path" of A
1772 * in this example). If X is marked obsolete, "A" will not
1773 * notice. That's what we are validating here via the
1774 * stale_bundle() check.
1775 *
1776 * When a policy's bundle is pruned, we dst_free() the XFRM
1777 * dst which causes it's ->obsolete field to be set to a
1778 * positive non-zero integer. If an XFRM dst has been pruned
1779 * like this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08001780 */
David S. Millerd49c73c2006-08-13 18:55:53 -07001781 if (dst->obsolete < 0 && !stale_bundle(dst))
1782 return dst;
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 return NULL;
1785}
1786
1787static int stale_bundle(struct dst_entry *dst)
1788{
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05001789 return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
Herbert Xuaabc9762005-05-03 16:27:10 -07001792void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001795 dst->dev = init_net.loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07001796 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 dev_put(dev);
1798 }
1799}
Herbert Xuaabc9762005-05-03 16:27:10 -07001800EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802static void xfrm_link_failure(struct sk_buff *skb)
1803{
1804 /* Impossible. Such dst must be popped before reaches point of failure. */
1805 return;
1806}
1807
1808static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1809{
1810 if (dst) {
1811 if (dst->obsolete) {
1812 dst_release(dst);
1813 dst = NULL;
1814 }
1815 }
1816 return dst;
1817}
1818
David S. Miller2518c7c2006-08-24 04:45:07 -07001819static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
1820{
1821 struct dst_entry *dst, **dstp;
1822
1823 write_lock(&pol->lock);
1824 dstp = &pol->bundles;
1825 while ((dst=*dstp) != NULL) {
1826 if (func(dst)) {
1827 *dstp = dst->next;
1828 dst->next = *gc_list_p;
1829 *gc_list_p = dst;
1830 } else {
1831 dstp = &dst->next;
1832 }
1833 }
1834 write_unlock(&pol->lock);
1835}
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1838{
David S. Miller2518c7c2006-08-24 04:45:07 -07001839 struct dst_entry *gc_list = NULL;
1840 int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07001843 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
1844 struct xfrm_policy *pol;
1845 struct hlist_node *entry;
1846 struct hlist_head *table;
1847 int i;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001848
David S. Miller2518c7c2006-08-24 04:45:07 -07001849 hlist_for_each_entry(pol, entry,
1850 &xfrm_policy_inexact[dir], bydst)
1851 prune_one_bundle(pol, func, &gc_list);
1852
1853 table = xfrm_policy_bydst[dir].table;
1854 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
1855 hlist_for_each_entry(pol, entry, table + i, bydst)
1856 prune_one_bundle(pol, func, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 }
1858 }
1859 read_unlock_bh(&xfrm_policy_lock);
1860
1861 while (gc_list) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001862 struct dst_entry *dst = gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 gc_list = dst->next;
1864 dst_free(dst);
1865 }
1866}
1867
1868static int unused_bundle(struct dst_entry *dst)
1869{
1870 return !atomic_read(&dst->__refcnt);
1871}
1872
1873static void __xfrm_garbage_collect(void)
1874{
1875 xfrm_prune_bundles(unused_bundle);
1876}
1877
David S. Miller1c095392006-08-24 03:30:28 -07001878static int xfrm_flush_bundles(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
1880 xfrm_prune_bundles(stale_bundle);
1881 return 0;
1882}
1883
1884void xfrm_init_pmtu(struct dst_entry *dst)
1885{
1886 do {
1887 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1888 u32 pmtu, route_mtu_cached;
1889
1890 pmtu = dst_mtu(dst->child);
1891 xdst->child_mtu_cached = pmtu;
1892
1893 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
1894
1895 route_mtu_cached = dst_mtu(xdst->route);
1896 xdst->route_mtu_cached = route_mtu_cached;
1897
1898 if (pmtu > route_mtu_cached)
1899 pmtu = route_mtu_cached;
1900
1901 dst->metrics[RTAX_MTU-1] = pmtu;
1902 } while ((dst = dst->next));
1903}
1904
1905EXPORT_SYMBOL(xfrm_init_pmtu);
1906
1907/* Check that the bundle accepts the flow and its components are
1908 * still valid.
1909 */
1910
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05001911int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
1912 struct flowi *fl, int family, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
1914 struct dst_entry *dst = &first->u.dst;
1915 struct xfrm_dst *last;
1916 u32 mtu;
1917
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07001918 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 (dst->dev && !netif_running(dst->dev)))
1920 return 0;
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001921#ifdef CONFIG_XFRM_SUB_POLICY
1922 if (fl) {
1923 if (first->origin && !flow_cache_uli_match(first->origin, fl))
1924 return 0;
1925 if (first->partner &&
1926 !xfrm_selector_match(first->partner, fl, family))
1927 return 0;
1928 }
1929#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 last = NULL;
1932
1933 do {
1934 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1935
1936 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
1937 return 0;
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06001938 if (fl && pol &&
1939 !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001940 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (dst->xfrm->km.state != XFRM_STATE_VALID)
1942 return 0;
David S. Miller9d4a7062006-08-24 03:18:09 -07001943 if (xdst->genid != dst->xfrm->genid)
1944 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Herbert Xu1bfcb102007-10-17 21:31:50 -07001946 if (strict && fl &&
Herbert Xu13996372007-10-17 21:35:51 -07001947 !(dst->xfrm->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07001948 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
1949 return 0;
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 mtu = dst_mtu(dst->child);
1952 if (xdst->child_mtu_cached != mtu) {
1953 last = xdst;
1954 xdst->child_mtu_cached = mtu;
1955 }
1956
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07001957 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return 0;
1959 mtu = dst_mtu(xdst->route);
1960 if (xdst->route_mtu_cached != mtu) {
1961 last = xdst;
1962 xdst->route_mtu_cached = mtu;
1963 }
1964
1965 dst = dst->child;
1966 } while (dst->xfrm);
1967
1968 if (likely(!last))
1969 return 1;
1970
1971 mtu = last->child_mtu_cached;
1972 for (;;) {
1973 dst = &last->u.dst;
1974
1975 mtu = xfrm_state_mtu(dst->xfrm, mtu);
1976 if (mtu > last->route_mtu_cached)
1977 mtu = last->route_mtu_cached;
1978 dst->metrics[RTAX_MTU-1] = mtu;
1979
1980 if (last == first)
1981 break;
1982
Patrick McHardybd0bf072007-07-18 01:55:52 -07001983 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 last->child_mtu_cached = mtu;
1985 }
1986
1987 return 1;
1988}
1989
1990EXPORT_SYMBOL(xfrm_bundle_ok);
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
1993{
1994 int err = 0;
1995 if (unlikely(afinfo == NULL))
1996 return -EINVAL;
1997 if (unlikely(afinfo->family >= NPROTO))
1998 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07001999 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2001 err = -ENOBUFS;
2002 else {
2003 struct dst_ops *dst_ops = afinfo->dst_ops;
2004 if (likely(dst_ops->kmem_cachep == NULL))
2005 dst_ops->kmem_cachep = xfrm_dst_cache;
2006 if (likely(dst_ops->check == NULL))
2007 dst_ops->check = xfrm_dst_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 if (likely(dst_ops->negative_advice == NULL))
2009 dst_ops->negative_advice = xfrm_negative_advice;
2010 if (likely(dst_ops->link_failure == NULL))
2011 dst_ops->link_failure = xfrm_link_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (likely(afinfo->garbage_collect == NULL))
2013 afinfo->garbage_collect = __xfrm_garbage_collect;
2014 xfrm_policy_afinfo[afinfo->family] = afinfo;
2015 }
Ingo Molnare959d812006-04-28 15:32:29 -07002016 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 return err;
2018}
2019EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2020
2021int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2022{
2023 int err = 0;
2024 if (unlikely(afinfo == NULL))
2025 return -EINVAL;
2026 if (unlikely(afinfo->family >= NPROTO))
2027 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002028 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2030 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2031 err = -EINVAL;
2032 else {
2033 struct dst_ops *dst_ops = afinfo->dst_ops;
2034 xfrm_policy_afinfo[afinfo->family] = NULL;
2035 dst_ops->kmem_cachep = NULL;
2036 dst_ops->check = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 dst_ops->negative_advice = NULL;
2038 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 afinfo->garbage_collect = NULL;
2040 }
2041 }
Ingo Molnare959d812006-04-28 15:32:29 -07002042 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return err;
2044}
2045EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2046
2047static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
2048{
2049 struct xfrm_policy_afinfo *afinfo;
2050 if (unlikely(family >= NPROTO))
2051 return NULL;
2052 read_lock(&xfrm_policy_afinfo_lock);
2053 afinfo = xfrm_policy_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07002054 if (unlikely(!afinfo))
2055 read_unlock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 return afinfo;
2057}
2058
2059static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2060{
Herbert Xu546be242006-05-27 23:03:58 -07002061 read_unlock(&xfrm_policy_afinfo_lock);
2062}
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2065{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002066 struct net_device *dev = ptr;
2067
2068 if (dev->nd_net != &init_net)
2069 return NOTIFY_DONE;
2070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 switch (event) {
2072 case NETDEV_DOWN:
2073 xfrm_flush_bundles();
2074 }
2075 return NOTIFY_DONE;
2076}
2077
2078static struct notifier_block xfrm_dev_notifier = {
2079 xfrm_dev_event,
2080 NULL,
2081 0
2082};
2083
2084static void __init xfrm_policy_init(void)
2085{
David S. Miller2518c7c2006-08-24 04:45:07 -07002086 unsigned int hmask, sz;
2087 int dir;
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2090 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002091 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002092 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
David S. Miller2518c7c2006-08-24 04:45:07 -07002094 hmask = 8 - 1;
2095 sz = (hmask+1) * sizeof(struct hlist_head);
2096
David S. Miller44e36b42006-08-24 04:50:50 -07002097 xfrm_policy_byidx = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002098 xfrm_idx_hmask = hmask;
2099 if (!xfrm_policy_byidx)
2100 panic("XFRM: failed to allocate byidx hash\n");
2101
2102 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2103 struct xfrm_policy_hash *htab;
2104
2105 INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
2106
2107 htab = &xfrm_policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002108 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002109 htab->hmask = hmask;
2110 if (!htab->table)
2111 panic("XFRM: failed to allocate bydst hash\n");
2112 }
2113
David Howellsc4028952006-11-22 14:57:56 +00002114 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 register_netdevice_notifier(&xfrm_dev_notifier);
2116}
2117
2118void __init xfrm_init(void)
2119{
2120 xfrm_state_init();
2121 xfrm_policy_init();
2122 xfrm_input_init();
2123}
2124
Joy Lattenab5f5e82007-09-17 11:51:22 -07002125#ifdef CONFIG_AUDITSYSCALL
2126static inline void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2127 struct audit_buffer *audit_buf)
2128{
2129 if (xp->security)
2130 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2131 xp->security->ctx_alg, xp->security->ctx_doi,
2132 xp->security->ctx_str);
2133
2134 switch(xp->selector.family) {
2135 case AF_INET:
2136 audit_log_format(audit_buf, " src=%u.%u.%u.%u dst=%u.%u.%u.%u",
2137 NIPQUAD(xp->selector.saddr.a4),
2138 NIPQUAD(xp->selector.daddr.a4));
2139 break;
2140 case AF_INET6:
2141 {
2142 struct in6_addr saddr6, daddr6;
2143
2144 memcpy(&saddr6, xp->selector.saddr.a6,
2145 sizeof(struct in6_addr));
2146 memcpy(&daddr6, xp->selector.daddr.a6,
2147 sizeof(struct in6_addr));
2148 audit_log_format(audit_buf,
2149 " src=" NIP6_FMT " dst=" NIP6_FMT,
2150 NIP6(saddr6), NIP6(daddr6));
2151 }
2152 break;
2153 }
2154}
2155
2156void
2157xfrm_audit_policy_add(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
2158{
2159 struct audit_buffer *audit_buf;
2160 extern int audit_enabled;
2161
2162 if (audit_enabled == 0)
2163 return;
Paul Moore5951cab2007-12-20 00:00:45 -08002164 audit_buf = xfrm_audit_start(auid, sid);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002165 if (audit_buf == NULL)
2166 return;
2167 audit_log_format(audit_buf, " op=SPD-add res=%u", result);
2168 xfrm_audit_common_policyinfo(xp, audit_buf);
2169 audit_log_end(audit_buf);
2170}
2171EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
2172
2173void
2174xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
2175{
2176 struct audit_buffer *audit_buf;
2177 extern int audit_enabled;
2178
2179 if (audit_enabled == 0)
2180 return;
Paul Moore5951cab2007-12-20 00:00:45 -08002181 audit_buf = xfrm_audit_start(auid, sid);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002182 if (audit_buf == NULL)
2183 return;
2184 audit_log_format(audit_buf, " op=SPD-delete res=%u", result);
2185 xfrm_audit_common_policyinfo(xp, audit_buf);
2186 audit_log_end(audit_buf);
2187}
2188EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
2189#endif
2190
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002191#ifdef CONFIG_XFRM_MIGRATE
2192static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
2193 struct xfrm_selector *sel_tgt)
2194{
2195 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
2196 if (sel_tgt->family == sel_cmp->family &&
2197 xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002198 sel_cmp->family) == 0 &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002199 xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
2200 sel_cmp->family) == 0 &&
2201 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
2202 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
2203 return 1;
2204 }
2205 } else {
2206 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
2207 return 1;
2208 }
2209 }
2210 return 0;
2211}
2212
2213static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
2214 u8 dir, u8 type)
2215{
2216 struct xfrm_policy *pol, *ret = NULL;
2217 struct hlist_node *entry;
2218 struct hlist_head *chain;
2219 u32 priority = ~0U;
2220
2221 read_lock_bh(&xfrm_policy_lock);
2222 chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
2223 hlist_for_each_entry(pol, entry, chain, bydst) {
2224 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2225 pol->type == type) {
2226 ret = pol;
2227 priority = ret->priority;
2228 break;
2229 }
2230 }
2231 chain = &xfrm_policy_inexact[dir];
2232 hlist_for_each_entry(pol, entry, chain, bydst) {
2233 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2234 pol->type == type &&
2235 pol->priority < priority) {
2236 ret = pol;
2237 break;
2238 }
2239 }
2240
2241 if (ret)
2242 xfrm_pol_hold(ret);
2243
2244 read_unlock_bh(&xfrm_policy_lock);
2245
2246 return ret;
2247}
2248
2249static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
2250{
2251 int match = 0;
2252
2253 if (t->mode == m->mode && t->id.proto == m->proto &&
2254 (m->reqid == 0 || t->reqid == m->reqid)) {
2255 switch (t->mode) {
2256 case XFRM_MODE_TUNNEL:
2257 case XFRM_MODE_BEET:
2258 if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
2259 m->old_family) == 0 &&
2260 xfrm_addr_cmp(&t->saddr, &m->old_saddr,
2261 m->old_family) == 0) {
2262 match = 1;
2263 }
2264 break;
2265 case XFRM_MODE_TRANSPORT:
2266 /* in case of transport mode, template does not store
2267 any IP addresses, hence we just compare mode and
2268 protocol */
2269 match = 1;
2270 break;
2271 default:
2272 break;
2273 }
2274 }
2275 return match;
2276}
2277
2278/* update endpoint address(es) of template(s) */
2279static int xfrm_policy_migrate(struct xfrm_policy *pol,
2280 struct xfrm_migrate *m, int num_migrate)
2281{
2282 struct xfrm_migrate *mp;
2283 struct dst_entry *dst;
2284 int i, j, n = 0;
2285
2286 write_lock_bh(&pol->lock);
2287 if (unlikely(pol->dead)) {
2288 /* target policy has been deleted */
2289 write_unlock_bh(&pol->lock);
2290 return -ENOENT;
2291 }
2292
2293 for (i = 0; i < pol->xfrm_nr; i++) {
2294 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
2295 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
2296 continue;
2297 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07002298 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
2299 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002300 continue;
2301 /* update endpoints */
2302 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
2303 sizeof(pol->xfrm_vec[i].id.daddr));
2304 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
2305 sizeof(pol->xfrm_vec[i].saddr));
2306 pol->xfrm_vec[i].encap_family = mp->new_family;
2307 /* flush bundles */
2308 while ((dst = pol->bundles) != NULL) {
2309 pol->bundles = dst->next;
2310 dst_free(dst);
2311 }
2312 }
2313 }
2314
2315 write_unlock_bh(&pol->lock);
2316
2317 if (!n)
2318 return -ENODATA;
2319
2320 return 0;
2321}
2322
2323static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
2324{
2325 int i, j;
2326
2327 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
2328 return -EINVAL;
2329
2330 for (i = 0; i < num_migrate; i++) {
2331 if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
2332 m[i].old_family) == 0) &&
2333 (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
2334 m[i].old_family) == 0))
2335 return -EINVAL;
2336 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
2337 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
2338 return -EINVAL;
2339
2340 /* check if there is any duplicated entry */
2341 for (j = i + 1; j < num_migrate; j++) {
2342 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
2343 sizeof(m[i].old_daddr)) &&
2344 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
2345 sizeof(m[i].old_saddr)) &&
2346 m[i].proto == m[j].proto &&
2347 m[i].mode == m[j].mode &&
2348 m[i].reqid == m[j].reqid &&
2349 m[i].old_family == m[j].old_family)
2350 return -EINVAL;
2351 }
2352 }
2353
2354 return 0;
2355}
2356
2357int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
2358 struct xfrm_migrate *m, int num_migrate)
2359{
2360 int i, err, nx_cur = 0, nx_new = 0;
2361 struct xfrm_policy *pol = NULL;
2362 struct xfrm_state *x, *xc;
2363 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
2364 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
2365 struct xfrm_migrate *mp;
2366
2367 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
2368 goto out;
2369
2370 /* Stage 1 - find policy */
2371 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
2372 err = -ENOENT;
2373 goto out;
2374 }
2375
2376 /* Stage 2 - find and update state(s) */
2377 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
2378 if ((x = xfrm_migrate_state_find(mp))) {
2379 x_cur[nx_cur] = x;
2380 nx_cur++;
2381 if ((xc = xfrm_state_migrate(x, mp))) {
2382 x_new[nx_new] = xc;
2383 nx_new++;
2384 } else {
2385 err = -ENODATA;
2386 goto restore_state;
2387 }
2388 }
2389 }
2390
2391 /* Stage 3 - update policy */
2392 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
2393 goto restore_state;
2394
2395 /* Stage 4 - delete old state(s) */
2396 if (nx_cur) {
2397 xfrm_states_put(x_cur, nx_cur);
2398 xfrm_states_delete(x_cur, nx_cur);
2399 }
2400
2401 /* Stage 5 - announce */
2402 km_migrate(sel, dir, type, m, num_migrate);
2403
2404 xfrm_pol_put(pol);
2405
2406 return 0;
2407out:
2408 return err;
2409
2410restore_state:
2411 if (pol)
2412 xfrm_pol_put(pol);
2413 if (nx_cur)
2414 xfrm_states_put(x_cur, nx_cur);
2415 if (nx_new)
2416 xfrm_states_delete(x_new, nx_new);
2417
2418 return err;
2419}
David S. Millere610e672007-02-08 13:29:15 -08002420EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002421#endif