blob: a24f385107197809e954d6dc1ea9eb41f53f2161 [file] [log] [blame]
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * xfrm_policy.c
3 *
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * Kazunori MIYAZAWA @USAGI
10 * YOSHIFUJI Hideaki
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
Trent Jaegerdf718372005-12-13 23:12:27 -080013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/slab.h>
17#include <linux/kmod.h>
18#include <linux/list.h>
19#include <linux/spinlock.h>
20#include <linux/workqueue.h>
21#include <linux/notifier.h>
22#include <linux/netdevice.h>
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -080023#include <linux/netfilter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
David S. Miller2518c7c2006-08-24 04:45:07 -070025#include <linux/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/xfrm.h>
27#include <net/ip.h>
Joy Latten161a09e2006-11-27 13:11:54 -060028#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
David S. Miller44e36b42006-08-24 04:50:50 -070030#include "xfrm_hash.h"
31
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080032DEFINE_MUTEX(xfrm_cfg_mutex);
33EXPORT_SYMBOL(xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35static DEFINE_RWLOCK(xfrm_policy_lock);
36
David S. Miller2518c7c2006-08-24 04:45:07 -070037unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
38EXPORT_SYMBOL(xfrm_policy_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
41static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
42
Christoph Lametere18b8902006-12-06 20:33:20 -080043static struct kmem_cache *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45static struct work_struct xfrm_policy_gc_work;
David S. Miller2518c7c2006-08-24 04:45:07 -070046static HLIST_HEAD(xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
48
49static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
50static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
Herbert Xu546be242006-05-27 23:03:58 -070051static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family);
52static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Andrew Morton77681022006-11-08 22:46:26 -080054static inline int
55__xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
56{
57 return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
58 addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
59 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
60 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
61 (fl->proto == sel->proto || !sel->proto) &&
62 (fl->oif == sel->ifindex || !sel->ifindex);
63}
64
65static inline int
66__xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
67{
68 return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
69 addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
70 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
71 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
72 (fl->proto == sel->proto || !sel->proto) &&
73 (fl->oif == sel->ifindex || !sel->ifindex);
74}
75
76int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
77 unsigned short family)
78{
79 switch (family) {
80 case AF_INET:
81 return __xfrm4_selector_match(sel, fl);
82 case AF_INET6:
83 return __xfrm6_selector_match(sel, fl);
84 }
85 return 0;
86}
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088int xfrm_register_type(struct xfrm_type *type, unsigned short family)
89{
Herbert Xu546be242006-05-27 23:03:58 -070090 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
91 struct xfrm_type **typemap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 int err = 0;
93
94 if (unlikely(afinfo == NULL))
95 return -EAFNOSUPPORT;
96 typemap = afinfo->type_map;
97
Herbert Xu546be242006-05-27 23:03:58 -070098 if (likely(typemap[type->proto] == NULL))
99 typemap[type->proto] = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 else
101 err = -EEXIST;
Herbert Xu546be242006-05-27 23:03:58 -0700102 xfrm_policy_unlock_afinfo(afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return err;
104}
105EXPORT_SYMBOL(xfrm_register_type);
106
107int xfrm_unregister_type(struct xfrm_type *type, unsigned short family)
108{
Herbert Xu546be242006-05-27 23:03:58 -0700109 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
110 struct xfrm_type **typemap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 int err = 0;
112
113 if (unlikely(afinfo == NULL))
114 return -EAFNOSUPPORT;
115 typemap = afinfo->type_map;
116
Herbert Xu546be242006-05-27 23:03:58 -0700117 if (unlikely(typemap[type->proto] != type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 err = -ENOENT;
119 else
Herbert Xu546be242006-05-27 23:03:58 -0700120 typemap[type->proto] = NULL;
121 xfrm_policy_unlock_afinfo(afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return err;
123}
124EXPORT_SYMBOL(xfrm_unregister_type);
125
126struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
127{
128 struct xfrm_policy_afinfo *afinfo;
Herbert Xu546be242006-05-27 23:03:58 -0700129 struct xfrm_type **typemap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 struct xfrm_type *type;
131 int modload_attempted = 0;
132
133retry:
134 afinfo = xfrm_policy_get_afinfo(family);
135 if (unlikely(afinfo == NULL))
136 return NULL;
137 typemap = afinfo->type_map;
138
Herbert Xu546be242006-05-27 23:03:58 -0700139 type = typemap[proto];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (unlikely(type && !try_module_get(type->owner)))
141 type = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (!type && !modload_attempted) {
143 xfrm_policy_put_afinfo(afinfo);
144 request_module("xfrm-type-%d-%d",
145 (int) family, (int) proto);
146 modload_attempted = 1;
147 goto retry;
148 }
149
150 xfrm_policy_put_afinfo(afinfo);
151 return type;
152}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900154int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 unsigned short family)
156{
157 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
158 int err = 0;
159
160 if (unlikely(afinfo == NULL))
161 return -EAFNOSUPPORT;
162
163 if (likely(afinfo->dst_lookup != NULL))
164 err = afinfo->dst_lookup(dst, fl);
165 else
166 err = -EINVAL;
167 xfrm_policy_put_afinfo(afinfo);
168 return err;
169}
170EXPORT_SYMBOL(xfrm_dst_lookup);
171
172void xfrm_put_type(struct xfrm_type *type)
173{
174 module_put(type->owner);
175}
176
Herbert Xub59f45d2006-05-27 23:05:54 -0700177int xfrm_register_mode(struct xfrm_mode *mode, int family)
178{
179 struct xfrm_policy_afinfo *afinfo;
180 struct xfrm_mode **modemap;
181 int err;
182
183 if (unlikely(mode->encap >= XFRM_MODE_MAX))
184 return -EINVAL;
185
186 afinfo = xfrm_policy_lock_afinfo(family);
187 if (unlikely(afinfo == NULL))
188 return -EAFNOSUPPORT;
189
190 err = -EEXIST;
191 modemap = afinfo->mode_map;
192 if (likely(modemap[mode->encap] == NULL)) {
193 modemap[mode->encap] = mode;
194 err = 0;
195 }
196
197 xfrm_policy_unlock_afinfo(afinfo);
198 return err;
199}
200EXPORT_SYMBOL(xfrm_register_mode);
201
202int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
203{
204 struct xfrm_policy_afinfo *afinfo;
205 struct xfrm_mode **modemap;
206 int err;
207
208 if (unlikely(mode->encap >= XFRM_MODE_MAX))
209 return -EINVAL;
210
211 afinfo = xfrm_policy_lock_afinfo(family);
212 if (unlikely(afinfo == NULL))
213 return -EAFNOSUPPORT;
214
215 err = -ENOENT;
216 modemap = afinfo->mode_map;
217 if (likely(modemap[mode->encap] == mode)) {
218 modemap[mode->encap] = NULL;
219 err = 0;
220 }
221
222 xfrm_policy_unlock_afinfo(afinfo);
223 return err;
224}
225EXPORT_SYMBOL(xfrm_unregister_mode);
226
227struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
228{
229 struct xfrm_policy_afinfo *afinfo;
230 struct xfrm_mode *mode;
231 int modload_attempted = 0;
232
233 if (unlikely(encap >= XFRM_MODE_MAX))
234 return NULL;
235
236retry:
237 afinfo = xfrm_policy_get_afinfo(family);
238 if (unlikely(afinfo == NULL))
239 return NULL;
240
241 mode = afinfo->mode_map[encap];
242 if (unlikely(mode && !try_module_get(mode->owner)))
243 mode = NULL;
244 if (!mode && !modload_attempted) {
245 xfrm_policy_put_afinfo(afinfo);
246 request_module("xfrm-mode-%d-%d", family, encap);
247 modload_attempted = 1;
248 goto retry;
249 }
250
251 xfrm_policy_put_afinfo(afinfo);
252 return mode;
253}
254
255void xfrm_put_mode(struct xfrm_mode *mode)
256{
257 module_put(mode->owner);
258}
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static inline unsigned long make_jiffies(long secs)
261{
262 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
263 return MAX_SCHEDULE_TIMEOUT-1;
264 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900265 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268static void xfrm_policy_timer(unsigned long data)
269{
270 struct xfrm_policy *xp = (struct xfrm_policy*)data;
271 unsigned long now = (unsigned long)xtime.tv_sec;
272 long next = LONG_MAX;
273 int warn = 0;
274 int dir;
275
276 read_lock(&xp->lock);
277
278 if (xp->dead)
279 goto out;
280
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700281 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 if (xp->lft.hard_add_expires_seconds) {
284 long tmo = xp->lft.hard_add_expires_seconds +
285 xp->curlft.add_time - now;
286 if (tmo <= 0)
287 goto expired;
288 if (tmo < next)
289 next = tmo;
290 }
291 if (xp->lft.hard_use_expires_seconds) {
292 long tmo = xp->lft.hard_use_expires_seconds +
293 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
294 if (tmo <= 0)
295 goto expired;
296 if (tmo < next)
297 next = tmo;
298 }
299 if (xp->lft.soft_add_expires_seconds) {
300 long tmo = xp->lft.soft_add_expires_seconds +
301 xp->curlft.add_time - now;
302 if (tmo <= 0) {
303 warn = 1;
304 tmo = XFRM_KM_TIMEOUT;
305 }
306 if (tmo < next)
307 next = tmo;
308 }
309 if (xp->lft.soft_use_expires_seconds) {
310 long tmo = xp->lft.soft_use_expires_seconds +
311 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
312 if (tmo <= 0) {
313 warn = 1;
314 tmo = XFRM_KM_TIMEOUT;
315 }
316 if (tmo < next)
317 next = tmo;
318 }
319
320 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800321 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (next != LONG_MAX &&
323 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
324 xfrm_pol_hold(xp);
325
326out:
327 read_unlock(&xp->lock);
328 xfrm_pol_put(xp);
329 return;
330
331expired:
332 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700333 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800334 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 xfrm_pol_put(xp);
336}
337
338
339/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
340 * SPD calls.
341 */
342
Al Virodd0fc662005-10-07 07:46:04 +0100343struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 struct xfrm_policy *policy;
346
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700347 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 if (policy) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700350 INIT_HLIST_NODE(&policy->bydst);
351 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700353 atomic_set(&policy->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 init_timer(&policy->timer);
355 policy->timer.data = (unsigned long)policy;
356 policy->timer.function = xfrm_policy_timer;
357 }
358 return policy;
359}
360EXPORT_SYMBOL(xfrm_policy_alloc);
361
362/* Destroy xfrm_policy: descendant resources must be released to this moment. */
363
364void __xfrm_policy_destroy(struct xfrm_policy *policy)
365{
Kris Katterjohn09a62662006-01-08 22:24:28 -0800366 BUG_ON(!policy->dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Kris Katterjohn09a62662006-01-08 22:24:28 -0800368 BUG_ON(policy->bundles);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 if (del_timer(&policy->timer))
371 BUG();
372
Trent Jaegerdf718372005-12-13 23:12:27 -0800373 security_xfrm_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 kfree(policy);
375}
376EXPORT_SYMBOL(__xfrm_policy_destroy);
377
378static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
379{
380 struct dst_entry *dst;
381
382 while ((dst = policy->bundles) != NULL) {
383 policy->bundles = dst->next;
384 dst_free(dst);
385 }
386
387 if (del_timer(&policy->timer))
388 atomic_dec(&policy->refcnt);
389
390 if (atomic_read(&policy->refcnt) > 1)
391 flow_cache_flush();
392
393 xfrm_pol_put(policy);
394}
395
David Howellsc4028952006-11-22 14:57:56 +0000396static void xfrm_policy_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 struct xfrm_policy *policy;
David S. Miller2518c7c2006-08-24 04:45:07 -0700399 struct hlist_node *entry, *tmp;
400 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 spin_lock_bh(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700403 gc_list.first = xfrm_policy_gc_list.first;
404 INIT_HLIST_HEAD(&xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 spin_unlock_bh(&xfrm_policy_gc_lock);
406
David S. Miller2518c7c2006-08-24 04:45:07 -0700407 hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 xfrm_policy_gc_kill(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
411/* Rule must be locked. Release descentant resources, announce
412 * entry dead. The rule must be unlinked from lists to the moment.
413 */
414
415static void xfrm_policy_kill(struct xfrm_policy *policy)
416{
417 int dead;
418
419 write_lock_bh(&policy->lock);
420 dead = policy->dead;
421 policy->dead = 1;
422 write_unlock_bh(&policy->lock);
423
424 if (unlikely(dead)) {
425 WARN_ON(1);
426 return;
427 }
428
429 spin_lock(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700430 hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 spin_unlock(&xfrm_policy_gc_lock);
432
433 schedule_work(&xfrm_policy_gc_work);
434}
435
David S. Miller2518c7c2006-08-24 04:45:07 -0700436struct xfrm_policy_hash {
437 struct hlist_head *table;
438 unsigned int hmask;
439};
440
441static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
442static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
443static struct hlist_head *xfrm_policy_byidx __read_mostly;
444static unsigned int xfrm_idx_hmask __read_mostly;
445static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
446
David S. Miller2518c7c2006-08-24 04:45:07 -0700447static inline unsigned int idx_hash(u32 index)
448{
449 return __idx_hash(index, xfrm_idx_hmask);
450}
451
David S. Miller2518c7c2006-08-24 04:45:07 -0700452static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
453{
454 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
455 unsigned int hash = __sel_hash(sel, family, hmask);
456
457 return (hash == hmask + 1 ?
458 &xfrm_policy_inexact[dir] :
459 xfrm_policy_bydst[dir].table + hash);
460}
461
462static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
463{
464 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
465 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
466
467 return xfrm_policy_bydst[dir].table + hash;
468}
469
David S. Miller2518c7c2006-08-24 04:45:07 -0700470static void xfrm_dst_hash_transfer(struct hlist_head *list,
471 struct hlist_head *ndsttable,
472 unsigned int nhashmask)
473{
474 struct hlist_node *entry, *tmp;
475 struct xfrm_policy *pol;
476
477 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
478 unsigned int h;
479
480 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
481 pol->family, nhashmask);
482 hlist_add_head(&pol->bydst, ndsttable+h);
483 }
484}
485
486static void xfrm_idx_hash_transfer(struct hlist_head *list,
487 struct hlist_head *nidxtable,
488 unsigned int nhashmask)
489{
490 struct hlist_node *entry, *tmp;
491 struct xfrm_policy *pol;
492
493 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
494 unsigned int h;
495
496 h = __idx_hash(pol->index, nhashmask);
497 hlist_add_head(&pol->byidx, nidxtable+h);
498 }
499}
500
501static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
502{
503 return ((old_hmask + 1) << 1) - 1;
504}
505
506static void xfrm_bydst_resize(int dir)
507{
508 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
509 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
510 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
511 struct hlist_head *odst = xfrm_policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700512 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700513 int i;
514
515 if (!ndst)
516 return;
517
518 write_lock_bh(&xfrm_policy_lock);
519
520 for (i = hmask; i >= 0; i--)
521 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
522
523 xfrm_policy_bydst[dir].table = ndst;
524 xfrm_policy_bydst[dir].hmask = nhashmask;
525
526 write_unlock_bh(&xfrm_policy_lock);
527
David S. Miller44e36b42006-08-24 04:50:50 -0700528 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700529}
530
531static void xfrm_byidx_resize(int total)
532{
533 unsigned int hmask = xfrm_idx_hmask;
534 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
535 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
536 struct hlist_head *oidx = xfrm_policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700537 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700538 int i;
539
540 if (!nidx)
541 return;
542
543 write_lock_bh(&xfrm_policy_lock);
544
545 for (i = hmask; i >= 0; i--)
546 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
547
548 xfrm_policy_byidx = nidx;
549 xfrm_idx_hmask = nhashmask;
550
551 write_unlock_bh(&xfrm_policy_lock);
552
David S. Miller44e36b42006-08-24 04:50:50 -0700553 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700554}
555
556static inline int xfrm_bydst_should_resize(int dir, int *total)
557{
558 unsigned int cnt = xfrm_policy_count[dir];
559 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
560
561 if (total)
562 *total += cnt;
563
564 if ((hmask + 1) < xfrm_policy_hashmax &&
565 cnt > hmask)
566 return 1;
567
568 return 0;
569}
570
571static inline int xfrm_byidx_should_resize(int total)
572{
573 unsigned int hmask = xfrm_idx_hmask;
574
575 if ((hmask + 1) < xfrm_policy_hashmax &&
576 total > hmask)
577 return 1;
578
579 return 0;
580}
581
582static DEFINE_MUTEX(hash_resize_mutex);
583
David Howellsc4028952006-11-22 14:57:56 +0000584static void xfrm_hash_resize(struct work_struct *__unused)
David S. Miller2518c7c2006-08-24 04:45:07 -0700585{
586 int dir, total;
587
588 mutex_lock(&hash_resize_mutex);
589
590 total = 0;
591 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
592 if (xfrm_bydst_should_resize(dir, &total))
593 xfrm_bydst_resize(dir);
594 }
595 if (xfrm_byidx_should_resize(total))
596 xfrm_byidx_resize(total);
597
598 mutex_unlock(&hash_resize_mutex);
599}
600
David Howellsc4028952006-11-22 14:57:56 +0000601static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603/* Generate new index... KAME seems to generate them ordered by cost
604 * of an absolute inpredictability of ordering of rules. This will not pass. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700605static u32 xfrm_gen_index(u8 type, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 static u32 idx_generator;
608
609 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700610 struct hlist_node *entry;
611 struct hlist_head *list;
612 struct xfrm_policy *p;
613 u32 idx;
614 int found;
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 idx = (idx_generator | dir);
617 idx_generator += 8;
618 if (idx == 0)
619 idx = 8;
David S. Miller2518c7c2006-08-24 04:45:07 -0700620 list = xfrm_policy_byidx + idx_hash(idx);
621 found = 0;
622 hlist_for_each_entry(p, entry, list, byidx) {
623 if (p->index == idx) {
624 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700628 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return idx;
630 }
631}
632
David S. Miller2518c7c2006-08-24 04:45:07 -0700633static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
634{
635 u32 *p1 = (u32 *) s1;
636 u32 *p2 = (u32 *) s2;
637 int len = sizeof(struct xfrm_selector) / sizeof(u32);
638 int i;
639
640 for (i = 0; i < len; i++) {
641 if (p1[i] != p2[i])
642 return 1;
643 }
644
645 return 0;
646}
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
649{
David S. Miller2518c7c2006-08-24 04:45:07 -0700650 struct xfrm_policy *pol;
651 struct xfrm_policy *delpol;
652 struct hlist_head *chain;
Herbert Xua6c7ab52007-01-16 16:52:02 -0800653 struct hlist_node *entry, *newpos;
David S. Miller9b78a822005-12-22 07:39:48 -0800654 struct dst_entry *gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700657 chain = policy_hash_bysel(&policy->selector, policy->family, dir);
658 delpol = NULL;
659 newpos = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700660 hlist_for_each_entry(pol, entry, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800661 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700662 !selector_cmp(&pol->selector, &policy->selector) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800663 xfrm_sec_ctx_match(pol->security, policy->security) &&
664 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (excl) {
666 write_unlock_bh(&xfrm_policy_lock);
667 return -EEXIST;
668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 delpol = pol;
670 if (policy->priority > pol->priority)
671 continue;
672 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800673 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 continue;
675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (delpol)
677 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700680 hlist_add_after(newpos, &policy->bydst);
681 else
682 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 xfrm_pol_hold(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700684 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700686 if (delpol) {
687 hlist_del(&delpol->bydst);
688 hlist_del(&delpol->byidx);
689 xfrm_policy_count[dir]--;
690 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700691 policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700692 hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 policy->curlft.add_time = (unsigned long)xtime.tv_sec;
694 policy->curlft.use_time = 0;
695 if (!mod_timer(&policy->timer, jiffies + HZ))
696 xfrm_pol_hold(policy);
697 write_unlock_bh(&xfrm_policy_lock);
698
David S. Miller9b78a822005-12-22 07:39:48 -0800699 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 xfrm_policy_kill(delpol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700701 else if (xfrm_bydst_should_resize(dir, NULL))
702 schedule_work(&xfrm_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800703
704 read_lock_bh(&xfrm_policy_lock);
705 gc_list = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700706 entry = &policy->bydst;
707 hlist_for_each_entry_continue(policy, entry, bydst) {
David S. Miller9b78a822005-12-22 07:39:48 -0800708 struct dst_entry *dst;
709
710 write_lock(&policy->lock);
711 dst = policy->bundles;
712 if (dst) {
713 struct dst_entry *tail = dst;
714 while (tail->next)
715 tail = tail->next;
716 tail->next = gc_list;
717 gc_list = dst;
718
719 policy->bundles = NULL;
720 }
721 write_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
David S. Miller9b78a822005-12-22 07:39:48 -0800723 read_unlock_bh(&xfrm_policy_lock);
724
725 while (gc_list) {
726 struct dst_entry *dst = gc_list;
727
728 gc_list = dst->next;
729 dst_free(dst);
730 }
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return 0;
733}
734EXPORT_SYMBOL(xfrm_policy_insert);
735
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700736struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
737 struct xfrm_selector *sel,
Trent Jaegerdf718372005-12-13 23:12:27 -0800738 struct xfrm_sec_ctx *ctx, int delete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
David S. Miller2518c7c2006-08-24 04:45:07 -0700740 struct xfrm_policy *pol, *ret;
741 struct hlist_head *chain;
742 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700745 chain = policy_hash_bysel(sel, sel->family, dir);
746 ret = NULL;
747 hlist_for_each_entry(pol, entry, chain, bydst) {
748 if (pol->type == type &&
749 !selector_cmp(sel, &pol->selector) &&
750 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700752 if (delete) {
753 hlist_del(&pol->bydst);
754 hlist_del(&pol->byidx);
755 xfrm_policy_count[dir]--;
756 }
757 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 break;
759 }
760 }
761 write_unlock_bh(&xfrm_policy_lock);
762
David S. Miller2518c7c2006-08-24 04:45:07 -0700763 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700765 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700767 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
Trent Jaegerdf718372005-12-13 23:12:27 -0800769EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700771struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
David S. Miller2518c7c2006-08-24 04:45:07 -0700773 struct xfrm_policy *pol, *ret;
774 struct hlist_head *chain;
775 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700778 chain = xfrm_policy_byidx + idx_hash(id);
779 ret = NULL;
780 hlist_for_each_entry(pol, entry, chain, byidx) {
781 if (pol->type == type && pol->index == id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700783 if (delete) {
784 hlist_del(&pol->bydst);
785 hlist_del(&pol->byidx);
786 xfrm_policy_count[dir]--;
787 }
788 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 break;
790 }
791 }
792 write_unlock_bh(&xfrm_policy_lock);
793
David S. Miller2518c7c2006-08-24 04:45:07 -0700794 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700796 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700798 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800EXPORT_SYMBOL(xfrm_policy_byid);
801
Joy Latten161a09e2006-11-27 13:11:54 -0600802void xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 int dir;
805
806 write_lock_bh(&xfrm_policy_lock);
807 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700808 struct xfrm_policy *pol;
809 struct hlist_node *entry;
David S. Millerae8c0572006-10-03 16:00:26 -0700810 int i, killed;
David S. Miller2518c7c2006-08-24 04:45:07 -0700811
David S. Millerae8c0572006-10-03 16:00:26 -0700812 killed = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700813 again1:
814 hlist_for_each_entry(pol, entry,
815 &xfrm_policy_inexact[dir], bydst) {
816 if (pol->type != type)
817 continue;
818 hlist_del(&pol->bydst);
819 hlist_del(&pol->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 write_unlock_bh(&xfrm_policy_lock);
821
Joy Latten161a09e2006-11-27 13:11:54 -0600822 xfrm_audit_log(audit_info->loginuid, audit_info->secid,
823 AUDIT_MAC_IPSEC_DELSPD, 1, pol, NULL);
824
David S. Miller2518c7c2006-08-24 04:45:07 -0700825 xfrm_policy_kill(pol);
David S. Millerae8c0572006-10-03 16:00:26 -0700826 killed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700829 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700831
832 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
833 again2:
834 hlist_for_each_entry(pol, entry,
835 xfrm_policy_bydst[dir].table + i,
836 bydst) {
837 if (pol->type != type)
838 continue;
839 hlist_del(&pol->bydst);
840 hlist_del(&pol->byidx);
841 write_unlock_bh(&xfrm_policy_lock);
842
Joy Latten161a09e2006-11-27 13:11:54 -0600843 xfrm_audit_log(audit_info->loginuid,
844 audit_info->secid,
845 AUDIT_MAC_IPSEC_DELSPD, 1,
846 pol, NULL);
847
David S. Miller2518c7c2006-08-24 04:45:07 -0700848 xfrm_policy_kill(pol);
David S. Millerae8c0572006-10-03 16:00:26 -0700849 killed++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700850
851 write_lock_bh(&xfrm_policy_lock);
852 goto again2;
853 }
854 }
855
David S. Millerae8c0572006-10-03 16:00:26 -0700856 xfrm_policy_count[dir] -= killed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858 atomic_inc(&flow_cache_genid);
859 write_unlock_bh(&xfrm_policy_lock);
860}
861EXPORT_SYMBOL(xfrm_policy_flush);
862
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700863int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 void *data)
865{
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800866 struct xfrm_policy *pol, *last = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700867 struct hlist_node *entry;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800868 int dir, last_dir = 0, count, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700871 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700874 struct hlist_head *table = xfrm_policy_bydst[dir].table;
875 int i;
876
877 hlist_for_each_entry(pol, entry,
878 &xfrm_policy_inexact[dir], bydst) {
879 if (pol->type != type)
880 continue;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800881 if (last) {
882 error = func(last, last_dir % XFRM_POLICY_MAX,
883 count, data);
884 if (error)
885 goto out;
886 }
887 last = pol;
888 last_dir = dir;
889 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700891 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
892 hlist_for_each_entry(pol, entry, table + i, bydst) {
893 if (pol->type != type)
894 continue;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800895 if (last) {
896 error = func(last, last_dir % XFRM_POLICY_MAX,
897 count, data);
898 if (error)
899 goto out;
900 }
901 last = pol;
902 last_dir = dir;
903 count++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700904 }
905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800907 if (count == 0) {
908 error = -ENOENT;
909 goto out;
910 }
911 error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912out:
913 read_unlock_bh(&xfrm_policy_lock);
914 return error;
915}
916EXPORT_SYMBOL(xfrm_policy_walk);
917
James Morris134b0fc2006-10-05 15:42:27 -0500918/*
919 * Find policy to apply to this flow.
920 *
921 * Returns 0 if policy found, else an -errno.
922 */
David S. Miller2518c7c2006-08-24 04:45:07 -0700923static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
924 u8 type, u16 family, int dir)
925{
926 struct xfrm_selector *sel = &pol->selector;
James Morris134b0fc2006-10-05 15:42:27 -0500927 int match, ret = -ESRCH;
David S. Miller2518c7c2006-08-24 04:45:07 -0700928
929 if (pol->family != family ||
930 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500931 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700932
933 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500934 if (match)
935 ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700936
James Morris134b0fc2006-10-05 15:42:27 -0500937 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700938}
939
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700940static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
941 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
James Morris134b0fc2006-10-05 15:42:27 -0500943 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700944 struct xfrm_policy *pol, *ret;
945 xfrm_address_t *daddr, *saddr;
946 struct hlist_node *entry;
947 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700948 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700949
950 daddr = xfrm_flowi_daddr(fl, family);
951 saddr = xfrm_flowi_saddr(fl, family);
952 if (unlikely(!daddr || !saddr))
953 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700956 chain = policy_hash_direct(daddr, saddr, family, dir);
957 ret = NULL;
958 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500959 err = xfrm_policy_match(pol, fl, type, family, dir);
960 if (err) {
961 if (err == -ESRCH)
962 continue;
963 else {
964 ret = ERR_PTR(err);
965 goto fail;
966 }
967 } else {
David S. Milleracba48e2006-08-25 15:46:46 -0700968 ret = pol;
969 priority = ret->priority;
970 break;
971 }
972 }
973 chain = &xfrm_policy_inexact[dir];
974 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500975 err = xfrm_policy_match(pol, fl, type, family, dir);
976 if (err) {
977 if (err == -ESRCH)
978 continue;
979 else {
980 ret = ERR_PTR(err);
981 goto fail;
982 }
983 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700984 ret = pol;
985 break;
986 }
987 }
David S. Milleracba48e2006-08-25 15:46:46 -0700988 if (ret)
989 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -0500990fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700992
David S. Miller2518c7c2006-08-24 04:45:07 -0700993 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700994}
995
James Morris134b0fc2006-10-05 15:42:27 -0500996static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700997 void **objp, atomic_t **obj_refp)
998{
999 struct xfrm_policy *pol;
James Morris134b0fc2006-10-05 15:42:27 -05001000 int err = 0;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001001
1002#ifdef CONFIG_XFRM_SUB_POLICY
1003 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
James Morris134b0fc2006-10-05 15:42:27 -05001004 if (IS_ERR(pol)) {
1005 err = PTR_ERR(pol);
1006 pol = NULL;
1007 }
1008 if (pol || err)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001009 goto end;
1010#endif
1011 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
James Morris134b0fc2006-10-05 15:42:27 -05001012 if (IS_ERR(pol)) {
1013 err = PTR_ERR(pol);
1014 pol = NULL;
1015 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001016#ifdef CONFIG_XFRM_SUB_POLICY
David S. Miller2518c7c2006-08-24 04:45:07 -07001017end:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001018#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if ((*objp = (void *) pol) != NULL)
1020 *obj_refp = &pol->refcnt;
James Morris134b0fc2006-10-05 15:42:27 -05001021 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Trent Jaegerdf718372005-12-13 23:12:27 -08001024static inline int policy_to_flow_dir(int dir)
1025{
1026 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001027 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1028 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1029 return dir;
1030 switch (dir) {
1031 default:
1032 case XFRM_POLICY_IN:
1033 return FLOW_DIR_IN;
1034 case XFRM_POLICY_OUT:
1035 return FLOW_DIR_OUT;
1036 case XFRM_POLICY_FWD:
1037 return FLOW_DIR_FWD;
Trent Jaegerdf718372005-12-13 23:12:27 -08001038 };
1039}
1040
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001041static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 struct xfrm_policy *pol;
1044
1045 read_lock_bh(&xfrm_policy_lock);
1046 if ((pol = sk->sk_policy[dir]) != NULL) {
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001047 int match = xfrm_selector_match(&pol->selector, fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001049 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001050
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001051 if (match) {
1052 err = security_xfrm_policy_lookup(pol, fl->secid,
1053 policy_to_flow_dir(dir));
1054 if (!err)
1055 xfrm_pol_hold(pol);
1056 else if (err == -ESRCH)
1057 pol = NULL;
1058 else
1059 pol = ERR_PTR(err);
1060 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 pol = NULL;
1062 }
1063 read_unlock_bh(&xfrm_policy_lock);
1064 return pol;
1065}
1066
1067static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1068{
David S. Miller2518c7c2006-08-24 04:45:07 -07001069 struct hlist_head *chain = policy_hash_bysel(&pol->selector,
1070 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001071
David S. Miller2518c7c2006-08-24 04:45:07 -07001072 hlist_add_head(&pol->bydst, chain);
1073 hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
1074 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001076
1077 if (xfrm_bydst_should_resize(dir, NULL))
1078 schedule_work(&xfrm_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
1081static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1082 int dir)
1083{
David S. Miller2518c7c2006-08-24 04:45:07 -07001084 if (hlist_unhashed(&pol->bydst))
1085 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
David S. Miller2518c7c2006-08-24 04:45:07 -07001087 hlist_del(&pol->bydst);
1088 hlist_del(&pol->byidx);
1089 xfrm_policy_count[dir]--;
1090
1091 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
Herbert Xu4666faa2005-06-18 22:43:22 -07001094int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
1096 write_lock_bh(&xfrm_policy_lock);
1097 pol = __xfrm_policy_unlink(pol, dir);
1098 write_unlock_bh(&xfrm_policy_lock);
1099 if (pol) {
1100 if (dir < XFRM_POLICY_MAX)
1101 atomic_inc(&flow_cache_genid);
1102 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001105 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
David S. Millera70fcb02006-03-20 19:18:52 -08001107EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1110{
1111 struct xfrm_policy *old_pol;
1112
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001113#ifdef CONFIG_XFRM_SUB_POLICY
1114 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1115 return -EINVAL;
1116#endif
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 write_lock_bh(&xfrm_policy_lock);
1119 old_pol = sk->sk_policy[dir];
1120 sk->sk_policy[dir] = pol;
1121 if (pol) {
1122 pol->curlft.add_time = (unsigned long)xtime.tv_sec;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001123 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1125 }
1126 if (old_pol)
1127 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1128 write_unlock_bh(&xfrm_policy_lock);
1129
1130 if (old_pol) {
1131 xfrm_policy_kill(old_pol);
1132 }
1133 return 0;
1134}
1135
1136static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1137{
1138 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
1139
1140 if (newp) {
1141 newp->selector = old->selector;
Trent Jaegerdf718372005-12-13 23:12:27 -08001142 if (security_xfrm_policy_clone(old, newp)) {
1143 kfree(newp);
1144 return NULL; /* ENOMEM */
1145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 newp->lft = old->lft;
1147 newp->curlft = old->curlft;
1148 newp->action = old->action;
1149 newp->flags = old->flags;
1150 newp->xfrm_nr = old->xfrm_nr;
1151 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001152 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 memcpy(newp->xfrm_vec, old->xfrm_vec,
1154 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1155 write_lock_bh(&xfrm_policy_lock);
1156 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1157 write_unlock_bh(&xfrm_policy_lock);
1158 xfrm_pol_put(newp);
1159 }
1160 return newp;
1161}
1162
1163int __xfrm_sk_clone_policy(struct sock *sk)
1164{
1165 struct xfrm_policy *p0 = sk->sk_policy[0],
1166 *p1 = sk->sk_policy[1];
1167
1168 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1169 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1170 return -ENOMEM;
1171 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1172 return -ENOMEM;
1173 return 0;
1174}
1175
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001176static int
1177xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
1178 unsigned short family)
1179{
1180 int err;
1181 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1182
1183 if (unlikely(afinfo == NULL))
1184 return -EINVAL;
1185 err = afinfo->get_saddr(local, remote);
1186 xfrm_policy_put_afinfo(afinfo);
1187 return err;
1188}
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190/* Resolve list of templates for the flow, given policy. */
1191
1192static int
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001193xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1194 struct xfrm_state **xfrm,
1195 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 int nx;
1198 int i, error;
1199 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1200 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001201 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1204 struct xfrm_state *x;
1205 xfrm_address_t *remote = daddr;
1206 xfrm_address_t *local = saddr;
1207 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1208
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001209 if (tmpl->mode == XFRM_MODE_TUNNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 remote = &tmpl->id.daddr;
1211 local = &tmpl->saddr;
Miika Komu76b3f052006-11-30 16:40:43 -08001212 family = tmpl->encap_family;
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001213 if (xfrm_addr_any(local, family)) {
1214 error = xfrm_get_saddr(&tmp, remote, family);
1215 if (error)
1216 goto fail;
1217 local = &tmp;
1218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220
1221 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1222
1223 if (x && x->km.state == XFRM_STATE_VALID) {
1224 xfrm[nx++] = x;
1225 daddr = remote;
1226 saddr = local;
1227 continue;
1228 }
1229 if (x) {
1230 error = (x->km.state == XFRM_STATE_ERROR ?
1231 -EINVAL : -EAGAIN);
1232 xfrm_state_put(x);
1233 }
1234
1235 if (!tmpl->optional)
1236 goto fail;
1237 }
1238 return nx;
1239
1240fail:
1241 for (nx--; nx>=0; nx--)
1242 xfrm_state_put(xfrm[nx]);
1243 return error;
1244}
1245
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001246static int
1247xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1248 struct xfrm_state **xfrm,
1249 unsigned short family)
1250{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001251 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1252 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001253 int cnx = 0;
1254 int error;
1255 int ret;
1256 int i;
1257
1258 for (i = 0; i < npols; i++) {
1259 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1260 error = -ENOBUFS;
1261 goto fail;
1262 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001263
1264 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001265 if (ret < 0) {
1266 error = ret;
1267 goto fail;
1268 } else
1269 cnx += ret;
1270 }
1271
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001272 /* found states are sorted for outbound processing */
1273 if (npols > 1)
1274 xfrm_state_sort(xfrm, tpp, cnx, family);
1275
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001276 return cnx;
1277
1278 fail:
1279 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001280 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001281 return error;
1282
1283}
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285/* Check that the bundle accepts the flow and its components are
1286 * still valid.
1287 */
1288
1289static struct dst_entry *
1290xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1291{
1292 struct dst_entry *x;
1293 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1294 if (unlikely(afinfo == NULL))
1295 return ERR_PTR(-EINVAL);
1296 x = afinfo->find_bundle(fl, policy);
1297 xfrm_policy_put_afinfo(afinfo);
1298 return x;
1299}
1300
1301/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1302 * all the metrics... Shortly, bundle a bundle.
1303 */
1304
1305static int
1306xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
1307 struct flowi *fl, struct dst_entry **dst_p,
1308 unsigned short family)
1309{
1310 int err;
1311 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1312 if (unlikely(afinfo == NULL))
1313 return -EINVAL;
1314 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
1315 xfrm_policy_put_afinfo(afinfo);
1316 return err;
1317}
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320static int stale_bundle(struct dst_entry *dst);
1321
1322/* Main function: finds/creates a bundle for given flow.
1323 *
1324 * At the moment we eat a raw IP route. Mostly to speed up lookups
1325 * on interfaces with disabled IPsec.
1326 */
1327int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1328 struct sock *sk, int flags)
1329{
1330 struct xfrm_policy *policy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001331 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1332 int npols;
1333 int pol_dead;
1334 int xfrm_nr;
1335 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1337 struct dst_entry *dst, *dst_orig = *dst_p;
1338 int nx = 0;
1339 int err;
1340 u32 genid;
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001341 u16 family;
Trent Jaegerdf718372005-12-13 23:12:27 -08001342 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344restart:
1345 genid = atomic_read(&flow_cache_genid);
1346 policy = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001347 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1348 pols[pi] = NULL;
1349 npols = 0;
1350 pol_dead = 0;
1351 xfrm_nr = 0;
1352
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001353 if (sk && sk->sk_policy[1]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001354 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001355 if (IS_ERR(policy))
1356 return PTR_ERR(policy);
1357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 if (!policy) {
1360 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07001361 if ((dst_orig->flags & DST_NOXFRM) ||
1362 !xfrm_policy_count[XFRM_POLICY_OUT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return 0;
1364
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001365 policy = flow_cache_lookup(fl, dst_orig->ops->family,
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001366 dir, xfrm_policy_lookup);
James Morris134b0fc2006-10-05 15:42:27 -05001367 if (IS_ERR(policy))
1368 return PTR_ERR(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370
1371 if (!policy)
1372 return 0;
1373
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001374 family = dst_orig->ops->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 policy->curlft.use_time = (unsigned long)xtime.tv_sec;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001376 pols[0] = policy;
1377 npols ++;
1378 xfrm_nr += pols[0]->xfrm_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 switch (policy->action) {
1381 case XFRM_POLICY_BLOCK:
1382 /* Prohibit the flow */
Patrick McHardye1044112005-09-08 15:11:55 -07001383 err = -EPERM;
1384 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 case XFRM_POLICY_ALLOW:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001387#ifndef CONFIG_XFRM_SUB_POLICY
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (policy->xfrm_nr == 0) {
1389 /* Flow passes not transformed. */
1390 xfrm_pol_put(policy);
1391 return 0;
1392 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001393#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 /* Try to find matching bundle.
1396 *
1397 * LATER: help from flow cache. It is optional, this
1398 * is required only for output policy.
1399 */
1400 dst = xfrm_find_bundle(fl, policy, family);
1401 if (IS_ERR(dst)) {
Patrick McHardye1044112005-09-08 15:11:55 -07001402 err = PTR_ERR(dst);
1403 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405
1406 if (dst)
1407 break;
1408
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001409#ifdef CONFIG_XFRM_SUB_POLICY
1410 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1411 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1412 fl, family,
1413 XFRM_POLICY_OUT);
1414 if (pols[1]) {
James Morris134b0fc2006-10-05 15:42:27 -05001415 if (IS_ERR(pols[1])) {
1416 err = PTR_ERR(pols[1]);
1417 goto error;
1418 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001419 if (pols[1]->action == XFRM_POLICY_BLOCK) {
1420 err = -EPERM;
1421 goto error;
1422 }
1423 npols ++;
1424 xfrm_nr += pols[1]->xfrm_nr;
1425 }
1426 }
1427
1428 /*
1429 * Because neither flowi nor bundle information knows about
1430 * transformation template size. On more than one policy usage
1431 * we can realize whether all of them is bypass or not after
1432 * they are searched. See above not-transformed bypass
1433 * is surrounded by non-sub policy configuration, too.
1434 */
1435 if (xfrm_nr == 0) {
1436 /* Flow passes not transformed. */
1437 xfrm_pols_put(pols, npols);
1438 return 0;
1439 }
1440
1441#endif
1442 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 if (unlikely(nx<0)) {
1445 err = nx;
1446 if (err == -EAGAIN && flags) {
1447 DECLARE_WAITQUEUE(wait, current);
1448
1449 add_wait_queue(&km_waitq, &wait);
1450 set_current_state(TASK_INTERRUPTIBLE);
1451 schedule();
1452 set_current_state(TASK_RUNNING);
1453 remove_wait_queue(&km_waitq, &wait);
1454
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001455 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 if (nx == -EAGAIN && signal_pending(current)) {
1458 err = -ERESTART;
1459 goto error;
1460 }
1461 if (nx == -EAGAIN ||
1462 genid != atomic_read(&flow_cache_genid)) {
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001463 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 goto restart;
1465 }
1466 err = nx;
1467 }
1468 if (err < 0)
1469 goto error;
1470 }
1471 if (nx == 0) {
1472 /* Flow passes not transformed. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001473 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return 0;
1475 }
1476
1477 dst = dst_orig;
1478 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
1479
1480 if (unlikely(err)) {
1481 int i;
1482 for (i=0; i<nx; i++)
1483 xfrm_state_put(xfrm[i]);
1484 goto error;
1485 }
1486
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001487 for (pi = 0; pi < npols; pi++) {
1488 read_lock_bh(&pols[pi]->lock);
1489 pol_dead |= pols[pi]->dead;
1490 read_unlock_bh(&pols[pi]->lock);
1491 }
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 write_lock_bh(&policy->lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001494 if (unlikely(pol_dead || stale_bundle(dst))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 /* Wow! While we worked on resolving, this
1496 * policy has gone. Retry. It is not paranoia,
1497 * we just cannot enlist new bundle to dead object.
1498 * We can't enlist stable bundles either.
1499 */
1500 write_unlock_bh(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (dst)
1502 dst_free(dst);
Herbert Xu00de6512006-02-13 16:01:27 -08001503
1504 err = -EHOSTUNREACH;
1505 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
1507 dst->next = policy->bundles;
1508 policy->bundles = dst;
1509 dst_hold(dst);
1510 write_unlock_bh(&policy->lock);
1511 }
1512 *dst_p = dst;
1513 dst_release(dst_orig);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001514 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return 0;
1516
1517error:
1518 dst_release(dst_orig);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001519 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 *dst_p = NULL;
1521 return err;
1522}
1523EXPORT_SYMBOL(xfrm_lookup);
1524
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001525static inline int
1526xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1527{
1528 struct xfrm_state *x;
1529 int err;
1530
1531 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1532 return 0;
1533 x = skb->sp->xvec[idx];
1534 if (!x->type->reject)
1535 return 0;
1536 xfrm_state_hold(x);
1537 err = x->type->reject(x, skb, fl);
1538 xfrm_state_put(x);
1539 return err;
1540}
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542/* When skb is transformed back to its "native" form, we have to
1543 * check policy restrictions. At the moment we make this in maximally
1544 * stupid way. Shame on me. :-) Of course, connected sockets must
1545 * have policy cached at them.
1546 */
1547
1548static inline int
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001549xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 unsigned short family)
1551{
1552 if (xfrm_state_kern(x))
1553 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family);
1554 return x->id.proto == tmpl->id.proto &&
1555 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1556 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1557 x->props.mode == tmpl->mode &&
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07001558 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1559 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001560 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1561 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001564/*
1565 * 0 or more than 0 is returned when validation is succeeded (either bypass
1566 * because of optional transport mode, or next index of the mathced secpath
1567 * state with the template.
1568 * -1 is returned when no matching template is found.
1569 * Otherwise "-2 - errored_index" is returned.
1570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571static inline int
1572xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1573 unsigned short family)
1574{
1575 int idx = start;
1576
1577 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001578 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 return start;
1580 } else
1581 start = -1;
1582 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001583 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001585 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1586 if (start == -1)
1587 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
1591 return start;
1592}
1593
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001594int
1595xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
1597 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001598 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 if (unlikely(afinfo == NULL))
1601 return -EAFNOSUPPORT;
1602
1603 afinfo->decode_session(skb, fl);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07001604 err = security_xfrm_decode_session(skb, &fl->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001606 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001608EXPORT_SYMBOL(xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001610static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
1612 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001613 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001614 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
1618
1619 return 0;
1620}
1621
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001622int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 unsigned short family)
1624{
1625 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001626 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1627 int npols = 0;
1628 int xfrm_nr;
1629 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 struct flowi fl;
Trent Jaegerdf718372005-12-13 23:12:27 -08001631 u8 fl_dir = policy_to_flow_dir(dir);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001632 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001634 if (xfrm_decode_session(skb, &fl, family) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return 0;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08001636 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 /* First, check used SA against their selectors. */
1639 if (skb->sp) {
1640 int i;
1641
1642 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001643 struct xfrm_state *x = skb->sp->xvec[i];
1644 if (!xfrm_selector_match(&x->sel, &fl, family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
1647 }
1648
1649 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001650 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001651 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001652 if (IS_ERR(pol))
1653 return 0;
1654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 if (!pol)
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001657 pol = flow_cache_lookup(&fl, family, fl_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 xfrm_policy_lookup);
1659
James Morris134b0fc2006-10-05 15:42:27 -05001660 if (IS_ERR(pol))
1661 return 0;
1662
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001663 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001664 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001665 xfrm_secpath_reject(xerr_idx, skb, &fl);
1666 return 0;
1667 }
1668 return 1;
1669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 pol->curlft.use_time = (unsigned long)xtime.tv_sec;
1672
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001673 pols[0] = pol;
1674 npols ++;
1675#ifdef CONFIG_XFRM_SUB_POLICY
1676 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1677 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1678 &fl, family,
1679 XFRM_POLICY_IN);
1680 if (pols[1]) {
James Morris134b0fc2006-10-05 15:42:27 -05001681 if (IS_ERR(pols[1]))
1682 return 0;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001683 pols[1]->curlft.use_time = (unsigned long)xtime.tv_sec;
1684 npols ++;
1685 }
1686 }
1687#endif
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (pol->action == XFRM_POLICY_ALLOW) {
1690 struct sec_path *sp;
1691 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001692 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001693 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001694 struct xfrm_tmpl **tpp = tp;
1695 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 int i, k;
1697
1698 if ((sp = skb->sp) == NULL)
1699 sp = &dummy;
1700
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001701 for (pi = 0; pi < npols; pi++) {
1702 if (pols[pi] != pol &&
1703 pols[pi]->action != XFRM_POLICY_ALLOW)
1704 goto reject;
1705 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
1706 goto reject_error;
1707 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1708 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1709 }
1710 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001711 if (npols > 1) {
1712 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1713 tpp = stp;
1714 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 /* For each tunnel xfrm, find the first matching tmpl.
1717 * For each tmpl before that, find corresponding xfrm.
1718 * Order is _important_. Later we will implement
1719 * some barriers, but at the moment barriers
1720 * are implied between each two transformations.
1721 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001722 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1723 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001724 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001725 if (k < -1)
1726 /* "-2 - errored_index" returned */
1727 xerr_idx = -(2+k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 }
1731
James Morrisd1d9fac2006-09-01 00:32:12 -07001732 if (secpath_has_nontransport(sp, k, &xerr_idx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 goto reject;
1734
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001735 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return 1;
1737 }
1738
1739reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001740 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001741reject_error:
1742 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return 0;
1744}
1745EXPORT_SYMBOL(__xfrm_policy_check);
1746
1747int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1748{
1749 struct flowi fl;
1750
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001751 if (xfrm_decode_session(skb, &fl, family) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return 0;
1753
1754 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1755}
1756EXPORT_SYMBOL(__xfrm_route_forward);
1757
David S. Millerd49c73c2006-08-13 18:55:53 -07001758/* Optimize later using cookies and generation ids. */
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1761{
David S. Millerd49c73c2006-08-13 18:55:53 -07001762 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1763 * to "-1" to force all XFRM destinations to get validated by
1764 * dst_ops->check on every use. We do this because when a
1765 * normal route referenced by an XFRM dst is obsoleted we do
1766 * not go looking around for all parent referencing XFRM dsts
1767 * so that we can invalidate them. It is just too much work.
1768 * Instead we make the checks here on every use. For example:
1769 *
1770 * XFRM dst A --> IPv4 dst X
1771 *
1772 * X is the "xdst->route" of A (X is also the "dst->path" of A
1773 * in this example). If X is marked obsolete, "A" will not
1774 * notice. That's what we are validating here via the
1775 * stale_bundle() check.
1776 *
1777 * When a policy's bundle is pruned, we dst_free() the XFRM
1778 * dst which causes it's ->obsolete field to be set to a
1779 * positive non-zero integer. If an XFRM dst has been pruned
1780 * like this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08001781 */
David S. Millerd49c73c2006-08-13 18:55:53 -07001782 if (dst->obsolete < 0 && !stale_bundle(dst))
1783 return dst;
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return NULL;
1786}
1787
1788static int stale_bundle(struct dst_entry *dst)
1789{
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05001790 return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
1792
Herbert Xuaabc9762005-05-03 16:27:10 -07001793void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
1796 dst->dev = &loopback_dev;
1797 dev_hold(&loopback_dev);
1798 dev_put(dev);
1799 }
1800}
Herbert Xuaabc9762005-05-03 16:27:10 -07001801EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803static void xfrm_link_failure(struct sk_buff *skb)
1804{
1805 /* Impossible. Such dst must be popped before reaches point of failure. */
1806 return;
1807}
1808
1809static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1810{
1811 if (dst) {
1812 if (dst->obsolete) {
1813 dst_release(dst);
1814 dst = NULL;
1815 }
1816 }
1817 return dst;
1818}
1819
David S. Miller2518c7c2006-08-24 04:45:07 -07001820static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
1821{
1822 struct dst_entry *dst, **dstp;
1823
1824 write_lock(&pol->lock);
1825 dstp = &pol->bundles;
1826 while ((dst=*dstp) != NULL) {
1827 if (func(dst)) {
1828 *dstp = dst->next;
1829 dst->next = *gc_list_p;
1830 *gc_list_p = dst;
1831 } else {
1832 dstp = &dst->next;
1833 }
1834 }
1835 write_unlock(&pol->lock);
1836}
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1839{
David S. Miller2518c7c2006-08-24 04:45:07 -07001840 struct dst_entry *gc_list = NULL;
1841 int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
1843 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07001844 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
1845 struct xfrm_policy *pol;
1846 struct hlist_node *entry;
1847 struct hlist_head *table;
1848 int i;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001849
David S. Miller2518c7c2006-08-24 04:45:07 -07001850 hlist_for_each_entry(pol, entry,
1851 &xfrm_policy_inexact[dir], bydst)
1852 prune_one_bundle(pol, func, &gc_list);
1853
1854 table = xfrm_policy_bydst[dir].table;
1855 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
1856 hlist_for_each_entry(pol, entry, table + i, bydst)
1857 prune_one_bundle(pol, func, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
1859 }
1860 read_unlock_bh(&xfrm_policy_lock);
1861
1862 while (gc_list) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001863 struct dst_entry *dst = gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 gc_list = dst->next;
1865 dst_free(dst);
1866 }
1867}
1868
1869static int unused_bundle(struct dst_entry *dst)
1870{
1871 return !atomic_read(&dst->__refcnt);
1872}
1873
1874static void __xfrm_garbage_collect(void)
1875{
1876 xfrm_prune_bundles(unused_bundle);
1877}
1878
David S. Miller1c095392006-08-24 03:30:28 -07001879static int xfrm_flush_bundles(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
1881 xfrm_prune_bundles(stale_bundle);
1882 return 0;
1883}
1884
1885void xfrm_init_pmtu(struct dst_entry *dst)
1886{
1887 do {
1888 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1889 u32 pmtu, route_mtu_cached;
1890
1891 pmtu = dst_mtu(dst->child);
1892 xdst->child_mtu_cached = pmtu;
1893
1894 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
1895
1896 route_mtu_cached = dst_mtu(xdst->route);
1897 xdst->route_mtu_cached = route_mtu_cached;
1898
1899 if (pmtu > route_mtu_cached)
1900 pmtu = route_mtu_cached;
1901
1902 dst->metrics[RTAX_MTU-1] = pmtu;
1903 } while ((dst = dst->next));
1904}
1905
1906EXPORT_SYMBOL(xfrm_init_pmtu);
1907
1908/* Check that the bundle accepts the flow and its components are
1909 * still valid.
1910 */
1911
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05001912int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
1913 struct flowi *fl, int family, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
1915 struct dst_entry *dst = &first->u.dst;
1916 struct xfrm_dst *last;
1917 u32 mtu;
1918
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07001919 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 (dst->dev && !netif_running(dst->dev)))
1921 return 0;
1922
1923 last = NULL;
1924
1925 do {
1926 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1927
1928 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
1929 return 0;
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06001930 if (fl && pol &&
1931 !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001932 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if (dst->xfrm->km.state != XFRM_STATE_VALID)
1934 return 0;
David S. Miller9d4a7062006-08-24 03:18:09 -07001935 if (xdst->genid != dst->xfrm->genid)
1936 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07001938 if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
1939 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
1940 return 0;
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 mtu = dst_mtu(dst->child);
1943 if (xdst->child_mtu_cached != mtu) {
1944 last = xdst;
1945 xdst->child_mtu_cached = mtu;
1946 }
1947
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07001948 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 return 0;
1950 mtu = dst_mtu(xdst->route);
1951 if (xdst->route_mtu_cached != mtu) {
1952 last = xdst;
1953 xdst->route_mtu_cached = mtu;
1954 }
1955
1956 dst = dst->child;
1957 } while (dst->xfrm);
1958
1959 if (likely(!last))
1960 return 1;
1961
1962 mtu = last->child_mtu_cached;
1963 for (;;) {
1964 dst = &last->u.dst;
1965
1966 mtu = xfrm_state_mtu(dst->xfrm, mtu);
1967 if (mtu > last->route_mtu_cached)
1968 mtu = last->route_mtu_cached;
1969 dst->metrics[RTAX_MTU-1] = mtu;
1970
1971 if (last == first)
1972 break;
1973
1974 last = last->u.next;
1975 last->child_mtu_cached = mtu;
1976 }
1977
1978 return 1;
1979}
1980
1981EXPORT_SYMBOL(xfrm_bundle_ok);
1982
Joy Lattenc9204d92006-11-30 15:50:43 -06001983#ifdef CONFIG_AUDITSYSCALL
Joy Latten161a09e2006-11-27 13:11:54 -06001984/* Audit addition and deletion of SAs and ipsec policy */
1985
1986void xfrm_audit_log(uid_t auid, u32 sid, int type, int result,
1987 struct xfrm_policy *xp, struct xfrm_state *x)
1988{
1989
1990 char *secctx;
1991 u32 secctx_len;
1992 struct xfrm_sec_ctx *sctx = NULL;
1993 struct audit_buffer *audit_buf;
1994 int family;
1995 extern int audit_enabled;
1996
1997 if (audit_enabled == 0)
1998 return;
1999
2000 audit_buf = audit_log_start(current->audit_context, GFP_ATOMIC, type);
2001 if (audit_buf == NULL)
2002 return;
2003
2004 switch(type) {
2005 case AUDIT_MAC_IPSEC_ADDSA:
2006 audit_log_format(audit_buf, "SAD add: auid=%u", auid);
2007 break;
2008 case AUDIT_MAC_IPSEC_DELSA:
2009 audit_log_format(audit_buf, "SAD delete: auid=%u", auid);
2010 break;
2011 case AUDIT_MAC_IPSEC_ADDSPD:
2012 audit_log_format(audit_buf, "SPD add: auid=%u", auid);
2013 break;
2014 case AUDIT_MAC_IPSEC_DELSPD:
2015 audit_log_format(audit_buf, "SPD delete: auid=%u", auid);
2016 break;
2017 default:
2018 return;
2019 }
2020
2021 if (sid != 0 &&
2022 security_secid_to_secctx(sid, &secctx, &secctx_len) == 0)
2023 audit_log_format(audit_buf, " subj=%s", secctx);
2024 else
2025 audit_log_task_context(audit_buf);
2026
2027 if (xp) {
2028 family = xp->selector.family;
2029 if (xp->security)
2030 sctx = xp->security;
2031 } else {
2032 family = x->props.family;
2033 if (x->security)
2034 sctx = x->security;
2035 }
2036
2037 if (sctx)
2038 audit_log_format(audit_buf,
2039 " sec_alg=%u sec_doi=%u sec_obj=%s",
2040 sctx->ctx_alg, sctx->ctx_doi, sctx->ctx_str);
2041
2042 switch(family) {
2043 case AF_INET:
2044 {
2045 struct in_addr saddr, daddr;
2046 if (xp) {
2047 saddr.s_addr = xp->selector.saddr.a4;
2048 daddr.s_addr = xp->selector.daddr.a4;
2049 } else {
2050 saddr.s_addr = x->props.saddr.a4;
2051 daddr.s_addr = x->id.daddr.a4;
2052 }
2053 audit_log_format(audit_buf,
2054 " src=%u.%u.%u.%u dst=%u.%u.%u.%u",
2055 NIPQUAD(saddr), NIPQUAD(daddr));
2056 }
2057 break;
2058 case AF_INET6:
2059 {
2060 struct in6_addr saddr6, daddr6;
2061 if (xp) {
2062 memcpy(&saddr6, xp->selector.saddr.a6,
2063 sizeof(struct in6_addr));
2064 memcpy(&daddr6, xp->selector.daddr.a6,
2065 sizeof(struct in6_addr));
2066 } else {
2067 memcpy(&saddr6, x->props.saddr.a6,
2068 sizeof(struct in6_addr));
2069 memcpy(&daddr6, x->id.daddr.a6,
2070 sizeof(struct in6_addr));
2071 }
2072 audit_log_format(audit_buf,
2073 " src=" NIP6_FMT "dst=" NIP6_FMT,
2074 NIP6(saddr6), NIP6(daddr6));
2075 }
2076 break;
2077 }
2078
2079 if (x)
2080 audit_log_format(audit_buf, " spi=%lu(0x%lx) protocol=%s",
2081 (unsigned long)ntohl(x->id.spi),
2082 (unsigned long)ntohl(x->id.spi),
2083 x->id.proto == IPPROTO_AH ? "AH" :
2084 (x->id.proto == IPPROTO_ESP ?
2085 "ESP" : "IPCOMP"));
2086
2087 audit_log_format(audit_buf, " res=%u", result);
2088 audit_log_end(audit_buf);
2089}
2090
2091EXPORT_SYMBOL(xfrm_audit_log);
Joy Lattenc9204d92006-11-30 15:50:43 -06002092#endif /* CONFIG_AUDITSYSCALL */
Joy Latten161a09e2006-11-27 13:11:54 -06002093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2095{
2096 int err = 0;
2097 if (unlikely(afinfo == NULL))
2098 return -EINVAL;
2099 if (unlikely(afinfo->family >= NPROTO))
2100 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002101 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2103 err = -ENOBUFS;
2104 else {
2105 struct dst_ops *dst_ops = afinfo->dst_ops;
2106 if (likely(dst_ops->kmem_cachep == NULL))
2107 dst_ops->kmem_cachep = xfrm_dst_cache;
2108 if (likely(dst_ops->check == NULL))
2109 dst_ops->check = xfrm_dst_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (likely(dst_ops->negative_advice == NULL))
2111 dst_ops->negative_advice = xfrm_negative_advice;
2112 if (likely(dst_ops->link_failure == NULL))
2113 dst_ops->link_failure = xfrm_link_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 if (likely(afinfo->garbage_collect == NULL))
2115 afinfo->garbage_collect = __xfrm_garbage_collect;
2116 xfrm_policy_afinfo[afinfo->family] = afinfo;
2117 }
Ingo Molnare959d812006-04-28 15:32:29 -07002118 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 return err;
2120}
2121EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2122
2123int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2124{
2125 int err = 0;
2126 if (unlikely(afinfo == NULL))
2127 return -EINVAL;
2128 if (unlikely(afinfo->family >= NPROTO))
2129 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002130 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2132 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2133 err = -EINVAL;
2134 else {
2135 struct dst_ops *dst_ops = afinfo->dst_ops;
2136 xfrm_policy_afinfo[afinfo->family] = NULL;
2137 dst_ops->kmem_cachep = NULL;
2138 dst_ops->check = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 dst_ops->negative_advice = NULL;
2140 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 afinfo->garbage_collect = NULL;
2142 }
2143 }
Ingo Molnare959d812006-04-28 15:32:29 -07002144 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 return err;
2146}
2147EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2148
2149static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
2150{
2151 struct xfrm_policy_afinfo *afinfo;
2152 if (unlikely(family >= NPROTO))
2153 return NULL;
2154 read_lock(&xfrm_policy_afinfo_lock);
2155 afinfo = xfrm_policy_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07002156 if (unlikely(!afinfo))
2157 read_unlock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 return afinfo;
2159}
2160
2161static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2162{
Herbert Xu546be242006-05-27 23:03:58 -07002163 read_unlock(&xfrm_policy_afinfo_lock);
2164}
2165
2166static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
2167{
2168 struct xfrm_policy_afinfo *afinfo;
2169 if (unlikely(family >= NPROTO))
2170 return NULL;
2171 write_lock_bh(&xfrm_policy_afinfo_lock);
2172 afinfo = xfrm_policy_afinfo[family];
2173 if (unlikely(!afinfo))
2174 write_unlock_bh(&xfrm_policy_afinfo_lock);
2175 return afinfo;
2176}
2177
2178static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
2179{
2180 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
2183static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2184{
2185 switch (event) {
2186 case NETDEV_DOWN:
2187 xfrm_flush_bundles();
2188 }
2189 return NOTIFY_DONE;
2190}
2191
2192static struct notifier_block xfrm_dev_notifier = {
2193 xfrm_dev_event,
2194 NULL,
2195 0
2196};
2197
2198static void __init xfrm_policy_init(void)
2199{
David S. Miller2518c7c2006-08-24 04:45:07 -07002200 unsigned int hmask, sz;
2201 int dir;
2202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2204 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07002205 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
David S. Miller2518c7c2006-08-24 04:45:07 -07002208 hmask = 8 - 1;
2209 sz = (hmask+1) * sizeof(struct hlist_head);
2210
David S. Miller44e36b42006-08-24 04:50:50 -07002211 xfrm_policy_byidx = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002212 xfrm_idx_hmask = hmask;
2213 if (!xfrm_policy_byidx)
2214 panic("XFRM: failed to allocate byidx hash\n");
2215
2216 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2217 struct xfrm_policy_hash *htab;
2218
2219 INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
2220
2221 htab = &xfrm_policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002222 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002223 htab->hmask = hmask;
2224 if (!htab->table)
2225 panic("XFRM: failed to allocate bydst hash\n");
2226 }
2227
David Howellsc4028952006-11-22 14:57:56 +00002228 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 register_netdevice_notifier(&xfrm_dev_notifier);
2230}
2231
2232void __init xfrm_init(void)
2233{
2234 xfrm_state_init();
2235 xfrm_policy_init();
2236 xfrm_input_init();
2237}
2238
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002239#ifdef CONFIG_XFRM_MIGRATE
2240static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
2241 struct xfrm_selector *sel_tgt)
2242{
2243 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
2244 if (sel_tgt->family == sel_cmp->family &&
2245 xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002246 sel_cmp->family) == 0 &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002247 xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
2248 sel_cmp->family) == 0 &&
2249 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
2250 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
2251 return 1;
2252 }
2253 } else {
2254 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
2255 return 1;
2256 }
2257 }
2258 return 0;
2259}
2260
2261static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
2262 u8 dir, u8 type)
2263{
2264 struct xfrm_policy *pol, *ret = NULL;
2265 struct hlist_node *entry;
2266 struct hlist_head *chain;
2267 u32 priority = ~0U;
2268
2269 read_lock_bh(&xfrm_policy_lock);
2270 chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
2271 hlist_for_each_entry(pol, entry, chain, bydst) {
2272 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2273 pol->type == type) {
2274 ret = pol;
2275 priority = ret->priority;
2276 break;
2277 }
2278 }
2279 chain = &xfrm_policy_inexact[dir];
2280 hlist_for_each_entry(pol, entry, chain, bydst) {
2281 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2282 pol->type == type &&
2283 pol->priority < priority) {
2284 ret = pol;
2285 break;
2286 }
2287 }
2288
2289 if (ret)
2290 xfrm_pol_hold(ret);
2291
2292 read_unlock_bh(&xfrm_policy_lock);
2293
2294 return ret;
2295}
2296
2297static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
2298{
2299 int match = 0;
2300
2301 if (t->mode == m->mode && t->id.proto == m->proto &&
2302 (m->reqid == 0 || t->reqid == m->reqid)) {
2303 switch (t->mode) {
2304 case XFRM_MODE_TUNNEL:
2305 case XFRM_MODE_BEET:
2306 if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
2307 m->old_family) == 0 &&
2308 xfrm_addr_cmp(&t->saddr, &m->old_saddr,
2309 m->old_family) == 0) {
2310 match = 1;
2311 }
2312 break;
2313 case XFRM_MODE_TRANSPORT:
2314 /* in case of transport mode, template does not store
2315 any IP addresses, hence we just compare mode and
2316 protocol */
2317 match = 1;
2318 break;
2319 default:
2320 break;
2321 }
2322 }
2323 return match;
2324}
2325
2326/* update endpoint address(es) of template(s) */
2327static int xfrm_policy_migrate(struct xfrm_policy *pol,
2328 struct xfrm_migrate *m, int num_migrate)
2329{
2330 struct xfrm_migrate *mp;
2331 struct dst_entry *dst;
2332 int i, j, n = 0;
2333
2334 write_lock_bh(&pol->lock);
2335 if (unlikely(pol->dead)) {
2336 /* target policy has been deleted */
2337 write_unlock_bh(&pol->lock);
2338 return -ENOENT;
2339 }
2340
2341 for (i = 0; i < pol->xfrm_nr; i++) {
2342 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
2343 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
2344 continue;
2345 n++;
2346 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL)
2347 continue;
2348 /* update endpoints */
2349 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
2350 sizeof(pol->xfrm_vec[i].id.daddr));
2351 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
2352 sizeof(pol->xfrm_vec[i].saddr));
2353 pol->xfrm_vec[i].encap_family = mp->new_family;
2354 /* flush bundles */
2355 while ((dst = pol->bundles) != NULL) {
2356 pol->bundles = dst->next;
2357 dst_free(dst);
2358 }
2359 }
2360 }
2361
2362 write_unlock_bh(&pol->lock);
2363
2364 if (!n)
2365 return -ENODATA;
2366
2367 return 0;
2368}
2369
2370static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
2371{
2372 int i, j;
2373
2374 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
2375 return -EINVAL;
2376
2377 for (i = 0; i < num_migrate; i++) {
2378 if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
2379 m[i].old_family) == 0) &&
2380 (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
2381 m[i].old_family) == 0))
2382 return -EINVAL;
2383 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
2384 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
2385 return -EINVAL;
2386
2387 /* check if there is any duplicated entry */
2388 for (j = i + 1; j < num_migrate; j++) {
2389 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
2390 sizeof(m[i].old_daddr)) &&
2391 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
2392 sizeof(m[i].old_saddr)) &&
2393 m[i].proto == m[j].proto &&
2394 m[i].mode == m[j].mode &&
2395 m[i].reqid == m[j].reqid &&
2396 m[i].old_family == m[j].old_family)
2397 return -EINVAL;
2398 }
2399 }
2400
2401 return 0;
2402}
2403
2404int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
2405 struct xfrm_migrate *m, int num_migrate)
2406{
2407 int i, err, nx_cur = 0, nx_new = 0;
2408 struct xfrm_policy *pol = NULL;
2409 struct xfrm_state *x, *xc;
2410 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
2411 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
2412 struct xfrm_migrate *mp;
2413
2414 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
2415 goto out;
2416
2417 /* Stage 1 - find policy */
2418 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
2419 err = -ENOENT;
2420 goto out;
2421 }
2422
2423 /* Stage 2 - find and update state(s) */
2424 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
2425 if ((x = xfrm_migrate_state_find(mp))) {
2426 x_cur[nx_cur] = x;
2427 nx_cur++;
2428 if ((xc = xfrm_state_migrate(x, mp))) {
2429 x_new[nx_new] = xc;
2430 nx_new++;
2431 } else {
2432 err = -ENODATA;
2433 goto restore_state;
2434 }
2435 }
2436 }
2437
2438 /* Stage 3 - update policy */
2439 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
2440 goto restore_state;
2441
2442 /* Stage 4 - delete old state(s) */
2443 if (nx_cur) {
2444 xfrm_states_put(x_cur, nx_cur);
2445 xfrm_states_delete(x_cur, nx_cur);
2446 }
2447
2448 /* Stage 5 - announce */
2449 km_migrate(sel, dir, type, m, num_migrate);
2450
2451 xfrm_pol_put(pol);
2452
2453 return 0;
2454out:
2455 return err;
2456
2457restore_state:
2458 if (pol)
2459 xfrm_pol_put(pol);
2460 if (nx_cur)
2461 xfrm_states_put(x_cur, nx_cur);
2462 if (nx_new)
2463 xfrm_states_delete(x_new, nx_new);
2464
2465 return err;
2466}
David S. Millere610e672007-02-08 13:29:15 -08002467EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002468#endif
2469