blob: abc3e39b115b8cfb7489ca9802022462fc76d37e [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>
Paul Moore68277ac2007-12-20 20:49:33 -080027#include <linux/audit.h>
Herbert Xu25ee3282007-12-11 09:32:34 -080028#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <net/xfrm.h>
30#include <net/ip.h>
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -080031#ifdef CONFIG_XFRM_STATISTICS
32#include <net/snmp.h>
33#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
David S. Miller44e36b42006-08-24 04:50:50 -070035#include "xfrm_hash.h"
36
David S. Milleraad0e0b2007-05-25 00:42:49 -070037int sysctl_xfrm_larval_drop __read_mostly;
David S. Miller14e50e52007-05-24 18:17:54 -070038
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -080039#ifdef CONFIG_XFRM_STATISTICS
40DEFINE_SNMP_STAT(struct linux_xfrm_mib, xfrm_statistics) __read_mostly;
41EXPORT_SYMBOL(xfrm_statistics);
42#endif
43
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080044DEFINE_MUTEX(xfrm_cfg_mutex);
45EXPORT_SYMBOL(xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47static DEFINE_RWLOCK(xfrm_policy_lock);
48
David S. Miller2518c7c2006-08-24 04:45:07 -070049unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
50EXPORT_SYMBOL(xfrm_policy_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
53static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
54
Christoph Lametere18b8902006-12-06 20:33:20 -080055static struct kmem_cache *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static struct work_struct xfrm_policy_gc_work;
David S. Miller2518c7c2006-08-24 04:45:07 -070058static HLIST_HEAD(xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
60
61static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
62static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
Herbert Xu25ee3282007-12-11 09:32:34 -080063static void xfrm_init_pmtu(struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Andrew Morton77681022006-11-08 22:46:26 -080065static inline int
66__xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
67{
68 return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
69 addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
70 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
71 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
72 (fl->proto == sel->proto || !sel->proto) &&
73 (fl->oif == sel->ifindex || !sel->ifindex);
74}
75
76static inline int
77__xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
78{
79 return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
80 addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
81 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
82 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
83 (fl->proto == sel->proto || !sel->proto) &&
84 (fl->oif == sel->ifindex || !sel->ifindex);
85}
86
87int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
88 unsigned short family)
89{
90 switch (family) {
91 case AF_INET:
92 return __xfrm4_selector_match(sel, fl);
93 case AF_INET6:
94 return __xfrm6_selector_match(sel, fl);
95 }
96 return 0;
97}
98
Herbert Xu25ee3282007-12-11 09:32:34 -080099static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
100 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800102 xfrm_address_t *saddr = &x->props.saddr;
103 xfrm_address_t *daddr = &x->id.daddr;
104 struct xfrm_policy_afinfo *afinfo;
105 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800107 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR)
108 saddr = x->coaddr;
109 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR)
110 daddr = x->coaddr;
111
Herbert Xu25ee3282007-12-11 09:32:34 -0800112 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800114 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800116 dst = afinfo->dst_lookup(tos, saddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 xfrm_policy_put_afinfo(afinfo);
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800118 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static inline unsigned long make_jiffies(long secs)
122{
123 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
124 return MAX_SCHEDULE_TIMEOUT-1;
125 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900126 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129static void xfrm_policy_timer(unsigned long data)
130{
131 struct xfrm_policy *xp = (struct xfrm_policy*)data;
James Morris9d729f72007-03-04 16:12:44 -0800132 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 long next = LONG_MAX;
134 int warn = 0;
135 int dir;
136
137 read_lock(&xp->lock);
138
139 if (xp->dead)
140 goto out;
141
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700142 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 if (xp->lft.hard_add_expires_seconds) {
145 long tmo = xp->lft.hard_add_expires_seconds +
146 xp->curlft.add_time - now;
147 if (tmo <= 0)
148 goto expired;
149 if (tmo < next)
150 next = tmo;
151 }
152 if (xp->lft.hard_use_expires_seconds) {
153 long tmo = xp->lft.hard_use_expires_seconds +
154 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
155 if (tmo <= 0)
156 goto expired;
157 if (tmo < next)
158 next = tmo;
159 }
160 if (xp->lft.soft_add_expires_seconds) {
161 long tmo = xp->lft.soft_add_expires_seconds +
162 xp->curlft.add_time - now;
163 if (tmo <= 0) {
164 warn = 1;
165 tmo = XFRM_KM_TIMEOUT;
166 }
167 if (tmo < next)
168 next = tmo;
169 }
170 if (xp->lft.soft_use_expires_seconds) {
171 long tmo = xp->lft.soft_use_expires_seconds +
172 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
173 if (tmo <= 0) {
174 warn = 1;
175 tmo = XFRM_KM_TIMEOUT;
176 }
177 if (tmo < next)
178 next = tmo;
179 }
180
181 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800182 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (next != LONG_MAX &&
184 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
185 xfrm_pol_hold(xp);
186
187out:
188 read_unlock(&xp->lock);
189 xfrm_pol_put(xp);
190 return;
191
192expired:
193 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700194 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800195 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 xfrm_pol_put(xp);
197}
198
199
200/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
201 * SPD calls.
202 */
203
Al Virodd0fc662005-10-07 07:46:04 +0100204struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 struct xfrm_policy *policy;
207
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700208 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 if (policy) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700211 INIT_HLIST_NODE(&policy->bydst);
212 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700214 atomic_set(&policy->refcnt, 1);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800215 setup_timer(&policy->timer, xfrm_policy_timer,
216 (unsigned long)policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218 return policy;
219}
220EXPORT_SYMBOL(xfrm_policy_alloc);
221
222/* Destroy xfrm_policy: descendant resources must be released to this moment. */
223
224void __xfrm_policy_destroy(struct xfrm_policy *policy)
225{
Kris Katterjohn09a62662006-01-08 22:24:28 -0800226 BUG_ON(!policy->dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Kris Katterjohn09a62662006-01-08 22:24:28 -0800228 BUG_ON(policy->bundles);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 if (del_timer(&policy->timer))
231 BUG();
232
Trent Jaegerdf718372005-12-13 23:12:27 -0800233 security_xfrm_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 kfree(policy);
235}
236EXPORT_SYMBOL(__xfrm_policy_destroy);
237
238static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
239{
240 struct dst_entry *dst;
241
242 while ((dst = policy->bundles) != NULL) {
243 policy->bundles = dst->next;
244 dst_free(dst);
245 }
246
247 if (del_timer(&policy->timer))
248 atomic_dec(&policy->refcnt);
249
250 if (atomic_read(&policy->refcnt) > 1)
251 flow_cache_flush();
252
253 xfrm_pol_put(policy);
254}
255
David Howellsc4028952006-11-22 14:57:56 +0000256static void xfrm_policy_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct xfrm_policy *policy;
David S. Miller2518c7c2006-08-24 04:45:07 -0700259 struct hlist_node *entry, *tmp;
260 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 spin_lock_bh(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700263 gc_list.first = xfrm_policy_gc_list.first;
264 INIT_HLIST_HEAD(&xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 spin_unlock_bh(&xfrm_policy_gc_lock);
266
David S. Miller2518c7c2006-08-24 04:45:07 -0700267 hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 xfrm_policy_gc_kill(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271/* Rule must be locked. Release descentant resources, announce
272 * entry dead. The rule must be unlinked from lists to the moment.
273 */
274
275static void xfrm_policy_kill(struct xfrm_policy *policy)
276{
277 int dead;
278
279 write_lock_bh(&policy->lock);
280 dead = policy->dead;
281 policy->dead = 1;
282 write_unlock_bh(&policy->lock);
283
284 if (unlikely(dead)) {
285 WARN_ON(1);
286 return;
287 }
288
289 spin_lock(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700290 hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 spin_unlock(&xfrm_policy_gc_lock);
292
293 schedule_work(&xfrm_policy_gc_work);
294}
295
David S. Miller2518c7c2006-08-24 04:45:07 -0700296struct xfrm_policy_hash {
297 struct hlist_head *table;
298 unsigned int hmask;
299};
300
301static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
302static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
303static struct hlist_head *xfrm_policy_byidx __read_mostly;
304static unsigned int xfrm_idx_hmask __read_mostly;
305static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
306
David S. Miller2518c7c2006-08-24 04:45:07 -0700307static inline unsigned int idx_hash(u32 index)
308{
309 return __idx_hash(index, xfrm_idx_hmask);
310}
311
David S. Miller2518c7c2006-08-24 04:45:07 -0700312static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
313{
314 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
315 unsigned int hash = __sel_hash(sel, family, hmask);
316
317 return (hash == hmask + 1 ?
318 &xfrm_policy_inexact[dir] :
319 xfrm_policy_bydst[dir].table + hash);
320}
321
322static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
323{
324 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
325 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
326
327 return xfrm_policy_bydst[dir].table + hash;
328}
329
David S. Miller2518c7c2006-08-24 04:45:07 -0700330static void xfrm_dst_hash_transfer(struct hlist_head *list,
331 struct hlist_head *ndsttable,
332 unsigned int nhashmask)
333{
334 struct hlist_node *entry, *tmp;
335 struct xfrm_policy *pol;
336
337 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
338 unsigned int h;
339
340 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
341 pol->family, nhashmask);
342 hlist_add_head(&pol->bydst, ndsttable+h);
343 }
344}
345
346static void xfrm_idx_hash_transfer(struct hlist_head *list,
347 struct hlist_head *nidxtable,
348 unsigned int nhashmask)
349{
350 struct hlist_node *entry, *tmp;
351 struct xfrm_policy *pol;
352
353 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
354 unsigned int h;
355
356 h = __idx_hash(pol->index, nhashmask);
357 hlist_add_head(&pol->byidx, nidxtable+h);
358 }
359}
360
361static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
362{
363 return ((old_hmask + 1) << 1) - 1;
364}
365
366static void xfrm_bydst_resize(int dir)
367{
368 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
369 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
370 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
371 struct hlist_head *odst = xfrm_policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700372 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700373 int i;
374
375 if (!ndst)
376 return;
377
378 write_lock_bh(&xfrm_policy_lock);
379
380 for (i = hmask; i >= 0; i--)
381 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
382
383 xfrm_policy_bydst[dir].table = ndst;
384 xfrm_policy_bydst[dir].hmask = nhashmask;
385
386 write_unlock_bh(&xfrm_policy_lock);
387
David S. Miller44e36b42006-08-24 04:50:50 -0700388 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700389}
390
391static void xfrm_byidx_resize(int total)
392{
393 unsigned int hmask = xfrm_idx_hmask;
394 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
395 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
396 struct hlist_head *oidx = xfrm_policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700397 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700398 int i;
399
400 if (!nidx)
401 return;
402
403 write_lock_bh(&xfrm_policy_lock);
404
405 for (i = hmask; i >= 0; i--)
406 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
407
408 xfrm_policy_byidx = nidx;
409 xfrm_idx_hmask = nhashmask;
410
411 write_unlock_bh(&xfrm_policy_lock);
412
David S. Miller44e36b42006-08-24 04:50:50 -0700413 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700414}
415
416static inline int xfrm_bydst_should_resize(int dir, int *total)
417{
418 unsigned int cnt = xfrm_policy_count[dir];
419 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
420
421 if (total)
422 *total += cnt;
423
424 if ((hmask + 1) < xfrm_policy_hashmax &&
425 cnt > hmask)
426 return 1;
427
428 return 0;
429}
430
431static inline int xfrm_byidx_should_resize(int total)
432{
433 unsigned int hmask = xfrm_idx_hmask;
434
435 if ((hmask + 1) < xfrm_policy_hashmax &&
436 total > hmask)
437 return 1;
438
439 return 0;
440}
441
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700442void xfrm_spd_getinfo(struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700443{
444 read_lock_bh(&xfrm_policy_lock);
445 si->incnt = xfrm_policy_count[XFRM_POLICY_IN];
446 si->outcnt = xfrm_policy_count[XFRM_POLICY_OUT];
447 si->fwdcnt = xfrm_policy_count[XFRM_POLICY_FWD];
448 si->inscnt = xfrm_policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
449 si->outscnt = xfrm_policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
450 si->fwdscnt = xfrm_policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
451 si->spdhcnt = xfrm_idx_hmask;
452 si->spdhmcnt = xfrm_policy_hashmax;
453 read_unlock_bh(&xfrm_policy_lock);
454}
455EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700456
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700457static DEFINE_MUTEX(hash_resize_mutex);
David Howellsc4028952006-11-22 14:57:56 +0000458static void xfrm_hash_resize(struct work_struct *__unused)
David S. Miller2518c7c2006-08-24 04:45:07 -0700459{
460 int dir, total;
461
462 mutex_lock(&hash_resize_mutex);
463
464 total = 0;
465 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
466 if (xfrm_bydst_should_resize(dir, &total))
467 xfrm_bydst_resize(dir);
468 }
469 if (xfrm_byidx_should_resize(total))
470 xfrm_byidx_resize(total);
471
472 mutex_unlock(&hash_resize_mutex);
473}
474
David Howellsc4028952006-11-22 14:57:56 +0000475static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477/* Generate new index... KAME seems to generate them ordered by cost
478 * of an absolute inpredictability of ordering of rules. This will not pass. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700479static u32 xfrm_gen_index(u8 type, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 static u32 idx_generator;
482
483 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700484 struct hlist_node *entry;
485 struct hlist_head *list;
486 struct xfrm_policy *p;
487 u32 idx;
488 int found;
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 idx = (idx_generator | dir);
491 idx_generator += 8;
492 if (idx == 0)
493 idx = 8;
David S. Miller2518c7c2006-08-24 04:45:07 -0700494 list = xfrm_policy_byidx + idx_hash(idx);
495 found = 0;
496 hlist_for_each_entry(p, entry, list, byidx) {
497 if (p->index == idx) {
498 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700502 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return idx;
504 }
505}
506
David S. Miller2518c7c2006-08-24 04:45:07 -0700507static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
508{
509 u32 *p1 = (u32 *) s1;
510 u32 *p2 = (u32 *) s2;
511 int len = sizeof(struct xfrm_selector) / sizeof(u32);
512 int i;
513
514 for (i = 0; i < len; i++) {
515 if (p1[i] != p2[i])
516 return 1;
517 }
518
519 return 0;
520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
523{
David S. Miller2518c7c2006-08-24 04:45:07 -0700524 struct xfrm_policy *pol;
525 struct xfrm_policy *delpol;
526 struct hlist_head *chain;
Herbert Xua6c7ab52007-01-16 16:52:02 -0800527 struct hlist_node *entry, *newpos;
David S. Miller9b78a822005-12-22 07:39:48 -0800528 struct dst_entry *gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700531 chain = policy_hash_bysel(&policy->selector, policy->family, dir);
532 delpol = NULL;
533 newpos = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700534 hlist_for_each_entry(pol, entry, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800535 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700536 !selector_cmp(&pol->selector, &policy->selector) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800537 xfrm_sec_ctx_match(pol->security, policy->security) &&
538 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (excl) {
540 write_unlock_bh(&xfrm_policy_lock);
541 return -EEXIST;
542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 delpol = pol;
544 if (policy->priority > pol->priority)
545 continue;
546 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800547 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 continue;
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (delpol)
551 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700554 hlist_add_after(newpos, &policy->bydst);
555 else
556 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 xfrm_pol_hold(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700558 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700560 if (delpol) {
561 hlist_del(&delpol->bydst);
562 hlist_del(&delpol->byidx);
563 xfrm_policy_count[dir]--;
564 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700565 policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700566 hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800567 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 policy->curlft.use_time = 0;
569 if (!mod_timer(&policy->timer, jiffies + HZ))
570 xfrm_pol_hold(policy);
571 write_unlock_bh(&xfrm_policy_lock);
572
David S. Miller9b78a822005-12-22 07:39:48 -0800573 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 xfrm_policy_kill(delpol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700575 else if (xfrm_bydst_should_resize(dir, NULL))
576 schedule_work(&xfrm_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800577
578 read_lock_bh(&xfrm_policy_lock);
579 gc_list = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700580 entry = &policy->bydst;
581 hlist_for_each_entry_continue(policy, entry, bydst) {
David S. Miller9b78a822005-12-22 07:39:48 -0800582 struct dst_entry *dst;
583
584 write_lock(&policy->lock);
585 dst = policy->bundles;
586 if (dst) {
587 struct dst_entry *tail = dst;
588 while (tail->next)
589 tail = tail->next;
590 tail->next = gc_list;
591 gc_list = dst;
592
593 policy->bundles = NULL;
594 }
595 write_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
David S. Miller9b78a822005-12-22 07:39:48 -0800597 read_unlock_bh(&xfrm_policy_lock);
598
599 while (gc_list) {
600 struct dst_entry *dst = gc_list;
601
602 gc_list = dst->next;
603 dst_free(dst);
604 }
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return 0;
607}
608EXPORT_SYMBOL(xfrm_policy_insert);
609
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700610struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
611 struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800612 struct xfrm_sec_ctx *ctx, int delete,
613 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
David S. Miller2518c7c2006-08-24 04:45:07 -0700615 struct xfrm_policy *pol, *ret;
616 struct hlist_head *chain;
617 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Eric Parisef41aaa2007-03-07 15:37:58 -0800619 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700621 chain = policy_hash_bysel(sel, sel->family, dir);
622 ret = NULL;
623 hlist_for_each_entry(pol, entry, chain, bydst) {
624 if (pol->type == type &&
625 !selector_cmp(sel, &pol->selector) &&
626 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700628 if (delete) {
Eric Parisef41aaa2007-03-07 15:37:58 -0800629 *err = security_xfrm_policy_delete(pol);
630 if (*err) {
631 write_unlock_bh(&xfrm_policy_lock);
632 return pol;
633 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700634 hlist_del(&pol->bydst);
635 hlist_del(&pol->byidx);
636 xfrm_policy_count[dir]--;
637 }
638 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 break;
640 }
641 }
642 write_unlock_bh(&xfrm_policy_lock);
643
David S. Miller2518c7c2006-08-24 04:45:07 -0700644 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700646 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700648 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
Trent Jaegerdf718372005-12-13 23:12:27 -0800650EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Eric Parisef41aaa2007-03-07 15:37:58 -0800652struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete,
653 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
David S. Miller2518c7c2006-08-24 04:45:07 -0700655 struct xfrm_policy *pol, *ret;
656 struct hlist_head *chain;
657 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Herbert Xub5505c62007-05-14 02:15:47 -0700659 *err = -ENOENT;
660 if (xfrm_policy_id2dir(id) != dir)
661 return NULL;
662
Eric Parisef41aaa2007-03-07 15:37:58 -0800663 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700665 chain = xfrm_policy_byidx + idx_hash(id);
666 ret = NULL;
667 hlist_for_each_entry(pol, entry, chain, byidx) {
668 if (pol->type == type && pol->index == id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700670 if (delete) {
Eric Parisef41aaa2007-03-07 15:37:58 -0800671 *err = security_xfrm_policy_delete(pol);
672 if (*err) {
673 write_unlock_bh(&xfrm_policy_lock);
674 return pol;
675 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700676 hlist_del(&pol->bydst);
677 hlist_del(&pol->byidx);
678 xfrm_policy_count[dir]--;
679 }
680 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 break;
682 }
683 }
684 write_unlock_bh(&xfrm_policy_lock);
685
David S. Miller2518c7c2006-08-24 04:45:07 -0700686 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700688 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700690 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692EXPORT_SYMBOL(xfrm_policy_byid);
693
Joy Latten4aa2e622007-06-04 19:05:57 -0400694#ifdef CONFIG_SECURITY_NETWORK_XFRM
695static inline int
696xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Joy Latten4aa2e622007-06-04 19:05:57 -0400698 int dir, err = 0;
699
700 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
701 struct xfrm_policy *pol;
702 struct hlist_node *entry;
703 int i;
704
705 hlist_for_each_entry(pol, entry,
706 &xfrm_policy_inexact[dir], bydst) {
707 if (pol->type != type)
708 continue;
709 err = security_xfrm_policy_delete(pol);
710 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700711 xfrm_audit_policy_delete(pol, 0,
712 audit_info->loginuid,
713 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400714 return err;
715 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900716 }
Joy Latten4aa2e622007-06-04 19:05:57 -0400717 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
718 hlist_for_each_entry(pol, entry,
719 xfrm_policy_bydst[dir].table + i,
720 bydst) {
721 if (pol->type != type)
722 continue;
723 err = security_xfrm_policy_delete(pol);
724 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700725 xfrm_audit_policy_delete(pol, 0,
726 audit_info->loginuid,
727 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400728 return err;
729 }
730 }
731 }
732 }
733 return err;
734}
735#else
736static inline int
737xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
738{
739 return 0;
740}
741#endif
742
743int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
744{
745 int dir, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 write_lock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400748
749 err = xfrm_policy_flush_secctx_check(type, audit_info);
750 if (err)
751 goto out;
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700754 struct xfrm_policy *pol;
755 struct hlist_node *entry;
David S. Millerae8c0572006-10-03 16:00:26 -0700756 int i, killed;
David S. Miller2518c7c2006-08-24 04:45:07 -0700757
David S. Millerae8c0572006-10-03 16:00:26 -0700758 killed = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700759 again1:
760 hlist_for_each_entry(pol, entry,
761 &xfrm_policy_inexact[dir], bydst) {
762 if (pol->type != type)
763 continue;
764 hlist_del(&pol->bydst);
765 hlist_del(&pol->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 write_unlock_bh(&xfrm_policy_lock);
767
Joy Lattenab5f5e82007-09-17 11:51:22 -0700768 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
769 audit_info->secid);
Joy Latten161a09e2006-11-27 13:11:54 -0600770
David S. Miller2518c7c2006-08-24 04:45:07 -0700771 xfrm_policy_kill(pol);
David S. Millerae8c0572006-10-03 16:00:26 -0700772 killed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700775 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700777
778 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
779 again2:
780 hlist_for_each_entry(pol, entry,
781 xfrm_policy_bydst[dir].table + i,
782 bydst) {
783 if (pol->type != type)
784 continue;
785 hlist_del(&pol->bydst);
786 hlist_del(&pol->byidx);
787 write_unlock_bh(&xfrm_policy_lock);
788
Joy Lattenab5f5e82007-09-17 11:51:22 -0700789 xfrm_audit_policy_delete(pol, 1,
790 audit_info->loginuid,
791 audit_info->secid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700792 xfrm_policy_kill(pol);
David S. Millerae8c0572006-10-03 16:00:26 -0700793 killed++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700794
795 write_lock_bh(&xfrm_policy_lock);
796 goto again2;
797 }
798 }
799
David S. Millerae8c0572006-10-03 16:00:26 -0700800 xfrm_policy_count[dir] -= killed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802 atomic_inc(&flow_cache_genid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400803out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 write_unlock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400805 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807EXPORT_SYMBOL(xfrm_policy_flush);
808
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700809int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 void *data)
811{
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800812 struct xfrm_policy *pol, *last = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700813 struct hlist_node *entry;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800814 int dir, last_dir = 0, count, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700817 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700820 struct hlist_head *table = xfrm_policy_bydst[dir].table;
821 int i;
822
823 hlist_for_each_entry(pol, entry,
824 &xfrm_policy_inexact[dir], bydst) {
825 if (pol->type != type)
826 continue;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800827 if (last) {
828 error = func(last, last_dir % XFRM_POLICY_MAX,
829 count, data);
830 if (error)
831 goto out;
832 }
833 last = pol;
834 last_dir = dir;
835 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700837 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
838 hlist_for_each_entry(pol, entry, table + i, bydst) {
839 if (pol->type != type)
840 continue;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800841 if (last) {
842 error = func(last, last_dir % XFRM_POLICY_MAX,
843 count, data);
844 if (error)
845 goto out;
846 }
847 last = pol;
848 last_dir = dir;
849 count++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700850 }
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800853 if (count == 0) {
854 error = -ENOENT;
855 goto out;
856 }
857 error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858out:
859 read_unlock_bh(&xfrm_policy_lock);
860 return error;
861}
862EXPORT_SYMBOL(xfrm_policy_walk);
863
James Morris134b0fc2006-10-05 15:42:27 -0500864/*
865 * Find policy to apply to this flow.
866 *
867 * Returns 0 if policy found, else an -errno.
868 */
David S. Miller2518c7c2006-08-24 04:45:07 -0700869static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
870 u8 type, u16 family, int dir)
871{
872 struct xfrm_selector *sel = &pol->selector;
James Morris134b0fc2006-10-05 15:42:27 -0500873 int match, ret = -ESRCH;
David S. Miller2518c7c2006-08-24 04:45:07 -0700874
875 if (pol->family != family ||
876 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500877 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700878
879 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500880 if (match)
881 ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700882
James Morris134b0fc2006-10-05 15:42:27 -0500883 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700884}
885
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700886static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
887 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
James Morris134b0fc2006-10-05 15:42:27 -0500889 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700890 struct xfrm_policy *pol, *ret;
891 xfrm_address_t *daddr, *saddr;
892 struct hlist_node *entry;
893 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700894 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700895
896 daddr = xfrm_flowi_daddr(fl, family);
897 saddr = xfrm_flowi_saddr(fl, family);
898 if (unlikely(!daddr || !saddr))
899 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700902 chain = policy_hash_direct(daddr, saddr, family, dir);
903 ret = NULL;
904 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500905 err = xfrm_policy_match(pol, fl, type, family, dir);
906 if (err) {
907 if (err == -ESRCH)
908 continue;
909 else {
910 ret = ERR_PTR(err);
911 goto fail;
912 }
913 } else {
David S. Milleracba48e2006-08-25 15:46:46 -0700914 ret = pol;
915 priority = ret->priority;
916 break;
917 }
918 }
919 chain = &xfrm_policy_inexact[dir];
920 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500921 err = xfrm_policy_match(pol, fl, type, family, dir);
922 if (err) {
923 if (err == -ESRCH)
924 continue;
925 else {
926 ret = ERR_PTR(err);
927 goto fail;
928 }
929 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700930 ret = pol;
931 break;
932 }
933 }
David S. Milleracba48e2006-08-25 15:46:46 -0700934 if (ret)
935 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -0500936fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700938
David S. Miller2518c7c2006-08-24 04:45:07 -0700939 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700940}
941
James Morris134b0fc2006-10-05 15:42:27 -0500942static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700943 void **objp, atomic_t **obj_refp)
944{
945 struct xfrm_policy *pol;
James Morris134b0fc2006-10-05 15:42:27 -0500946 int err = 0;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700947
948#ifdef CONFIG_XFRM_SUB_POLICY
949 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
James Morris134b0fc2006-10-05 15:42:27 -0500950 if (IS_ERR(pol)) {
951 err = PTR_ERR(pol);
952 pol = NULL;
953 }
954 if (pol || err)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700955 goto end;
956#endif
957 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
James Morris134b0fc2006-10-05 15:42:27 -0500958 if (IS_ERR(pol)) {
959 err = PTR_ERR(pol);
960 pol = NULL;
961 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700962#ifdef CONFIG_XFRM_SUB_POLICY
David S. Miller2518c7c2006-08-24 04:45:07 -0700963end:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700964#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if ((*objp = (void *) pol) != NULL)
966 *obj_refp = &pol->refcnt;
James Morris134b0fc2006-10-05 15:42:27 -0500967 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
Trent Jaegerdf718372005-12-13 23:12:27 -0800970static inline int policy_to_flow_dir(int dir)
971{
972 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900973 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
974 XFRM_POLICY_FWD == FLOW_DIR_FWD)
975 return dir;
976 switch (dir) {
977 default:
978 case XFRM_POLICY_IN:
979 return FLOW_DIR_IN;
980 case XFRM_POLICY_OUT:
981 return FLOW_DIR_OUT;
982 case XFRM_POLICY_FWD:
983 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700984 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800985}
986
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700987static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 struct xfrm_policy *pol;
990
991 read_lock_bh(&xfrm_policy_lock);
992 if ((pol = sk->sk_policy[dir]) != NULL) {
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900993 int match = xfrm_selector_match(&pol->selector, fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900995 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -0800996
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -0500997 if (match) {
998 err = security_xfrm_policy_lookup(pol, fl->secid,
999 policy_to_flow_dir(dir));
1000 if (!err)
1001 xfrm_pol_hold(pol);
1002 else if (err == -ESRCH)
1003 pol = NULL;
1004 else
1005 pol = ERR_PTR(err);
1006 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 pol = NULL;
1008 }
1009 read_unlock_bh(&xfrm_policy_lock);
1010 return pol;
1011}
1012
1013static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1014{
David S. Miller2518c7c2006-08-24 04:45:07 -07001015 struct hlist_head *chain = policy_hash_bysel(&pol->selector,
1016 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001017
David S. Miller2518c7c2006-08-24 04:45:07 -07001018 hlist_add_head(&pol->bydst, chain);
1019 hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
1020 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001022
1023 if (xfrm_bydst_should_resize(dir, NULL))
1024 schedule_work(&xfrm_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
1027static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1028 int dir)
1029{
David S. Miller2518c7c2006-08-24 04:45:07 -07001030 if (hlist_unhashed(&pol->bydst))
1031 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
David S. Miller2518c7c2006-08-24 04:45:07 -07001033 hlist_del(&pol->bydst);
1034 hlist_del(&pol->byidx);
1035 xfrm_policy_count[dir]--;
1036
1037 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
Herbert Xu4666faa2005-06-18 22:43:22 -07001040int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 write_lock_bh(&xfrm_policy_lock);
1043 pol = __xfrm_policy_unlink(pol, dir);
1044 write_unlock_bh(&xfrm_policy_lock);
1045 if (pol) {
1046 if (dir < XFRM_POLICY_MAX)
1047 atomic_inc(&flow_cache_genid);
1048 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001051 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
David S. Millera70fcb02006-03-20 19:18:52 -08001053EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1056{
1057 struct xfrm_policy *old_pol;
1058
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001059#ifdef CONFIG_XFRM_SUB_POLICY
1060 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1061 return -EINVAL;
1062#endif
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 write_lock_bh(&xfrm_policy_lock);
1065 old_pol = sk->sk_policy[dir];
1066 sk->sk_policy[dir] = pol;
1067 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001068 pol->curlft.add_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001069 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1071 }
1072 if (old_pol)
1073 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1074 write_unlock_bh(&xfrm_policy_lock);
1075
1076 if (old_pol) {
1077 xfrm_policy_kill(old_pol);
1078 }
1079 return 0;
1080}
1081
1082static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1083{
1084 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
1085
1086 if (newp) {
1087 newp->selector = old->selector;
Trent Jaegerdf718372005-12-13 23:12:27 -08001088 if (security_xfrm_policy_clone(old, newp)) {
1089 kfree(newp);
1090 return NULL; /* ENOMEM */
1091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 newp->lft = old->lft;
1093 newp->curlft = old->curlft;
1094 newp->action = old->action;
1095 newp->flags = old->flags;
1096 newp->xfrm_nr = old->xfrm_nr;
1097 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001098 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 memcpy(newp->xfrm_vec, old->xfrm_vec,
1100 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1101 write_lock_bh(&xfrm_policy_lock);
1102 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1103 write_unlock_bh(&xfrm_policy_lock);
1104 xfrm_pol_put(newp);
1105 }
1106 return newp;
1107}
1108
1109int __xfrm_sk_clone_policy(struct sock *sk)
1110{
1111 struct xfrm_policy *p0 = sk->sk_policy[0],
1112 *p1 = sk->sk_policy[1];
1113
1114 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1115 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1116 return -ENOMEM;
1117 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1118 return -ENOMEM;
1119 return 0;
1120}
1121
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001122static int
1123xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
1124 unsigned short family)
1125{
1126 int err;
1127 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1128
1129 if (unlikely(afinfo == NULL))
1130 return -EINVAL;
1131 err = afinfo->get_saddr(local, remote);
1132 xfrm_policy_put_afinfo(afinfo);
1133 return err;
1134}
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136/* Resolve list of templates for the flow, given policy. */
1137
1138static int
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001139xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1140 struct xfrm_state **xfrm,
1141 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
1143 int nx;
1144 int i, error;
1145 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1146 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001147 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1150 struct xfrm_state *x;
1151 xfrm_address_t *remote = daddr;
1152 xfrm_address_t *local = saddr;
1153 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1154
Joakim Koskela48b8d782007-07-26 00:08:42 -07001155 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1156 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 remote = &tmpl->id.daddr;
1158 local = &tmpl->saddr;
Miika Komu76b3f052006-11-30 16:40:43 -08001159 family = tmpl->encap_family;
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001160 if (xfrm_addr_any(local, family)) {
1161 error = xfrm_get_saddr(&tmp, remote, family);
1162 if (error)
1163 goto fail;
1164 local = &tmp;
1165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167
1168 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1169
1170 if (x && x->km.state == XFRM_STATE_VALID) {
1171 xfrm[nx++] = x;
1172 daddr = remote;
1173 saddr = local;
1174 continue;
1175 }
1176 if (x) {
1177 error = (x->km.state == XFRM_STATE_ERROR ?
1178 -EINVAL : -EAGAIN);
1179 xfrm_state_put(x);
1180 }
1181
1182 if (!tmpl->optional)
1183 goto fail;
1184 }
1185 return nx;
1186
1187fail:
1188 for (nx--; nx>=0; nx--)
1189 xfrm_state_put(xfrm[nx]);
1190 return error;
1191}
1192
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001193static int
1194xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1195 struct xfrm_state **xfrm,
1196 unsigned short family)
1197{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001198 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1199 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001200 int cnx = 0;
1201 int error;
1202 int ret;
1203 int i;
1204
1205 for (i = 0; i < npols; i++) {
1206 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1207 error = -ENOBUFS;
1208 goto fail;
1209 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001210
1211 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001212 if (ret < 0) {
1213 error = ret;
1214 goto fail;
1215 } else
1216 cnx += ret;
1217 }
1218
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001219 /* found states are sorted for outbound processing */
1220 if (npols > 1)
1221 xfrm_state_sort(xfrm, tpp, cnx, family);
1222
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001223 return cnx;
1224
1225 fail:
1226 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001227 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001228 return error;
1229
1230}
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232/* Check that the bundle accepts the flow and its components are
1233 * still valid.
1234 */
1235
1236static struct dst_entry *
1237xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1238{
1239 struct dst_entry *x;
1240 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1241 if (unlikely(afinfo == NULL))
1242 return ERR_PTR(-EINVAL);
1243 x = afinfo->find_bundle(fl, policy);
1244 xfrm_policy_put_afinfo(afinfo);
1245 return x;
1246}
1247
Herbert Xu25ee3282007-12-11 09:32:34 -08001248static inline int xfrm_get_tos(struct flowi *fl, int family)
1249{
1250 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1251 int tos;
1252
1253 if (!afinfo)
1254 return -EINVAL;
1255
1256 tos = afinfo->get_tos(fl);
1257
1258 xfrm_policy_put_afinfo(afinfo);
1259
1260 return tos;
1261}
1262
1263static inline struct xfrm_dst *xfrm_alloc_dst(int family)
1264{
1265 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1266 struct xfrm_dst *xdst;
1267
1268 if (!afinfo)
1269 return ERR_PTR(-EINVAL);
1270
1271 xdst = dst_alloc(afinfo->dst_ops) ?: ERR_PTR(-ENOBUFS);
1272
1273 xfrm_policy_put_afinfo(afinfo);
1274
1275 return xdst;
1276}
1277
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001278static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1279 int nfheader_len)
1280{
1281 struct xfrm_policy_afinfo *afinfo =
1282 xfrm_policy_get_afinfo(dst->ops->family);
1283 int err;
1284
1285 if (!afinfo)
1286 return -EINVAL;
1287
1288 err = afinfo->init_path(path, dst, nfheader_len);
1289
1290 xfrm_policy_put_afinfo(afinfo);
1291
1292 return err;
1293}
1294
Herbert Xu25ee3282007-12-11 09:32:34 -08001295static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev)
1296{
1297 struct xfrm_policy_afinfo *afinfo =
1298 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1299 int err;
1300
1301 if (!afinfo)
1302 return -EINVAL;
1303
1304 err = afinfo->fill_dst(xdst, dev);
1305
1306 xfrm_policy_put_afinfo(afinfo);
1307
1308 return err;
1309}
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1312 * all the metrics... Shortly, bundle a bundle.
1313 */
1314
Herbert Xu25ee3282007-12-11 09:32:34 -08001315static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1316 struct xfrm_state **xfrm, int nx,
1317 struct flowi *fl,
1318 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Herbert Xu25ee3282007-12-11 09:32:34 -08001320 unsigned long now = jiffies;
1321 struct net_device *dev;
1322 struct dst_entry *dst_prev = NULL;
1323 struct dst_entry *dst0 = NULL;
1324 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001326 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001327 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001328 int trailer_len = 0;
1329 int tos;
1330 int family = policy->selector.family;
1331
1332 tos = xfrm_get_tos(fl, family);
1333 err = tos;
1334 if (tos < 0)
1335 goto put_states;
1336
1337 dst_hold(dst);
1338
1339 for (; i < nx; i++) {
1340 struct xfrm_dst *xdst = xfrm_alloc_dst(family);
1341 struct dst_entry *dst1 = &xdst->u.dst;
1342
1343 err = PTR_ERR(xdst);
1344 if (IS_ERR(xdst)) {
1345 dst_release(dst);
1346 goto put_states;
1347 }
1348
1349 if (!dst_prev)
1350 dst0 = dst1;
1351 else {
1352 dst_prev->child = dst_clone(dst1);
1353 dst1->flags |= DST_NOHASH;
1354 }
1355
1356 xdst->route = dst;
1357 memcpy(&dst1->metrics, &dst->metrics, sizeof(dst->metrics));
1358
1359 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1360 family = xfrm[i]->props.family;
1361 dst = xfrm_dst_lookup(xfrm[i], tos, family);
1362 err = PTR_ERR(dst);
1363 if (IS_ERR(dst))
1364 goto put_states;
1365 } else
1366 dst_hold(dst);
1367
1368 dst1->xfrm = xfrm[i];
1369 xdst->genid = xfrm[i]->genid;
1370
1371 dst1->obsolete = -1;
1372 dst1->flags |= DST_HOST;
1373 dst1->lastuse = now;
1374
1375 dst1->input = dst_discard;
1376 dst1->output = xfrm[i]->outer_mode->afinfo->output;
1377
1378 dst1->next = dst_prev;
1379 dst_prev = dst1;
1380
1381 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001382 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1383 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001384 trailer_len += xfrm[i]->props.trailer_len;
1385 }
1386
1387 dst_prev->child = dst;
1388 dst0->path = dst;
1389
1390 err = -ENODEV;
1391 dev = dst->dev;
1392 if (!dev)
1393 goto free_dst;
1394
1395 /* Copy neighbout for reachability confirmation */
1396 dst0->neighbour = neigh_clone(dst->neighbour);
1397
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001398 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001399 xfrm_init_pmtu(dst_prev);
1400
1401 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1402 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1403
1404 err = xfrm_fill_dst(xdst, dev);
1405 if (err)
1406 goto free_dst;
1407
1408 dst_prev->header_len = header_len;
1409 dst_prev->trailer_len = trailer_len;
1410 header_len -= xdst->u.dst.xfrm->props.header_len;
1411 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1412 }
1413
1414out:
1415 return dst0;
1416
1417put_states:
1418 for (; i < nx; i++)
1419 xfrm_state_put(xfrm[i]);
1420free_dst:
1421 if (dst0)
1422 dst_free(dst0);
1423 dst0 = ERR_PTR(err);
1424 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001427static int inline
1428xfrm_dst_alloc_copy(void **target, void *src, int size)
1429{
1430 if (!*target) {
1431 *target = kmalloc(size, GFP_ATOMIC);
1432 if (!*target)
1433 return -ENOMEM;
1434 }
1435 memcpy(*target, src, size);
1436 return 0;
1437}
1438
1439static int inline
1440xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
1441{
1442#ifdef CONFIG_XFRM_SUB_POLICY
1443 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1444 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1445 sel, sizeof(*sel));
1446#else
1447 return 0;
1448#endif
1449}
1450
1451static int inline
1452xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
1453{
1454#ifdef CONFIG_XFRM_SUB_POLICY
1455 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1456 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1457#else
1458 return 0;
1459#endif
1460}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462static int stale_bundle(struct dst_entry *dst);
1463
1464/* Main function: finds/creates a bundle for given flow.
1465 *
1466 * At the moment we eat a raw IP route. Mostly to speed up lookups
1467 * on interfaces with disabled IPsec.
1468 */
David S. Miller14e50e52007-05-24 18:17:54 -07001469int __xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1470 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 struct xfrm_policy *policy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001473 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1474 int npols;
1475 int pol_dead;
1476 int xfrm_nr;
1477 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1479 struct dst_entry *dst, *dst_orig = *dst_p;
1480 int nx = 0;
1481 int err;
1482 u32 genid;
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001483 u16 family;
Trent Jaegerdf718372005-12-13 23:12:27 -08001484 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486restart:
1487 genid = atomic_read(&flow_cache_genid);
1488 policy = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001489 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1490 pols[pi] = NULL;
1491 npols = 0;
1492 pol_dead = 0;
1493 xfrm_nr = 0;
1494
Thomas Graff7944fb2007-08-25 13:46:55 -07001495 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001496 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
Herbert Xu75b8c132007-12-11 04:38:08 -08001497 err = PTR_ERR(policy);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001498 if (IS_ERR(policy)) {
1499 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLERROR);
Herbert Xu75b8c132007-12-11 04:38:08 -08001500 goto dropdst;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001501 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 if (!policy) {
1505 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07001506 if ((dst_orig->flags & DST_NOXFRM) ||
1507 !xfrm_policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08001508 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001510 policy = flow_cache_lookup(fl, dst_orig->ops->family,
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001511 dir, xfrm_policy_lookup);
Herbert Xu75b8c132007-12-11 04:38:08 -08001512 err = PTR_ERR(policy);
James Morris134b0fc2006-10-05 15:42:27 -05001513 if (IS_ERR(policy))
Herbert Xu75b8c132007-12-11 04:38:08 -08001514 goto dropdst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
1516
1517 if (!policy)
Herbert Xu8b7817f2007-12-12 10:44:43 -08001518 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001520 family = dst_orig->ops->family;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001521 pols[0] = policy;
1522 npols ++;
1523 xfrm_nr += pols[0]->xfrm_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Herbert Xuaef21782007-12-13 09:30:59 -08001525 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001526 if ((flags & XFRM_LOOKUP_ICMP) && !(policy->flags & XFRM_POLICY_ICMP))
1527 goto error;
1528
1529 policy->curlft.use_time = get_seconds();
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 switch (policy->action) {
Herbert Xu5e5234f2007-11-30 00:50:31 +11001532 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 case XFRM_POLICY_BLOCK:
1534 /* Prohibit the flow */
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001535 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye1044112005-09-08 15:11:55 -07001536 err = -EPERM;
1537 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 case XFRM_POLICY_ALLOW:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001540#ifndef CONFIG_XFRM_SUB_POLICY
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (policy->xfrm_nr == 0) {
1542 /* Flow passes not transformed. */
1543 xfrm_pol_put(policy);
1544 return 0;
1545 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001546#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 /* Try to find matching bundle.
1549 *
1550 * LATER: help from flow cache. It is optional, this
1551 * is required only for output policy.
1552 */
1553 dst = xfrm_find_bundle(fl, policy, family);
1554 if (IS_ERR(dst)) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001555 XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
Patrick McHardye1044112005-09-08 15:11:55 -07001556 err = PTR_ERR(dst);
1557 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559
1560 if (dst)
1561 break;
1562
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001563#ifdef CONFIG_XFRM_SUB_POLICY
1564 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1565 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1566 fl, family,
1567 XFRM_POLICY_OUT);
1568 if (pols[1]) {
James Morris134b0fc2006-10-05 15:42:27 -05001569 if (IS_ERR(pols[1])) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001570 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05001571 err = PTR_ERR(pols[1]);
1572 goto error;
1573 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001574 if (pols[1]->action == XFRM_POLICY_BLOCK) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001575 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001576 err = -EPERM;
1577 goto error;
1578 }
1579 npols ++;
1580 xfrm_nr += pols[1]->xfrm_nr;
1581 }
1582 }
1583
1584 /*
1585 * Because neither flowi nor bundle information knows about
1586 * transformation template size. On more than one policy usage
1587 * we can realize whether all of them is bypass or not after
1588 * they are searched. See above not-transformed bypass
1589 * is surrounded by non-sub policy configuration, too.
1590 */
1591 if (xfrm_nr == 0) {
1592 /* Flow passes not transformed. */
1593 xfrm_pols_put(pols, npols);
1594 return 0;
1595 }
1596
1597#endif
1598 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 if (unlikely(nx<0)) {
1601 err = nx;
David S. Miller14e50e52007-05-24 18:17:54 -07001602 if (err == -EAGAIN && sysctl_xfrm_larval_drop) {
1603 /* EREMOTE tells the caller to generate
1604 * a one-shot blackhole route.
1605 */
1606 xfrm_pol_put(policy);
1607 return -EREMOTE;
1608 }
Herbert Xu815f4e52007-12-12 10:36:59 -08001609 if (err == -EAGAIN && (flags & XFRM_LOOKUP_WAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 DECLARE_WAITQUEUE(wait, current);
1611
1612 add_wait_queue(&km_waitq, &wait);
1613 set_current_state(TASK_INTERRUPTIBLE);
1614 schedule();
1615 set_current_state(TASK_RUNNING);
1616 remove_wait_queue(&km_waitq, &wait);
1617
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001618 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 if (nx == -EAGAIN && signal_pending(current)) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001621 XFRM_INC_STATS(LINUX_MIB_XFRMOUTNOSTATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 err = -ERESTART;
1623 goto error;
1624 }
1625 if (nx == -EAGAIN ||
1626 genid != atomic_read(&flow_cache_genid)) {
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001627 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 goto restart;
1629 }
1630 err = nx;
1631 }
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001632 if (err < 0) {
1633 XFRM_INC_STATS(LINUX_MIB_XFRMOUTNOSTATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 goto error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637 if (nx == 0) {
1638 /* Flow passes not transformed. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001639 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return 0;
1641 }
1642
Herbert Xu25ee3282007-12-11 09:32:34 -08001643 dst = xfrm_bundle_create(policy, xfrm, nx, fl, dst_orig);
1644 err = PTR_ERR(dst);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001645 if (IS_ERR(dst)) {
1646 XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLEGENERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 goto error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001650 for (pi = 0; pi < npols; pi++) {
1651 read_lock_bh(&pols[pi]->lock);
1652 pol_dead |= pols[pi]->dead;
1653 read_unlock_bh(&pols[pi]->lock);
1654 }
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 write_lock_bh(&policy->lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001657 if (unlikely(pol_dead || stale_bundle(dst))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 /* Wow! While we worked on resolving, this
1659 * policy has gone. Retry. It is not paranoia,
1660 * we just cannot enlist new bundle to dead object.
1661 * We can't enlist stable bundles either.
1662 */
1663 write_unlock_bh(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (dst)
1665 dst_free(dst);
Herbert Xu00de6512006-02-13 16:01:27 -08001666
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001667 if (pol_dead)
1668 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLDEAD);
1669 else
1670 XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
Herbert Xu00de6512006-02-13 16:01:27 -08001671 err = -EHOSTUNREACH;
1672 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001674
1675 if (npols > 1)
1676 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1677 else
1678 err = xfrm_dst_update_origin(dst, fl);
1679 if (unlikely(err)) {
1680 write_unlock_bh(&policy->lock);
1681 if (dst)
1682 dst_free(dst);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001683 XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001684 goto error;
1685 }
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 dst->next = policy->bundles;
1688 policy->bundles = dst;
1689 dst_hold(dst);
1690 write_unlock_bh(&policy->lock);
1691 }
1692 *dst_p = dst;
1693 dst_release(dst_orig);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001694 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return 0;
1696
1697error:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001698 xfrm_pols_put(pols, npols);
Herbert Xu75b8c132007-12-11 04:38:08 -08001699dropdst:
1700 dst_release(dst_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 *dst_p = NULL;
1702 return err;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001703
1704nopol:
Herbert Xuaef21782007-12-13 09:30:59 -08001705 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001706 if (flags & XFRM_LOOKUP_ICMP)
1707 goto dropdst;
1708 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
David S. Miller14e50e52007-05-24 18:17:54 -07001710EXPORT_SYMBOL(__xfrm_lookup);
1711
1712int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1713 struct sock *sk, int flags)
1714{
1715 int err = __xfrm_lookup(dst_p, fl, sk, flags);
1716
1717 if (err == -EREMOTE) {
1718 dst_release(*dst_p);
1719 *dst_p = NULL;
1720 err = -EAGAIN;
1721 }
1722
1723 return err;
1724}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725EXPORT_SYMBOL(xfrm_lookup);
1726
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001727static inline int
1728xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1729{
1730 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001731
1732 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1733 return 0;
1734 x = skb->sp->xvec[idx];
1735 if (!x->type->reject)
1736 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07001737 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001738}
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740/* When skb is transformed back to its "native" form, we have to
1741 * check policy restrictions. At the moment we make this in maximally
1742 * stupid way. Shame on me. :-) Of course, connected sockets must
1743 * have policy cached at them.
1744 */
1745
1746static inline int
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001747xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 unsigned short family)
1749{
1750 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08001751 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return x->id.proto == tmpl->id.proto &&
1753 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1754 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1755 x->props.mode == tmpl->mode &&
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07001756 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1757 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001758 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1759 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001762/*
1763 * 0 or more than 0 is returned when validation is succeeded (either bypass
1764 * because of optional transport mode, or next index of the mathced secpath
1765 * state with the template.
1766 * -1 is returned when no matching template is found.
1767 * Otherwise "-2 - errored_index" is returned.
1768 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769static inline int
1770xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1771 unsigned short family)
1772{
1773 int idx = start;
1774
1775 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001776 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return start;
1778 } else
1779 start = -1;
1780 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001781 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001783 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1784 if (start == -1)
1785 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
1789 return start;
1790}
1791
Herbert Xud5422ef2007-12-12 10:44:16 -08001792int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
1793 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
1795 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001796 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 if (unlikely(afinfo == NULL))
1799 return -EAFNOSUPPORT;
1800
Herbert Xud5422ef2007-12-12 10:44:16 -08001801 afinfo->decode_session(skb, fl, reverse);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07001802 err = security_xfrm_decode_session(skb, &fl->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001804 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
Herbert Xud5422ef2007-12-12 10:44:16 -08001806EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001808static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
1810 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001811 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001812 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
1816
1817 return 0;
1818}
1819
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001820int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 unsigned short family)
1822{
1823 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001824 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1825 int npols = 0;
1826 int xfrm_nr;
1827 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08001828 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08001830 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001831 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Herbert Xud5422ef2007-12-12 10:44:16 -08001833 reverse = dir & ~XFRM_POLICY_MASK;
1834 dir &= XFRM_POLICY_MASK;
1835 fl_dir = policy_to_flow_dir(dir);
1836
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001837 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
1838 XFRM_INC_STATS(LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001840 }
1841
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08001842 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 /* First, check used SA against their selectors. */
1845 if (skb->sp) {
1846 int i;
1847
1848 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001849 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001850 if (!xfrm_selector_match(&x->sel, &fl, family)) {
1851 XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
1855 }
1856
1857 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001858 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001859 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001860 if (IS_ERR(pol)) {
1861 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001862 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001863 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 if (!pol)
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001867 pol = flow_cache_lookup(&fl, family, fl_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 xfrm_policy_lookup);
1869
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001870 if (IS_ERR(pol)) {
1871 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05001872 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001873 }
James Morris134b0fc2006-10-05 15:42:27 -05001874
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001875 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001876 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001877 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001878 XFRM_INC_STATS(LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001879 return 0;
1880 }
1881 return 1;
1882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
James Morris9d729f72007-03-04 16:12:44 -08001884 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001886 pols[0] = pol;
1887 npols ++;
1888#ifdef CONFIG_XFRM_SUB_POLICY
1889 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1890 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1891 &fl, family,
1892 XFRM_POLICY_IN);
1893 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001894 if (IS_ERR(pols[1])) {
1895 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05001896 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001897 }
James Morris9d729f72007-03-04 16:12:44 -08001898 pols[1]->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001899 npols ++;
1900 }
1901 }
1902#endif
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (pol->action == XFRM_POLICY_ALLOW) {
1905 struct sec_path *sp;
1906 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001907 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001908 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001909 struct xfrm_tmpl **tpp = tp;
1910 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 int i, k;
1912
1913 if ((sp = skb->sp) == NULL)
1914 sp = &dummy;
1915
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001916 for (pi = 0; pi < npols; pi++) {
1917 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001918 pols[pi]->action != XFRM_POLICY_ALLOW) {
1919 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001920 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001921 }
1922 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
1923 XFRM_INC_STATS(LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001924 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001925 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001926 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1927 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1928 }
1929 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001930 if (npols > 1) {
1931 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1932 tpp = stp;
1933 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 /* For each tunnel xfrm, find the first matching tmpl.
1936 * For each tmpl before that, find corresponding xfrm.
1937 * Order is _important_. Later we will implement
1938 * some barriers, but at the moment barriers
1939 * are implied between each two transformations.
1940 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001941 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1942 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001943 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001944 if (k < -1)
1945 /* "-2 - errored_index" returned */
1946 xerr_idx = -(2+k);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001947 XFRM_INC_STATS(LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001952 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
1953 XFRM_INC_STATS(LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001957 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return 1;
1959 }
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001960 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001963 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001964reject_error:
1965 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return 0;
1967}
1968EXPORT_SYMBOL(__xfrm_policy_check);
1969
1970int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1971{
1972 struct flowi fl;
1973
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001974 if (xfrm_decode_session(skb, &fl, family) < 0) {
1975 /* XXX: we should have something like FWDHDRERROR here. */
1976 XFRM_INC_STATS(LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1981}
1982EXPORT_SYMBOL(__xfrm_route_forward);
1983
David S. Millerd49c73c2006-08-13 18:55:53 -07001984/* Optimize later using cookies and generation ids. */
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1987{
David S. Millerd49c73c2006-08-13 18:55:53 -07001988 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1989 * to "-1" to force all XFRM destinations to get validated by
1990 * dst_ops->check on every use. We do this because when a
1991 * normal route referenced by an XFRM dst is obsoleted we do
1992 * not go looking around for all parent referencing XFRM dsts
1993 * so that we can invalidate them. It is just too much work.
1994 * Instead we make the checks here on every use. For example:
1995 *
1996 * XFRM dst A --> IPv4 dst X
1997 *
1998 * X is the "xdst->route" of A (X is also the "dst->path" of A
1999 * in this example). If X is marked obsolete, "A" will not
2000 * notice. That's what we are validating here via the
2001 * stale_bundle() check.
2002 *
2003 * When a policy's bundle is pruned, we dst_free() the XFRM
2004 * dst which causes it's ->obsolete field to be set to a
2005 * positive non-zero integer. If an XFRM dst has been pruned
2006 * like this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002007 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002008 if (dst->obsolete < 0 && !stale_bundle(dst))
2009 return dst;
2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 return NULL;
2012}
2013
2014static int stale_bundle(struct dst_entry *dst)
2015{
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05002016 return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
Herbert Xuaabc9762005-05-03 16:27:10 -07002019void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -08002022 dst->dev = dev->nd_net->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002023 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 dev_put(dev);
2025 }
2026}
Herbert Xuaabc9762005-05-03 16:27:10 -07002027EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029static void xfrm_link_failure(struct sk_buff *skb)
2030{
2031 /* Impossible. Such dst must be popped before reaches point of failure. */
2032 return;
2033}
2034
2035static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2036{
2037 if (dst) {
2038 if (dst->obsolete) {
2039 dst_release(dst);
2040 dst = NULL;
2041 }
2042 }
2043 return dst;
2044}
2045
David S. Miller2518c7c2006-08-24 04:45:07 -07002046static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
2047{
2048 struct dst_entry *dst, **dstp;
2049
2050 write_lock(&pol->lock);
2051 dstp = &pol->bundles;
2052 while ((dst=*dstp) != NULL) {
2053 if (func(dst)) {
2054 *dstp = dst->next;
2055 dst->next = *gc_list_p;
2056 *gc_list_p = dst;
2057 } else {
2058 dstp = &dst->next;
2059 }
2060 }
2061 write_unlock(&pol->lock);
2062}
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
2065{
David S. Miller2518c7c2006-08-24 04:45:07 -07002066 struct dst_entry *gc_list = NULL;
2067 int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07002070 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2071 struct xfrm_policy *pol;
2072 struct hlist_node *entry;
2073 struct hlist_head *table;
2074 int i;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002075
David S. Miller2518c7c2006-08-24 04:45:07 -07002076 hlist_for_each_entry(pol, entry,
2077 &xfrm_policy_inexact[dir], bydst)
2078 prune_one_bundle(pol, func, &gc_list);
2079
2080 table = xfrm_policy_bydst[dir].table;
2081 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
2082 hlist_for_each_entry(pol, entry, table + i, bydst)
2083 prune_one_bundle(pol, func, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 }
2085 }
2086 read_unlock_bh(&xfrm_policy_lock);
2087
2088 while (gc_list) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002089 struct dst_entry *dst = gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 gc_list = dst->next;
2091 dst_free(dst);
2092 }
2093}
2094
2095static int unused_bundle(struct dst_entry *dst)
2096{
2097 return !atomic_read(&dst->__refcnt);
2098}
2099
2100static void __xfrm_garbage_collect(void)
2101{
2102 xfrm_prune_bundles(unused_bundle);
2103}
2104
David S. Miller1c095392006-08-24 03:30:28 -07002105static int xfrm_flush_bundles(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
2107 xfrm_prune_bundles(stale_bundle);
2108 return 0;
2109}
2110
Herbert Xu25ee3282007-12-11 09:32:34 -08002111static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 do {
2114 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2115 u32 pmtu, route_mtu_cached;
2116
2117 pmtu = dst_mtu(dst->child);
2118 xdst->child_mtu_cached = pmtu;
2119
2120 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2121
2122 route_mtu_cached = dst_mtu(xdst->route);
2123 xdst->route_mtu_cached = route_mtu_cached;
2124
2125 if (pmtu > route_mtu_cached)
2126 pmtu = route_mtu_cached;
2127
2128 dst->metrics[RTAX_MTU-1] = pmtu;
2129 } while ((dst = dst->next));
2130}
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132/* Check that the bundle accepts the flow and its components are
2133 * still valid.
2134 */
2135
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05002136int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
2137 struct flowi *fl, int family, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
2139 struct dst_entry *dst = &first->u.dst;
2140 struct xfrm_dst *last;
2141 u32 mtu;
2142
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002143 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 (dst->dev && !netif_running(dst->dev)))
2145 return 0;
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07002146#ifdef CONFIG_XFRM_SUB_POLICY
2147 if (fl) {
2148 if (first->origin && !flow_cache_uli_match(first->origin, fl))
2149 return 0;
2150 if (first->partner &&
2151 !xfrm_selector_match(first->partner, fl, family))
2152 return 0;
2153 }
2154#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 last = NULL;
2157
2158 do {
2159 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2160
2161 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
2162 return 0;
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06002163 if (fl && pol &&
2164 !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2167 return 0;
David S. Miller9d4a7062006-08-24 03:18:09 -07002168 if (xdst->genid != dst->xfrm->genid)
2169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Herbert Xu1bfcb102007-10-17 21:31:50 -07002171 if (strict && fl &&
Herbert Xu13996372007-10-17 21:35:51 -07002172 !(dst->xfrm->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07002173 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
2174 return 0;
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 mtu = dst_mtu(dst->child);
2177 if (xdst->child_mtu_cached != mtu) {
2178 last = xdst;
2179 xdst->child_mtu_cached = mtu;
2180 }
2181
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002182 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return 0;
2184 mtu = dst_mtu(xdst->route);
2185 if (xdst->route_mtu_cached != mtu) {
2186 last = xdst;
2187 xdst->route_mtu_cached = mtu;
2188 }
2189
2190 dst = dst->child;
2191 } while (dst->xfrm);
2192
2193 if (likely(!last))
2194 return 1;
2195
2196 mtu = last->child_mtu_cached;
2197 for (;;) {
2198 dst = &last->u.dst;
2199
2200 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2201 if (mtu > last->route_mtu_cached)
2202 mtu = last->route_mtu_cached;
2203 dst->metrics[RTAX_MTU-1] = mtu;
2204
2205 if (last == first)
2206 break;
2207
Patrick McHardybd0bf072007-07-18 01:55:52 -07002208 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 last->child_mtu_cached = mtu;
2210 }
2211
2212 return 1;
2213}
2214
2215EXPORT_SYMBOL(xfrm_bundle_ok);
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2218{
2219 int err = 0;
2220 if (unlikely(afinfo == NULL))
2221 return -EINVAL;
2222 if (unlikely(afinfo->family >= NPROTO))
2223 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002224 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2226 err = -ENOBUFS;
2227 else {
2228 struct dst_ops *dst_ops = afinfo->dst_ops;
2229 if (likely(dst_ops->kmem_cachep == NULL))
2230 dst_ops->kmem_cachep = xfrm_dst_cache;
2231 if (likely(dst_ops->check == NULL))
2232 dst_ops->check = xfrm_dst_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (likely(dst_ops->negative_advice == NULL))
2234 dst_ops->negative_advice = xfrm_negative_advice;
2235 if (likely(dst_ops->link_failure == NULL))
2236 dst_ops->link_failure = xfrm_link_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (likely(afinfo->garbage_collect == NULL))
2238 afinfo->garbage_collect = __xfrm_garbage_collect;
2239 xfrm_policy_afinfo[afinfo->family] = afinfo;
2240 }
Ingo Molnare959d812006-04-28 15:32:29 -07002241 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 return err;
2243}
2244EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2245
2246int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2247{
2248 int err = 0;
2249 if (unlikely(afinfo == NULL))
2250 return -EINVAL;
2251 if (unlikely(afinfo->family >= NPROTO))
2252 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002253 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2255 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2256 err = -EINVAL;
2257 else {
2258 struct dst_ops *dst_ops = afinfo->dst_ops;
2259 xfrm_policy_afinfo[afinfo->family] = NULL;
2260 dst_ops->kmem_cachep = NULL;
2261 dst_ops->check = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 dst_ops->negative_advice = NULL;
2263 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 afinfo->garbage_collect = NULL;
2265 }
2266 }
Ingo Molnare959d812006-04-28 15:32:29 -07002267 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return err;
2269}
2270EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2271
2272static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
2273{
2274 struct xfrm_policy_afinfo *afinfo;
2275 if (unlikely(family >= NPROTO))
2276 return NULL;
2277 read_lock(&xfrm_policy_afinfo_lock);
2278 afinfo = xfrm_policy_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07002279 if (unlikely(!afinfo))
2280 read_unlock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 return afinfo;
2282}
2283
2284static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2285{
Herbert Xu546be242006-05-27 23:03:58 -07002286 read_unlock(&xfrm_policy_afinfo_lock);
2287}
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2290{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002291 struct net_device *dev = ptr;
2292
2293 if (dev->nd_net != &init_net)
2294 return NOTIFY_DONE;
2295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 switch (event) {
2297 case NETDEV_DOWN:
2298 xfrm_flush_bundles();
2299 }
2300 return NOTIFY_DONE;
2301}
2302
2303static struct notifier_block xfrm_dev_notifier = {
2304 xfrm_dev_event,
2305 NULL,
2306 0
2307};
2308
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002309#ifdef CONFIG_XFRM_STATISTICS
2310static int __init xfrm_statistics_init(void)
2311{
2312 if (snmp_mib_init((void **)xfrm_statistics,
2313 sizeof(struct linux_xfrm_mib)) < 0)
2314 return -ENOMEM;
2315 return 0;
2316}
2317#endif
2318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319static void __init xfrm_policy_init(void)
2320{
David S. Miller2518c7c2006-08-24 04:45:07 -07002321 unsigned int hmask, sz;
2322 int dir;
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2325 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07002326 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002327 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
David S. Miller2518c7c2006-08-24 04:45:07 -07002329 hmask = 8 - 1;
2330 sz = (hmask+1) * sizeof(struct hlist_head);
2331
David S. Miller44e36b42006-08-24 04:50:50 -07002332 xfrm_policy_byidx = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002333 xfrm_idx_hmask = hmask;
2334 if (!xfrm_policy_byidx)
2335 panic("XFRM: failed to allocate byidx hash\n");
2336
2337 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2338 struct xfrm_policy_hash *htab;
2339
2340 INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
2341
2342 htab = &xfrm_policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002343 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002344 htab->hmask = hmask;
2345 if (!htab->table)
2346 panic("XFRM: failed to allocate bydst hash\n");
2347 }
2348
David Howellsc4028952006-11-22 14:57:56 +00002349 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 register_netdevice_notifier(&xfrm_dev_notifier);
2351}
2352
2353void __init xfrm_init(void)
2354{
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002355#ifdef CONFIG_XFRM_STATISTICS
2356 xfrm_statistics_init();
2357#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 xfrm_state_init();
2359 xfrm_policy_init();
2360 xfrm_input_init();
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002361#ifdef CONFIG_XFRM_STATISTICS
2362 xfrm_proc_init();
2363#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
Joy Lattenab5f5e82007-09-17 11:51:22 -07002366#ifdef CONFIG_AUDITSYSCALL
2367static inline void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2368 struct audit_buffer *audit_buf)
2369{
Paul Moore875179f2007-12-01 23:27:18 +11002370 struct xfrm_sec_ctx *ctx = xp->security;
2371 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002372
Paul Moore875179f2007-12-01 23:27:18 +11002373 if (ctx)
2374 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2375 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2376
2377 switch(sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002378 case AF_INET:
Paul Moore875179f2007-12-01 23:27:18 +11002379 audit_log_format(audit_buf, " src=" NIPQUAD_FMT,
2380 NIPQUAD(sel->saddr.a4));
2381 if (sel->prefixlen_s != 32)
2382 audit_log_format(audit_buf, " src_prefixlen=%d",
2383 sel->prefixlen_s);
2384 audit_log_format(audit_buf, " dst=" NIPQUAD_FMT,
2385 NIPQUAD(sel->daddr.a4));
2386 if (sel->prefixlen_d != 32)
2387 audit_log_format(audit_buf, " dst_prefixlen=%d",
2388 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002389 break;
2390 case AF_INET6:
Paul Moore875179f2007-12-01 23:27:18 +11002391 audit_log_format(audit_buf, " src=" NIP6_FMT,
2392 NIP6(*(struct in6_addr *)sel->saddr.a6));
2393 if (sel->prefixlen_s != 128)
2394 audit_log_format(audit_buf, " src_prefixlen=%d",
2395 sel->prefixlen_s);
2396 audit_log_format(audit_buf, " dst=" NIP6_FMT,
2397 NIP6(*(struct in6_addr *)sel->daddr.a6));
2398 if (sel->prefixlen_d != 128)
2399 audit_log_format(audit_buf, " dst_prefixlen=%d",
2400 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002401 break;
2402 }
2403}
2404
Paul Moore68277ac2007-12-20 20:49:33 -08002405void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
2406 u32 auid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002407{
2408 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002409
2410 if (audit_enabled == 0)
2411 return;
Paul Moore68277ac2007-12-20 20:49:33 -08002412 audit_buf = xfrm_audit_start(auid, secid);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002413 if (audit_buf == NULL)
2414 return;
2415 audit_log_format(audit_buf, " op=SPD-add res=%u", result);
2416 xfrm_audit_common_policyinfo(xp, audit_buf);
2417 audit_log_end(audit_buf);
2418}
2419EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
2420
Paul Moore68277ac2007-12-20 20:49:33 -08002421void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
2422 u32 auid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002423{
2424 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002425
2426 if (audit_enabled == 0)
2427 return;
Paul Moore68277ac2007-12-20 20:49:33 -08002428 audit_buf = xfrm_audit_start(auid, secid);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002429 if (audit_buf == NULL)
2430 return;
2431 audit_log_format(audit_buf, " op=SPD-delete res=%u", result);
2432 xfrm_audit_common_policyinfo(xp, audit_buf);
2433 audit_log_end(audit_buf);
2434}
2435EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
2436#endif
2437
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002438#ifdef CONFIG_XFRM_MIGRATE
2439static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
2440 struct xfrm_selector *sel_tgt)
2441{
2442 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
2443 if (sel_tgt->family == sel_cmp->family &&
2444 xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002445 sel_cmp->family) == 0 &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002446 xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
2447 sel_cmp->family) == 0 &&
2448 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
2449 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
2450 return 1;
2451 }
2452 } else {
2453 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
2454 return 1;
2455 }
2456 }
2457 return 0;
2458}
2459
2460static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
2461 u8 dir, u8 type)
2462{
2463 struct xfrm_policy *pol, *ret = NULL;
2464 struct hlist_node *entry;
2465 struct hlist_head *chain;
2466 u32 priority = ~0U;
2467
2468 read_lock_bh(&xfrm_policy_lock);
2469 chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
2470 hlist_for_each_entry(pol, entry, chain, bydst) {
2471 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2472 pol->type == type) {
2473 ret = pol;
2474 priority = ret->priority;
2475 break;
2476 }
2477 }
2478 chain = &xfrm_policy_inexact[dir];
2479 hlist_for_each_entry(pol, entry, chain, bydst) {
2480 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2481 pol->type == type &&
2482 pol->priority < priority) {
2483 ret = pol;
2484 break;
2485 }
2486 }
2487
2488 if (ret)
2489 xfrm_pol_hold(ret);
2490
2491 read_unlock_bh(&xfrm_policy_lock);
2492
2493 return ret;
2494}
2495
2496static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
2497{
2498 int match = 0;
2499
2500 if (t->mode == m->mode && t->id.proto == m->proto &&
2501 (m->reqid == 0 || t->reqid == m->reqid)) {
2502 switch (t->mode) {
2503 case XFRM_MODE_TUNNEL:
2504 case XFRM_MODE_BEET:
2505 if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
2506 m->old_family) == 0 &&
2507 xfrm_addr_cmp(&t->saddr, &m->old_saddr,
2508 m->old_family) == 0) {
2509 match = 1;
2510 }
2511 break;
2512 case XFRM_MODE_TRANSPORT:
2513 /* in case of transport mode, template does not store
2514 any IP addresses, hence we just compare mode and
2515 protocol */
2516 match = 1;
2517 break;
2518 default:
2519 break;
2520 }
2521 }
2522 return match;
2523}
2524
2525/* update endpoint address(es) of template(s) */
2526static int xfrm_policy_migrate(struct xfrm_policy *pol,
2527 struct xfrm_migrate *m, int num_migrate)
2528{
2529 struct xfrm_migrate *mp;
2530 struct dst_entry *dst;
2531 int i, j, n = 0;
2532
2533 write_lock_bh(&pol->lock);
2534 if (unlikely(pol->dead)) {
2535 /* target policy has been deleted */
2536 write_unlock_bh(&pol->lock);
2537 return -ENOENT;
2538 }
2539
2540 for (i = 0; i < pol->xfrm_nr; i++) {
2541 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
2542 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
2543 continue;
2544 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07002545 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
2546 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002547 continue;
2548 /* update endpoints */
2549 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
2550 sizeof(pol->xfrm_vec[i].id.daddr));
2551 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
2552 sizeof(pol->xfrm_vec[i].saddr));
2553 pol->xfrm_vec[i].encap_family = mp->new_family;
2554 /* flush bundles */
2555 while ((dst = pol->bundles) != NULL) {
2556 pol->bundles = dst->next;
2557 dst_free(dst);
2558 }
2559 }
2560 }
2561
2562 write_unlock_bh(&pol->lock);
2563
2564 if (!n)
2565 return -ENODATA;
2566
2567 return 0;
2568}
2569
2570static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
2571{
2572 int i, j;
2573
2574 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
2575 return -EINVAL;
2576
2577 for (i = 0; i < num_migrate; i++) {
2578 if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
2579 m[i].old_family) == 0) &&
2580 (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
2581 m[i].old_family) == 0))
2582 return -EINVAL;
2583 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
2584 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
2585 return -EINVAL;
2586
2587 /* check if there is any duplicated entry */
2588 for (j = i + 1; j < num_migrate; j++) {
2589 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
2590 sizeof(m[i].old_daddr)) &&
2591 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
2592 sizeof(m[i].old_saddr)) &&
2593 m[i].proto == m[j].proto &&
2594 m[i].mode == m[j].mode &&
2595 m[i].reqid == m[j].reqid &&
2596 m[i].old_family == m[j].old_family)
2597 return -EINVAL;
2598 }
2599 }
2600
2601 return 0;
2602}
2603
2604int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
2605 struct xfrm_migrate *m, int num_migrate)
2606{
2607 int i, err, nx_cur = 0, nx_new = 0;
2608 struct xfrm_policy *pol = NULL;
2609 struct xfrm_state *x, *xc;
2610 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
2611 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
2612 struct xfrm_migrate *mp;
2613
2614 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
2615 goto out;
2616
2617 /* Stage 1 - find policy */
2618 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
2619 err = -ENOENT;
2620 goto out;
2621 }
2622
2623 /* Stage 2 - find and update state(s) */
2624 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
2625 if ((x = xfrm_migrate_state_find(mp))) {
2626 x_cur[nx_cur] = x;
2627 nx_cur++;
2628 if ((xc = xfrm_state_migrate(x, mp))) {
2629 x_new[nx_new] = xc;
2630 nx_new++;
2631 } else {
2632 err = -ENODATA;
2633 goto restore_state;
2634 }
2635 }
2636 }
2637
2638 /* Stage 3 - update policy */
2639 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
2640 goto restore_state;
2641
2642 /* Stage 4 - delete old state(s) */
2643 if (nx_cur) {
2644 xfrm_states_put(x_cur, nx_cur);
2645 xfrm_states_delete(x_cur, nx_cur);
2646 }
2647
2648 /* Stage 5 - announce */
2649 km_migrate(sel, dir, type, m, num_migrate);
2650
2651 xfrm_pol_put(pol);
2652
2653 return 0;
2654out:
2655 return err;
2656
2657restore_state:
2658 if (pol)
2659 xfrm_pol_put(pol);
2660 if (nx_cur)
2661 xfrm_states_put(x_cur, nx_cur);
2662 if (nx_new)
2663 xfrm_states_delete(x_new, nx_new);
2664
2665 return err;
2666}
David S. Millere610e672007-02-08 13:29:15 -08002667EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002668#endif