blob: 06ccc71c871fef465a374b9b18f651efce319a3e [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
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080037DEFINE_MUTEX(xfrm_cfg_mutex);
38EXPORT_SYMBOL(xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Timo Teräs80c802f2010-04-07 00:30:05 +000040static DEFINE_SPINLOCK(xfrm_policy_sk_bundle_lock);
41static struct dst_entry *xfrm_policy_sk_bundles;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static DEFINE_RWLOCK(xfrm_policy_lock);
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
45static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
46
Christoph Lametere18b8902006-12-06 20:33:20 -080047static struct kmem_cache *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
David S. Miller2518c7c2006-08-24 04:45:07 -070049static HLIST_HEAD(xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
51
52static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
53static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
Herbert Xu25ee3282007-12-11 09:32:34 -080054static void xfrm_init_pmtu(struct dst_entry *dst);
Timo Teräs80c802f2010-04-07 00:30:05 +000055static int stale_bundle(struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Wei Yongjun29fa0b302008-12-03 00:33:09 -080057static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
58 int dir);
59
Andrew Morton77681022006-11-08 22:46:26 -080060static inline int
61__xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
62{
63 return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
64 addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
65 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
66 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
67 (fl->proto == sel->proto || !sel->proto) &&
68 (fl->oif == sel->ifindex || !sel->ifindex);
69}
70
71static inline int
72__xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
73{
74 return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
75 addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
76 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
77 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
78 (fl->proto == sel->proto || !sel->proto) &&
79 (fl->oif == sel->ifindex || !sel->ifindex);
80}
81
82int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
83 unsigned short family)
84{
85 switch (family) {
86 case AF_INET:
87 return __xfrm4_selector_match(sel, fl);
88 case AF_INET6:
89 return __xfrm6_selector_match(sel, fl);
90 }
91 return 0;
92}
93
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -080094static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +090095 xfrm_address_t *saddr,
96 xfrm_address_t *daddr,
97 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Herbert Xu66cdb3c2007-11-13 21:37:28 -080099 struct xfrm_policy_afinfo *afinfo;
100 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Herbert Xu25ee3282007-12-11 09:32:34 -0800102 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800104 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800106 dst = afinfo->dst_lookup(net, tos, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900109
110 return dst;
111}
112
113static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
114 xfrm_address_t *prev_saddr,
115 xfrm_address_t *prev_daddr,
116 int family)
117{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800118 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900119 xfrm_address_t *saddr = &x->props.saddr;
120 xfrm_address_t *daddr = &x->id.daddr;
121 struct dst_entry *dst;
122
123 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
124 saddr = x->coaddr;
125 daddr = prev_daddr;
126 }
127 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
128 saddr = prev_saddr;
129 daddr = x->coaddr;
130 }
131
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800132 dst = __xfrm_dst_lookup(net, tos, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900133
134 if (!IS_ERR(dst)) {
135 if (prev_saddr != saddr)
136 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
137 if (prev_daddr != daddr)
138 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
139 }
140
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800141 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static inline unsigned long make_jiffies(long secs)
145{
146 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
147 return MAX_SCHEDULE_TIMEOUT-1;
148 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900149 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152static void xfrm_policy_timer(unsigned long data)
153{
154 struct xfrm_policy *xp = (struct xfrm_policy*)data;
James Morris9d729f72007-03-04 16:12:44 -0800155 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 long next = LONG_MAX;
157 int warn = 0;
158 int dir;
159
160 read_lock(&xp->lock);
161
Timo Teräsea2dea92010-03-31 00:17:05 +0000162 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 goto out;
164
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700165 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 if (xp->lft.hard_add_expires_seconds) {
168 long tmo = xp->lft.hard_add_expires_seconds +
169 xp->curlft.add_time - now;
170 if (tmo <= 0)
171 goto expired;
172 if (tmo < next)
173 next = tmo;
174 }
175 if (xp->lft.hard_use_expires_seconds) {
176 long tmo = xp->lft.hard_use_expires_seconds +
177 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
178 if (tmo <= 0)
179 goto expired;
180 if (tmo < next)
181 next = tmo;
182 }
183 if (xp->lft.soft_add_expires_seconds) {
184 long tmo = xp->lft.soft_add_expires_seconds +
185 xp->curlft.add_time - now;
186 if (tmo <= 0) {
187 warn = 1;
188 tmo = XFRM_KM_TIMEOUT;
189 }
190 if (tmo < next)
191 next = tmo;
192 }
193 if (xp->lft.soft_use_expires_seconds) {
194 long tmo = xp->lft.soft_use_expires_seconds +
195 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
196 if (tmo <= 0) {
197 warn = 1;
198 tmo = XFRM_KM_TIMEOUT;
199 }
200 if (tmo < next)
201 next = tmo;
202 }
203
204 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800205 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (next != LONG_MAX &&
207 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
208 xfrm_pol_hold(xp);
209
210out:
211 read_unlock(&xp->lock);
212 xfrm_pol_put(xp);
213 return;
214
215expired:
216 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700217 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800218 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 xfrm_pol_put(xp);
220}
221
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000222static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
223{
224 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
225
226 if (unlikely(pol->walk.dead))
227 flo = NULL;
228 else
229 xfrm_pol_hold(pol);
230
231 return flo;
232}
233
234static int xfrm_policy_flo_check(struct flow_cache_object *flo)
235{
236 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
237
238 return !pol->walk.dead;
239}
240
241static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
242{
243 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
244}
245
246static const struct flow_cache_ops xfrm_policy_fc_ops = {
247 .get = xfrm_policy_flo_get,
248 .check = xfrm_policy_flo_check,
249 .delete = xfrm_policy_flo_delete,
250};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
253 * SPD calls.
254 */
255
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800256struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct xfrm_policy *policy;
259
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700260 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800263 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700264 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700265 INIT_HLIST_NODE(&policy->bydst);
266 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700268 atomic_set(&policy->refcnt, 1);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800269 setup_timer(&policy->timer, xfrm_policy_timer,
270 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000271 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273 return policy;
274}
275EXPORT_SYMBOL(xfrm_policy_alloc);
276
277/* Destroy xfrm_policy: descendant resources must be released to this moment. */
278
WANG Cong64c31b32008-01-07 22:34:29 -0800279void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Herbert Xu12a169e2008-10-01 07:03:24 -0700281 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (del_timer(&policy->timer))
284 BUG();
285
Paul Moore03e1ad72008-04-12 19:07:52 -0700286 security_xfrm_policy_free(policy->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 kfree(policy);
288}
WANG Cong64c31b32008-01-07 22:34:29 -0800289EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
292{
Timo Teräs80c802f2010-04-07 00:30:05 +0000293 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 if (del_timer(&policy->timer))
296 atomic_dec(&policy->refcnt);
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 xfrm_pol_put(policy);
299}
300
David Howellsc4028952006-11-22 14:57:56 +0000301static void xfrm_policy_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct xfrm_policy *policy;
David S. Miller2518c7c2006-08-24 04:45:07 -0700304 struct hlist_node *entry, *tmp;
305 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 spin_lock_bh(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700308 gc_list.first = xfrm_policy_gc_list.first;
309 INIT_HLIST_HEAD(&xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 spin_unlock_bh(&xfrm_policy_gc_lock);
311
David S. Miller2518c7c2006-08-24 04:45:07 -0700312 hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 xfrm_policy_gc_kill(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
Alexey Dobriyanc9583962008-11-25 17:13:59 -0800315static DECLARE_WORK(xfrm_policy_gc_work, xfrm_policy_gc_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317/* Rule must be locked. Release descentant resources, announce
318 * entry dead. The rule must be unlinked from lists to the moment.
319 */
320
321static void xfrm_policy_kill(struct xfrm_policy *policy)
322{
Herbert Xu12a169e2008-10-01 07:03:24 -0700323 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Alexey Dobriyanbbb770e2008-11-03 19:11:29 -0800325 spin_lock_bh(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700326 hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
Alexey Dobriyanbbb770e2008-11-03 19:11:29 -0800327 spin_unlock_bh(&xfrm_policy_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 schedule_work(&xfrm_policy_gc_work);
330}
331
David S. Miller2518c7c2006-08-24 04:45:07 -0700332static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
333
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800334static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700335{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800336 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700337}
338
Alexey Dobriyan11219942008-11-25 17:33:06 -0800339static struct hlist_head *policy_hash_bysel(struct net *net, struct xfrm_selector *sel, unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700340{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800341 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700342 unsigned int hash = __sel_hash(sel, family, hmask);
343
344 return (hash == hmask + 1 ?
Alexey Dobriyan11219942008-11-25 17:33:06 -0800345 &net->xfrm.policy_inexact[dir] :
346 net->xfrm.policy_bydst[dir].table + hash);
David S. Miller2518c7c2006-08-24 04:45:07 -0700347}
348
Alexey Dobriyan11219942008-11-25 17:33:06 -0800349static struct hlist_head *policy_hash_direct(struct net *net, xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700350{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800351 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700352 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
353
Alexey Dobriyan11219942008-11-25 17:33:06 -0800354 return net->xfrm.policy_bydst[dir].table + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700355}
356
David S. Miller2518c7c2006-08-24 04:45:07 -0700357static void xfrm_dst_hash_transfer(struct hlist_head *list,
358 struct hlist_head *ndsttable,
359 unsigned int nhashmask)
360{
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800361 struct hlist_node *entry, *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700362 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800363 unsigned int h0 = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700364
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800365redo:
David S. Miller2518c7c2006-08-24 04:45:07 -0700366 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
367 unsigned int h;
368
369 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
370 pol->family, nhashmask);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800371 if (!entry0) {
372 hlist_del(entry);
373 hlist_add_head(&pol->bydst, ndsttable+h);
374 h0 = h;
375 } else {
376 if (h != h0)
377 continue;
378 hlist_del(entry);
379 hlist_add_after(entry0, &pol->bydst);
380 }
381 entry0 = entry;
382 }
383 if (!hlist_empty(list)) {
384 entry0 = NULL;
385 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700386 }
387}
388
389static void xfrm_idx_hash_transfer(struct hlist_head *list,
390 struct hlist_head *nidxtable,
391 unsigned int nhashmask)
392{
393 struct hlist_node *entry, *tmp;
394 struct xfrm_policy *pol;
395
396 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
397 unsigned int h;
398
399 h = __idx_hash(pol->index, nhashmask);
400 hlist_add_head(&pol->byidx, nidxtable+h);
401 }
402}
403
404static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
405{
406 return ((old_hmask + 1) << 1) - 1;
407}
408
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800409static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700410{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800411 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700412 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
413 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800414 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700415 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700416 int i;
417
418 if (!ndst)
419 return;
420
421 write_lock_bh(&xfrm_policy_lock);
422
423 for (i = hmask; i >= 0; i--)
424 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
425
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800426 net->xfrm.policy_bydst[dir].table = ndst;
427 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700428
429 write_unlock_bh(&xfrm_policy_lock);
430
David S. Miller44e36b42006-08-24 04:50:50 -0700431 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700432}
433
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800434static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700435{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800436 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700437 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
438 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800439 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700440 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700441 int i;
442
443 if (!nidx)
444 return;
445
446 write_lock_bh(&xfrm_policy_lock);
447
448 for (i = hmask; i >= 0; i--)
449 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
450
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800451 net->xfrm.policy_byidx = nidx;
452 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700453
454 write_unlock_bh(&xfrm_policy_lock);
455
David S. Miller44e36b42006-08-24 04:50:50 -0700456 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700457}
458
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800459static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700460{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800461 unsigned int cnt = net->xfrm.policy_count[dir];
462 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700463
464 if (total)
465 *total += cnt;
466
467 if ((hmask + 1) < xfrm_policy_hashmax &&
468 cnt > hmask)
469 return 1;
470
471 return 0;
472}
473
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800474static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700475{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800476 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700477
478 if ((hmask + 1) < xfrm_policy_hashmax &&
479 total > hmask)
480 return 1;
481
482 return 0;
483}
484
Alexey Dobriyane0710412010-01-23 13:37:10 +0000485void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700486{
487 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000488 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
489 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
490 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
491 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
492 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
493 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
494 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700495 si->spdhmcnt = xfrm_policy_hashmax;
496 read_unlock_bh(&xfrm_policy_lock);
497}
498EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700499
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700500static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800501static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700502{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800503 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700504 int dir, total;
505
506 mutex_lock(&hash_resize_mutex);
507
508 total = 0;
509 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800510 if (xfrm_bydst_should_resize(net, dir, &total))
511 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700512 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800513 if (xfrm_byidx_should_resize(net, total))
514 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700515
516 mutex_unlock(&hash_resize_mutex);
517}
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519/* Generate new index... KAME seems to generate them ordered by cost
520 * of an absolute inpredictability of ordering of rules. This will not pass. */
Alexey Dobriyan11219942008-11-25 17:33:06 -0800521static u32 xfrm_gen_index(struct net *net, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 static u32 idx_generator;
524
525 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700526 struct hlist_node *entry;
527 struct hlist_head *list;
528 struct xfrm_policy *p;
529 u32 idx;
530 int found;
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 idx = (idx_generator | dir);
533 idx_generator += 8;
534 if (idx == 0)
535 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800536 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700537 found = 0;
538 hlist_for_each_entry(p, entry, list, byidx) {
539 if (p->index == idx) {
540 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700544 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return idx;
546 }
547}
548
David S. Miller2518c7c2006-08-24 04:45:07 -0700549static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
550{
551 u32 *p1 = (u32 *) s1;
552 u32 *p2 = (u32 *) s2;
553 int len = sizeof(struct xfrm_selector) / sizeof(u32);
554 int i;
555
556 for (i = 0; i < len; i++) {
557 if (p1[i] != p2[i])
558 return 1;
559 }
560
561 return 0;
562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
565{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800566 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700567 struct xfrm_policy *pol;
568 struct xfrm_policy *delpol;
569 struct hlist_head *chain;
Herbert Xua6c7ab52007-01-16 16:52:02 -0800570 struct hlist_node *entry, *newpos;
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000571 u32 mark = policy->mark.v & policy->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800574 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700575 delpol = NULL;
576 newpos = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700577 hlist_for_each_entry(pol, entry, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800578 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700579 !selector_cmp(&pol->selector, &policy->selector) &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000580 (mark & pol->mark.m) == pol->mark.v &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800581 xfrm_sec_ctx_match(pol->security, policy->security) &&
582 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (excl) {
584 write_unlock_bh(&xfrm_policy_lock);
585 return -EEXIST;
586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 delpol = pol;
588 if (policy->priority > pol->priority)
589 continue;
590 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800591 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 continue;
593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (delpol)
595 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700598 hlist_add_after(newpos, &policy->bydst);
599 else
600 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800602 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 atomic_inc(&flow_cache_genid);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800604 if (delpol)
605 __xfrm_policy_unlink(delpol, dir);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800606 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir);
607 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800608 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 policy->curlft.use_time = 0;
610 if (!mod_timer(&policy->timer, jiffies + HZ))
611 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800612 list_add(&policy->walk.all, &net->xfrm.policy_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 write_unlock_bh(&xfrm_policy_lock);
614
David S. Miller9b78a822005-12-22 07:39:48 -0800615 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800617 else if (xfrm_bydst_should_resize(net, dir, NULL))
618 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return 0;
621}
622EXPORT_SYMBOL(xfrm_policy_insert);
623
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000624struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
625 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800626 struct xfrm_sec_ctx *ctx, int delete,
627 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
David S. Miller2518c7c2006-08-24 04:45:07 -0700629 struct xfrm_policy *pol, *ret;
630 struct hlist_head *chain;
631 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Eric Parisef41aaa2007-03-07 15:37:58 -0800633 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800635 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700636 ret = NULL;
637 hlist_for_each_entry(pol, entry, chain, bydst) {
638 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000639 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700640 !selector_cmp(sel, &pol->selector) &&
641 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700643 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700644 *err = security_xfrm_policy_delete(
645 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800646 if (*err) {
647 write_unlock_bh(&xfrm_policy_lock);
648 return pol;
649 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800650 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700651 }
652 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 break;
654 }
655 }
656 write_unlock_bh(&xfrm_policy_lock);
657
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000658 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700659 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700660 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
Trent Jaegerdf718372005-12-13 23:12:27 -0800662EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000664struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
665 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
David S. Miller2518c7c2006-08-24 04:45:07 -0700667 struct xfrm_policy *pol, *ret;
668 struct hlist_head *chain;
669 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Herbert Xub5505c62007-05-14 02:15:47 -0700671 *err = -ENOENT;
672 if (xfrm_policy_id2dir(id) != dir)
673 return NULL;
674
Eric Parisef41aaa2007-03-07 15:37:58 -0800675 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800677 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700678 ret = NULL;
679 hlist_for_each_entry(pol, entry, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000680 if (pol->type == type && pol->index == id &&
681 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700683 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700684 *err = security_xfrm_policy_delete(
685 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800686 if (*err) {
687 write_unlock_bh(&xfrm_policy_lock);
688 return pol;
689 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800690 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700691 }
692 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 break;
694 }
695 }
696 write_unlock_bh(&xfrm_policy_lock);
697
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000698 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700699 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700700 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702EXPORT_SYMBOL(xfrm_policy_byid);
703
Joy Latten4aa2e622007-06-04 19:05:57 -0400704#ifdef CONFIG_SECURITY_NETWORK_XFRM
705static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800706xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Joy Latten4aa2e622007-06-04 19:05:57 -0400708 int dir, err = 0;
709
710 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
711 struct xfrm_policy *pol;
712 struct hlist_node *entry;
713 int i;
714
715 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800716 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400717 if (pol->type != type)
718 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700719 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400720 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700721 xfrm_audit_policy_delete(pol, 0,
722 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400723 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700724 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400725 return err;
726 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900727 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800728 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400729 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800730 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400731 bydst) {
732 if (pol->type != type)
733 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700734 err = security_xfrm_policy_delete(
735 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400736 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700737 xfrm_audit_policy_delete(pol, 0,
738 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400739 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700740 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400741 return err;
742 }
743 }
744 }
745 }
746 return err;
747}
748#else
749static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800750xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400751{
752 return 0;
753}
754#endif
755
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800756int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400757{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000758 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 write_lock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400761
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800762 err = xfrm_policy_flush_secctx_check(net, type, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400763 if (err)
764 goto out;
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700767 struct xfrm_policy *pol;
768 struct hlist_node *entry;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800769 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700770
771 again1:
772 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800773 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700774 if (pol->type != type)
775 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000776 __xfrm_policy_unlink(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000778 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Joy Lattenab5f5e82007-09-17 11:51:22 -0700780 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400781 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700782 audit_info->secid);
Joy Latten161a09e2006-11-27 13:11:54 -0600783
David S. Miller2518c7c2006-08-24 04:45:07 -0700784 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700787 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700789
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800790 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700791 again2:
792 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800793 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700794 bydst) {
795 if (pol->type != type)
796 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000797 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700798 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000799 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700800
Joy Lattenab5f5e82007-09-17 11:51:22 -0700801 xfrm_audit_policy_delete(pol, 1,
802 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400803 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700804 audit_info->secid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700805 xfrm_policy_kill(pol);
806
807 write_lock_bh(&xfrm_policy_lock);
808 goto again2;
809 }
810 }
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000813 if (!cnt)
814 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400815out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 write_unlock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400817 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819EXPORT_SYMBOL(xfrm_policy_flush);
820
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800821int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800822 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 void *data)
824{
Herbert Xu12a169e2008-10-01 07:03:24 -0700825 struct xfrm_policy *pol;
826 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -0800827 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Timo Teras4c563f72008-02-28 21:31:08 -0800829 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
830 walk->type != XFRM_POLICY_TYPE_ANY)
831 return -EINVAL;
832
Herbert Xu12a169e2008-10-01 07:03:24 -0700833 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -0800834 return 0;
835
Herbert Xu12a169e2008-10-01 07:03:24 -0700836 write_lock_bh(&xfrm_policy_lock);
837 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800838 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -0700839 else
840 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800841 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -0700842 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -0800843 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -0700844 pol = container_of(x, struct xfrm_policy, walk);
845 if (walk->type != XFRM_POLICY_TYPE_ANY &&
846 walk->type != pol->type)
847 continue;
848 error = func(pol, xfrm_policy_id2dir(pol->index),
849 walk->seq, data);
850 if (error) {
851 list_move_tail(&walk->walk.all, &x->all);
852 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -0800853 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700854 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700856 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800857 error = -ENOENT;
858 goto out;
859 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700860 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861out:
Herbert Xu12a169e2008-10-01 07:03:24 -0700862 write_unlock_bh(&xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return error;
864}
865EXPORT_SYMBOL(xfrm_policy_walk);
866
Herbert Xu12a169e2008-10-01 07:03:24 -0700867void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
868{
869 INIT_LIST_HEAD(&walk->walk.all);
870 walk->walk.dead = 1;
871 walk->type = type;
872 walk->seq = 0;
873}
874EXPORT_SYMBOL(xfrm_policy_walk_init);
875
876void xfrm_policy_walk_done(struct xfrm_policy_walk *walk)
877{
878 if (list_empty(&walk->walk.all))
879 return;
880
881 write_lock_bh(&xfrm_policy_lock);
882 list_del(&walk->walk.all);
883 write_unlock_bh(&xfrm_policy_lock);
884}
885EXPORT_SYMBOL(xfrm_policy_walk_done);
886
James Morris134b0fc2006-10-05 15:42:27 -0500887/*
888 * Find policy to apply to this flow.
889 *
890 * Returns 0 if policy found, else an -errno.
891 */
David S. Miller2518c7c2006-08-24 04:45:07 -0700892static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
893 u8 type, u16 family, int dir)
894{
895 struct xfrm_selector *sel = &pol->selector;
James Morris134b0fc2006-10-05 15:42:27 -0500896 int match, ret = -ESRCH;
David S. Miller2518c7c2006-08-24 04:45:07 -0700897
898 if (pol->family != family ||
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000899 (fl->mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -0700900 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500901 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700902
903 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500904 if (match)
Paul Moore03e1ad72008-04-12 19:07:52 -0700905 ret = security_xfrm_policy_lookup(pol->security, fl->secid,
906 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700907
James Morris134b0fc2006-10-05 15:42:27 -0500908 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700909}
910
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800911static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
912 struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700913 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);
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800928 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700929 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 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800945 chain = &net->xfrm.policy_inexact[dir];
David S. Milleracba48e2006-08-25 15:46:46 -0700946 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
Timo Teräs80c802f2010-04-07 00:30:05 +0000968static struct xfrm_policy *
969__xfrm_policy_lookup(struct net *net, struct flowi *fl, u16 family, u8 dir)
970{
971#ifdef CONFIG_XFRM_SUB_POLICY
972 struct xfrm_policy *pol;
973
974 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
975 if (pol != NULL)
976 return pol;
977#endif
978 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
979}
980
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000981static struct flow_cache_object *
982xfrm_policy_lookup(struct net *net, struct flowi *fl, u16 family,
983 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700984{
985 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000986
987 if (old_obj)
988 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700989
Timo Teräs80c802f2010-04-07 00:30:05 +0000990 pol = __xfrm_policy_lookup(net, fl, family, dir);
991 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000992 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000993
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000994 /* Resolver returns two references:
995 * one for cache and one for caller of flow_cache_lookup() */
996 xfrm_pol_hold(pol);
997
998 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
Trent Jaegerdf718372005-12-13 23:12:27 -08001001static inline int policy_to_flow_dir(int dir)
1002{
1003 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001004 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1005 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1006 return dir;
1007 switch (dir) {
1008 default:
1009 case XFRM_POLICY_IN:
1010 return FLOW_DIR_IN;
1011 case XFRM_POLICY_OUT:
1012 return FLOW_DIR_OUT;
1013 case XFRM_POLICY_FWD:
1014 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001015 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001016}
1017
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001018static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 struct xfrm_policy *pol;
1021
1022 read_lock_bh(&xfrm_policy_lock);
1023 if ((pol = sk->sk_policy[dir]) != NULL) {
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001024 int match = xfrm_selector_match(&pol->selector, fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001026 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001027
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001028 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001029 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1030 pol = NULL;
1031 goto out;
1032 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001033 err = security_xfrm_policy_lookup(pol->security,
1034 fl->secid,
1035 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001036 if (!err)
1037 xfrm_pol_hold(pol);
1038 else if (err == -ESRCH)
1039 pol = NULL;
1040 else
1041 pol = ERR_PTR(err);
1042 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 pol = NULL;
1044 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001045out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 read_unlock_bh(&xfrm_policy_lock);
1047 return pol;
1048}
1049
1050static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1051{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001052 struct net *net = xp_net(pol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001053 struct hlist_head *chain = policy_hash_bysel(net, &pol->selector,
David S. Miller2518c7c2006-08-24 04:45:07 -07001054 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001055
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001056 list_add(&pol->walk.all, &net->xfrm.policy_all);
David S. Miller2518c7c2006-08-24 04:45:07 -07001057 hlist_add_head(&pol->bydst, chain);
Alexey Dobriyane92303f2008-11-25 17:32:41 -08001058 hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001059 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001061
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001062 if (xfrm_bydst_should_resize(net, dir, NULL))
1063 schedule_work(&net->xfrm.policy_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
1066static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1067 int dir)
1068{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001069 struct net *net = xp_net(pol);
1070
David S. Miller2518c7c2006-08-24 04:45:07 -07001071 if (hlist_unhashed(&pol->bydst))
1072 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
David S. Miller2518c7c2006-08-24 04:45:07 -07001074 hlist_del(&pol->bydst);
1075 hlist_del(&pol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -07001076 list_del(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001077 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001078
1079 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Herbert Xu4666faa2005-06-18 22:43:22 -07001082int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 write_lock_bh(&xfrm_policy_lock);
1085 pol = __xfrm_policy_unlink(pol, dir);
1086 write_unlock_bh(&xfrm_policy_lock);
1087 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001091 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
David S. Millera70fcb02006-03-20 19:18:52 -08001093EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1096{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001097 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 struct xfrm_policy *old_pol;
1099
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001100#ifdef CONFIG_XFRM_SUB_POLICY
1101 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1102 return -EINVAL;
1103#endif
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 write_lock_bh(&xfrm_policy_lock);
1106 old_pol = sk->sk_policy[dir];
1107 sk->sk_policy[dir] = pol;
1108 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001109 pol->curlft.add_time = get_seconds();
Alexey Dobriyan11219942008-11-25 17:33:06 -08001110 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1112 }
1113 if (old_pol)
Timo Teräsea2dea92010-03-31 00:17:05 +00001114 /* Unlinking succeeds always. This is the only function
1115 * allowed to delete or replace socket policy.
1116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1118 write_unlock_bh(&xfrm_policy_lock);
1119
1120 if (old_pol) {
1121 xfrm_policy_kill(old_pol);
1122 }
1123 return 0;
1124}
1125
1126static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1127{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001128 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 if (newp) {
1131 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001132 if (security_xfrm_policy_clone(old->security,
1133 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001134 kfree(newp);
1135 return NULL; /* ENOMEM */
1136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 newp->lft = old->lft;
1138 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001139 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 newp->action = old->action;
1141 newp->flags = old->flags;
1142 newp->xfrm_nr = old->xfrm_nr;
1143 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001144 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 memcpy(newp->xfrm_vec, old->xfrm_vec,
1146 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1147 write_lock_bh(&xfrm_policy_lock);
1148 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1149 write_unlock_bh(&xfrm_policy_lock);
1150 xfrm_pol_put(newp);
1151 }
1152 return newp;
1153}
1154
1155int __xfrm_sk_clone_policy(struct sock *sk)
1156{
1157 struct xfrm_policy *p0 = sk->sk_policy[0],
1158 *p1 = sk->sk_policy[1];
1159
1160 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1161 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1162 return -ENOMEM;
1163 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1164 return -ENOMEM;
1165 return 0;
1166}
1167
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001168static int
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001169xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001170 unsigned short family)
1171{
1172 int err;
1173 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1174
1175 if (unlikely(afinfo == NULL))
1176 return -EINVAL;
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001177 err = afinfo->get_saddr(net, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001178 xfrm_policy_put_afinfo(afinfo);
1179 return err;
1180}
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182/* Resolve list of templates for the flow, given policy. */
1183
1184static int
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001185xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1186 struct xfrm_state **xfrm,
1187 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001189 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 int nx;
1191 int i, error;
1192 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1193 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001194 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1197 struct xfrm_state *x;
1198 xfrm_address_t *remote = daddr;
1199 xfrm_address_t *local = saddr;
1200 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1201
Joakim Koskela48b8d782007-07-26 00:08:42 -07001202 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1203 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 remote = &tmpl->id.daddr;
1205 local = &tmpl->saddr;
Miika Komu76b3f052006-11-30 16:40:43 -08001206 family = tmpl->encap_family;
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001207 if (xfrm_addr_any(local, family)) {
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001208 error = xfrm_get_saddr(net, &tmp, remote, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001209 if (error)
1210 goto fail;
1211 local = &tmp;
1212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214
1215 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1216
1217 if (x && x->km.state == XFRM_STATE_VALID) {
1218 xfrm[nx++] = x;
1219 daddr = remote;
1220 saddr = local;
1221 continue;
1222 }
1223 if (x) {
1224 error = (x->km.state == XFRM_STATE_ERROR ?
1225 -EINVAL : -EAGAIN);
1226 xfrm_state_put(x);
1227 }
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001228 else if (error == -ESRCH)
1229 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 if (!tmpl->optional)
1232 goto fail;
1233 }
1234 return nx;
1235
1236fail:
1237 for (nx--; nx>=0; nx--)
1238 xfrm_state_put(xfrm[nx]);
1239 return error;
1240}
1241
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001242static int
1243xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1244 struct xfrm_state **xfrm,
1245 unsigned short family)
1246{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001247 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1248 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001249 int cnx = 0;
1250 int error;
1251 int ret;
1252 int i;
1253
1254 for (i = 0; i < npols; i++) {
1255 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1256 error = -ENOBUFS;
1257 goto fail;
1258 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001259
1260 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001261 if (ret < 0) {
1262 error = ret;
1263 goto fail;
1264 } else
1265 cnx += ret;
1266 }
1267
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001268 /* found states are sorted for outbound processing */
1269 if (npols > 1)
1270 xfrm_state_sort(xfrm, tpp, cnx, family);
1271
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001272 return cnx;
1273
1274 fail:
1275 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001276 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001277 return error;
1278
1279}
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281/* Check that the bundle accepts the flow and its components are
1282 * still valid.
1283 */
1284
Herbert Xu25ee3282007-12-11 09:32:34 -08001285static inline int xfrm_get_tos(struct flowi *fl, int family)
1286{
1287 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1288 int tos;
1289
1290 if (!afinfo)
1291 return -EINVAL;
1292
1293 tos = afinfo->get_tos(fl);
1294
1295 xfrm_policy_put_afinfo(afinfo);
1296
1297 return tos;
1298}
1299
Timo Teräs80c802f2010-04-07 00:30:05 +00001300static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1301{
1302 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1303 struct dst_entry *dst = &xdst->u.dst;
1304
1305 if (xdst->route == NULL) {
1306 /* Dummy bundle - if it has xfrms we were not
1307 * able to build bundle as template resolution failed.
1308 * It means we need to try again resolving. */
1309 if (xdst->num_xfrms > 0)
1310 return NULL;
1311 } else {
1312 /* Real bundle */
1313 if (stale_bundle(dst))
1314 return NULL;
1315 }
1316
1317 dst_hold(dst);
1318 return flo;
1319}
1320
1321static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1322{
1323 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1324 struct dst_entry *dst = &xdst->u.dst;
1325
1326 if (!xdst->route)
1327 return 0;
1328 if (stale_bundle(dst))
1329 return 0;
1330
1331 return 1;
1332}
1333
1334static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1335{
1336 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1337 struct dst_entry *dst = &xdst->u.dst;
1338
1339 dst_free(dst);
1340}
1341
1342static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1343 .get = xfrm_bundle_flo_get,
1344 .check = xfrm_bundle_flo_check,
1345 .delete = xfrm_bundle_flo_delete,
1346};
1347
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001348static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001349{
1350 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001351 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001352 struct xfrm_dst *xdst;
1353
1354 if (!afinfo)
1355 return ERR_PTR(-EINVAL);
1356
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001357 switch (family) {
1358 case AF_INET:
1359 dst_ops = &net->xfrm.xfrm4_dst_ops;
1360 break;
1361#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1362 case AF_INET6:
1363 dst_ops = &net->xfrm.xfrm6_dst_ops;
1364 break;
1365#endif
1366 default:
1367 BUG();
1368 }
1369 xdst = dst_alloc(dst_ops) ?: ERR_PTR(-ENOBUFS);
Herbert Xu25ee3282007-12-11 09:32:34 -08001370 xfrm_policy_put_afinfo(afinfo);
1371
Timo Teräs80c802f2010-04-07 00:30:05 +00001372 xdst->flo.ops = &xfrm_bundle_fc_ops;
1373
Herbert Xu25ee3282007-12-11 09:32:34 -08001374 return xdst;
1375}
1376
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001377static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1378 int nfheader_len)
1379{
1380 struct xfrm_policy_afinfo *afinfo =
1381 xfrm_policy_get_afinfo(dst->ops->family);
1382 int err;
1383
1384 if (!afinfo)
1385 return -EINVAL;
1386
1387 err = afinfo->init_path(path, dst, nfheader_len);
1388
1389 xfrm_policy_put_afinfo(afinfo);
1390
1391 return err;
1392}
1393
Herbert Xu87c1e122010-03-02 02:51:56 +00001394static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
1395 struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001396{
1397 struct xfrm_policy_afinfo *afinfo =
1398 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1399 int err;
1400
1401 if (!afinfo)
1402 return -EINVAL;
1403
Herbert Xu87c1e122010-03-02 02:51:56 +00001404 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001405
1406 xfrm_policy_put_afinfo(afinfo);
1407
1408 return err;
1409}
1410
Timo Teräs80c802f2010-04-07 00:30:05 +00001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1413 * all the metrics... Shortly, bundle a bundle.
1414 */
1415
Herbert Xu25ee3282007-12-11 09:32:34 -08001416static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1417 struct xfrm_state **xfrm, int nx,
1418 struct flowi *fl,
1419 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001421 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001422 unsigned long now = jiffies;
1423 struct net_device *dev;
1424 struct dst_entry *dst_prev = NULL;
1425 struct dst_entry *dst0 = NULL;
1426 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001428 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001429 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001430 int trailer_len = 0;
1431 int tos;
1432 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001433 xfrm_address_t saddr, daddr;
1434
1435 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001436
1437 tos = xfrm_get_tos(fl, family);
1438 err = tos;
1439 if (tos < 0)
1440 goto put_states;
1441
1442 dst_hold(dst);
1443
1444 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001445 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001446 struct dst_entry *dst1 = &xdst->u.dst;
1447
1448 err = PTR_ERR(xdst);
1449 if (IS_ERR(xdst)) {
1450 dst_release(dst);
1451 goto put_states;
1452 }
1453
1454 if (!dst_prev)
1455 dst0 = dst1;
1456 else {
1457 dst_prev->child = dst_clone(dst1);
1458 dst1->flags |= DST_NOHASH;
1459 }
1460
1461 xdst->route = dst;
1462 memcpy(&dst1->metrics, &dst->metrics, sizeof(dst->metrics));
1463
1464 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1465 family = xfrm[i]->props.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001466 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1467 family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001468 err = PTR_ERR(dst);
1469 if (IS_ERR(dst))
1470 goto put_states;
1471 } else
1472 dst_hold(dst);
1473
1474 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001475 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001476
1477 dst1->obsolete = -1;
1478 dst1->flags |= DST_HOST;
1479 dst1->lastuse = now;
1480
1481 dst1->input = dst_discard;
1482 dst1->output = xfrm[i]->outer_mode->afinfo->output;
1483
1484 dst1->next = dst_prev;
1485 dst_prev = dst1;
1486
1487 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001488 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1489 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001490 trailer_len += xfrm[i]->props.trailer_len;
1491 }
1492
1493 dst_prev->child = dst;
1494 dst0->path = dst;
1495
1496 err = -ENODEV;
1497 dev = dst->dev;
1498 if (!dev)
1499 goto free_dst;
1500
Ralf Baechle96c53402009-12-25 23:30:02 +00001501 /* Copy neighbour for reachability confirmation */
Herbert Xu25ee3282007-12-11 09:32:34 -08001502 dst0->neighbour = neigh_clone(dst->neighbour);
1503
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001504 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001505 xfrm_init_pmtu(dst_prev);
1506
1507 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1508 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1509
Herbert Xu87c1e122010-03-02 02:51:56 +00001510 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001511 if (err)
1512 goto free_dst;
1513
1514 dst_prev->header_len = header_len;
1515 dst_prev->trailer_len = trailer_len;
1516 header_len -= xdst->u.dst.xfrm->props.header_len;
1517 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1518 }
1519
1520out:
1521 return dst0;
1522
1523put_states:
1524 for (; i < nx; i++)
1525 xfrm_state_put(xfrm[i]);
1526free_dst:
1527 if (dst0)
1528 dst_free(dst0);
1529 dst0 = ERR_PTR(err);
1530 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001533static int inline
1534xfrm_dst_alloc_copy(void **target, void *src, int size)
1535{
1536 if (!*target) {
1537 *target = kmalloc(size, GFP_ATOMIC);
1538 if (!*target)
1539 return -ENOMEM;
1540 }
1541 memcpy(*target, src, size);
1542 return 0;
1543}
1544
1545static int inline
1546xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
1547{
1548#ifdef CONFIG_XFRM_SUB_POLICY
1549 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1550 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1551 sel, sizeof(*sel));
1552#else
1553 return 0;
1554#endif
1555}
1556
1557static int inline
1558xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
1559{
1560#ifdef CONFIG_XFRM_SUB_POLICY
1561 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1562 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1563#else
1564 return 0;
1565#endif
1566}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Timo Teräs80c802f2010-04-07 00:30:05 +00001568static int xfrm_expand_policies(struct flowi *fl, u16 family,
1569 struct xfrm_policy **pols,
1570 int *num_pols, int *num_xfrms)
1571{
1572 int i;
1573
1574 if (*num_pols == 0 || !pols[0]) {
1575 *num_pols = 0;
1576 *num_xfrms = 0;
1577 return 0;
1578 }
1579 if (IS_ERR(pols[0]))
1580 return PTR_ERR(pols[0]);
1581
1582 *num_xfrms = pols[0]->xfrm_nr;
1583
1584#ifdef CONFIG_XFRM_SUB_POLICY
1585 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1586 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1587 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1588 XFRM_POLICY_TYPE_MAIN,
1589 fl, family,
1590 XFRM_POLICY_OUT);
1591 if (pols[1]) {
1592 if (IS_ERR(pols[1])) {
1593 xfrm_pols_put(pols, *num_pols);
1594 return PTR_ERR(pols[1]);
1595 }
1596 (*num_pols) ++;
1597 (*num_xfrms) += pols[1]->xfrm_nr;
1598 }
1599 }
1600#endif
1601 for (i = 0; i < *num_pols; i++) {
1602 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1603 *num_xfrms = -1;
1604 break;
1605 }
1606 }
1607
1608 return 0;
1609
1610}
1611
1612static struct xfrm_dst *
1613xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
1614 struct flowi *fl, u16 family,
1615 struct dst_entry *dst_orig)
1616{
1617 struct net *net = xp_net(pols[0]);
1618 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1619 struct dst_entry *dst;
1620 struct xfrm_dst *xdst;
1621 int err;
1622
1623 /* Try to instantiate a bundle */
1624 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
1625 if (err < 0) {
1626 if (err != -EAGAIN)
1627 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1628 return ERR_PTR(err);
1629 }
1630
1631 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1632 if (IS_ERR(dst)) {
1633 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1634 return ERR_CAST(dst);
1635 }
1636
1637 xdst = (struct xfrm_dst *)dst;
1638 xdst->num_xfrms = err;
1639 if (num_pols > 1)
1640 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1641 else
1642 err = xfrm_dst_update_origin(dst, fl);
1643 if (unlikely(err)) {
1644 dst_free(dst);
1645 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1646 return ERR_PTR(err);
1647 }
1648
1649 xdst->num_pols = num_pols;
1650 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
1651 xdst->policy_genid = atomic_read(&pols[0]->genid);
1652
1653 return xdst;
1654}
1655
1656static struct flow_cache_object *
1657xfrm_bundle_lookup(struct net *net, struct flowi *fl, u16 family, u8 dir,
1658 struct flow_cache_object *oldflo, void *ctx)
1659{
1660 struct dst_entry *dst_orig = (struct dst_entry *)ctx;
1661 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1662 struct xfrm_dst *xdst, *new_xdst;
1663 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
1664
1665 /* Check if the policies from old bundle are usable */
1666 xdst = NULL;
1667 if (oldflo) {
1668 xdst = container_of(oldflo, struct xfrm_dst, flo);
1669 num_pols = xdst->num_pols;
1670 num_xfrms = xdst->num_xfrms;
1671 pol_dead = 0;
1672 for (i = 0; i < num_pols; i++) {
1673 pols[i] = xdst->pols[i];
1674 pol_dead |= pols[i]->walk.dead;
1675 }
1676 if (pol_dead) {
1677 dst_free(&xdst->u.dst);
1678 xdst = NULL;
1679 num_pols = 0;
1680 num_xfrms = 0;
1681 oldflo = NULL;
1682 }
1683 }
1684
1685 /* Resolve policies to use if we couldn't get them from
1686 * previous cache entry */
1687 if (xdst == NULL) {
1688 num_pols = 1;
1689 pols[0] = __xfrm_policy_lookup(net, fl, family, dir);
1690 err = xfrm_expand_policies(fl, family, pols,
1691 &num_pols, &num_xfrms);
1692 if (err < 0)
1693 goto inc_error;
1694 if (num_pols == 0)
1695 return NULL;
1696 if (num_xfrms <= 0)
1697 goto make_dummy_bundle;
1698 }
1699
1700 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family, dst_orig);
1701 if (IS_ERR(new_xdst)) {
1702 err = PTR_ERR(new_xdst);
1703 if (err != -EAGAIN)
1704 goto error;
1705 if (oldflo == NULL)
1706 goto make_dummy_bundle;
1707 dst_hold(&xdst->u.dst);
1708 return oldflo;
1709 }
1710
1711 /* Kill the previous bundle */
1712 if (xdst) {
1713 /* The policies were stolen for newly generated bundle */
1714 xdst->num_pols = 0;
1715 dst_free(&xdst->u.dst);
1716 }
1717
1718 /* Flow cache does not have reference, it dst_free()'s,
1719 * but we do need to return one reference for original caller */
1720 dst_hold(&new_xdst->u.dst);
1721 return &new_xdst->flo;
1722
1723make_dummy_bundle:
1724 /* We found policies, but there's no bundles to instantiate:
1725 * either because the policy blocks, has no transformations or
1726 * we could not build template (no xfrm_states).*/
1727 xdst = xfrm_alloc_dst(net, family);
1728 if (IS_ERR(xdst)) {
1729 xfrm_pols_put(pols, num_pols);
1730 return ERR_CAST(xdst);
1731 }
1732 xdst->num_pols = num_pols;
1733 xdst->num_xfrms = num_xfrms;
1734 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
1735
1736 dst_hold(&xdst->u.dst);
1737 return &xdst->flo;
1738
1739inc_error:
1740 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1741error:
1742 if (xdst != NULL)
1743 dst_free(&xdst->u.dst);
1744 else
1745 xfrm_pols_put(pols, num_pols);
1746 return ERR_PTR(err);
1747}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749/* Main function: finds/creates a bundle for given flow.
1750 *
1751 * At the moment we eat a raw IP route. Mostly to speed up lookups
1752 * on interfaces with disabled IPsec.
1753 */
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001754int __xfrm_lookup(struct net *net, struct dst_entry **dst_p, struct flowi *fl,
David S. Miller14e50e52007-05-24 18:17:54 -07001755 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001757 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00001758 struct flow_cache_object *flo;
1759 struct xfrm_dst *xdst;
1760 struct dst_entry *dst, *dst_orig = *dst_p, *route;
1761 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08001762 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Timo Teräs80c802f2010-04-07 00:30:05 +00001763 int i, err, num_pols, num_xfrms, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765restart:
Timo Teräs80c802f2010-04-07 00:30:05 +00001766 dst = NULL;
1767 xdst = NULL;
1768 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001769
Thomas Graff7944fb2007-08-25 13:46:55 -07001770 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00001771 num_pols = 1;
1772 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
1773 err = xfrm_expand_policies(fl, family, pols,
1774 &num_pols, &num_xfrms);
1775 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08001776 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00001777
1778 if (num_pols) {
1779 if (num_xfrms <= 0) {
1780 drop_pols = num_pols;
1781 goto no_transform;
1782 }
1783
1784 xdst = xfrm_resolve_and_create_bundle(
1785 pols, num_pols, fl,
1786 family, dst_orig);
1787 if (IS_ERR(xdst)) {
1788 xfrm_pols_put(pols, num_pols);
1789 err = PTR_ERR(xdst);
1790 goto dropdst;
1791 }
1792
1793 spin_lock_bh(&xfrm_policy_sk_bundle_lock);
1794 xdst->u.dst.next = xfrm_policy_sk_bundles;
1795 xfrm_policy_sk_bundles = &xdst->u.dst;
1796 spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
1797
1798 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001799 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Timo Teräs80c802f2010-04-07 00:30:05 +00001802 if (xdst == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07001804 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001805 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08001806 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Timo Teräs80c802f2010-04-07 00:30:05 +00001808 flo = flow_cache_lookup(net, fl, family, dir,
1809 xfrm_bundle_lookup, dst_orig);
1810 if (flo == NULL)
1811 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001812 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00001813 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08001814 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08001815 }
Timo Teräs80c802f2010-04-07 00:30:05 +00001816 xdst = container_of(flo, struct xfrm_dst, flo);
1817
1818 num_pols = xdst->num_pols;
1819 num_xfrms = xdst->num_xfrms;
1820 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy*) * num_pols);
1821 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
1823
Timo Teräs80c802f2010-04-07 00:30:05 +00001824 dst = &xdst->u.dst;
1825 if (route == NULL && num_xfrms > 0) {
1826 /* The only case when xfrm_bundle_lookup() returns a
1827 * bundle with null route, is when the template could
1828 * not be resolved. It means policies are there, but
1829 * bundle could not be created, since we don't yet
1830 * have the xfrm_state's. We need to wait for KM to
1831 * negotiate new SA's or bail out with error.*/
1832 if (net->xfrm.sysctl_larval_drop) {
1833 /* EREMOTE tells the caller to generate
1834 * a one-shot blackhole route. */
1835 dst_release(dst);
1836 xfrm_pols_put(pols, num_pols);
1837 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
1838 return -EREMOTE;
1839 }
1840 if (flags & XFRM_LOOKUP_WAIT) {
1841 DECLARE_WAITQUEUE(wait, current);
1842
1843 add_wait_queue(&net->xfrm.km_waitq, &wait);
1844 set_current_state(TASK_INTERRUPTIBLE);
1845 schedule();
1846 set_current_state(TASK_RUNNING);
1847 remove_wait_queue(&net->xfrm.km_waitq, &wait);
1848
1849 if (!signal_pending(current)) {
1850 dst_release(dst);
1851 goto restart;
1852 }
1853
1854 err = -ERESTART;
1855 } else
1856 err = -EAGAIN;
1857
1858 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
1859 goto error;
1860 }
1861
1862no_transform:
1863 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08001864 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Timo Teräs80c802f2010-04-07 00:30:05 +00001866 if ((flags & XFRM_LOOKUP_ICMP) &&
1867 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
1868 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001869 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00001870 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08001871
Timo Teräs80c802f2010-04-07 00:30:05 +00001872 for (i = 0; i < num_pols; i++)
1873 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08001874
Timo Teräs80c802f2010-04-07 00:30:05 +00001875 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001877 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye1044112005-09-08 15:11:55 -07001878 err = -EPERM;
1879 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00001880 } else if (num_xfrms > 0) {
1881 /* Flow transformed */
1882 *dst_p = dst;
1883 dst_release(dst_orig);
1884 } else {
1885 /* Flow passes untransformed */
1886 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
Timo Teräs80c802f2010-04-07 00:30:05 +00001888ok:
1889 xfrm_pols_put(pols, drop_pols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 return 0;
1891
Timo Teräs80c802f2010-04-07 00:30:05 +00001892nopol:
1893 if (!(flags & XFRM_LOOKUP_ICMP))
1894 goto ok;
1895 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896error:
Timo Teräs80c802f2010-04-07 00:30:05 +00001897 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08001898dropdst:
1899 dst_release(dst_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 *dst_p = NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001901 xfrm_pols_put(pols, drop_pols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return err;
1903}
David S. Miller14e50e52007-05-24 18:17:54 -07001904EXPORT_SYMBOL(__xfrm_lookup);
1905
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001906int xfrm_lookup(struct net *net, struct dst_entry **dst_p, struct flowi *fl,
David S. Miller14e50e52007-05-24 18:17:54 -07001907 struct sock *sk, int flags)
1908{
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001909 int err = __xfrm_lookup(net, dst_p, fl, sk, flags);
David S. Miller14e50e52007-05-24 18:17:54 -07001910
1911 if (err == -EREMOTE) {
1912 dst_release(*dst_p);
1913 *dst_p = NULL;
1914 err = -EAGAIN;
1915 }
1916
1917 return err;
1918}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919EXPORT_SYMBOL(xfrm_lookup);
1920
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001921static inline int
1922xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1923{
1924 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001925
1926 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1927 return 0;
1928 x = skb->sp->xvec[idx];
1929 if (!x->type->reject)
1930 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07001931 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001932}
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934/* When skb is transformed back to its "native" form, we have to
1935 * check policy restrictions. At the moment we make this in maximally
1936 * stupid way. Shame on me. :-) Of course, connected sockets must
1937 * have policy cached at them.
1938 */
1939
1940static inline int
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001941xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 unsigned short family)
1943{
1944 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08001945 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return x->id.proto == tmpl->id.proto &&
1947 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1948 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1949 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07001950 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07001951 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001952 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1953 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001956/*
1957 * 0 or more than 0 is returned when validation is succeeded (either bypass
1958 * because of optional transport mode, or next index of the mathced secpath
1959 * state with the template.
1960 * -1 is returned when no matching template is found.
1961 * Otherwise "-2 - errored_index" is returned.
1962 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963static inline int
1964xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1965 unsigned short family)
1966{
1967 int idx = start;
1968
1969 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001970 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return start;
1972 } else
1973 start = -1;
1974 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001975 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001977 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1978 if (start == -1)
1979 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
1983 return start;
1984}
1985
Herbert Xud5422ef2007-12-12 10:44:16 -08001986int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
1987 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001990 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992 if (unlikely(afinfo == NULL))
1993 return -EAFNOSUPPORT;
1994
Herbert Xud5422ef2007-12-12 10:44:16 -08001995 afinfo->decode_session(skb, fl, reverse);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07001996 err = security_xfrm_decode_session(skb, &fl->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001998 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
Herbert Xud5422ef2007-12-12 10:44:16 -08002000EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002002static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
2004 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002005 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002006 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
2010
2011 return 0;
2012}
2013
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002014int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 unsigned short family)
2016{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002017 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002019 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2020 int npols = 0;
2021 int xfrm_nr;
2022 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002023 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002025 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002026 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Herbert Xud5422ef2007-12-12 10:44:16 -08002028 reverse = dir & ~XFRM_POLICY_MASK;
2029 dir &= XFRM_POLICY_MASK;
2030 fl_dir = policy_to_flow_dir(dir);
2031
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002032 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002033 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002035 }
2036
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002037 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 /* First, check used SA against their selectors. */
2040 if (skb->sp) {
2041 int i;
2042
2043 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002044 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002045 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002046 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
2050 }
2051
2052 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002053 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002054 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002055 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002056 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002057 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002058 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002061 if (!pol) {
2062 struct flow_cache_object *flo;
2063
2064 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2065 xfrm_policy_lookup, NULL);
2066 if (IS_ERR_OR_NULL(flo))
2067 pol = ERR_CAST(flo);
2068 else
2069 pol = container_of(flo, struct xfrm_policy, flo);
2070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002072 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002073 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002074 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002075 }
James Morris134b0fc2006-10-05 15:42:27 -05002076
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002077 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002078 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002079 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002080 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002081 return 0;
2082 }
2083 return 1;
2084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
James Morris9d729f72007-03-04 16:12:44 -08002086 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002088 pols[0] = pol;
2089 npols ++;
2090#ifdef CONFIG_XFRM_SUB_POLICY
2091 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002092 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002093 &fl, family,
2094 XFRM_POLICY_IN);
2095 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002096 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002097 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002098 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002099 }
James Morris9d729f72007-03-04 16:12:44 -08002100 pols[1]->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002101 npols ++;
2102 }
2103 }
2104#endif
2105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (pol->action == XFRM_POLICY_ALLOW) {
2107 struct sec_path *sp;
2108 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002109 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002110 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002111 struct xfrm_tmpl **tpp = tp;
2112 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 int i, k;
2114
2115 if ((sp = skb->sp) == NULL)
2116 sp = &dummy;
2117
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002118 for (pi = 0; pi < npols; pi++) {
2119 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002120 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002121 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002122 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002123 }
2124 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002125 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002126 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002127 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002128 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2129 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2130 }
2131 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002132 if (npols > 1) {
2133 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
2134 tpp = stp;
2135 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 /* For each tunnel xfrm, find the first matching tmpl.
2138 * For each tmpl before that, find corresponding xfrm.
2139 * Order is _important_. Later we will implement
2140 * some barriers, but at the moment barriers
2141 * are implied between each two transformations.
2142 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002143 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2144 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002145 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002146 if (k < -1)
2147 /* "-2 - errored_index" returned */
2148 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002149 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 }
2153
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002154 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002155 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002159 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 return 1;
2161 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002162 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002165 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002166reject_error:
2167 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return 0;
2169}
2170EXPORT_SYMBOL(__xfrm_policy_check);
2171
2172int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2173{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002174 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002176 struct dst_entry *dst;
2177 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002179 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002180 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Eric Dumazetadf30902009-06-02 05:19:30 +00002184 dst = skb_dst(skb);
2185
2186 res = xfrm_lookup(net, &dst, &fl, NULL, 0) == 0;
2187 skb_dst_set(skb, dst);
2188 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
2190EXPORT_SYMBOL(__xfrm_route_forward);
2191
David S. Millerd49c73c2006-08-13 18:55:53 -07002192/* Optimize later using cookies and generation ids. */
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2195{
David S. Millerd49c73c2006-08-13 18:55:53 -07002196 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2197 * to "-1" to force all XFRM destinations to get validated by
2198 * dst_ops->check on every use. We do this because when a
2199 * normal route referenced by an XFRM dst is obsoleted we do
2200 * not go looking around for all parent referencing XFRM dsts
2201 * so that we can invalidate them. It is just too much work.
2202 * Instead we make the checks here on every use. For example:
2203 *
2204 * XFRM dst A --> IPv4 dst X
2205 *
2206 * X is the "xdst->route" of A (X is also the "dst->path" of A
2207 * in this example). If X is marked obsolete, "A" will not
2208 * notice. That's what we are validating here via the
2209 * stale_bundle() check.
2210 *
2211 * When a policy's bundle is pruned, we dst_free() the XFRM
2212 * dst which causes it's ->obsolete field to be set to a
2213 * positive non-zero integer. If an XFRM dst has been pruned
2214 * like this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002215 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002216 if (dst->obsolete < 0 && !stale_bundle(dst))
2217 return dst;
2218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return NULL;
2220}
2221
2222static int stale_bundle(struct dst_entry *dst)
2223{
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05002224 return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
Herbert Xuaabc9762005-05-03 16:27:10 -07002227void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002230 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002231 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 dev_put(dev);
2233 }
2234}
Herbert Xuaabc9762005-05-03 16:27:10 -07002235EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237static void xfrm_link_failure(struct sk_buff *skb)
2238{
2239 /* Impossible. Such dst must be popped before reaches point of failure. */
2240 return;
2241}
2242
2243static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2244{
2245 if (dst) {
2246 if (dst->obsolete) {
2247 dst_release(dst);
2248 dst = NULL;
2249 }
2250 }
2251 return dst;
2252}
2253
Alexey Dobriyanddcfd792008-11-25 17:37:23 -08002254static void __xfrm_garbage_collect(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Timo Teräs80c802f2010-04-07 00:30:05 +00002256 struct dst_entry *head, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Timo Teräs80c802f2010-04-07 00:30:05 +00002258 flow_cache_flush();
2259
2260 spin_lock_bh(&xfrm_policy_sk_bundle_lock);
2261 head = xfrm_policy_sk_bundles;
2262 xfrm_policy_sk_bundles = NULL;
2263 spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
2264
2265 while (head) {
2266 next = head->next;
2267 dst_free(head);
2268 head = next;
2269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270}
2271
Herbert Xu25ee3282007-12-11 09:32:34 -08002272static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
2274 do {
2275 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2276 u32 pmtu, route_mtu_cached;
2277
2278 pmtu = dst_mtu(dst->child);
2279 xdst->child_mtu_cached = pmtu;
2280
2281 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2282
2283 route_mtu_cached = dst_mtu(xdst->route);
2284 xdst->route_mtu_cached = route_mtu_cached;
2285
2286 if (pmtu > route_mtu_cached)
2287 pmtu = route_mtu_cached;
2288
2289 dst->metrics[RTAX_MTU-1] = pmtu;
2290 } while ((dst = dst->next));
2291}
2292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293/* Check that the bundle accepts the flow and its components are
2294 * still valid.
2295 */
2296
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05002297int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
2298 struct flowi *fl, int family, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
2300 struct dst_entry *dst = &first->u.dst;
2301 struct xfrm_dst *last;
2302 u32 mtu;
2303
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002304 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 (dst->dev && !netif_running(dst->dev)))
2306 return 0;
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07002307#ifdef CONFIG_XFRM_SUB_POLICY
2308 if (fl) {
2309 if (first->origin && !flow_cache_uli_match(first->origin, fl))
2310 return 0;
2311 if (first->partner &&
2312 !xfrm_selector_match(first->partner, fl, family))
2313 return 0;
2314 }
2315#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 last = NULL;
2318
2319 do {
2320 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2321
2322 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
2323 return 0;
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06002324 if (fl && pol &&
2325 !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002326 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2328 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002329 if (xdst->xfrm_genid != dst->xfrm->genid)
2330 return 0;
2331 if (xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Herbert Xu1bfcb102007-10-17 21:31:50 -07002334 if (strict && fl &&
Herbert Xu13996372007-10-17 21:35:51 -07002335 !(dst->xfrm->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07002336 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
2337 return 0;
2338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 mtu = dst_mtu(dst->child);
2340 if (xdst->child_mtu_cached != mtu) {
2341 last = xdst;
2342 xdst->child_mtu_cached = mtu;
2343 }
2344
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002345 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return 0;
2347 mtu = dst_mtu(xdst->route);
2348 if (xdst->route_mtu_cached != mtu) {
2349 last = xdst;
2350 xdst->route_mtu_cached = mtu;
2351 }
2352
2353 dst = dst->child;
2354 } while (dst->xfrm);
2355
2356 if (likely(!last))
2357 return 1;
2358
2359 mtu = last->child_mtu_cached;
2360 for (;;) {
2361 dst = &last->u.dst;
2362
2363 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2364 if (mtu > last->route_mtu_cached)
2365 mtu = last->route_mtu_cached;
2366 dst->metrics[RTAX_MTU-1] = mtu;
2367
2368 if (last == first)
2369 break;
2370
Patrick McHardybd0bf072007-07-18 01:55:52 -07002371 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 last->child_mtu_cached = mtu;
2373 }
2374
2375 return 1;
2376}
2377
2378EXPORT_SYMBOL(xfrm_bundle_ok);
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2381{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002382 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 int err = 0;
2384 if (unlikely(afinfo == NULL))
2385 return -EINVAL;
2386 if (unlikely(afinfo->family >= NPROTO))
2387 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002388 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2390 err = -ENOBUFS;
2391 else {
2392 struct dst_ops *dst_ops = afinfo->dst_ops;
2393 if (likely(dst_ops->kmem_cachep == NULL))
2394 dst_ops->kmem_cachep = xfrm_dst_cache;
2395 if (likely(dst_ops->check == NULL))
2396 dst_ops->check = xfrm_dst_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 if (likely(dst_ops->negative_advice == NULL))
2398 dst_ops->negative_advice = xfrm_negative_advice;
2399 if (likely(dst_ops->link_failure == NULL))
2400 dst_ops->link_failure = xfrm_link_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 if (likely(afinfo->garbage_collect == NULL))
2402 afinfo->garbage_collect = __xfrm_garbage_collect;
2403 xfrm_policy_afinfo[afinfo->family] = afinfo;
2404 }
Ingo Molnare959d812006-04-28 15:32:29 -07002405 write_unlock_bh(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002406
2407 rtnl_lock();
2408 for_each_net(net) {
2409 struct dst_ops *xfrm_dst_ops;
2410
2411 switch (afinfo->family) {
2412 case AF_INET:
2413 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2414 break;
2415#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2416 case AF_INET6:
2417 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2418 break;
2419#endif
2420 default:
2421 BUG();
2422 }
2423 *xfrm_dst_ops = *afinfo->dst_ops;
2424 }
2425 rtnl_unlock();
2426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return err;
2428}
2429EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2430
2431int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2432{
2433 int err = 0;
2434 if (unlikely(afinfo == NULL))
2435 return -EINVAL;
2436 if (unlikely(afinfo->family >= NPROTO))
2437 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002438 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2440 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2441 err = -EINVAL;
2442 else {
2443 struct dst_ops *dst_ops = afinfo->dst_ops;
2444 xfrm_policy_afinfo[afinfo->family] = NULL;
2445 dst_ops->kmem_cachep = NULL;
2446 dst_ops->check = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 dst_ops->negative_advice = NULL;
2448 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 afinfo->garbage_collect = NULL;
2450 }
2451 }
Ingo Molnare959d812006-04-28 15:32:29 -07002452 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 return err;
2454}
2455EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2456
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002457static void __net_init xfrm_dst_ops_init(struct net *net)
2458{
2459 struct xfrm_policy_afinfo *afinfo;
2460
2461 read_lock_bh(&xfrm_policy_afinfo_lock);
2462 afinfo = xfrm_policy_afinfo[AF_INET];
2463 if (afinfo)
2464 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
2465#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2466 afinfo = xfrm_policy_afinfo[AF_INET6];
2467 if (afinfo)
2468 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2469#endif
2470 read_unlock_bh(&xfrm_policy_afinfo_lock);
2471}
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
2474{
2475 struct xfrm_policy_afinfo *afinfo;
2476 if (unlikely(family >= NPROTO))
2477 return NULL;
2478 read_lock(&xfrm_policy_afinfo_lock);
2479 afinfo = xfrm_policy_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07002480 if (unlikely(!afinfo))
2481 read_unlock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 return afinfo;
2483}
2484
2485static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2486{
Herbert Xu546be242006-05-27 23:03:58 -07002487 read_unlock(&xfrm_policy_afinfo_lock);
2488}
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2491{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002492 struct net_device *dev = ptr;
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 switch (event) {
2495 case NETDEV_DOWN:
Timo Teräs80c802f2010-04-07 00:30:05 +00002496 __xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 }
2498 return NOTIFY_DONE;
2499}
2500
2501static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002502 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503};
2504
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002505#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002506static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002507{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002508 int rv;
2509
Tejun Heo7d720c32010-02-16 15:20:26 +00002510 if (snmp_mib_init((void __percpu **)net->mib.xfrm_statistics,
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002511 sizeof(struct linux_xfrm_mib)) < 0)
2512 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002513 rv = xfrm_proc_init(net);
2514 if (rv < 0)
Tejun Heo7d720c32010-02-16 15:20:26 +00002515 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002516 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002517}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002518
2519static void xfrm_statistics_fini(struct net *net)
2520{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002521 xfrm_proc_fini(net);
Tejun Heo7d720c32010-02-16 15:20:26 +00002522 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002523}
2524#else
2525static int __net_init xfrm_statistics_init(struct net *net)
2526{
2527 return 0;
2528}
2529
2530static void xfrm_statistics_fini(struct net *net)
2531{
2532}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002533#endif
2534
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002535static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536{
David S. Miller2518c7c2006-08-24 04:45:07 -07002537 unsigned int hmask, sz;
2538 int dir;
2539
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002540 if (net_eq(net, &init_net))
2541 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07002543 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002544 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
David S. Miller2518c7c2006-08-24 04:45:07 -07002546 hmask = 8 - 1;
2547 sz = (hmask+1) * sizeof(struct hlist_head);
2548
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002549 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2550 if (!net->xfrm.policy_byidx)
2551 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002552 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002553
2554 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2555 struct xfrm_policy_hash *htab;
2556
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002557 net->xfrm.policy_count[dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002558 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002559
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002560 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002561 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002562 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002563 goto out_bydst;
2564 htab->hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002565 }
2566
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002567 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002568 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002569 if (net_eq(net, &init_net))
2570 register_netdevice_notifier(&xfrm_dev_notifier);
2571 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002572
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002573out_bydst:
2574 for (dir--; dir >= 0; dir--) {
2575 struct xfrm_policy_hash *htab;
2576
2577 htab = &net->xfrm.policy_bydst[dir];
2578 xfrm_hash_free(htab->table, sz);
2579 }
2580 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002581out_byidx:
2582 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002585static void xfrm_policy_fini(struct net *net)
2586{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002587 struct xfrm_audit audit_info;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002588 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002589 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002590
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002591 flush_work(&net->xfrm.policy_hash_work);
2592#ifdef CONFIG_XFRM_SUB_POLICY
2593 audit_info.loginuid = -1;
2594 audit_info.sessionid = -1;
2595 audit_info.secid = 0;
2596 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, &audit_info);
2597#endif
2598 audit_info.loginuid = -1;
2599 audit_info.sessionid = -1;
2600 audit_info.secid = 0;
2601 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
2602 flush_work(&xfrm_policy_gc_work);
2603
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002604 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002605
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002606 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002607 struct xfrm_policy_hash *htab;
2608
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002609 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002610
2611 htab = &net->xfrm.policy_bydst[dir];
2612 sz = (htab->hmask + 1);
2613 WARN_ON(!hlist_empty(htab->table));
2614 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002615 }
2616
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002617 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002618 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2619 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002620}
2621
2622static int __net_init xfrm_net_init(struct net *net)
2623{
2624 int rv;
2625
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002626 rv = xfrm_statistics_init(net);
2627 if (rv < 0)
2628 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002629 rv = xfrm_state_init(net);
2630 if (rv < 0)
2631 goto out_state;
2632 rv = xfrm_policy_init(net);
2633 if (rv < 0)
2634 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002635 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002636 rv = xfrm_sysctl_init(net);
2637 if (rv < 0)
2638 goto out_sysctl;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002639 return 0;
2640
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002641out_sysctl:
2642 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002643out_policy:
2644 xfrm_state_fini(net);
2645out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002646 xfrm_statistics_fini(net);
2647out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002648 return rv;
2649}
2650
2651static void __net_exit xfrm_net_exit(struct net *net)
2652{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002653 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002654 xfrm_policy_fini(net);
2655 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002656 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002657}
2658
2659static struct pernet_operations __net_initdata xfrm_net_ops = {
2660 .init = xfrm_net_init,
2661 .exit = xfrm_net_exit,
2662};
2663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664void __init xfrm_init(void)
2665{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002666 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 xfrm_input_init();
2668}
2669
Joy Lattenab5f5e82007-09-17 11:51:22 -07002670#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd2008-01-12 03:20:03 -08002671static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2672 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002673{
Paul Moore875179f2007-12-01 23:27:18 +11002674 struct xfrm_sec_ctx *ctx = xp->security;
2675 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002676
Paul Moore875179f2007-12-01 23:27:18 +11002677 if (ctx)
2678 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2679 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2680
2681 switch(sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002682 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002683 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002684 if (sel->prefixlen_s != 32)
2685 audit_log_format(audit_buf, " src_prefixlen=%d",
2686 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002687 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002688 if (sel->prefixlen_d != 32)
2689 audit_log_format(audit_buf, " dst_prefixlen=%d",
2690 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002691 break;
2692 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002693 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002694 if (sel->prefixlen_s != 128)
2695 audit_log_format(audit_buf, " src_prefixlen=%d",
2696 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002697 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002698 if (sel->prefixlen_d != 128)
2699 audit_log_format(audit_buf, " dst_prefixlen=%d",
2700 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002701 break;
2702 }
2703}
2704
Paul Moore68277ac2007-12-20 20:49:33 -08002705void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
Eric Paris25323862008-04-18 10:09:25 -04002706 uid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002707{
2708 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002709
Paul Mooreafeb14b2007-12-21 14:58:11 -08002710 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002711 if (audit_buf == NULL)
2712 return;
Eric Paris25323862008-04-18 10:09:25 -04002713 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002714 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002715 xfrm_audit_common_policyinfo(xp, audit_buf);
2716 audit_log_end(audit_buf);
2717}
2718EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
2719
Paul Moore68277ac2007-12-20 20:49:33 -08002720void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Eric Paris25323862008-04-18 10:09:25 -04002721 uid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002722{
2723 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002724
Paul Mooreafeb14b2007-12-21 14:58:11 -08002725 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002726 if (audit_buf == NULL)
2727 return;
Eric Paris25323862008-04-18 10:09:25 -04002728 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002729 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002730 xfrm_audit_common_policyinfo(xp, audit_buf);
2731 audit_log_end(audit_buf);
2732}
2733EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
2734#endif
2735
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002736#ifdef CONFIG_XFRM_MIGRATE
2737static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
2738 struct xfrm_selector *sel_tgt)
2739{
2740 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
2741 if (sel_tgt->family == sel_cmp->family &&
2742 xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002743 sel_cmp->family) == 0 &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002744 xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
2745 sel_cmp->family) == 0 &&
2746 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
2747 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
2748 return 1;
2749 }
2750 } else {
2751 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
2752 return 1;
2753 }
2754 }
2755 return 0;
2756}
2757
2758static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
2759 u8 dir, u8 type)
2760{
2761 struct xfrm_policy *pol, *ret = NULL;
2762 struct hlist_node *entry;
2763 struct hlist_head *chain;
2764 u32 priority = ~0U;
2765
2766 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -08002767 chain = policy_hash_direct(&init_net, &sel->daddr, &sel->saddr, sel->family, dir);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002768 hlist_for_each_entry(pol, entry, chain, bydst) {
2769 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2770 pol->type == type) {
2771 ret = pol;
2772 priority = ret->priority;
2773 break;
2774 }
2775 }
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002776 chain = &init_net.xfrm.policy_inexact[dir];
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002777 hlist_for_each_entry(pol, entry, chain, bydst) {
2778 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2779 pol->type == type &&
2780 pol->priority < priority) {
2781 ret = pol;
2782 break;
2783 }
2784 }
2785
2786 if (ret)
2787 xfrm_pol_hold(ret);
2788
2789 read_unlock_bh(&xfrm_policy_lock);
2790
2791 return ret;
2792}
2793
2794static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
2795{
2796 int match = 0;
2797
2798 if (t->mode == m->mode && t->id.proto == m->proto &&
2799 (m->reqid == 0 || t->reqid == m->reqid)) {
2800 switch (t->mode) {
2801 case XFRM_MODE_TUNNEL:
2802 case XFRM_MODE_BEET:
2803 if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
2804 m->old_family) == 0 &&
2805 xfrm_addr_cmp(&t->saddr, &m->old_saddr,
2806 m->old_family) == 0) {
2807 match = 1;
2808 }
2809 break;
2810 case XFRM_MODE_TRANSPORT:
2811 /* in case of transport mode, template does not store
2812 any IP addresses, hence we just compare mode and
2813 protocol */
2814 match = 1;
2815 break;
2816 default:
2817 break;
2818 }
2819 }
2820 return match;
2821}
2822
2823/* update endpoint address(es) of template(s) */
2824static int xfrm_policy_migrate(struct xfrm_policy *pol,
2825 struct xfrm_migrate *m, int num_migrate)
2826{
2827 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002828 int i, j, n = 0;
2829
2830 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07002831 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002832 /* target policy has been deleted */
2833 write_unlock_bh(&pol->lock);
2834 return -ENOENT;
2835 }
2836
2837 for (i = 0; i < pol->xfrm_nr; i++) {
2838 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
2839 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
2840 continue;
2841 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07002842 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
2843 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002844 continue;
2845 /* update endpoints */
2846 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
2847 sizeof(pol->xfrm_vec[i].id.daddr));
2848 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
2849 sizeof(pol->xfrm_vec[i].saddr));
2850 pol->xfrm_vec[i].encap_family = mp->new_family;
2851 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00002852 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002853 }
2854 }
2855
2856 write_unlock_bh(&pol->lock);
2857
2858 if (!n)
2859 return -ENODATA;
2860
2861 return 0;
2862}
2863
2864static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
2865{
2866 int i, j;
2867
2868 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
2869 return -EINVAL;
2870
2871 for (i = 0; i < num_migrate; i++) {
2872 if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
2873 m[i].old_family) == 0) &&
2874 (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
2875 m[i].old_family) == 0))
2876 return -EINVAL;
2877 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
2878 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
2879 return -EINVAL;
2880
2881 /* check if there is any duplicated entry */
2882 for (j = i + 1; j < num_migrate; j++) {
2883 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
2884 sizeof(m[i].old_daddr)) &&
2885 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
2886 sizeof(m[i].old_saddr)) &&
2887 m[i].proto == m[j].proto &&
2888 m[i].mode == m[j].mode &&
2889 m[i].reqid == m[j].reqid &&
2890 m[i].old_family == m[j].old_family)
2891 return -EINVAL;
2892 }
2893 }
2894
2895 return 0;
2896}
2897
2898int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002899 struct xfrm_migrate *m, int num_migrate,
2900 struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002901{
2902 int i, err, nx_cur = 0, nx_new = 0;
2903 struct xfrm_policy *pol = NULL;
2904 struct xfrm_state *x, *xc;
2905 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
2906 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
2907 struct xfrm_migrate *mp;
2908
2909 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
2910 goto out;
2911
2912 /* Stage 1 - find policy */
2913 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
2914 err = -ENOENT;
2915 goto out;
2916 }
2917
2918 /* Stage 2 - find and update state(s) */
2919 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
2920 if ((x = xfrm_migrate_state_find(mp))) {
2921 x_cur[nx_cur] = x;
2922 nx_cur++;
2923 if ((xc = xfrm_state_migrate(x, mp))) {
2924 x_new[nx_new] = xc;
2925 nx_new++;
2926 } else {
2927 err = -ENODATA;
2928 goto restore_state;
2929 }
2930 }
2931 }
2932
2933 /* Stage 3 - update policy */
2934 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
2935 goto restore_state;
2936
2937 /* Stage 4 - delete old state(s) */
2938 if (nx_cur) {
2939 xfrm_states_put(x_cur, nx_cur);
2940 xfrm_states_delete(x_cur, nx_cur);
2941 }
2942
2943 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002944 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002945
2946 xfrm_pol_put(pol);
2947
2948 return 0;
2949out:
2950 return err;
2951
2952restore_state:
2953 if (pol)
2954 xfrm_pol_put(pol);
2955 if (nx_cur)
2956 xfrm_states_put(x_cur, nx_cur);
2957 if (nx_new)
2958 xfrm_states_delete(x_new, nx_new);
2959
2960 return err;
2961}
David S. Millere610e672007-02-08 13:29:15 -08002962EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002963#endif