blob: 9fc4c315f6cd9702a223b5758d3f61e26c89808b [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
WANG Cong64c31b32008-01-07 22:34:29 -0800224void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
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}
WANG Cong64c31b32008-01-07 22:34:29 -0800236EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
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{
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800334 struct hlist_node *entry, *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700335 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800336 unsigned int h0 = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700337
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800338redo:
David S. Miller2518c7c2006-08-24 04:45:07 -0700339 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
340 unsigned int h;
341
342 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
343 pol->family, nhashmask);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800344 if (!entry0) {
345 hlist_del(entry);
346 hlist_add_head(&pol->bydst, ndsttable+h);
347 h0 = h;
348 } else {
349 if (h != h0)
350 continue;
351 hlist_del(entry);
352 hlist_add_after(entry0, &pol->bydst);
353 }
354 entry0 = entry;
355 }
356 if (!hlist_empty(list)) {
357 entry0 = NULL;
358 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700359 }
360}
361
362static void xfrm_idx_hash_transfer(struct hlist_head *list,
363 struct hlist_head *nidxtable,
364 unsigned int nhashmask)
365{
366 struct hlist_node *entry, *tmp;
367 struct xfrm_policy *pol;
368
369 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
370 unsigned int h;
371
372 h = __idx_hash(pol->index, nhashmask);
373 hlist_add_head(&pol->byidx, nidxtable+h);
374 }
375}
376
377static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
378{
379 return ((old_hmask + 1) << 1) - 1;
380}
381
382static void xfrm_bydst_resize(int dir)
383{
384 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
385 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
386 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
387 struct hlist_head *odst = xfrm_policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700388 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700389 int i;
390
391 if (!ndst)
392 return;
393
394 write_lock_bh(&xfrm_policy_lock);
395
396 for (i = hmask; i >= 0; i--)
397 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
398
399 xfrm_policy_bydst[dir].table = ndst;
400 xfrm_policy_bydst[dir].hmask = nhashmask;
401
402 write_unlock_bh(&xfrm_policy_lock);
403
David S. Miller44e36b42006-08-24 04:50:50 -0700404 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700405}
406
407static void xfrm_byidx_resize(int total)
408{
409 unsigned int hmask = xfrm_idx_hmask;
410 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
411 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
412 struct hlist_head *oidx = xfrm_policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700413 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700414 int i;
415
416 if (!nidx)
417 return;
418
419 write_lock_bh(&xfrm_policy_lock);
420
421 for (i = hmask; i >= 0; i--)
422 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
423
424 xfrm_policy_byidx = nidx;
425 xfrm_idx_hmask = nhashmask;
426
427 write_unlock_bh(&xfrm_policy_lock);
428
David S. Miller44e36b42006-08-24 04:50:50 -0700429 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700430}
431
432static inline int xfrm_bydst_should_resize(int dir, int *total)
433{
434 unsigned int cnt = xfrm_policy_count[dir];
435 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
436
437 if (total)
438 *total += cnt;
439
440 if ((hmask + 1) < xfrm_policy_hashmax &&
441 cnt > hmask)
442 return 1;
443
444 return 0;
445}
446
447static inline int xfrm_byidx_should_resize(int total)
448{
449 unsigned int hmask = xfrm_idx_hmask;
450
451 if ((hmask + 1) < xfrm_policy_hashmax &&
452 total > hmask)
453 return 1;
454
455 return 0;
456}
457
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700458void xfrm_spd_getinfo(struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700459{
460 read_lock_bh(&xfrm_policy_lock);
461 si->incnt = xfrm_policy_count[XFRM_POLICY_IN];
462 si->outcnt = xfrm_policy_count[XFRM_POLICY_OUT];
463 si->fwdcnt = xfrm_policy_count[XFRM_POLICY_FWD];
464 si->inscnt = xfrm_policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
465 si->outscnt = xfrm_policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
466 si->fwdscnt = xfrm_policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
467 si->spdhcnt = xfrm_idx_hmask;
468 si->spdhmcnt = xfrm_policy_hashmax;
469 read_unlock_bh(&xfrm_policy_lock);
470}
471EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700472
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700473static DEFINE_MUTEX(hash_resize_mutex);
David Howellsc4028952006-11-22 14:57:56 +0000474static void xfrm_hash_resize(struct work_struct *__unused)
David S. Miller2518c7c2006-08-24 04:45:07 -0700475{
476 int dir, total;
477
478 mutex_lock(&hash_resize_mutex);
479
480 total = 0;
481 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
482 if (xfrm_bydst_should_resize(dir, &total))
483 xfrm_bydst_resize(dir);
484 }
485 if (xfrm_byidx_should_resize(total))
486 xfrm_byidx_resize(total);
487
488 mutex_unlock(&hash_resize_mutex);
489}
490
David Howellsc4028952006-11-22 14:57:56 +0000491static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493/* Generate new index... KAME seems to generate them ordered by cost
494 * of an absolute inpredictability of ordering of rules. This will not pass. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700495static u32 xfrm_gen_index(u8 type, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 static u32 idx_generator;
498
499 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700500 struct hlist_node *entry;
501 struct hlist_head *list;
502 struct xfrm_policy *p;
503 u32 idx;
504 int found;
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 idx = (idx_generator | dir);
507 idx_generator += 8;
508 if (idx == 0)
509 idx = 8;
David S. Miller2518c7c2006-08-24 04:45:07 -0700510 list = xfrm_policy_byidx + idx_hash(idx);
511 found = 0;
512 hlist_for_each_entry(p, entry, list, byidx) {
513 if (p->index == idx) {
514 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700518 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return idx;
520 }
521}
522
David S. Miller2518c7c2006-08-24 04:45:07 -0700523static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
524{
525 u32 *p1 = (u32 *) s1;
526 u32 *p2 = (u32 *) s2;
527 int len = sizeof(struct xfrm_selector) / sizeof(u32);
528 int i;
529
530 for (i = 0; i < len; i++) {
531 if (p1[i] != p2[i])
532 return 1;
533 }
534
535 return 0;
536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
539{
David S. Miller2518c7c2006-08-24 04:45:07 -0700540 struct xfrm_policy *pol;
541 struct xfrm_policy *delpol;
542 struct hlist_head *chain;
Herbert Xua6c7ab52007-01-16 16:52:02 -0800543 struct hlist_node *entry, *newpos;
David S. Miller9b78a822005-12-22 07:39:48 -0800544 struct dst_entry *gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700547 chain = policy_hash_bysel(&policy->selector, policy->family, dir);
548 delpol = NULL;
549 newpos = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700550 hlist_for_each_entry(pol, entry, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800551 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700552 !selector_cmp(&pol->selector, &policy->selector) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800553 xfrm_sec_ctx_match(pol->security, policy->security) &&
554 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (excl) {
556 write_unlock_bh(&xfrm_policy_lock);
557 return -EEXIST;
558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 delpol = pol;
560 if (policy->priority > pol->priority)
561 continue;
562 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800563 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 continue;
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (delpol)
567 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700570 hlist_add_after(newpos, &policy->bydst);
571 else
572 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 xfrm_pol_hold(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700574 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700576 if (delpol) {
577 hlist_del(&delpol->bydst);
578 hlist_del(&delpol->byidx);
579 xfrm_policy_count[dir]--;
580 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700581 policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700582 hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800583 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 policy->curlft.use_time = 0;
585 if (!mod_timer(&policy->timer, jiffies + HZ))
586 xfrm_pol_hold(policy);
587 write_unlock_bh(&xfrm_policy_lock);
588
David S. Miller9b78a822005-12-22 07:39:48 -0800589 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 xfrm_policy_kill(delpol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700591 else if (xfrm_bydst_should_resize(dir, NULL))
592 schedule_work(&xfrm_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800593
594 read_lock_bh(&xfrm_policy_lock);
595 gc_list = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700596 entry = &policy->bydst;
597 hlist_for_each_entry_continue(policy, entry, bydst) {
David S. Miller9b78a822005-12-22 07:39:48 -0800598 struct dst_entry *dst;
599
600 write_lock(&policy->lock);
601 dst = policy->bundles;
602 if (dst) {
603 struct dst_entry *tail = dst;
604 while (tail->next)
605 tail = tail->next;
606 tail->next = gc_list;
607 gc_list = dst;
608
609 policy->bundles = NULL;
610 }
611 write_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
David S. Miller9b78a822005-12-22 07:39:48 -0800613 read_unlock_bh(&xfrm_policy_lock);
614
615 while (gc_list) {
616 struct dst_entry *dst = gc_list;
617
618 gc_list = dst->next;
619 dst_free(dst);
620 }
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 0;
623}
624EXPORT_SYMBOL(xfrm_policy_insert);
625
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700626struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
627 struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800628 struct xfrm_sec_ctx *ctx, int delete,
629 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
David S. Miller2518c7c2006-08-24 04:45:07 -0700631 struct xfrm_policy *pol, *ret;
632 struct hlist_head *chain;
633 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Eric Parisef41aaa2007-03-07 15:37:58 -0800635 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700637 chain = policy_hash_bysel(sel, sel->family, dir);
638 ret = NULL;
639 hlist_for_each_entry(pol, entry, chain, bydst) {
640 if (pol->type == type &&
641 !selector_cmp(sel, &pol->selector) &&
642 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700644 if (delete) {
Eric Parisef41aaa2007-03-07 15:37:58 -0800645 *err = security_xfrm_policy_delete(pol);
646 if (*err) {
647 write_unlock_bh(&xfrm_policy_lock);
648 return pol;
649 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700650 hlist_del(&pol->bydst);
651 hlist_del(&pol->byidx);
652 xfrm_policy_count[dir]--;
653 }
654 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 break;
656 }
657 }
658 write_unlock_bh(&xfrm_policy_lock);
659
David S. Miller2518c7c2006-08-24 04:45:07 -0700660 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700662 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700664 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
Trent Jaegerdf718372005-12-13 23:12:27 -0800666EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Eric Parisef41aaa2007-03-07 15:37:58 -0800668struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete,
669 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
David S. Miller2518c7c2006-08-24 04:45:07 -0700671 struct xfrm_policy *pol, *ret;
672 struct hlist_head *chain;
673 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Herbert Xub5505c62007-05-14 02:15:47 -0700675 *err = -ENOENT;
676 if (xfrm_policy_id2dir(id) != dir)
677 return NULL;
678
Eric Parisef41aaa2007-03-07 15:37:58 -0800679 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700681 chain = xfrm_policy_byidx + idx_hash(id);
682 ret = NULL;
683 hlist_for_each_entry(pol, entry, chain, byidx) {
684 if (pol->type == type && pol->index == id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700686 if (delete) {
Eric Parisef41aaa2007-03-07 15:37:58 -0800687 *err = security_xfrm_policy_delete(pol);
688 if (*err) {
689 write_unlock_bh(&xfrm_policy_lock);
690 return pol;
691 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700692 hlist_del(&pol->bydst);
693 hlist_del(&pol->byidx);
694 xfrm_policy_count[dir]--;
695 }
696 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 break;
698 }
699 }
700 write_unlock_bh(&xfrm_policy_lock);
701
David S. Miller2518c7c2006-08-24 04:45:07 -0700702 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700704 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700706 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708EXPORT_SYMBOL(xfrm_policy_byid);
709
Joy Latten4aa2e622007-06-04 19:05:57 -0400710#ifdef CONFIG_SECURITY_NETWORK_XFRM
711static inline int
712xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Joy Latten4aa2e622007-06-04 19:05:57 -0400714 int dir, err = 0;
715
716 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
717 struct xfrm_policy *pol;
718 struct hlist_node *entry;
719 int i;
720
721 hlist_for_each_entry(pol, entry,
722 &xfrm_policy_inexact[dir], bydst) {
723 if (pol->type != type)
724 continue;
725 err = security_xfrm_policy_delete(pol);
726 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700727 xfrm_audit_policy_delete(pol, 0,
728 audit_info->loginuid,
729 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400730 return err;
731 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900732 }
Joy Latten4aa2e622007-06-04 19:05:57 -0400733 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
734 hlist_for_each_entry(pol, entry,
735 xfrm_policy_bydst[dir].table + i,
736 bydst) {
737 if (pol->type != type)
738 continue;
739 err = security_xfrm_policy_delete(pol);
740 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700741 xfrm_audit_policy_delete(pol, 0,
742 audit_info->loginuid,
743 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400744 return err;
745 }
746 }
747 }
748 }
749 return err;
750}
751#else
752static inline int
753xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
754{
755 return 0;
756}
757#endif
758
759int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
760{
761 int dir, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 write_lock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400764
765 err = xfrm_policy_flush_secctx_check(type, audit_info);
766 if (err)
767 goto out;
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700770 struct xfrm_policy *pol;
771 struct hlist_node *entry;
David S. Millerae8c0572006-10-03 16:00:26 -0700772 int i, killed;
David S. Miller2518c7c2006-08-24 04:45:07 -0700773
David S. Millerae8c0572006-10-03 16:00:26 -0700774 killed = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700775 again1:
776 hlist_for_each_entry(pol, entry,
777 &xfrm_policy_inexact[dir], bydst) {
778 if (pol->type != type)
779 continue;
780 hlist_del(&pol->bydst);
781 hlist_del(&pol->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 write_unlock_bh(&xfrm_policy_lock);
783
Joy Lattenab5f5e82007-09-17 11:51:22 -0700784 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
785 audit_info->secid);
Joy Latten161a09e2006-11-27 13:11:54 -0600786
David S. Miller2518c7c2006-08-24 04:45:07 -0700787 xfrm_policy_kill(pol);
David S. Millerae8c0572006-10-03 16:00:26 -0700788 killed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700791 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700793
794 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
795 again2:
796 hlist_for_each_entry(pol, entry,
797 xfrm_policy_bydst[dir].table + i,
798 bydst) {
799 if (pol->type != type)
800 continue;
801 hlist_del(&pol->bydst);
802 hlist_del(&pol->byidx);
803 write_unlock_bh(&xfrm_policy_lock);
804
Joy Lattenab5f5e82007-09-17 11:51:22 -0700805 xfrm_audit_policy_delete(pol, 1,
806 audit_info->loginuid,
807 audit_info->secid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700808 xfrm_policy_kill(pol);
David S. Millerae8c0572006-10-03 16:00:26 -0700809 killed++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700810
811 write_lock_bh(&xfrm_policy_lock);
812 goto again2;
813 }
814 }
815
David S. Millerae8c0572006-10-03 16:00:26 -0700816 xfrm_policy_count[dir] -= killed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818 atomic_inc(&flow_cache_genid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400819out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 write_unlock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400821 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823EXPORT_SYMBOL(xfrm_policy_flush);
824
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700825int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 void *data)
827{
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800828 struct xfrm_policy *pol, *last = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700829 struct hlist_node *entry;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800830 int dir, last_dir = 0, count, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700833 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700836 struct hlist_head *table = xfrm_policy_bydst[dir].table;
837 int i;
838
839 hlist_for_each_entry(pol, entry,
840 &xfrm_policy_inexact[dir], bydst) {
841 if (pol->type != type)
842 continue;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800843 if (last) {
844 error = func(last, last_dir % XFRM_POLICY_MAX,
845 count, data);
846 if (error)
847 goto out;
848 }
849 last = pol;
850 last_dir = dir;
851 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700853 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
854 hlist_for_each_entry(pol, entry, table + i, bydst) {
855 if (pol->type != type)
856 continue;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800857 if (last) {
858 error = func(last, last_dir % XFRM_POLICY_MAX,
859 count, data);
860 if (error)
861 goto out;
862 }
863 last = pol;
864 last_dir = dir;
865 count++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700866 }
867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800869 if (count == 0) {
870 error = -ENOENT;
871 goto out;
872 }
873 error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874out:
875 read_unlock_bh(&xfrm_policy_lock);
876 return error;
877}
878EXPORT_SYMBOL(xfrm_policy_walk);
879
James Morris134b0fc2006-10-05 15:42:27 -0500880/*
881 * Find policy to apply to this flow.
882 *
883 * Returns 0 if policy found, else an -errno.
884 */
David S. Miller2518c7c2006-08-24 04:45:07 -0700885static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
886 u8 type, u16 family, int dir)
887{
888 struct xfrm_selector *sel = &pol->selector;
James Morris134b0fc2006-10-05 15:42:27 -0500889 int match, ret = -ESRCH;
David S. Miller2518c7c2006-08-24 04:45:07 -0700890
891 if (pol->family != family ||
892 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500893 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700894
895 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500896 if (match)
897 ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700898
James Morris134b0fc2006-10-05 15:42:27 -0500899 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700900}
901
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700902static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
903 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
James Morris134b0fc2006-10-05 15:42:27 -0500905 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700906 struct xfrm_policy *pol, *ret;
907 xfrm_address_t *daddr, *saddr;
908 struct hlist_node *entry;
909 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700910 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700911
912 daddr = xfrm_flowi_daddr(fl, family);
913 saddr = xfrm_flowi_saddr(fl, family);
914 if (unlikely(!daddr || !saddr))
915 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700918 chain = policy_hash_direct(daddr, saddr, family, dir);
919 ret = NULL;
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 {
David S. Milleracba48e2006-08-25 15:46:46 -0700930 ret = pol;
931 priority = ret->priority;
932 break;
933 }
934 }
935 chain = &xfrm_policy_inexact[dir];
936 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500937 err = xfrm_policy_match(pol, fl, type, family, dir);
938 if (err) {
939 if (err == -ESRCH)
940 continue;
941 else {
942 ret = ERR_PTR(err);
943 goto fail;
944 }
945 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700946 ret = pol;
947 break;
948 }
949 }
David S. Milleracba48e2006-08-25 15:46:46 -0700950 if (ret)
951 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -0500952fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700954
David S. Miller2518c7c2006-08-24 04:45:07 -0700955 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700956}
957
James Morris134b0fc2006-10-05 15:42:27 -0500958static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700959 void **objp, atomic_t **obj_refp)
960{
961 struct xfrm_policy *pol;
James Morris134b0fc2006-10-05 15:42:27 -0500962 int err = 0;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700963
964#ifdef CONFIG_XFRM_SUB_POLICY
965 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
James Morris134b0fc2006-10-05 15:42:27 -0500966 if (IS_ERR(pol)) {
967 err = PTR_ERR(pol);
968 pol = NULL;
969 }
970 if (pol || err)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700971 goto end;
972#endif
973 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
James Morris134b0fc2006-10-05 15:42:27 -0500974 if (IS_ERR(pol)) {
975 err = PTR_ERR(pol);
976 pol = NULL;
977 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700978#ifdef CONFIG_XFRM_SUB_POLICY
David S. Miller2518c7c2006-08-24 04:45:07 -0700979end:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700980#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if ((*objp = (void *) pol) != NULL)
982 *obj_refp = &pol->refcnt;
James Morris134b0fc2006-10-05 15:42:27 -0500983 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Trent Jaegerdf718372005-12-13 23:12:27 -0800986static inline int policy_to_flow_dir(int dir)
987{
988 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900989 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
990 XFRM_POLICY_FWD == FLOW_DIR_FWD)
991 return dir;
992 switch (dir) {
993 default:
994 case XFRM_POLICY_IN:
995 return FLOW_DIR_IN;
996 case XFRM_POLICY_OUT:
997 return FLOW_DIR_OUT;
998 case XFRM_POLICY_FWD:
999 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001000 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001001}
1002
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001003static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 struct xfrm_policy *pol;
1006
1007 read_lock_bh(&xfrm_policy_lock);
1008 if ((pol = sk->sk_policy[dir]) != NULL) {
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001009 int match = xfrm_selector_match(&pol->selector, fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001011 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001012
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001013 if (match) {
1014 err = security_xfrm_policy_lookup(pol, fl->secid,
1015 policy_to_flow_dir(dir));
1016 if (!err)
1017 xfrm_pol_hold(pol);
1018 else if (err == -ESRCH)
1019 pol = NULL;
1020 else
1021 pol = ERR_PTR(err);
1022 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 pol = NULL;
1024 }
1025 read_unlock_bh(&xfrm_policy_lock);
1026 return pol;
1027}
1028
1029static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1030{
David S. Miller2518c7c2006-08-24 04:45:07 -07001031 struct hlist_head *chain = policy_hash_bysel(&pol->selector,
1032 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001033
David S. Miller2518c7c2006-08-24 04:45:07 -07001034 hlist_add_head(&pol->bydst, chain);
1035 hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
1036 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001038
1039 if (xfrm_bydst_should_resize(dir, NULL))
1040 schedule_work(&xfrm_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
1043static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1044 int dir)
1045{
David S. Miller2518c7c2006-08-24 04:45:07 -07001046 if (hlist_unhashed(&pol->bydst))
1047 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
David S. Miller2518c7c2006-08-24 04:45:07 -07001049 hlist_del(&pol->bydst);
1050 hlist_del(&pol->byidx);
1051 xfrm_policy_count[dir]--;
1052
1053 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
Herbert Xu4666faa2005-06-18 22:43:22 -07001056int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 write_lock_bh(&xfrm_policy_lock);
1059 pol = __xfrm_policy_unlink(pol, dir);
1060 write_unlock_bh(&xfrm_policy_lock);
1061 if (pol) {
1062 if (dir < XFRM_POLICY_MAX)
1063 atomic_inc(&flow_cache_genid);
1064 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001065 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001067 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
David S. Millera70fcb02006-03-20 19:18:52 -08001069EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1072{
1073 struct xfrm_policy *old_pol;
1074
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001075#ifdef CONFIG_XFRM_SUB_POLICY
1076 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1077 return -EINVAL;
1078#endif
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 write_lock_bh(&xfrm_policy_lock);
1081 old_pol = sk->sk_policy[dir];
1082 sk->sk_policy[dir] = pol;
1083 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001084 pol->curlft.add_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001085 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1087 }
1088 if (old_pol)
1089 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1090 write_unlock_bh(&xfrm_policy_lock);
1091
1092 if (old_pol) {
1093 xfrm_policy_kill(old_pol);
1094 }
1095 return 0;
1096}
1097
1098static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1099{
1100 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
1101
1102 if (newp) {
1103 newp->selector = old->selector;
Trent Jaegerdf718372005-12-13 23:12:27 -08001104 if (security_xfrm_policy_clone(old, newp)) {
1105 kfree(newp);
1106 return NULL; /* ENOMEM */
1107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 newp->lft = old->lft;
1109 newp->curlft = old->curlft;
1110 newp->action = old->action;
1111 newp->flags = old->flags;
1112 newp->xfrm_nr = old->xfrm_nr;
1113 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001114 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 memcpy(newp->xfrm_vec, old->xfrm_vec,
1116 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1117 write_lock_bh(&xfrm_policy_lock);
1118 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1119 write_unlock_bh(&xfrm_policy_lock);
1120 xfrm_pol_put(newp);
1121 }
1122 return newp;
1123}
1124
1125int __xfrm_sk_clone_policy(struct sock *sk)
1126{
1127 struct xfrm_policy *p0 = sk->sk_policy[0],
1128 *p1 = sk->sk_policy[1];
1129
1130 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1131 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1132 return -ENOMEM;
1133 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1134 return -ENOMEM;
1135 return 0;
1136}
1137
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001138static int
1139xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
1140 unsigned short family)
1141{
1142 int err;
1143 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1144
1145 if (unlikely(afinfo == NULL))
1146 return -EINVAL;
1147 err = afinfo->get_saddr(local, remote);
1148 xfrm_policy_put_afinfo(afinfo);
1149 return err;
1150}
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152/* Resolve list of templates for the flow, given policy. */
1153
1154static int
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001155xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1156 struct xfrm_state **xfrm,
1157 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 int nx;
1160 int i, error;
1161 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1162 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001163 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1166 struct xfrm_state *x;
1167 xfrm_address_t *remote = daddr;
1168 xfrm_address_t *local = saddr;
1169 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1170
Joakim Koskela48b8d782007-07-26 00:08:42 -07001171 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1172 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 remote = &tmpl->id.daddr;
1174 local = &tmpl->saddr;
Miika Komu76b3f052006-11-30 16:40:43 -08001175 family = tmpl->encap_family;
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001176 if (xfrm_addr_any(local, family)) {
1177 error = xfrm_get_saddr(&tmp, remote, family);
1178 if (error)
1179 goto fail;
1180 local = &tmp;
1181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183
1184 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1185
1186 if (x && x->km.state == XFRM_STATE_VALID) {
1187 xfrm[nx++] = x;
1188 daddr = remote;
1189 saddr = local;
1190 continue;
1191 }
1192 if (x) {
1193 error = (x->km.state == XFRM_STATE_ERROR ?
1194 -EINVAL : -EAGAIN);
1195 xfrm_state_put(x);
1196 }
1197
1198 if (!tmpl->optional)
1199 goto fail;
1200 }
1201 return nx;
1202
1203fail:
1204 for (nx--; nx>=0; nx--)
1205 xfrm_state_put(xfrm[nx]);
1206 return error;
1207}
1208
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001209static int
1210xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1211 struct xfrm_state **xfrm,
1212 unsigned short family)
1213{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001214 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1215 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001216 int cnx = 0;
1217 int error;
1218 int ret;
1219 int i;
1220
1221 for (i = 0; i < npols; i++) {
1222 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1223 error = -ENOBUFS;
1224 goto fail;
1225 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001226
1227 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001228 if (ret < 0) {
1229 error = ret;
1230 goto fail;
1231 } else
1232 cnx += ret;
1233 }
1234
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001235 /* found states are sorted for outbound processing */
1236 if (npols > 1)
1237 xfrm_state_sort(xfrm, tpp, cnx, family);
1238
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001239 return cnx;
1240
1241 fail:
1242 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001243 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001244 return error;
1245
1246}
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248/* Check that the bundle accepts the flow and its components are
1249 * still valid.
1250 */
1251
1252static struct dst_entry *
1253xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1254{
1255 struct dst_entry *x;
1256 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1257 if (unlikely(afinfo == NULL))
1258 return ERR_PTR(-EINVAL);
1259 x = afinfo->find_bundle(fl, policy);
1260 xfrm_policy_put_afinfo(afinfo);
1261 return x;
1262}
1263
Herbert Xu25ee3282007-12-11 09:32:34 -08001264static inline int xfrm_get_tos(struct flowi *fl, int family)
1265{
1266 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1267 int tos;
1268
1269 if (!afinfo)
1270 return -EINVAL;
1271
1272 tos = afinfo->get_tos(fl);
1273
1274 xfrm_policy_put_afinfo(afinfo);
1275
1276 return tos;
1277}
1278
1279static inline struct xfrm_dst *xfrm_alloc_dst(int family)
1280{
1281 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1282 struct xfrm_dst *xdst;
1283
1284 if (!afinfo)
1285 return ERR_PTR(-EINVAL);
1286
1287 xdst = dst_alloc(afinfo->dst_ops) ?: ERR_PTR(-ENOBUFS);
1288
1289 xfrm_policy_put_afinfo(afinfo);
1290
1291 return xdst;
1292}
1293
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001294static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1295 int nfheader_len)
1296{
1297 struct xfrm_policy_afinfo *afinfo =
1298 xfrm_policy_get_afinfo(dst->ops->family);
1299 int err;
1300
1301 if (!afinfo)
1302 return -EINVAL;
1303
1304 err = afinfo->init_path(path, dst, nfheader_len);
1305
1306 xfrm_policy_put_afinfo(afinfo);
1307
1308 return err;
1309}
1310
Herbert Xu25ee3282007-12-11 09:32:34 -08001311static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev)
1312{
1313 struct xfrm_policy_afinfo *afinfo =
1314 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1315 int err;
1316
1317 if (!afinfo)
1318 return -EINVAL;
1319
1320 err = afinfo->fill_dst(xdst, dev);
1321
1322 xfrm_policy_put_afinfo(afinfo);
1323
1324 return err;
1325}
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1328 * all the metrics... Shortly, bundle a bundle.
1329 */
1330
Herbert Xu25ee3282007-12-11 09:32:34 -08001331static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1332 struct xfrm_state **xfrm, int nx,
1333 struct flowi *fl,
1334 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Herbert Xu25ee3282007-12-11 09:32:34 -08001336 unsigned long now = jiffies;
1337 struct net_device *dev;
1338 struct dst_entry *dst_prev = NULL;
1339 struct dst_entry *dst0 = NULL;
1340 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001342 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001343 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001344 int trailer_len = 0;
1345 int tos;
1346 int family = policy->selector.family;
1347
1348 tos = xfrm_get_tos(fl, family);
1349 err = tos;
1350 if (tos < 0)
1351 goto put_states;
1352
1353 dst_hold(dst);
1354
1355 for (; i < nx; i++) {
1356 struct xfrm_dst *xdst = xfrm_alloc_dst(family);
1357 struct dst_entry *dst1 = &xdst->u.dst;
1358
1359 err = PTR_ERR(xdst);
1360 if (IS_ERR(xdst)) {
1361 dst_release(dst);
1362 goto put_states;
1363 }
1364
1365 if (!dst_prev)
1366 dst0 = dst1;
1367 else {
1368 dst_prev->child = dst_clone(dst1);
1369 dst1->flags |= DST_NOHASH;
1370 }
1371
1372 xdst->route = dst;
1373 memcpy(&dst1->metrics, &dst->metrics, sizeof(dst->metrics));
1374
1375 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1376 family = xfrm[i]->props.family;
1377 dst = xfrm_dst_lookup(xfrm[i], tos, family);
1378 err = PTR_ERR(dst);
1379 if (IS_ERR(dst))
1380 goto put_states;
1381 } else
1382 dst_hold(dst);
1383
1384 dst1->xfrm = xfrm[i];
1385 xdst->genid = xfrm[i]->genid;
1386
1387 dst1->obsolete = -1;
1388 dst1->flags |= DST_HOST;
1389 dst1->lastuse = now;
1390
1391 dst1->input = dst_discard;
1392 dst1->output = xfrm[i]->outer_mode->afinfo->output;
1393
1394 dst1->next = dst_prev;
1395 dst_prev = dst1;
1396
1397 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001398 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1399 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001400 trailer_len += xfrm[i]->props.trailer_len;
1401 }
1402
1403 dst_prev->child = dst;
1404 dst0->path = dst;
1405
1406 err = -ENODEV;
1407 dev = dst->dev;
1408 if (!dev)
1409 goto free_dst;
1410
1411 /* Copy neighbout for reachability confirmation */
1412 dst0->neighbour = neigh_clone(dst->neighbour);
1413
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001414 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001415 xfrm_init_pmtu(dst_prev);
1416
1417 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1418 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1419
1420 err = xfrm_fill_dst(xdst, dev);
1421 if (err)
1422 goto free_dst;
1423
1424 dst_prev->header_len = header_len;
1425 dst_prev->trailer_len = trailer_len;
1426 header_len -= xdst->u.dst.xfrm->props.header_len;
1427 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1428 }
1429
1430out:
1431 return dst0;
1432
1433put_states:
1434 for (; i < nx; i++)
1435 xfrm_state_put(xfrm[i]);
1436free_dst:
1437 if (dst0)
1438 dst_free(dst0);
1439 dst0 = ERR_PTR(err);
1440 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001443static int inline
1444xfrm_dst_alloc_copy(void **target, void *src, int size)
1445{
1446 if (!*target) {
1447 *target = kmalloc(size, GFP_ATOMIC);
1448 if (!*target)
1449 return -ENOMEM;
1450 }
1451 memcpy(*target, src, size);
1452 return 0;
1453}
1454
1455static int inline
1456xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
1457{
1458#ifdef CONFIG_XFRM_SUB_POLICY
1459 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1460 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1461 sel, sizeof(*sel));
1462#else
1463 return 0;
1464#endif
1465}
1466
1467static int inline
1468xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
1469{
1470#ifdef CONFIG_XFRM_SUB_POLICY
1471 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1472 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1473#else
1474 return 0;
1475#endif
1476}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478static int stale_bundle(struct dst_entry *dst);
1479
1480/* Main function: finds/creates a bundle for given flow.
1481 *
1482 * At the moment we eat a raw IP route. Mostly to speed up lookups
1483 * on interfaces with disabled IPsec.
1484 */
David S. Miller14e50e52007-05-24 18:17:54 -07001485int __xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1486 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 struct xfrm_policy *policy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001489 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1490 int npols;
1491 int pol_dead;
1492 int xfrm_nr;
1493 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1495 struct dst_entry *dst, *dst_orig = *dst_p;
1496 int nx = 0;
1497 int err;
1498 u32 genid;
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001499 u16 family;
Trent Jaegerdf718372005-12-13 23:12:27 -08001500 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502restart:
1503 genid = atomic_read(&flow_cache_genid);
1504 policy = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001505 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1506 pols[pi] = NULL;
1507 npols = 0;
1508 pol_dead = 0;
1509 xfrm_nr = 0;
1510
Thomas Graff7944fb2007-08-25 13:46:55 -07001511 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001512 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
Herbert Xu75b8c132007-12-11 04:38:08 -08001513 err = PTR_ERR(policy);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001514 if (IS_ERR(policy)) {
1515 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLERROR);
Herbert Xu75b8c132007-12-11 04:38:08 -08001516 goto dropdst;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001517 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 if (!policy) {
1521 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07001522 if ((dst_orig->flags & DST_NOXFRM) ||
1523 !xfrm_policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08001524 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001526 policy = flow_cache_lookup(fl, dst_orig->ops->family,
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001527 dir, xfrm_policy_lookup);
Herbert Xu75b8c132007-12-11 04:38:08 -08001528 err = PTR_ERR(policy);
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08001529 if (IS_ERR(policy)) {
1530 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLERROR);
Herbert Xu75b8c132007-12-11 04:38:08 -08001531 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08001532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534
1535 if (!policy)
Herbert Xu8b7817f2007-12-12 10:44:43 -08001536 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001538 family = dst_orig->ops->family;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001539 pols[0] = policy;
1540 npols ++;
1541 xfrm_nr += pols[0]->xfrm_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Herbert Xuaef21782007-12-13 09:30:59 -08001543 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001544 if ((flags & XFRM_LOOKUP_ICMP) && !(policy->flags & XFRM_POLICY_ICMP))
1545 goto error;
1546
1547 policy->curlft.use_time = get_seconds();
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 switch (policy->action) {
Herbert Xu5e5234f2007-11-30 00:50:31 +11001550 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 case XFRM_POLICY_BLOCK:
1552 /* Prohibit the flow */
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001553 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07001554 err = -EPERM;
1555 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 case XFRM_POLICY_ALLOW:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001558#ifndef CONFIG_XFRM_SUB_POLICY
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (policy->xfrm_nr == 0) {
1560 /* Flow passes not transformed. */
1561 xfrm_pol_put(policy);
1562 return 0;
1563 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001564#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 /* Try to find matching bundle.
1567 *
1568 * LATER: help from flow cache. It is optional, this
1569 * is required only for output policy.
1570 */
1571 dst = xfrm_find_bundle(fl, policy, family);
1572 if (IS_ERR(dst)) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001573 XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
Patrick McHardye104411b2005-09-08 15:11:55 -07001574 err = PTR_ERR(dst);
1575 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577
1578 if (dst)
1579 break;
1580
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001581#ifdef CONFIG_XFRM_SUB_POLICY
1582 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1583 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1584 fl, family,
1585 XFRM_POLICY_OUT);
1586 if (pols[1]) {
James Morris134b0fc2006-10-05 15:42:27 -05001587 if (IS_ERR(pols[1])) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001588 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05001589 err = PTR_ERR(pols[1]);
1590 goto error;
1591 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001592 if (pols[1]->action == XFRM_POLICY_BLOCK) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001593 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001594 err = -EPERM;
1595 goto error;
1596 }
1597 npols ++;
1598 xfrm_nr += pols[1]->xfrm_nr;
1599 }
1600 }
1601
1602 /*
1603 * Because neither flowi nor bundle information knows about
1604 * transformation template size. On more than one policy usage
1605 * we can realize whether all of them is bypass or not after
1606 * they are searched. See above not-transformed bypass
1607 * is surrounded by non-sub policy configuration, too.
1608 */
1609 if (xfrm_nr == 0) {
1610 /* Flow passes not transformed. */
1611 xfrm_pols_put(pols, npols);
1612 return 0;
1613 }
1614
1615#endif
1616 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 if (unlikely(nx<0)) {
1619 err = nx;
David S. Miller14e50e52007-05-24 18:17:54 -07001620 if (err == -EAGAIN && sysctl_xfrm_larval_drop) {
1621 /* EREMOTE tells the caller to generate
1622 * a one-shot blackhole route.
1623 */
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08001624 XFRM_INC_STATS(LINUX_MIB_XFRMOUTNOSTATES);
David S. Miller14e50e52007-05-24 18:17:54 -07001625 xfrm_pol_put(policy);
1626 return -EREMOTE;
1627 }
Herbert Xu815f4e52007-12-12 10:36:59 -08001628 if (err == -EAGAIN && (flags & XFRM_LOOKUP_WAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 DECLARE_WAITQUEUE(wait, current);
1630
1631 add_wait_queue(&km_waitq, &wait);
1632 set_current_state(TASK_INTERRUPTIBLE);
1633 schedule();
1634 set_current_state(TASK_RUNNING);
1635 remove_wait_queue(&km_waitq, &wait);
1636
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001637 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 if (nx == -EAGAIN && signal_pending(current)) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001640 XFRM_INC_STATS(LINUX_MIB_XFRMOUTNOSTATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 err = -ERESTART;
1642 goto error;
1643 }
1644 if (nx == -EAGAIN ||
1645 genid != atomic_read(&flow_cache_genid)) {
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001646 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 goto restart;
1648 }
1649 err = nx;
1650 }
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001651 if (err < 0) {
1652 XFRM_INC_STATS(LINUX_MIB_XFRMOUTNOSTATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 goto error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
1656 if (nx == 0) {
1657 /* Flow passes not transformed. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001658 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return 0;
1660 }
1661
Herbert Xu25ee3282007-12-11 09:32:34 -08001662 dst = xfrm_bundle_create(policy, xfrm, nx, fl, dst_orig);
1663 err = PTR_ERR(dst);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001664 if (IS_ERR(dst)) {
1665 XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLEGENERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 goto error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001669 for (pi = 0; pi < npols; pi++) {
1670 read_lock_bh(&pols[pi]->lock);
1671 pol_dead |= pols[pi]->dead;
1672 read_unlock_bh(&pols[pi]->lock);
1673 }
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 write_lock_bh(&policy->lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001676 if (unlikely(pol_dead || stale_bundle(dst))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 /* Wow! While we worked on resolving, this
1678 * policy has gone. Retry. It is not paranoia,
1679 * we just cannot enlist new bundle to dead object.
1680 * We can't enlist stable bundles either.
1681 */
1682 write_unlock_bh(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (dst)
1684 dst_free(dst);
Herbert Xu00de6512006-02-13 16:01:27 -08001685
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001686 if (pol_dead)
1687 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLDEAD);
1688 else
1689 XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
Herbert Xu00de6512006-02-13 16:01:27 -08001690 err = -EHOSTUNREACH;
1691 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001693
1694 if (npols > 1)
1695 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1696 else
1697 err = xfrm_dst_update_origin(dst, fl);
1698 if (unlikely(err)) {
1699 write_unlock_bh(&policy->lock);
1700 if (dst)
1701 dst_free(dst);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001702 XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001703 goto error;
1704 }
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 dst->next = policy->bundles;
1707 policy->bundles = dst;
1708 dst_hold(dst);
1709 write_unlock_bh(&policy->lock);
1710 }
1711 *dst_p = dst;
1712 dst_release(dst_orig);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001713 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 return 0;
1715
1716error:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001717 xfrm_pols_put(pols, npols);
Herbert Xu75b8c132007-12-11 04:38:08 -08001718dropdst:
1719 dst_release(dst_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 *dst_p = NULL;
1721 return err;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001722
1723nopol:
Herbert Xuaef21782007-12-13 09:30:59 -08001724 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001725 if (flags & XFRM_LOOKUP_ICMP)
1726 goto dropdst;
1727 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
David S. Miller14e50e52007-05-24 18:17:54 -07001729EXPORT_SYMBOL(__xfrm_lookup);
1730
1731int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1732 struct sock *sk, int flags)
1733{
1734 int err = __xfrm_lookup(dst_p, fl, sk, flags);
1735
1736 if (err == -EREMOTE) {
1737 dst_release(*dst_p);
1738 *dst_p = NULL;
1739 err = -EAGAIN;
1740 }
1741
1742 return err;
1743}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744EXPORT_SYMBOL(xfrm_lookup);
1745
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001746static inline int
1747xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1748{
1749 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001750
1751 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1752 return 0;
1753 x = skb->sp->xvec[idx];
1754 if (!x->type->reject)
1755 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07001756 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001757}
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759/* When skb is transformed back to its "native" form, we have to
1760 * check policy restrictions. At the moment we make this in maximally
1761 * stupid way. Shame on me. :-) Of course, connected sockets must
1762 * have policy cached at them.
1763 */
1764
1765static inline int
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001766xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 unsigned short family)
1768{
1769 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08001770 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 return x->id.proto == tmpl->id.proto &&
1772 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1773 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1774 x->props.mode == tmpl->mode &&
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07001775 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1776 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001777 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1778 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001781/*
1782 * 0 or more than 0 is returned when validation is succeeded (either bypass
1783 * because of optional transport mode, or next index of the mathced secpath
1784 * state with the template.
1785 * -1 is returned when no matching template is found.
1786 * Otherwise "-2 - errored_index" is returned.
1787 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788static inline int
1789xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1790 unsigned short family)
1791{
1792 int idx = start;
1793
1794 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001795 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return start;
1797 } else
1798 start = -1;
1799 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001800 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001802 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1803 if (start == -1)
1804 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
1808 return start;
1809}
1810
Herbert Xud5422ef2007-12-12 10:44:16 -08001811int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
1812 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
1814 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001815 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 if (unlikely(afinfo == NULL))
1818 return -EAFNOSUPPORT;
1819
Herbert Xud5422ef2007-12-12 10:44:16 -08001820 afinfo->decode_session(skb, fl, reverse);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07001821 err = security_xfrm_decode_session(skb, &fl->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001823 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824}
Herbert Xud5422ef2007-12-12 10:44:16 -08001825EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001827static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
1829 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001830 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001831 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
1835
1836 return 0;
1837}
1838
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001839int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 unsigned short family)
1841{
1842 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001843 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1844 int npols = 0;
1845 int xfrm_nr;
1846 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08001847 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08001849 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001850 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Herbert Xud5422ef2007-12-12 10:44:16 -08001852 reverse = dir & ~XFRM_POLICY_MASK;
1853 dir &= XFRM_POLICY_MASK;
1854 fl_dir = policy_to_flow_dir(dir);
1855
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001856 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
1857 XFRM_INC_STATS(LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001859 }
1860
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08001861 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 /* First, check used SA against their selectors. */
1864 if (skb->sp) {
1865 int i;
1866
1867 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001868 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001869 if (!xfrm_selector_match(&x->sel, &fl, family)) {
1870 XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
1874 }
1875
1876 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001877 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001878 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001879 if (IS_ERR(pol)) {
1880 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001881 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001882 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 if (!pol)
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001886 pol = flow_cache_lookup(&fl, family, fl_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 xfrm_policy_lookup);
1888
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001889 if (IS_ERR(pol)) {
1890 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05001891 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001892 }
James Morris134b0fc2006-10-05 15:42:27 -05001893
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001894 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001895 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001896 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001897 XFRM_INC_STATS(LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001898 return 0;
1899 }
1900 return 1;
1901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
James Morris9d729f72007-03-04 16:12:44 -08001903 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001905 pols[0] = pol;
1906 npols ++;
1907#ifdef CONFIG_XFRM_SUB_POLICY
1908 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1909 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1910 &fl, family,
1911 XFRM_POLICY_IN);
1912 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001913 if (IS_ERR(pols[1])) {
1914 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05001915 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001916 }
James Morris9d729f72007-03-04 16:12:44 -08001917 pols[1]->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001918 npols ++;
1919 }
1920 }
1921#endif
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 if (pol->action == XFRM_POLICY_ALLOW) {
1924 struct sec_path *sp;
1925 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001926 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001927 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001928 struct xfrm_tmpl **tpp = tp;
1929 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 int i, k;
1931
1932 if ((sp = skb->sp) == NULL)
1933 sp = &dummy;
1934
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001935 for (pi = 0; pi < npols; pi++) {
1936 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001937 pols[pi]->action != XFRM_POLICY_ALLOW) {
1938 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001939 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001940 }
1941 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
1942 XFRM_INC_STATS(LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001943 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001944 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001945 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1946 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1947 }
1948 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001949 if (npols > 1) {
1950 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1951 tpp = stp;
1952 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 /* For each tunnel xfrm, find the first matching tmpl.
1955 * For each tmpl before that, find corresponding xfrm.
1956 * Order is _important_. Later we will implement
1957 * some barriers, but at the moment barriers
1958 * are implied between each two transformations.
1959 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001960 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1961 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001962 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001963 if (k < -1)
1964 /* "-2 - errored_index" returned */
1965 xerr_idx = -(2+k);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001966 XFRM_INC_STATS(LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 }
1970
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001971 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
1972 XFRM_INC_STATS(LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001976 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return 1;
1978 }
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001979 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001982 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001983reject_error:
1984 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 return 0;
1986}
1987EXPORT_SYMBOL(__xfrm_policy_check);
1988
1989int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1990{
1991 struct flowi fl;
1992
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001993 if (xfrm_decode_session(skb, &fl, family) < 0) {
1994 /* XXX: we should have something like FWDHDRERROR here. */
1995 XFRM_INC_STATS(LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
2000}
2001EXPORT_SYMBOL(__xfrm_route_forward);
2002
David S. Millerd49c73c2006-08-13 18:55:53 -07002003/* Optimize later using cookies and generation ids. */
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2006{
David S. Millerd49c73c2006-08-13 18:55:53 -07002007 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2008 * to "-1" to force all XFRM destinations to get validated by
2009 * dst_ops->check on every use. We do this because when a
2010 * normal route referenced by an XFRM dst is obsoleted we do
2011 * not go looking around for all parent referencing XFRM dsts
2012 * so that we can invalidate them. It is just too much work.
2013 * Instead we make the checks here on every use. For example:
2014 *
2015 * XFRM dst A --> IPv4 dst X
2016 *
2017 * X is the "xdst->route" of A (X is also the "dst->path" of A
2018 * in this example). If X is marked obsolete, "A" will not
2019 * notice. That's what we are validating here via the
2020 * stale_bundle() check.
2021 *
2022 * When a policy's bundle is pruned, we dst_free() the XFRM
2023 * dst which causes it's ->obsolete field to be set to a
2024 * positive non-zero integer. If an XFRM dst has been pruned
2025 * like this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002026 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002027 if (dst->obsolete < 0 && !stale_bundle(dst))
2028 return dst;
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return NULL;
2031}
2032
2033static int stale_bundle(struct dst_entry *dst)
2034{
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05002035 return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036}
2037
Herbert Xuaabc9762005-05-03 16:27:10 -07002038void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -08002041 dst->dev = dev->nd_net->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002042 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 dev_put(dev);
2044 }
2045}
Herbert Xuaabc9762005-05-03 16:27:10 -07002046EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048static void xfrm_link_failure(struct sk_buff *skb)
2049{
2050 /* Impossible. Such dst must be popped before reaches point of failure. */
2051 return;
2052}
2053
2054static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2055{
2056 if (dst) {
2057 if (dst->obsolete) {
2058 dst_release(dst);
2059 dst = NULL;
2060 }
2061 }
2062 return dst;
2063}
2064
David S. Miller2518c7c2006-08-24 04:45:07 -07002065static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
2066{
2067 struct dst_entry *dst, **dstp;
2068
2069 write_lock(&pol->lock);
2070 dstp = &pol->bundles;
2071 while ((dst=*dstp) != NULL) {
2072 if (func(dst)) {
2073 *dstp = dst->next;
2074 dst->next = *gc_list_p;
2075 *gc_list_p = dst;
2076 } else {
2077 dstp = &dst->next;
2078 }
2079 }
2080 write_unlock(&pol->lock);
2081}
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
2084{
David S. Miller2518c7c2006-08-24 04:45:07 -07002085 struct dst_entry *gc_list = NULL;
2086 int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07002089 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2090 struct xfrm_policy *pol;
2091 struct hlist_node *entry;
2092 struct hlist_head *table;
2093 int i;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002094
David S. Miller2518c7c2006-08-24 04:45:07 -07002095 hlist_for_each_entry(pol, entry,
2096 &xfrm_policy_inexact[dir], bydst)
2097 prune_one_bundle(pol, func, &gc_list);
2098
2099 table = xfrm_policy_bydst[dir].table;
2100 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
2101 hlist_for_each_entry(pol, entry, table + i, bydst)
2102 prune_one_bundle(pol, func, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
2104 }
2105 read_unlock_bh(&xfrm_policy_lock);
2106
2107 while (gc_list) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002108 struct dst_entry *dst = gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 gc_list = dst->next;
2110 dst_free(dst);
2111 }
2112}
2113
2114static int unused_bundle(struct dst_entry *dst)
2115{
2116 return !atomic_read(&dst->__refcnt);
2117}
2118
2119static void __xfrm_garbage_collect(void)
2120{
2121 xfrm_prune_bundles(unused_bundle);
2122}
2123
David S. Miller1c095392006-08-24 03:30:28 -07002124static int xfrm_flush_bundles(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 xfrm_prune_bundles(stale_bundle);
2127 return 0;
2128}
2129
Herbert Xu25ee3282007-12-11 09:32:34 -08002130static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
2132 do {
2133 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2134 u32 pmtu, route_mtu_cached;
2135
2136 pmtu = dst_mtu(dst->child);
2137 xdst->child_mtu_cached = pmtu;
2138
2139 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2140
2141 route_mtu_cached = dst_mtu(xdst->route);
2142 xdst->route_mtu_cached = route_mtu_cached;
2143
2144 if (pmtu > route_mtu_cached)
2145 pmtu = route_mtu_cached;
2146
2147 dst->metrics[RTAX_MTU-1] = pmtu;
2148 } while ((dst = dst->next));
2149}
2150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151/* Check that the bundle accepts the flow and its components are
2152 * still valid.
2153 */
2154
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05002155int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
2156 struct flowi *fl, int family, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
2158 struct dst_entry *dst = &first->u.dst;
2159 struct xfrm_dst *last;
2160 u32 mtu;
2161
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002162 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 (dst->dev && !netif_running(dst->dev)))
2164 return 0;
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07002165#ifdef CONFIG_XFRM_SUB_POLICY
2166 if (fl) {
2167 if (first->origin && !flow_cache_uli_match(first->origin, fl))
2168 return 0;
2169 if (first->partner &&
2170 !xfrm_selector_match(first->partner, fl, family))
2171 return 0;
2172 }
2173#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 last = NULL;
2176
2177 do {
2178 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2179
2180 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
2181 return 0;
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06002182 if (fl && pol &&
2183 !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2186 return 0;
David S. Miller9d4a7062006-08-24 03:18:09 -07002187 if (xdst->genid != dst->xfrm->genid)
2188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Herbert Xu1bfcb102007-10-17 21:31:50 -07002190 if (strict && fl &&
Herbert Xu13996372007-10-17 21:35:51 -07002191 !(dst->xfrm->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07002192 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
2193 return 0;
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 mtu = dst_mtu(dst->child);
2196 if (xdst->child_mtu_cached != mtu) {
2197 last = xdst;
2198 xdst->child_mtu_cached = mtu;
2199 }
2200
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002201 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 return 0;
2203 mtu = dst_mtu(xdst->route);
2204 if (xdst->route_mtu_cached != mtu) {
2205 last = xdst;
2206 xdst->route_mtu_cached = mtu;
2207 }
2208
2209 dst = dst->child;
2210 } while (dst->xfrm);
2211
2212 if (likely(!last))
2213 return 1;
2214
2215 mtu = last->child_mtu_cached;
2216 for (;;) {
2217 dst = &last->u.dst;
2218
2219 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2220 if (mtu > last->route_mtu_cached)
2221 mtu = last->route_mtu_cached;
2222 dst->metrics[RTAX_MTU-1] = mtu;
2223
2224 if (last == first)
2225 break;
2226
Patrick McHardybd0bf072007-07-18 01:55:52 -07002227 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 last->child_mtu_cached = mtu;
2229 }
2230
2231 return 1;
2232}
2233
2234EXPORT_SYMBOL(xfrm_bundle_ok);
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2237{
2238 int err = 0;
2239 if (unlikely(afinfo == NULL))
2240 return -EINVAL;
2241 if (unlikely(afinfo->family >= NPROTO))
2242 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002243 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2245 err = -ENOBUFS;
2246 else {
2247 struct dst_ops *dst_ops = afinfo->dst_ops;
2248 if (likely(dst_ops->kmem_cachep == NULL))
2249 dst_ops->kmem_cachep = xfrm_dst_cache;
2250 if (likely(dst_ops->check == NULL))
2251 dst_ops->check = xfrm_dst_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 if (likely(dst_ops->negative_advice == NULL))
2253 dst_ops->negative_advice = xfrm_negative_advice;
2254 if (likely(dst_ops->link_failure == NULL))
2255 dst_ops->link_failure = xfrm_link_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (likely(afinfo->garbage_collect == NULL))
2257 afinfo->garbage_collect = __xfrm_garbage_collect;
2258 xfrm_policy_afinfo[afinfo->family] = afinfo;
2259 }
Ingo Molnare959d812006-04-28 15:32:29 -07002260 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return err;
2262}
2263EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2264
2265int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2266{
2267 int err = 0;
2268 if (unlikely(afinfo == NULL))
2269 return -EINVAL;
2270 if (unlikely(afinfo->family >= NPROTO))
2271 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002272 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2274 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2275 err = -EINVAL;
2276 else {
2277 struct dst_ops *dst_ops = afinfo->dst_ops;
2278 xfrm_policy_afinfo[afinfo->family] = NULL;
2279 dst_ops->kmem_cachep = NULL;
2280 dst_ops->check = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 dst_ops->negative_advice = NULL;
2282 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 afinfo->garbage_collect = NULL;
2284 }
2285 }
Ingo Molnare959d812006-04-28 15:32:29 -07002286 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return err;
2288}
2289EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2290
2291static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
2292{
2293 struct xfrm_policy_afinfo *afinfo;
2294 if (unlikely(family >= NPROTO))
2295 return NULL;
2296 read_lock(&xfrm_policy_afinfo_lock);
2297 afinfo = xfrm_policy_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07002298 if (unlikely(!afinfo))
2299 read_unlock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return afinfo;
2301}
2302
2303static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2304{
Herbert Xu546be242006-05-27 23:03:58 -07002305 read_unlock(&xfrm_policy_afinfo_lock);
2306}
2307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2309{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002310 struct net_device *dev = ptr;
2311
2312 if (dev->nd_net != &init_net)
2313 return NOTIFY_DONE;
2314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 switch (event) {
2316 case NETDEV_DOWN:
2317 xfrm_flush_bundles();
2318 }
2319 return NOTIFY_DONE;
2320}
2321
2322static struct notifier_block xfrm_dev_notifier = {
2323 xfrm_dev_event,
2324 NULL,
2325 0
2326};
2327
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002328#ifdef CONFIG_XFRM_STATISTICS
2329static int __init xfrm_statistics_init(void)
2330{
2331 if (snmp_mib_init((void **)xfrm_statistics,
2332 sizeof(struct linux_xfrm_mib)) < 0)
2333 return -ENOMEM;
2334 return 0;
2335}
2336#endif
2337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338static void __init xfrm_policy_init(void)
2339{
David S. Miller2518c7c2006-08-24 04:45:07 -07002340 unsigned int hmask, sz;
2341 int dir;
2342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2344 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002345 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002346 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
David S. Miller2518c7c2006-08-24 04:45:07 -07002348 hmask = 8 - 1;
2349 sz = (hmask+1) * sizeof(struct hlist_head);
2350
David S. Miller44e36b42006-08-24 04:50:50 -07002351 xfrm_policy_byidx = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002352 xfrm_idx_hmask = hmask;
2353 if (!xfrm_policy_byidx)
2354 panic("XFRM: failed to allocate byidx hash\n");
2355
2356 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2357 struct xfrm_policy_hash *htab;
2358
2359 INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
2360
2361 htab = &xfrm_policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002362 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002363 htab->hmask = hmask;
2364 if (!htab->table)
2365 panic("XFRM: failed to allocate bydst hash\n");
2366 }
2367
David Howellsc4028952006-11-22 14:57:56 +00002368 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 register_netdevice_notifier(&xfrm_dev_notifier);
2370}
2371
2372void __init xfrm_init(void)
2373{
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002374#ifdef CONFIG_XFRM_STATISTICS
2375 xfrm_statistics_init();
2376#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 xfrm_state_init();
2378 xfrm_policy_init();
2379 xfrm_input_init();
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002380#ifdef CONFIG_XFRM_STATISTICS
2381 xfrm_proc_init();
2382#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
Joy Lattenab5f5e82007-09-17 11:51:22 -07002385#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08002386static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2387 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002388{
Paul Moore875179f2007-12-01 23:27:18 +11002389 struct xfrm_sec_ctx *ctx = xp->security;
2390 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002391
Paul Moore875179f2007-12-01 23:27:18 +11002392 if (ctx)
2393 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2394 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2395
2396 switch(sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002397 case AF_INET:
Paul Moore875179f2007-12-01 23:27:18 +11002398 audit_log_format(audit_buf, " src=" NIPQUAD_FMT,
2399 NIPQUAD(sel->saddr.a4));
2400 if (sel->prefixlen_s != 32)
2401 audit_log_format(audit_buf, " src_prefixlen=%d",
2402 sel->prefixlen_s);
2403 audit_log_format(audit_buf, " dst=" NIPQUAD_FMT,
2404 NIPQUAD(sel->daddr.a4));
2405 if (sel->prefixlen_d != 32)
2406 audit_log_format(audit_buf, " dst_prefixlen=%d",
2407 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002408 break;
2409 case AF_INET6:
Paul Moore875179f2007-12-01 23:27:18 +11002410 audit_log_format(audit_buf, " src=" NIP6_FMT,
2411 NIP6(*(struct in6_addr *)sel->saddr.a6));
2412 if (sel->prefixlen_s != 128)
2413 audit_log_format(audit_buf, " src_prefixlen=%d",
2414 sel->prefixlen_s);
2415 audit_log_format(audit_buf, " dst=" NIP6_FMT,
2416 NIP6(*(struct in6_addr *)sel->daddr.a6));
2417 if (sel->prefixlen_d != 128)
2418 audit_log_format(audit_buf, " dst_prefixlen=%d",
2419 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002420 break;
2421 }
2422}
2423
Paul Moore68277ac2007-12-20 20:49:33 -08002424void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
2425 u32 auid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002426{
2427 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002428
Paul Mooreafeb14b2007-12-21 14:58:11 -08002429 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002430 if (audit_buf == NULL)
2431 return;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002432 xfrm_audit_helper_usrinfo(auid, secid, audit_buf);
2433 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002434 xfrm_audit_common_policyinfo(xp, audit_buf);
2435 audit_log_end(audit_buf);
2436}
2437EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
2438
Paul Moore68277ac2007-12-20 20:49:33 -08002439void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
2440 u32 auid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002441{
2442 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002443
Paul Mooreafeb14b2007-12-21 14:58:11 -08002444 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002445 if (audit_buf == NULL)
2446 return;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002447 xfrm_audit_helper_usrinfo(auid, secid, audit_buf);
2448 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002449 xfrm_audit_common_policyinfo(xp, audit_buf);
2450 audit_log_end(audit_buf);
2451}
2452EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
2453#endif
2454
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002455#ifdef CONFIG_XFRM_MIGRATE
2456static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
2457 struct xfrm_selector *sel_tgt)
2458{
2459 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
2460 if (sel_tgt->family == sel_cmp->family &&
2461 xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002462 sel_cmp->family) == 0 &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002463 xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
2464 sel_cmp->family) == 0 &&
2465 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
2466 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
2467 return 1;
2468 }
2469 } else {
2470 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
2471 return 1;
2472 }
2473 }
2474 return 0;
2475}
2476
2477static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
2478 u8 dir, u8 type)
2479{
2480 struct xfrm_policy *pol, *ret = NULL;
2481 struct hlist_node *entry;
2482 struct hlist_head *chain;
2483 u32 priority = ~0U;
2484
2485 read_lock_bh(&xfrm_policy_lock);
2486 chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
2487 hlist_for_each_entry(pol, entry, chain, bydst) {
2488 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2489 pol->type == type) {
2490 ret = pol;
2491 priority = ret->priority;
2492 break;
2493 }
2494 }
2495 chain = &xfrm_policy_inexact[dir];
2496 hlist_for_each_entry(pol, entry, chain, bydst) {
2497 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2498 pol->type == type &&
2499 pol->priority < priority) {
2500 ret = pol;
2501 break;
2502 }
2503 }
2504
2505 if (ret)
2506 xfrm_pol_hold(ret);
2507
2508 read_unlock_bh(&xfrm_policy_lock);
2509
2510 return ret;
2511}
2512
2513static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
2514{
2515 int match = 0;
2516
2517 if (t->mode == m->mode && t->id.proto == m->proto &&
2518 (m->reqid == 0 || t->reqid == m->reqid)) {
2519 switch (t->mode) {
2520 case XFRM_MODE_TUNNEL:
2521 case XFRM_MODE_BEET:
2522 if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
2523 m->old_family) == 0 &&
2524 xfrm_addr_cmp(&t->saddr, &m->old_saddr,
2525 m->old_family) == 0) {
2526 match = 1;
2527 }
2528 break;
2529 case XFRM_MODE_TRANSPORT:
2530 /* in case of transport mode, template does not store
2531 any IP addresses, hence we just compare mode and
2532 protocol */
2533 match = 1;
2534 break;
2535 default:
2536 break;
2537 }
2538 }
2539 return match;
2540}
2541
2542/* update endpoint address(es) of template(s) */
2543static int xfrm_policy_migrate(struct xfrm_policy *pol,
2544 struct xfrm_migrate *m, int num_migrate)
2545{
2546 struct xfrm_migrate *mp;
2547 struct dst_entry *dst;
2548 int i, j, n = 0;
2549
2550 write_lock_bh(&pol->lock);
2551 if (unlikely(pol->dead)) {
2552 /* target policy has been deleted */
2553 write_unlock_bh(&pol->lock);
2554 return -ENOENT;
2555 }
2556
2557 for (i = 0; i < pol->xfrm_nr; i++) {
2558 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
2559 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
2560 continue;
2561 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07002562 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
2563 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002564 continue;
2565 /* update endpoints */
2566 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
2567 sizeof(pol->xfrm_vec[i].id.daddr));
2568 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
2569 sizeof(pol->xfrm_vec[i].saddr));
2570 pol->xfrm_vec[i].encap_family = mp->new_family;
2571 /* flush bundles */
2572 while ((dst = pol->bundles) != NULL) {
2573 pol->bundles = dst->next;
2574 dst_free(dst);
2575 }
2576 }
2577 }
2578
2579 write_unlock_bh(&pol->lock);
2580
2581 if (!n)
2582 return -ENODATA;
2583
2584 return 0;
2585}
2586
2587static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
2588{
2589 int i, j;
2590
2591 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
2592 return -EINVAL;
2593
2594 for (i = 0; i < num_migrate; i++) {
2595 if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
2596 m[i].old_family) == 0) &&
2597 (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
2598 m[i].old_family) == 0))
2599 return -EINVAL;
2600 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
2601 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
2602 return -EINVAL;
2603
2604 /* check if there is any duplicated entry */
2605 for (j = i + 1; j < num_migrate; j++) {
2606 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
2607 sizeof(m[i].old_daddr)) &&
2608 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
2609 sizeof(m[i].old_saddr)) &&
2610 m[i].proto == m[j].proto &&
2611 m[i].mode == m[j].mode &&
2612 m[i].reqid == m[j].reqid &&
2613 m[i].old_family == m[j].old_family)
2614 return -EINVAL;
2615 }
2616 }
2617
2618 return 0;
2619}
2620
2621int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
2622 struct xfrm_migrate *m, int num_migrate)
2623{
2624 int i, err, nx_cur = 0, nx_new = 0;
2625 struct xfrm_policy *pol = NULL;
2626 struct xfrm_state *x, *xc;
2627 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
2628 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
2629 struct xfrm_migrate *mp;
2630
2631 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
2632 goto out;
2633
2634 /* Stage 1 - find policy */
2635 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
2636 err = -ENOENT;
2637 goto out;
2638 }
2639
2640 /* Stage 2 - find and update state(s) */
2641 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
2642 if ((x = xfrm_migrate_state_find(mp))) {
2643 x_cur[nx_cur] = x;
2644 nx_cur++;
2645 if ((xc = xfrm_state_migrate(x, mp))) {
2646 x_new[nx_new] = xc;
2647 nx_new++;
2648 } else {
2649 err = -ENODATA;
2650 goto restore_state;
2651 }
2652 }
2653 }
2654
2655 /* Stage 3 - update policy */
2656 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
2657 goto restore_state;
2658
2659 /* Stage 4 - delete old state(s) */
2660 if (nx_cur) {
2661 xfrm_states_put(x_cur, nx_cur);
2662 xfrm_states_delete(x_cur, nx_cur);
2663 }
2664
2665 /* Stage 5 - announce */
2666 km_migrate(sel, dir, type, m, num_migrate);
2667
2668 xfrm_pol_put(pol);
2669
2670 return 0;
2671out:
2672 return err;
2673
2674restore_state:
2675 if (pol)
2676 xfrm_pol_put(pol);
2677 if (nx_cur)
2678 xfrm_states_put(x_cur, nx_cur);
2679 if (nx_new)
2680 xfrm_states_delete(x_new, nx_new);
2681
2682 return err;
2683}
David S. Millere610e672007-02-08 13:29:15 -08002684EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002685#endif