blob: bae94a8031a290d5138cb99c60f007026c11d548 [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
Timo Teras4c563f72008-02-28 21:31:08 -080049static struct list_head xfrm_policy_bytype[XFRM_POLICY_TYPE_MAX];
David S. Miller2518c7c2006-08-24 04:45:07 -070050unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
51EXPORT_SYMBOL(xfrm_policy_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
54static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
55
Christoph Lametere18b8902006-12-06 20:33:20 -080056static struct kmem_cache *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58static struct work_struct xfrm_policy_gc_work;
David S. Miller2518c7c2006-08-24 04:45:07 -070059static HLIST_HEAD(xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
61
62static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
63static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
Herbert Xu25ee3282007-12-11 09:32:34 -080064static void xfrm_init_pmtu(struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Andrew Morton77681022006-11-08 22:46:26 -080066static inline int
67__xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
68{
69 return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
70 addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
71 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
72 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
73 (fl->proto == sel->proto || !sel->proto) &&
74 (fl->oif == sel->ifindex || !sel->ifindex);
75}
76
77static inline int
78__xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
79{
80 return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
81 addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
82 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
83 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
84 (fl->proto == sel->proto || !sel->proto) &&
85 (fl->oif == sel->ifindex || !sel->ifindex);
86}
87
88int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
89 unsigned short family)
90{
91 switch (family) {
92 case AF_INET:
93 return __xfrm4_selector_match(sel, fl);
94 case AF_INET6:
95 return __xfrm6_selector_match(sel, fl);
96 }
97 return 0;
98}
99
Herbert Xu25ee3282007-12-11 09:32:34 -0800100static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
101 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800103 xfrm_address_t *saddr = &x->props.saddr;
104 xfrm_address_t *daddr = &x->id.daddr;
105 struct xfrm_policy_afinfo *afinfo;
106 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800108 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR)
109 saddr = x->coaddr;
110 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR)
111 daddr = x->coaddr;
112
Herbert Xu25ee3282007-12-11 09:32:34 -0800113 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800115 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800117 dst = afinfo->dst_lookup(tos, saddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 xfrm_policy_put_afinfo(afinfo);
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800119 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static inline unsigned long make_jiffies(long secs)
123{
124 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
125 return MAX_SCHEDULE_TIMEOUT-1;
126 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900127 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130static void xfrm_policy_timer(unsigned long data)
131{
132 struct xfrm_policy *xp = (struct xfrm_policy*)data;
James Morris9d729f72007-03-04 16:12:44 -0800133 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 long next = LONG_MAX;
135 int warn = 0;
136 int dir;
137
138 read_lock(&xp->lock);
139
140 if (xp->dead)
141 goto out;
142
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700143 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 if (xp->lft.hard_add_expires_seconds) {
146 long tmo = xp->lft.hard_add_expires_seconds +
147 xp->curlft.add_time - now;
148 if (tmo <= 0)
149 goto expired;
150 if (tmo < next)
151 next = tmo;
152 }
153 if (xp->lft.hard_use_expires_seconds) {
154 long tmo = xp->lft.hard_use_expires_seconds +
155 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
156 if (tmo <= 0)
157 goto expired;
158 if (tmo < next)
159 next = tmo;
160 }
161 if (xp->lft.soft_add_expires_seconds) {
162 long tmo = xp->lft.soft_add_expires_seconds +
163 xp->curlft.add_time - now;
164 if (tmo <= 0) {
165 warn = 1;
166 tmo = XFRM_KM_TIMEOUT;
167 }
168 if (tmo < next)
169 next = tmo;
170 }
171 if (xp->lft.soft_use_expires_seconds) {
172 long tmo = xp->lft.soft_use_expires_seconds +
173 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
174 if (tmo <= 0) {
175 warn = 1;
176 tmo = XFRM_KM_TIMEOUT;
177 }
178 if (tmo < next)
179 next = tmo;
180 }
181
182 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800183 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (next != LONG_MAX &&
185 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
186 xfrm_pol_hold(xp);
187
188out:
189 read_unlock(&xp->lock);
190 xfrm_pol_put(xp);
191 return;
192
193expired:
194 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700195 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800196 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 xfrm_pol_put(xp);
198}
199
200
201/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
202 * SPD calls.
203 */
204
Al Virodd0fc662005-10-07 07:46:04 +0100205struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 struct xfrm_policy *policy;
208
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700209 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 if (policy) {
Timo Teras4c563f72008-02-28 21:31:08 -0800212 INIT_LIST_HEAD(&policy->bytype);
David S. Miller2518c7c2006-08-24 04:45:07 -0700213 INIT_HLIST_NODE(&policy->bydst);
214 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700216 atomic_set(&policy->refcnt, 1);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800217 setup_timer(&policy->timer, xfrm_policy_timer,
218 (unsigned long)policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220 return policy;
221}
222EXPORT_SYMBOL(xfrm_policy_alloc);
223
224/* Destroy xfrm_policy: descendant resources must be released to this moment. */
225
WANG Cong64c31b32008-01-07 22:34:29 -0800226void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Kris Katterjohn09a62662006-01-08 22:24:28 -0800228 BUG_ON(!policy->dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Kris Katterjohn09a62662006-01-08 22:24:28 -0800230 BUG_ON(policy->bundles);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 if (del_timer(&policy->timer))
233 BUG();
234
Timo Teras4c563f72008-02-28 21:31:08 -0800235 write_lock_bh(&xfrm_policy_lock);
236 list_del(&policy->bytype);
237 write_unlock_bh(&xfrm_policy_lock);
238
Trent Jaegerdf718372005-12-13 23:12:27 -0800239 security_xfrm_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 kfree(policy);
241}
WANG Cong64c31b32008-01-07 22:34:29 -0800242EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
245{
246 struct dst_entry *dst;
247
248 while ((dst = policy->bundles) != NULL) {
249 policy->bundles = dst->next;
250 dst_free(dst);
251 }
252
253 if (del_timer(&policy->timer))
254 atomic_dec(&policy->refcnt);
255
256 if (atomic_read(&policy->refcnt) > 1)
257 flow_cache_flush();
258
259 xfrm_pol_put(policy);
260}
261
David Howellsc4028952006-11-22 14:57:56 +0000262static void xfrm_policy_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 struct xfrm_policy *policy;
David S. Miller2518c7c2006-08-24 04:45:07 -0700265 struct hlist_node *entry, *tmp;
266 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 spin_lock_bh(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700269 gc_list.first = xfrm_policy_gc_list.first;
270 INIT_HLIST_HEAD(&xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 spin_unlock_bh(&xfrm_policy_gc_lock);
272
David S. Miller2518c7c2006-08-24 04:45:07 -0700273 hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 xfrm_policy_gc_kill(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277/* Rule must be locked. Release descentant resources, announce
278 * entry dead. The rule must be unlinked from lists to the moment.
279 */
280
281static void xfrm_policy_kill(struct xfrm_policy *policy)
282{
283 int dead;
284
285 write_lock_bh(&policy->lock);
286 dead = policy->dead;
287 policy->dead = 1;
288 write_unlock_bh(&policy->lock);
289
290 if (unlikely(dead)) {
291 WARN_ON(1);
292 return;
293 }
294
295 spin_lock(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700296 hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 spin_unlock(&xfrm_policy_gc_lock);
298
299 schedule_work(&xfrm_policy_gc_work);
300}
301
David S. Miller2518c7c2006-08-24 04:45:07 -0700302struct xfrm_policy_hash {
303 struct hlist_head *table;
304 unsigned int hmask;
305};
306
307static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
308static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
309static struct hlist_head *xfrm_policy_byidx __read_mostly;
310static unsigned int xfrm_idx_hmask __read_mostly;
311static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
312
David S. Miller2518c7c2006-08-24 04:45:07 -0700313static inline unsigned int idx_hash(u32 index)
314{
315 return __idx_hash(index, xfrm_idx_hmask);
316}
317
David S. Miller2518c7c2006-08-24 04:45:07 -0700318static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
319{
320 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
321 unsigned int hash = __sel_hash(sel, family, hmask);
322
323 return (hash == hmask + 1 ?
324 &xfrm_policy_inexact[dir] :
325 xfrm_policy_bydst[dir].table + hash);
326}
327
328static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
329{
330 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
331 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
332
333 return xfrm_policy_bydst[dir].table + hash;
334}
335
David S. Miller2518c7c2006-08-24 04:45:07 -0700336static void xfrm_dst_hash_transfer(struct hlist_head *list,
337 struct hlist_head *ndsttable,
338 unsigned int nhashmask)
339{
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800340 struct hlist_node *entry, *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700341 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800342 unsigned int h0 = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700343
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800344redo:
David S. Miller2518c7c2006-08-24 04:45:07 -0700345 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
346 unsigned int h;
347
348 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
349 pol->family, nhashmask);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800350 if (!entry0) {
351 hlist_del(entry);
352 hlist_add_head(&pol->bydst, ndsttable+h);
353 h0 = h;
354 } else {
355 if (h != h0)
356 continue;
357 hlist_del(entry);
358 hlist_add_after(entry0, &pol->bydst);
359 }
360 entry0 = entry;
361 }
362 if (!hlist_empty(list)) {
363 entry0 = NULL;
364 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700365 }
366}
367
368static void xfrm_idx_hash_transfer(struct hlist_head *list,
369 struct hlist_head *nidxtable,
370 unsigned int nhashmask)
371{
372 struct hlist_node *entry, *tmp;
373 struct xfrm_policy *pol;
374
375 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
376 unsigned int h;
377
378 h = __idx_hash(pol->index, nhashmask);
379 hlist_add_head(&pol->byidx, nidxtable+h);
380 }
381}
382
383static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
384{
385 return ((old_hmask + 1) << 1) - 1;
386}
387
388static void xfrm_bydst_resize(int dir)
389{
390 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
391 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
392 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
393 struct hlist_head *odst = xfrm_policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700394 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700395 int i;
396
397 if (!ndst)
398 return;
399
400 write_lock_bh(&xfrm_policy_lock);
401
402 for (i = hmask; i >= 0; i--)
403 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
404
405 xfrm_policy_bydst[dir].table = ndst;
406 xfrm_policy_bydst[dir].hmask = nhashmask;
407
408 write_unlock_bh(&xfrm_policy_lock);
409
David S. Miller44e36b42006-08-24 04:50:50 -0700410 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700411}
412
413static void xfrm_byidx_resize(int total)
414{
415 unsigned int hmask = xfrm_idx_hmask;
416 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
417 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
418 struct hlist_head *oidx = xfrm_policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700419 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700420 int i;
421
422 if (!nidx)
423 return;
424
425 write_lock_bh(&xfrm_policy_lock);
426
427 for (i = hmask; i >= 0; i--)
428 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
429
430 xfrm_policy_byidx = nidx;
431 xfrm_idx_hmask = nhashmask;
432
433 write_unlock_bh(&xfrm_policy_lock);
434
David S. Miller44e36b42006-08-24 04:50:50 -0700435 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700436}
437
438static inline int xfrm_bydst_should_resize(int dir, int *total)
439{
440 unsigned int cnt = xfrm_policy_count[dir];
441 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
442
443 if (total)
444 *total += cnt;
445
446 if ((hmask + 1) < xfrm_policy_hashmax &&
447 cnt > hmask)
448 return 1;
449
450 return 0;
451}
452
453static inline int xfrm_byidx_should_resize(int total)
454{
455 unsigned int hmask = xfrm_idx_hmask;
456
457 if ((hmask + 1) < xfrm_policy_hashmax &&
458 total > hmask)
459 return 1;
460
461 return 0;
462}
463
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700464void xfrm_spd_getinfo(struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700465{
466 read_lock_bh(&xfrm_policy_lock);
467 si->incnt = xfrm_policy_count[XFRM_POLICY_IN];
468 si->outcnt = xfrm_policy_count[XFRM_POLICY_OUT];
469 si->fwdcnt = xfrm_policy_count[XFRM_POLICY_FWD];
470 si->inscnt = xfrm_policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
471 si->outscnt = xfrm_policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
472 si->fwdscnt = xfrm_policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
473 si->spdhcnt = xfrm_idx_hmask;
474 si->spdhmcnt = xfrm_policy_hashmax;
475 read_unlock_bh(&xfrm_policy_lock);
476}
477EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700478
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700479static DEFINE_MUTEX(hash_resize_mutex);
David Howellsc4028952006-11-22 14:57:56 +0000480static void xfrm_hash_resize(struct work_struct *__unused)
David S. Miller2518c7c2006-08-24 04:45:07 -0700481{
482 int dir, total;
483
484 mutex_lock(&hash_resize_mutex);
485
486 total = 0;
487 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
488 if (xfrm_bydst_should_resize(dir, &total))
489 xfrm_bydst_resize(dir);
490 }
491 if (xfrm_byidx_should_resize(total))
492 xfrm_byidx_resize(total);
493
494 mutex_unlock(&hash_resize_mutex);
495}
496
David Howellsc4028952006-11-22 14:57:56 +0000497static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499/* Generate new index... KAME seems to generate them ordered by cost
500 * of an absolute inpredictability of ordering of rules. This will not pass. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700501static u32 xfrm_gen_index(u8 type, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 static u32 idx_generator;
504
505 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700506 struct hlist_node *entry;
507 struct hlist_head *list;
508 struct xfrm_policy *p;
509 u32 idx;
510 int found;
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 idx = (idx_generator | dir);
513 idx_generator += 8;
514 if (idx == 0)
515 idx = 8;
David S. Miller2518c7c2006-08-24 04:45:07 -0700516 list = xfrm_policy_byidx + idx_hash(idx);
517 found = 0;
518 hlist_for_each_entry(p, entry, list, byidx) {
519 if (p->index == idx) {
520 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700524 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return idx;
526 }
527}
528
David S. Miller2518c7c2006-08-24 04:45:07 -0700529static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
530{
531 u32 *p1 = (u32 *) s1;
532 u32 *p2 = (u32 *) s2;
533 int len = sizeof(struct xfrm_selector) / sizeof(u32);
534 int i;
535
536 for (i = 0; i < len; i++) {
537 if (p1[i] != p2[i])
538 return 1;
539 }
540
541 return 0;
542}
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
545{
David S. Miller2518c7c2006-08-24 04:45:07 -0700546 struct xfrm_policy *pol;
547 struct xfrm_policy *delpol;
548 struct hlist_head *chain;
Herbert Xua6c7ab52007-01-16 16:52:02 -0800549 struct hlist_node *entry, *newpos;
David S. Miller9b78a822005-12-22 07:39:48 -0800550 struct dst_entry *gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700553 chain = policy_hash_bysel(&policy->selector, policy->family, dir);
554 delpol = NULL;
555 newpos = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700556 hlist_for_each_entry(pol, entry, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800557 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700558 !selector_cmp(&pol->selector, &policy->selector) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800559 xfrm_sec_ctx_match(pol->security, policy->security) &&
560 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (excl) {
562 write_unlock_bh(&xfrm_policy_lock);
563 return -EEXIST;
564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 delpol = pol;
566 if (policy->priority > pol->priority)
567 continue;
568 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800569 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 continue;
571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (delpol)
573 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700576 hlist_add_after(newpos, &policy->bydst);
577 else
578 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 xfrm_pol_hold(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700580 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700582 if (delpol) {
583 hlist_del(&delpol->bydst);
584 hlist_del(&delpol->byidx);
585 xfrm_policy_count[dir]--;
586 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700587 policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700588 hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800589 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 policy->curlft.use_time = 0;
591 if (!mod_timer(&policy->timer, jiffies + HZ))
592 xfrm_pol_hold(policy);
Timo Teras4c563f72008-02-28 21:31:08 -0800593 list_add_tail(&policy->bytype, &xfrm_policy_bytype[policy->type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 write_unlock_bh(&xfrm_policy_lock);
595
David S. Miller9b78a822005-12-22 07:39:48 -0800596 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 xfrm_policy_kill(delpol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700598 else if (xfrm_bydst_should_resize(dir, NULL))
599 schedule_work(&xfrm_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800600
601 read_lock_bh(&xfrm_policy_lock);
602 gc_list = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700603 entry = &policy->bydst;
604 hlist_for_each_entry_continue(policy, entry, bydst) {
David S. Miller9b78a822005-12-22 07:39:48 -0800605 struct dst_entry *dst;
606
607 write_lock(&policy->lock);
608 dst = policy->bundles;
609 if (dst) {
610 struct dst_entry *tail = dst;
611 while (tail->next)
612 tail = tail->next;
613 tail->next = gc_list;
614 gc_list = dst;
615
616 policy->bundles = NULL;
617 }
618 write_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
David S. Miller9b78a822005-12-22 07:39:48 -0800620 read_unlock_bh(&xfrm_policy_lock);
621
622 while (gc_list) {
623 struct dst_entry *dst = gc_list;
624
625 gc_list = dst->next;
626 dst_free(dst);
627 }
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return 0;
630}
631EXPORT_SYMBOL(xfrm_policy_insert);
632
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700633struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
634 struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800635 struct xfrm_sec_ctx *ctx, int delete,
636 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
David S. Miller2518c7c2006-08-24 04:45:07 -0700638 struct xfrm_policy *pol, *ret;
639 struct hlist_head *chain;
640 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Eric Parisef41aaa2007-03-07 15:37:58 -0800642 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700644 chain = policy_hash_bysel(sel, sel->family, dir);
645 ret = NULL;
646 hlist_for_each_entry(pol, entry, chain, bydst) {
647 if (pol->type == type &&
648 !selector_cmp(sel, &pol->selector) &&
649 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700651 if (delete) {
Eric Parisef41aaa2007-03-07 15:37:58 -0800652 *err = security_xfrm_policy_delete(pol);
653 if (*err) {
654 write_unlock_bh(&xfrm_policy_lock);
655 return pol;
656 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700657 hlist_del(&pol->bydst);
658 hlist_del(&pol->byidx);
659 xfrm_policy_count[dir]--;
660 }
661 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 break;
663 }
664 }
665 write_unlock_bh(&xfrm_policy_lock);
666
David S. Miller2518c7c2006-08-24 04:45:07 -0700667 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700669 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700671 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
Trent Jaegerdf718372005-12-13 23:12:27 -0800673EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Eric Parisef41aaa2007-03-07 15:37:58 -0800675struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete,
676 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
David S. Miller2518c7c2006-08-24 04:45:07 -0700678 struct xfrm_policy *pol, *ret;
679 struct hlist_head *chain;
680 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Herbert Xub5505c62007-05-14 02:15:47 -0700682 *err = -ENOENT;
683 if (xfrm_policy_id2dir(id) != dir)
684 return NULL;
685
Eric Parisef41aaa2007-03-07 15:37:58 -0800686 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700688 chain = xfrm_policy_byidx + idx_hash(id);
689 ret = NULL;
690 hlist_for_each_entry(pol, entry, chain, byidx) {
691 if (pol->type == type && pol->index == id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700693 if (delete) {
Eric Parisef41aaa2007-03-07 15:37:58 -0800694 *err = security_xfrm_policy_delete(pol);
695 if (*err) {
696 write_unlock_bh(&xfrm_policy_lock);
697 return pol;
698 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700699 hlist_del(&pol->bydst);
700 hlist_del(&pol->byidx);
701 xfrm_policy_count[dir]--;
702 }
703 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 break;
705 }
706 }
707 write_unlock_bh(&xfrm_policy_lock);
708
David S. Miller2518c7c2006-08-24 04:45:07 -0700709 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700711 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700713 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715EXPORT_SYMBOL(xfrm_policy_byid);
716
Joy Latten4aa2e622007-06-04 19:05:57 -0400717#ifdef CONFIG_SECURITY_NETWORK_XFRM
718static inline int
719xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Joy Latten4aa2e622007-06-04 19:05:57 -0400721 int dir, err = 0;
722
723 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
724 struct xfrm_policy *pol;
725 struct hlist_node *entry;
726 int i;
727
728 hlist_for_each_entry(pol, entry,
729 &xfrm_policy_inexact[dir], bydst) {
730 if (pol->type != type)
731 continue;
732 err = security_xfrm_policy_delete(pol);
733 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700734 xfrm_audit_policy_delete(pol, 0,
735 audit_info->loginuid,
736 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400737 return err;
738 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900739 }
Joy Latten4aa2e622007-06-04 19:05:57 -0400740 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
741 hlist_for_each_entry(pol, entry,
742 xfrm_policy_bydst[dir].table + i,
743 bydst) {
744 if (pol->type != type)
745 continue;
746 err = security_xfrm_policy_delete(pol);
747 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700748 xfrm_audit_policy_delete(pol, 0,
749 audit_info->loginuid,
750 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400751 return err;
752 }
753 }
754 }
755 }
756 return err;
757}
758#else
759static inline int
760xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
761{
762 return 0;
763}
764#endif
765
766int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
767{
768 int dir, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 write_lock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400771
772 err = xfrm_policy_flush_secctx_check(type, audit_info);
773 if (err)
774 goto out;
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700777 struct xfrm_policy *pol;
778 struct hlist_node *entry;
David S. Millerae8c0572006-10-03 16:00:26 -0700779 int i, killed;
David S. Miller2518c7c2006-08-24 04:45:07 -0700780
David S. Millerae8c0572006-10-03 16:00:26 -0700781 killed = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700782 again1:
783 hlist_for_each_entry(pol, entry,
784 &xfrm_policy_inexact[dir], bydst) {
785 if (pol->type != type)
786 continue;
787 hlist_del(&pol->bydst);
788 hlist_del(&pol->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 write_unlock_bh(&xfrm_policy_lock);
790
Joy Lattenab5f5e82007-09-17 11:51:22 -0700791 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
792 audit_info->secid);
Joy Latten161a09e2006-11-27 13:11:54 -0600793
David S. Miller2518c7c2006-08-24 04:45:07 -0700794 xfrm_policy_kill(pol);
David S. Millerae8c0572006-10-03 16:00:26 -0700795 killed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700798 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700800
801 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
802 again2:
803 hlist_for_each_entry(pol, entry,
804 xfrm_policy_bydst[dir].table + i,
805 bydst) {
806 if (pol->type != type)
807 continue;
808 hlist_del(&pol->bydst);
809 hlist_del(&pol->byidx);
810 write_unlock_bh(&xfrm_policy_lock);
811
Joy Lattenab5f5e82007-09-17 11:51:22 -0700812 xfrm_audit_policy_delete(pol, 1,
813 audit_info->loginuid,
814 audit_info->secid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700815 xfrm_policy_kill(pol);
David S. Millerae8c0572006-10-03 16:00:26 -0700816 killed++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700817
818 write_lock_bh(&xfrm_policy_lock);
819 goto again2;
820 }
821 }
822
David S. Millerae8c0572006-10-03 16:00:26 -0700823 xfrm_policy_count[dir] -= killed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825 atomic_inc(&flow_cache_genid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400826out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 write_unlock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400828 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830EXPORT_SYMBOL(xfrm_policy_flush);
831
Timo Teras4c563f72008-02-28 21:31:08 -0800832int xfrm_policy_walk(struct xfrm_policy_walk *walk,
833 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 void *data)
835{
Timo Teras4c563f72008-02-28 21:31:08 -0800836 struct xfrm_policy *old, *pol, *last = NULL;
837 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Timo Teras4c563f72008-02-28 21:31:08 -0800839 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
840 walk->type != XFRM_POLICY_TYPE_ANY)
841 return -EINVAL;
842
843 if (walk->policy == NULL && walk->count != 0)
844 return 0;
845
846 old = pol = walk->policy;
847 walk->policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 read_lock_bh(&xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Timo Teras4c563f72008-02-28 21:31:08 -0800850 for (; walk->cur_type < XFRM_POLICY_TYPE_MAX; walk->cur_type++) {
851 if (walk->type != walk->cur_type &&
852 walk->type != XFRM_POLICY_TYPE_ANY)
853 continue;
David S. Miller2518c7c2006-08-24 04:45:07 -0700854
Timo Teras4c563f72008-02-28 21:31:08 -0800855 if (pol == NULL) {
856 pol = list_first_entry(&xfrm_policy_bytype[walk->cur_type],
857 struct xfrm_policy, bytype);
858 }
859 list_for_each_entry_from(pol, &xfrm_policy_bytype[walk->cur_type], bytype) {
860 if (pol->dead)
David S. Miller2518c7c2006-08-24 04:45:07 -0700861 continue;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800862 if (last) {
Timo Teras4c563f72008-02-28 21:31:08 -0800863 error = func(last, xfrm_policy_id2dir(last->index),
864 walk->count, data);
865 if (error) {
866 xfrm_pol_hold(last);
867 walk->policy = last;
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800868 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -0800869 }
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800870 }
871 last = pol;
Timo Teras4c563f72008-02-28 21:31:08 -0800872 walk->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
Timo Teras4c563f72008-02-28 21:31:08 -0800874 pol = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
Timo Teras4c563f72008-02-28 21:31:08 -0800876 if (walk->count == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800877 error = -ENOENT;
878 goto out;
879 }
Timo Teras4c563f72008-02-28 21:31:08 -0800880 if (last)
881 error = func(last, xfrm_policy_id2dir(last->index), 0, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882out:
883 read_unlock_bh(&xfrm_policy_lock);
Timo Teras4c563f72008-02-28 21:31:08 -0800884 if (old != NULL)
885 xfrm_pol_put(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return error;
887}
888EXPORT_SYMBOL(xfrm_policy_walk);
889
James Morris134b0fc2006-10-05 15:42:27 -0500890/*
891 * Find policy to apply to this flow.
892 *
893 * Returns 0 if policy found, else an -errno.
894 */
David S. Miller2518c7c2006-08-24 04:45:07 -0700895static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
896 u8 type, u16 family, int dir)
897{
898 struct xfrm_selector *sel = &pol->selector;
James Morris134b0fc2006-10-05 15:42:27 -0500899 int match, ret = -ESRCH;
David S. Miller2518c7c2006-08-24 04:45:07 -0700900
901 if (pol->family != family ||
902 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500903 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700904
905 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500906 if (match)
907 ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700908
James Morris134b0fc2006-10-05 15:42:27 -0500909 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700910}
911
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700912static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
913 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
James Morris134b0fc2006-10-05 15:42:27 -0500915 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700916 struct xfrm_policy *pol, *ret;
917 xfrm_address_t *daddr, *saddr;
918 struct hlist_node *entry;
919 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700920 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700921
922 daddr = xfrm_flowi_daddr(fl, family);
923 saddr = xfrm_flowi_saddr(fl, family);
924 if (unlikely(!daddr || !saddr))
925 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700928 chain = policy_hash_direct(daddr, saddr, family, dir);
929 ret = NULL;
930 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500931 err = xfrm_policy_match(pol, fl, type, family, dir);
932 if (err) {
933 if (err == -ESRCH)
934 continue;
935 else {
936 ret = ERR_PTR(err);
937 goto fail;
938 }
939 } else {
David S. Milleracba48e2006-08-25 15:46:46 -0700940 ret = pol;
941 priority = ret->priority;
942 break;
943 }
944 }
945 chain = &xfrm_policy_inexact[dir];
946 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500947 err = xfrm_policy_match(pol, fl, type, family, dir);
948 if (err) {
949 if (err == -ESRCH)
950 continue;
951 else {
952 ret = ERR_PTR(err);
953 goto fail;
954 }
955 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700956 ret = pol;
957 break;
958 }
959 }
David S. Milleracba48e2006-08-25 15:46:46 -0700960 if (ret)
961 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -0500962fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700964
David S. Miller2518c7c2006-08-24 04:45:07 -0700965 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700966}
967
James Morris134b0fc2006-10-05 15:42:27 -0500968static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700969 void **objp, atomic_t **obj_refp)
970{
971 struct xfrm_policy *pol;
James Morris134b0fc2006-10-05 15:42:27 -0500972 int err = 0;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700973
974#ifdef CONFIG_XFRM_SUB_POLICY
975 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
James Morris134b0fc2006-10-05 15:42:27 -0500976 if (IS_ERR(pol)) {
977 err = PTR_ERR(pol);
978 pol = NULL;
979 }
980 if (pol || err)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700981 goto end;
982#endif
983 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
James Morris134b0fc2006-10-05 15:42:27 -0500984 if (IS_ERR(pol)) {
985 err = PTR_ERR(pol);
986 pol = NULL;
987 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700988#ifdef CONFIG_XFRM_SUB_POLICY
David S. Miller2518c7c2006-08-24 04:45:07 -0700989end:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700990#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if ((*objp = (void *) pol) != NULL)
992 *obj_refp = &pol->refcnt;
James Morris134b0fc2006-10-05 15:42:27 -0500993 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Trent Jaegerdf718372005-12-13 23:12:27 -0800996static inline int policy_to_flow_dir(int dir)
997{
998 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900999 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1000 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1001 return dir;
1002 switch (dir) {
1003 default:
1004 case XFRM_POLICY_IN:
1005 return FLOW_DIR_IN;
1006 case XFRM_POLICY_OUT:
1007 return FLOW_DIR_OUT;
1008 case XFRM_POLICY_FWD:
1009 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001010 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001011}
1012
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001013static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 struct xfrm_policy *pol;
1016
1017 read_lock_bh(&xfrm_policy_lock);
1018 if ((pol = sk->sk_policy[dir]) != NULL) {
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001019 int match = xfrm_selector_match(&pol->selector, fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001021 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001022
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001023 if (match) {
1024 err = security_xfrm_policy_lookup(pol, fl->secid,
1025 policy_to_flow_dir(dir));
1026 if (!err)
1027 xfrm_pol_hold(pol);
1028 else if (err == -ESRCH)
1029 pol = NULL;
1030 else
1031 pol = ERR_PTR(err);
1032 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 pol = NULL;
1034 }
1035 read_unlock_bh(&xfrm_policy_lock);
1036 return pol;
1037}
1038
1039static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1040{
David S. Miller2518c7c2006-08-24 04:45:07 -07001041 struct hlist_head *chain = policy_hash_bysel(&pol->selector,
1042 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001043
David S. Miller2518c7c2006-08-24 04:45:07 -07001044 hlist_add_head(&pol->bydst, chain);
1045 hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
1046 xfrm_policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001048
1049 if (xfrm_bydst_should_resize(dir, NULL))
1050 schedule_work(&xfrm_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1054 int dir)
1055{
David S. Miller2518c7c2006-08-24 04:45:07 -07001056 if (hlist_unhashed(&pol->bydst))
1057 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
David S. Miller2518c7c2006-08-24 04:45:07 -07001059 hlist_del(&pol->bydst);
1060 hlist_del(&pol->byidx);
1061 xfrm_policy_count[dir]--;
1062
1063 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
Herbert Xu4666faa2005-06-18 22:43:22 -07001066int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 write_lock_bh(&xfrm_policy_lock);
1069 pol = __xfrm_policy_unlink(pol, dir);
1070 write_unlock_bh(&xfrm_policy_lock);
1071 if (pol) {
1072 if (dir < XFRM_POLICY_MAX)
1073 atomic_inc(&flow_cache_genid);
1074 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001077 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
David S. Millera70fcb02006-03-20 19:18:52 -08001079EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1082{
1083 struct xfrm_policy *old_pol;
1084
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001085#ifdef CONFIG_XFRM_SUB_POLICY
1086 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1087 return -EINVAL;
1088#endif
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 write_lock_bh(&xfrm_policy_lock);
1091 old_pol = sk->sk_policy[dir];
1092 sk->sk_policy[dir] = pol;
1093 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001094 pol->curlft.add_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001095 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1097 }
1098 if (old_pol)
1099 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1100 write_unlock_bh(&xfrm_policy_lock);
1101
1102 if (old_pol) {
1103 xfrm_policy_kill(old_pol);
1104 }
1105 return 0;
1106}
1107
1108static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1109{
1110 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
1111
1112 if (newp) {
1113 newp->selector = old->selector;
Trent Jaegerdf718372005-12-13 23:12:27 -08001114 if (security_xfrm_policy_clone(old, newp)) {
1115 kfree(newp);
1116 return NULL; /* ENOMEM */
1117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 newp->lft = old->lft;
1119 newp->curlft = old->curlft;
1120 newp->action = old->action;
1121 newp->flags = old->flags;
1122 newp->xfrm_nr = old->xfrm_nr;
1123 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001124 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 memcpy(newp->xfrm_vec, old->xfrm_vec,
1126 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1127 write_lock_bh(&xfrm_policy_lock);
1128 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1129 write_unlock_bh(&xfrm_policy_lock);
1130 xfrm_pol_put(newp);
1131 }
1132 return newp;
1133}
1134
1135int __xfrm_sk_clone_policy(struct sock *sk)
1136{
1137 struct xfrm_policy *p0 = sk->sk_policy[0],
1138 *p1 = sk->sk_policy[1];
1139
1140 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1141 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1142 return -ENOMEM;
1143 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1144 return -ENOMEM;
1145 return 0;
1146}
1147
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001148static int
1149xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
1150 unsigned short family)
1151{
1152 int err;
1153 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1154
1155 if (unlikely(afinfo == NULL))
1156 return -EINVAL;
1157 err = afinfo->get_saddr(local, remote);
1158 xfrm_policy_put_afinfo(afinfo);
1159 return err;
1160}
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162/* Resolve list of templates for the flow, given policy. */
1163
1164static int
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001165xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1166 struct xfrm_state **xfrm,
1167 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 int nx;
1170 int i, error;
1171 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1172 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001173 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1176 struct xfrm_state *x;
1177 xfrm_address_t *remote = daddr;
1178 xfrm_address_t *local = saddr;
1179 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1180
Joakim Koskela48b8d782007-07-26 00:08:42 -07001181 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1182 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 remote = &tmpl->id.daddr;
1184 local = &tmpl->saddr;
Miika Komu76b3f052006-11-30 16:40:43 -08001185 family = tmpl->encap_family;
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001186 if (xfrm_addr_any(local, family)) {
1187 error = xfrm_get_saddr(&tmp, remote, family);
1188 if (error)
1189 goto fail;
1190 local = &tmp;
1191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193
1194 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1195
1196 if (x && x->km.state == XFRM_STATE_VALID) {
1197 xfrm[nx++] = x;
1198 daddr = remote;
1199 saddr = local;
1200 continue;
1201 }
1202 if (x) {
1203 error = (x->km.state == XFRM_STATE_ERROR ?
1204 -EINVAL : -EAGAIN);
1205 xfrm_state_put(x);
1206 }
1207
1208 if (!tmpl->optional)
1209 goto fail;
1210 }
1211 return nx;
1212
1213fail:
1214 for (nx--; nx>=0; nx--)
1215 xfrm_state_put(xfrm[nx]);
1216 return error;
1217}
1218
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001219static int
1220xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1221 struct xfrm_state **xfrm,
1222 unsigned short family)
1223{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001224 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1225 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001226 int cnx = 0;
1227 int error;
1228 int ret;
1229 int i;
1230
1231 for (i = 0; i < npols; i++) {
1232 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1233 error = -ENOBUFS;
1234 goto fail;
1235 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001236
1237 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001238 if (ret < 0) {
1239 error = ret;
1240 goto fail;
1241 } else
1242 cnx += ret;
1243 }
1244
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001245 /* found states are sorted for outbound processing */
1246 if (npols > 1)
1247 xfrm_state_sort(xfrm, tpp, cnx, family);
1248
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001249 return cnx;
1250
1251 fail:
1252 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001253 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001254 return error;
1255
1256}
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258/* Check that the bundle accepts the flow and its components are
1259 * still valid.
1260 */
1261
1262static struct dst_entry *
1263xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1264{
1265 struct dst_entry *x;
1266 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1267 if (unlikely(afinfo == NULL))
1268 return ERR_PTR(-EINVAL);
1269 x = afinfo->find_bundle(fl, policy);
1270 xfrm_policy_put_afinfo(afinfo);
1271 return x;
1272}
1273
Herbert Xu25ee3282007-12-11 09:32:34 -08001274static inline int xfrm_get_tos(struct flowi *fl, int family)
1275{
1276 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1277 int tos;
1278
1279 if (!afinfo)
1280 return -EINVAL;
1281
1282 tos = afinfo->get_tos(fl);
1283
1284 xfrm_policy_put_afinfo(afinfo);
1285
1286 return tos;
1287}
1288
1289static inline struct xfrm_dst *xfrm_alloc_dst(int family)
1290{
1291 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1292 struct xfrm_dst *xdst;
1293
1294 if (!afinfo)
1295 return ERR_PTR(-EINVAL);
1296
1297 xdst = dst_alloc(afinfo->dst_ops) ?: ERR_PTR(-ENOBUFS);
1298
1299 xfrm_policy_put_afinfo(afinfo);
1300
1301 return xdst;
1302}
1303
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001304static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1305 int nfheader_len)
1306{
1307 struct xfrm_policy_afinfo *afinfo =
1308 xfrm_policy_get_afinfo(dst->ops->family);
1309 int err;
1310
1311 if (!afinfo)
1312 return -EINVAL;
1313
1314 err = afinfo->init_path(path, dst, nfheader_len);
1315
1316 xfrm_policy_put_afinfo(afinfo);
1317
1318 return err;
1319}
1320
Herbert Xu25ee3282007-12-11 09:32:34 -08001321static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev)
1322{
1323 struct xfrm_policy_afinfo *afinfo =
1324 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1325 int err;
1326
1327 if (!afinfo)
1328 return -EINVAL;
1329
1330 err = afinfo->fill_dst(xdst, dev);
1331
1332 xfrm_policy_put_afinfo(afinfo);
1333
1334 return err;
1335}
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1338 * all the metrics... Shortly, bundle a bundle.
1339 */
1340
Herbert Xu25ee3282007-12-11 09:32:34 -08001341static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1342 struct xfrm_state **xfrm, int nx,
1343 struct flowi *fl,
1344 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Herbert Xu25ee3282007-12-11 09:32:34 -08001346 unsigned long now = jiffies;
1347 struct net_device *dev;
1348 struct dst_entry *dst_prev = NULL;
1349 struct dst_entry *dst0 = NULL;
1350 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001352 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001353 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001354 int trailer_len = 0;
1355 int tos;
1356 int family = policy->selector.family;
1357
1358 tos = xfrm_get_tos(fl, family);
1359 err = tos;
1360 if (tos < 0)
1361 goto put_states;
1362
1363 dst_hold(dst);
1364
1365 for (; i < nx; i++) {
1366 struct xfrm_dst *xdst = xfrm_alloc_dst(family);
1367 struct dst_entry *dst1 = &xdst->u.dst;
1368
1369 err = PTR_ERR(xdst);
1370 if (IS_ERR(xdst)) {
1371 dst_release(dst);
1372 goto put_states;
1373 }
1374
1375 if (!dst_prev)
1376 dst0 = dst1;
1377 else {
1378 dst_prev->child = dst_clone(dst1);
1379 dst1->flags |= DST_NOHASH;
1380 }
1381
1382 xdst->route = dst;
1383 memcpy(&dst1->metrics, &dst->metrics, sizeof(dst->metrics));
1384
1385 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1386 family = xfrm[i]->props.family;
1387 dst = xfrm_dst_lookup(xfrm[i], tos, family);
1388 err = PTR_ERR(dst);
1389 if (IS_ERR(dst))
1390 goto put_states;
1391 } else
1392 dst_hold(dst);
1393
1394 dst1->xfrm = xfrm[i];
1395 xdst->genid = xfrm[i]->genid;
1396
1397 dst1->obsolete = -1;
1398 dst1->flags |= DST_HOST;
1399 dst1->lastuse = now;
1400
1401 dst1->input = dst_discard;
1402 dst1->output = xfrm[i]->outer_mode->afinfo->output;
1403
1404 dst1->next = dst_prev;
1405 dst_prev = dst1;
1406
1407 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001408 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1409 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001410 trailer_len += xfrm[i]->props.trailer_len;
1411 }
1412
1413 dst_prev->child = dst;
1414 dst0->path = dst;
1415
1416 err = -ENODEV;
1417 dev = dst->dev;
1418 if (!dev)
1419 goto free_dst;
1420
1421 /* Copy neighbout for reachability confirmation */
1422 dst0->neighbour = neigh_clone(dst->neighbour);
1423
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001424 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001425 xfrm_init_pmtu(dst_prev);
1426
1427 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1428 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1429
1430 err = xfrm_fill_dst(xdst, dev);
1431 if (err)
1432 goto free_dst;
1433
1434 dst_prev->header_len = header_len;
1435 dst_prev->trailer_len = trailer_len;
1436 header_len -= xdst->u.dst.xfrm->props.header_len;
1437 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1438 }
1439
1440out:
1441 return dst0;
1442
1443put_states:
1444 for (; i < nx; i++)
1445 xfrm_state_put(xfrm[i]);
1446free_dst:
1447 if (dst0)
1448 dst_free(dst0);
1449 dst0 = ERR_PTR(err);
1450 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001453static int inline
1454xfrm_dst_alloc_copy(void **target, void *src, int size)
1455{
1456 if (!*target) {
1457 *target = kmalloc(size, GFP_ATOMIC);
1458 if (!*target)
1459 return -ENOMEM;
1460 }
1461 memcpy(*target, src, size);
1462 return 0;
1463}
1464
1465static int inline
1466xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
1467{
1468#ifdef CONFIG_XFRM_SUB_POLICY
1469 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1470 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1471 sel, sizeof(*sel));
1472#else
1473 return 0;
1474#endif
1475}
1476
1477static int inline
1478xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
1479{
1480#ifdef CONFIG_XFRM_SUB_POLICY
1481 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1482 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1483#else
1484 return 0;
1485#endif
1486}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488static int stale_bundle(struct dst_entry *dst);
1489
1490/* Main function: finds/creates a bundle for given flow.
1491 *
1492 * At the moment we eat a raw IP route. Mostly to speed up lookups
1493 * on interfaces with disabled IPsec.
1494 */
David S. Miller14e50e52007-05-24 18:17:54 -07001495int __xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1496 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 struct xfrm_policy *policy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001499 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1500 int npols;
1501 int pol_dead;
1502 int xfrm_nr;
1503 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1505 struct dst_entry *dst, *dst_orig = *dst_p;
1506 int nx = 0;
1507 int err;
1508 u32 genid;
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001509 u16 family;
Trent Jaegerdf718372005-12-13 23:12:27 -08001510 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512restart:
1513 genid = atomic_read(&flow_cache_genid);
1514 policy = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001515 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1516 pols[pi] = NULL;
1517 npols = 0;
1518 pol_dead = 0;
1519 xfrm_nr = 0;
1520
Thomas Graff7944fb2007-08-25 13:46:55 -07001521 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001522 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
Herbert Xu75b8c132007-12-11 04:38:08 -08001523 err = PTR_ERR(policy);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001524 if (IS_ERR(policy)) {
1525 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLERROR);
Herbert Xu75b8c132007-12-11 04:38:08 -08001526 goto dropdst;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001527 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 if (!policy) {
1531 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07001532 if ((dst_orig->flags & DST_NOXFRM) ||
1533 !xfrm_policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08001534 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001536 policy = flow_cache_lookup(fl, dst_orig->ops->family,
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001537 dir, xfrm_policy_lookup);
Herbert Xu75b8c132007-12-11 04:38:08 -08001538 err = PTR_ERR(policy);
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08001539 if (IS_ERR(policy)) {
1540 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLERROR);
Herbert Xu75b8c132007-12-11 04:38:08 -08001541 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08001542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
1544
1545 if (!policy)
Herbert Xu8b7817f2007-12-12 10:44:43 -08001546 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001548 family = dst_orig->ops->family;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001549 pols[0] = policy;
1550 npols ++;
1551 xfrm_nr += pols[0]->xfrm_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Herbert Xuaef21782007-12-13 09:30:59 -08001553 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001554 if ((flags & XFRM_LOOKUP_ICMP) && !(policy->flags & XFRM_POLICY_ICMP))
1555 goto error;
1556
1557 policy->curlft.use_time = get_seconds();
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 switch (policy->action) {
Herbert Xu5e5234f2007-11-30 00:50:31 +11001560 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 case XFRM_POLICY_BLOCK:
1562 /* Prohibit the flow */
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001563 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye1044112005-09-08 15:11:55 -07001564 err = -EPERM;
1565 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 case XFRM_POLICY_ALLOW:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001568#ifndef CONFIG_XFRM_SUB_POLICY
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (policy->xfrm_nr == 0) {
1570 /* Flow passes not transformed. */
1571 xfrm_pol_put(policy);
1572 return 0;
1573 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001574#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 /* Try to find matching bundle.
1577 *
1578 * LATER: help from flow cache. It is optional, this
1579 * is required only for output policy.
1580 */
1581 dst = xfrm_find_bundle(fl, policy, family);
1582 if (IS_ERR(dst)) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001583 XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
Patrick McHardye1044112005-09-08 15:11:55 -07001584 err = PTR_ERR(dst);
1585 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587
1588 if (dst)
1589 break;
1590
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001591#ifdef CONFIG_XFRM_SUB_POLICY
1592 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1593 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1594 fl, family,
1595 XFRM_POLICY_OUT);
1596 if (pols[1]) {
James Morris134b0fc2006-10-05 15:42:27 -05001597 if (IS_ERR(pols[1])) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001598 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05001599 err = PTR_ERR(pols[1]);
1600 goto error;
1601 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001602 if (pols[1]->action == XFRM_POLICY_BLOCK) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001603 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001604 err = -EPERM;
1605 goto error;
1606 }
1607 npols ++;
1608 xfrm_nr += pols[1]->xfrm_nr;
1609 }
1610 }
1611
1612 /*
1613 * Because neither flowi nor bundle information knows about
1614 * transformation template size. On more than one policy usage
1615 * we can realize whether all of them is bypass or not after
1616 * they are searched. See above not-transformed bypass
1617 * is surrounded by non-sub policy configuration, too.
1618 */
1619 if (xfrm_nr == 0) {
1620 /* Flow passes not transformed. */
1621 xfrm_pols_put(pols, npols);
1622 return 0;
1623 }
1624
1625#endif
1626 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 if (unlikely(nx<0)) {
1629 err = nx;
David S. Miller14e50e52007-05-24 18:17:54 -07001630 if (err == -EAGAIN && sysctl_xfrm_larval_drop) {
1631 /* EREMOTE tells the caller to generate
1632 * a one-shot blackhole route.
1633 */
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08001634 XFRM_INC_STATS(LINUX_MIB_XFRMOUTNOSTATES);
David S. Miller14e50e52007-05-24 18:17:54 -07001635 xfrm_pol_put(policy);
1636 return -EREMOTE;
1637 }
Herbert Xu815f4e52007-12-12 10:36:59 -08001638 if (err == -EAGAIN && (flags & XFRM_LOOKUP_WAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 DECLARE_WAITQUEUE(wait, current);
1640
1641 add_wait_queue(&km_waitq, &wait);
1642 set_current_state(TASK_INTERRUPTIBLE);
1643 schedule();
1644 set_current_state(TASK_RUNNING);
1645 remove_wait_queue(&km_waitq, &wait);
1646
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001647 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 if (nx == -EAGAIN && signal_pending(current)) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001650 XFRM_INC_STATS(LINUX_MIB_XFRMOUTNOSTATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 err = -ERESTART;
1652 goto error;
1653 }
1654 if (nx == -EAGAIN ||
1655 genid != atomic_read(&flow_cache_genid)) {
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001656 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 goto restart;
1658 }
1659 err = nx;
1660 }
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001661 if (err < 0) {
1662 XFRM_INC_STATS(LINUX_MIB_XFRMOUTNOSTATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 goto error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 }
1666 if (nx == 0) {
1667 /* Flow passes not transformed. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001668 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return 0;
1670 }
1671
Herbert Xu25ee3282007-12-11 09:32:34 -08001672 dst = xfrm_bundle_create(policy, xfrm, nx, fl, dst_orig);
1673 err = PTR_ERR(dst);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001674 if (IS_ERR(dst)) {
1675 XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLEGENERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 goto error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001679 for (pi = 0; pi < npols; pi++) {
1680 read_lock_bh(&pols[pi]->lock);
1681 pol_dead |= pols[pi]->dead;
1682 read_unlock_bh(&pols[pi]->lock);
1683 }
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 write_lock_bh(&policy->lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001686 if (unlikely(pol_dead || stale_bundle(dst))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 /* Wow! While we worked on resolving, this
1688 * policy has gone. Retry. It is not paranoia,
1689 * we just cannot enlist new bundle to dead object.
1690 * We can't enlist stable bundles either.
1691 */
1692 write_unlock_bh(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 if (dst)
1694 dst_free(dst);
Herbert Xu00de6512006-02-13 16:01:27 -08001695
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001696 if (pol_dead)
1697 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLDEAD);
1698 else
1699 XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
Herbert Xu00de6512006-02-13 16:01:27 -08001700 err = -EHOSTUNREACH;
1701 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001703
1704 if (npols > 1)
1705 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1706 else
1707 err = xfrm_dst_update_origin(dst, fl);
1708 if (unlikely(err)) {
1709 write_unlock_bh(&policy->lock);
1710 if (dst)
1711 dst_free(dst);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001712 XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001713 goto error;
1714 }
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 dst->next = policy->bundles;
1717 policy->bundles = dst;
1718 dst_hold(dst);
1719 write_unlock_bh(&policy->lock);
1720 }
1721 *dst_p = dst;
1722 dst_release(dst_orig);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001723 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return 0;
1725
1726error:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001727 xfrm_pols_put(pols, npols);
Herbert Xu75b8c132007-12-11 04:38:08 -08001728dropdst:
1729 dst_release(dst_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 *dst_p = NULL;
1731 return err;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001732
1733nopol:
Herbert Xuaef21782007-12-13 09:30:59 -08001734 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001735 if (flags & XFRM_LOOKUP_ICMP)
1736 goto dropdst;
1737 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
David S. Miller14e50e52007-05-24 18:17:54 -07001739EXPORT_SYMBOL(__xfrm_lookup);
1740
1741int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1742 struct sock *sk, int flags)
1743{
1744 int err = __xfrm_lookup(dst_p, fl, sk, flags);
1745
1746 if (err == -EREMOTE) {
1747 dst_release(*dst_p);
1748 *dst_p = NULL;
1749 err = -EAGAIN;
1750 }
1751
1752 return err;
1753}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754EXPORT_SYMBOL(xfrm_lookup);
1755
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001756static inline int
1757xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1758{
1759 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001760
1761 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1762 return 0;
1763 x = skb->sp->xvec[idx];
1764 if (!x->type->reject)
1765 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07001766 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001767}
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769/* When skb is transformed back to its "native" form, we have to
1770 * check policy restrictions. At the moment we make this in maximally
1771 * stupid way. Shame on me. :-) Of course, connected sockets must
1772 * have policy cached at them.
1773 */
1774
1775static inline int
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001776xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 unsigned short family)
1778{
1779 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08001780 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 return x->id.proto == tmpl->id.proto &&
1782 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1783 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1784 x->props.mode == tmpl->mode &&
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07001785 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1786 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001787 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1788 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001791/*
1792 * 0 or more than 0 is returned when validation is succeeded (either bypass
1793 * because of optional transport mode, or next index of the mathced secpath
1794 * state with the template.
1795 * -1 is returned when no matching template is found.
1796 * Otherwise "-2 - errored_index" is returned.
1797 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798static inline int
1799xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1800 unsigned short family)
1801{
1802 int idx = start;
1803
1804 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001805 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return start;
1807 } else
1808 start = -1;
1809 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001810 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001812 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1813 if (start == -1)
1814 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 }
1818 return start;
1819}
1820
Herbert Xud5422ef2007-12-12 10:44:16 -08001821int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
1822 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
1824 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001825 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 if (unlikely(afinfo == NULL))
1828 return -EAFNOSUPPORT;
1829
Herbert Xud5422ef2007-12-12 10:44:16 -08001830 afinfo->decode_session(skb, fl, reverse);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07001831 err = security_xfrm_decode_session(skb, &fl->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001833 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
Herbert Xud5422ef2007-12-12 10:44:16 -08001835EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001837static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
1839 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001840 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001841 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 }
1845
1846 return 0;
1847}
1848
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001849int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 unsigned short family)
1851{
1852 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001853 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1854 int npols = 0;
1855 int xfrm_nr;
1856 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08001857 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08001859 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001860 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Herbert Xud5422ef2007-12-12 10:44:16 -08001862 reverse = dir & ~XFRM_POLICY_MASK;
1863 dir &= XFRM_POLICY_MASK;
1864 fl_dir = policy_to_flow_dir(dir);
1865
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001866 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
1867 XFRM_INC_STATS(LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001869 }
1870
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08001871 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 /* First, check used SA against their selectors. */
1874 if (skb->sp) {
1875 int i;
1876
1877 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001878 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001879 if (!xfrm_selector_match(&x->sel, &fl, family)) {
1880 XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
1884 }
1885
1886 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001887 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001888 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001889 if (IS_ERR(pol)) {
1890 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001891 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001892 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 if (!pol)
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001896 pol = flow_cache_lookup(&fl, family, fl_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 xfrm_policy_lookup);
1898
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001899 if (IS_ERR(pol)) {
1900 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05001901 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001902 }
James Morris134b0fc2006-10-05 15:42:27 -05001903
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001904 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001905 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001906 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001907 XFRM_INC_STATS(LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001908 return 0;
1909 }
1910 return 1;
1911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
James Morris9d729f72007-03-04 16:12:44 -08001913 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001915 pols[0] = pol;
1916 npols ++;
1917#ifdef CONFIG_XFRM_SUB_POLICY
1918 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1919 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1920 &fl, family,
1921 XFRM_POLICY_IN);
1922 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001923 if (IS_ERR(pols[1])) {
1924 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05001925 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001926 }
James Morris9d729f72007-03-04 16:12:44 -08001927 pols[1]->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001928 npols ++;
1929 }
1930 }
1931#endif
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if (pol->action == XFRM_POLICY_ALLOW) {
1934 struct sec_path *sp;
1935 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001936 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001937 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001938 struct xfrm_tmpl **tpp = tp;
1939 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 int i, k;
1941
1942 if ((sp = skb->sp) == NULL)
1943 sp = &dummy;
1944
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001945 for (pi = 0; pi < npols; pi++) {
1946 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001947 pols[pi]->action != XFRM_POLICY_ALLOW) {
1948 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001949 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001950 }
1951 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
1952 XFRM_INC_STATS(LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001953 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001954 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001955 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1956 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1957 }
1958 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001959 if (npols > 1) {
1960 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1961 tpp = stp;
1962 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 /* For each tunnel xfrm, find the first matching tmpl.
1965 * For each tmpl before that, find corresponding xfrm.
1966 * Order is _important_. Later we will implement
1967 * some barriers, but at the moment barriers
1968 * are implied between each two transformations.
1969 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001970 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1971 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001972 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001973 if (k < -1)
1974 /* "-2 - errored_index" returned */
1975 xerr_idx = -(2+k);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001976 XFRM_INC_STATS(LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
1980
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001981 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
1982 XFRM_INC_STATS(LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001986 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 return 1;
1988 }
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001989 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001992 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001993reject_error:
1994 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return 0;
1996}
1997EXPORT_SYMBOL(__xfrm_policy_check);
1998
1999int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2000{
2001 struct flowi fl;
2002
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002003 if (xfrm_decode_session(skb, &fl, family) < 0) {
2004 /* XXX: we should have something like FWDHDRERROR here. */
2005 XFRM_INC_STATS(LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
2010}
2011EXPORT_SYMBOL(__xfrm_route_forward);
2012
David S. Millerd49c73c2006-08-13 18:55:53 -07002013/* Optimize later using cookies and generation ids. */
2014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2016{
David S. Millerd49c73c2006-08-13 18:55:53 -07002017 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2018 * to "-1" to force all XFRM destinations to get validated by
2019 * dst_ops->check on every use. We do this because when a
2020 * normal route referenced by an XFRM dst is obsoleted we do
2021 * not go looking around for all parent referencing XFRM dsts
2022 * so that we can invalidate them. It is just too much work.
2023 * Instead we make the checks here on every use. For example:
2024 *
2025 * XFRM dst A --> IPv4 dst X
2026 *
2027 * X is the "xdst->route" of A (X is also the "dst->path" of A
2028 * in this example). If X is marked obsolete, "A" will not
2029 * notice. That's what we are validating here via the
2030 * stale_bundle() check.
2031 *
2032 * When a policy's bundle is pruned, we dst_free() the XFRM
2033 * dst which causes it's ->obsolete field to be set to a
2034 * positive non-zero integer. If an XFRM dst has been pruned
2035 * like this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002036 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002037 if (dst->obsolete < 0 && !stale_bundle(dst))
2038 return dst;
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return NULL;
2041}
2042
2043static int stale_bundle(struct dst_entry *dst)
2044{
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05002045 return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
2047
Herbert Xuaabc9762005-05-03 16:27:10 -07002048void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -08002051 dst->dev = dev->nd_net->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002052 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 dev_put(dev);
2054 }
2055}
Herbert Xuaabc9762005-05-03 16:27:10 -07002056EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
2058static void xfrm_link_failure(struct sk_buff *skb)
2059{
2060 /* Impossible. Such dst must be popped before reaches point of failure. */
2061 return;
2062}
2063
2064static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2065{
2066 if (dst) {
2067 if (dst->obsolete) {
2068 dst_release(dst);
2069 dst = NULL;
2070 }
2071 }
2072 return dst;
2073}
2074
David S. Miller2518c7c2006-08-24 04:45:07 -07002075static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
2076{
2077 struct dst_entry *dst, **dstp;
2078
2079 write_lock(&pol->lock);
2080 dstp = &pol->bundles;
2081 while ((dst=*dstp) != NULL) {
2082 if (func(dst)) {
2083 *dstp = dst->next;
2084 dst->next = *gc_list_p;
2085 *gc_list_p = dst;
2086 } else {
2087 dstp = &dst->next;
2088 }
2089 }
2090 write_unlock(&pol->lock);
2091}
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
2094{
David S. Miller2518c7c2006-08-24 04:45:07 -07002095 struct dst_entry *gc_list = NULL;
2096 int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07002099 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2100 struct xfrm_policy *pol;
2101 struct hlist_node *entry;
2102 struct hlist_head *table;
2103 int i;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002104
David S. Miller2518c7c2006-08-24 04:45:07 -07002105 hlist_for_each_entry(pol, entry,
2106 &xfrm_policy_inexact[dir], bydst)
2107 prune_one_bundle(pol, func, &gc_list);
2108
2109 table = xfrm_policy_bydst[dir].table;
2110 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
2111 hlist_for_each_entry(pol, entry, table + i, bydst)
2112 prune_one_bundle(pol, func, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114 }
2115 read_unlock_bh(&xfrm_policy_lock);
2116
2117 while (gc_list) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002118 struct dst_entry *dst = gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 gc_list = dst->next;
2120 dst_free(dst);
2121 }
2122}
2123
2124static int unused_bundle(struct dst_entry *dst)
2125{
2126 return !atomic_read(&dst->__refcnt);
2127}
2128
2129static void __xfrm_garbage_collect(void)
2130{
2131 xfrm_prune_bundles(unused_bundle);
2132}
2133
David S. Miller1c095392006-08-24 03:30:28 -07002134static int xfrm_flush_bundles(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
2136 xfrm_prune_bundles(stale_bundle);
2137 return 0;
2138}
2139
Herbert Xu25ee3282007-12-11 09:32:34 -08002140static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
2142 do {
2143 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2144 u32 pmtu, route_mtu_cached;
2145
2146 pmtu = dst_mtu(dst->child);
2147 xdst->child_mtu_cached = pmtu;
2148
2149 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2150
2151 route_mtu_cached = dst_mtu(xdst->route);
2152 xdst->route_mtu_cached = route_mtu_cached;
2153
2154 if (pmtu > route_mtu_cached)
2155 pmtu = route_mtu_cached;
2156
2157 dst->metrics[RTAX_MTU-1] = pmtu;
2158 } while ((dst = dst->next));
2159}
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161/* Check that the bundle accepts the flow and its components are
2162 * still valid.
2163 */
2164
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05002165int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
2166 struct flowi *fl, int family, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
2168 struct dst_entry *dst = &first->u.dst;
2169 struct xfrm_dst *last;
2170 u32 mtu;
2171
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002172 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 (dst->dev && !netif_running(dst->dev)))
2174 return 0;
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07002175#ifdef CONFIG_XFRM_SUB_POLICY
2176 if (fl) {
2177 if (first->origin && !flow_cache_uli_match(first->origin, fl))
2178 return 0;
2179 if (first->partner &&
2180 !xfrm_selector_match(first->partner, fl, family))
2181 return 0;
2182 }
2183#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 last = NULL;
2186
2187 do {
2188 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2189
2190 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
2191 return 0;
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06002192 if (fl && pol &&
2193 !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2196 return 0;
David S. Miller9d4a7062006-08-24 03:18:09 -07002197 if (xdst->genid != dst->xfrm->genid)
2198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Herbert Xu1bfcb102007-10-17 21:31:50 -07002200 if (strict && fl &&
Herbert Xu13996372007-10-17 21:35:51 -07002201 !(dst->xfrm->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07002202 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
2203 return 0;
2204
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 mtu = dst_mtu(dst->child);
2206 if (xdst->child_mtu_cached != mtu) {
2207 last = xdst;
2208 xdst->child_mtu_cached = mtu;
2209 }
2210
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002211 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 return 0;
2213 mtu = dst_mtu(xdst->route);
2214 if (xdst->route_mtu_cached != mtu) {
2215 last = xdst;
2216 xdst->route_mtu_cached = mtu;
2217 }
2218
2219 dst = dst->child;
2220 } while (dst->xfrm);
2221
2222 if (likely(!last))
2223 return 1;
2224
2225 mtu = last->child_mtu_cached;
2226 for (;;) {
2227 dst = &last->u.dst;
2228
2229 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2230 if (mtu > last->route_mtu_cached)
2231 mtu = last->route_mtu_cached;
2232 dst->metrics[RTAX_MTU-1] = mtu;
2233
2234 if (last == first)
2235 break;
2236
Patrick McHardybd0bf072007-07-18 01:55:52 -07002237 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 last->child_mtu_cached = mtu;
2239 }
2240
2241 return 1;
2242}
2243
2244EXPORT_SYMBOL(xfrm_bundle_ok);
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2247{
2248 int err = 0;
2249 if (unlikely(afinfo == NULL))
2250 return -EINVAL;
2251 if (unlikely(afinfo->family >= NPROTO))
2252 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002253 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2255 err = -ENOBUFS;
2256 else {
2257 struct dst_ops *dst_ops = afinfo->dst_ops;
2258 if (likely(dst_ops->kmem_cachep == NULL))
2259 dst_ops->kmem_cachep = xfrm_dst_cache;
2260 if (likely(dst_ops->check == NULL))
2261 dst_ops->check = xfrm_dst_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (likely(dst_ops->negative_advice == NULL))
2263 dst_ops->negative_advice = xfrm_negative_advice;
2264 if (likely(dst_ops->link_failure == NULL))
2265 dst_ops->link_failure = xfrm_link_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 if (likely(afinfo->garbage_collect == NULL))
2267 afinfo->garbage_collect = __xfrm_garbage_collect;
2268 xfrm_policy_afinfo[afinfo->family] = afinfo;
2269 }
Ingo Molnare959d812006-04-28 15:32:29 -07002270 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 return err;
2272}
2273EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2274
2275int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2276{
2277 int err = 0;
2278 if (unlikely(afinfo == NULL))
2279 return -EINVAL;
2280 if (unlikely(afinfo->family >= NPROTO))
2281 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002282 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2284 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2285 err = -EINVAL;
2286 else {
2287 struct dst_ops *dst_ops = afinfo->dst_ops;
2288 xfrm_policy_afinfo[afinfo->family] = NULL;
2289 dst_ops->kmem_cachep = NULL;
2290 dst_ops->check = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 dst_ops->negative_advice = NULL;
2292 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 afinfo->garbage_collect = NULL;
2294 }
2295 }
Ingo Molnare959d812006-04-28 15:32:29 -07002296 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return err;
2298}
2299EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2300
2301static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
2302{
2303 struct xfrm_policy_afinfo *afinfo;
2304 if (unlikely(family >= NPROTO))
2305 return NULL;
2306 read_lock(&xfrm_policy_afinfo_lock);
2307 afinfo = xfrm_policy_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07002308 if (unlikely(!afinfo))
2309 read_unlock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return afinfo;
2311}
2312
2313static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2314{
Herbert Xu546be242006-05-27 23:03:58 -07002315 read_unlock(&xfrm_policy_afinfo_lock);
2316}
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2319{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002320 struct net_device *dev = ptr;
2321
2322 if (dev->nd_net != &init_net)
2323 return NOTIFY_DONE;
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 switch (event) {
2326 case NETDEV_DOWN:
2327 xfrm_flush_bundles();
2328 }
2329 return NOTIFY_DONE;
2330}
2331
2332static struct notifier_block xfrm_dev_notifier = {
2333 xfrm_dev_event,
2334 NULL,
2335 0
2336};
2337
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002338#ifdef CONFIG_XFRM_STATISTICS
2339static int __init xfrm_statistics_init(void)
2340{
2341 if (snmp_mib_init((void **)xfrm_statistics,
2342 sizeof(struct linux_xfrm_mib)) < 0)
2343 return -ENOMEM;
2344 return 0;
2345}
2346#endif
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348static void __init xfrm_policy_init(void)
2349{
David S. Miller2518c7c2006-08-24 04:45:07 -07002350 unsigned int hmask, sz;
2351 int dir;
2352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2354 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07002355 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002356 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
David S. Miller2518c7c2006-08-24 04:45:07 -07002358 hmask = 8 - 1;
2359 sz = (hmask+1) * sizeof(struct hlist_head);
2360
David S. Miller44e36b42006-08-24 04:50:50 -07002361 xfrm_policy_byidx = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002362 xfrm_idx_hmask = hmask;
2363 if (!xfrm_policy_byidx)
2364 panic("XFRM: failed to allocate byidx hash\n");
2365
2366 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2367 struct xfrm_policy_hash *htab;
2368
2369 INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
2370
2371 htab = &xfrm_policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002372 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002373 htab->hmask = hmask;
2374 if (!htab->table)
2375 panic("XFRM: failed to allocate bydst hash\n");
2376 }
2377
Timo Teras4c563f72008-02-28 21:31:08 -08002378 for (dir = 0; dir < XFRM_POLICY_TYPE_MAX; dir++)
2379 INIT_LIST_HEAD(&xfrm_policy_bytype[dir]);
2380
David Howellsc4028952006-11-22 14:57:56 +00002381 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 register_netdevice_notifier(&xfrm_dev_notifier);
2383}
2384
2385void __init xfrm_init(void)
2386{
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002387#ifdef CONFIG_XFRM_STATISTICS
2388 xfrm_statistics_init();
2389#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 xfrm_state_init();
2391 xfrm_policy_init();
2392 xfrm_input_init();
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002393#ifdef CONFIG_XFRM_STATISTICS
2394 xfrm_proc_init();
2395#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396}
2397
Joy Lattenab5f5e82007-09-17 11:51:22 -07002398#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd2008-01-12 03:20:03 -08002399static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2400 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002401{
Paul Moore875179f2007-12-01 23:27:18 +11002402 struct xfrm_sec_ctx *ctx = xp->security;
2403 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002404
Paul Moore875179f2007-12-01 23:27:18 +11002405 if (ctx)
2406 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2407 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2408
2409 switch(sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002410 case AF_INET:
Paul Moore875179f2007-12-01 23:27:18 +11002411 audit_log_format(audit_buf, " src=" NIPQUAD_FMT,
2412 NIPQUAD(sel->saddr.a4));
2413 if (sel->prefixlen_s != 32)
2414 audit_log_format(audit_buf, " src_prefixlen=%d",
2415 sel->prefixlen_s);
2416 audit_log_format(audit_buf, " dst=" NIPQUAD_FMT,
2417 NIPQUAD(sel->daddr.a4));
2418 if (sel->prefixlen_d != 32)
2419 audit_log_format(audit_buf, " dst_prefixlen=%d",
2420 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002421 break;
2422 case AF_INET6:
Paul Moore875179f2007-12-01 23:27:18 +11002423 audit_log_format(audit_buf, " src=" NIP6_FMT,
2424 NIP6(*(struct in6_addr *)sel->saddr.a6));
2425 if (sel->prefixlen_s != 128)
2426 audit_log_format(audit_buf, " src_prefixlen=%d",
2427 sel->prefixlen_s);
2428 audit_log_format(audit_buf, " dst=" NIP6_FMT,
2429 NIP6(*(struct in6_addr *)sel->daddr.a6));
2430 if (sel->prefixlen_d != 128)
2431 audit_log_format(audit_buf, " dst_prefixlen=%d",
2432 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002433 break;
2434 }
2435}
2436
Paul Moore68277ac2007-12-20 20:49:33 -08002437void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
2438 u32 auid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002439{
2440 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002441
Paul Mooreafeb14b2007-12-21 14:58:11 -08002442 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002443 if (audit_buf == NULL)
2444 return;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002445 xfrm_audit_helper_usrinfo(auid, secid, audit_buf);
2446 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002447 xfrm_audit_common_policyinfo(xp, audit_buf);
2448 audit_log_end(audit_buf);
2449}
2450EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
2451
Paul Moore68277ac2007-12-20 20:49:33 -08002452void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
2453 u32 auid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002454{
2455 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002456
Paul Mooreafeb14b2007-12-21 14:58:11 -08002457 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002458 if (audit_buf == NULL)
2459 return;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002460 xfrm_audit_helper_usrinfo(auid, secid, audit_buf);
2461 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002462 xfrm_audit_common_policyinfo(xp, audit_buf);
2463 audit_log_end(audit_buf);
2464}
2465EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
2466#endif
2467
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002468#ifdef CONFIG_XFRM_MIGRATE
2469static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
2470 struct xfrm_selector *sel_tgt)
2471{
2472 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
2473 if (sel_tgt->family == sel_cmp->family &&
2474 xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002475 sel_cmp->family) == 0 &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002476 xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
2477 sel_cmp->family) == 0 &&
2478 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
2479 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
2480 return 1;
2481 }
2482 } else {
2483 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
2484 return 1;
2485 }
2486 }
2487 return 0;
2488}
2489
2490static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
2491 u8 dir, u8 type)
2492{
2493 struct xfrm_policy *pol, *ret = NULL;
2494 struct hlist_node *entry;
2495 struct hlist_head *chain;
2496 u32 priority = ~0U;
2497
2498 read_lock_bh(&xfrm_policy_lock);
2499 chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
2500 hlist_for_each_entry(pol, entry, chain, bydst) {
2501 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2502 pol->type == type) {
2503 ret = pol;
2504 priority = ret->priority;
2505 break;
2506 }
2507 }
2508 chain = &xfrm_policy_inexact[dir];
2509 hlist_for_each_entry(pol, entry, chain, bydst) {
2510 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2511 pol->type == type &&
2512 pol->priority < priority) {
2513 ret = pol;
2514 break;
2515 }
2516 }
2517
2518 if (ret)
2519 xfrm_pol_hold(ret);
2520
2521 read_unlock_bh(&xfrm_policy_lock);
2522
2523 return ret;
2524}
2525
2526static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
2527{
2528 int match = 0;
2529
2530 if (t->mode == m->mode && t->id.proto == m->proto &&
2531 (m->reqid == 0 || t->reqid == m->reqid)) {
2532 switch (t->mode) {
2533 case XFRM_MODE_TUNNEL:
2534 case XFRM_MODE_BEET:
2535 if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
2536 m->old_family) == 0 &&
2537 xfrm_addr_cmp(&t->saddr, &m->old_saddr,
2538 m->old_family) == 0) {
2539 match = 1;
2540 }
2541 break;
2542 case XFRM_MODE_TRANSPORT:
2543 /* in case of transport mode, template does not store
2544 any IP addresses, hence we just compare mode and
2545 protocol */
2546 match = 1;
2547 break;
2548 default:
2549 break;
2550 }
2551 }
2552 return match;
2553}
2554
2555/* update endpoint address(es) of template(s) */
2556static int xfrm_policy_migrate(struct xfrm_policy *pol,
2557 struct xfrm_migrate *m, int num_migrate)
2558{
2559 struct xfrm_migrate *mp;
2560 struct dst_entry *dst;
2561 int i, j, n = 0;
2562
2563 write_lock_bh(&pol->lock);
2564 if (unlikely(pol->dead)) {
2565 /* target policy has been deleted */
2566 write_unlock_bh(&pol->lock);
2567 return -ENOENT;
2568 }
2569
2570 for (i = 0; i < pol->xfrm_nr; i++) {
2571 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
2572 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
2573 continue;
2574 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07002575 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
2576 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002577 continue;
2578 /* update endpoints */
2579 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
2580 sizeof(pol->xfrm_vec[i].id.daddr));
2581 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
2582 sizeof(pol->xfrm_vec[i].saddr));
2583 pol->xfrm_vec[i].encap_family = mp->new_family;
2584 /* flush bundles */
2585 while ((dst = pol->bundles) != NULL) {
2586 pol->bundles = dst->next;
2587 dst_free(dst);
2588 }
2589 }
2590 }
2591
2592 write_unlock_bh(&pol->lock);
2593
2594 if (!n)
2595 return -ENODATA;
2596
2597 return 0;
2598}
2599
2600static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
2601{
2602 int i, j;
2603
2604 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
2605 return -EINVAL;
2606
2607 for (i = 0; i < num_migrate; i++) {
2608 if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
2609 m[i].old_family) == 0) &&
2610 (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
2611 m[i].old_family) == 0))
2612 return -EINVAL;
2613 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
2614 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
2615 return -EINVAL;
2616
2617 /* check if there is any duplicated entry */
2618 for (j = i + 1; j < num_migrate; j++) {
2619 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
2620 sizeof(m[i].old_daddr)) &&
2621 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
2622 sizeof(m[i].old_saddr)) &&
2623 m[i].proto == m[j].proto &&
2624 m[i].mode == m[j].mode &&
2625 m[i].reqid == m[j].reqid &&
2626 m[i].old_family == m[j].old_family)
2627 return -EINVAL;
2628 }
2629 }
2630
2631 return 0;
2632}
2633
2634int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
2635 struct xfrm_migrate *m, int num_migrate)
2636{
2637 int i, err, nx_cur = 0, nx_new = 0;
2638 struct xfrm_policy *pol = NULL;
2639 struct xfrm_state *x, *xc;
2640 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
2641 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
2642 struct xfrm_migrate *mp;
2643
2644 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
2645 goto out;
2646
2647 /* Stage 1 - find policy */
2648 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
2649 err = -ENOENT;
2650 goto out;
2651 }
2652
2653 /* Stage 2 - find and update state(s) */
2654 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
2655 if ((x = xfrm_migrate_state_find(mp))) {
2656 x_cur[nx_cur] = x;
2657 nx_cur++;
2658 if ((xc = xfrm_state_migrate(x, mp))) {
2659 x_new[nx_new] = xc;
2660 nx_new++;
2661 } else {
2662 err = -ENODATA;
2663 goto restore_state;
2664 }
2665 }
2666 }
2667
2668 /* Stage 3 - update policy */
2669 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
2670 goto restore_state;
2671
2672 /* Stage 4 - delete old state(s) */
2673 if (nx_cur) {
2674 xfrm_states_put(x_cur, nx_cur);
2675 xfrm_states_delete(x_cur, nx_cur);
2676 }
2677
2678 /* Stage 5 - announce */
2679 km_migrate(sel, dir, type, m, num_migrate);
2680
2681 xfrm_pol_put(pol);
2682
2683 return 0;
2684out:
2685 return err;
2686
2687restore_state:
2688 if (pol)
2689 xfrm_pol_put(pol);
2690 if (nx_cur)
2691 xfrm_states_put(x_cur, nx_cur);
2692 if (nx_new)
2693 xfrm_states_delete(x_new, nx_new);
2694
2695 return err;
2696}
David S. Millere610e672007-02-08 13:29:15 -08002697EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002698#endif