blob: 7722baeb140dc2c279607a5807c370ca1245a75d [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
40static DEFINE_RWLOCK(xfrm_policy_lock);
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
43static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
44
Christoph Lametere18b8902006-12-06 20:33:20 -080045static struct kmem_cache *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
David S. Miller2518c7c2006-08-24 04:45:07 -070047static HLIST_HEAD(xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
49
50static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
51static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
Herbert Xu25ee3282007-12-11 09:32:34 -080052static void xfrm_init_pmtu(struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Wei Yongjun29fa0b302008-12-03 00:33:09 -080054static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
55 int dir);
56
Andrew Morton77681022006-11-08 22:46:26 -080057static inline int
58__xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
59{
60 return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
61 addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
62 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
63 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
64 (fl->proto == sel->proto || !sel->proto) &&
65 (fl->oif == sel->ifindex || !sel->ifindex);
66}
67
68static inline int
69__xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
70{
71 return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
72 addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
73 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
74 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
75 (fl->proto == sel->proto || !sel->proto) &&
76 (fl->oif == sel->ifindex || !sel->ifindex);
77}
78
79int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
80 unsigned short family)
81{
82 switch (family) {
83 case AF_INET:
84 return __xfrm4_selector_match(sel, fl);
85 case AF_INET6:
86 return __xfrm6_selector_match(sel, fl);
87 }
88 return 0;
89}
90
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -080091static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +090092 xfrm_address_t *saddr,
93 xfrm_address_t *daddr,
94 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Herbert Xu66cdb3c2007-11-13 21:37:28 -080096 struct xfrm_policy_afinfo *afinfo;
97 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Herbert Xu25ee3282007-12-11 09:32:34 -080099 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800101 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800103 dst = afinfo->dst_lookup(net, tos, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900106
107 return dst;
108}
109
110static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
111 xfrm_address_t *prev_saddr,
112 xfrm_address_t *prev_daddr,
113 int family)
114{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800115 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900116 xfrm_address_t *saddr = &x->props.saddr;
117 xfrm_address_t *daddr = &x->id.daddr;
118 struct dst_entry *dst;
119
120 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
121 saddr = x->coaddr;
122 daddr = prev_daddr;
123 }
124 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
125 saddr = prev_saddr;
126 daddr = x->coaddr;
127 }
128
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800129 dst = __xfrm_dst_lookup(net, tos, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900130
131 if (!IS_ERR(dst)) {
132 if (prev_saddr != saddr)
133 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
134 if (prev_daddr != daddr)
135 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
136 }
137
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800138 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static inline unsigned long make_jiffies(long secs)
142{
143 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
144 return MAX_SCHEDULE_TIMEOUT-1;
145 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900146 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149static void xfrm_policy_timer(unsigned long data)
150{
151 struct xfrm_policy *xp = (struct xfrm_policy*)data;
James Morris9d729f72007-03-04 16:12:44 -0800152 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 long next = LONG_MAX;
154 int warn = 0;
155 int dir;
156
157 read_lock(&xp->lock);
158
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000159 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 goto out;
161
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700162 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 if (xp->lft.hard_add_expires_seconds) {
165 long tmo = xp->lft.hard_add_expires_seconds +
166 xp->curlft.add_time - now;
167 if (tmo <= 0)
168 goto expired;
169 if (tmo < next)
170 next = tmo;
171 }
172 if (xp->lft.hard_use_expires_seconds) {
173 long tmo = xp->lft.hard_use_expires_seconds +
174 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
175 if (tmo <= 0)
176 goto expired;
177 if (tmo < next)
178 next = tmo;
179 }
180 if (xp->lft.soft_add_expires_seconds) {
181 long tmo = xp->lft.soft_add_expires_seconds +
182 xp->curlft.add_time - now;
183 if (tmo <= 0) {
184 warn = 1;
185 tmo = XFRM_KM_TIMEOUT;
186 }
187 if (tmo < next)
188 next = tmo;
189 }
190 if (xp->lft.soft_use_expires_seconds) {
191 long tmo = xp->lft.soft_use_expires_seconds +
192 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
193 if (tmo <= 0) {
194 warn = 1;
195 tmo = XFRM_KM_TIMEOUT;
196 }
197 if (tmo < next)
198 next = tmo;
199 }
200
201 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800202 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (next != LONG_MAX &&
204 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
205 xfrm_pol_hold(xp);
206
207out:
208 read_unlock(&xp->lock);
209 xfrm_pol_put(xp);
210 return;
211
212expired:
213 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700214 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800215 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 xfrm_pol_put(xp);
217}
218
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000219static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
220{
221 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
222
223 if (unlikely(pol->walk.dead))
224 flo = NULL;
225 else
226 xfrm_pol_hold(pol);
227
228 return flo;
229}
230
231static int xfrm_policy_flo_check(struct flow_cache_object *flo)
232{
233 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
234
235 return !pol->walk.dead;
236}
237
238static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
239{
240 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
241}
242
243static const struct flow_cache_ops xfrm_policy_fc_ops = {
244 .get = xfrm_policy_flo_get,
245 .check = xfrm_policy_flo_check,
246 .delete = xfrm_policy_flo_delete,
247};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
250 * SPD calls.
251 */
252
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800253struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct xfrm_policy *policy;
256
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700257 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800260 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700261 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700262 INIT_HLIST_NODE(&policy->bydst);
263 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700265 atomic_set(&policy->refcnt, 1);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800266 setup_timer(&policy->timer, xfrm_policy_timer,
267 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000268 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270 return policy;
271}
272EXPORT_SYMBOL(xfrm_policy_alloc);
273
274/* Destroy xfrm_policy: descendant resources must be released to this moment. */
275
WANG Cong64c31b32008-01-07 22:34:29 -0800276void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Herbert Xu12a169e2008-10-01 07:03:24 -0700278 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Kris Katterjohn09a62662006-01-08 22:24:28 -0800280 BUG_ON(policy->bundles);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 if (del_timer(&policy->timer))
283 BUG();
284
Paul Moore03e1ad72008-04-12 19:07:52 -0700285 security_xfrm_policy_free(policy->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 kfree(policy);
287}
WANG Cong64c31b32008-01-07 22:34:29 -0800288EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
291{
292 struct dst_entry *dst;
293
294 while ((dst = policy->bundles) != NULL) {
295 policy->bundles = dst->next;
296 dst_free(dst);
297 }
298
299 if (del_timer(&policy->timer))
300 atomic_dec(&policy->refcnt);
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 xfrm_pol_put(policy);
303}
304
David Howellsc4028952006-11-22 14:57:56 +0000305static void xfrm_policy_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 struct xfrm_policy *policy;
David S. Miller2518c7c2006-08-24 04:45:07 -0700308 struct hlist_node *entry, *tmp;
309 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 spin_lock_bh(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700312 gc_list.first = xfrm_policy_gc_list.first;
313 INIT_HLIST_HEAD(&xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 spin_unlock_bh(&xfrm_policy_gc_lock);
315
David S. Miller2518c7c2006-08-24 04:45:07 -0700316 hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 xfrm_policy_gc_kill(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
Alexey Dobriyanc9583962008-11-25 17:13:59 -0800319static DECLARE_WORK(xfrm_policy_gc_work, xfrm_policy_gc_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321/* Rule must be locked. Release descentant resources, announce
322 * entry dead. The rule must be unlinked from lists to the moment.
323 */
324
325static void xfrm_policy_kill(struct xfrm_policy *policy)
326{
Herbert Xu12a169e2008-10-01 07:03:24 -0700327 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Alexey Dobriyanbbb770e2008-11-03 19:11:29 -0800329 spin_lock_bh(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700330 hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
Alexey Dobriyanbbb770e2008-11-03 19:11:29 -0800331 spin_unlock_bh(&xfrm_policy_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 schedule_work(&xfrm_policy_gc_work);
334}
335
David S. Miller2518c7c2006-08-24 04:45:07 -0700336static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
337
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800338static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700339{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800340 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700341}
342
Alexey Dobriyan11219942008-11-25 17:33:06 -0800343static 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 -0700344{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800345 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700346 unsigned int hash = __sel_hash(sel, family, hmask);
347
348 return (hash == hmask + 1 ?
Alexey Dobriyan11219942008-11-25 17:33:06 -0800349 &net->xfrm.policy_inexact[dir] :
350 net->xfrm.policy_bydst[dir].table + hash);
David S. Miller2518c7c2006-08-24 04:45:07 -0700351}
352
Alexey Dobriyan11219942008-11-25 17:33:06 -0800353static 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 -0700354{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800355 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700356 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
357
Alexey Dobriyan11219942008-11-25 17:33:06 -0800358 return net->xfrm.policy_bydst[dir].table + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700359}
360
David S. Miller2518c7c2006-08-24 04:45:07 -0700361static void xfrm_dst_hash_transfer(struct hlist_head *list,
362 struct hlist_head *ndsttable,
363 unsigned int nhashmask)
364{
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800365 struct hlist_node *entry, *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700366 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800367 unsigned int h0 = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700368
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800369redo:
David S. Miller2518c7c2006-08-24 04:45:07 -0700370 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
371 unsigned int h;
372
373 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
374 pol->family, nhashmask);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800375 if (!entry0) {
376 hlist_del(entry);
377 hlist_add_head(&pol->bydst, ndsttable+h);
378 h0 = h;
379 } else {
380 if (h != h0)
381 continue;
382 hlist_del(entry);
383 hlist_add_after(entry0, &pol->bydst);
384 }
385 entry0 = entry;
386 }
387 if (!hlist_empty(list)) {
388 entry0 = NULL;
389 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700390 }
391}
392
393static void xfrm_idx_hash_transfer(struct hlist_head *list,
394 struct hlist_head *nidxtable,
395 unsigned int nhashmask)
396{
397 struct hlist_node *entry, *tmp;
398 struct xfrm_policy *pol;
399
400 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
401 unsigned int h;
402
403 h = __idx_hash(pol->index, nhashmask);
404 hlist_add_head(&pol->byidx, nidxtable+h);
405 }
406}
407
408static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
409{
410 return ((old_hmask + 1) << 1) - 1;
411}
412
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800413static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700414{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800415 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700416 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
417 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800418 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700419 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700420 int i;
421
422 if (!ndst)
423 return;
424
425 write_lock_bh(&xfrm_policy_lock);
426
427 for (i = hmask; i >= 0; i--)
428 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
429
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800430 net->xfrm.policy_bydst[dir].table = ndst;
431 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700432
433 write_unlock_bh(&xfrm_policy_lock);
434
David S. Miller44e36b42006-08-24 04:50:50 -0700435 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700436}
437
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800438static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700439{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800440 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700441 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
442 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800443 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700444 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700445 int i;
446
447 if (!nidx)
448 return;
449
450 write_lock_bh(&xfrm_policy_lock);
451
452 for (i = hmask; i >= 0; i--)
453 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
454
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800455 net->xfrm.policy_byidx = nidx;
456 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700457
458 write_unlock_bh(&xfrm_policy_lock);
459
David S. Miller44e36b42006-08-24 04:50:50 -0700460 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700461}
462
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800463static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700464{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800465 unsigned int cnt = net->xfrm.policy_count[dir];
466 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700467
468 if (total)
469 *total += cnt;
470
471 if ((hmask + 1) < xfrm_policy_hashmax &&
472 cnt > hmask)
473 return 1;
474
475 return 0;
476}
477
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800478static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700479{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800480 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700481
482 if ((hmask + 1) < xfrm_policy_hashmax &&
483 total > hmask)
484 return 1;
485
486 return 0;
487}
488
Alexey Dobriyane0710412010-01-23 13:37:10 +0000489void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700490{
491 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000492 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
493 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
494 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
495 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
496 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
497 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
498 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700499 si->spdhmcnt = xfrm_policy_hashmax;
500 read_unlock_bh(&xfrm_policy_lock);
501}
502EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700503
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700504static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800505static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700506{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800507 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700508 int dir, total;
509
510 mutex_lock(&hash_resize_mutex);
511
512 total = 0;
513 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800514 if (xfrm_bydst_should_resize(net, dir, &total))
515 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700516 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800517 if (xfrm_byidx_should_resize(net, total))
518 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700519
520 mutex_unlock(&hash_resize_mutex);
521}
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/* Generate new index... KAME seems to generate them ordered by cost
524 * of an absolute inpredictability of ordering of rules. This will not pass. */
Alexey Dobriyan11219942008-11-25 17:33:06 -0800525static u32 xfrm_gen_index(struct net *net, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 static u32 idx_generator;
528
529 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700530 struct hlist_node *entry;
531 struct hlist_head *list;
532 struct xfrm_policy *p;
533 u32 idx;
534 int found;
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 idx = (idx_generator | dir);
537 idx_generator += 8;
538 if (idx == 0)
539 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800540 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700541 found = 0;
542 hlist_for_each_entry(p, entry, list, byidx) {
543 if (p->index == idx) {
544 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700548 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return idx;
550 }
551}
552
David S. Miller2518c7c2006-08-24 04:45:07 -0700553static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
554{
555 u32 *p1 = (u32 *) s1;
556 u32 *p2 = (u32 *) s2;
557 int len = sizeof(struct xfrm_selector) / sizeof(u32);
558 int i;
559
560 for (i = 0; i < len; i++) {
561 if (p1[i] != p2[i])
562 return 1;
563 }
564
565 return 0;
566}
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
569{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800570 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700571 struct xfrm_policy *pol;
572 struct xfrm_policy *delpol;
573 struct hlist_head *chain;
Herbert Xua6c7ab52007-01-16 16:52:02 -0800574 struct hlist_node *entry, *newpos;
David S. Miller9b78a822005-12-22 07:39:48 -0800575 struct dst_entry *gc_list;
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000576 u32 mark = policy->mark.v & policy->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800579 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700580 delpol = NULL;
581 newpos = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700582 hlist_for_each_entry(pol, entry, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800583 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700584 !selector_cmp(&pol->selector, &policy->selector) &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000585 (mark & pol->mark.m) == pol->mark.v &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800586 xfrm_sec_ctx_match(pol->security, policy->security) &&
587 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (excl) {
589 write_unlock_bh(&xfrm_policy_lock);
590 return -EEXIST;
591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 delpol = pol;
593 if (policy->priority > pol->priority)
594 continue;
595 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800596 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 continue;
598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (delpol)
600 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700603 hlist_add_after(newpos, &policy->bydst);
604 else
605 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800607 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 atomic_inc(&flow_cache_genid);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800609 if (delpol)
610 __xfrm_policy_unlink(delpol, dir);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800611 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir);
612 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800613 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 policy->curlft.use_time = 0;
615 if (!mod_timer(&policy->timer, jiffies + HZ))
616 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800617 list_add(&policy->walk.all, &net->xfrm.policy_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 write_unlock_bh(&xfrm_policy_lock);
619
David S. Miller9b78a822005-12-22 07:39:48 -0800620 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800622 else if (xfrm_bydst_should_resize(net, dir, NULL))
623 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800624
625 read_lock_bh(&xfrm_policy_lock);
626 gc_list = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700627 entry = &policy->bydst;
628 hlist_for_each_entry_continue(policy, entry, bydst) {
David S. Miller9b78a822005-12-22 07:39:48 -0800629 struct dst_entry *dst;
630
631 write_lock(&policy->lock);
632 dst = policy->bundles;
633 if (dst) {
634 struct dst_entry *tail = dst;
635 while (tail->next)
636 tail = tail->next;
637 tail->next = gc_list;
638 gc_list = dst;
639
640 policy->bundles = NULL;
641 }
642 write_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
David S. Miller9b78a822005-12-22 07:39:48 -0800644 read_unlock_bh(&xfrm_policy_lock);
645
646 while (gc_list) {
647 struct dst_entry *dst = gc_list;
648
649 gc_list = dst->next;
650 dst_free(dst);
651 }
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return 0;
654}
655EXPORT_SYMBOL(xfrm_policy_insert);
656
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000657struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
658 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800659 struct xfrm_sec_ctx *ctx, int delete,
660 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
David S. Miller2518c7c2006-08-24 04:45:07 -0700662 struct xfrm_policy *pol, *ret;
663 struct hlist_head *chain;
664 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Eric Parisef41aaa2007-03-07 15:37:58 -0800666 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800668 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700669 ret = NULL;
670 hlist_for_each_entry(pol, entry, chain, bydst) {
671 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000672 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700673 !selector_cmp(sel, &pol->selector) &&
674 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700676 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700677 *err = security_xfrm_policy_delete(
678 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800679 if (*err) {
680 write_unlock_bh(&xfrm_policy_lock);
681 return pol;
682 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800683 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700684 }
685 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 break;
687 }
688 }
689 write_unlock_bh(&xfrm_policy_lock);
690
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000691 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700692 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700693 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
Trent Jaegerdf718372005-12-13 23:12:27 -0800695EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000697struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
698 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
David S. Miller2518c7c2006-08-24 04:45:07 -0700700 struct xfrm_policy *pol, *ret;
701 struct hlist_head *chain;
702 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Herbert Xub5505c62007-05-14 02:15:47 -0700704 *err = -ENOENT;
705 if (xfrm_policy_id2dir(id) != dir)
706 return NULL;
707
Eric Parisef41aaa2007-03-07 15:37:58 -0800708 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800710 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700711 ret = NULL;
712 hlist_for_each_entry(pol, entry, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000713 if (pol->type == type && pol->index == id &&
714 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700716 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700717 *err = security_xfrm_policy_delete(
718 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800719 if (*err) {
720 write_unlock_bh(&xfrm_policy_lock);
721 return pol;
722 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800723 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700724 }
725 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 break;
727 }
728 }
729 write_unlock_bh(&xfrm_policy_lock);
730
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000731 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700732 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700733 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735EXPORT_SYMBOL(xfrm_policy_byid);
736
Joy Latten4aa2e622007-06-04 19:05:57 -0400737#ifdef CONFIG_SECURITY_NETWORK_XFRM
738static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800739xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Joy Latten4aa2e622007-06-04 19:05:57 -0400741 int dir, err = 0;
742
743 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
744 struct xfrm_policy *pol;
745 struct hlist_node *entry;
746 int i;
747
748 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800749 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400750 if (pol->type != type)
751 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700752 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400753 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700754 xfrm_audit_policy_delete(pol, 0,
755 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400756 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700757 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400758 return err;
759 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900760 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800761 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400762 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800763 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400764 bydst) {
765 if (pol->type != type)
766 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700767 err = security_xfrm_policy_delete(
768 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400769 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700770 xfrm_audit_policy_delete(pol, 0,
771 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400772 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700773 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400774 return err;
775 }
776 }
777 }
778 }
779 return err;
780}
781#else
782static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800783xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400784{
785 return 0;
786}
787#endif
788
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800789int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400790{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000791 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 write_lock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400794
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800795 err = xfrm_policy_flush_secctx_check(net, type, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400796 if (err)
797 goto out;
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700800 struct xfrm_policy *pol;
801 struct hlist_node *entry;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800802 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700803
804 again1:
805 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800806 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700807 if (pol->type != type)
808 continue;
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000809 __xfrm_policy_unlink(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000811 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Joy Lattenab5f5e82007-09-17 11:51:22 -0700813 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400814 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700815 audit_info->secid);
Joy Latten161a09e2006-11-27 13:11:54 -0600816
David S. Miller2518c7c2006-08-24 04:45:07 -0700817 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700820 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700822
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800823 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700824 again2:
825 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800826 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700827 bydst) {
828 if (pol->type != type)
829 continue;
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000830 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700831 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000832 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700833
Joy Lattenab5f5e82007-09-17 11:51:22 -0700834 xfrm_audit_policy_delete(pol, 1,
835 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400836 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700837 audit_info->secid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700838 xfrm_policy_kill(pol);
839
840 write_lock_bh(&xfrm_policy_lock);
841 goto again2;
842 }
843 }
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000846 if (!cnt)
847 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400848out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 write_unlock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400850 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852EXPORT_SYMBOL(xfrm_policy_flush);
853
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800854int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800855 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 void *data)
857{
Herbert Xu12a169e2008-10-01 07:03:24 -0700858 struct xfrm_policy *pol;
859 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -0800860 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Timo Teras4c563f72008-02-28 21:31:08 -0800862 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
863 walk->type != XFRM_POLICY_TYPE_ANY)
864 return -EINVAL;
865
Herbert Xu12a169e2008-10-01 07:03:24 -0700866 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -0800867 return 0;
868
Herbert Xu12a169e2008-10-01 07:03:24 -0700869 write_lock_bh(&xfrm_policy_lock);
870 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800871 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -0700872 else
873 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800874 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -0700875 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -0800876 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -0700877 pol = container_of(x, struct xfrm_policy, walk);
878 if (walk->type != XFRM_POLICY_TYPE_ANY &&
879 walk->type != pol->type)
880 continue;
881 error = func(pol, xfrm_policy_id2dir(pol->index),
882 walk->seq, data);
883 if (error) {
884 list_move_tail(&walk->walk.all, &x->all);
885 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -0800886 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700887 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700889 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800890 error = -ENOENT;
891 goto out;
892 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700893 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894out:
Herbert Xu12a169e2008-10-01 07:03:24 -0700895 write_unlock_bh(&xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return error;
897}
898EXPORT_SYMBOL(xfrm_policy_walk);
899
Herbert Xu12a169e2008-10-01 07:03:24 -0700900void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
901{
902 INIT_LIST_HEAD(&walk->walk.all);
903 walk->walk.dead = 1;
904 walk->type = type;
905 walk->seq = 0;
906}
907EXPORT_SYMBOL(xfrm_policy_walk_init);
908
909void xfrm_policy_walk_done(struct xfrm_policy_walk *walk)
910{
911 if (list_empty(&walk->walk.all))
912 return;
913
914 write_lock_bh(&xfrm_policy_lock);
915 list_del(&walk->walk.all);
916 write_unlock_bh(&xfrm_policy_lock);
917}
918EXPORT_SYMBOL(xfrm_policy_walk_done);
919
James Morris134b0fc2006-10-05 15:42:27 -0500920/*
921 * Find policy to apply to this flow.
922 *
923 * Returns 0 if policy found, else an -errno.
924 */
David S. Miller2518c7c2006-08-24 04:45:07 -0700925static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
926 u8 type, u16 family, int dir)
927{
928 struct xfrm_selector *sel = &pol->selector;
James Morris134b0fc2006-10-05 15:42:27 -0500929 int match, ret = -ESRCH;
David S. Miller2518c7c2006-08-24 04:45:07 -0700930
931 if (pol->family != family ||
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000932 (fl->mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -0700933 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500934 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700935
936 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500937 if (match)
Paul Moore03e1ad72008-04-12 19:07:52 -0700938 ret = security_xfrm_policy_lookup(pol->security, fl->secid,
939 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700940
James Morris134b0fc2006-10-05 15:42:27 -0500941 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700942}
943
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800944static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
945 struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700946 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
James Morris134b0fc2006-10-05 15:42:27 -0500948 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700949 struct xfrm_policy *pol, *ret;
950 xfrm_address_t *daddr, *saddr;
951 struct hlist_node *entry;
952 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700953 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700954
955 daddr = xfrm_flowi_daddr(fl, family);
956 saddr = xfrm_flowi_saddr(fl, family);
957 if (unlikely(!daddr || !saddr))
958 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800961 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700962 ret = NULL;
963 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500964 err = xfrm_policy_match(pol, fl, type, family, dir);
965 if (err) {
966 if (err == -ESRCH)
967 continue;
968 else {
969 ret = ERR_PTR(err);
970 goto fail;
971 }
972 } else {
David S. Milleracba48e2006-08-25 15:46:46 -0700973 ret = pol;
974 priority = ret->priority;
975 break;
976 }
977 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800978 chain = &net->xfrm.policy_inexact[dir];
David S. Milleracba48e2006-08-25 15:46:46 -0700979 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500980 err = xfrm_policy_match(pol, fl, type, family, dir);
981 if (err) {
982 if (err == -ESRCH)
983 continue;
984 else {
985 ret = ERR_PTR(err);
986 goto fail;
987 }
988 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700989 ret = pol;
990 break;
991 }
992 }
David S. Milleracba48e2006-08-25 15:46:46 -0700993 if (ret)
994 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -0500995fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700997
David S. Miller2518c7c2006-08-24 04:45:07 -0700998 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700999}
1000
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001001static struct flow_cache_object *
1002xfrm_policy_lookup(struct net *net, struct flowi *fl, u16 family,
1003 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001004{
1005 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001006
1007 if (old_obj)
1008 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001009
1010#ifdef CONFIG_XFRM_SUB_POLICY
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001011 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001012 if (IS_ERR(pol))
1013 return ERR_CAST(pol);
1014 if (pol)
1015 goto found;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001016#endif
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001017 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001018 if (IS_ERR(pol))
1019 return ERR_CAST(pol);
1020 if (pol)
1021 goto found;
1022 return NULL;
1023
1024found:
1025 /* Resolver returns two references:
1026 * one for cache and one for caller of flow_cache_lookup() */
1027 xfrm_pol_hold(pol);
1028
1029 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
Trent Jaegerdf718372005-12-13 23:12:27 -08001032static inline int policy_to_flow_dir(int dir)
1033{
1034 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001035 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1036 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1037 return dir;
1038 switch (dir) {
1039 default:
1040 case XFRM_POLICY_IN:
1041 return FLOW_DIR_IN;
1042 case XFRM_POLICY_OUT:
1043 return FLOW_DIR_OUT;
1044 case XFRM_POLICY_FWD:
1045 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001046 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001047}
1048
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001049static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
1051 struct xfrm_policy *pol;
1052
1053 read_lock_bh(&xfrm_policy_lock);
1054 if ((pol = sk->sk_policy[dir]) != NULL) {
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001055 int match = xfrm_selector_match(&pol->selector, fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001057 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001058
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001059 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001060 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1061 pol = NULL;
1062 goto out;
1063 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001064 err = security_xfrm_policy_lookup(pol->security,
1065 fl->secid,
1066 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001067 if (!err)
1068 xfrm_pol_hold(pol);
1069 else if (err == -ESRCH)
1070 pol = NULL;
1071 else
1072 pol = ERR_PTR(err);
1073 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 pol = NULL;
1075 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001076out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 read_unlock_bh(&xfrm_policy_lock);
1078 return pol;
1079}
1080
1081static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1082{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001083 struct net *net = xp_net(pol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001084 struct hlist_head *chain = policy_hash_bysel(net, &pol->selector,
David S. Miller2518c7c2006-08-24 04:45:07 -07001085 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001086
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001087 list_add(&pol->walk.all, &net->xfrm.policy_all);
David S. Miller2518c7c2006-08-24 04:45:07 -07001088 hlist_add_head(&pol->bydst, chain);
Alexey Dobriyane92303f2008-11-25 17:32:41 -08001089 hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001090 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001092
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001093 if (xfrm_bydst_should_resize(net, dir, NULL))
1094 schedule_work(&net->xfrm.policy_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
1097static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1098 int dir)
1099{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001100 struct net *net = xp_net(pol);
1101
David S. Miller2518c7c2006-08-24 04:45:07 -07001102 if (hlist_unhashed(&pol->bydst))
1103 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
David S. Miller2518c7c2006-08-24 04:45:07 -07001105 hlist_del(&pol->bydst);
1106 hlist_del(&pol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -07001107 list_del(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001108 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001109
1110 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
Herbert Xu4666faa2005-06-18 22:43:22 -07001113int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 write_lock_bh(&xfrm_policy_lock);
1116 pol = __xfrm_policy_unlink(pol, dir);
1117 write_unlock_bh(&xfrm_policy_lock);
1118 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001122 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
David S. Millera70fcb02006-03-20 19:18:52 -08001124EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1127{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001128 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 struct xfrm_policy *old_pol;
1130
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001131#ifdef CONFIG_XFRM_SUB_POLICY
1132 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1133 return -EINVAL;
1134#endif
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 write_lock_bh(&xfrm_policy_lock);
1137 old_pol = sk->sk_policy[dir];
1138 sk->sk_policy[dir] = pol;
1139 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001140 pol->curlft.add_time = get_seconds();
Alexey Dobriyan11219942008-11-25 17:33:06 -08001141 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1143 }
1144 if (old_pol)
Timo Teräsea2dea9d2010-03-31 00:17:05 +00001145 /* Unlinking succeeds always. This is the only function
1146 * allowed to delete or replace socket policy.
1147 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1149 write_unlock_bh(&xfrm_policy_lock);
1150
1151 if (old_pol) {
1152 xfrm_policy_kill(old_pol);
1153 }
1154 return 0;
1155}
1156
1157static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1158{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001159 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 if (newp) {
1162 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001163 if (security_xfrm_policy_clone(old->security,
1164 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001165 kfree(newp);
1166 return NULL; /* ENOMEM */
1167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 newp->lft = old->lft;
1169 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001170 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 newp->action = old->action;
1172 newp->flags = old->flags;
1173 newp->xfrm_nr = old->xfrm_nr;
1174 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001175 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 memcpy(newp->xfrm_vec, old->xfrm_vec,
1177 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1178 write_lock_bh(&xfrm_policy_lock);
1179 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1180 write_unlock_bh(&xfrm_policy_lock);
1181 xfrm_pol_put(newp);
1182 }
1183 return newp;
1184}
1185
1186int __xfrm_sk_clone_policy(struct sock *sk)
1187{
1188 struct xfrm_policy *p0 = sk->sk_policy[0],
1189 *p1 = sk->sk_policy[1];
1190
1191 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1192 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1193 return -ENOMEM;
1194 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1195 return -ENOMEM;
1196 return 0;
1197}
1198
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001199static int
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001200xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001201 unsigned short family)
1202{
1203 int err;
1204 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1205
1206 if (unlikely(afinfo == NULL))
1207 return -EINVAL;
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001208 err = afinfo->get_saddr(net, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001209 xfrm_policy_put_afinfo(afinfo);
1210 return err;
1211}
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213/* Resolve list of templates for the flow, given policy. */
1214
1215static int
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001216xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1217 struct xfrm_state **xfrm,
1218 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001220 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 int nx;
1222 int i, error;
1223 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1224 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001225 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1228 struct xfrm_state *x;
1229 xfrm_address_t *remote = daddr;
1230 xfrm_address_t *local = saddr;
1231 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1232
Joakim Koskela48b8d782007-07-26 00:08:42 -07001233 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1234 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 remote = &tmpl->id.daddr;
1236 local = &tmpl->saddr;
Miika Komu76b3f052006-11-30 16:40:43 -08001237 family = tmpl->encap_family;
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001238 if (xfrm_addr_any(local, family)) {
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001239 error = xfrm_get_saddr(net, &tmp, remote, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001240 if (error)
1241 goto fail;
1242 local = &tmp;
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
1245
1246 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1247
1248 if (x && x->km.state == XFRM_STATE_VALID) {
1249 xfrm[nx++] = x;
1250 daddr = remote;
1251 saddr = local;
1252 continue;
1253 }
1254 if (x) {
1255 error = (x->km.state == XFRM_STATE_ERROR ?
1256 -EINVAL : -EAGAIN);
1257 xfrm_state_put(x);
1258 }
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001259 else if (error == -ESRCH)
1260 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 if (!tmpl->optional)
1263 goto fail;
1264 }
1265 return nx;
1266
1267fail:
1268 for (nx--; nx>=0; nx--)
1269 xfrm_state_put(xfrm[nx]);
1270 return error;
1271}
1272
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001273static int
1274xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1275 struct xfrm_state **xfrm,
1276 unsigned short family)
1277{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001278 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1279 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001280 int cnx = 0;
1281 int error;
1282 int ret;
1283 int i;
1284
1285 for (i = 0; i < npols; i++) {
1286 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1287 error = -ENOBUFS;
1288 goto fail;
1289 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001290
1291 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001292 if (ret < 0) {
1293 error = ret;
1294 goto fail;
1295 } else
1296 cnx += ret;
1297 }
1298
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001299 /* found states are sorted for outbound processing */
1300 if (npols > 1)
1301 xfrm_state_sort(xfrm, tpp, cnx, family);
1302
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001303 return cnx;
1304
1305 fail:
1306 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001307 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001308 return error;
1309
1310}
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312/* Check that the bundle accepts the flow and its components are
1313 * still valid.
1314 */
1315
1316static struct dst_entry *
1317xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1318{
1319 struct dst_entry *x;
1320 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1321 if (unlikely(afinfo == NULL))
1322 return ERR_PTR(-EINVAL);
1323 x = afinfo->find_bundle(fl, policy);
1324 xfrm_policy_put_afinfo(afinfo);
1325 return x;
1326}
1327
Herbert Xu25ee3282007-12-11 09:32:34 -08001328static inline int xfrm_get_tos(struct flowi *fl, int family)
1329{
1330 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1331 int tos;
1332
1333 if (!afinfo)
1334 return -EINVAL;
1335
1336 tos = afinfo->get_tos(fl);
1337
1338 xfrm_policy_put_afinfo(afinfo);
1339
1340 return tos;
1341}
1342
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001343static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001344{
1345 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001346 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001347 struct xfrm_dst *xdst;
1348
1349 if (!afinfo)
1350 return ERR_PTR(-EINVAL);
1351
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001352 switch (family) {
1353 case AF_INET:
1354 dst_ops = &net->xfrm.xfrm4_dst_ops;
1355 break;
1356#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1357 case AF_INET6:
1358 dst_ops = &net->xfrm.xfrm6_dst_ops;
1359 break;
1360#endif
1361 default:
1362 BUG();
1363 }
1364 xdst = dst_alloc(dst_ops) ?: ERR_PTR(-ENOBUFS);
Herbert Xu25ee3282007-12-11 09:32:34 -08001365
1366 xfrm_policy_put_afinfo(afinfo);
1367
1368 return xdst;
1369}
1370
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001371static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1372 int nfheader_len)
1373{
1374 struct xfrm_policy_afinfo *afinfo =
1375 xfrm_policy_get_afinfo(dst->ops->family);
1376 int err;
1377
1378 if (!afinfo)
1379 return -EINVAL;
1380
1381 err = afinfo->init_path(path, dst, nfheader_len);
1382
1383 xfrm_policy_put_afinfo(afinfo);
1384
1385 return err;
1386}
1387
Herbert Xu87c1e122010-03-02 02:51:56 +00001388static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
1389 struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001390{
1391 struct xfrm_policy_afinfo *afinfo =
1392 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1393 int err;
1394
1395 if (!afinfo)
1396 return -EINVAL;
1397
Herbert Xu87c1e122010-03-02 02:51:56 +00001398 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001399
1400 xfrm_policy_put_afinfo(afinfo);
1401
1402 return err;
1403}
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1406 * all the metrics... Shortly, bundle a bundle.
1407 */
1408
Herbert Xu25ee3282007-12-11 09:32:34 -08001409static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1410 struct xfrm_state **xfrm, int nx,
1411 struct flowi *fl,
1412 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001414 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001415 unsigned long now = jiffies;
1416 struct net_device *dev;
1417 struct dst_entry *dst_prev = NULL;
1418 struct dst_entry *dst0 = NULL;
1419 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001421 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001422 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001423 int trailer_len = 0;
1424 int tos;
1425 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001426 xfrm_address_t saddr, daddr;
1427
1428 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001429
1430 tos = xfrm_get_tos(fl, family);
1431 err = tos;
1432 if (tos < 0)
1433 goto put_states;
1434
1435 dst_hold(dst);
1436
1437 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001438 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001439 struct dst_entry *dst1 = &xdst->u.dst;
1440
1441 err = PTR_ERR(xdst);
1442 if (IS_ERR(xdst)) {
1443 dst_release(dst);
1444 goto put_states;
1445 }
1446
1447 if (!dst_prev)
1448 dst0 = dst1;
1449 else {
1450 dst_prev->child = dst_clone(dst1);
1451 dst1->flags |= DST_NOHASH;
1452 }
1453
1454 xdst->route = dst;
1455 memcpy(&dst1->metrics, &dst->metrics, sizeof(dst->metrics));
1456
1457 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1458 family = xfrm[i]->props.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001459 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1460 family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001461 err = PTR_ERR(dst);
1462 if (IS_ERR(dst))
1463 goto put_states;
1464 } else
1465 dst_hold(dst);
1466
1467 dst1->xfrm = xfrm[i];
1468 xdst->genid = xfrm[i]->genid;
1469
1470 dst1->obsolete = -1;
1471 dst1->flags |= DST_HOST;
1472 dst1->lastuse = now;
1473
1474 dst1->input = dst_discard;
1475 dst1->output = xfrm[i]->outer_mode->afinfo->output;
1476
1477 dst1->next = dst_prev;
1478 dst_prev = dst1;
1479
1480 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001481 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1482 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001483 trailer_len += xfrm[i]->props.trailer_len;
1484 }
1485
1486 dst_prev->child = dst;
1487 dst0->path = dst;
1488
1489 err = -ENODEV;
1490 dev = dst->dev;
1491 if (!dev)
1492 goto free_dst;
1493
Ralf Baechle96c53402009-12-25 23:30:02 +00001494 /* Copy neighbour for reachability confirmation */
Herbert Xu25ee3282007-12-11 09:32:34 -08001495 dst0->neighbour = neigh_clone(dst->neighbour);
1496
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001497 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001498 xfrm_init_pmtu(dst_prev);
1499
1500 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1501 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1502
Herbert Xu87c1e122010-03-02 02:51:56 +00001503 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001504 if (err)
1505 goto free_dst;
1506
1507 dst_prev->header_len = header_len;
1508 dst_prev->trailer_len = trailer_len;
1509 header_len -= xdst->u.dst.xfrm->props.header_len;
1510 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1511 }
1512
1513out:
1514 return dst0;
1515
1516put_states:
1517 for (; i < nx; i++)
1518 xfrm_state_put(xfrm[i]);
1519free_dst:
1520 if (dst0)
1521 dst_free(dst0);
1522 dst0 = ERR_PTR(err);
1523 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001526static int inline
1527xfrm_dst_alloc_copy(void **target, void *src, int size)
1528{
1529 if (!*target) {
1530 *target = kmalloc(size, GFP_ATOMIC);
1531 if (!*target)
1532 return -ENOMEM;
1533 }
1534 memcpy(*target, src, size);
1535 return 0;
1536}
1537
1538static int inline
1539xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
1540{
1541#ifdef CONFIG_XFRM_SUB_POLICY
1542 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1543 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1544 sel, sizeof(*sel));
1545#else
1546 return 0;
1547#endif
1548}
1549
1550static int inline
1551xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
1552{
1553#ifdef CONFIG_XFRM_SUB_POLICY
1554 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1555 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1556#else
1557 return 0;
1558#endif
1559}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561static int stale_bundle(struct dst_entry *dst);
1562
1563/* Main function: finds/creates a bundle for given flow.
1564 *
1565 * At the moment we eat a raw IP route. Mostly to speed up lookups
1566 * on interfaces with disabled IPsec.
1567 */
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001568int __xfrm_lookup(struct net *net, struct dst_entry **dst_p, struct flowi *fl,
David S. Miller14e50e52007-05-24 18:17:54 -07001569 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
1571 struct xfrm_policy *policy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001572 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1573 int npols;
1574 int pol_dead;
1575 int xfrm_nr;
1576 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1578 struct dst_entry *dst, *dst_orig = *dst_p;
1579 int nx = 0;
1580 int err;
1581 u32 genid;
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001582 u16 family;
Trent Jaegerdf718372005-12-13 23:12:27 -08001583 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585restart:
1586 genid = atomic_read(&flow_cache_genid);
1587 policy = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001588 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1589 pols[pi] = NULL;
1590 npols = 0;
1591 pol_dead = 0;
1592 xfrm_nr = 0;
1593
Thomas Graff7944fb2007-08-25 13:46:55 -07001594 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001595 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
Herbert Xu75b8c132007-12-11 04:38:08 -08001596 err = PTR_ERR(policy);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001597 if (IS_ERR(policy)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001598 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
Herbert Xu75b8c132007-12-11 04:38:08 -08001599 goto dropdst;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001600 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 if (!policy) {
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001604 struct flow_cache_object *flo;
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07001607 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001608 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08001609 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001611 flo = flow_cache_lookup(net, fl, dst_orig->ops->family,
1612 dir, xfrm_policy_lookup, NULL);
1613 err = PTR_ERR(flo);
1614 if (IS_ERR(flo)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001615 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
Herbert Xu75b8c132007-12-11 04:38:08 -08001616 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08001617 }
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001618 if (flo)
1619 policy = container_of(flo, struct xfrm_policy, flo);
1620 else
1621 policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
1623
1624 if (!policy)
Herbert Xu8b7817f2007-12-12 10:44:43 -08001625 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001627 family = dst_orig->ops->family;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001628 pols[0] = policy;
1629 npols ++;
1630 xfrm_nr += pols[0]->xfrm_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Herbert Xuaef21782007-12-13 09:30:59 -08001632 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001633 if ((flags & XFRM_LOOKUP_ICMP) && !(policy->flags & XFRM_POLICY_ICMP))
1634 goto error;
1635
1636 policy->curlft.use_time = get_seconds();
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 switch (policy->action) {
Herbert Xu5e5234f2007-11-30 00:50:31 +11001639 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 case XFRM_POLICY_BLOCK:
1641 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001642 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07001643 err = -EPERM;
1644 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 case XFRM_POLICY_ALLOW:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001647#ifndef CONFIG_XFRM_SUB_POLICY
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (policy->xfrm_nr == 0) {
1649 /* Flow passes not transformed. */
1650 xfrm_pol_put(policy);
1651 return 0;
1652 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001653#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 /* Try to find matching bundle.
1656 *
1657 * LATER: help from flow cache. It is optional, this
1658 * is required only for output policy.
1659 */
1660 dst = xfrm_find_bundle(fl, policy, family);
1661 if (IS_ERR(dst)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001662 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
Patrick McHardye104411b2005-09-08 15:11:55 -07001663 err = PTR_ERR(dst);
1664 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 }
1666
1667 if (dst)
1668 break;
1669
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001670#ifdef CONFIG_XFRM_SUB_POLICY
1671 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001672 pols[1] = xfrm_policy_lookup_bytype(net,
1673 XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001674 fl, family,
1675 XFRM_POLICY_OUT);
1676 if (pols[1]) {
James Morris134b0fc2006-10-05 15:42:27 -05001677 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001678 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05001679 err = PTR_ERR(pols[1]);
1680 goto error;
1681 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001682 if (pols[1]->action == XFRM_POLICY_BLOCK) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001683 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001684 err = -EPERM;
1685 goto error;
1686 }
1687 npols ++;
1688 xfrm_nr += pols[1]->xfrm_nr;
1689 }
1690 }
1691
1692 /*
1693 * Because neither flowi nor bundle information knows about
1694 * transformation template size. On more than one policy usage
1695 * we can realize whether all of them is bypass or not after
1696 * they are searched. See above not-transformed bypass
1697 * is surrounded by non-sub policy configuration, too.
1698 */
1699 if (xfrm_nr == 0) {
1700 /* Flow passes not transformed. */
1701 xfrm_pols_put(pols, npols);
1702 return 0;
1703 }
1704
1705#endif
1706 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 if (unlikely(nx<0)) {
1709 err = nx;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001710 if (err == -EAGAIN && net->xfrm.sysctl_larval_drop) {
David S. Miller14e50e52007-05-24 18:17:54 -07001711 /* EREMOTE tells the caller to generate
1712 * a one-shot blackhole route.
1713 */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001714 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
David S. Miller14e50e52007-05-24 18:17:54 -07001715 xfrm_pol_put(policy);
1716 return -EREMOTE;
1717 }
Herbert Xu815f4e52007-12-12 10:36:59 -08001718 if (err == -EAGAIN && (flags & XFRM_LOOKUP_WAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 DECLARE_WAITQUEUE(wait, current);
1720
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001721 add_wait_queue(&net->xfrm.km_waitq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 set_current_state(TASK_INTERRUPTIBLE);
1723 schedule();
1724 set_current_state(TASK_RUNNING);
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001725 remove_wait_queue(&net->xfrm.km_waitq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001727 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729 if (nx == -EAGAIN && signal_pending(current)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001730 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 err = -ERESTART;
1732 goto error;
1733 }
1734 if (nx == -EAGAIN ||
1735 genid != atomic_read(&flow_cache_genid)) {
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001736 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 goto restart;
1738 }
1739 err = nx;
1740 }
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001741 if (err < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001742 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 goto error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746 if (nx == 0) {
1747 /* Flow passes not transformed. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001748 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 return 0;
1750 }
1751
Herbert Xu25ee3282007-12-11 09:32:34 -08001752 dst = xfrm_bundle_create(policy, xfrm, nx, fl, dst_orig);
1753 err = PTR_ERR(dst);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001754 if (IS_ERR(dst)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001755 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 goto error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Timo Teräsea2dea9d2010-03-31 00:17:05 +00001759 for (pi = 0; pi < npols; pi++)
Herbert Xu12a169e2008-10-01 07:03:24 -07001760 pol_dead |= pols[pi]->walk.dead;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 write_lock_bh(&policy->lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001763 if (unlikely(pol_dead || stale_bundle(dst))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 /* Wow! While we worked on resolving, this
1765 * policy has gone. Retry. It is not paranoia,
1766 * we just cannot enlist new bundle to dead object.
1767 * We can't enlist stable bundles either.
1768 */
1769 write_unlock_bh(&policy->lock);
Julien Brunel9d7d7402008-09-02 17:24:28 -07001770 dst_free(dst);
Herbert Xu00de6512006-02-13 16:01:27 -08001771
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001772 if (pol_dead)
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001773 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLDEAD);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001774 else
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001775 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
Herbert Xu00de6512006-02-13 16:01:27 -08001776 err = -EHOSTUNREACH;
1777 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001779
1780 if (npols > 1)
1781 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1782 else
1783 err = xfrm_dst_update_origin(dst, fl);
1784 if (unlikely(err)) {
1785 write_unlock_bh(&policy->lock);
Julien Brunel9d7d7402008-09-02 17:24:28 -07001786 dst_free(dst);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001787 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001788 goto error;
1789 }
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 dst->next = policy->bundles;
1792 policy->bundles = dst;
1793 dst_hold(dst);
1794 write_unlock_bh(&policy->lock);
1795 }
1796 *dst_p = dst;
1797 dst_release(dst_orig);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001798 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 return 0;
1800
1801error:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001802 xfrm_pols_put(pols, npols);
Herbert Xu75b8c132007-12-11 04:38:08 -08001803dropdst:
1804 dst_release(dst_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 *dst_p = NULL;
1806 return err;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001807
1808nopol:
Herbert Xuaef21782007-12-13 09:30:59 -08001809 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001810 if (flags & XFRM_LOOKUP_ICMP)
1811 goto dropdst;
1812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
David S. Miller14e50e52007-05-24 18:17:54 -07001814EXPORT_SYMBOL(__xfrm_lookup);
1815
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001816int xfrm_lookup(struct net *net, struct dst_entry **dst_p, struct flowi *fl,
David S. Miller14e50e52007-05-24 18:17:54 -07001817 struct sock *sk, int flags)
1818{
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001819 int err = __xfrm_lookup(net, dst_p, fl, sk, flags);
David S. Miller14e50e52007-05-24 18:17:54 -07001820
1821 if (err == -EREMOTE) {
1822 dst_release(*dst_p);
1823 *dst_p = NULL;
1824 err = -EAGAIN;
1825 }
1826
1827 return err;
1828}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829EXPORT_SYMBOL(xfrm_lookup);
1830
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001831static inline int
1832xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1833{
1834 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001835
1836 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1837 return 0;
1838 x = skb->sp->xvec[idx];
1839 if (!x->type->reject)
1840 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07001841 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001842}
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844/* When skb is transformed back to its "native" form, we have to
1845 * check policy restrictions. At the moment we make this in maximally
1846 * stupid way. Shame on me. :-) Of course, connected sockets must
1847 * have policy cached at them.
1848 */
1849
1850static inline int
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001851xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 unsigned short family)
1853{
1854 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08001855 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 return x->id.proto == tmpl->id.proto &&
1857 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1858 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1859 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07001860 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07001861 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001862 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1863 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
1865
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001866/*
1867 * 0 or more than 0 is returned when validation is succeeded (either bypass
1868 * because of optional transport mode, or next index of the mathced secpath
1869 * state with the template.
1870 * -1 is returned when no matching template is found.
1871 * Otherwise "-2 - errored_index" is returned.
1872 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873static inline int
1874xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1875 unsigned short family)
1876{
1877 int idx = start;
1878
1879 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001880 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return start;
1882 } else
1883 start = -1;
1884 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001885 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001887 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1888 if (start == -1)
1889 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
1893 return start;
1894}
1895
Herbert Xud5422ef2007-12-12 10:44:16 -08001896int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
1897 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
1899 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001900 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902 if (unlikely(afinfo == NULL))
1903 return -EAFNOSUPPORT;
1904
Herbert Xud5422ef2007-12-12 10:44:16 -08001905 afinfo->decode_session(skb, fl, reverse);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07001906 err = security_xfrm_decode_session(skb, &fl->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001908 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
Herbert Xud5422ef2007-12-12 10:44:16 -08001910EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001912static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
1914 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001915 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001916 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920
1921 return 0;
1922}
1923
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001924int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 unsigned short family)
1926{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08001927 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001929 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1930 int npols = 0;
1931 int xfrm_nr;
1932 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08001933 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08001935 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001936 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Herbert Xud5422ef2007-12-12 10:44:16 -08001938 reverse = dir & ~XFRM_POLICY_MASK;
1939 dir &= XFRM_POLICY_MASK;
1940 fl_dir = policy_to_flow_dir(dir);
1941
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001942 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001943 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001945 }
1946
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08001947 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 /* First, check used SA against their selectors. */
1950 if (skb->sp) {
1951 int i;
1952
1953 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001954 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001955 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001956 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
1960 }
1961
1962 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001963 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001964 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001965 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001966 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001967 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001968 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001971 if (!pol) {
1972 struct flow_cache_object *flo;
1973
1974 flo = flow_cache_lookup(net, &fl, family, fl_dir,
1975 xfrm_policy_lookup, NULL);
1976 if (IS_ERR_OR_NULL(flo))
1977 pol = ERR_CAST(flo);
1978 else
1979 pol = container_of(flo, struct xfrm_policy, flo);
1980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001982 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001983 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05001984 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001985 }
James Morris134b0fc2006-10-05 15:42:27 -05001986
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001987 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001988 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001989 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001990 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001991 return 0;
1992 }
1993 return 1;
1994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
James Morris9d729f72007-03-04 16:12:44 -08001996 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001998 pols[0] = pol;
1999 npols ++;
2000#ifdef CONFIG_XFRM_SUB_POLICY
2001 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002002 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002003 &fl, family,
2004 XFRM_POLICY_IN);
2005 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002006 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002007 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002008 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002009 }
James Morris9d729f72007-03-04 16:12:44 -08002010 pols[1]->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002011 npols ++;
2012 }
2013 }
2014#endif
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (pol->action == XFRM_POLICY_ALLOW) {
2017 struct sec_path *sp;
2018 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002019 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002020 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002021 struct xfrm_tmpl **tpp = tp;
2022 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 int i, k;
2024
2025 if ((sp = skb->sp) == NULL)
2026 sp = &dummy;
2027
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002028 for (pi = 0; pi < npols; pi++) {
2029 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002030 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002031 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002032 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002033 }
2034 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002035 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002036 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002037 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002038 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2039 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2040 }
2041 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002042 if (npols > 1) {
2043 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
2044 tpp = stp;
2045 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 /* For each tunnel xfrm, find the first matching tmpl.
2048 * For each tmpl before that, find corresponding xfrm.
2049 * Order is _important_. Later we will implement
2050 * some barriers, but at the moment barriers
2051 * are implied between each two transformations.
2052 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002053 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2054 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002055 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002056 if (k < -1)
2057 /* "-2 - errored_index" returned */
2058 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002059 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
2063
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002064 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002065 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002069 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return 1;
2071 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002072 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002075 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002076reject_error:
2077 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return 0;
2079}
2080EXPORT_SYMBOL(__xfrm_policy_check);
2081
2082int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2083{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002084 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002086 struct dst_entry *dst;
2087 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002089 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002090 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Eric Dumazetadf30902009-06-02 05:19:30 +00002094 dst = skb_dst(skb);
2095
2096 res = xfrm_lookup(net, &dst, &fl, NULL, 0) == 0;
2097 skb_dst_set(skb, dst);
2098 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100EXPORT_SYMBOL(__xfrm_route_forward);
2101
David S. Millerd49c73c2006-08-13 18:55:53 -07002102/* Optimize later using cookies and generation ids. */
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2105{
David S. Millerd49c73c2006-08-13 18:55:53 -07002106 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2107 * to "-1" to force all XFRM destinations to get validated by
2108 * dst_ops->check on every use. We do this because when a
2109 * normal route referenced by an XFRM dst is obsoleted we do
2110 * not go looking around for all parent referencing XFRM dsts
2111 * so that we can invalidate them. It is just too much work.
2112 * Instead we make the checks here on every use. For example:
2113 *
2114 * XFRM dst A --> IPv4 dst X
2115 *
2116 * X is the "xdst->route" of A (X is also the "dst->path" of A
2117 * in this example). If X is marked obsolete, "A" will not
2118 * notice. That's what we are validating here via the
2119 * stale_bundle() check.
2120 *
2121 * When a policy's bundle is pruned, we dst_free() the XFRM
2122 * dst which causes it's ->obsolete field to be set to a
2123 * positive non-zero integer. If an XFRM dst has been pruned
2124 * like this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002125 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002126 if (dst->obsolete < 0 && !stale_bundle(dst))
2127 return dst;
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 return NULL;
2130}
2131
2132static int stale_bundle(struct dst_entry *dst)
2133{
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05002134 return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
Herbert Xuaabc9762005-05-03 16:27:10 -07002137void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002140 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002141 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 dev_put(dev);
2143 }
2144}
Herbert Xuaabc9762005-05-03 16:27:10 -07002145EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147static void xfrm_link_failure(struct sk_buff *skb)
2148{
2149 /* Impossible. Such dst must be popped before reaches point of failure. */
2150 return;
2151}
2152
2153static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2154{
2155 if (dst) {
2156 if (dst->obsolete) {
2157 dst_release(dst);
2158 dst = NULL;
2159 }
2160 }
2161 return dst;
2162}
2163
David S. Miller2518c7c2006-08-24 04:45:07 -07002164static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
2165{
2166 struct dst_entry *dst, **dstp;
2167
2168 write_lock(&pol->lock);
2169 dstp = &pol->bundles;
2170 while ((dst=*dstp) != NULL) {
2171 if (func(dst)) {
2172 *dstp = dst->next;
2173 dst->next = *gc_list_p;
2174 *gc_list_p = dst;
2175 } else {
2176 dstp = &dst->next;
2177 }
2178 }
2179 write_unlock(&pol->lock);
2180}
2181
Alexey Dobriyan3dd0b492008-11-25 17:36:51 -08002182static void xfrm_prune_bundles(struct net *net, int (*func)(struct dst_entry *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
David S. Miller2518c7c2006-08-24 04:45:07 -07002184 struct dst_entry *gc_list = NULL;
2185 int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07002188 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2189 struct xfrm_policy *pol;
2190 struct hlist_node *entry;
2191 struct hlist_head *table;
2192 int i;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002193
David S. Miller2518c7c2006-08-24 04:45:07 -07002194 hlist_for_each_entry(pol, entry,
Alexey Dobriyan3dd0b492008-11-25 17:36:51 -08002195 &net->xfrm.policy_inexact[dir], bydst)
David S. Miller2518c7c2006-08-24 04:45:07 -07002196 prune_one_bundle(pol, func, &gc_list);
2197
Alexey Dobriyan3dd0b492008-11-25 17:36:51 -08002198 table = net->xfrm.policy_bydst[dir].table;
2199 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002200 hlist_for_each_entry(pol, entry, table + i, bydst)
2201 prune_one_bundle(pol, func, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
2203 }
2204 read_unlock_bh(&xfrm_policy_lock);
2205
2206 while (gc_list) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002207 struct dst_entry *dst = gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 gc_list = dst->next;
2209 dst_free(dst);
2210 }
2211}
2212
2213static int unused_bundle(struct dst_entry *dst)
2214{
2215 return !atomic_read(&dst->__refcnt);
2216}
2217
Alexey Dobriyanddcfd792008-11-25 17:37:23 -08002218static void __xfrm_garbage_collect(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Alexey Dobriyanddcfd792008-11-25 17:37:23 -08002220 xfrm_prune_bundles(net, unused_bundle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221}
2222
Alexey Dobriyan3dd0b492008-11-25 17:36:51 -08002223static int xfrm_flush_bundles(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Alexey Dobriyan3dd0b492008-11-25 17:36:51 -08002225 xfrm_prune_bundles(net, stale_bundle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 return 0;
2227}
2228
Herbert Xu25ee3282007-12-11 09:32:34 -08002229static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
2231 do {
2232 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2233 u32 pmtu, route_mtu_cached;
2234
2235 pmtu = dst_mtu(dst->child);
2236 xdst->child_mtu_cached = pmtu;
2237
2238 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2239
2240 route_mtu_cached = dst_mtu(xdst->route);
2241 xdst->route_mtu_cached = route_mtu_cached;
2242
2243 if (pmtu > route_mtu_cached)
2244 pmtu = route_mtu_cached;
2245
2246 dst->metrics[RTAX_MTU-1] = pmtu;
2247 } while ((dst = dst->next));
2248}
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250/* Check that the bundle accepts the flow and its components are
2251 * still valid.
2252 */
2253
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05002254int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
2255 struct flowi *fl, int family, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 struct dst_entry *dst = &first->u.dst;
2258 struct xfrm_dst *last;
2259 u32 mtu;
2260
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002261 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 (dst->dev && !netif_running(dst->dev)))
2263 return 0;
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07002264#ifdef CONFIG_XFRM_SUB_POLICY
2265 if (fl) {
2266 if (first->origin && !flow_cache_uli_match(first->origin, fl))
2267 return 0;
2268 if (first->partner &&
2269 !xfrm_selector_match(first->partner, fl, family))
2270 return 0;
2271 }
2272#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274 last = NULL;
2275
2276 do {
2277 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2278
2279 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
2280 return 0;
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06002281 if (fl && pol &&
2282 !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2285 return 0;
David S. Miller9d4a7062006-08-24 03:18:09 -07002286 if (xdst->genid != dst->xfrm->genid)
2287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Herbert Xu1bfcb102007-10-17 21:31:50 -07002289 if (strict && fl &&
Herbert Xu13996372007-10-17 21:35:51 -07002290 !(dst->xfrm->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07002291 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
2292 return 0;
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 mtu = dst_mtu(dst->child);
2295 if (xdst->child_mtu_cached != mtu) {
2296 last = xdst;
2297 xdst->child_mtu_cached = mtu;
2298 }
2299
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002300 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 return 0;
2302 mtu = dst_mtu(xdst->route);
2303 if (xdst->route_mtu_cached != mtu) {
2304 last = xdst;
2305 xdst->route_mtu_cached = mtu;
2306 }
2307
2308 dst = dst->child;
2309 } while (dst->xfrm);
2310
2311 if (likely(!last))
2312 return 1;
2313
2314 mtu = last->child_mtu_cached;
2315 for (;;) {
2316 dst = &last->u.dst;
2317
2318 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2319 if (mtu > last->route_mtu_cached)
2320 mtu = last->route_mtu_cached;
2321 dst->metrics[RTAX_MTU-1] = mtu;
2322
2323 if (last == first)
2324 break;
2325
Patrick McHardybd0bf072007-07-18 01:55:52 -07002326 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 last->child_mtu_cached = mtu;
2328 }
2329
2330 return 1;
2331}
2332
2333EXPORT_SYMBOL(xfrm_bundle_ok);
2334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2336{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002337 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 int err = 0;
2339 if (unlikely(afinfo == NULL))
2340 return -EINVAL;
2341 if (unlikely(afinfo->family >= NPROTO))
2342 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002343 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2345 err = -ENOBUFS;
2346 else {
2347 struct dst_ops *dst_ops = afinfo->dst_ops;
2348 if (likely(dst_ops->kmem_cachep == NULL))
2349 dst_ops->kmem_cachep = xfrm_dst_cache;
2350 if (likely(dst_ops->check == NULL))
2351 dst_ops->check = xfrm_dst_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 if (likely(dst_ops->negative_advice == NULL))
2353 dst_ops->negative_advice = xfrm_negative_advice;
2354 if (likely(dst_ops->link_failure == NULL))
2355 dst_ops->link_failure = xfrm_link_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 if (likely(afinfo->garbage_collect == NULL))
2357 afinfo->garbage_collect = __xfrm_garbage_collect;
2358 xfrm_policy_afinfo[afinfo->family] = afinfo;
2359 }
Ingo Molnare959d812006-04-28 15:32:29 -07002360 write_unlock_bh(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002361
2362 rtnl_lock();
2363 for_each_net(net) {
2364 struct dst_ops *xfrm_dst_ops;
2365
2366 switch (afinfo->family) {
2367 case AF_INET:
2368 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2369 break;
2370#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2371 case AF_INET6:
2372 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2373 break;
2374#endif
2375 default:
2376 BUG();
2377 }
2378 *xfrm_dst_ops = *afinfo->dst_ops;
2379 }
2380 rtnl_unlock();
2381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return err;
2383}
2384EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2385
2386int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2387{
2388 int err = 0;
2389 if (unlikely(afinfo == NULL))
2390 return -EINVAL;
2391 if (unlikely(afinfo->family >= NPROTO))
2392 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002393 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2395 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2396 err = -EINVAL;
2397 else {
2398 struct dst_ops *dst_ops = afinfo->dst_ops;
2399 xfrm_policy_afinfo[afinfo->family] = NULL;
2400 dst_ops->kmem_cachep = NULL;
2401 dst_ops->check = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 dst_ops->negative_advice = NULL;
2403 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 afinfo->garbage_collect = NULL;
2405 }
2406 }
Ingo Molnare959d812006-04-28 15:32:29 -07002407 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 return err;
2409}
2410EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2411
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002412static void __net_init xfrm_dst_ops_init(struct net *net)
2413{
2414 struct xfrm_policy_afinfo *afinfo;
2415
2416 read_lock_bh(&xfrm_policy_afinfo_lock);
2417 afinfo = xfrm_policy_afinfo[AF_INET];
2418 if (afinfo)
2419 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
2420#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2421 afinfo = xfrm_policy_afinfo[AF_INET6];
2422 if (afinfo)
2423 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2424#endif
2425 read_unlock_bh(&xfrm_policy_afinfo_lock);
2426}
2427
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
2429{
2430 struct xfrm_policy_afinfo *afinfo;
2431 if (unlikely(family >= NPROTO))
2432 return NULL;
2433 read_lock(&xfrm_policy_afinfo_lock);
2434 afinfo = xfrm_policy_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07002435 if (unlikely(!afinfo))
2436 read_unlock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return afinfo;
2438}
2439
2440static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2441{
Herbert Xu546be242006-05-27 23:03:58 -07002442 read_unlock(&xfrm_policy_afinfo_lock);
2443}
2444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2446{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002447 struct net_device *dev = ptr;
2448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 switch (event) {
2450 case NETDEV_DOWN:
Alexey Dobriyan3dd0b492008-11-25 17:36:51 -08002451 xfrm_flush_bundles(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 }
2453 return NOTIFY_DONE;
2454}
2455
2456static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002457 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458};
2459
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002460#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002461static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002462{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002463 int rv;
2464
Tejun Heo7d720c32010-02-16 15:20:26 +00002465 if (snmp_mib_init((void __percpu **)net->mib.xfrm_statistics,
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002466 sizeof(struct linux_xfrm_mib)) < 0)
2467 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002468 rv = xfrm_proc_init(net);
2469 if (rv < 0)
Tejun Heo7d720c32010-02-16 15:20:26 +00002470 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002471 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002472}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002473
2474static void xfrm_statistics_fini(struct net *net)
2475{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002476 xfrm_proc_fini(net);
Tejun Heo7d720c32010-02-16 15:20:26 +00002477 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002478}
2479#else
2480static int __net_init xfrm_statistics_init(struct net *net)
2481{
2482 return 0;
2483}
2484
2485static void xfrm_statistics_fini(struct net *net)
2486{
2487}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002488#endif
2489
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002490static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491{
David S. Miller2518c7c2006-08-24 04:45:07 -07002492 unsigned int hmask, sz;
2493 int dir;
2494
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002495 if (net_eq(net, &init_net))
2496 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002498 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002499 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
David S. Miller2518c7c2006-08-24 04:45:07 -07002501 hmask = 8 - 1;
2502 sz = (hmask+1) * sizeof(struct hlist_head);
2503
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002504 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2505 if (!net->xfrm.policy_byidx)
2506 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002507 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002508
2509 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2510 struct xfrm_policy_hash *htab;
2511
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002512 net->xfrm.policy_count[dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002513 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002514
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002515 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002516 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002517 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002518 goto out_bydst;
2519 htab->hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002520 }
2521
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002522 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002523 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002524 if (net_eq(net, &init_net))
2525 register_netdevice_notifier(&xfrm_dev_notifier);
2526 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002527
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002528out_bydst:
2529 for (dir--; dir >= 0; dir--) {
2530 struct xfrm_policy_hash *htab;
2531
2532 htab = &net->xfrm.policy_bydst[dir];
2533 xfrm_hash_free(htab->table, sz);
2534 }
2535 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002536out_byidx:
2537 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
2539
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002540static void xfrm_policy_fini(struct net *net)
2541{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002542 struct xfrm_audit audit_info;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002543 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002544 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002545
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002546 flush_work(&net->xfrm.policy_hash_work);
2547#ifdef CONFIG_XFRM_SUB_POLICY
2548 audit_info.loginuid = -1;
2549 audit_info.sessionid = -1;
2550 audit_info.secid = 0;
2551 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, &audit_info);
2552#endif
2553 audit_info.loginuid = -1;
2554 audit_info.sessionid = -1;
2555 audit_info.secid = 0;
2556 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
2557 flush_work(&xfrm_policy_gc_work);
2558
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002559 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002560
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002561 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002562 struct xfrm_policy_hash *htab;
2563
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002564 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002565
2566 htab = &net->xfrm.policy_bydst[dir];
2567 sz = (htab->hmask + 1);
2568 WARN_ON(!hlist_empty(htab->table));
2569 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002570 }
2571
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002572 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002573 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2574 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002575}
2576
2577static int __net_init xfrm_net_init(struct net *net)
2578{
2579 int rv;
2580
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002581 rv = xfrm_statistics_init(net);
2582 if (rv < 0)
2583 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002584 rv = xfrm_state_init(net);
2585 if (rv < 0)
2586 goto out_state;
2587 rv = xfrm_policy_init(net);
2588 if (rv < 0)
2589 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002590 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002591 rv = xfrm_sysctl_init(net);
2592 if (rv < 0)
2593 goto out_sysctl;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002594 return 0;
2595
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002596out_sysctl:
2597 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002598out_policy:
2599 xfrm_state_fini(net);
2600out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002601 xfrm_statistics_fini(net);
2602out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002603 return rv;
2604}
2605
2606static void __net_exit xfrm_net_exit(struct net *net)
2607{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002608 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002609 xfrm_policy_fini(net);
2610 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002611 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002612}
2613
2614static struct pernet_operations __net_initdata xfrm_net_ops = {
2615 .init = xfrm_net_init,
2616 .exit = xfrm_net_exit,
2617};
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619void __init xfrm_init(void)
2620{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002621 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 xfrm_input_init();
2623}
2624
Joy Lattenab5f5e82007-09-17 11:51:22 -07002625#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08002626static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2627 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002628{
Paul Moore875179f2007-12-01 23:27:18 +11002629 struct xfrm_sec_ctx *ctx = xp->security;
2630 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002631
Paul Moore875179f2007-12-01 23:27:18 +11002632 if (ctx)
2633 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2634 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2635
2636 switch(sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002637 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002638 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002639 if (sel->prefixlen_s != 32)
2640 audit_log_format(audit_buf, " src_prefixlen=%d",
2641 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002642 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002643 if (sel->prefixlen_d != 32)
2644 audit_log_format(audit_buf, " dst_prefixlen=%d",
2645 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002646 break;
2647 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002648 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002649 if (sel->prefixlen_s != 128)
2650 audit_log_format(audit_buf, " src_prefixlen=%d",
2651 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002652 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002653 if (sel->prefixlen_d != 128)
2654 audit_log_format(audit_buf, " dst_prefixlen=%d",
2655 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002656 break;
2657 }
2658}
2659
Paul Moore68277ac2007-12-20 20:49:33 -08002660void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
Eric Paris25323862008-04-18 10:09:25 -04002661 uid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002662{
2663 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002664
Paul Mooreafeb14b2007-12-21 14:58:11 -08002665 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002666 if (audit_buf == NULL)
2667 return;
Eric Paris25323862008-04-18 10:09:25 -04002668 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002669 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002670 xfrm_audit_common_policyinfo(xp, audit_buf);
2671 audit_log_end(audit_buf);
2672}
2673EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
2674
Paul Moore68277ac2007-12-20 20:49:33 -08002675void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Eric Paris25323862008-04-18 10:09:25 -04002676 uid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002677{
2678 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002679
Paul Mooreafeb14b2007-12-21 14:58:11 -08002680 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002681 if (audit_buf == NULL)
2682 return;
Eric Paris25323862008-04-18 10:09:25 -04002683 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002684 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002685 xfrm_audit_common_policyinfo(xp, audit_buf);
2686 audit_log_end(audit_buf);
2687}
2688EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
2689#endif
2690
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002691#ifdef CONFIG_XFRM_MIGRATE
2692static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
2693 struct xfrm_selector *sel_tgt)
2694{
2695 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
2696 if (sel_tgt->family == sel_cmp->family &&
2697 xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002698 sel_cmp->family) == 0 &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002699 xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
2700 sel_cmp->family) == 0 &&
2701 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
2702 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
2703 return 1;
2704 }
2705 } else {
2706 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
2707 return 1;
2708 }
2709 }
2710 return 0;
2711}
2712
2713static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
2714 u8 dir, u8 type)
2715{
2716 struct xfrm_policy *pol, *ret = NULL;
2717 struct hlist_node *entry;
2718 struct hlist_head *chain;
2719 u32 priority = ~0U;
2720
2721 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -08002722 chain = policy_hash_direct(&init_net, &sel->daddr, &sel->saddr, sel->family, dir);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002723 hlist_for_each_entry(pol, entry, chain, bydst) {
2724 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2725 pol->type == type) {
2726 ret = pol;
2727 priority = ret->priority;
2728 break;
2729 }
2730 }
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002731 chain = &init_net.xfrm.policy_inexact[dir];
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002732 hlist_for_each_entry(pol, entry, chain, bydst) {
2733 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2734 pol->type == type &&
2735 pol->priority < priority) {
2736 ret = pol;
2737 break;
2738 }
2739 }
2740
2741 if (ret)
2742 xfrm_pol_hold(ret);
2743
2744 read_unlock_bh(&xfrm_policy_lock);
2745
2746 return ret;
2747}
2748
2749static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
2750{
2751 int match = 0;
2752
2753 if (t->mode == m->mode && t->id.proto == m->proto &&
2754 (m->reqid == 0 || t->reqid == m->reqid)) {
2755 switch (t->mode) {
2756 case XFRM_MODE_TUNNEL:
2757 case XFRM_MODE_BEET:
2758 if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
2759 m->old_family) == 0 &&
2760 xfrm_addr_cmp(&t->saddr, &m->old_saddr,
2761 m->old_family) == 0) {
2762 match = 1;
2763 }
2764 break;
2765 case XFRM_MODE_TRANSPORT:
2766 /* in case of transport mode, template does not store
2767 any IP addresses, hence we just compare mode and
2768 protocol */
2769 match = 1;
2770 break;
2771 default:
2772 break;
2773 }
2774 }
2775 return match;
2776}
2777
2778/* update endpoint address(es) of template(s) */
2779static int xfrm_policy_migrate(struct xfrm_policy *pol,
2780 struct xfrm_migrate *m, int num_migrate)
2781{
2782 struct xfrm_migrate *mp;
2783 struct dst_entry *dst;
2784 int i, j, n = 0;
2785
2786 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07002787 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002788 /* target policy has been deleted */
2789 write_unlock_bh(&pol->lock);
2790 return -ENOENT;
2791 }
2792
2793 for (i = 0; i < pol->xfrm_nr; i++) {
2794 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
2795 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
2796 continue;
2797 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07002798 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
2799 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002800 continue;
2801 /* update endpoints */
2802 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
2803 sizeof(pol->xfrm_vec[i].id.daddr));
2804 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
2805 sizeof(pol->xfrm_vec[i].saddr));
2806 pol->xfrm_vec[i].encap_family = mp->new_family;
2807 /* flush bundles */
2808 while ((dst = pol->bundles) != NULL) {
2809 pol->bundles = dst->next;
2810 dst_free(dst);
2811 }
2812 }
2813 }
2814
2815 write_unlock_bh(&pol->lock);
2816
2817 if (!n)
2818 return -ENODATA;
2819
2820 return 0;
2821}
2822
2823static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
2824{
2825 int i, j;
2826
2827 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
2828 return -EINVAL;
2829
2830 for (i = 0; i < num_migrate; i++) {
2831 if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
2832 m[i].old_family) == 0) &&
2833 (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
2834 m[i].old_family) == 0))
2835 return -EINVAL;
2836 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
2837 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
2838 return -EINVAL;
2839
2840 /* check if there is any duplicated entry */
2841 for (j = i + 1; j < num_migrate; j++) {
2842 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
2843 sizeof(m[i].old_daddr)) &&
2844 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
2845 sizeof(m[i].old_saddr)) &&
2846 m[i].proto == m[j].proto &&
2847 m[i].mode == m[j].mode &&
2848 m[i].reqid == m[j].reqid &&
2849 m[i].old_family == m[j].old_family)
2850 return -EINVAL;
2851 }
2852 }
2853
2854 return 0;
2855}
2856
2857int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002858 struct xfrm_migrate *m, int num_migrate,
2859 struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002860{
2861 int i, err, nx_cur = 0, nx_new = 0;
2862 struct xfrm_policy *pol = NULL;
2863 struct xfrm_state *x, *xc;
2864 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
2865 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
2866 struct xfrm_migrate *mp;
2867
2868 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
2869 goto out;
2870
2871 /* Stage 1 - find policy */
2872 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
2873 err = -ENOENT;
2874 goto out;
2875 }
2876
2877 /* Stage 2 - find and update state(s) */
2878 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
2879 if ((x = xfrm_migrate_state_find(mp))) {
2880 x_cur[nx_cur] = x;
2881 nx_cur++;
2882 if ((xc = xfrm_state_migrate(x, mp))) {
2883 x_new[nx_new] = xc;
2884 nx_new++;
2885 } else {
2886 err = -ENODATA;
2887 goto restore_state;
2888 }
2889 }
2890 }
2891
2892 /* Stage 3 - update policy */
2893 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
2894 goto restore_state;
2895
2896 /* Stage 4 - delete old state(s) */
2897 if (nx_cur) {
2898 xfrm_states_put(x_cur, nx_cur);
2899 xfrm_states_delete(x_cur, nx_cur);
2900 }
2901
2902 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002903 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002904
2905 xfrm_pol_put(pol);
2906
2907 return 0;
2908out:
2909 return err;
2910
2911restore_state:
2912 if (pol)
2913 xfrm_pol_put(pol);
2914 if (nx_cur)
2915 xfrm_states_put(x_cur, nx_cur);
2916 if (nx_new)
2917 xfrm_states_delete(x_new, nx_new);
2918
2919 return err;
2920}
David S. Millere610e672007-02-08 13:29:15 -08002921EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002922#endif