blob: 3d516d57b5b2dc70538a9b74b969662b95670c7b [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
Herbert Xu66cdb3c2007-11-13 21:37:28 -080016#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/slab.h>
18#include <linux/kmod.h>
19#include <linux/list.h>
20#include <linux/spinlock.h>
21#include <linux/workqueue.h>
22#include <linux/notifier.h>
23#include <linux/netdevice.h>
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -080024#include <linux/netfilter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
David S. Miller2518c7c2006-08-24 04:45:07 -070026#include <linux/cache.h>
Herbert Xu25ee3282007-12-11 09:32:34 -080027#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <net/xfrm.h>
29#include <net/ip.h>
30
David S. Miller44e36b42006-08-24 04:50:50 -070031#include "xfrm_hash.h"
32
David S. Milleraad0e0b2007-05-25 00:42:49 -070033int sysctl_xfrm_larval_drop __read_mostly;
David S. Miller14e50e52007-05-24 18:17:54 -070034
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080035DEFINE_MUTEX(xfrm_cfg_mutex);
36EXPORT_SYMBOL(xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38static DEFINE_RWLOCK(xfrm_policy_lock);
39
David S. Miller2518c7c2006-08-24 04:45:07 -070040unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
41EXPORT_SYMBOL(xfrm_policy_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
44static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
45
Christoph Lametere18b8902006-12-06 20:33:20 -080046static struct kmem_cache *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48static struct work_struct xfrm_policy_gc_work;
David S. Miller2518c7c2006-08-24 04:45:07 -070049static HLIST_HEAD(xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
51
52static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
53static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
Herbert Xu25ee3282007-12-11 09:32:34 -080054static void xfrm_init_pmtu(struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Andrew Morton77681022006-11-08 22:46:26 -080056static inline int
57__xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
58{
59 return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
60 addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
61 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
62 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
63 (fl->proto == sel->proto || !sel->proto) &&
64 (fl->oif == sel->ifindex || !sel->ifindex);
65}
66
67static inline int
68__xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
69{
70 return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
71 addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
72 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
73 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
74 (fl->proto == sel->proto || !sel->proto) &&
75 (fl->oif == sel->ifindex || !sel->ifindex);
76}
77
78int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
79 unsigned short family)
80{
81 switch (family) {
82 case AF_INET:
83 return __xfrm4_selector_match(sel, fl);
84 case AF_INET6:
85 return __xfrm6_selector_match(sel, fl);
86 }
87 return 0;
88}
89
Herbert Xu25ee3282007-12-11 09:32:34 -080090static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
91 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Herbert Xu66cdb3c2007-11-13 21:37:28 -080093 xfrm_address_t *saddr = &x->props.saddr;
94 xfrm_address_t *daddr = &x->id.daddr;
95 struct xfrm_policy_afinfo *afinfo;
96 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Herbert Xu66cdb3c2007-11-13 21:37:28 -080098 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR)
99 saddr = x->coaddr;
100 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR)
101 daddr = x->coaddr;
102
Herbert Xu25ee3282007-12-11 09:32:34 -0800103 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800105 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800107 dst = afinfo->dst_lookup(tos, saddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 xfrm_policy_put_afinfo(afinfo);
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800109 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static inline unsigned long make_jiffies(long secs)
113{
114 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
115 return MAX_SCHEDULE_TIMEOUT-1;
116 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900117 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120static void xfrm_policy_timer(unsigned long data)
121{
122 struct xfrm_policy *xp = (struct xfrm_policy*)data;
James Morris9d729f72007-03-04 16:12:44 -0800123 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 long next = LONG_MAX;
125 int warn = 0;
126 int dir;
127
128 read_lock(&xp->lock);
129
130 if (xp->dead)
131 goto out;
132
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700133 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 if (xp->lft.hard_add_expires_seconds) {
136 long tmo = xp->lft.hard_add_expires_seconds +
137 xp->curlft.add_time - now;
138 if (tmo <= 0)
139 goto expired;
140 if (tmo < next)
141 next = tmo;
142 }
143 if (xp->lft.hard_use_expires_seconds) {
144 long tmo = xp->lft.hard_use_expires_seconds +
145 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
146 if (tmo <= 0)
147 goto expired;
148 if (tmo < next)
149 next = tmo;
150 }
151 if (xp->lft.soft_add_expires_seconds) {
152 long tmo = xp->lft.soft_add_expires_seconds +
153 xp->curlft.add_time - now;
154 if (tmo <= 0) {
155 warn = 1;
156 tmo = XFRM_KM_TIMEOUT;
157 }
158 if (tmo < next)
159 next = tmo;
160 }
161 if (xp->lft.soft_use_expires_seconds) {
162 long tmo = xp->lft.soft_use_expires_seconds +
163 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
164 if (tmo <= 0) {
165 warn = 1;
166 tmo = XFRM_KM_TIMEOUT;
167 }
168 if (tmo < next)
169 next = tmo;
170 }
171
172 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800173 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (next != LONG_MAX &&
175 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
176 xfrm_pol_hold(xp);
177
178out:
179 read_unlock(&xp->lock);
180 xfrm_pol_put(xp);
181 return;
182
183expired:
184 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700185 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800186 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 xfrm_pol_put(xp);
188}
189
190
191/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
192 * SPD calls.
193 */
194
Al Virodd0fc662005-10-07 07:46:04 +0100195struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 struct xfrm_policy *policy;
198
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700199 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 if (policy) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700202 INIT_HLIST_NODE(&policy->bydst);
203 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700205 atomic_set(&policy->refcnt, 1);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800206 setup_timer(&policy->timer, xfrm_policy_timer,
207 (unsigned long)policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209 return policy;
210}
211EXPORT_SYMBOL(xfrm_policy_alloc);
212
213/* Destroy xfrm_policy: descendant resources must be released to this moment. */
214
215void __xfrm_policy_destroy(struct xfrm_policy *policy)
216{
Kris Katterjohn09a62662006-01-08 22:24:28 -0800217 BUG_ON(!policy->dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Kris Katterjohn09a62662006-01-08 22:24:28 -0800219 BUG_ON(policy->bundles);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 if (del_timer(&policy->timer))
222 BUG();
223
Trent Jaegerdf718372005-12-13 23:12:27 -0800224 security_xfrm_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 kfree(policy);
226}
227EXPORT_SYMBOL(__xfrm_policy_destroy);
228
229static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
230{
231 struct dst_entry *dst;
232
233 while ((dst = policy->bundles) != NULL) {
234 policy->bundles = dst->next;
235 dst_free(dst);
236 }
237
238 if (del_timer(&policy->timer))
239 atomic_dec(&policy->refcnt);
240
241 if (atomic_read(&policy->refcnt) > 1)
242 flow_cache_flush();
243
244 xfrm_pol_put(policy);
245}
246
David Howellsc4028952006-11-22 14:57:56 +0000247static void xfrm_policy_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct xfrm_policy *policy;
David S. Miller2518c7c2006-08-24 04:45:07 -0700250 struct hlist_node *entry, *tmp;
251 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 spin_lock_bh(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700254 gc_list.first = xfrm_policy_gc_list.first;
255 INIT_HLIST_HEAD(&xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 spin_unlock_bh(&xfrm_policy_gc_lock);
257
David S. Miller2518c7c2006-08-24 04:45:07 -0700258 hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 xfrm_policy_gc_kill(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262/* Rule must be locked. Release descentant resources, announce
263 * entry dead. The rule must be unlinked from lists to the moment.
264 */
265
266static void xfrm_policy_kill(struct xfrm_policy *policy)
267{
268 int dead;
269
270 write_lock_bh(&policy->lock);
271 dead = policy->dead;
272 policy->dead = 1;
273 write_unlock_bh(&policy->lock);
274
275 if (unlikely(dead)) {
276 WARN_ON(1);
277 return;
278 }
279
280 spin_lock(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700281 hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 spin_unlock(&xfrm_policy_gc_lock);
283
284 schedule_work(&xfrm_policy_gc_work);
285}
286
David S. Miller2518c7c2006-08-24 04:45:07 -0700287struct xfrm_policy_hash {
288 struct hlist_head *table;
289 unsigned int hmask;
290};
291
292static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
293static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
294static struct hlist_head *xfrm_policy_byidx __read_mostly;
295static unsigned int xfrm_idx_hmask __read_mostly;
296static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
297
David S. Miller2518c7c2006-08-24 04:45:07 -0700298static inline unsigned int idx_hash(u32 index)
299{
300 return __idx_hash(index, xfrm_idx_hmask);
301}
302
David S. Miller2518c7c2006-08-24 04:45:07 -0700303static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
304{
305 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
306 unsigned int hash = __sel_hash(sel, family, hmask);
307
308 return (hash == hmask + 1 ?
309 &xfrm_policy_inexact[dir] :
310 xfrm_policy_bydst[dir].table + hash);
311}
312
313static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
314{
315 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
316 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
317
318 return xfrm_policy_bydst[dir].table + hash;
319}
320
David S. Miller2518c7c2006-08-24 04:45:07 -0700321static void xfrm_dst_hash_transfer(struct hlist_head *list,
322 struct hlist_head *ndsttable,
323 unsigned int nhashmask)
324{
325 struct hlist_node *entry, *tmp;
326 struct xfrm_policy *pol;
327
328 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
329 unsigned int h;
330
331 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
332 pol->family, nhashmask);
333 hlist_add_head(&pol->bydst, ndsttable+h);
334 }
335}
336
337static void xfrm_idx_hash_transfer(struct hlist_head *list,
338 struct hlist_head *nidxtable,
339 unsigned int nhashmask)
340{
341 struct hlist_node *entry, *tmp;
342 struct xfrm_policy *pol;
343
344 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
345 unsigned int h;
346
347 h = __idx_hash(pol->index, nhashmask);
348 hlist_add_head(&pol->byidx, nidxtable+h);
349 }
350}
351
352static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
353{
354 return ((old_hmask + 1) << 1) - 1;
355}
356
357static void xfrm_bydst_resize(int dir)
358{
359 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
360 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
361 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
362 struct hlist_head *odst = xfrm_policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700363 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700364 int i;
365
366 if (!ndst)
367 return;
368
369 write_lock_bh(&xfrm_policy_lock);
370
371 for (i = hmask; i >= 0; i--)
372 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
373
374 xfrm_policy_bydst[dir].table = ndst;
375 xfrm_policy_bydst[dir].hmask = nhashmask;
376
377 write_unlock_bh(&xfrm_policy_lock);
378
David S. Miller44e36b42006-08-24 04:50:50 -0700379 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700380}
381
382static void xfrm_byidx_resize(int total)
383{
384 unsigned int hmask = xfrm_idx_hmask;
385 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
386 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
387 struct hlist_head *oidx = xfrm_policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700388 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700389 int i;
390
391 if (!nidx)
392 return;
393
394 write_lock_bh(&xfrm_policy_lock);
395
396 for (i = hmask; i >= 0; i--)
397 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
398
399 xfrm_policy_byidx = nidx;
400 xfrm_idx_hmask = nhashmask;
401
402 write_unlock_bh(&xfrm_policy_lock);
403
David S. Miller44e36b42006-08-24 04:50:50 -0700404 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700405}
406
407static inline int xfrm_bydst_should_resize(int dir, int *total)
408{
409 unsigned int cnt = xfrm_policy_count[dir];
410 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
411
412 if (total)
413 *total += cnt;
414
415 if ((hmask + 1) < xfrm_policy_hashmax &&
416 cnt > hmask)
417 return 1;
418
419 return 0;
420}
421
422static inline int xfrm_byidx_should_resize(int total)
423{
424 unsigned int hmask = xfrm_idx_hmask;
425
426 if ((hmask + 1) < xfrm_policy_hashmax &&
427 total > hmask)
428 return 1;
429
430 return 0;
431}
432
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700433void xfrm_spd_getinfo(struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700434{
435 read_lock_bh(&xfrm_policy_lock);
436 si->incnt = xfrm_policy_count[XFRM_POLICY_IN];
437 si->outcnt = xfrm_policy_count[XFRM_POLICY_OUT];
438 si->fwdcnt = xfrm_policy_count[XFRM_POLICY_FWD];
439 si->inscnt = xfrm_policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
440 si->outscnt = xfrm_policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
441 si->fwdscnt = xfrm_policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
442 si->spdhcnt = xfrm_idx_hmask;
443 si->spdhmcnt = xfrm_policy_hashmax;
444 read_unlock_bh(&xfrm_policy_lock);
445}
446EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700447
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700448static DEFINE_MUTEX(hash_resize_mutex);
David Howellsc4028952006-11-22 14:57:56 +0000449static void xfrm_hash_resize(struct work_struct *__unused)
David S. Miller2518c7c2006-08-24 04:45:07 -0700450{
451 int dir, total;
452
453 mutex_lock(&hash_resize_mutex);
454
455 total = 0;
456 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
457 if (xfrm_bydst_should_resize(dir, &total))
458 xfrm_bydst_resize(dir);
459 }
460 if (xfrm_byidx_should_resize(total))
461 xfrm_byidx_resize(total);
462
463 mutex_unlock(&hash_resize_mutex);
464}
465
David Howellsc4028952006-11-22 14:57:56 +0000466static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/* Generate new index... KAME seems to generate them ordered by cost
469 * of an absolute inpredictability of ordering of rules. This will not pass. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700470static u32 xfrm_gen_index(u8 type, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 static u32 idx_generator;
473
474 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700475 struct hlist_node *entry;
476 struct hlist_head *list;
477 struct xfrm_policy *p;
478 u32 idx;
479 int found;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 idx = (idx_generator | dir);
482 idx_generator += 8;
483 if (idx == 0)
484 idx = 8;
David S. Miller2518c7c2006-08-24 04:45:07 -0700485 list = xfrm_policy_byidx + idx_hash(idx);
486 found = 0;
487 hlist_for_each_entry(p, entry, list, byidx) {
488 if (p->index == idx) {
489 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700493 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return idx;
495 }
496}
497
David S. Miller2518c7c2006-08-24 04:45:07 -0700498static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
499{
500 u32 *p1 = (u32 *) s1;
501 u32 *p2 = (u32 *) s2;
502 int len = sizeof(struct xfrm_selector) / sizeof(u32);
503 int i;
504
505 for (i = 0; i < len; i++) {
506 if (p1[i] != p2[i])
507 return 1;
508 }
509
510 return 0;
511}
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
514{
David S. Miller2518c7c2006-08-24 04:45:07 -0700515 struct xfrm_policy *pol;
516 struct xfrm_policy *delpol;
517 struct hlist_head *chain;
Herbert Xua6c7ab52007-01-16 16:52:02 -0800518 struct hlist_node *entry, *newpos;
David S. Miller9b78a822005-12-22 07:39:48 -0800519 struct dst_entry *gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700522 chain = policy_hash_bysel(&policy->selector, policy->family, dir);
523 delpol = NULL;
524 newpos = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700525 hlist_for_each_entry(pol, entry, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800526 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700527 !selector_cmp(&pol->selector, &policy->selector) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800528 xfrm_sec_ctx_match(pol->security, policy->security) &&
529 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (excl) {
531 write_unlock_bh(&xfrm_policy_lock);
532 return -EEXIST;
533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 delpol = pol;
535 if (policy->priority > pol->priority)
536 continue;
537 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800538 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 continue;
540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (delpol)
542 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700545 hlist_add_after(newpos, &policy->bydst);
546 else
547 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 xfrm_pol_hold(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700549 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700551 if (delpol) {
552 hlist_del(&delpol->bydst);
553 hlist_del(&delpol->byidx);
554 xfrm_policy_count[dir]--;
555 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700556 policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700557 hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800558 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 policy->curlft.use_time = 0;
560 if (!mod_timer(&policy->timer, jiffies + HZ))
561 xfrm_pol_hold(policy);
562 write_unlock_bh(&xfrm_policy_lock);
563
David S. Miller9b78a822005-12-22 07:39:48 -0800564 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 xfrm_policy_kill(delpol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700566 else if (xfrm_bydst_should_resize(dir, NULL))
567 schedule_work(&xfrm_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800568
569 read_lock_bh(&xfrm_policy_lock);
570 gc_list = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700571 entry = &policy->bydst;
572 hlist_for_each_entry_continue(policy, entry, bydst) {
David S. Miller9b78a822005-12-22 07:39:48 -0800573 struct dst_entry *dst;
574
575 write_lock(&policy->lock);
576 dst = policy->bundles;
577 if (dst) {
578 struct dst_entry *tail = dst;
579 while (tail->next)
580 tail = tail->next;
581 tail->next = gc_list;
582 gc_list = dst;
583
584 policy->bundles = NULL;
585 }
586 write_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
David S. Miller9b78a822005-12-22 07:39:48 -0800588 read_unlock_bh(&xfrm_policy_lock);
589
590 while (gc_list) {
591 struct dst_entry *dst = gc_list;
592
593 gc_list = dst->next;
594 dst_free(dst);
595 }
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return 0;
598}
599EXPORT_SYMBOL(xfrm_policy_insert);
600
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700601struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
602 struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800603 struct xfrm_sec_ctx *ctx, int delete,
604 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
David S. Miller2518c7c2006-08-24 04:45:07 -0700606 struct xfrm_policy *pol, *ret;
607 struct hlist_head *chain;
608 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Eric Parisef41aaa2007-03-07 15:37:58 -0800610 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700612 chain = policy_hash_bysel(sel, sel->family, dir);
613 ret = NULL;
614 hlist_for_each_entry(pol, entry, chain, bydst) {
615 if (pol->type == type &&
616 !selector_cmp(sel, &pol->selector) &&
617 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700619 if (delete) {
Eric Parisef41aaa2007-03-07 15:37:58 -0800620 *err = security_xfrm_policy_delete(pol);
621 if (*err) {
622 write_unlock_bh(&xfrm_policy_lock);
623 return pol;
624 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700625 hlist_del(&pol->bydst);
626 hlist_del(&pol->byidx);
627 xfrm_policy_count[dir]--;
628 }
629 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 break;
631 }
632 }
633 write_unlock_bh(&xfrm_policy_lock);
634
David S. Miller2518c7c2006-08-24 04:45:07 -0700635 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700637 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700639 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
Trent Jaegerdf718372005-12-13 23:12:27 -0800641EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Eric Parisef41aaa2007-03-07 15:37:58 -0800643struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete,
644 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
David S. Miller2518c7c2006-08-24 04:45:07 -0700646 struct xfrm_policy *pol, *ret;
647 struct hlist_head *chain;
648 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Herbert Xub5505c62007-05-14 02:15:47 -0700650 *err = -ENOENT;
651 if (xfrm_policy_id2dir(id) != dir)
652 return NULL;
653
Eric Parisef41aaa2007-03-07 15:37:58 -0800654 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700656 chain = xfrm_policy_byidx + idx_hash(id);
657 ret = NULL;
658 hlist_for_each_entry(pol, entry, chain, byidx) {
659 if (pol->type == type && pol->index == id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700661 if (delete) {
Eric Parisef41aaa2007-03-07 15:37:58 -0800662 *err = security_xfrm_policy_delete(pol);
663 if (*err) {
664 write_unlock_bh(&xfrm_policy_lock);
665 return pol;
666 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700667 hlist_del(&pol->bydst);
668 hlist_del(&pol->byidx);
669 xfrm_policy_count[dir]--;
670 }
671 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 break;
673 }
674 }
675 write_unlock_bh(&xfrm_policy_lock);
676
David S. Miller2518c7c2006-08-24 04:45:07 -0700677 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700679 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700681 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683EXPORT_SYMBOL(xfrm_policy_byid);
684
Joy Latten4aa2e622007-06-04 19:05:57 -0400685#ifdef CONFIG_SECURITY_NETWORK_XFRM
686static inline int
687xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Joy Latten4aa2e622007-06-04 19:05:57 -0400689 int dir, err = 0;
690
691 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
692 struct xfrm_policy *pol;
693 struct hlist_node *entry;
694 int i;
695
696 hlist_for_each_entry(pol, entry,
697 &xfrm_policy_inexact[dir], bydst) {
698 if (pol->type != type)
699 continue;
700 err = security_xfrm_policy_delete(pol);
701 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700702 xfrm_audit_policy_delete(pol, 0,
703 audit_info->loginuid,
704 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400705 return err;
706 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900707 }
Joy Latten4aa2e622007-06-04 19:05:57 -0400708 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
709 hlist_for_each_entry(pol, entry,
710 xfrm_policy_bydst[dir].table + i,
711 bydst) {
712 if (pol->type != type)
713 continue;
714 err = security_xfrm_policy_delete(pol);
715 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700716 xfrm_audit_policy_delete(pol, 0,
717 audit_info->loginuid,
718 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400719 return err;
720 }
721 }
722 }
723 }
724 return err;
725}
726#else
727static inline int
728xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
729{
730 return 0;
731}
732#endif
733
734int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
735{
736 int dir, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 write_lock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400739
740 err = xfrm_policy_flush_secctx_check(type, audit_info);
741 if (err)
742 goto out;
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700745 struct xfrm_policy *pol;
746 struct hlist_node *entry;
David S. Millerae8c0572006-10-03 16:00:26 -0700747 int i, killed;
David S. Miller2518c7c2006-08-24 04:45:07 -0700748
David S. Millerae8c0572006-10-03 16:00:26 -0700749 killed = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700750 again1:
751 hlist_for_each_entry(pol, entry,
752 &xfrm_policy_inexact[dir], bydst) {
753 if (pol->type != type)
754 continue;
755 hlist_del(&pol->bydst);
756 hlist_del(&pol->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 write_unlock_bh(&xfrm_policy_lock);
758
Joy Lattenab5f5e82007-09-17 11:51:22 -0700759 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
760 audit_info->secid);
Joy Latten161a09e2006-11-27 13:11:54 -0600761
David S. Miller2518c7c2006-08-24 04:45:07 -0700762 xfrm_policy_kill(pol);
David S. Millerae8c0572006-10-03 16:00:26 -0700763 killed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700766 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700768
769 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
770 again2:
771 hlist_for_each_entry(pol, entry,
772 xfrm_policy_bydst[dir].table + i,
773 bydst) {
774 if (pol->type != type)
775 continue;
776 hlist_del(&pol->bydst);
777 hlist_del(&pol->byidx);
778 write_unlock_bh(&xfrm_policy_lock);
779
Joy Lattenab5f5e82007-09-17 11:51:22 -0700780 xfrm_audit_policy_delete(pol, 1,
781 audit_info->loginuid,
782 audit_info->secid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700783 xfrm_policy_kill(pol);
David S. Millerae8c0572006-10-03 16:00:26 -0700784 killed++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700785
786 write_lock_bh(&xfrm_policy_lock);
787 goto again2;
788 }
789 }
790
David S. Millerae8c0572006-10-03 16:00:26 -0700791 xfrm_policy_count[dir] -= killed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793 atomic_inc(&flow_cache_genid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400794out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 write_unlock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400796 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798EXPORT_SYMBOL(xfrm_policy_flush);
799
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700800int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 void *data)
802{
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800803 struct xfrm_policy *pol, *last = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700804 struct hlist_node *entry;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800805 int dir, last_dir = 0, count, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700808 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700811 struct hlist_head *table = xfrm_policy_bydst[dir].table;
812 int i;
813
814 hlist_for_each_entry(pol, entry,
815 &xfrm_policy_inexact[dir], bydst) {
816 if (pol->type != type)
817 continue;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800818 if (last) {
819 error = func(last, last_dir % XFRM_POLICY_MAX,
820 count, data);
821 if (error)
822 goto out;
823 }
824 last = pol;
825 last_dir = dir;
826 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700828 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
829 hlist_for_each_entry(pol, entry, table + i, bydst) {
830 if (pol->type != type)
831 continue;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800832 if (last) {
833 error = func(last, last_dir % XFRM_POLICY_MAX,
834 count, data);
835 if (error)
836 goto out;
837 }
838 last = pol;
839 last_dir = dir;
840 count++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700841 }
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800844 if (count == 0) {
845 error = -ENOENT;
846 goto out;
847 }
848 error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849out:
850 read_unlock_bh(&xfrm_policy_lock);
851 return error;
852}
853EXPORT_SYMBOL(xfrm_policy_walk);
854
James Morris134b0fc2006-10-05 15:42:27 -0500855/*
856 * Find policy to apply to this flow.
857 *
858 * Returns 0 if policy found, else an -errno.
859 */
David S. Miller2518c7c2006-08-24 04:45:07 -0700860static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
861 u8 type, u16 family, int dir)
862{
863 struct xfrm_selector *sel = &pol->selector;
James Morris134b0fc2006-10-05 15:42:27 -0500864 int match, ret = -ESRCH;
David S. Miller2518c7c2006-08-24 04:45:07 -0700865
866 if (pol->family != family ||
867 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500868 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700869
870 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500871 if (match)
872 ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700873
James Morris134b0fc2006-10-05 15:42:27 -0500874 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700875}
876
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700877static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
878 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
James Morris134b0fc2006-10-05 15:42:27 -0500880 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700881 struct xfrm_policy *pol, *ret;
882 xfrm_address_t *daddr, *saddr;
883 struct hlist_node *entry;
884 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700885 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700886
887 daddr = xfrm_flowi_daddr(fl, family);
888 saddr = xfrm_flowi_saddr(fl, family);
889 if (unlikely(!daddr || !saddr))
890 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700893 chain = policy_hash_direct(daddr, saddr, family, dir);
894 ret = NULL;
895 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500896 err = xfrm_policy_match(pol, fl, type, family, dir);
897 if (err) {
898 if (err == -ESRCH)
899 continue;
900 else {
901 ret = ERR_PTR(err);
902 goto fail;
903 }
904 } else {
David S. Milleracba48e2006-08-25 15:46:46 -0700905 ret = pol;
906 priority = ret->priority;
907 break;
908 }
909 }
910 chain = &xfrm_policy_inexact[dir];
911 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500912 err = xfrm_policy_match(pol, fl, type, family, dir);
913 if (err) {
914 if (err == -ESRCH)
915 continue;
916 else {
917 ret = ERR_PTR(err);
918 goto fail;
919 }
920 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700921 ret = pol;
922 break;
923 }
924 }
David S. Milleracba48e2006-08-25 15:46:46 -0700925 if (ret)
926 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -0500927fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700929
David S. Miller2518c7c2006-08-24 04:45:07 -0700930 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700931}
932
James Morris134b0fc2006-10-05 15:42:27 -0500933static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700934 void **objp, atomic_t **obj_refp)
935{
936 struct xfrm_policy *pol;
James Morris134b0fc2006-10-05 15:42:27 -0500937 int err = 0;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700938
939#ifdef CONFIG_XFRM_SUB_POLICY
940 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
James Morris134b0fc2006-10-05 15:42:27 -0500941 if (IS_ERR(pol)) {
942 err = PTR_ERR(pol);
943 pol = NULL;
944 }
945 if (pol || err)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700946 goto end;
947#endif
948 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
James Morris134b0fc2006-10-05 15:42:27 -0500949 if (IS_ERR(pol)) {
950 err = PTR_ERR(pol);
951 pol = NULL;
952 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700953#ifdef CONFIG_XFRM_SUB_POLICY
David S. Miller2518c7c2006-08-24 04:45:07 -0700954end:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700955#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if ((*objp = (void *) pol) != NULL)
957 *obj_refp = &pol->refcnt;
James Morris134b0fc2006-10-05 15:42:27 -0500958 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
Trent Jaegerdf718372005-12-13 23:12:27 -0800961static inline int policy_to_flow_dir(int dir)
962{
963 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900964 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
965 XFRM_POLICY_FWD == FLOW_DIR_FWD)
966 return dir;
967 switch (dir) {
968 default:
969 case XFRM_POLICY_IN:
970 return FLOW_DIR_IN;
971 case XFRM_POLICY_OUT:
972 return FLOW_DIR_OUT;
973 case XFRM_POLICY_FWD:
974 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700975 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800976}
977
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700978static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 struct xfrm_policy *pol;
981
982 read_lock_bh(&xfrm_policy_lock);
983 if ((pol = sk->sk_policy[dir]) != NULL) {
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900984 int match = xfrm_selector_match(&pol->selector, fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900986 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -0800987
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -0500988 if (match) {
989 err = security_xfrm_policy_lookup(pol, fl->secid,
990 policy_to_flow_dir(dir));
991 if (!err)
992 xfrm_pol_hold(pol);
993 else if (err == -ESRCH)
994 pol = NULL;
995 else
996 pol = ERR_PTR(err);
997 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 pol = NULL;
999 }
1000 read_unlock_bh(&xfrm_policy_lock);
1001 return pol;
1002}
1003
1004static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1005{
David S. Miller2518c7c2006-08-24 04:45:07 -07001006 struct hlist_head *chain = policy_hash_bysel(&pol->selector,
1007 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001008
David S. Miller2518c7c2006-08-24 04:45:07 -07001009 hlist_add_head(&pol->bydst, chain);
1010 hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
1011 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001013
1014 if (xfrm_bydst_should_resize(dir, NULL))
1015 schedule_work(&xfrm_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
1018static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1019 int dir)
1020{
David S. Miller2518c7c2006-08-24 04:45:07 -07001021 if (hlist_unhashed(&pol->bydst))
1022 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
David S. Miller2518c7c2006-08-24 04:45:07 -07001024 hlist_del(&pol->bydst);
1025 hlist_del(&pol->byidx);
1026 xfrm_policy_count[dir]--;
1027
1028 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
Herbert Xu4666faa2005-06-18 22:43:22 -07001031int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033 write_lock_bh(&xfrm_policy_lock);
1034 pol = __xfrm_policy_unlink(pol, dir);
1035 write_unlock_bh(&xfrm_policy_lock);
1036 if (pol) {
1037 if (dir < XFRM_POLICY_MAX)
1038 atomic_inc(&flow_cache_genid);
1039 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001042 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
David S. Millera70fcb02006-03-20 19:18:52 -08001044EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1047{
1048 struct xfrm_policy *old_pol;
1049
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001050#ifdef CONFIG_XFRM_SUB_POLICY
1051 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1052 return -EINVAL;
1053#endif
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 write_lock_bh(&xfrm_policy_lock);
1056 old_pol = sk->sk_policy[dir];
1057 sk->sk_policy[dir] = pol;
1058 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001059 pol->curlft.add_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001060 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1062 }
1063 if (old_pol)
1064 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1065 write_unlock_bh(&xfrm_policy_lock);
1066
1067 if (old_pol) {
1068 xfrm_policy_kill(old_pol);
1069 }
1070 return 0;
1071}
1072
1073static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1074{
1075 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
1076
1077 if (newp) {
1078 newp->selector = old->selector;
Trent Jaegerdf718372005-12-13 23:12:27 -08001079 if (security_xfrm_policy_clone(old, newp)) {
1080 kfree(newp);
1081 return NULL; /* ENOMEM */
1082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 newp->lft = old->lft;
1084 newp->curlft = old->curlft;
1085 newp->action = old->action;
1086 newp->flags = old->flags;
1087 newp->xfrm_nr = old->xfrm_nr;
1088 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001089 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 memcpy(newp->xfrm_vec, old->xfrm_vec,
1091 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1092 write_lock_bh(&xfrm_policy_lock);
1093 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1094 write_unlock_bh(&xfrm_policy_lock);
1095 xfrm_pol_put(newp);
1096 }
1097 return newp;
1098}
1099
1100int __xfrm_sk_clone_policy(struct sock *sk)
1101{
1102 struct xfrm_policy *p0 = sk->sk_policy[0],
1103 *p1 = sk->sk_policy[1];
1104
1105 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1106 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1107 return -ENOMEM;
1108 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1109 return -ENOMEM;
1110 return 0;
1111}
1112
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001113static int
1114xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
1115 unsigned short family)
1116{
1117 int err;
1118 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1119
1120 if (unlikely(afinfo == NULL))
1121 return -EINVAL;
1122 err = afinfo->get_saddr(local, remote);
1123 xfrm_policy_put_afinfo(afinfo);
1124 return err;
1125}
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127/* Resolve list of templates for the flow, given policy. */
1128
1129static int
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001130xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1131 struct xfrm_state **xfrm,
1132 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
1134 int nx;
1135 int i, error;
1136 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1137 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001138 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1141 struct xfrm_state *x;
1142 xfrm_address_t *remote = daddr;
1143 xfrm_address_t *local = saddr;
1144 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1145
Joakim Koskela48b8d782007-07-26 00:08:42 -07001146 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1147 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 remote = &tmpl->id.daddr;
1149 local = &tmpl->saddr;
Miika Komu76b3f052006-11-30 16:40:43 -08001150 family = tmpl->encap_family;
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001151 if (xfrm_addr_any(local, family)) {
1152 error = xfrm_get_saddr(&tmp, remote, family);
1153 if (error)
1154 goto fail;
1155 local = &tmp;
1156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158
1159 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1160
1161 if (x && x->km.state == XFRM_STATE_VALID) {
1162 xfrm[nx++] = x;
1163 daddr = remote;
1164 saddr = local;
1165 continue;
1166 }
1167 if (x) {
1168 error = (x->km.state == XFRM_STATE_ERROR ?
1169 -EINVAL : -EAGAIN);
1170 xfrm_state_put(x);
1171 }
1172
1173 if (!tmpl->optional)
1174 goto fail;
1175 }
1176 return nx;
1177
1178fail:
1179 for (nx--; nx>=0; nx--)
1180 xfrm_state_put(xfrm[nx]);
1181 return error;
1182}
1183
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001184static int
1185xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1186 struct xfrm_state **xfrm,
1187 unsigned short family)
1188{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001189 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1190 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001191 int cnx = 0;
1192 int error;
1193 int ret;
1194 int i;
1195
1196 for (i = 0; i < npols; i++) {
1197 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1198 error = -ENOBUFS;
1199 goto fail;
1200 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001201
1202 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001203 if (ret < 0) {
1204 error = ret;
1205 goto fail;
1206 } else
1207 cnx += ret;
1208 }
1209
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001210 /* found states are sorted for outbound processing */
1211 if (npols > 1)
1212 xfrm_state_sort(xfrm, tpp, cnx, family);
1213
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001214 return cnx;
1215
1216 fail:
1217 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001218 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001219 return error;
1220
1221}
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223/* Check that the bundle accepts the flow and its components are
1224 * still valid.
1225 */
1226
1227static struct dst_entry *
1228xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1229{
1230 struct dst_entry *x;
1231 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1232 if (unlikely(afinfo == NULL))
1233 return ERR_PTR(-EINVAL);
1234 x = afinfo->find_bundle(fl, policy);
1235 xfrm_policy_put_afinfo(afinfo);
1236 return x;
1237}
1238
Herbert Xu25ee3282007-12-11 09:32:34 -08001239static inline int xfrm_get_tos(struct flowi *fl, int family)
1240{
1241 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1242 int tos;
1243
1244 if (!afinfo)
1245 return -EINVAL;
1246
1247 tos = afinfo->get_tos(fl);
1248
1249 xfrm_policy_put_afinfo(afinfo);
1250
1251 return tos;
1252}
1253
1254static inline struct xfrm_dst *xfrm_alloc_dst(int family)
1255{
1256 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1257 struct xfrm_dst *xdst;
1258
1259 if (!afinfo)
1260 return ERR_PTR(-EINVAL);
1261
1262 xdst = dst_alloc(afinfo->dst_ops) ?: ERR_PTR(-ENOBUFS);
1263
1264 xfrm_policy_put_afinfo(afinfo);
1265
1266 return xdst;
1267}
1268
1269static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev)
1270{
1271 struct xfrm_policy_afinfo *afinfo =
1272 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1273 int err;
1274
1275 if (!afinfo)
1276 return -EINVAL;
1277
1278 err = afinfo->fill_dst(xdst, dev);
1279
1280 xfrm_policy_put_afinfo(afinfo);
1281
1282 return err;
1283}
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1286 * all the metrics... Shortly, bundle a bundle.
1287 */
1288
Herbert Xu25ee3282007-12-11 09:32:34 -08001289static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1290 struct xfrm_state **xfrm, int nx,
1291 struct flowi *fl,
1292 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Herbert Xu25ee3282007-12-11 09:32:34 -08001294 unsigned long now = jiffies;
1295 struct net_device *dev;
1296 struct dst_entry *dst_prev = NULL;
1297 struct dst_entry *dst0 = NULL;
1298 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001300 int header_len = 0;
1301 int trailer_len = 0;
1302 int tos;
1303 int family = policy->selector.family;
1304
1305 tos = xfrm_get_tos(fl, family);
1306 err = tos;
1307 if (tos < 0)
1308 goto put_states;
1309
1310 dst_hold(dst);
1311
1312 for (; i < nx; i++) {
1313 struct xfrm_dst *xdst = xfrm_alloc_dst(family);
1314 struct dst_entry *dst1 = &xdst->u.dst;
1315
1316 err = PTR_ERR(xdst);
1317 if (IS_ERR(xdst)) {
1318 dst_release(dst);
1319 goto put_states;
1320 }
1321
1322 if (!dst_prev)
1323 dst0 = dst1;
1324 else {
1325 dst_prev->child = dst_clone(dst1);
1326 dst1->flags |= DST_NOHASH;
1327 }
1328
1329 xdst->route = dst;
1330 memcpy(&dst1->metrics, &dst->metrics, sizeof(dst->metrics));
1331
1332 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1333 family = xfrm[i]->props.family;
1334 dst = xfrm_dst_lookup(xfrm[i], tos, family);
1335 err = PTR_ERR(dst);
1336 if (IS_ERR(dst))
1337 goto put_states;
1338 } else
1339 dst_hold(dst);
1340
1341 dst1->xfrm = xfrm[i];
1342 xdst->genid = xfrm[i]->genid;
1343
1344 dst1->obsolete = -1;
1345 dst1->flags |= DST_HOST;
1346 dst1->lastuse = now;
1347
1348 dst1->input = dst_discard;
1349 dst1->output = xfrm[i]->outer_mode->afinfo->output;
1350
1351 dst1->next = dst_prev;
1352 dst_prev = dst1;
1353
1354 header_len += xfrm[i]->props.header_len;
1355 trailer_len += xfrm[i]->props.trailer_len;
1356 }
1357
1358 dst_prev->child = dst;
1359 dst0->path = dst;
1360
1361 err = -ENODEV;
1362 dev = dst->dev;
1363 if (!dev)
1364 goto free_dst;
1365
1366 /* Copy neighbout for reachability confirmation */
1367 dst0->neighbour = neigh_clone(dst->neighbour);
1368
1369 xfrm_init_pmtu(dst_prev);
1370
1371 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1372 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1373
1374 err = xfrm_fill_dst(xdst, dev);
1375 if (err)
1376 goto free_dst;
1377
1378 dst_prev->header_len = header_len;
1379 dst_prev->trailer_len = trailer_len;
1380 header_len -= xdst->u.dst.xfrm->props.header_len;
1381 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1382 }
1383
1384out:
1385 return dst0;
1386
1387put_states:
1388 for (; i < nx; i++)
1389 xfrm_state_put(xfrm[i]);
1390free_dst:
1391 if (dst0)
1392 dst_free(dst0);
1393 dst0 = ERR_PTR(err);
1394 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001397static int inline
1398xfrm_dst_alloc_copy(void **target, void *src, int size)
1399{
1400 if (!*target) {
1401 *target = kmalloc(size, GFP_ATOMIC);
1402 if (!*target)
1403 return -ENOMEM;
1404 }
1405 memcpy(*target, src, size);
1406 return 0;
1407}
1408
1409static int inline
1410xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
1411{
1412#ifdef CONFIG_XFRM_SUB_POLICY
1413 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1414 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1415 sel, sizeof(*sel));
1416#else
1417 return 0;
1418#endif
1419}
1420
1421static int inline
1422xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
1423{
1424#ifdef CONFIG_XFRM_SUB_POLICY
1425 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1426 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1427#else
1428 return 0;
1429#endif
1430}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432static int stale_bundle(struct dst_entry *dst);
1433
1434/* Main function: finds/creates a bundle for given flow.
1435 *
1436 * At the moment we eat a raw IP route. Mostly to speed up lookups
1437 * on interfaces with disabled IPsec.
1438 */
David S. Miller14e50e52007-05-24 18:17:54 -07001439int __xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1440 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 struct xfrm_policy *policy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001443 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1444 int npols;
1445 int pol_dead;
1446 int xfrm_nr;
1447 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1449 struct dst_entry *dst, *dst_orig = *dst_p;
1450 int nx = 0;
1451 int err;
1452 u32 genid;
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001453 u16 family;
Trent Jaegerdf718372005-12-13 23:12:27 -08001454 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456restart:
1457 genid = atomic_read(&flow_cache_genid);
1458 policy = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001459 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1460 pols[pi] = NULL;
1461 npols = 0;
1462 pol_dead = 0;
1463 xfrm_nr = 0;
1464
Thomas Graff7944fb2007-08-25 13:46:55 -07001465 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001466 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
Herbert Xu75b8c132007-12-11 04:38:08 -08001467 err = PTR_ERR(policy);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001468 if (IS_ERR(policy))
Herbert Xu75b8c132007-12-11 04:38:08 -08001469 goto dropdst;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 if (!policy) {
1473 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07001474 if ((dst_orig->flags & DST_NOXFRM) ||
1475 !xfrm_policy_count[XFRM_POLICY_OUT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return 0;
1477
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001478 policy = flow_cache_lookup(fl, dst_orig->ops->family,
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001479 dir, xfrm_policy_lookup);
Herbert Xu75b8c132007-12-11 04:38:08 -08001480 err = PTR_ERR(policy);
James Morris134b0fc2006-10-05 15:42:27 -05001481 if (IS_ERR(policy))
Herbert Xu75b8c132007-12-11 04:38:08 -08001482 goto dropdst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484
1485 if (!policy)
1486 return 0;
1487
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001488 family = dst_orig->ops->family;
James Morris9d729f72007-03-04 16:12:44 -08001489 policy->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001490 pols[0] = policy;
1491 npols ++;
1492 xfrm_nr += pols[0]->xfrm_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 switch (policy->action) {
Herbert Xu5e5234f2007-11-30 00:50:31 +11001495 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 case XFRM_POLICY_BLOCK:
1497 /* Prohibit the flow */
Patrick McHardye104411b2005-09-08 15:11:55 -07001498 err = -EPERM;
1499 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 case XFRM_POLICY_ALLOW:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001502#ifndef CONFIG_XFRM_SUB_POLICY
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (policy->xfrm_nr == 0) {
1504 /* Flow passes not transformed. */
1505 xfrm_pol_put(policy);
1506 return 0;
1507 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001508#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 /* Try to find matching bundle.
1511 *
1512 * LATER: help from flow cache. It is optional, this
1513 * is required only for output policy.
1514 */
1515 dst = xfrm_find_bundle(fl, policy, family);
1516 if (IS_ERR(dst)) {
Patrick McHardye104411b2005-09-08 15:11:55 -07001517 err = PTR_ERR(dst);
1518 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 }
1520
1521 if (dst)
1522 break;
1523
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001524#ifdef CONFIG_XFRM_SUB_POLICY
1525 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1526 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1527 fl, family,
1528 XFRM_POLICY_OUT);
1529 if (pols[1]) {
James Morris134b0fc2006-10-05 15:42:27 -05001530 if (IS_ERR(pols[1])) {
1531 err = PTR_ERR(pols[1]);
1532 goto error;
1533 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001534 if (pols[1]->action == XFRM_POLICY_BLOCK) {
1535 err = -EPERM;
1536 goto error;
1537 }
1538 npols ++;
1539 xfrm_nr += pols[1]->xfrm_nr;
1540 }
1541 }
1542
1543 /*
1544 * Because neither flowi nor bundle information knows about
1545 * transformation template size. On more than one policy usage
1546 * we can realize whether all of them is bypass or not after
1547 * they are searched. See above not-transformed bypass
1548 * is surrounded by non-sub policy configuration, too.
1549 */
1550 if (xfrm_nr == 0) {
1551 /* Flow passes not transformed. */
1552 xfrm_pols_put(pols, npols);
1553 return 0;
1554 }
1555
1556#endif
1557 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 if (unlikely(nx<0)) {
1560 err = nx;
David S. Miller14e50e52007-05-24 18:17:54 -07001561 if (err == -EAGAIN && sysctl_xfrm_larval_drop) {
1562 /* EREMOTE tells the caller to generate
1563 * a one-shot blackhole route.
1564 */
1565 xfrm_pol_put(policy);
1566 return -EREMOTE;
1567 }
Herbert Xu815f4e52007-12-12 10:36:59 -08001568 if (err == -EAGAIN && (flags & XFRM_LOOKUP_WAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 DECLARE_WAITQUEUE(wait, current);
1570
1571 add_wait_queue(&km_waitq, &wait);
1572 set_current_state(TASK_INTERRUPTIBLE);
1573 schedule();
1574 set_current_state(TASK_RUNNING);
1575 remove_wait_queue(&km_waitq, &wait);
1576
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001577 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579 if (nx == -EAGAIN && signal_pending(current)) {
1580 err = -ERESTART;
1581 goto error;
1582 }
1583 if (nx == -EAGAIN ||
1584 genid != atomic_read(&flow_cache_genid)) {
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001585 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 goto restart;
1587 }
1588 err = nx;
1589 }
1590 if (err < 0)
1591 goto error;
1592 }
1593 if (nx == 0) {
1594 /* Flow passes not transformed. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001595 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return 0;
1597 }
1598
Herbert Xu25ee3282007-12-11 09:32:34 -08001599 dst = xfrm_bundle_create(policy, xfrm, nx, fl, dst_orig);
1600 err = PTR_ERR(dst);
1601 if (IS_ERR(dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001604 for (pi = 0; pi < npols; pi++) {
1605 read_lock_bh(&pols[pi]->lock);
1606 pol_dead |= pols[pi]->dead;
1607 read_unlock_bh(&pols[pi]->lock);
1608 }
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 write_lock_bh(&policy->lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001611 if (unlikely(pol_dead || stale_bundle(dst))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 /* Wow! While we worked on resolving, this
1613 * policy has gone. Retry. It is not paranoia,
1614 * we just cannot enlist new bundle to dead object.
1615 * We can't enlist stable bundles either.
1616 */
1617 write_unlock_bh(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (dst)
1619 dst_free(dst);
Herbert Xu00de6512006-02-13 16:01:27 -08001620
1621 err = -EHOSTUNREACH;
1622 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001624
1625 if (npols > 1)
1626 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1627 else
1628 err = xfrm_dst_update_origin(dst, fl);
1629 if (unlikely(err)) {
1630 write_unlock_bh(&policy->lock);
1631 if (dst)
1632 dst_free(dst);
1633 goto error;
1634 }
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 dst->next = policy->bundles;
1637 policy->bundles = dst;
1638 dst_hold(dst);
1639 write_unlock_bh(&policy->lock);
1640 }
1641 *dst_p = dst;
1642 dst_release(dst_orig);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001643 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return 0;
1645
1646error:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001647 xfrm_pols_put(pols, npols);
Herbert Xu75b8c132007-12-11 04:38:08 -08001648dropdst:
1649 dst_release(dst_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 *dst_p = NULL;
1651 return err;
1652}
David S. Miller14e50e52007-05-24 18:17:54 -07001653EXPORT_SYMBOL(__xfrm_lookup);
1654
1655int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1656 struct sock *sk, int flags)
1657{
1658 int err = __xfrm_lookup(dst_p, fl, sk, flags);
1659
1660 if (err == -EREMOTE) {
1661 dst_release(*dst_p);
1662 *dst_p = NULL;
1663 err = -EAGAIN;
1664 }
1665
1666 return err;
1667}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668EXPORT_SYMBOL(xfrm_lookup);
1669
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001670static inline int
1671xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1672{
1673 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001674
1675 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1676 return 0;
1677 x = skb->sp->xvec[idx];
1678 if (!x->type->reject)
1679 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07001680 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001681}
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683/* When skb is transformed back to its "native" form, we have to
1684 * check policy restrictions. At the moment we make this in maximally
1685 * stupid way. Shame on me. :-) Of course, connected sockets must
1686 * have policy cached at them.
1687 */
1688
1689static inline int
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001690xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 unsigned short family)
1692{
1693 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08001694 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return x->id.proto == tmpl->id.proto &&
1696 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1697 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1698 x->props.mode == tmpl->mode &&
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07001699 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1700 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001701 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1702 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001705/*
1706 * 0 or more than 0 is returned when validation is succeeded (either bypass
1707 * because of optional transport mode, or next index of the mathced secpath
1708 * state with the template.
1709 * -1 is returned when no matching template is found.
1710 * Otherwise "-2 - errored_index" is returned.
1711 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712static inline int
1713xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1714 unsigned short family)
1715{
1716 int idx = start;
1717
1718 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001719 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 return start;
1721 } else
1722 start = -1;
1723 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001724 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001726 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1727 if (start == -1)
1728 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
1732 return start;
1733}
1734
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001735int
1736xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001739 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 if (unlikely(afinfo == NULL))
1742 return -EAFNOSUPPORT;
1743
1744 afinfo->decode_session(skb, fl);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07001745 err = security_xfrm_decode_session(skb, &fl->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001747 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001749EXPORT_SYMBOL(xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001751static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
1753 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001754 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001755 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
1759
1760 return 0;
1761}
1762
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001763int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 unsigned short family)
1765{
1766 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001767 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1768 int npols = 0;
1769 int xfrm_nr;
1770 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 struct flowi fl;
Trent Jaegerdf718372005-12-13 23:12:27 -08001772 u8 fl_dir = policy_to_flow_dir(dir);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001773 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001775 if (xfrm_decode_session(skb, &fl, family) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return 0;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08001777 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 /* First, check used SA against their selectors. */
1780 if (skb->sp) {
1781 int i;
1782
1783 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001784 struct xfrm_state *x = skb->sp->xvec[i];
1785 if (!xfrm_selector_match(&x->sel, &fl, family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
1788 }
1789
1790 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001791 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001792 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001793 if (IS_ERR(pol))
1794 return 0;
1795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 if (!pol)
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001798 pol = flow_cache_lookup(&fl, family, fl_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 xfrm_policy_lookup);
1800
James Morris134b0fc2006-10-05 15:42:27 -05001801 if (IS_ERR(pol))
1802 return 0;
1803
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001804 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001805 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001806 xfrm_secpath_reject(xerr_idx, skb, &fl);
1807 return 0;
1808 }
1809 return 1;
1810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
James Morris9d729f72007-03-04 16:12:44 -08001812 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001814 pols[0] = pol;
1815 npols ++;
1816#ifdef CONFIG_XFRM_SUB_POLICY
1817 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1818 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1819 &fl, family,
1820 XFRM_POLICY_IN);
1821 if (pols[1]) {
James Morris134b0fc2006-10-05 15:42:27 -05001822 if (IS_ERR(pols[1]))
1823 return 0;
James Morris9d729f72007-03-04 16:12:44 -08001824 pols[1]->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001825 npols ++;
1826 }
1827 }
1828#endif
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (pol->action == XFRM_POLICY_ALLOW) {
1831 struct sec_path *sp;
1832 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001833 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001834 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001835 struct xfrm_tmpl **tpp = tp;
1836 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 int i, k;
1838
1839 if ((sp = skb->sp) == NULL)
1840 sp = &dummy;
1841
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001842 for (pi = 0; pi < npols; pi++) {
1843 if (pols[pi] != pol &&
1844 pols[pi]->action != XFRM_POLICY_ALLOW)
1845 goto reject;
1846 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
1847 goto reject_error;
1848 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1849 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1850 }
1851 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001852 if (npols > 1) {
1853 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1854 tpp = stp;
1855 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 /* For each tunnel xfrm, find the first matching tmpl.
1858 * For each tmpl before that, find corresponding xfrm.
1859 * Order is _important_. Later we will implement
1860 * some barriers, but at the moment barriers
1861 * are implied between each two transformations.
1862 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001863 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1864 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001865 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001866 if (k < -1)
1867 /* "-2 - errored_index" returned */
1868 xerr_idx = -(2+k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
1872
James Morrisd1d9fac2006-09-01 00:32:12 -07001873 if (secpath_has_nontransport(sp, k, &xerr_idx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 goto reject;
1875
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001876 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return 1;
1878 }
1879
1880reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001881 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001882reject_error:
1883 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return 0;
1885}
1886EXPORT_SYMBOL(__xfrm_policy_check);
1887
1888int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1889{
1890 struct flowi fl;
1891
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001892 if (xfrm_decode_session(skb, &fl, family) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 return 0;
1894
1895 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1896}
1897EXPORT_SYMBOL(__xfrm_route_forward);
1898
David S. Millerd49c73c2006-08-13 18:55:53 -07001899/* Optimize later using cookies and generation ids. */
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1902{
David S. Millerd49c73c2006-08-13 18:55:53 -07001903 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1904 * to "-1" to force all XFRM destinations to get validated by
1905 * dst_ops->check on every use. We do this because when a
1906 * normal route referenced by an XFRM dst is obsoleted we do
1907 * not go looking around for all parent referencing XFRM dsts
1908 * so that we can invalidate them. It is just too much work.
1909 * Instead we make the checks here on every use. For example:
1910 *
1911 * XFRM dst A --> IPv4 dst X
1912 *
1913 * X is the "xdst->route" of A (X is also the "dst->path" of A
1914 * in this example). If X is marked obsolete, "A" will not
1915 * notice. That's what we are validating here via the
1916 * stale_bundle() check.
1917 *
1918 * When a policy's bundle is pruned, we dst_free() the XFRM
1919 * dst which causes it's ->obsolete field to be set to a
1920 * positive non-zero integer. If an XFRM dst has been pruned
1921 * like this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08001922 */
David S. Millerd49c73c2006-08-13 18:55:53 -07001923 if (dst->obsolete < 0 && !stale_bundle(dst))
1924 return dst;
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return NULL;
1927}
1928
1929static int stale_bundle(struct dst_entry *dst)
1930{
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05001931 return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
1933
Herbert Xuaabc9762005-05-03 16:27:10 -07001934void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -08001937 dst->dev = dev->nd_net->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07001938 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 dev_put(dev);
1940 }
1941}
Herbert Xuaabc9762005-05-03 16:27:10 -07001942EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944static void xfrm_link_failure(struct sk_buff *skb)
1945{
1946 /* Impossible. Such dst must be popped before reaches point of failure. */
1947 return;
1948}
1949
1950static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1951{
1952 if (dst) {
1953 if (dst->obsolete) {
1954 dst_release(dst);
1955 dst = NULL;
1956 }
1957 }
1958 return dst;
1959}
1960
David S. Miller2518c7c2006-08-24 04:45:07 -07001961static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
1962{
1963 struct dst_entry *dst, **dstp;
1964
1965 write_lock(&pol->lock);
1966 dstp = &pol->bundles;
1967 while ((dst=*dstp) != NULL) {
1968 if (func(dst)) {
1969 *dstp = dst->next;
1970 dst->next = *gc_list_p;
1971 *gc_list_p = dst;
1972 } else {
1973 dstp = &dst->next;
1974 }
1975 }
1976 write_unlock(&pol->lock);
1977}
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1980{
David S. Miller2518c7c2006-08-24 04:45:07 -07001981 struct dst_entry *gc_list = NULL;
1982 int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07001985 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
1986 struct xfrm_policy *pol;
1987 struct hlist_node *entry;
1988 struct hlist_head *table;
1989 int i;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001990
David S. Miller2518c7c2006-08-24 04:45:07 -07001991 hlist_for_each_entry(pol, entry,
1992 &xfrm_policy_inexact[dir], bydst)
1993 prune_one_bundle(pol, func, &gc_list);
1994
1995 table = xfrm_policy_bydst[dir].table;
1996 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
1997 hlist_for_each_entry(pol, entry, table + i, bydst)
1998 prune_one_bundle(pol, func, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
2000 }
2001 read_unlock_bh(&xfrm_policy_lock);
2002
2003 while (gc_list) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002004 struct dst_entry *dst = gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 gc_list = dst->next;
2006 dst_free(dst);
2007 }
2008}
2009
2010static int unused_bundle(struct dst_entry *dst)
2011{
2012 return !atomic_read(&dst->__refcnt);
2013}
2014
2015static void __xfrm_garbage_collect(void)
2016{
2017 xfrm_prune_bundles(unused_bundle);
2018}
2019
David S. Miller1c095392006-08-24 03:30:28 -07002020static int xfrm_flush_bundles(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
2022 xfrm_prune_bundles(stale_bundle);
2023 return 0;
2024}
2025
Herbert Xu25ee3282007-12-11 09:32:34 -08002026static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
2028 do {
2029 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2030 u32 pmtu, route_mtu_cached;
2031
2032 pmtu = dst_mtu(dst->child);
2033 xdst->child_mtu_cached = pmtu;
2034
2035 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2036
2037 route_mtu_cached = dst_mtu(xdst->route);
2038 xdst->route_mtu_cached = route_mtu_cached;
2039
2040 if (pmtu > route_mtu_cached)
2041 pmtu = route_mtu_cached;
2042
2043 dst->metrics[RTAX_MTU-1] = pmtu;
2044 } while ((dst = dst->next));
2045}
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047/* Check that the bundle accepts the flow and its components are
2048 * still valid.
2049 */
2050
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05002051int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
2052 struct flowi *fl, int family, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
2054 struct dst_entry *dst = &first->u.dst;
2055 struct xfrm_dst *last;
2056 u32 mtu;
2057
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002058 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 (dst->dev && !netif_running(dst->dev)))
2060 return 0;
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07002061#ifdef CONFIG_XFRM_SUB_POLICY
2062 if (fl) {
2063 if (first->origin && !flow_cache_uli_match(first->origin, fl))
2064 return 0;
2065 if (first->partner &&
2066 !xfrm_selector_match(first->partner, fl, family))
2067 return 0;
2068 }
2069#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 last = NULL;
2072
2073 do {
2074 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2075
2076 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
2077 return 0;
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06002078 if (fl && pol &&
2079 !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2082 return 0;
David S. Miller9d4a7062006-08-24 03:18:09 -07002083 if (xdst->genid != dst->xfrm->genid)
2084 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Herbert Xu1bfcb102007-10-17 21:31:50 -07002086 if (strict && fl &&
Herbert Xu13996372007-10-17 21:35:51 -07002087 !(dst->xfrm->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07002088 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
2089 return 0;
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 mtu = dst_mtu(dst->child);
2092 if (xdst->child_mtu_cached != mtu) {
2093 last = xdst;
2094 xdst->child_mtu_cached = mtu;
2095 }
2096
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002097 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 return 0;
2099 mtu = dst_mtu(xdst->route);
2100 if (xdst->route_mtu_cached != mtu) {
2101 last = xdst;
2102 xdst->route_mtu_cached = mtu;
2103 }
2104
2105 dst = dst->child;
2106 } while (dst->xfrm);
2107
2108 if (likely(!last))
2109 return 1;
2110
2111 mtu = last->child_mtu_cached;
2112 for (;;) {
2113 dst = &last->u.dst;
2114
2115 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2116 if (mtu > last->route_mtu_cached)
2117 mtu = last->route_mtu_cached;
2118 dst->metrics[RTAX_MTU-1] = mtu;
2119
2120 if (last == first)
2121 break;
2122
Patrick McHardybd0bf072007-07-18 01:55:52 -07002123 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 last->child_mtu_cached = mtu;
2125 }
2126
2127 return 1;
2128}
2129
2130EXPORT_SYMBOL(xfrm_bundle_ok);
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2133{
2134 int err = 0;
2135 if (unlikely(afinfo == NULL))
2136 return -EINVAL;
2137 if (unlikely(afinfo->family >= NPROTO))
2138 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002139 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2141 err = -ENOBUFS;
2142 else {
2143 struct dst_ops *dst_ops = afinfo->dst_ops;
2144 if (likely(dst_ops->kmem_cachep == NULL))
2145 dst_ops->kmem_cachep = xfrm_dst_cache;
2146 if (likely(dst_ops->check == NULL))
2147 dst_ops->check = xfrm_dst_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 if (likely(dst_ops->negative_advice == NULL))
2149 dst_ops->negative_advice = xfrm_negative_advice;
2150 if (likely(dst_ops->link_failure == NULL))
2151 dst_ops->link_failure = xfrm_link_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (likely(afinfo->garbage_collect == NULL))
2153 afinfo->garbage_collect = __xfrm_garbage_collect;
2154 xfrm_policy_afinfo[afinfo->family] = afinfo;
2155 }
Ingo Molnare959d812006-04-28 15:32:29 -07002156 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return err;
2158}
2159EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2160
2161int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2162{
2163 int err = 0;
2164 if (unlikely(afinfo == NULL))
2165 return -EINVAL;
2166 if (unlikely(afinfo->family >= NPROTO))
2167 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002168 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2170 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2171 err = -EINVAL;
2172 else {
2173 struct dst_ops *dst_ops = afinfo->dst_ops;
2174 xfrm_policy_afinfo[afinfo->family] = NULL;
2175 dst_ops->kmem_cachep = NULL;
2176 dst_ops->check = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 dst_ops->negative_advice = NULL;
2178 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 afinfo->garbage_collect = NULL;
2180 }
2181 }
Ingo Molnare959d812006-04-28 15:32:29 -07002182 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return err;
2184}
2185EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2186
2187static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
2188{
2189 struct xfrm_policy_afinfo *afinfo;
2190 if (unlikely(family >= NPROTO))
2191 return NULL;
2192 read_lock(&xfrm_policy_afinfo_lock);
2193 afinfo = xfrm_policy_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07002194 if (unlikely(!afinfo))
2195 read_unlock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 return afinfo;
2197}
2198
2199static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2200{
Herbert Xu546be242006-05-27 23:03:58 -07002201 read_unlock(&xfrm_policy_afinfo_lock);
2202}
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2205{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002206 struct net_device *dev = ptr;
2207
2208 if (dev->nd_net != &init_net)
2209 return NOTIFY_DONE;
2210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 switch (event) {
2212 case NETDEV_DOWN:
2213 xfrm_flush_bundles();
2214 }
2215 return NOTIFY_DONE;
2216}
2217
2218static struct notifier_block xfrm_dev_notifier = {
2219 xfrm_dev_event,
2220 NULL,
2221 0
2222};
2223
2224static void __init xfrm_policy_init(void)
2225{
David S. Miller2518c7c2006-08-24 04:45:07 -07002226 unsigned int hmask, sz;
2227 int dir;
2228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2230 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002231 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002232 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
David S. Miller2518c7c2006-08-24 04:45:07 -07002234 hmask = 8 - 1;
2235 sz = (hmask+1) * sizeof(struct hlist_head);
2236
David S. Miller44e36b42006-08-24 04:50:50 -07002237 xfrm_policy_byidx = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002238 xfrm_idx_hmask = hmask;
2239 if (!xfrm_policy_byidx)
2240 panic("XFRM: failed to allocate byidx hash\n");
2241
2242 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2243 struct xfrm_policy_hash *htab;
2244
2245 INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
2246
2247 htab = &xfrm_policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002248 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002249 htab->hmask = hmask;
2250 if (!htab->table)
2251 panic("XFRM: failed to allocate bydst hash\n");
2252 }
2253
David Howellsc4028952006-11-22 14:57:56 +00002254 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 register_netdevice_notifier(&xfrm_dev_notifier);
2256}
2257
2258void __init xfrm_init(void)
2259{
2260 xfrm_state_init();
2261 xfrm_policy_init();
2262 xfrm_input_init();
2263}
2264
Joy Lattenab5f5e82007-09-17 11:51:22 -07002265#ifdef CONFIG_AUDITSYSCALL
2266static inline void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2267 struct audit_buffer *audit_buf)
2268{
Paul Moore875179f2007-12-01 23:27:18 +11002269 struct xfrm_sec_ctx *ctx = xp->security;
2270 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002271
Paul Moore875179f2007-12-01 23:27:18 +11002272 if (ctx)
2273 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2274 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2275
2276 switch(sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002277 case AF_INET:
Paul Moore875179f2007-12-01 23:27:18 +11002278 audit_log_format(audit_buf, " src=" NIPQUAD_FMT,
2279 NIPQUAD(sel->saddr.a4));
2280 if (sel->prefixlen_s != 32)
2281 audit_log_format(audit_buf, " src_prefixlen=%d",
2282 sel->prefixlen_s);
2283 audit_log_format(audit_buf, " dst=" NIPQUAD_FMT,
2284 NIPQUAD(sel->daddr.a4));
2285 if (sel->prefixlen_d != 32)
2286 audit_log_format(audit_buf, " dst_prefixlen=%d",
2287 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002288 break;
2289 case AF_INET6:
Paul Moore875179f2007-12-01 23:27:18 +11002290 audit_log_format(audit_buf, " src=" NIP6_FMT,
2291 NIP6(*(struct in6_addr *)sel->saddr.a6));
2292 if (sel->prefixlen_s != 128)
2293 audit_log_format(audit_buf, " src_prefixlen=%d",
2294 sel->prefixlen_s);
2295 audit_log_format(audit_buf, " dst=" NIP6_FMT,
2296 NIP6(*(struct in6_addr *)sel->daddr.a6));
2297 if (sel->prefixlen_d != 128)
2298 audit_log_format(audit_buf, " dst_prefixlen=%d",
2299 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002300 break;
2301 }
2302}
2303
2304void
2305xfrm_audit_policy_add(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
2306{
2307 struct audit_buffer *audit_buf;
2308 extern int audit_enabled;
2309
2310 if (audit_enabled == 0)
2311 return;
Paul Moore5951cab2007-12-20 00:00:45 -08002312 audit_buf = xfrm_audit_start(auid, sid);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002313 if (audit_buf == NULL)
2314 return;
2315 audit_log_format(audit_buf, " op=SPD-add res=%u", result);
2316 xfrm_audit_common_policyinfo(xp, audit_buf);
2317 audit_log_end(audit_buf);
2318}
2319EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
2320
2321void
2322xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
2323{
2324 struct audit_buffer *audit_buf;
2325 extern int audit_enabled;
2326
2327 if (audit_enabled == 0)
2328 return;
Paul Moore5951cab2007-12-20 00:00:45 -08002329 audit_buf = xfrm_audit_start(auid, sid);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002330 if (audit_buf == NULL)
2331 return;
2332 audit_log_format(audit_buf, " op=SPD-delete res=%u", result);
2333 xfrm_audit_common_policyinfo(xp, audit_buf);
2334 audit_log_end(audit_buf);
2335}
2336EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
2337#endif
2338
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002339#ifdef CONFIG_XFRM_MIGRATE
2340static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
2341 struct xfrm_selector *sel_tgt)
2342{
2343 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
2344 if (sel_tgt->family == sel_cmp->family &&
2345 xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002346 sel_cmp->family) == 0 &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002347 xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
2348 sel_cmp->family) == 0 &&
2349 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
2350 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
2351 return 1;
2352 }
2353 } else {
2354 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
2355 return 1;
2356 }
2357 }
2358 return 0;
2359}
2360
2361static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
2362 u8 dir, u8 type)
2363{
2364 struct xfrm_policy *pol, *ret = NULL;
2365 struct hlist_node *entry;
2366 struct hlist_head *chain;
2367 u32 priority = ~0U;
2368
2369 read_lock_bh(&xfrm_policy_lock);
2370 chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
2371 hlist_for_each_entry(pol, entry, chain, bydst) {
2372 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2373 pol->type == type) {
2374 ret = pol;
2375 priority = ret->priority;
2376 break;
2377 }
2378 }
2379 chain = &xfrm_policy_inexact[dir];
2380 hlist_for_each_entry(pol, entry, chain, bydst) {
2381 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2382 pol->type == type &&
2383 pol->priority < priority) {
2384 ret = pol;
2385 break;
2386 }
2387 }
2388
2389 if (ret)
2390 xfrm_pol_hold(ret);
2391
2392 read_unlock_bh(&xfrm_policy_lock);
2393
2394 return ret;
2395}
2396
2397static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
2398{
2399 int match = 0;
2400
2401 if (t->mode == m->mode && t->id.proto == m->proto &&
2402 (m->reqid == 0 || t->reqid == m->reqid)) {
2403 switch (t->mode) {
2404 case XFRM_MODE_TUNNEL:
2405 case XFRM_MODE_BEET:
2406 if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
2407 m->old_family) == 0 &&
2408 xfrm_addr_cmp(&t->saddr, &m->old_saddr,
2409 m->old_family) == 0) {
2410 match = 1;
2411 }
2412 break;
2413 case XFRM_MODE_TRANSPORT:
2414 /* in case of transport mode, template does not store
2415 any IP addresses, hence we just compare mode and
2416 protocol */
2417 match = 1;
2418 break;
2419 default:
2420 break;
2421 }
2422 }
2423 return match;
2424}
2425
2426/* update endpoint address(es) of template(s) */
2427static int xfrm_policy_migrate(struct xfrm_policy *pol,
2428 struct xfrm_migrate *m, int num_migrate)
2429{
2430 struct xfrm_migrate *mp;
2431 struct dst_entry *dst;
2432 int i, j, n = 0;
2433
2434 write_lock_bh(&pol->lock);
2435 if (unlikely(pol->dead)) {
2436 /* target policy has been deleted */
2437 write_unlock_bh(&pol->lock);
2438 return -ENOENT;
2439 }
2440
2441 for (i = 0; i < pol->xfrm_nr; i++) {
2442 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
2443 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
2444 continue;
2445 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07002446 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
2447 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002448 continue;
2449 /* update endpoints */
2450 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
2451 sizeof(pol->xfrm_vec[i].id.daddr));
2452 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
2453 sizeof(pol->xfrm_vec[i].saddr));
2454 pol->xfrm_vec[i].encap_family = mp->new_family;
2455 /* flush bundles */
2456 while ((dst = pol->bundles) != NULL) {
2457 pol->bundles = dst->next;
2458 dst_free(dst);
2459 }
2460 }
2461 }
2462
2463 write_unlock_bh(&pol->lock);
2464
2465 if (!n)
2466 return -ENODATA;
2467
2468 return 0;
2469}
2470
2471static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
2472{
2473 int i, j;
2474
2475 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
2476 return -EINVAL;
2477
2478 for (i = 0; i < num_migrate; i++) {
2479 if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
2480 m[i].old_family) == 0) &&
2481 (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
2482 m[i].old_family) == 0))
2483 return -EINVAL;
2484 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
2485 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
2486 return -EINVAL;
2487
2488 /* check if there is any duplicated entry */
2489 for (j = i + 1; j < num_migrate; j++) {
2490 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
2491 sizeof(m[i].old_daddr)) &&
2492 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
2493 sizeof(m[i].old_saddr)) &&
2494 m[i].proto == m[j].proto &&
2495 m[i].mode == m[j].mode &&
2496 m[i].reqid == m[j].reqid &&
2497 m[i].old_family == m[j].old_family)
2498 return -EINVAL;
2499 }
2500 }
2501
2502 return 0;
2503}
2504
2505int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
2506 struct xfrm_migrate *m, int num_migrate)
2507{
2508 int i, err, nx_cur = 0, nx_new = 0;
2509 struct xfrm_policy *pol = NULL;
2510 struct xfrm_state *x, *xc;
2511 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
2512 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
2513 struct xfrm_migrate *mp;
2514
2515 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
2516 goto out;
2517
2518 /* Stage 1 - find policy */
2519 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
2520 err = -ENOENT;
2521 goto out;
2522 }
2523
2524 /* Stage 2 - find and update state(s) */
2525 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
2526 if ((x = xfrm_migrate_state_find(mp))) {
2527 x_cur[nx_cur] = x;
2528 nx_cur++;
2529 if ((xc = xfrm_state_migrate(x, mp))) {
2530 x_new[nx_new] = xc;
2531 nx_new++;
2532 } else {
2533 err = -ENODATA;
2534 goto restore_state;
2535 }
2536 }
2537 }
2538
2539 /* Stage 3 - update policy */
2540 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
2541 goto restore_state;
2542
2543 /* Stage 4 - delete old state(s) */
2544 if (nx_cur) {
2545 xfrm_states_put(x_cur, nx_cur);
2546 xfrm_states_delete(x_cur, nx_cur);
2547 }
2548
2549 /* Stage 5 - announce */
2550 km_migrate(sel, dir, type, m, num_migrate);
2551
2552 xfrm_pol_put(pol);
2553
2554 return 0;
2555out:
2556 return err;
2557
2558restore_state:
2559 if (pol)
2560 xfrm_pol_put(pol);
2561 if (nx_cur)
2562 xfrm_states_put(x_cur, nx_cur);
2563 if (nx_new)
2564 xfrm_states_delete(x_new, nx_new);
2565
2566 return err;
2567}
David S. Millere610e672007-02-08 13:29:15 -08002568EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002569#endif