blob: 32ddb7b12e7f01af481c2da07f7ec6f310c067a7 [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>
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -080030#ifdef CONFIG_XFRM_STATISTICS
31#include <net/snmp.h>
32#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
David S. Miller44e36b42006-08-24 04:50:50 -070034#include "xfrm_hash.h"
35
David S. Milleraad0e0b2007-05-25 00:42:49 -070036int sysctl_xfrm_larval_drop __read_mostly;
David S. Miller14e50e52007-05-24 18:17:54 -070037
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -080038#ifdef CONFIG_XFRM_STATISTICS
39DEFINE_SNMP_STAT(struct linux_xfrm_mib, xfrm_statistics) __read_mostly;
40EXPORT_SYMBOL(xfrm_statistics);
41#endif
42
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080043DEFINE_MUTEX(xfrm_cfg_mutex);
44EXPORT_SYMBOL(xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46static DEFINE_RWLOCK(xfrm_policy_lock);
47
David S. Miller2518c7c2006-08-24 04:45:07 -070048unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
49EXPORT_SYMBOL(xfrm_policy_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
52static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
53
Christoph Lametere18b8902006-12-06 20:33:20 -080054static struct kmem_cache *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56static struct work_struct xfrm_policy_gc_work;
David S. Miller2518c7c2006-08-24 04:45:07 -070057static HLIST_HEAD(xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
59
60static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
61static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
Herbert Xu25ee3282007-12-11 09:32:34 -080062static void xfrm_init_pmtu(struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Andrew Morton77681022006-11-08 22:46:26 -080064static inline int
65__xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
66{
67 return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
68 addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
69 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
70 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
71 (fl->proto == sel->proto || !sel->proto) &&
72 (fl->oif == sel->ifindex || !sel->ifindex);
73}
74
75static inline int
76__xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
77{
78 return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
79 addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
80 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
81 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
82 (fl->proto == sel->proto || !sel->proto) &&
83 (fl->oif == sel->ifindex || !sel->ifindex);
84}
85
86int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
87 unsigned short family)
88{
89 switch (family) {
90 case AF_INET:
91 return __xfrm4_selector_match(sel, fl);
92 case AF_INET6:
93 return __xfrm6_selector_match(sel, fl);
94 }
95 return 0;
96}
97
Herbert Xu25ee3282007-12-11 09:32:34 -080098static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
99 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800101 xfrm_address_t *saddr = &x->props.saddr;
102 xfrm_address_t *daddr = &x->id.daddr;
103 struct xfrm_policy_afinfo *afinfo;
104 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800106 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR)
107 saddr = x->coaddr;
108 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR)
109 daddr = x->coaddr;
110
Herbert Xu25ee3282007-12-11 09:32:34 -0800111 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800113 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800115 dst = afinfo->dst_lookup(tos, saddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 xfrm_policy_put_afinfo(afinfo);
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800117 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static inline unsigned long make_jiffies(long secs)
121{
122 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
123 return MAX_SCHEDULE_TIMEOUT-1;
124 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900125 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128static void xfrm_policy_timer(unsigned long data)
129{
130 struct xfrm_policy *xp = (struct xfrm_policy*)data;
James Morris9d729f72007-03-04 16:12:44 -0800131 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 long next = LONG_MAX;
133 int warn = 0;
134 int dir;
135
136 read_lock(&xp->lock);
137
138 if (xp->dead)
139 goto out;
140
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700141 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 if (xp->lft.hard_add_expires_seconds) {
144 long tmo = xp->lft.hard_add_expires_seconds +
145 xp->curlft.add_time - now;
146 if (tmo <= 0)
147 goto expired;
148 if (tmo < next)
149 next = tmo;
150 }
151 if (xp->lft.hard_use_expires_seconds) {
152 long tmo = xp->lft.hard_use_expires_seconds +
153 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
154 if (tmo <= 0)
155 goto expired;
156 if (tmo < next)
157 next = tmo;
158 }
159 if (xp->lft.soft_add_expires_seconds) {
160 long tmo = xp->lft.soft_add_expires_seconds +
161 xp->curlft.add_time - now;
162 if (tmo <= 0) {
163 warn = 1;
164 tmo = XFRM_KM_TIMEOUT;
165 }
166 if (tmo < next)
167 next = tmo;
168 }
169 if (xp->lft.soft_use_expires_seconds) {
170 long tmo = xp->lft.soft_use_expires_seconds +
171 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
172 if (tmo <= 0) {
173 warn = 1;
174 tmo = XFRM_KM_TIMEOUT;
175 }
176 if (tmo < next)
177 next = tmo;
178 }
179
180 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800181 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (next != LONG_MAX &&
183 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
184 xfrm_pol_hold(xp);
185
186out:
187 read_unlock(&xp->lock);
188 xfrm_pol_put(xp);
189 return;
190
191expired:
192 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700193 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800194 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 xfrm_pol_put(xp);
196}
197
198
199/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
200 * SPD calls.
201 */
202
Al Virodd0fc662005-10-07 07:46:04 +0100203struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 struct xfrm_policy *policy;
206
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700207 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 if (policy) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700210 INIT_HLIST_NODE(&policy->bydst);
211 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700213 atomic_set(&policy->refcnt, 1);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800214 setup_timer(&policy->timer, xfrm_policy_timer,
215 (unsigned long)policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217 return policy;
218}
219EXPORT_SYMBOL(xfrm_policy_alloc);
220
221/* Destroy xfrm_policy: descendant resources must be released to this moment. */
222
223void __xfrm_policy_destroy(struct xfrm_policy *policy)
224{
Kris Katterjohn09a62662006-01-08 22:24:28 -0800225 BUG_ON(!policy->dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Kris Katterjohn09a62662006-01-08 22:24:28 -0800227 BUG_ON(policy->bundles);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 if (del_timer(&policy->timer))
230 BUG();
231
Trent Jaegerdf718372005-12-13 23:12:27 -0800232 security_xfrm_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 kfree(policy);
234}
235EXPORT_SYMBOL(__xfrm_policy_destroy);
236
237static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
238{
239 struct dst_entry *dst;
240
241 while ((dst = policy->bundles) != NULL) {
242 policy->bundles = dst->next;
243 dst_free(dst);
244 }
245
246 if (del_timer(&policy->timer))
247 atomic_dec(&policy->refcnt);
248
249 if (atomic_read(&policy->refcnt) > 1)
250 flow_cache_flush();
251
252 xfrm_pol_put(policy);
253}
254
David Howellsc4028952006-11-22 14:57:56 +0000255static void xfrm_policy_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 struct xfrm_policy *policy;
David S. Miller2518c7c2006-08-24 04:45:07 -0700258 struct hlist_node *entry, *tmp;
259 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 spin_lock_bh(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700262 gc_list.first = xfrm_policy_gc_list.first;
263 INIT_HLIST_HEAD(&xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 spin_unlock_bh(&xfrm_policy_gc_lock);
265
David S. Miller2518c7c2006-08-24 04:45:07 -0700266 hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 xfrm_policy_gc_kill(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270/* Rule must be locked. Release descentant resources, announce
271 * entry dead. The rule must be unlinked from lists to the moment.
272 */
273
274static void xfrm_policy_kill(struct xfrm_policy *policy)
275{
276 int dead;
277
278 write_lock_bh(&policy->lock);
279 dead = policy->dead;
280 policy->dead = 1;
281 write_unlock_bh(&policy->lock);
282
283 if (unlikely(dead)) {
284 WARN_ON(1);
285 return;
286 }
287
288 spin_lock(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700289 hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 spin_unlock(&xfrm_policy_gc_lock);
291
292 schedule_work(&xfrm_policy_gc_work);
293}
294
David S. Miller2518c7c2006-08-24 04:45:07 -0700295struct xfrm_policy_hash {
296 struct hlist_head *table;
297 unsigned int hmask;
298};
299
300static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
301static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
302static struct hlist_head *xfrm_policy_byidx __read_mostly;
303static unsigned int xfrm_idx_hmask __read_mostly;
304static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
305
David S. Miller2518c7c2006-08-24 04:45:07 -0700306static inline unsigned int idx_hash(u32 index)
307{
308 return __idx_hash(index, xfrm_idx_hmask);
309}
310
David S. Miller2518c7c2006-08-24 04:45:07 -0700311static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
312{
313 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
314 unsigned int hash = __sel_hash(sel, family, hmask);
315
316 return (hash == hmask + 1 ?
317 &xfrm_policy_inexact[dir] :
318 xfrm_policy_bydst[dir].table + hash);
319}
320
321static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
322{
323 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
324 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
325
326 return xfrm_policy_bydst[dir].table + hash;
327}
328
David S. Miller2518c7c2006-08-24 04:45:07 -0700329static void xfrm_dst_hash_transfer(struct hlist_head *list,
330 struct hlist_head *ndsttable,
331 unsigned int nhashmask)
332{
333 struct hlist_node *entry, *tmp;
334 struct xfrm_policy *pol;
335
336 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
337 unsigned int h;
338
339 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
340 pol->family, nhashmask);
341 hlist_add_head(&pol->bydst, ndsttable+h);
342 }
343}
344
345static void xfrm_idx_hash_transfer(struct hlist_head *list,
346 struct hlist_head *nidxtable,
347 unsigned int nhashmask)
348{
349 struct hlist_node *entry, *tmp;
350 struct xfrm_policy *pol;
351
352 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
353 unsigned int h;
354
355 h = __idx_hash(pol->index, nhashmask);
356 hlist_add_head(&pol->byidx, nidxtable+h);
357 }
358}
359
360static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
361{
362 return ((old_hmask + 1) << 1) - 1;
363}
364
365static void xfrm_bydst_resize(int dir)
366{
367 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
368 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
369 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
370 struct hlist_head *odst = xfrm_policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700371 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700372 int i;
373
374 if (!ndst)
375 return;
376
377 write_lock_bh(&xfrm_policy_lock);
378
379 for (i = hmask; i >= 0; i--)
380 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
381
382 xfrm_policy_bydst[dir].table = ndst;
383 xfrm_policy_bydst[dir].hmask = nhashmask;
384
385 write_unlock_bh(&xfrm_policy_lock);
386
David S. Miller44e36b42006-08-24 04:50:50 -0700387 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700388}
389
390static void xfrm_byidx_resize(int total)
391{
392 unsigned int hmask = xfrm_idx_hmask;
393 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
394 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
395 struct hlist_head *oidx = xfrm_policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700396 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700397 int i;
398
399 if (!nidx)
400 return;
401
402 write_lock_bh(&xfrm_policy_lock);
403
404 for (i = hmask; i >= 0; i--)
405 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
406
407 xfrm_policy_byidx = nidx;
408 xfrm_idx_hmask = nhashmask;
409
410 write_unlock_bh(&xfrm_policy_lock);
411
David S. Miller44e36b42006-08-24 04:50:50 -0700412 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700413}
414
415static inline int xfrm_bydst_should_resize(int dir, int *total)
416{
417 unsigned int cnt = xfrm_policy_count[dir];
418 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
419
420 if (total)
421 *total += cnt;
422
423 if ((hmask + 1) < xfrm_policy_hashmax &&
424 cnt > hmask)
425 return 1;
426
427 return 0;
428}
429
430static inline int xfrm_byidx_should_resize(int total)
431{
432 unsigned int hmask = xfrm_idx_hmask;
433
434 if ((hmask + 1) < xfrm_policy_hashmax &&
435 total > hmask)
436 return 1;
437
438 return 0;
439}
440
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700441void xfrm_spd_getinfo(struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700442{
443 read_lock_bh(&xfrm_policy_lock);
444 si->incnt = xfrm_policy_count[XFRM_POLICY_IN];
445 si->outcnt = xfrm_policy_count[XFRM_POLICY_OUT];
446 si->fwdcnt = xfrm_policy_count[XFRM_POLICY_FWD];
447 si->inscnt = xfrm_policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
448 si->outscnt = xfrm_policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
449 si->fwdscnt = xfrm_policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
450 si->spdhcnt = xfrm_idx_hmask;
451 si->spdhmcnt = xfrm_policy_hashmax;
452 read_unlock_bh(&xfrm_policy_lock);
453}
454EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700455
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700456static DEFINE_MUTEX(hash_resize_mutex);
David Howellsc4028952006-11-22 14:57:56 +0000457static void xfrm_hash_resize(struct work_struct *__unused)
David S. Miller2518c7c2006-08-24 04:45:07 -0700458{
459 int dir, total;
460
461 mutex_lock(&hash_resize_mutex);
462
463 total = 0;
464 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
465 if (xfrm_bydst_should_resize(dir, &total))
466 xfrm_bydst_resize(dir);
467 }
468 if (xfrm_byidx_should_resize(total))
469 xfrm_byidx_resize(total);
470
471 mutex_unlock(&hash_resize_mutex);
472}
473
David Howellsc4028952006-11-22 14:57:56 +0000474static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476/* Generate new index... KAME seems to generate them ordered by cost
477 * of an absolute inpredictability of ordering of rules. This will not pass. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700478static u32 xfrm_gen_index(u8 type, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 static u32 idx_generator;
481
482 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700483 struct hlist_node *entry;
484 struct hlist_head *list;
485 struct xfrm_policy *p;
486 u32 idx;
487 int found;
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 idx = (idx_generator | dir);
490 idx_generator += 8;
491 if (idx == 0)
492 idx = 8;
David S. Miller2518c7c2006-08-24 04:45:07 -0700493 list = xfrm_policy_byidx + idx_hash(idx);
494 found = 0;
495 hlist_for_each_entry(p, entry, list, byidx) {
496 if (p->index == idx) {
497 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700501 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return idx;
503 }
504}
505
David S. Miller2518c7c2006-08-24 04:45:07 -0700506static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
507{
508 u32 *p1 = (u32 *) s1;
509 u32 *p2 = (u32 *) s2;
510 int len = sizeof(struct xfrm_selector) / sizeof(u32);
511 int i;
512
513 for (i = 0; i < len; i++) {
514 if (p1[i] != p2[i])
515 return 1;
516 }
517
518 return 0;
519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
522{
David S. Miller2518c7c2006-08-24 04:45:07 -0700523 struct xfrm_policy *pol;
524 struct xfrm_policy *delpol;
525 struct hlist_head *chain;
Herbert Xua6c7ab52007-01-16 16:52:02 -0800526 struct hlist_node *entry, *newpos;
David S. Miller9b78a822005-12-22 07:39:48 -0800527 struct dst_entry *gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700530 chain = policy_hash_bysel(&policy->selector, policy->family, dir);
531 delpol = NULL;
532 newpos = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700533 hlist_for_each_entry(pol, entry, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800534 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700535 !selector_cmp(&pol->selector, &policy->selector) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800536 xfrm_sec_ctx_match(pol->security, policy->security) &&
537 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (excl) {
539 write_unlock_bh(&xfrm_policy_lock);
540 return -EEXIST;
541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 delpol = pol;
543 if (policy->priority > pol->priority)
544 continue;
545 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800546 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 continue;
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (delpol)
550 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700553 hlist_add_after(newpos, &policy->bydst);
554 else
555 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 xfrm_pol_hold(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700557 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700559 if (delpol) {
560 hlist_del(&delpol->bydst);
561 hlist_del(&delpol->byidx);
562 xfrm_policy_count[dir]--;
563 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700564 policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700565 hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800566 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 policy->curlft.use_time = 0;
568 if (!mod_timer(&policy->timer, jiffies + HZ))
569 xfrm_pol_hold(policy);
570 write_unlock_bh(&xfrm_policy_lock);
571
David S. Miller9b78a822005-12-22 07:39:48 -0800572 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 xfrm_policy_kill(delpol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700574 else if (xfrm_bydst_should_resize(dir, NULL))
575 schedule_work(&xfrm_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800576
577 read_lock_bh(&xfrm_policy_lock);
578 gc_list = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700579 entry = &policy->bydst;
580 hlist_for_each_entry_continue(policy, entry, bydst) {
David S. Miller9b78a822005-12-22 07:39:48 -0800581 struct dst_entry *dst;
582
583 write_lock(&policy->lock);
584 dst = policy->bundles;
585 if (dst) {
586 struct dst_entry *tail = dst;
587 while (tail->next)
588 tail = tail->next;
589 tail->next = gc_list;
590 gc_list = dst;
591
592 policy->bundles = NULL;
593 }
594 write_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
David S. Miller9b78a822005-12-22 07:39:48 -0800596 read_unlock_bh(&xfrm_policy_lock);
597
598 while (gc_list) {
599 struct dst_entry *dst = gc_list;
600
601 gc_list = dst->next;
602 dst_free(dst);
603 }
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return 0;
606}
607EXPORT_SYMBOL(xfrm_policy_insert);
608
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700609struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
610 struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800611 struct xfrm_sec_ctx *ctx, int delete,
612 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
David S. Miller2518c7c2006-08-24 04:45:07 -0700614 struct xfrm_policy *pol, *ret;
615 struct hlist_head *chain;
616 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Eric Parisef41aaa2007-03-07 15:37:58 -0800618 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700620 chain = policy_hash_bysel(sel, sel->family, dir);
621 ret = NULL;
622 hlist_for_each_entry(pol, entry, chain, bydst) {
623 if (pol->type == type &&
624 !selector_cmp(sel, &pol->selector) &&
625 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700627 if (delete) {
Eric Parisef41aaa2007-03-07 15:37:58 -0800628 *err = security_xfrm_policy_delete(pol);
629 if (*err) {
630 write_unlock_bh(&xfrm_policy_lock);
631 return pol;
632 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700633 hlist_del(&pol->bydst);
634 hlist_del(&pol->byidx);
635 xfrm_policy_count[dir]--;
636 }
637 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 break;
639 }
640 }
641 write_unlock_bh(&xfrm_policy_lock);
642
David S. Miller2518c7c2006-08-24 04:45:07 -0700643 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700645 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700647 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
Trent Jaegerdf718372005-12-13 23:12:27 -0800649EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Eric Parisef41aaa2007-03-07 15:37:58 -0800651struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete,
652 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
David S. Miller2518c7c2006-08-24 04:45:07 -0700654 struct xfrm_policy *pol, *ret;
655 struct hlist_head *chain;
656 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Herbert Xub5505c62007-05-14 02:15:47 -0700658 *err = -ENOENT;
659 if (xfrm_policy_id2dir(id) != dir)
660 return NULL;
661
Eric Parisef41aaa2007-03-07 15:37:58 -0800662 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700664 chain = xfrm_policy_byidx + idx_hash(id);
665 ret = NULL;
666 hlist_for_each_entry(pol, entry, chain, byidx) {
667 if (pol->type == type && pol->index == id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700669 if (delete) {
Eric Parisef41aaa2007-03-07 15:37:58 -0800670 *err = security_xfrm_policy_delete(pol);
671 if (*err) {
672 write_unlock_bh(&xfrm_policy_lock);
673 return pol;
674 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700675 hlist_del(&pol->bydst);
676 hlist_del(&pol->byidx);
677 xfrm_policy_count[dir]--;
678 }
679 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 break;
681 }
682 }
683 write_unlock_bh(&xfrm_policy_lock);
684
David S. Miller2518c7c2006-08-24 04:45:07 -0700685 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700687 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700689 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691EXPORT_SYMBOL(xfrm_policy_byid);
692
Joy Latten4aa2e622007-06-04 19:05:57 -0400693#ifdef CONFIG_SECURITY_NETWORK_XFRM
694static inline int
695xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Joy Latten4aa2e622007-06-04 19:05:57 -0400697 int dir, err = 0;
698
699 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
700 struct xfrm_policy *pol;
701 struct hlist_node *entry;
702 int i;
703
704 hlist_for_each_entry(pol, entry,
705 &xfrm_policy_inexact[dir], bydst) {
706 if (pol->type != type)
707 continue;
708 err = security_xfrm_policy_delete(pol);
709 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700710 xfrm_audit_policy_delete(pol, 0,
711 audit_info->loginuid,
712 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400713 return err;
714 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900715 }
Joy Latten4aa2e622007-06-04 19:05:57 -0400716 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
717 hlist_for_each_entry(pol, entry,
718 xfrm_policy_bydst[dir].table + i,
719 bydst) {
720 if (pol->type != type)
721 continue;
722 err = security_xfrm_policy_delete(pol);
723 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700724 xfrm_audit_policy_delete(pol, 0,
725 audit_info->loginuid,
726 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400727 return err;
728 }
729 }
730 }
731 }
732 return err;
733}
734#else
735static inline int
736xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
737{
738 return 0;
739}
740#endif
741
742int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
743{
744 int dir, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 write_lock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400747
748 err = xfrm_policy_flush_secctx_check(type, audit_info);
749 if (err)
750 goto out;
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700753 struct xfrm_policy *pol;
754 struct hlist_node *entry;
David S. Millerae8c0572006-10-03 16:00:26 -0700755 int i, killed;
David S. Miller2518c7c2006-08-24 04:45:07 -0700756
David S. Millerae8c0572006-10-03 16:00:26 -0700757 killed = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700758 again1:
759 hlist_for_each_entry(pol, entry,
760 &xfrm_policy_inexact[dir], bydst) {
761 if (pol->type != type)
762 continue;
763 hlist_del(&pol->bydst);
764 hlist_del(&pol->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 write_unlock_bh(&xfrm_policy_lock);
766
Joy Lattenab5f5e82007-09-17 11:51:22 -0700767 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
768 audit_info->secid);
Joy Latten161a09e2006-11-27 13:11:54 -0600769
David S. Miller2518c7c2006-08-24 04:45:07 -0700770 xfrm_policy_kill(pol);
David S. Millerae8c0572006-10-03 16:00:26 -0700771 killed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700774 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700776
777 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
778 again2:
779 hlist_for_each_entry(pol, entry,
780 xfrm_policy_bydst[dir].table + i,
781 bydst) {
782 if (pol->type != type)
783 continue;
784 hlist_del(&pol->bydst);
785 hlist_del(&pol->byidx);
786 write_unlock_bh(&xfrm_policy_lock);
787
Joy Lattenab5f5e82007-09-17 11:51:22 -0700788 xfrm_audit_policy_delete(pol, 1,
789 audit_info->loginuid,
790 audit_info->secid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700791 xfrm_policy_kill(pol);
David S. Millerae8c0572006-10-03 16:00:26 -0700792 killed++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700793
794 write_lock_bh(&xfrm_policy_lock);
795 goto again2;
796 }
797 }
798
David S. Millerae8c0572006-10-03 16:00:26 -0700799 xfrm_policy_count[dir] -= killed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801 atomic_inc(&flow_cache_genid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400802out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 write_unlock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400804 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806EXPORT_SYMBOL(xfrm_policy_flush);
807
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700808int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 void *data)
810{
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800811 struct xfrm_policy *pol, *last = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700812 struct hlist_node *entry;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800813 int dir, last_dir = 0, count, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700816 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700819 struct hlist_head *table = xfrm_policy_bydst[dir].table;
820 int i;
821
822 hlist_for_each_entry(pol, entry,
823 &xfrm_policy_inexact[dir], bydst) {
824 if (pol->type != type)
825 continue;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800826 if (last) {
827 error = func(last, last_dir % XFRM_POLICY_MAX,
828 count, data);
829 if (error)
830 goto out;
831 }
832 last = pol;
833 last_dir = dir;
834 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700836 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
837 hlist_for_each_entry(pol, entry, table + i, bydst) {
838 if (pol->type != type)
839 continue;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800840 if (last) {
841 error = func(last, last_dir % XFRM_POLICY_MAX,
842 count, data);
843 if (error)
844 goto out;
845 }
846 last = pol;
847 last_dir = dir;
848 count++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700849 }
850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800852 if (count == 0) {
853 error = -ENOENT;
854 goto out;
855 }
856 error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857out:
858 read_unlock_bh(&xfrm_policy_lock);
859 return error;
860}
861EXPORT_SYMBOL(xfrm_policy_walk);
862
James Morris134b0fc2006-10-05 15:42:27 -0500863/*
864 * Find policy to apply to this flow.
865 *
866 * Returns 0 if policy found, else an -errno.
867 */
David S. Miller2518c7c2006-08-24 04:45:07 -0700868static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
869 u8 type, u16 family, int dir)
870{
871 struct xfrm_selector *sel = &pol->selector;
James Morris134b0fc2006-10-05 15:42:27 -0500872 int match, ret = -ESRCH;
David S. Miller2518c7c2006-08-24 04:45:07 -0700873
874 if (pol->family != family ||
875 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500876 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700877
878 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500879 if (match)
880 ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700881
James Morris134b0fc2006-10-05 15:42:27 -0500882 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700883}
884
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700885static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
886 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
James Morris134b0fc2006-10-05 15:42:27 -0500888 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700889 struct xfrm_policy *pol, *ret;
890 xfrm_address_t *daddr, *saddr;
891 struct hlist_node *entry;
892 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700893 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700894
895 daddr = xfrm_flowi_daddr(fl, family);
896 saddr = xfrm_flowi_saddr(fl, family);
897 if (unlikely(!daddr || !saddr))
898 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700901 chain = policy_hash_direct(daddr, saddr, family, dir);
902 ret = NULL;
903 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500904 err = xfrm_policy_match(pol, fl, type, family, dir);
905 if (err) {
906 if (err == -ESRCH)
907 continue;
908 else {
909 ret = ERR_PTR(err);
910 goto fail;
911 }
912 } else {
David S. Milleracba48e2006-08-25 15:46:46 -0700913 ret = pol;
914 priority = ret->priority;
915 break;
916 }
917 }
918 chain = &xfrm_policy_inexact[dir];
919 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500920 err = xfrm_policy_match(pol, fl, type, family, dir);
921 if (err) {
922 if (err == -ESRCH)
923 continue;
924 else {
925 ret = ERR_PTR(err);
926 goto fail;
927 }
928 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700929 ret = pol;
930 break;
931 }
932 }
David S. Milleracba48e2006-08-25 15:46:46 -0700933 if (ret)
934 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -0500935fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700937
David S. Miller2518c7c2006-08-24 04:45:07 -0700938 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700939}
940
James Morris134b0fc2006-10-05 15:42:27 -0500941static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700942 void **objp, atomic_t **obj_refp)
943{
944 struct xfrm_policy *pol;
James Morris134b0fc2006-10-05 15:42:27 -0500945 int err = 0;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700946
947#ifdef CONFIG_XFRM_SUB_POLICY
948 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
James Morris134b0fc2006-10-05 15:42:27 -0500949 if (IS_ERR(pol)) {
950 err = PTR_ERR(pol);
951 pol = NULL;
952 }
953 if (pol || err)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700954 goto end;
955#endif
956 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
James Morris134b0fc2006-10-05 15:42:27 -0500957 if (IS_ERR(pol)) {
958 err = PTR_ERR(pol);
959 pol = NULL;
960 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700961#ifdef CONFIG_XFRM_SUB_POLICY
David S. Miller2518c7c2006-08-24 04:45:07 -0700962end:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700963#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if ((*objp = (void *) pol) != NULL)
965 *obj_refp = &pol->refcnt;
James Morris134b0fc2006-10-05 15:42:27 -0500966 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
Trent Jaegerdf718372005-12-13 23:12:27 -0800969static inline int policy_to_flow_dir(int dir)
970{
971 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900972 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
973 XFRM_POLICY_FWD == FLOW_DIR_FWD)
974 return dir;
975 switch (dir) {
976 default:
977 case XFRM_POLICY_IN:
978 return FLOW_DIR_IN;
979 case XFRM_POLICY_OUT:
980 return FLOW_DIR_OUT;
981 case XFRM_POLICY_FWD:
982 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700983 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800984}
985
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700986static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 struct xfrm_policy *pol;
989
990 read_lock_bh(&xfrm_policy_lock);
991 if ((pol = sk->sk_policy[dir]) != NULL) {
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900992 int match = xfrm_selector_match(&pol->selector, fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900994 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -0800995
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -0500996 if (match) {
997 err = security_xfrm_policy_lookup(pol, fl->secid,
998 policy_to_flow_dir(dir));
999 if (!err)
1000 xfrm_pol_hold(pol);
1001 else if (err == -ESRCH)
1002 pol = NULL;
1003 else
1004 pol = ERR_PTR(err);
1005 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 pol = NULL;
1007 }
1008 read_unlock_bh(&xfrm_policy_lock);
1009 return pol;
1010}
1011
1012static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1013{
David S. Miller2518c7c2006-08-24 04:45:07 -07001014 struct hlist_head *chain = policy_hash_bysel(&pol->selector,
1015 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001016
David S. Miller2518c7c2006-08-24 04:45:07 -07001017 hlist_add_head(&pol->bydst, chain);
1018 hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
1019 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001021
1022 if (xfrm_bydst_should_resize(dir, NULL))
1023 schedule_work(&xfrm_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
1026static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1027 int dir)
1028{
David S. Miller2518c7c2006-08-24 04:45:07 -07001029 if (hlist_unhashed(&pol->bydst))
1030 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
David S. Miller2518c7c2006-08-24 04:45:07 -07001032 hlist_del(&pol->bydst);
1033 hlist_del(&pol->byidx);
1034 xfrm_policy_count[dir]--;
1035
1036 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
Herbert Xu4666faa2005-06-18 22:43:22 -07001039int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 write_lock_bh(&xfrm_policy_lock);
1042 pol = __xfrm_policy_unlink(pol, dir);
1043 write_unlock_bh(&xfrm_policy_lock);
1044 if (pol) {
1045 if (dir < XFRM_POLICY_MAX)
1046 atomic_inc(&flow_cache_genid);
1047 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001050 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
David S. Millera70fcb02006-03-20 19:18:52 -08001052EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1055{
1056 struct xfrm_policy *old_pol;
1057
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001058#ifdef CONFIG_XFRM_SUB_POLICY
1059 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1060 return -EINVAL;
1061#endif
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 write_lock_bh(&xfrm_policy_lock);
1064 old_pol = sk->sk_policy[dir];
1065 sk->sk_policy[dir] = pol;
1066 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001067 pol->curlft.add_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001068 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1070 }
1071 if (old_pol)
1072 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1073 write_unlock_bh(&xfrm_policy_lock);
1074
1075 if (old_pol) {
1076 xfrm_policy_kill(old_pol);
1077 }
1078 return 0;
1079}
1080
1081static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1082{
1083 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
1084
1085 if (newp) {
1086 newp->selector = old->selector;
Trent Jaegerdf718372005-12-13 23:12:27 -08001087 if (security_xfrm_policy_clone(old, newp)) {
1088 kfree(newp);
1089 return NULL; /* ENOMEM */
1090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 newp->lft = old->lft;
1092 newp->curlft = old->curlft;
1093 newp->action = old->action;
1094 newp->flags = old->flags;
1095 newp->xfrm_nr = old->xfrm_nr;
1096 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001097 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 memcpy(newp->xfrm_vec, old->xfrm_vec,
1099 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1100 write_lock_bh(&xfrm_policy_lock);
1101 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1102 write_unlock_bh(&xfrm_policy_lock);
1103 xfrm_pol_put(newp);
1104 }
1105 return newp;
1106}
1107
1108int __xfrm_sk_clone_policy(struct sock *sk)
1109{
1110 struct xfrm_policy *p0 = sk->sk_policy[0],
1111 *p1 = sk->sk_policy[1];
1112
1113 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1114 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1115 return -ENOMEM;
1116 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1117 return -ENOMEM;
1118 return 0;
1119}
1120
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001121static int
1122xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
1123 unsigned short family)
1124{
1125 int err;
1126 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1127
1128 if (unlikely(afinfo == NULL))
1129 return -EINVAL;
1130 err = afinfo->get_saddr(local, remote);
1131 xfrm_policy_put_afinfo(afinfo);
1132 return err;
1133}
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135/* Resolve list of templates for the flow, given policy. */
1136
1137static int
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001138xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1139 struct xfrm_state **xfrm,
1140 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 int nx;
1143 int i, error;
1144 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1145 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001146 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1149 struct xfrm_state *x;
1150 xfrm_address_t *remote = daddr;
1151 xfrm_address_t *local = saddr;
1152 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1153
Joakim Koskela48b8d782007-07-26 00:08:42 -07001154 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1155 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 remote = &tmpl->id.daddr;
1157 local = &tmpl->saddr;
Miika Komu76b3f052006-11-30 16:40:43 -08001158 family = tmpl->encap_family;
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001159 if (xfrm_addr_any(local, family)) {
1160 error = xfrm_get_saddr(&tmp, remote, family);
1161 if (error)
1162 goto fail;
1163 local = &tmp;
1164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166
1167 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1168
1169 if (x && x->km.state == XFRM_STATE_VALID) {
1170 xfrm[nx++] = x;
1171 daddr = remote;
1172 saddr = local;
1173 continue;
1174 }
1175 if (x) {
1176 error = (x->km.state == XFRM_STATE_ERROR ?
1177 -EINVAL : -EAGAIN);
1178 xfrm_state_put(x);
1179 }
1180
1181 if (!tmpl->optional)
1182 goto fail;
1183 }
1184 return nx;
1185
1186fail:
1187 for (nx--; nx>=0; nx--)
1188 xfrm_state_put(xfrm[nx]);
1189 return error;
1190}
1191
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001192static int
1193xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1194 struct xfrm_state **xfrm,
1195 unsigned short family)
1196{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001197 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1198 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001199 int cnx = 0;
1200 int error;
1201 int ret;
1202 int i;
1203
1204 for (i = 0; i < npols; i++) {
1205 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1206 error = -ENOBUFS;
1207 goto fail;
1208 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001209
1210 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001211 if (ret < 0) {
1212 error = ret;
1213 goto fail;
1214 } else
1215 cnx += ret;
1216 }
1217
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001218 /* found states are sorted for outbound processing */
1219 if (npols > 1)
1220 xfrm_state_sort(xfrm, tpp, cnx, family);
1221
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001222 return cnx;
1223
1224 fail:
1225 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001226 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001227 return error;
1228
1229}
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231/* Check that the bundle accepts the flow and its components are
1232 * still valid.
1233 */
1234
1235static struct dst_entry *
1236xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1237{
1238 struct dst_entry *x;
1239 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1240 if (unlikely(afinfo == NULL))
1241 return ERR_PTR(-EINVAL);
1242 x = afinfo->find_bundle(fl, policy);
1243 xfrm_policy_put_afinfo(afinfo);
1244 return x;
1245}
1246
Herbert Xu25ee3282007-12-11 09:32:34 -08001247static inline int xfrm_get_tos(struct flowi *fl, int family)
1248{
1249 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1250 int tos;
1251
1252 if (!afinfo)
1253 return -EINVAL;
1254
1255 tos = afinfo->get_tos(fl);
1256
1257 xfrm_policy_put_afinfo(afinfo);
1258
1259 return tos;
1260}
1261
1262static inline struct xfrm_dst *xfrm_alloc_dst(int family)
1263{
1264 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1265 struct xfrm_dst *xdst;
1266
1267 if (!afinfo)
1268 return ERR_PTR(-EINVAL);
1269
1270 xdst = dst_alloc(afinfo->dst_ops) ?: ERR_PTR(-ENOBUFS);
1271
1272 xfrm_policy_put_afinfo(afinfo);
1273
1274 return xdst;
1275}
1276
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001277static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1278 int nfheader_len)
1279{
1280 struct xfrm_policy_afinfo *afinfo =
1281 xfrm_policy_get_afinfo(dst->ops->family);
1282 int err;
1283
1284 if (!afinfo)
1285 return -EINVAL;
1286
1287 err = afinfo->init_path(path, dst, nfheader_len);
1288
1289 xfrm_policy_put_afinfo(afinfo);
1290
1291 return err;
1292}
1293
Herbert Xu25ee3282007-12-11 09:32:34 -08001294static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev)
1295{
1296 struct xfrm_policy_afinfo *afinfo =
1297 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1298 int err;
1299
1300 if (!afinfo)
1301 return -EINVAL;
1302
1303 err = afinfo->fill_dst(xdst, dev);
1304
1305 xfrm_policy_put_afinfo(afinfo);
1306
1307 return err;
1308}
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1311 * all the metrics... Shortly, bundle a bundle.
1312 */
1313
Herbert Xu25ee3282007-12-11 09:32:34 -08001314static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1315 struct xfrm_state **xfrm, int nx,
1316 struct flowi *fl,
1317 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Herbert Xu25ee3282007-12-11 09:32:34 -08001319 unsigned long now = jiffies;
1320 struct net_device *dev;
1321 struct dst_entry *dst_prev = NULL;
1322 struct dst_entry *dst0 = NULL;
1323 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001325 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001326 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001327 int trailer_len = 0;
1328 int tos;
1329 int family = policy->selector.family;
1330
1331 tos = xfrm_get_tos(fl, family);
1332 err = tos;
1333 if (tos < 0)
1334 goto put_states;
1335
1336 dst_hold(dst);
1337
1338 for (; i < nx; i++) {
1339 struct xfrm_dst *xdst = xfrm_alloc_dst(family);
1340 struct dst_entry *dst1 = &xdst->u.dst;
1341
1342 err = PTR_ERR(xdst);
1343 if (IS_ERR(xdst)) {
1344 dst_release(dst);
1345 goto put_states;
1346 }
1347
1348 if (!dst_prev)
1349 dst0 = dst1;
1350 else {
1351 dst_prev->child = dst_clone(dst1);
1352 dst1->flags |= DST_NOHASH;
1353 }
1354
1355 xdst->route = dst;
1356 memcpy(&dst1->metrics, &dst->metrics, sizeof(dst->metrics));
1357
1358 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1359 family = xfrm[i]->props.family;
1360 dst = xfrm_dst_lookup(xfrm[i], tos, family);
1361 err = PTR_ERR(dst);
1362 if (IS_ERR(dst))
1363 goto put_states;
1364 } else
1365 dst_hold(dst);
1366
1367 dst1->xfrm = xfrm[i];
1368 xdst->genid = xfrm[i]->genid;
1369
1370 dst1->obsolete = -1;
1371 dst1->flags |= DST_HOST;
1372 dst1->lastuse = now;
1373
1374 dst1->input = dst_discard;
1375 dst1->output = xfrm[i]->outer_mode->afinfo->output;
1376
1377 dst1->next = dst_prev;
1378 dst_prev = dst1;
1379
1380 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001381 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1382 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001383 trailer_len += xfrm[i]->props.trailer_len;
1384 }
1385
1386 dst_prev->child = dst;
1387 dst0->path = dst;
1388
1389 err = -ENODEV;
1390 dev = dst->dev;
1391 if (!dev)
1392 goto free_dst;
1393
1394 /* Copy neighbout for reachability confirmation */
1395 dst0->neighbour = neigh_clone(dst->neighbour);
1396
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001397 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001398 xfrm_init_pmtu(dst_prev);
1399
1400 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1401 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1402
1403 err = xfrm_fill_dst(xdst, dev);
1404 if (err)
1405 goto free_dst;
1406
1407 dst_prev->header_len = header_len;
1408 dst_prev->trailer_len = trailer_len;
1409 header_len -= xdst->u.dst.xfrm->props.header_len;
1410 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1411 }
1412
1413out:
1414 return dst0;
1415
1416put_states:
1417 for (; i < nx; i++)
1418 xfrm_state_put(xfrm[i]);
1419free_dst:
1420 if (dst0)
1421 dst_free(dst0);
1422 dst0 = ERR_PTR(err);
1423 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001426static int inline
1427xfrm_dst_alloc_copy(void **target, void *src, int size)
1428{
1429 if (!*target) {
1430 *target = kmalloc(size, GFP_ATOMIC);
1431 if (!*target)
1432 return -ENOMEM;
1433 }
1434 memcpy(*target, src, size);
1435 return 0;
1436}
1437
1438static int inline
1439xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
1440{
1441#ifdef CONFIG_XFRM_SUB_POLICY
1442 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1443 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1444 sel, sizeof(*sel));
1445#else
1446 return 0;
1447#endif
1448}
1449
1450static int inline
1451xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
1452{
1453#ifdef CONFIG_XFRM_SUB_POLICY
1454 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1455 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1456#else
1457 return 0;
1458#endif
1459}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461static int stale_bundle(struct dst_entry *dst);
1462
1463/* Main function: finds/creates a bundle for given flow.
1464 *
1465 * At the moment we eat a raw IP route. Mostly to speed up lookups
1466 * on interfaces with disabled IPsec.
1467 */
David S. Miller14e50e52007-05-24 18:17:54 -07001468int __xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1469 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
1471 struct xfrm_policy *policy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001472 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1473 int npols;
1474 int pol_dead;
1475 int xfrm_nr;
1476 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1478 struct dst_entry *dst, *dst_orig = *dst_p;
1479 int nx = 0;
1480 int err;
1481 u32 genid;
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001482 u16 family;
Trent Jaegerdf718372005-12-13 23:12:27 -08001483 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485restart:
1486 genid = atomic_read(&flow_cache_genid);
1487 policy = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001488 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1489 pols[pi] = NULL;
1490 npols = 0;
1491 pol_dead = 0;
1492 xfrm_nr = 0;
1493
Thomas Graff7944fb2007-08-25 13:46:55 -07001494 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001495 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
Herbert Xu75b8c132007-12-11 04:38:08 -08001496 err = PTR_ERR(policy);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001497 if (IS_ERR(policy))
Herbert Xu75b8c132007-12-11 04:38:08 -08001498 goto dropdst;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 if (!policy) {
1502 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07001503 if ((dst_orig->flags & DST_NOXFRM) ||
1504 !xfrm_policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08001505 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001507 policy = flow_cache_lookup(fl, dst_orig->ops->family,
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001508 dir, xfrm_policy_lookup);
Herbert Xu75b8c132007-12-11 04:38:08 -08001509 err = PTR_ERR(policy);
James Morris134b0fc2006-10-05 15:42:27 -05001510 if (IS_ERR(policy))
Herbert Xu75b8c132007-12-11 04:38:08 -08001511 goto dropdst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
1513
1514 if (!policy)
Herbert Xu8b7817f2007-12-12 10:44:43 -08001515 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001517 family = dst_orig->ops->family;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001518 pols[0] = policy;
1519 npols ++;
1520 xfrm_nr += pols[0]->xfrm_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Herbert Xuaef21782007-12-13 09:30:59 -08001522 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001523 if ((flags & XFRM_LOOKUP_ICMP) && !(policy->flags & XFRM_POLICY_ICMP))
1524 goto error;
1525
1526 policy->curlft.use_time = get_seconds();
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 switch (policy->action) {
Herbert Xu5e5234f2007-11-30 00:50:31 +11001529 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 case XFRM_POLICY_BLOCK:
1531 /* Prohibit the flow */
Patrick McHardye1044112005-09-08 15:11:55 -07001532 err = -EPERM;
1533 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 case XFRM_POLICY_ALLOW:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001536#ifndef CONFIG_XFRM_SUB_POLICY
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (policy->xfrm_nr == 0) {
1538 /* Flow passes not transformed. */
1539 xfrm_pol_put(policy);
1540 return 0;
1541 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001542#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 /* Try to find matching bundle.
1545 *
1546 * LATER: help from flow cache. It is optional, this
1547 * is required only for output policy.
1548 */
1549 dst = xfrm_find_bundle(fl, policy, family);
1550 if (IS_ERR(dst)) {
Patrick McHardye1044112005-09-08 15:11:55 -07001551 err = PTR_ERR(dst);
1552 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
1554
1555 if (dst)
1556 break;
1557
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001558#ifdef CONFIG_XFRM_SUB_POLICY
1559 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1560 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1561 fl, family,
1562 XFRM_POLICY_OUT);
1563 if (pols[1]) {
James Morris134b0fc2006-10-05 15:42:27 -05001564 if (IS_ERR(pols[1])) {
1565 err = PTR_ERR(pols[1]);
1566 goto error;
1567 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001568 if (pols[1]->action == XFRM_POLICY_BLOCK) {
1569 err = -EPERM;
1570 goto error;
1571 }
1572 npols ++;
1573 xfrm_nr += pols[1]->xfrm_nr;
1574 }
1575 }
1576
1577 /*
1578 * Because neither flowi nor bundle information knows about
1579 * transformation template size. On more than one policy usage
1580 * we can realize whether all of them is bypass or not after
1581 * they are searched. See above not-transformed bypass
1582 * is surrounded by non-sub policy configuration, too.
1583 */
1584 if (xfrm_nr == 0) {
1585 /* Flow passes not transformed. */
1586 xfrm_pols_put(pols, npols);
1587 return 0;
1588 }
1589
1590#endif
1591 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 if (unlikely(nx<0)) {
1594 err = nx;
David S. Miller14e50e52007-05-24 18:17:54 -07001595 if (err == -EAGAIN && sysctl_xfrm_larval_drop) {
1596 /* EREMOTE tells the caller to generate
1597 * a one-shot blackhole route.
1598 */
1599 xfrm_pol_put(policy);
1600 return -EREMOTE;
1601 }
Herbert Xu815f4e52007-12-12 10:36:59 -08001602 if (err == -EAGAIN && (flags & XFRM_LOOKUP_WAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 DECLARE_WAITQUEUE(wait, current);
1604
1605 add_wait_queue(&km_waitq, &wait);
1606 set_current_state(TASK_INTERRUPTIBLE);
1607 schedule();
1608 set_current_state(TASK_RUNNING);
1609 remove_wait_queue(&km_waitq, &wait);
1610
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001611 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 if (nx == -EAGAIN && signal_pending(current)) {
1614 err = -ERESTART;
1615 goto error;
1616 }
1617 if (nx == -EAGAIN ||
1618 genid != atomic_read(&flow_cache_genid)) {
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001619 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 goto restart;
1621 }
1622 err = nx;
1623 }
1624 if (err < 0)
1625 goto error;
1626 }
1627 if (nx == 0) {
1628 /* Flow passes not transformed. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001629 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return 0;
1631 }
1632
Herbert Xu25ee3282007-12-11 09:32:34 -08001633 dst = xfrm_bundle_create(policy, xfrm, nx, fl, dst_orig);
1634 err = PTR_ERR(dst);
1635 if (IS_ERR(dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001638 for (pi = 0; pi < npols; pi++) {
1639 read_lock_bh(&pols[pi]->lock);
1640 pol_dead |= pols[pi]->dead;
1641 read_unlock_bh(&pols[pi]->lock);
1642 }
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 write_lock_bh(&policy->lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001645 if (unlikely(pol_dead || stale_bundle(dst))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 /* Wow! While we worked on resolving, this
1647 * policy has gone. Retry. It is not paranoia,
1648 * we just cannot enlist new bundle to dead object.
1649 * We can't enlist stable bundles either.
1650 */
1651 write_unlock_bh(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (dst)
1653 dst_free(dst);
Herbert Xu00de6512006-02-13 16:01:27 -08001654
1655 err = -EHOSTUNREACH;
1656 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001658
1659 if (npols > 1)
1660 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1661 else
1662 err = xfrm_dst_update_origin(dst, fl);
1663 if (unlikely(err)) {
1664 write_unlock_bh(&policy->lock);
1665 if (dst)
1666 dst_free(dst);
1667 goto error;
1668 }
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 dst->next = policy->bundles;
1671 policy->bundles = dst;
1672 dst_hold(dst);
1673 write_unlock_bh(&policy->lock);
1674 }
1675 *dst_p = dst;
1676 dst_release(dst_orig);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001677 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return 0;
1679
1680error:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001681 xfrm_pols_put(pols, npols);
Herbert Xu75b8c132007-12-11 04:38:08 -08001682dropdst:
1683 dst_release(dst_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 *dst_p = NULL;
1685 return err;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001686
1687nopol:
Herbert Xuaef21782007-12-13 09:30:59 -08001688 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001689 if (flags & XFRM_LOOKUP_ICMP)
1690 goto dropdst;
1691 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
David S. Miller14e50e52007-05-24 18:17:54 -07001693EXPORT_SYMBOL(__xfrm_lookup);
1694
1695int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1696 struct sock *sk, int flags)
1697{
1698 int err = __xfrm_lookup(dst_p, fl, sk, flags);
1699
1700 if (err == -EREMOTE) {
1701 dst_release(*dst_p);
1702 *dst_p = NULL;
1703 err = -EAGAIN;
1704 }
1705
1706 return err;
1707}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708EXPORT_SYMBOL(xfrm_lookup);
1709
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001710static inline int
1711xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1712{
1713 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001714
1715 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1716 return 0;
1717 x = skb->sp->xvec[idx];
1718 if (!x->type->reject)
1719 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07001720 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001721}
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723/* When skb is transformed back to its "native" form, we have to
1724 * check policy restrictions. At the moment we make this in maximally
1725 * stupid way. Shame on me. :-) Of course, connected sockets must
1726 * have policy cached at them.
1727 */
1728
1729static inline int
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001730xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 unsigned short family)
1732{
1733 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08001734 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return x->id.proto == tmpl->id.proto &&
1736 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1737 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1738 x->props.mode == tmpl->mode &&
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07001739 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1740 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001741 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1742 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001745/*
1746 * 0 or more than 0 is returned when validation is succeeded (either bypass
1747 * because of optional transport mode, or next index of the mathced secpath
1748 * state with the template.
1749 * -1 is returned when no matching template is found.
1750 * Otherwise "-2 - errored_index" is returned.
1751 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752static inline int
1753xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1754 unsigned short family)
1755{
1756 int idx = start;
1757
1758 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001759 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return start;
1761 } else
1762 start = -1;
1763 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001764 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001766 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1767 if (start == -1)
1768 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
1772 return start;
1773}
1774
Herbert Xud5422ef2007-12-12 10:44:16 -08001775int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
1776 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
1778 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001779 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 if (unlikely(afinfo == NULL))
1782 return -EAFNOSUPPORT;
1783
Herbert Xud5422ef2007-12-12 10:44:16 -08001784 afinfo->decode_session(skb, fl, reverse);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07001785 err = security_xfrm_decode_session(skb, &fl->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001787 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
Herbert Xud5422ef2007-12-12 10:44:16 -08001789EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001791static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
1793 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001794 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001795 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
1799
1800 return 0;
1801}
1802
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001803int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 unsigned short family)
1805{
1806 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001807 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1808 int npols = 0;
1809 int xfrm_nr;
1810 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08001811 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08001813 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001814 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Herbert Xud5422ef2007-12-12 10:44:16 -08001816 reverse = dir & ~XFRM_POLICY_MASK;
1817 dir &= XFRM_POLICY_MASK;
1818 fl_dir = policy_to_flow_dir(dir);
1819
1820 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return 0;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08001822 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 /* First, check used SA against their selectors. */
1825 if (skb->sp) {
1826 int i;
1827
1828 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001829 struct xfrm_state *x = skb->sp->xvec[i];
1830 if (!xfrm_selector_match(&x->sel, &fl, family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
1833 }
1834
1835 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001836 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001837 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001838 if (IS_ERR(pol))
1839 return 0;
1840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 if (!pol)
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001843 pol = flow_cache_lookup(&fl, family, fl_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 xfrm_policy_lookup);
1845
James Morris134b0fc2006-10-05 15:42:27 -05001846 if (IS_ERR(pol))
1847 return 0;
1848
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001849 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001850 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001851 xfrm_secpath_reject(xerr_idx, skb, &fl);
1852 return 0;
1853 }
1854 return 1;
1855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
James Morris9d729f72007-03-04 16:12:44 -08001857 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001859 pols[0] = pol;
1860 npols ++;
1861#ifdef CONFIG_XFRM_SUB_POLICY
1862 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1863 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1864 &fl, family,
1865 XFRM_POLICY_IN);
1866 if (pols[1]) {
James Morris134b0fc2006-10-05 15:42:27 -05001867 if (IS_ERR(pols[1]))
1868 return 0;
James Morris9d729f72007-03-04 16:12:44 -08001869 pols[1]->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001870 npols ++;
1871 }
1872 }
1873#endif
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (pol->action == XFRM_POLICY_ALLOW) {
1876 struct sec_path *sp;
1877 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001878 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001879 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001880 struct xfrm_tmpl **tpp = tp;
1881 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 int i, k;
1883
1884 if ((sp = skb->sp) == NULL)
1885 sp = &dummy;
1886
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001887 for (pi = 0; pi < npols; pi++) {
1888 if (pols[pi] != pol &&
1889 pols[pi]->action != XFRM_POLICY_ALLOW)
1890 goto reject;
1891 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
1892 goto reject_error;
1893 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1894 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1895 }
1896 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001897 if (npols > 1) {
1898 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1899 tpp = stp;
1900 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /* For each tunnel xfrm, find the first matching tmpl.
1903 * For each tmpl before that, find corresponding xfrm.
1904 * Order is _important_. Later we will implement
1905 * some barriers, but at the moment barriers
1906 * are implied between each two transformations.
1907 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001908 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1909 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001910 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001911 if (k < -1)
1912 /* "-2 - errored_index" returned */
1913 xerr_idx = -(2+k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
1917
James Morrisd1d9fac2006-09-01 00:32:12 -07001918 if (secpath_has_nontransport(sp, k, &xerr_idx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 goto reject;
1920
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001921 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return 1;
1923 }
1924
1925reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001926 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001927reject_error:
1928 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return 0;
1930}
1931EXPORT_SYMBOL(__xfrm_policy_check);
1932
1933int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1934{
1935 struct flowi fl;
1936
Patrick McHardy3e3850e2006-01-06 23:04:54 -08001937 if (xfrm_decode_session(skb, &fl, family) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 return 0;
1939
1940 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1941}
1942EXPORT_SYMBOL(__xfrm_route_forward);
1943
David S. Millerd49c73c2006-08-13 18:55:53 -07001944/* Optimize later using cookies and generation ids. */
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1947{
David S. Millerd49c73c2006-08-13 18:55:53 -07001948 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1949 * to "-1" to force all XFRM destinations to get validated by
1950 * dst_ops->check on every use. We do this because when a
1951 * normal route referenced by an XFRM dst is obsoleted we do
1952 * not go looking around for all parent referencing XFRM dsts
1953 * so that we can invalidate them. It is just too much work.
1954 * Instead we make the checks here on every use. For example:
1955 *
1956 * XFRM dst A --> IPv4 dst X
1957 *
1958 * X is the "xdst->route" of A (X is also the "dst->path" of A
1959 * in this example). If X is marked obsolete, "A" will not
1960 * notice. That's what we are validating here via the
1961 * stale_bundle() check.
1962 *
1963 * When a policy's bundle is pruned, we dst_free() the XFRM
1964 * dst which causes it's ->obsolete field to be set to a
1965 * positive non-zero integer. If an XFRM dst has been pruned
1966 * like this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08001967 */
David S. Millerd49c73c2006-08-13 18:55:53 -07001968 if (dst->obsolete < 0 && !stale_bundle(dst))
1969 return dst;
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return NULL;
1972}
1973
1974static int stale_bundle(struct dst_entry *dst)
1975{
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05001976 return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
Herbert Xuaabc9762005-05-03 16:27:10 -07001979void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -08001982 dst->dev = dev->nd_net->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07001983 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 dev_put(dev);
1985 }
1986}
Herbert Xuaabc9762005-05-03 16:27:10 -07001987EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989static void xfrm_link_failure(struct sk_buff *skb)
1990{
1991 /* Impossible. Such dst must be popped before reaches point of failure. */
1992 return;
1993}
1994
1995static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1996{
1997 if (dst) {
1998 if (dst->obsolete) {
1999 dst_release(dst);
2000 dst = NULL;
2001 }
2002 }
2003 return dst;
2004}
2005
David S. Miller2518c7c2006-08-24 04:45:07 -07002006static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
2007{
2008 struct dst_entry *dst, **dstp;
2009
2010 write_lock(&pol->lock);
2011 dstp = &pol->bundles;
2012 while ((dst=*dstp) != NULL) {
2013 if (func(dst)) {
2014 *dstp = dst->next;
2015 dst->next = *gc_list_p;
2016 *gc_list_p = dst;
2017 } else {
2018 dstp = &dst->next;
2019 }
2020 }
2021 write_unlock(&pol->lock);
2022}
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
2025{
David S. Miller2518c7c2006-08-24 04:45:07 -07002026 struct dst_entry *gc_list = NULL;
2027 int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07002030 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2031 struct xfrm_policy *pol;
2032 struct hlist_node *entry;
2033 struct hlist_head *table;
2034 int i;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002035
David S. Miller2518c7c2006-08-24 04:45:07 -07002036 hlist_for_each_entry(pol, entry,
2037 &xfrm_policy_inexact[dir], bydst)
2038 prune_one_bundle(pol, func, &gc_list);
2039
2040 table = xfrm_policy_bydst[dir].table;
2041 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
2042 hlist_for_each_entry(pol, entry, table + i, bydst)
2043 prune_one_bundle(pol, func, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
2045 }
2046 read_unlock_bh(&xfrm_policy_lock);
2047
2048 while (gc_list) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002049 struct dst_entry *dst = gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 gc_list = dst->next;
2051 dst_free(dst);
2052 }
2053}
2054
2055static int unused_bundle(struct dst_entry *dst)
2056{
2057 return !atomic_read(&dst->__refcnt);
2058}
2059
2060static void __xfrm_garbage_collect(void)
2061{
2062 xfrm_prune_bundles(unused_bundle);
2063}
2064
David S. Miller1c095392006-08-24 03:30:28 -07002065static int xfrm_flush_bundles(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
2067 xfrm_prune_bundles(stale_bundle);
2068 return 0;
2069}
2070
Herbert Xu25ee3282007-12-11 09:32:34 -08002071static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
2073 do {
2074 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2075 u32 pmtu, route_mtu_cached;
2076
2077 pmtu = dst_mtu(dst->child);
2078 xdst->child_mtu_cached = pmtu;
2079
2080 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2081
2082 route_mtu_cached = dst_mtu(xdst->route);
2083 xdst->route_mtu_cached = route_mtu_cached;
2084
2085 if (pmtu > route_mtu_cached)
2086 pmtu = route_mtu_cached;
2087
2088 dst->metrics[RTAX_MTU-1] = pmtu;
2089 } while ((dst = dst->next));
2090}
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092/* Check that the bundle accepts the flow and its components are
2093 * still valid.
2094 */
2095
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05002096int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
2097 struct flowi *fl, int family, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
2099 struct dst_entry *dst = &first->u.dst;
2100 struct xfrm_dst *last;
2101 u32 mtu;
2102
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002103 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 (dst->dev && !netif_running(dst->dev)))
2105 return 0;
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07002106#ifdef CONFIG_XFRM_SUB_POLICY
2107 if (fl) {
2108 if (first->origin && !flow_cache_uli_match(first->origin, fl))
2109 return 0;
2110 if (first->partner &&
2111 !xfrm_selector_match(first->partner, fl, family))
2112 return 0;
2113 }
2114#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 last = NULL;
2117
2118 do {
2119 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2120
2121 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
2122 return 0;
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06002123 if (fl && pol &&
2124 !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2127 return 0;
David S. Miller9d4a7062006-08-24 03:18:09 -07002128 if (xdst->genid != dst->xfrm->genid)
2129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Herbert Xu1bfcb102007-10-17 21:31:50 -07002131 if (strict && fl &&
Herbert Xu13996372007-10-17 21:35:51 -07002132 !(dst->xfrm->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07002133 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
2134 return 0;
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 mtu = dst_mtu(dst->child);
2137 if (xdst->child_mtu_cached != mtu) {
2138 last = xdst;
2139 xdst->child_mtu_cached = mtu;
2140 }
2141
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002142 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 return 0;
2144 mtu = dst_mtu(xdst->route);
2145 if (xdst->route_mtu_cached != mtu) {
2146 last = xdst;
2147 xdst->route_mtu_cached = mtu;
2148 }
2149
2150 dst = dst->child;
2151 } while (dst->xfrm);
2152
2153 if (likely(!last))
2154 return 1;
2155
2156 mtu = last->child_mtu_cached;
2157 for (;;) {
2158 dst = &last->u.dst;
2159
2160 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2161 if (mtu > last->route_mtu_cached)
2162 mtu = last->route_mtu_cached;
2163 dst->metrics[RTAX_MTU-1] = mtu;
2164
2165 if (last == first)
2166 break;
2167
Patrick McHardybd0bf072007-07-18 01:55:52 -07002168 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 last->child_mtu_cached = mtu;
2170 }
2171
2172 return 1;
2173}
2174
2175EXPORT_SYMBOL(xfrm_bundle_ok);
2176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2178{
2179 int err = 0;
2180 if (unlikely(afinfo == NULL))
2181 return -EINVAL;
2182 if (unlikely(afinfo->family >= NPROTO))
2183 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002184 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2186 err = -ENOBUFS;
2187 else {
2188 struct dst_ops *dst_ops = afinfo->dst_ops;
2189 if (likely(dst_ops->kmem_cachep == NULL))
2190 dst_ops->kmem_cachep = xfrm_dst_cache;
2191 if (likely(dst_ops->check == NULL))
2192 dst_ops->check = xfrm_dst_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 if (likely(dst_ops->negative_advice == NULL))
2194 dst_ops->negative_advice = xfrm_negative_advice;
2195 if (likely(dst_ops->link_failure == NULL))
2196 dst_ops->link_failure = xfrm_link_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (likely(afinfo->garbage_collect == NULL))
2198 afinfo->garbage_collect = __xfrm_garbage_collect;
2199 xfrm_policy_afinfo[afinfo->family] = afinfo;
2200 }
Ingo Molnare959d812006-04-28 15:32:29 -07002201 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 return err;
2203}
2204EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2205
2206int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2207{
2208 int err = 0;
2209 if (unlikely(afinfo == NULL))
2210 return -EINVAL;
2211 if (unlikely(afinfo->family >= NPROTO))
2212 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002213 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2215 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2216 err = -EINVAL;
2217 else {
2218 struct dst_ops *dst_ops = afinfo->dst_ops;
2219 xfrm_policy_afinfo[afinfo->family] = NULL;
2220 dst_ops->kmem_cachep = NULL;
2221 dst_ops->check = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 dst_ops->negative_advice = NULL;
2223 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 afinfo->garbage_collect = NULL;
2225 }
2226 }
Ingo Molnare959d812006-04-28 15:32:29 -07002227 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return err;
2229}
2230EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2231
2232static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
2233{
2234 struct xfrm_policy_afinfo *afinfo;
2235 if (unlikely(family >= NPROTO))
2236 return NULL;
2237 read_lock(&xfrm_policy_afinfo_lock);
2238 afinfo = xfrm_policy_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07002239 if (unlikely(!afinfo))
2240 read_unlock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return afinfo;
2242}
2243
2244static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2245{
Herbert Xu546be242006-05-27 23:03:58 -07002246 read_unlock(&xfrm_policy_afinfo_lock);
2247}
2248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2250{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002251 struct net_device *dev = ptr;
2252
2253 if (dev->nd_net != &init_net)
2254 return NOTIFY_DONE;
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 switch (event) {
2257 case NETDEV_DOWN:
2258 xfrm_flush_bundles();
2259 }
2260 return NOTIFY_DONE;
2261}
2262
2263static struct notifier_block xfrm_dev_notifier = {
2264 xfrm_dev_event,
2265 NULL,
2266 0
2267};
2268
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002269#ifdef CONFIG_XFRM_STATISTICS
2270static int __init xfrm_statistics_init(void)
2271{
2272 if (snmp_mib_init((void **)xfrm_statistics,
2273 sizeof(struct linux_xfrm_mib)) < 0)
2274 return -ENOMEM;
2275 return 0;
2276}
2277#endif
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279static void __init xfrm_policy_init(void)
2280{
David S. Miller2518c7c2006-08-24 04:45:07 -07002281 unsigned int hmask, sz;
2282 int dir;
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2285 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07002286 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002287 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
David S. Miller2518c7c2006-08-24 04:45:07 -07002289 hmask = 8 - 1;
2290 sz = (hmask+1) * sizeof(struct hlist_head);
2291
David S. Miller44e36b42006-08-24 04:50:50 -07002292 xfrm_policy_byidx = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002293 xfrm_idx_hmask = hmask;
2294 if (!xfrm_policy_byidx)
2295 panic("XFRM: failed to allocate byidx hash\n");
2296
2297 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2298 struct xfrm_policy_hash *htab;
2299
2300 INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
2301
2302 htab = &xfrm_policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002303 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002304 htab->hmask = hmask;
2305 if (!htab->table)
2306 panic("XFRM: failed to allocate bydst hash\n");
2307 }
2308
David Howellsc4028952006-11-22 14:57:56 +00002309 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 register_netdevice_notifier(&xfrm_dev_notifier);
2311}
2312
2313void __init xfrm_init(void)
2314{
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002315#ifdef CONFIG_XFRM_STATISTICS
2316 xfrm_statistics_init();
2317#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 xfrm_state_init();
2319 xfrm_policy_init();
2320 xfrm_input_init();
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002321#ifdef CONFIG_XFRM_STATISTICS
2322 xfrm_proc_init();
2323#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324}
2325
Joy Lattenab5f5e82007-09-17 11:51:22 -07002326#ifdef CONFIG_AUDITSYSCALL
2327static inline void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2328 struct audit_buffer *audit_buf)
2329{
Paul Moore875179f2007-12-01 23:27:18 +11002330 struct xfrm_sec_ctx *ctx = xp->security;
2331 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002332
Paul Moore875179f2007-12-01 23:27:18 +11002333 if (ctx)
2334 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2335 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2336
2337 switch(sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002338 case AF_INET:
Paul Moore875179f2007-12-01 23:27:18 +11002339 audit_log_format(audit_buf, " src=" NIPQUAD_FMT,
2340 NIPQUAD(sel->saddr.a4));
2341 if (sel->prefixlen_s != 32)
2342 audit_log_format(audit_buf, " src_prefixlen=%d",
2343 sel->prefixlen_s);
2344 audit_log_format(audit_buf, " dst=" NIPQUAD_FMT,
2345 NIPQUAD(sel->daddr.a4));
2346 if (sel->prefixlen_d != 32)
2347 audit_log_format(audit_buf, " dst_prefixlen=%d",
2348 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002349 break;
2350 case AF_INET6:
Paul Moore875179f2007-12-01 23:27:18 +11002351 audit_log_format(audit_buf, " src=" NIP6_FMT,
2352 NIP6(*(struct in6_addr *)sel->saddr.a6));
2353 if (sel->prefixlen_s != 128)
2354 audit_log_format(audit_buf, " src_prefixlen=%d",
2355 sel->prefixlen_s);
2356 audit_log_format(audit_buf, " dst=" NIP6_FMT,
2357 NIP6(*(struct in6_addr *)sel->daddr.a6));
2358 if (sel->prefixlen_d != 128)
2359 audit_log_format(audit_buf, " dst_prefixlen=%d",
2360 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002361 break;
2362 }
2363}
2364
2365void
2366xfrm_audit_policy_add(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
2367{
2368 struct audit_buffer *audit_buf;
2369 extern int audit_enabled;
2370
2371 if (audit_enabled == 0)
2372 return;
Paul Moore5951cab2007-12-20 00:00:45 -08002373 audit_buf = xfrm_audit_start(auid, sid);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002374 if (audit_buf == NULL)
2375 return;
2376 audit_log_format(audit_buf, " op=SPD-add res=%u", result);
2377 xfrm_audit_common_policyinfo(xp, audit_buf);
2378 audit_log_end(audit_buf);
2379}
2380EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
2381
2382void
2383xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
2384{
2385 struct audit_buffer *audit_buf;
2386 extern int audit_enabled;
2387
2388 if (audit_enabled == 0)
2389 return;
Paul Moore5951cab2007-12-20 00:00:45 -08002390 audit_buf = xfrm_audit_start(auid, sid);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002391 if (audit_buf == NULL)
2392 return;
2393 audit_log_format(audit_buf, " op=SPD-delete res=%u", result);
2394 xfrm_audit_common_policyinfo(xp, audit_buf);
2395 audit_log_end(audit_buf);
2396}
2397EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
2398#endif
2399
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002400#ifdef CONFIG_XFRM_MIGRATE
2401static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
2402 struct xfrm_selector *sel_tgt)
2403{
2404 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
2405 if (sel_tgt->family == sel_cmp->family &&
2406 xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002407 sel_cmp->family) == 0 &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002408 xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
2409 sel_cmp->family) == 0 &&
2410 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
2411 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
2412 return 1;
2413 }
2414 } else {
2415 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
2416 return 1;
2417 }
2418 }
2419 return 0;
2420}
2421
2422static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
2423 u8 dir, u8 type)
2424{
2425 struct xfrm_policy *pol, *ret = NULL;
2426 struct hlist_node *entry;
2427 struct hlist_head *chain;
2428 u32 priority = ~0U;
2429
2430 read_lock_bh(&xfrm_policy_lock);
2431 chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
2432 hlist_for_each_entry(pol, entry, chain, bydst) {
2433 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2434 pol->type == type) {
2435 ret = pol;
2436 priority = ret->priority;
2437 break;
2438 }
2439 }
2440 chain = &xfrm_policy_inexact[dir];
2441 hlist_for_each_entry(pol, entry, chain, bydst) {
2442 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2443 pol->type == type &&
2444 pol->priority < priority) {
2445 ret = pol;
2446 break;
2447 }
2448 }
2449
2450 if (ret)
2451 xfrm_pol_hold(ret);
2452
2453 read_unlock_bh(&xfrm_policy_lock);
2454
2455 return ret;
2456}
2457
2458static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
2459{
2460 int match = 0;
2461
2462 if (t->mode == m->mode && t->id.proto == m->proto &&
2463 (m->reqid == 0 || t->reqid == m->reqid)) {
2464 switch (t->mode) {
2465 case XFRM_MODE_TUNNEL:
2466 case XFRM_MODE_BEET:
2467 if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
2468 m->old_family) == 0 &&
2469 xfrm_addr_cmp(&t->saddr, &m->old_saddr,
2470 m->old_family) == 0) {
2471 match = 1;
2472 }
2473 break;
2474 case XFRM_MODE_TRANSPORT:
2475 /* in case of transport mode, template does not store
2476 any IP addresses, hence we just compare mode and
2477 protocol */
2478 match = 1;
2479 break;
2480 default:
2481 break;
2482 }
2483 }
2484 return match;
2485}
2486
2487/* update endpoint address(es) of template(s) */
2488static int xfrm_policy_migrate(struct xfrm_policy *pol,
2489 struct xfrm_migrate *m, int num_migrate)
2490{
2491 struct xfrm_migrate *mp;
2492 struct dst_entry *dst;
2493 int i, j, n = 0;
2494
2495 write_lock_bh(&pol->lock);
2496 if (unlikely(pol->dead)) {
2497 /* target policy has been deleted */
2498 write_unlock_bh(&pol->lock);
2499 return -ENOENT;
2500 }
2501
2502 for (i = 0; i < pol->xfrm_nr; i++) {
2503 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
2504 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
2505 continue;
2506 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07002507 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
2508 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002509 continue;
2510 /* update endpoints */
2511 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
2512 sizeof(pol->xfrm_vec[i].id.daddr));
2513 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
2514 sizeof(pol->xfrm_vec[i].saddr));
2515 pol->xfrm_vec[i].encap_family = mp->new_family;
2516 /* flush bundles */
2517 while ((dst = pol->bundles) != NULL) {
2518 pol->bundles = dst->next;
2519 dst_free(dst);
2520 }
2521 }
2522 }
2523
2524 write_unlock_bh(&pol->lock);
2525
2526 if (!n)
2527 return -ENODATA;
2528
2529 return 0;
2530}
2531
2532static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
2533{
2534 int i, j;
2535
2536 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
2537 return -EINVAL;
2538
2539 for (i = 0; i < num_migrate; i++) {
2540 if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
2541 m[i].old_family) == 0) &&
2542 (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
2543 m[i].old_family) == 0))
2544 return -EINVAL;
2545 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
2546 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
2547 return -EINVAL;
2548
2549 /* check if there is any duplicated entry */
2550 for (j = i + 1; j < num_migrate; j++) {
2551 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
2552 sizeof(m[i].old_daddr)) &&
2553 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
2554 sizeof(m[i].old_saddr)) &&
2555 m[i].proto == m[j].proto &&
2556 m[i].mode == m[j].mode &&
2557 m[i].reqid == m[j].reqid &&
2558 m[i].old_family == m[j].old_family)
2559 return -EINVAL;
2560 }
2561 }
2562
2563 return 0;
2564}
2565
2566int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
2567 struct xfrm_migrate *m, int num_migrate)
2568{
2569 int i, err, nx_cur = 0, nx_new = 0;
2570 struct xfrm_policy *pol = NULL;
2571 struct xfrm_state *x, *xc;
2572 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
2573 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
2574 struct xfrm_migrate *mp;
2575
2576 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
2577 goto out;
2578
2579 /* Stage 1 - find policy */
2580 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
2581 err = -ENOENT;
2582 goto out;
2583 }
2584
2585 /* Stage 2 - find and update state(s) */
2586 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
2587 if ((x = xfrm_migrate_state_find(mp))) {
2588 x_cur[nx_cur] = x;
2589 nx_cur++;
2590 if ((xc = xfrm_state_migrate(x, mp))) {
2591 x_new[nx_new] = xc;
2592 nx_new++;
2593 } else {
2594 err = -ENODATA;
2595 goto restore_state;
2596 }
2597 }
2598 }
2599
2600 /* Stage 3 - update policy */
2601 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
2602 goto restore_state;
2603
2604 /* Stage 4 - delete old state(s) */
2605 if (nx_cur) {
2606 xfrm_states_put(x_cur, nx_cur);
2607 xfrm_states_delete(x_cur, nx_cur);
2608 }
2609
2610 /* Stage 5 - announce */
2611 km_migrate(sel, dir, type, m, num_migrate);
2612
2613 xfrm_pol_put(pol);
2614
2615 return 0;
2616out:
2617 return err;
2618
2619restore_state:
2620 if (pol)
2621 xfrm_pol_put(pol);
2622 if (nx_cur)
2623 xfrm_states_put(x_cur, nx_cur);
2624 if (nx_new)
2625 xfrm_states_delete(x_new, nx_new);
2626
2627 return err;
2628}
David S. Millere610e672007-02-08 13:29:15 -08002629EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002630#endif