blob: 5f9be976770e3694b6dce7b50aef09040e4da6c0 [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>
Eric Paris6ce74ec2012-02-16 15:08:39 -050029#include <net/flow.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <net/xfrm.h>
31#include <net/ip.h>
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -080032#ifdef CONFIG_XFRM_STATISTICS
33#include <net/snmp.h>
34#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
David S. Miller44e36b42006-08-24 04:50:50 -070036#include "xfrm_hash.h"
37
Steffen Klasserta0073fe2013-02-05 12:52:55 +010038#define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
39#define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
40#define XFRM_MAX_QUEUE_LEN 100
41
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080042DEFINE_MUTEX(xfrm_cfg_mutex);
43EXPORT_SYMBOL(xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Timo Teräs80c802f2010-04-07 00:30:05 +000045static DEFINE_SPINLOCK(xfrm_policy_sk_bundle_lock);
46static struct dst_entry *xfrm_policy_sk_bundles;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static DEFINE_RWLOCK(xfrm_policy_lock);
48
Priyanka Jain418a99a2012-08-12 21:22:29 +000049static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
50static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO]
51 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Christoph Lametere18b8902006-12-06 20:33:20 -080053static struct kmem_cache *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Herbert Xu25ee3282007-12-11 09:32:34 -080055static void xfrm_init_pmtu(struct dst_entry *dst);
Timo Teräs80c802f2010-04-07 00:30:05 +000056static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +000057static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Steffen Klasserta0073fe2013-02-05 12:52:55 +010058static void xfrm_policy_queue_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Wei Yongjun29fa0b302008-12-03 00:33:09 -080060static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
61 int dir);
62
David S. Millerbc9b35a2012-05-15 15:04:57 -040063static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050064__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080065{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050066 const struct flowi4 *fl4 = &fl->u.ip4;
67
Alexey Dobriyan26bff942011-11-22 06:46:02 +000068 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
69 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -050070 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
71 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
72 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
73 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080074}
75
David S. Millerbc9b35a2012-05-15 15:04:57 -040076static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050077__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080078{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050079 const struct flowi6 *fl6 = &fl->u.ip6;
80
81 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
82 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
83 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
84 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
85 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
86 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080087}
88
David S. Millerbc9b35a2012-05-15 15:04:57 -040089bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
90 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -080091{
92 switch (family) {
93 case AF_INET:
94 return __xfrm4_selector_match(sel, fl);
95 case AF_INET6:
96 return __xfrm6_selector_match(sel, fl);
97 }
David S. Millerbc9b35a2012-05-15 15:04:57 -040098 return false;
Andrew Morton77681022006-11-08 22:46:26 -080099}
100
Eric Dumazetef8531b2012-08-19 12:31:48 +0200101static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
102{
103 struct xfrm_policy_afinfo *afinfo;
104
105 if (unlikely(family >= NPROTO))
106 return NULL;
107 rcu_read_lock();
108 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
109 if (unlikely(!afinfo))
110 rcu_read_unlock();
111 return afinfo;
112}
113
114static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
115{
116 rcu_read_unlock();
117}
118
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800119static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos,
David S. Miller6418c4e2011-02-24 00:16:53 -0500120 const xfrm_address_t *saddr,
121 const xfrm_address_t *daddr,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900122 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800124 struct xfrm_policy_afinfo *afinfo;
125 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Herbert Xu25ee3282007-12-11 09:32:34 -0800127 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800129 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800131 dst = afinfo->dst_lookup(net, tos, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900134
135 return dst;
136}
137
138static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
139 xfrm_address_t *prev_saddr,
140 xfrm_address_t *prev_daddr,
141 int family)
142{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800143 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900144 xfrm_address_t *saddr = &x->props.saddr;
145 xfrm_address_t *daddr = &x->id.daddr;
146 struct dst_entry *dst;
147
148 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
149 saddr = x->coaddr;
150 daddr = prev_daddr;
151 }
152 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
153 saddr = prev_saddr;
154 daddr = x->coaddr;
155 }
156
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800157 dst = __xfrm_dst_lookup(net, tos, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900158
159 if (!IS_ERR(dst)) {
160 if (prev_saddr != saddr)
161 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
162 if (prev_daddr != daddr)
163 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
164 }
165
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800166 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static inline unsigned long make_jiffies(long secs)
170{
171 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
172 return MAX_SCHEDULE_TIMEOUT-1;
173 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900174 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177static void xfrm_policy_timer(unsigned long data)
178{
179 struct xfrm_policy *xp = (struct xfrm_policy*)data;
James Morris9d729f72007-03-04 16:12:44 -0800180 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 long next = LONG_MAX;
182 int warn = 0;
183 int dir;
184
185 read_lock(&xp->lock);
186
Timo Teräsea2dea92010-03-31 00:17:05 +0000187 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 goto out;
189
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700190 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 if (xp->lft.hard_add_expires_seconds) {
193 long tmo = xp->lft.hard_add_expires_seconds +
194 xp->curlft.add_time - now;
195 if (tmo <= 0)
196 goto expired;
197 if (tmo < next)
198 next = tmo;
199 }
200 if (xp->lft.hard_use_expires_seconds) {
201 long tmo = xp->lft.hard_use_expires_seconds +
202 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
203 if (tmo <= 0)
204 goto expired;
205 if (tmo < next)
206 next = tmo;
207 }
208 if (xp->lft.soft_add_expires_seconds) {
209 long tmo = xp->lft.soft_add_expires_seconds +
210 xp->curlft.add_time - now;
211 if (tmo <= 0) {
212 warn = 1;
213 tmo = XFRM_KM_TIMEOUT;
214 }
215 if (tmo < next)
216 next = tmo;
217 }
218 if (xp->lft.soft_use_expires_seconds) {
219 long tmo = xp->lft.soft_use_expires_seconds +
220 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
221 if (tmo <= 0) {
222 warn = 1;
223 tmo = XFRM_KM_TIMEOUT;
224 }
225 if (tmo < next)
226 next = tmo;
227 }
228
229 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800230 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (next != LONG_MAX &&
232 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
233 xfrm_pol_hold(xp);
234
235out:
236 read_unlock(&xp->lock);
237 xfrm_pol_put(xp);
238 return;
239
240expired:
241 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700242 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800243 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 xfrm_pol_put(xp);
245}
246
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000247static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
248{
249 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
250
251 if (unlikely(pol->walk.dead))
252 flo = NULL;
253 else
254 xfrm_pol_hold(pol);
255
256 return flo;
257}
258
259static int xfrm_policy_flo_check(struct flow_cache_object *flo)
260{
261 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
262
263 return !pol->walk.dead;
264}
265
266static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
267{
268 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
269}
270
271static const struct flow_cache_ops xfrm_policy_fc_ops = {
272 .get = xfrm_policy_flo_get,
273 .check = xfrm_policy_flo_check,
274 .delete = xfrm_policy_flo_delete,
275};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
278 * SPD calls.
279 */
280
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800281struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct xfrm_policy *policy;
284
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700285 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800288 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700289 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700290 INIT_HLIST_NODE(&policy->bydst);
291 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700293 atomic_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100294 skb_queue_head_init(&policy->polq.hold_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800295 setup_timer(&policy->timer, xfrm_policy_timer,
296 (unsigned long)policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100297 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
298 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000299 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301 return policy;
302}
303EXPORT_SYMBOL(xfrm_policy_alloc);
304
305/* Destroy xfrm_policy: descendant resources must be released to this moment. */
306
WANG Cong64c31b32008-01-07 22:34:29 -0800307void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Herbert Xu12a169e2008-10-01 07:03:24 -0700309 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Fan Du0659eea2013-08-01 18:08:36 +0800311 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 BUG();
313
Paul Moore03e1ad72008-04-12 19:07:52 -0700314 security_xfrm_policy_free(policy->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 kfree(policy);
316}
WANG Cong64c31b32008-01-07 22:34:29 -0800317EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100319static void xfrm_queue_purge(struct sk_buff_head *list)
320{
321 struct sk_buff *skb;
322
Steffen Klassert302a50b2013-08-28 08:47:14 +0200323 while ((skb = skb_dequeue(list)) != NULL)
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100324 kfree_skb(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/* Rule must be locked. Release descentant resources, announce
328 * entry dead. The rule must be unlinked from lists to the moment.
329 */
330
331static void xfrm_policy_kill(struct xfrm_policy *policy)
332{
Herbert Xu12a169e2008-10-01 07:03:24 -0700333 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Timo Teräs285ead12010-04-07 00:30:06 +0000335 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200337 if (del_timer(&policy->polq.hold_timer))
338 xfrm_pol_put(policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100339 xfrm_queue_purge(&policy->polq.hold_queue);
340
Timo Teräs285ead12010-04-07 00:30:06 +0000341 if (del_timer(&policy->timer))
342 xfrm_pol_put(policy);
343
344 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
David S. Miller2518c7c2006-08-24 04:45:07 -0700347static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
348
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800349static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700350{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800351 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700352}
353
David S. Miller5f803b52011-02-24 00:33:19 -0500354static struct hlist_head *policy_hash_bysel(struct net *net,
355 const struct xfrm_selector *sel,
356 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700357{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800358 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700359 unsigned int hash = __sel_hash(sel, family, hmask);
360
361 return (hash == hmask + 1 ?
Alexey Dobriyan11219942008-11-25 17:33:06 -0800362 &net->xfrm.policy_inexact[dir] :
363 net->xfrm.policy_bydst[dir].table + hash);
David S. Miller2518c7c2006-08-24 04:45:07 -0700364}
365
David S. Miller5f803b52011-02-24 00:33:19 -0500366static struct hlist_head *policy_hash_direct(struct net *net,
367 const xfrm_address_t *daddr,
368 const xfrm_address_t *saddr,
369 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700370{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800371 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700372 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
373
Alexey Dobriyan11219942008-11-25 17:33:06 -0800374 return net->xfrm.policy_bydst[dir].table + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700375}
376
David S. Miller2518c7c2006-08-24 04:45:07 -0700377static void xfrm_dst_hash_transfer(struct hlist_head *list,
378 struct hlist_head *ndsttable,
379 unsigned int nhashmask)
380{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800381 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700382 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800383 unsigned int h0 = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700384
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800385redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800386 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700387 unsigned int h;
388
389 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
390 pol->family, nhashmask);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800391 if (!entry0) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800392 hlist_del(&pol->bydst);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800393 hlist_add_head(&pol->bydst, ndsttable+h);
394 h0 = h;
395 } else {
396 if (h != h0)
397 continue;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800398 hlist_del(&pol->bydst);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800399 hlist_add_after(entry0, &pol->bydst);
400 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800401 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800402 }
403 if (!hlist_empty(list)) {
404 entry0 = NULL;
405 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700406 }
407}
408
409static void xfrm_idx_hash_transfer(struct hlist_head *list,
410 struct hlist_head *nidxtable,
411 unsigned int nhashmask)
412{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800413 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700414 struct xfrm_policy *pol;
415
Sasha Levinb67bfe02013-02-27 17:06:00 -0800416 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700417 unsigned int h;
418
419 h = __idx_hash(pol->index, nhashmask);
420 hlist_add_head(&pol->byidx, nidxtable+h);
421 }
422}
423
424static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
425{
426 return ((old_hmask + 1) << 1) - 1;
427}
428
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800429static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700430{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800431 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700432 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
433 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800434 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700435 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700436 int i;
437
438 if (!ndst)
439 return;
440
441 write_lock_bh(&xfrm_policy_lock);
442
443 for (i = hmask; i >= 0; i--)
444 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
445
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800446 net->xfrm.policy_bydst[dir].table = ndst;
447 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700448
449 write_unlock_bh(&xfrm_policy_lock);
450
David S. Miller44e36b42006-08-24 04:50:50 -0700451 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700452}
453
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800454static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700455{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800456 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700457 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
458 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800459 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700460 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700461 int i;
462
463 if (!nidx)
464 return;
465
466 write_lock_bh(&xfrm_policy_lock);
467
468 for (i = hmask; i >= 0; i--)
469 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
470
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800471 net->xfrm.policy_byidx = nidx;
472 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700473
474 write_unlock_bh(&xfrm_policy_lock);
475
David S. Miller44e36b42006-08-24 04:50:50 -0700476 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700477}
478
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800479static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700480{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800481 unsigned int cnt = net->xfrm.policy_count[dir];
482 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700483
484 if (total)
485 *total += cnt;
486
487 if ((hmask + 1) < xfrm_policy_hashmax &&
488 cnt > hmask)
489 return 1;
490
491 return 0;
492}
493
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800494static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700495{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800496 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700497
498 if ((hmask + 1) < xfrm_policy_hashmax &&
499 total > hmask)
500 return 1;
501
502 return 0;
503}
504
Alexey Dobriyane0710412010-01-23 13:37:10 +0000505void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700506{
507 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000508 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
509 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
510 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
511 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
512 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
513 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
514 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700515 si->spdhmcnt = xfrm_policy_hashmax;
516 read_unlock_bh(&xfrm_policy_lock);
517}
518EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700519
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700520static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800521static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700522{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800523 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700524 int dir, total;
525
526 mutex_lock(&hash_resize_mutex);
527
528 total = 0;
529 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800530 if (xfrm_bydst_should_resize(net, dir, &total))
531 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700532 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800533 if (xfrm_byidx_should_resize(net, total))
534 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700535
536 mutex_unlock(&hash_resize_mutex);
537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539/* Generate new index... KAME seems to generate them ordered by cost
540 * of an absolute inpredictability of ordering of rules. This will not pass. */
Alexey Dobriyan11219942008-11-25 17:33:06 -0800541static u32 xfrm_gen_index(struct net *net, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 static u32 idx_generator;
544
545 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700546 struct hlist_head *list;
547 struct xfrm_policy *p;
548 u32 idx;
549 int found;
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 idx = (idx_generator | dir);
552 idx_generator += 8;
553 if (idx == 0)
554 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800555 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700556 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800557 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700558 if (p->index == idx) {
559 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700563 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return idx;
565 }
566}
567
David S. Miller2518c7c2006-08-24 04:45:07 -0700568static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
569{
570 u32 *p1 = (u32 *) s1;
571 u32 *p2 = (u32 *) s2;
572 int len = sizeof(struct xfrm_selector) / sizeof(u32);
573 int i;
574
575 for (i = 0; i < len; i++) {
576 if (p1[i] != p2[i])
577 return 1;
578 }
579
580 return 0;
581}
582
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100583static void xfrm_policy_requeue(struct xfrm_policy *old,
584 struct xfrm_policy *new)
585{
586 struct xfrm_policy_queue *pq = &old->polq;
587 struct sk_buff_head list;
588
589 __skb_queue_head_init(&list);
590
591 spin_lock_bh(&pq->hold_queue.lock);
592 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200593 if (del_timer(&pq->hold_timer))
594 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100595 spin_unlock_bh(&pq->hold_queue.lock);
596
597 if (skb_queue_empty(&list))
598 return;
599
600 pq = &new->polq;
601
602 spin_lock_bh(&pq->hold_queue.lock);
603 skb_queue_splice(&list, &pq->hold_queue);
604 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200605 if (!mod_timer(&pq->hold_timer, jiffies))
606 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100607 spin_unlock_bh(&pq->hold_queue.lock);
608}
609
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100610static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
611 struct xfrm_policy *pol)
612{
613 u32 mark = policy->mark.v & policy->mark.m;
614
615 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
616 return true;
617
618 if ((mark & pol->mark.m) == pol->mark.v &&
619 policy->priority == pol->priority)
620 return true;
621
622 return false;
623}
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
626{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800627 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700628 struct xfrm_policy *pol;
629 struct xfrm_policy *delpol;
630 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800631 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800634 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700635 delpol = NULL;
636 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800637 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800638 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700639 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100640 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800641 xfrm_sec_ctx_match(pol->security, policy->security) &&
642 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (excl) {
644 write_unlock_bh(&xfrm_policy_lock);
645 return -EEXIST;
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 delpol = pol;
648 if (policy->priority > pol->priority)
649 continue;
650 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800651 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 continue;
653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (delpol)
655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700658 hlist_add_after(newpos, &policy->bydst);
659 else
660 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800662 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 atomic_inc(&flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800664
665 /* After previous checking, family can either be AF_INET or AF_INET6 */
666 if (policy->family == AF_INET)
667 rt_genid_bump_ipv4(net);
668 else
669 rt_genid_bump_ipv6(net);
670
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100671 if (delpol) {
672 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800673 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100674 }
Alexey Dobriyan11219942008-11-25 17:33:06 -0800675 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir);
676 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800677 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 policy->curlft.use_time = 0;
679 if (!mod_timer(&policy->timer, jiffies + HZ))
680 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800681 list_add(&policy->walk.all, &net->xfrm.policy_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 write_unlock_bh(&xfrm_policy_lock);
683
David S. Miller9b78a822005-12-22 07:39:48 -0800684 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800686 else if (xfrm_bydst_should_resize(net, dir, NULL))
687 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 0;
690}
691EXPORT_SYMBOL(xfrm_policy_insert);
692
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000693struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
694 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800695 struct xfrm_sec_ctx *ctx, int delete,
696 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
David S. Miller2518c7c2006-08-24 04:45:07 -0700698 struct xfrm_policy *pol, *ret;
699 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Eric Parisef41aaa2007-03-07 15:37:58 -0800701 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800703 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700704 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800705 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700706 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000707 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700708 !selector_cmp(sel, &pol->selector) &&
709 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700711 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700712 *err = security_xfrm_policy_delete(
713 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800714 if (*err) {
715 write_unlock_bh(&xfrm_policy_lock);
716 return pol;
717 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800718 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700719 }
720 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 break;
722 }
723 }
724 write_unlock_bh(&xfrm_policy_lock);
725
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000726 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700727 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700728 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
Trent Jaegerdf718372005-12-13 23:12:27 -0800730EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000732struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
733 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
David S. Miller2518c7c2006-08-24 04:45:07 -0700735 struct xfrm_policy *pol, *ret;
736 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Herbert Xub5505c62007-05-14 02:15:47 -0700738 *err = -ENOENT;
739 if (xfrm_policy_id2dir(id) != dir)
740 return NULL;
741
Eric Parisef41aaa2007-03-07 15:37:58 -0800742 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800744 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700745 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800746 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000747 if (pol->type == type && pol->index == id &&
748 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700750 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700751 *err = security_xfrm_policy_delete(
752 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800753 if (*err) {
754 write_unlock_bh(&xfrm_policy_lock);
755 return pol;
756 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800757 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700758 }
759 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 break;
761 }
762 }
763 write_unlock_bh(&xfrm_policy_lock);
764
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000765 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700766 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700767 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769EXPORT_SYMBOL(xfrm_policy_byid);
770
Joy Latten4aa2e622007-06-04 19:05:57 -0400771#ifdef CONFIG_SECURITY_NETWORK_XFRM
772static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800773xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Joy Latten4aa2e622007-06-04 19:05:57 -0400775 int dir, err = 0;
776
777 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
778 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400779 int i;
780
Sasha Levinb67bfe02013-02-27 17:06:00 -0800781 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800782 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400783 if (pol->type != type)
784 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700785 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400786 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700787 xfrm_audit_policy_delete(pol, 0,
788 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400789 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700790 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400791 return err;
792 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900793 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800794 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800795 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800796 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400797 bydst) {
798 if (pol->type != type)
799 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700800 err = security_xfrm_policy_delete(
801 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400802 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700803 xfrm_audit_policy_delete(pol, 0,
804 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400805 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700806 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400807 return err;
808 }
809 }
810 }
811 }
812 return err;
813}
814#else
815static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800816xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400817{
818 return 0;
819}
820#endif
821
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800822int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400823{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000824 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 write_lock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400827
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800828 err = xfrm_policy_flush_secctx_check(net, type, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400829 if (err)
830 goto out;
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700833 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800834 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700835
836 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800837 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800838 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700839 if (pol->type != type)
840 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000841 __xfrm_policy_unlink(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000843 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Joy Lattenab5f5e82007-09-17 11:51:22 -0700845 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400846 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700847 audit_info->secid);
Joy Latten161a09e2006-11-27 13:11:54 -0600848
David S. Miller2518c7c2006-08-24 04:45:07 -0700849 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700852 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700854
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800855 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700856 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800857 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800858 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700859 bydst) {
860 if (pol->type != type)
861 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000862 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700863 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000864 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700865
Joy Lattenab5f5e82007-09-17 11:51:22 -0700866 xfrm_audit_policy_delete(pol, 1,
867 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400868 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700869 audit_info->secid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700870 xfrm_policy_kill(pol);
871
872 write_lock_bh(&xfrm_policy_lock);
873 goto again2;
874 }
875 }
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000878 if (!cnt)
879 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400880out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 write_unlock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400882 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884EXPORT_SYMBOL(xfrm_policy_flush);
885
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800886int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800887 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 void *data)
889{
Herbert Xu12a169e2008-10-01 07:03:24 -0700890 struct xfrm_policy *pol;
891 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -0800892 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Timo Teras4c563f72008-02-28 21:31:08 -0800894 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
895 walk->type != XFRM_POLICY_TYPE_ANY)
896 return -EINVAL;
897
Herbert Xu12a169e2008-10-01 07:03:24 -0700898 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -0800899 return 0;
900
Herbert Xu12a169e2008-10-01 07:03:24 -0700901 write_lock_bh(&xfrm_policy_lock);
902 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800903 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -0700904 else
905 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800906 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -0700907 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -0800908 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -0700909 pol = container_of(x, struct xfrm_policy, walk);
910 if (walk->type != XFRM_POLICY_TYPE_ANY &&
911 walk->type != pol->type)
912 continue;
913 error = func(pol, xfrm_policy_id2dir(pol->index),
914 walk->seq, data);
915 if (error) {
916 list_move_tail(&walk->walk.all, &x->all);
917 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -0800918 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700919 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700921 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800922 error = -ENOENT;
923 goto out;
924 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700925 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926out:
Herbert Xu12a169e2008-10-01 07:03:24 -0700927 write_unlock_bh(&xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return error;
929}
930EXPORT_SYMBOL(xfrm_policy_walk);
931
Herbert Xu12a169e2008-10-01 07:03:24 -0700932void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
933{
934 INIT_LIST_HEAD(&walk->walk.all);
935 walk->walk.dead = 1;
936 walk->type = type;
937 walk->seq = 0;
938}
939EXPORT_SYMBOL(xfrm_policy_walk_init);
940
941void xfrm_policy_walk_done(struct xfrm_policy_walk *walk)
942{
943 if (list_empty(&walk->walk.all))
944 return;
945
946 write_lock_bh(&xfrm_policy_lock);
947 list_del(&walk->walk.all);
948 write_unlock_bh(&xfrm_policy_lock);
949}
950EXPORT_SYMBOL(xfrm_policy_walk_done);
951
James Morris134b0fc2006-10-05 15:42:27 -0500952/*
953 * Find policy to apply to this flow.
954 *
955 * Returns 0 if policy found, else an -errno.
956 */
David S. Millerf299d552011-02-24 01:23:30 -0500957static int xfrm_policy_match(const struct xfrm_policy *pol,
958 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -0700959 u8 type, u16 family, int dir)
960{
David S. Millerf299d552011-02-24 01:23:30 -0500961 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -0400962 int ret = -ESRCH;
963 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -0700964
965 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -0500966 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -0700967 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500968 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700969
970 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500971 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -0500972 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -0700973 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700974
James Morris134b0fc2006-10-05 15:42:27 -0500975 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700976}
977
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800978static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -0800979 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700980 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
James Morris134b0fc2006-10-05 15:42:27 -0500982 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700983 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -0500984 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -0700985 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700986 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700987
988 daddr = xfrm_flowi_daddr(fl, family);
989 saddr = xfrm_flowi_saddr(fl, family);
990 if (unlikely(!daddr || !saddr))
991 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800994 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700995 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800996 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500997 err = xfrm_policy_match(pol, fl, type, family, dir);
998 if (err) {
999 if (err == -ESRCH)
1000 continue;
1001 else {
1002 ret = ERR_PTR(err);
1003 goto fail;
1004 }
1005 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001006 ret = pol;
1007 priority = ret->priority;
1008 break;
1009 }
1010 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001011 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001012 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001013 err = xfrm_policy_match(pol, fl, type, family, dir);
1014 if (err) {
1015 if (err == -ESRCH)
1016 continue;
1017 else {
1018 ret = ERR_PTR(err);
1019 goto fail;
1020 }
1021 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001022 ret = pol;
1023 break;
1024 }
1025 }
David S. Milleracba48e2006-08-25 15:46:46 -07001026 if (ret)
1027 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -05001028fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001030
David S. Miller2518c7c2006-08-24 04:45:07 -07001031 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001032}
1033
Timo Teräs80c802f2010-04-07 00:30:05 +00001034static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001035__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001036{
1037#ifdef CONFIG_XFRM_SUB_POLICY
1038 struct xfrm_policy *pol;
1039
1040 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1041 if (pol != NULL)
1042 return pol;
1043#endif
1044 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1045}
1046
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001047static int flow_to_policy_dir(int dir)
1048{
1049 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1050 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1051 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1052 return dir;
1053
1054 switch (dir) {
1055 default:
1056 case FLOW_DIR_IN:
1057 return XFRM_POLICY_IN;
1058 case FLOW_DIR_OUT:
1059 return XFRM_POLICY_OUT;
1060 case FLOW_DIR_FWD:
1061 return XFRM_POLICY_FWD;
1062 }
1063}
1064
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001065static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001066xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001067 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001068{
1069 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001070
1071 if (old_obj)
1072 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001073
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001074 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001075 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001076 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001077
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001078 /* Resolver returns two references:
1079 * one for cache and one for caller of flow_cache_lookup() */
1080 xfrm_pol_hold(pol);
1081
1082 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Trent Jaegerdf718372005-12-13 23:12:27 -08001085static inline int policy_to_flow_dir(int dir)
1086{
1087 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001088 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1089 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1090 return dir;
1091 switch (dir) {
1092 default:
1093 case XFRM_POLICY_IN:
1094 return FLOW_DIR_IN;
1095 case XFRM_POLICY_OUT:
1096 return FLOW_DIR_OUT;
1097 case XFRM_POLICY_FWD:
1098 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001099 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001100}
1101
David S. Millerdee9f4b2011-02-22 18:44:31 -08001102static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir,
1103 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 struct xfrm_policy *pol;
1106
1107 read_lock_bh(&xfrm_policy_lock);
1108 if ((pol = sk->sk_policy[dir]) != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001109 bool match = xfrm_selector_match(&pol->selector, fl,
1110 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001111 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001112
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001113 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001114 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1115 pol = NULL;
1116 goto out;
1117 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001118 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001119 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001120 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001121 if (!err)
1122 xfrm_pol_hold(pol);
1123 else if (err == -ESRCH)
1124 pol = NULL;
1125 else
1126 pol = ERR_PTR(err);
1127 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 pol = NULL;
1129 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001130out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 read_unlock_bh(&xfrm_policy_lock);
1132 return pol;
1133}
1134
1135static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1136{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001137 struct net *net = xp_net(pol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001138 struct hlist_head *chain = policy_hash_bysel(net, &pol->selector,
David S. Miller2518c7c2006-08-24 04:45:07 -07001139 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001140
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001141 list_add(&pol->walk.all, &net->xfrm.policy_all);
David S. Miller2518c7c2006-08-24 04:45:07 -07001142 hlist_add_head(&pol->bydst, chain);
Alexey Dobriyane92303f2008-11-25 17:32:41 -08001143 hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001144 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001146
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001147 if (xfrm_bydst_should_resize(net, dir, NULL))
1148 schedule_work(&net->xfrm.policy_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
1151static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1152 int dir)
1153{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001154 struct net *net = xp_net(pol);
1155
David S. Miller2518c7c2006-08-24 04:45:07 -07001156 if (hlist_unhashed(&pol->bydst))
1157 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
David S. Miller2518c7c2006-08-24 04:45:07 -07001159 hlist_del(&pol->bydst);
1160 hlist_del(&pol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -07001161 list_del(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001162 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001163
1164 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
Herbert Xu4666faa2005-06-18 22:43:22 -07001167int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 write_lock_bh(&xfrm_policy_lock);
1170 pol = __xfrm_policy_unlink(pol, dir);
1171 write_unlock_bh(&xfrm_policy_lock);
1172 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001176 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
David S. Millera70fcb02006-03-20 19:18:52 -08001178EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1181{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001182 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 struct xfrm_policy *old_pol;
1184
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001185#ifdef CONFIG_XFRM_SUB_POLICY
1186 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1187 return -EINVAL;
1188#endif
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 write_lock_bh(&xfrm_policy_lock);
1191 old_pol = sk->sk_policy[dir];
1192 sk->sk_policy[dir] = pol;
1193 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001194 pol->curlft.add_time = get_seconds();
Alexey Dobriyan11219942008-11-25 17:33:06 -08001195 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1197 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001198 if (old_pol) {
1199 if (pol)
1200 xfrm_policy_requeue(old_pol, pol);
1201
Timo Teräsea2dea92010-03-31 00:17:05 +00001202 /* Unlinking succeeds always. This is the only function
1203 * allowed to delete or replace socket policy.
1204 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 write_unlock_bh(&xfrm_policy_lock);
1208
1209 if (old_pol) {
1210 xfrm_policy_kill(old_pol);
1211 }
1212 return 0;
1213}
1214
David S. Millerd3e40a92011-02-24 01:25:41 -05001215static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001217 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 if (newp) {
1220 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001221 if (security_xfrm_policy_clone(old->security,
1222 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001223 kfree(newp);
1224 return NULL; /* ENOMEM */
1225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 newp->lft = old->lft;
1227 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001228 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 newp->action = old->action;
1230 newp->flags = old->flags;
1231 newp->xfrm_nr = old->xfrm_nr;
1232 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001233 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 memcpy(newp->xfrm_vec, old->xfrm_vec,
1235 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1236 write_lock_bh(&xfrm_policy_lock);
1237 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1238 write_unlock_bh(&xfrm_policy_lock);
1239 xfrm_pol_put(newp);
1240 }
1241 return newp;
1242}
1243
1244int __xfrm_sk_clone_policy(struct sock *sk)
1245{
1246 struct xfrm_policy *p0 = sk->sk_policy[0],
1247 *p1 = sk->sk_policy[1];
1248
1249 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1250 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1251 return -ENOMEM;
1252 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1253 return -ENOMEM;
1254 return 0;
1255}
1256
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001257static int
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001258xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001259 unsigned short family)
1260{
1261 int err;
1262 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1263
1264 if (unlikely(afinfo == NULL))
1265 return -EINVAL;
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001266 err = afinfo->get_saddr(net, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001267 xfrm_policy_put_afinfo(afinfo);
1268 return err;
1269}
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271/* Resolve list of templates for the flow, given policy. */
1272
1273static int
David S. Millera6c2e612011-02-22 18:35:39 -08001274xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1275 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001277 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 int nx;
1279 int i, error;
1280 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1281 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001282 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1285 struct xfrm_state *x;
1286 xfrm_address_t *remote = daddr;
1287 xfrm_address_t *local = saddr;
1288 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1289
Joakim Koskela48b8d782007-07-26 00:08:42 -07001290 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1291 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 remote = &tmpl->id.daddr;
1293 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001294 if (xfrm_addr_any(local, tmpl->encap_family)) {
1295 error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001296 if (error)
1297 goto fail;
1298 local = &tmp;
1299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301
1302 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1303
1304 if (x && x->km.state == XFRM_STATE_VALID) {
1305 xfrm[nx++] = x;
1306 daddr = remote;
1307 saddr = local;
1308 continue;
1309 }
1310 if (x) {
1311 error = (x->km.state == XFRM_STATE_ERROR ?
1312 -EINVAL : -EAGAIN);
1313 xfrm_state_put(x);
1314 }
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001315 else if (error == -ESRCH)
1316 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 if (!tmpl->optional)
1319 goto fail;
1320 }
1321 return nx;
1322
1323fail:
1324 for (nx--; nx>=0; nx--)
1325 xfrm_state_put(xfrm[nx]);
1326 return error;
1327}
1328
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001329static int
David S. Millera6c2e612011-02-22 18:35:39 -08001330xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1331 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001332{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001333 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1334 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001335 int cnx = 0;
1336 int error;
1337 int ret;
1338 int i;
1339
1340 for (i = 0; i < npols; i++) {
1341 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1342 error = -ENOBUFS;
1343 goto fail;
1344 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001345
1346 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001347 if (ret < 0) {
1348 error = ret;
1349 goto fail;
1350 } else
1351 cnx += ret;
1352 }
1353
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001354 /* found states are sorted for outbound processing */
1355 if (npols > 1)
1356 xfrm_state_sort(xfrm, tpp, cnx, family);
1357
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001358 return cnx;
1359
1360 fail:
1361 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001362 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001363 return error;
1364
1365}
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367/* Check that the bundle accepts the flow and its components are
1368 * still valid.
1369 */
1370
David S. Miller05d84022011-02-22 17:47:10 -08001371static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001372{
1373 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1374 int tos;
1375
1376 if (!afinfo)
1377 return -EINVAL;
1378
1379 tos = afinfo->get_tos(fl);
1380
1381 xfrm_policy_put_afinfo(afinfo);
1382
1383 return tos;
1384}
1385
Timo Teräs80c802f2010-04-07 00:30:05 +00001386static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1387{
1388 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1389 struct dst_entry *dst = &xdst->u.dst;
1390
1391 if (xdst->route == NULL) {
1392 /* Dummy bundle - if it has xfrms we were not
1393 * able to build bundle as template resolution failed.
1394 * It means we need to try again resolving. */
1395 if (xdst->num_xfrms > 0)
1396 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001397 } else if (dst->flags & DST_XFRM_QUEUE) {
1398 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001399 } else {
1400 /* Real bundle */
1401 if (stale_bundle(dst))
1402 return NULL;
1403 }
1404
1405 dst_hold(dst);
1406 return flo;
1407}
1408
1409static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1410{
1411 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1412 struct dst_entry *dst = &xdst->u.dst;
1413
1414 if (!xdst->route)
1415 return 0;
1416 if (stale_bundle(dst))
1417 return 0;
1418
1419 return 1;
1420}
1421
1422static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1423{
1424 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1425 struct dst_entry *dst = &xdst->u.dst;
1426
1427 dst_free(dst);
1428}
1429
1430static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1431 .get = xfrm_bundle_flo_get,
1432 .check = xfrm_bundle_flo_check,
1433 .delete = xfrm_bundle_flo_delete,
1434};
1435
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001436static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001437{
1438 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001439 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001440 struct xfrm_dst *xdst;
1441
1442 if (!afinfo)
1443 return ERR_PTR(-EINVAL);
1444
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001445 switch (family) {
1446 case AF_INET:
1447 dst_ops = &net->xfrm.xfrm4_dst_ops;
1448 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001449#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001450 case AF_INET6:
1451 dst_ops = &net->xfrm.xfrm6_dst_ops;
1452 break;
1453#endif
1454 default:
1455 BUG();
1456 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001457 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001458
Madalin Bucurd4cae562011-09-26 07:04:36 +00001459 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001460 struct dst_entry *dst = &xdst->u.dst;
1461
1462 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001463 xdst->flo.ops = &xfrm_bundle_fc_ops;
Patrick McHardy9d7b0fc2012-08-20 02:56:56 -07001464 if (afinfo->init_dst)
1465 afinfo->init_dst(net, xdst);
Madalin Bucurd4cae562011-09-26 07:04:36 +00001466 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001467 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001468
Madalin Bucurd4cae562011-09-26 07:04:36 +00001469 xfrm_policy_put_afinfo(afinfo);
1470
Herbert Xu25ee3282007-12-11 09:32:34 -08001471 return xdst;
1472}
1473
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001474static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1475 int nfheader_len)
1476{
1477 struct xfrm_policy_afinfo *afinfo =
1478 xfrm_policy_get_afinfo(dst->ops->family);
1479 int err;
1480
1481 if (!afinfo)
1482 return -EINVAL;
1483
1484 err = afinfo->init_path(path, dst, nfheader_len);
1485
1486 xfrm_policy_put_afinfo(afinfo);
1487
1488 return err;
1489}
1490
Herbert Xu87c1e122010-03-02 02:51:56 +00001491static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001492 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001493{
1494 struct xfrm_policy_afinfo *afinfo =
1495 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1496 int err;
1497
1498 if (!afinfo)
1499 return -EINVAL;
1500
Herbert Xu87c1e122010-03-02 02:51:56 +00001501 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001502
1503 xfrm_policy_put_afinfo(afinfo);
1504
1505 return err;
1506}
1507
Timo Teräs80c802f2010-04-07 00:30:05 +00001508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1510 * all the metrics... Shortly, bundle a bundle.
1511 */
1512
Herbert Xu25ee3282007-12-11 09:32:34 -08001513static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1514 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001515 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001516 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001518 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001519 unsigned long now = jiffies;
1520 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001521 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001522 struct dst_entry *dst_prev = NULL;
1523 struct dst_entry *dst0 = NULL;
1524 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001526 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001527 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001528 int trailer_len = 0;
1529 int tos;
1530 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001531 xfrm_address_t saddr, daddr;
1532
1533 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001534
1535 tos = xfrm_get_tos(fl, family);
1536 err = tos;
1537 if (tos < 0)
1538 goto put_states;
1539
1540 dst_hold(dst);
1541
1542 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001543 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001544 struct dst_entry *dst1 = &xdst->u.dst;
1545
1546 err = PTR_ERR(xdst);
1547 if (IS_ERR(xdst)) {
1548 dst_release(dst);
1549 goto put_states;
1550 }
1551
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001552 if (xfrm[i]->sel.family == AF_UNSPEC) {
1553 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1554 xfrm_af2proto(family));
1555 if (!inner_mode) {
1556 err = -EAFNOSUPPORT;
1557 dst_release(dst);
1558 goto put_states;
1559 }
1560 } else
1561 inner_mode = xfrm[i]->inner_mode;
1562
Herbert Xu25ee3282007-12-11 09:32:34 -08001563 if (!dst_prev)
1564 dst0 = dst1;
1565 else {
1566 dst_prev->child = dst_clone(dst1);
1567 dst1->flags |= DST_NOHASH;
1568 }
1569
1570 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001571 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001572
1573 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1574 family = xfrm[i]->props.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001575 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1576 family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001577 err = PTR_ERR(dst);
1578 if (IS_ERR(dst))
1579 goto put_states;
1580 } else
1581 dst_hold(dst);
1582
1583 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001584 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001585
David S. Millerf5b0a872012-07-19 12:31:33 -07001586 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001587 dst1->flags |= DST_HOST;
1588 dst1->lastuse = now;
1589
1590 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001591 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001592
1593 dst1->next = dst_prev;
1594 dst_prev = dst1;
1595
1596 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001597 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1598 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001599 trailer_len += xfrm[i]->props.trailer_len;
1600 }
1601
1602 dst_prev->child = dst;
1603 dst0->path = dst;
1604
1605 err = -ENODEV;
1606 dev = dst->dev;
1607 if (!dev)
1608 goto free_dst;
1609
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001610 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001611 xfrm_init_pmtu(dst_prev);
1612
1613 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1614 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1615
Herbert Xu87c1e122010-03-02 02:51:56 +00001616 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001617 if (err)
1618 goto free_dst;
1619
1620 dst_prev->header_len = header_len;
1621 dst_prev->trailer_len = trailer_len;
1622 header_len -= xdst->u.dst.xfrm->props.header_len;
1623 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1624 }
1625
1626out:
1627 return dst0;
1628
1629put_states:
1630 for (; i < nx; i++)
1631 xfrm_state_put(xfrm[i]);
1632free_dst:
1633 if (dst0)
1634 dst_free(dst0);
1635 dst0 = ERR_PTR(err);
1636 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001639static int inline
David S. Miller3f0e18f2011-02-22 18:38:14 -08001640xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001641{
1642 if (!*target) {
1643 *target = kmalloc(size, GFP_ATOMIC);
1644 if (!*target)
1645 return -ENOMEM;
1646 }
1647 memcpy(*target, src, size);
1648 return 0;
1649}
1650
1651static int inline
David S. Miller1786b382011-02-24 01:32:54 -05001652xfrm_dst_update_parent(struct dst_entry *dst, const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001653{
1654#ifdef CONFIG_XFRM_SUB_POLICY
1655 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1656 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1657 sel, sizeof(*sel));
1658#else
1659 return 0;
1660#endif
1661}
1662
1663static int inline
David S. Miller3f0e18f2011-02-22 18:38:14 -08001664xfrm_dst_update_origin(struct dst_entry *dst, const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001665{
1666#ifdef CONFIG_XFRM_SUB_POLICY
1667 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1668 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1669#else
1670 return 0;
1671#endif
1672}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
David S. Miller73ff93c2011-02-22 18:33:42 -08001674static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001675 struct xfrm_policy **pols,
1676 int *num_pols, int *num_xfrms)
1677{
1678 int i;
1679
1680 if (*num_pols == 0 || !pols[0]) {
1681 *num_pols = 0;
1682 *num_xfrms = 0;
1683 return 0;
1684 }
1685 if (IS_ERR(pols[0]))
1686 return PTR_ERR(pols[0]);
1687
1688 *num_xfrms = pols[0]->xfrm_nr;
1689
1690#ifdef CONFIG_XFRM_SUB_POLICY
1691 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1692 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1693 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1694 XFRM_POLICY_TYPE_MAIN,
1695 fl, family,
1696 XFRM_POLICY_OUT);
1697 if (pols[1]) {
1698 if (IS_ERR(pols[1])) {
1699 xfrm_pols_put(pols, *num_pols);
1700 return PTR_ERR(pols[1]);
1701 }
1702 (*num_pols) ++;
1703 (*num_xfrms) += pols[1]->xfrm_nr;
1704 }
1705 }
1706#endif
1707 for (i = 0; i < *num_pols; i++) {
1708 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1709 *num_xfrms = -1;
1710 break;
1711 }
1712 }
1713
1714 return 0;
1715
1716}
1717
1718static struct xfrm_dst *
1719xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001720 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001721 struct dst_entry *dst_orig)
1722{
1723 struct net *net = xp_net(pols[0]);
1724 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1725 struct dst_entry *dst;
1726 struct xfrm_dst *xdst;
1727 int err;
1728
1729 /* Try to instantiate a bundle */
1730 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001731 if (err <= 0) {
1732 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001733 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1734 return ERR_PTR(err);
1735 }
1736
1737 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1738 if (IS_ERR(dst)) {
1739 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1740 return ERR_CAST(dst);
1741 }
1742
1743 xdst = (struct xfrm_dst *)dst;
1744 xdst->num_xfrms = err;
1745 if (num_pols > 1)
1746 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1747 else
1748 err = xfrm_dst_update_origin(dst, fl);
1749 if (unlikely(err)) {
1750 dst_free(dst);
1751 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1752 return ERR_PTR(err);
1753 }
1754
1755 xdst->num_pols = num_pols;
1756 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
1757 xdst->policy_genid = atomic_read(&pols[0]->genid);
1758
1759 return xdst;
1760}
1761
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001762static void xfrm_policy_queue_process(unsigned long arg)
1763{
1764 int err = 0;
1765 struct sk_buff *skb;
1766 struct sock *sk;
1767 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001768 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1769 struct xfrm_policy_queue *pq = &pol->polq;
1770 struct flowi fl;
1771 struct sk_buff_head list;
1772
1773 spin_lock(&pq->hold_queue.lock);
1774 skb = skb_peek(&pq->hold_queue);
1775 dst = skb_dst(skb);
1776 sk = skb->sk;
1777 xfrm_decode_session(skb, &fl, dst->ops->family);
1778 spin_unlock(&pq->hold_queue.lock);
1779
1780 dst_hold(dst->path);
1781 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1782 sk, 0);
1783 if (IS_ERR(dst))
1784 goto purge_queue;
1785
1786 if (dst->flags & DST_XFRM_QUEUE) {
1787 dst_release(dst);
1788
1789 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1790 goto purge_queue;
1791
1792 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001793 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1794 xfrm_pol_hold(pol);
1795 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001796 }
1797
1798 dst_release(dst);
1799
1800 __skb_queue_head_init(&list);
1801
1802 spin_lock(&pq->hold_queue.lock);
1803 pq->timeout = 0;
1804 skb_queue_splice_init(&pq->hold_queue, &list);
1805 spin_unlock(&pq->hold_queue.lock);
1806
1807 while (!skb_queue_empty(&list)) {
1808 skb = __skb_dequeue(&list);
1809
1810 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1811 dst_hold(skb_dst(skb)->path);
1812 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1813 &fl, skb->sk, 0);
1814 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001815 kfree_skb(skb);
1816 continue;
1817 }
1818
1819 nf_reset(skb);
1820 skb_dst_drop(skb);
1821 skb_dst_set(skb, dst);
1822
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001823 err = dst_output(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001824 }
1825
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001826out:
1827 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001828 return;
1829
1830purge_queue:
1831 pq->timeout = 0;
1832 xfrm_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001833 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001834}
1835
1836static int xdst_queue_output(struct sk_buff *skb)
1837{
1838 unsigned long sched_next;
1839 struct dst_entry *dst = skb_dst(skb);
1840 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001841 struct xfrm_policy *pol = xdst->pols[0];
1842 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001843
1844 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1845 kfree_skb(skb);
1846 return -EAGAIN;
1847 }
1848
1849 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001850
1851 spin_lock_bh(&pq->hold_queue.lock);
1852
1853 if (!pq->timeout)
1854 pq->timeout = XFRM_QUEUE_TMO_MIN;
1855
1856 sched_next = jiffies + pq->timeout;
1857
1858 if (del_timer(&pq->hold_timer)) {
1859 if (time_before(pq->hold_timer.expires, sched_next))
1860 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001861 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001862 }
1863
1864 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001865 if (!mod_timer(&pq->hold_timer, sched_next))
1866 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001867
1868 spin_unlock_bh(&pq->hold_queue.lock);
1869
1870 return 0;
1871}
1872
1873static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
1874 struct dst_entry *dst,
1875 const struct flowi *fl,
1876 int num_xfrms,
1877 u16 family)
1878{
1879 int err;
1880 struct net_device *dev;
1881 struct dst_entry *dst1;
1882 struct xfrm_dst *xdst;
1883
1884 xdst = xfrm_alloc_dst(net, family);
1885 if (IS_ERR(xdst))
1886 return xdst;
1887
1888 if (net->xfrm.sysctl_larval_drop || num_xfrms <= 0 ||
1889 (fl->flowi_flags & FLOWI_FLAG_CAN_SLEEP))
1890 return xdst;
1891
1892 dst1 = &xdst->u.dst;
1893 dst_hold(dst);
1894 xdst->route = dst;
1895
1896 dst_copy_metrics(dst1, dst);
1897
1898 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1899 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
1900 dst1->lastuse = jiffies;
1901
1902 dst1->input = dst_discard;
1903 dst1->output = xdst_queue_output;
1904
1905 dst_hold(dst);
1906 dst1->child = dst;
1907 dst1->path = dst;
1908
1909 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
1910
1911 err = -ENODEV;
1912 dev = dst->dev;
1913 if (!dev)
1914 goto free_dst;
1915
1916 err = xfrm_fill_dst(xdst, dev, fl);
1917 if (err)
1918 goto free_dst;
1919
1920out:
1921 return xdst;
1922
1923free_dst:
1924 dst_release(dst1);
1925 xdst = ERR_PTR(err);
1926 goto out;
1927}
1928
Timo Teräs80c802f2010-04-07 00:30:05 +00001929static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001930xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00001931 struct flow_cache_object *oldflo, void *ctx)
1932{
1933 struct dst_entry *dst_orig = (struct dst_entry *)ctx;
1934 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1935 struct xfrm_dst *xdst, *new_xdst;
1936 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
1937
1938 /* Check if the policies from old bundle are usable */
1939 xdst = NULL;
1940 if (oldflo) {
1941 xdst = container_of(oldflo, struct xfrm_dst, flo);
1942 num_pols = xdst->num_pols;
1943 num_xfrms = xdst->num_xfrms;
1944 pol_dead = 0;
1945 for (i = 0; i < num_pols; i++) {
1946 pols[i] = xdst->pols[i];
1947 pol_dead |= pols[i]->walk.dead;
1948 }
1949 if (pol_dead) {
1950 dst_free(&xdst->u.dst);
1951 xdst = NULL;
1952 num_pols = 0;
1953 num_xfrms = 0;
1954 oldflo = NULL;
1955 }
1956 }
1957
1958 /* Resolve policies to use if we couldn't get them from
1959 * previous cache entry */
1960 if (xdst == NULL) {
1961 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001962 pols[0] = __xfrm_policy_lookup(net, fl, family,
1963 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001964 err = xfrm_expand_policies(fl, family, pols,
1965 &num_pols, &num_xfrms);
1966 if (err < 0)
1967 goto inc_error;
1968 if (num_pols == 0)
1969 return NULL;
1970 if (num_xfrms <= 0)
1971 goto make_dummy_bundle;
1972 }
1973
1974 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family, dst_orig);
1975 if (IS_ERR(new_xdst)) {
1976 err = PTR_ERR(new_xdst);
1977 if (err != -EAGAIN)
1978 goto error;
1979 if (oldflo == NULL)
1980 goto make_dummy_bundle;
1981 dst_hold(&xdst->u.dst);
1982 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00001983 } else if (new_xdst == NULL) {
1984 num_xfrms = 0;
1985 if (oldflo == NULL)
1986 goto make_dummy_bundle;
1987 xdst->num_xfrms = 0;
1988 dst_hold(&xdst->u.dst);
1989 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00001990 }
1991
1992 /* Kill the previous bundle */
1993 if (xdst) {
1994 /* The policies were stolen for newly generated bundle */
1995 xdst->num_pols = 0;
1996 dst_free(&xdst->u.dst);
1997 }
1998
1999 /* Flow cache does not have reference, it dst_free()'s,
2000 * but we do need to return one reference for original caller */
2001 dst_hold(&new_xdst->u.dst);
2002 return &new_xdst->flo;
2003
2004make_dummy_bundle:
2005 /* We found policies, but there's no bundles to instantiate:
2006 * either because the policy blocks, has no transformations or
2007 * we could not build template (no xfrm_states).*/
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002008 xdst = xfrm_create_dummy_bundle(net, dst_orig, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002009 if (IS_ERR(xdst)) {
2010 xfrm_pols_put(pols, num_pols);
2011 return ERR_CAST(xdst);
2012 }
2013 xdst->num_pols = num_pols;
2014 xdst->num_xfrms = num_xfrms;
2015 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
2016
2017 dst_hold(&xdst->u.dst);
2018 return &xdst->flo;
2019
2020inc_error:
2021 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2022error:
2023 if (xdst != NULL)
2024 dst_free(&xdst->u.dst);
2025 else
2026 xfrm_pols_put(pols, num_pols);
2027 return ERR_PTR(err);
2028}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
David S. Miller2774c132011-03-01 14:59:04 -08002030static struct dst_entry *make_blackhole(struct net *net, u16 family,
2031 struct dst_entry *dst_orig)
2032{
2033 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2034 struct dst_entry *ret;
2035
2036 if (!afinfo) {
2037 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002038 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002039 } else {
2040 ret = afinfo->blackhole_route(net, dst_orig);
2041 }
2042 xfrm_policy_put_afinfo(afinfo);
2043
2044 return ret;
2045}
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047/* Main function: finds/creates a bundle for given flow.
2048 *
2049 * At the moment we eat a raw IP route. Mostly to speed up lookups
2050 * on interfaces with disabled IPsec.
2051 */
David S. Miller452edd52011-03-02 13:27:41 -08002052struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2053 const struct flowi *fl,
2054 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002056 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002057 struct flow_cache_object *flo;
2058 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002059 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002060 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002061 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002062 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064restart:
Timo Teräs80c802f2010-04-07 00:30:05 +00002065 dst = NULL;
2066 xdst = NULL;
2067 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002068
Thomas Graff7944fb2007-08-25 13:46:55 -07002069 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002070 num_pols = 1;
2071 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2072 err = xfrm_expand_policies(fl, family, pols,
2073 &num_pols, &num_xfrms);
2074 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002075 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002076
2077 if (num_pols) {
2078 if (num_xfrms <= 0) {
2079 drop_pols = num_pols;
2080 goto no_transform;
2081 }
2082
2083 xdst = xfrm_resolve_and_create_bundle(
2084 pols, num_pols, fl,
2085 family, dst_orig);
2086 if (IS_ERR(xdst)) {
2087 xfrm_pols_put(pols, num_pols);
2088 err = PTR_ERR(xdst);
2089 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002090 } else if (xdst == NULL) {
2091 num_xfrms = 0;
2092 drop_pols = num_pols;
2093 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002094 }
2095
Steffen Klassertfbd50602011-03-15 21:12:49 +00002096 dst_hold(&xdst->u.dst);
2097
Timo Teräs80c802f2010-04-07 00:30:05 +00002098 spin_lock_bh(&xfrm_policy_sk_bundle_lock);
2099 xdst->u.dst.next = xfrm_policy_sk_bundles;
2100 xfrm_policy_sk_bundles = &xdst->u.dst;
2101 spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
2102
2103 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002104 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Timo Teräs80c802f2010-04-07 00:30:05 +00002107 if (xdst == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002109 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002110 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002111 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Timo Teräs80c802f2010-04-07 00:30:05 +00002113 flo = flow_cache_lookup(net, fl, family, dir,
2114 xfrm_bundle_lookup, dst_orig);
2115 if (flo == NULL)
2116 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002117 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002118 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002119 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002120 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002121 xdst = container_of(flo, struct xfrm_dst, flo);
2122
2123 num_pols = xdst->num_pols;
2124 num_xfrms = xdst->num_xfrms;
2125 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy*) * num_pols);
2126 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
2128
Timo Teräs80c802f2010-04-07 00:30:05 +00002129 dst = &xdst->u.dst;
2130 if (route == NULL && num_xfrms > 0) {
2131 /* The only case when xfrm_bundle_lookup() returns a
2132 * bundle with null route, is when the template could
2133 * not be resolved. It means policies are there, but
2134 * bundle could not be created, since we don't yet
2135 * have the xfrm_state's. We need to wait for KM to
2136 * negotiate new SA's or bail out with error.*/
2137 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002138 dst_release(dst);
Timo Terasa1aa3482010-05-15 23:49:26 -07002139 xfrm_pols_put(pols, drop_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002140 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
David S. Miller2774c132011-03-01 14:59:04 -08002141
David S. Miller452edd52011-03-02 13:27:41 -08002142 return make_blackhole(net, family, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002143 }
David S. Miller1d28f422011-03-12 00:29:39 -05002144 if (fl->flowi_flags & FLOWI_FLAG_CAN_SLEEP) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002145 DECLARE_WAITQUEUE(wait, current);
2146
2147 add_wait_queue(&net->xfrm.km_waitq, &wait);
2148 set_current_state(TASK_INTERRUPTIBLE);
2149 schedule();
2150 set_current_state(TASK_RUNNING);
2151 remove_wait_queue(&net->xfrm.km_waitq, &wait);
2152
2153 if (!signal_pending(current)) {
2154 dst_release(dst);
2155 goto restart;
2156 }
2157
2158 err = -ERESTART;
2159 } else
2160 err = -EAGAIN;
2161
2162 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2163 goto error;
2164 }
2165
2166no_transform:
2167 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002168 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Timo Teräs80c802f2010-04-07 00:30:05 +00002170 if ((flags & XFRM_LOOKUP_ICMP) &&
2171 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2172 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002173 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002174 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002175
Timo Teräs80c802f2010-04-07 00:30:05 +00002176 for (i = 0; i < num_pols; i++)
2177 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002178
Timo Teräs80c802f2010-04-07 00:30:05 +00002179 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002181 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002182 err = -EPERM;
2183 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002184 } else if (num_xfrms > 0) {
2185 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002186 dst_release(dst_orig);
2187 } else {
2188 /* Flow passes untransformed */
2189 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002190 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002192ok:
2193 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002194 if (dst && dst->xfrm &&
2195 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2196 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002197 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Timo Teräs80c802f2010-04-07 00:30:05 +00002199nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002200 if (!(flags & XFRM_LOOKUP_ICMP)) {
2201 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002202 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002203 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002204 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002206 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002207dropdst:
2208 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002209 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002210 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211}
2212EXPORT_SYMBOL(xfrm_lookup);
2213
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002214static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002215xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002216{
2217 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002218
2219 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2220 return 0;
2221 x = skb->sp->xvec[idx];
2222 if (!x->type->reject)
2223 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002224 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002225}
2226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227/* When skb is transformed back to its "native" form, we have to
2228 * check policy restrictions. At the moment we make this in maximally
2229 * stupid way. Shame on me. :-) Of course, connected sockets must
2230 * have policy cached at them.
2231 */
2232
2233static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002234xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 unsigned short family)
2236{
2237 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002238 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 return x->id.proto == tmpl->id.proto &&
2240 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2241 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2242 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002243 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002244 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002245 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2246 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247}
2248
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002249/*
2250 * 0 or more than 0 is returned when validation is succeeded (either bypass
2251 * because of optional transport mode, or next index of the mathced secpath
2252 * state with the template.
2253 * -1 is returned when no matching template is found.
2254 * Otherwise "-2 - errored_index" is returned.
2255 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002257xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 unsigned short family)
2259{
2260 int idx = start;
2261
2262 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002263 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 return start;
2265 } else
2266 start = -1;
2267 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002268 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002270 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2271 if (start == -1)
2272 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
2276 return start;
2277}
2278
Herbert Xud5422ef2007-12-12 10:44:16 -08002279int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2280 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
2282 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002283 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285 if (unlikely(afinfo == NULL))
2286 return -EAFNOSUPPORT;
2287
Herbert Xud5422ef2007-12-12 10:44:16 -08002288 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002289 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002291 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292}
Herbert Xud5422ef2007-12-12 10:44:16 -08002293EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
David S. Miller9a7386e2011-02-24 01:44:12 -05002295static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
2297 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002298 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002299 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
2303
2304 return 0;
2305}
2306
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002307int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 unsigned short family)
2309{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002310 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002312 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2313 int npols = 0;
2314 int xfrm_nr;
2315 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002316 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002318 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002319 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Herbert Xud5422ef2007-12-12 10:44:16 -08002321 reverse = dir & ~XFRM_POLICY_MASK;
2322 dir &= XFRM_POLICY_MASK;
2323 fl_dir = policy_to_flow_dir(dir);
2324
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002325 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002326 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002328 }
2329
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002330 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 /* First, check used SA against their selectors. */
2333 if (skb->sp) {
2334 int i;
2335
2336 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002337 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002338 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002339 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 }
2343 }
2344
2345 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002346 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002347 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002348 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002349 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002350 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002351 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002354 if (!pol) {
2355 struct flow_cache_object *flo;
2356
2357 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2358 xfrm_policy_lookup, NULL);
2359 if (IS_ERR_OR_NULL(flo))
2360 pol = ERR_CAST(flo);
2361 else
2362 pol = container_of(flo, struct xfrm_policy, flo);
2363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002365 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002366 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002367 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002368 }
James Morris134b0fc2006-10-05 15:42:27 -05002369
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002370 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002371 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002372 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002373 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002374 return 0;
2375 }
2376 return 1;
2377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
James Morris9d729f72007-03-04 16:12:44 -08002379 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002381 pols[0] = pol;
2382 npols ++;
2383#ifdef CONFIG_XFRM_SUB_POLICY
2384 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002385 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002386 &fl, family,
2387 XFRM_POLICY_IN);
2388 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002389 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002390 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002391 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002392 }
James Morris9d729f72007-03-04 16:12:44 -08002393 pols[1]->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002394 npols ++;
2395 }
2396 }
2397#endif
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 if (pol->action == XFRM_POLICY_ALLOW) {
2400 struct sec_path *sp;
2401 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002402 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002403 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002404 struct xfrm_tmpl **tpp = tp;
2405 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 int i, k;
2407
2408 if ((sp = skb->sp) == NULL)
2409 sp = &dummy;
2410
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002411 for (pi = 0; pi < npols; pi++) {
2412 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002413 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002414 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002415 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002416 }
2417 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002418 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002419 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002420 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002421 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2422 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2423 }
2424 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002425 if (npols > 1) {
2426 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
2427 tpp = stp;
2428 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002429
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 /* For each tunnel xfrm, find the first matching tmpl.
2431 * For each tmpl before that, find corresponding xfrm.
2432 * Order is _important_. Later we will implement
2433 * some barriers, but at the moment barriers
2434 * are implied between each two transformations.
2435 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002436 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2437 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002438 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002439 if (k < -1)
2440 /* "-2 - errored_index" returned */
2441 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002442 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
2446
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002447 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002448 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002452 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 return 1;
2454 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002455 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002458 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002459reject_error:
2460 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 return 0;
2462}
2463EXPORT_SYMBOL(__xfrm_policy_check);
2464
2465int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2466{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002467 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002469 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002470 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002472 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002473 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002477 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002478
David S. Miller452edd52011-03-02 13:27:41 -08002479 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, 0);
2480 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002481 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002482 dst = NULL;
2483 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002484 skb_dst_set(skb, dst);
2485 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486}
2487EXPORT_SYMBOL(__xfrm_route_forward);
2488
David S. Millerd49c73c2006-08-13 18:55:53 -07002489/* Optimize later using cookies and generation ids. */
2490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2492{
David S. Millerd49c73c2006-08-13 18:55:53 -07002493 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002494 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2495 * get validated by dst_ops->check on every use. We do this
2496 * because when a normal route referenced by an XFRM dst is
2497 * obsoleted we do not go looking around for all parent
2498 * referencing XFRM dsts so that we can invalidate them. It
2499 * is just too much work. Instead we make the checks here on
2500 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002501 *
2502 * XFRM dst A --> IPv4 dst X
2503 *
2504 * X is the "xdst->route" of A (X is also the "dst->path" of A
2505 * in this example). If X is marked obsolete, "A" will not
2506 * notice. That's what we are validating here via the
2507 * stale_bundle() check.
2508 *
2509 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002510 * dst which causes it's ->obsolete field to be set to
2511 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2512 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002513 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002514 if (dst->obsolete < 0 && !stale_bundle(dst))
2515 return dst;
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return NULL;
2518}
2519
2520static int stale_bundle(struct dst_entry *dst)
2521{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002522 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523}
2524
Herbert Xuaabc9762005-05-03 16:27:10 -07002525void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002528 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002529 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 dev_put(dev);
2531 }
2532}
Herbert Xuaabc9762005-05-03 16:27:10 -07002533EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
2535static void xfrm_link_failure(struct sk_buff *skb)
2536{
2537 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
2539
2540static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2541{
2542 if (dst) {
2543 if (dst->obsolete) {
2544 dst_release(dst);
2545 dst = NULL;
2546 }
2547 }
2548 return dst;
2549}
2550
Alexey Dobriyanddcfd792008-11-25 17:37:23 -08002551static void __xfrm_garbage_collect(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552{
Timo Teräs80c802f2010-04-07 00:30:05 +00002553 struct dst_entry *head, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Timo Teräs80c802f2010-04-07 00:30:05 +00002555 spin_lock_bh(&xfrm_policy_sk_bundle_lock);
2556 head = xfrm_policy_sk_bundles;
2557 xfrm_policy_sk_bundles = NULL;
2558 spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
2559
2560 while (head) {
2561 next = head->next;
2562 dst_free(head);
2563 head = next;
2564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565}
2566
Paul Mooree4c17212013-05-29 07:36:25 +00002567void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002568{
2569 flow_cache_flush();
2570 __xfrm_garbage_collect(net);
2571}
Paul Mooree4c17212013-05-29 07:36:25 +00002572EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002573
2574static void xfrm_garbage_collect_deferred(struct net *net)
2575{
2576 flow_cache_flush_deferred();
2577 __xfrm_garbage_collect(net);
2578}
2579
Herbert Xu25ee3282007-12-11 09:32:34 -08002580static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
2582 do {
2583 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2584 u32 pmtu, route_mtu_cached;
2585
2586 pmtu = dst_mtu(dst->child);
2587 xdst->child_mtu_cached = pmtu;
2588
2589 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2590
2591 route_mtu_cached = dst_mtu(xdst->route);
2592 xdst->route_mtu_cached = route_mtu_cached;
2593
2594 if (pmtu > route_mtu_cached)
2595 pmtu = route_mtu_cached;
2596
David S. Millerdefb3512010-12-08 21:16:57 -08002597 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 } while ((dst = dst->next));
2599}
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601/* Check that the bundle accepts the flow and its components are
2602 * still valid.
2603 */
2604
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002605static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
2607 struct dst_entry *dst = &first->u.dst;
2608 struct xfrm_dst *last;
2609 u32 mtu;
2610
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002611 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 (dst->dev && !netif_running(dst->dev)))
2613 return 0;
2614
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002615 if (dst->flags & DST_XFRM_QUEUE)
2616 return 1;
2617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 last = NULL;
2619
2620 do {
2621 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2624 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002625 if (xdst->xfrm_genid != dst->xfrm->genid)
2626 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002627 if (xdst->num_pols > 0 &&
2628 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002629 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
2631 mtu = dst_mtu(dst->child);
2632 if (xdst->child_mtu_cached != mtu) {
2633 last = xdst;
2634 xdst->child_mtu_cached = mtu;
2635 }
2636
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002637 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 return 0;
2639 mtu = dst_mtu(xdst->route);
2640 if (xdst->route_mtu_cached != mtu) {
2641 last = xdst;
2642 xdst->route_mtu_cached = mtu;
2643 }
2644
2645 dst = dst->child;
2646 } while (dst->xfrm);
2647
2648 if (likely(!last))
2649 return 1;
2650
2651 mtu = last->child_mtu_cached;
2652 for (;;) {
2653 dst = &last->u.dst;
2654
2655 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2656 if (mtu > last->route_mtu_cached)
2657 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002658 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
2660 if (last == first)
2661 break;
2662
Patrick McHardybd0bf072007-07-18 01:55:52 -07002663 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 last->child_mtu_cached = mtu;
2665 }
2666
2667 return 1;
2668}
2669
David S. Miller0dbaee32010-12-13 12:52:14 -08002670static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2671{
2672 return dst_metric_advmss(dst->path);
2673}
2674
Steffen Klassertebb762f2011-11-23 02:12:51 +00002675static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002676{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002677 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2678
2679 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002680}
2681
David S. Millerf894cbf2012-07-02 21:52:24 -07002682static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2683 struct sk_buff *skb,
2684 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002685{
David S. Millerf894cbf2012-07-02 21:52:24 -07002686 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002687}
2688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2690{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002691 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 int err = 0;
2693 if (unlikely(afinfo == NULL))
2694 return -EINVAL;
2695 if (unlikely(afinfo->family >= NPROTO))
2696 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002697 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2699 err = -ENOBUFS;
2700 else {
2701 struct dst_ops *dst_ops = afinfo->dst_ops;
2702 if (likely(dst_ops->kmem_cachep == NULL))
2703 dst_ops->kmem_cachep = xfrm_dst_cache;
2704 if (likely(dst_ops->check == NULL))
2705 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002706 if (likely(dst_ops->default_advmss == NULL))
2707 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002708 if (likely(dst_ops->mtu == NULL))
2709 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 if (likely(dst_ops->negative_advice == NULL))
2711 dst_ops->negative_advice = xfrm_negative_advice;
2712 if (likely(dst_ops->link_failure == NULL))
2713 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002714 if (likely(dst_ops->neigh_lookup == NULL))
2715 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002717 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002718 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002720 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002721
2722 rtnl_lock();
2723 for_each_net(net) {
2724 struct dst_ops *xfrm_dst_ops;
2725
2726 switch (afinfo->family) {
2727 case AF_INET:
2728 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2729 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002730#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002731 case AF_INET6:
2732 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2733 break;
2734#endif
2735 default:
2736 BUG();
2737 }
2738 *xfrm_dst_ops = *afinfo->dst_ops;
2739 }
2740 rtnl_unlock();
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 return err;
2743}
2744EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2745
2746int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2747{
2748 int err = 0;
2749 if (unlikely(afinfo == NULL))
2750 return -EINVAL;
2751 if (unlikely(afinfo->family >= NPROTO))
2752 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002753 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2755 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2756 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002757 else
2758 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2759 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002761 spin_unlock(&xfrm_policy_afinfo_lock);
2762 if (!err) {
2763 struct dst_ops *dst_ops = afinfo->dst_ops;
2764
2765 synchronize_rcu();
2766
2767 dst_ops->kmem_cachep = NULL;
2768 dst_ops->check = NULL;
2769 dst_ops->negative_advice = NULL;
2770 dst_ops->link_failure = NULL;
2771 afinfo->garbage_collect = NULL;
2772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 return err;
2774}
2775EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2776
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002777static void __net_init xfrm_dst_ops_init(struct net *net)
2778{
2779 struct xfrm_policy_afinfo *afinfo;
2780
Eric Dumazetef8531b2012-08-19 12:31:48 +02002781 rcu_read_lock();
2782 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002783 if (afinfo)
2784 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002785#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetef8531b2012-08-19 12:31:48 +02002786 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002787 if (afinfo)
2788 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2789#endif
Priyanka Jain418a99a2012-08-12 21:22:29 +00002790 rcu_read_unlock();
Herbert Xu546be242006-05-27 23:03:58 -07002791}
2792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2794{
Jiri Pirko351638e2013-05-28 01:30:21 +00002795 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 switch (event) {
2798 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002799 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 }
2801 return NOTIFY_DONE;
2802}
2803
2804static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002805 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806};
2807
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002808#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002809static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002810{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002811 int rv;
2812
Tejun Heo7d720c32010-02-16 15:20:26 +00002813 if (snmp_mib_init((void __percpu **)net->mib.xfrm_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00002814 sizeof(struct linux_xfrm_mib),
2815 __alignof__(struct linux_xfrm_mib)) < 0)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002816 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002817 rv = xfrm_proc_init(net);
2818 if (rv < 0)
Tejun Heo7d720c32010-02-16 15:20:26 +00002819 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002820 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002821}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002822
2823static void xfrm_statistics_fini(struct net *net)
2824{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002825 xfrm_proc_fini(net);
Tejun Heo7d720c32010-02-16 15:20:26 +00002826 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002827}
2828#else
2829static int __net_init xfrm_statistics_init(struct net *net)
2830{
2831 return 0;
2832}
2833
2834static void xfrm_statistics_fini(struct net *net)
2835{
2836}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002837#endif
2838
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002839static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840{
David S. Miller2518c7c2006-08-24 04:45:07 -07002841 unsigned int hmask, sz;
2842 int dir;
2843
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002844 if (net_eq(net, &init_net))
2845 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002847 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002848 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
David S. Miller2518c7c2006-08-24 04:45:07 -07002850 hmask = 8 - 1;
2851 sz = (hmask+1) * sizeof(struct hlist_head);
2852
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002853 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2854 if (!net->xfrm.policy_byidx)
2855 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002856 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002857
2858 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2859 struct xfrm_policy_hash *htab;
2860
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002861 net->xfrm.policy_count[dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002862 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002863
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002864 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002865 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002866 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002867 goto out_bydst;
2868 htab->hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002869 }
2870
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002871 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002872 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002873 if (net_eq(net, &init_net))
2874 register_netdevice_notifier(&xfrm_dev_notifier);
2875 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002876
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002877out_bydst:
2878 for (dir--; dir >= 0; dir--) {
2879 struct xfrm_policy_hash *htab;
2880
2881 htab = &net->xfrm.policy_bydst[dir];
2882 xfrm_hash_free(htab->table, sz);
2883 }
2884 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002885out_byidx:
2886 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887}
2888
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002889static void xfrm_policy_fini(struct net *net)
2890{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002891 struct xfrm_audit audit_info;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002892 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002893 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002894
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002895 flush_work(&net->xfrm.policy_hash_work);
2896#ifdef CONFIG_XFRM_SUB_POLICY
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002897 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002898 audit_info.sessionid = -1;
2899 audit_info.secid = 0;
2900 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, &audit_info);
2901#endif
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002902 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002903 audit_info.sessionid = -1;
2904 audit_info.secid = 0;
2905 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002906
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002907 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002908
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002909 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002910 struct xfrm_policy_hash *htab;
2911
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002912 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002913
2914 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01002915 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002916 WARN_ON(!hlist_empty(htab->table));
2917 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002918 }
2919
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002920 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002921 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2922 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002923}
2924
2925static int __net_init xfrm_net_init(struct net *net)
2926{
2927 int rv;
2928
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002929 rv = xfrm_statistics_init(net);
2930 if (rv < 0)
2931 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002932 rv = xfrm_state_init(net);
2933 if (rv < 0)
2934 goto out_state;
2935 rv = xfrm_policy_init(net);
2936 if (rv < 0)
2937 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002938 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002939 rv = xfrm_sysctl_init(net);
2940 if (rv < 0)
2941 goto out_sysctl;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002942 return 0;
2943
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002944out_sysctl:
2945 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002946out_policy:
2947 xfrm_state_fini(net);
2948out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002949 xfrm_statistics_fini(net);
2950out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002951 return rv;
2952}
2953
2954static void __net_exit xfrm_net_exit(struct net *net)
2955{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002956 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002957 xfrm_policy_fini(net);
2958 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002959 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002960}
2961
2962static struct pernet_operations __net_initdata xfrm_net_ops = {
2963 .init = xfrm_net_init,
2964 .exit = xfrm_net_exit,
2965};
2966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967void __init xfrm_init(void)
2968{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002969 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 xfrm_input_init();
2971}
2972
Joy Lattenab5f5e82007-09-17 11:51:22 -07002973#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08002974static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2975 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002976{
Paul Moore875179f2007-12-01 23:27:18 +11002977 struct xfrm_sec_ctx *ctx = xp->security;
2978 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002979
Paul Moore875179f2007-12-01 23:27:18 +11002980 if (ctx)
2981 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2982 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2983
2984 switch(sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002985 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002986 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002987 if (sel->prefixlen_s != 32)
2988 audit_log_format(audit_buf, " src_prefixlen=%d",
2989 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002990 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002991 if (sel->prefixlen_d != 32)
2992 audit_log_format(audit_buf, " dst_prefixlen=%d",
2993 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002994 break;
2995 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002996 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002997 if (sel->prefixlen_s != 128)
2998 audit_log_format(audit_buf, " src_prefixlen=%d",
2999 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003000 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003001 if (sel->prefixlen_d != 128)
3002 audit_log_format(audit_buf, " dst_prefixlen=%d",
3003 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003004 break;
3005 }
3006}
3007
Paul Moore68277ac2007-12-20 20:49:33 -08003008void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07003009 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003010{
3011 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003012
Paul Mooreafeb14b2007-12-21 14:58:11 -08003013 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003014 if (audit_buf == NULL)
3015 return;
Eric Paris25323862008-04-18 10:09:25 -04003016 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003017 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003018 xfrm_audit_common_policyinfo(xp, audit_buf);
3019 audit_log_end(audit_buf);
3020}
3021EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3022
Paul Moore68277ac2007-12-20 20:49:33 -08003023void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07003024 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003025{
3026 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003027
Paul Mooreafeb14b2007-12-21 14:58:11 -08003028 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003029 if (audit_buf == NULL)
3030 return;
Eric Paris25323862008-04-18 10:09:25 -04003031 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003032 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003033 xfrm_audit_common_policyinfo(xp, audit_buf);
3034 audit_log_end(audit_buf);
3035}
3036EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3037#endif
3038
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003039#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003040static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3041 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003042{
3043 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3044 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003045 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3046 sel_cmp->family) &&
3047 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3048 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003049 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3050 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003051 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003052 }
3053 } else {
3054 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003055 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003056 }
3057 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003058 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003059}
3060
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003061static struct xfrm_policy * xfrm_migrate_policy_find(const struct xfrm_selector *sel,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003062 u8 dir, u8 type)
3063{
3064 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003065 struct hlist_head *chain;
3066 u32 priority = ~0U;
3067
3068 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -08003069 chain = policy_hash_direct(&init_net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003070 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003071 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3072 pol->type == type) {
3073 ret = pol;
3074 priority = ret->priority;
3075 break;
3076 }
3077 }
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003078 chain = &init_net.xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003079 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003080 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3081 pol->type == type &&
3082 pol->priority < priority) {
3083 ret = pol;
3084 break;
3085 }
3086 }
3087
3088 if (ret)
3089 xfrm_pol_hold(ret);
3090
3091 read_unlock_bh(&xfrm_policy_lock);
3092
3093 return ret;
3094}
3095
David S. Millerdd701752011-02-24 00:21:08 -05003096static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003097{
3098 int match = 0;
3099
3100 if (t->mode == m->mode && t->id.proto == m->proto &&
3101 (m->reqid == 0 || t->reqid == m->reqid)) {
3102 switch (t->mode) {
3103 case XFRM_MODE_TUNNEL:
3104 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003105 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3106 m->old_family) &&
3107 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3108 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003109 match = 1;
3110 }
3111 break;
3112 case XFRM_MODE_TRANSPORT:
3113 /* in case of transport mode, template does not store
3114 any IP addresses, hence we just compare mode and
3115 protocol */
3116 match = 1;
3117 break;
3118 default:
3119 break;
3120 }
3121 }
3122 return match;
3123}
3124
3125/* update endpoint address(es) of template(s) */
3126static int xfrm_policy_migrate(struct xfrm_policy *pol,
3127 struct xfrm_migrate *m, int num_migrate)
3128{
3129 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003130 int i, j, n = 0;
3131
3132 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003133 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003134 /* target policy has been deleted */
3135 write_unlock_bh(&pol->lock);
3136 return -ENOENT;
3137 }
3138
3139 for (i = 0; i < pol->xfrm_nr; i++) {
3140 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3141 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3142 continue;
3143 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003144 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3145 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003146 continue;
3147 /* update endpoints */
3148 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3149 sizeof(pol->xfrm_vec[i].id.daddr));
3150 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3151 sizeof(pol->xfrm_vec[i].saddr));
3152 pol->xfrm_vec[i].encap_family = mp->new_family;
3153 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003154 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003155 }
3156 }
3157
3158 write_unlock_bh(&pol->lock);
3159
3160 if (!n)
3161 return -ENODATA;
3162
3163 return 0;
3164}
3165
David S. Millerdd701752011-02-24 00:21:08 -05003166static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003167{
3168 int i, j;
3169
3170 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3171 return -EINVAL;
3172
3173 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003174 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3175 m[i].old_family) &&
3176 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3177 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003178 return -EINVAL;
3179 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3180 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3181 return -EINVAL;
3182
3183 /* check if there is any duplicated entry */
3184 for (j = i + 1; j < num_migrate; j++) {
3185 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3186 sizeof(m[i].old_daddr)) &&
3187 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3188 sizeof(m[i].old_saddr)) &&
3189 m[i].proto == m[j].proto &&
3190 m[i].mode == m[j].mode &&
3191 m[i].reqid == m[j].reqid &&
3192 m[i].old_family == m[j].old_family)
3193 return -EINVAL;
3194 }
3195 }
3196
3197 return 0;
3198}
3199
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003200int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003201 struct xfrm_migrate *m, int num_migrate,
3202 struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003203{
3204 int i, err, nx_cur = 0, nx_new = 0;
3205 struct xfrm_policy *pol = NULL;
3206 struct xfrm_state *x, *xc;
3207 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3208 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3209 struct xfrm_migrate *mp;
3210
3211 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3212 goto out;
3213
3214 /* Stage 1 - find policy */
3215 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
3216 err = -ENOENT;
3217 goto out;
3218 }
3219
3220 /* Stage 2 - find and update state(s) */
3221 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
3222 if ((x = xfrm_migrate_state_find(mp))) {
3223 x_cur[nx_cur] = x;
3224 nx_cur++;
3225 if ((xc = xfrm_state_migrate(x, mp))) {
3226 x_new[nx_new] = xc;
3227 nx_new++;
3228 } else {
3229 err = -ENODATA;
3230 goto restore_state;
3231 }
3232 }
3233 }
3234
3235 /* Stage 3 - update policy */
3236 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3237 goto restore_state;
3238
3239 /* Stage 4 - delete old state(s) */
3240 if (nx_cur) {
3241 xfrm_states_put(x_cur, nx_cur);
3242 xfrm_states_delete(x_cur, nx_cur);
3243 }
3244
3245 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003246 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003247
3248 xfrm_pol_put(pol);
3249
3250 return 0;
3251out:
3252 return err;
3253
3254restore_state:
3255 if (pol)
3256 xfrm_pol_put(pol);
3257 if (nx_cur)
3258 xfrm_states_put(x_cur, nx_cur);
3259 if (nx_new)
3260 xfrm_states_delete(x_new, nx_new);
3261
3262 return err;
3263}
David S. Millere610e672007-02-08 13:29:15 -08003264EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003265#endif