blob: f77c371ea72b9e1a3d239998bdeb91de7585cd3b [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (del_timer(&policy->timer))
312 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 Klasserta0073fe2013-02-05 12:52:55 +0100337 del_timer(&policy->polq.hold_timer);
338 xfrm_queue_purge(&policy->polq.hold_queue);
339
Timo Teräs285ead12010-04-07 00:30:06 +0000340 if (del_timer(&policy->timer))
341 xfrm_pol_put(policy);
342
343 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
David S. Miller2518c7c2006-08-24 04:45:07 -0700346static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
347
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800348static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700349{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800350 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700351}
352
David S. Miller5f803b52011-02-24 00:33:19 -0500353static struct hlist_head *policy_hash_bysel(struct net *net,
354 const struct xfrm_selector *sel,
355 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700356{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800357 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700358 unsigned int hash = __sel_hash(sel, family, hmask);
359
360 return (hash == hmask + 1 ?
Alexey Dobriyan11219942008-11-25 17:33:06 -0800361 &net->xfrm.policy_inexact[dir] :
362 net->xfrm.policy_bydst[dir].table + hash);
David S. Miller2518c7c2006-08-24 04:45:07 -0700363}
364
David S. Miller5f803b52011-02-24 00:33:19 -0500365static struct hlist_head *policy_hash_direct(struct net *net,
366 const xfrm_address_t *daddr,
367 const xfrm_address_t *saddr,
368 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700369{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800370 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700371 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
372
Alexey Dobriyan11219942008-11-25 17:33:06 -0800373 return net->xfrm.policy_bydst[dir].table + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700374}
375
David S. Miller2518c7c2006-08-24 04:45:07 -0700376static void xfrm_dst_hash_transfer(struct hlist_head *list,
377 struct hlist_head *ndsttable,
378 unsigned int nhashmask)
379{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800380 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700381 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800382 unsigned int h0 = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700383
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800384redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800385 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700386 unsigned int h;
387
388 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
389 pol->family, nhashmask);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800390 if (!entry0) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800391 hlist_del(&pol->bydst);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800392 hlist_add_head(&pol->bydst, ndsttable+h);
393 h0 = h;
394 } else {
395 if (h != h0)
396 continue;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800397 hlist_del(&pol->bydst);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800398 hlist_add_after(entry0, &pol->bydst);
399 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800400 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800401 }
402 if (!hlist_empty(list)) {
403 entry0 = NULL;
404 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700405 }
406}
407
408static void xfrm_idx_hash_transfer(struct hlist_head *list,
409 struct hlist_head *nidxtable,
410 unsigned int nhashmask)
411{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800412 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700413 struct xfrm_policy *pol;
414
Sasha Levinb67bfe02013-02-27 17:06:00 -0800415 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700416 unsigned int h;
417
418 h = __idx_hash(pol->index, nhashmask);
419 hlist_add_head(&pol->byidx, nidxtable+h);
420 }
421}
422
423static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
424{
425 return ((old_hmask + 1) << 1) - 1;
426}
427
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800428static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700429{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800430 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700431 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
432 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800433 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700434 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700435 int i;
436
437 if (!ndst)
438 return;
439
440 write_lock_bh(&xfrm_policy_lock);
441
442 for (i = hmask; i >= 0; i--)
443 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
444
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800445 net->xfrm.policy_bydst[dir].table = ndst;
446 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700447
448 write_unlock_bh(&xfrm_policy_lock);
449
David S. Miller44e36b42006-08-24 04:50:50 -0700450 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700451}
452
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800453static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700454{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800455 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700456 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
457 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800458 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700459 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700460 int i;
461
462 if (!nidx)
463 return;
464
465 write_lock_bh(&xfrm_policy_lock);
466
467 for (i = hmask; i >= 0; i--)
468 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
469
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800470 net->xfrm.policy_byidx = nidx;
471 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700472
473 write_unlock_bh(&xfrm_policy_lock);
474
David S. Miller44e36b42006-08-24 04:50:50 -0700475 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700476}
477
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800478static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700479{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800480 unsigned int cnt = net->xfrm.policy_count[dir];
481 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700482
483 if (total)
484 *total += cnt;
485
486 if ((hmask + 1) < xfrm_policy_hashmax &&
487 cnt > hmask)
488 return 1;
489
490 return 0;
491}
492
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800493static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700494{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800495 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700496
497 if ((hmask + 1) < xfrm_policy_hashmax &&
498 total > hmask)
499 return 1;
500
501 return 0;
502}
503
Alexey Dobriyane0710412010-01-23 13:37:10 +0000504void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700505{
506 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000507 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
508 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
509 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
510 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
511 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
512 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
513 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700514 si->spdhmcnt = xfrm_policy_hashmax;
515 read_unlock_bh(&xfrm_policy_lock);
516}
517EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700518
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700519static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800520static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700521{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800522 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700523 int dir, total;
524
525 mutex_lock(&hash_resize_mutex);
526
527 total = 0;
528 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800529 if (xfrm_bydst_should_resize(net, dir, &total))
530 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700531 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800532 if (xfrm_byidx_should_resize(net, total))
533 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700534
535 mutex_unlock(&hash_resize_mutex);
536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538/* Generate new index... KAME seems to generate them ordered by cost
539 * of an absolute inpredictability of ordering of rules. This will not pass. */
Alexey Dobriyan11219942008-11-25 17:33:06 -0800540static u32 xfrm_gen_index(struct net *net, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 static u32 idx_generator;
543
544 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700545 struct hlist_head *list;
546 struct xfrm_policy *p;
547 u32 idx;
548 int found;
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 idx = (idx_generator | dir);
551 idx_generator += 8;
552 if (idx == 0)
553 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800554 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700555 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800556 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700557 if (p->index == idx) {
558 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700562 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return idx;
564 }
565}
566
David S. Miller2518c7c2006-08-24 04:45:07 -0700567static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
568{
569 u32 *p1 = (u32 *) s1;
570 u32 *p2 = (u32 *) s2;
571 int len = sizeof(struct xfrm_selector) / sizeof(u32);
572 int i;
573
574 for (i = 0; i < len; i++) {
575 if (p1[i] != p2[i])
576 return 1;
577 }
578
579 return 0;
580}
581
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100582static void xfrm_policy_requeue(struct xfrm_policy *old,
583 struct xfrm_policy *new)
584{
585 struct xfrm_policy_queue *pq = &old->polq;
586 struct sk_buff_head list;
587
588 __skb_queue_head_init(&list);
589
590 spin_lock_bh(&pq->hold_queue.lock);
591 skb_queue_splice_init(&pq->hold_queue, &list);
592 del_timer(&pq->hold_timer);
593 spin_unlock_bh(&pq->hold_queue.lock);
594
595 if (skb_queue_empty(&list))
596 return;
597
598 pq = &new->polq;
599
600 spin_lock_bh(&pq->hold_queue.lock);
601 skb_queue_splice(&list, &pq->hold_queue);
602 pq->timeout = XFRM_QUEUE_TMO_MIN;
603 mod_timer(&pq->hold_timer, jiffies);
604 spin_unlock_bh(&pq->hold_queue.lock);
605}
606
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100607static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
608 struct xfrm_policy *pol)
609{
610 u32 mark = policy->mark.v & policy->mark.m;
611
612 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
613 return true;
614
615 if ((mark & pol->mark.m) == pol->mark.v &&
616 policy->priority == pol->priority)
617 return true;
618
619 return false;
620}
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
623{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800624 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700625 struct xfrm_policy *pol;
626 struct xfrm_policy *delpol;
627 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800628 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800631 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700632 delpol = NULL;
633 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800634 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800635 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700636 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100637 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800638 xfrm_sec_ctx_match(pol->security, policy->security) &&
639 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (excl) {
641 write_unlock_bh(&xfrm_policy_lock);
642 return -EEXIST;
643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 delpol = pol;
645 if (policy->priority > pol->priority)
646 continue;
647 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800648 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 continue;
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (delpol)
652 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700655 hlist_add_after(newpos, &policy->bydst);
656 else
657 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800659 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 atomic_inc(&flow_cache_genid);
Nicolas Dichtelee8372d2012-09-10 22:09:45 +0000661 rt_genid_bump(net);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100662 if (delpol) {
663 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800664 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100665 }
Alexey Dobriyan11219942008-11-25 17:33:06 -0800666 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir);
667 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800668 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 policy->curlft.use_time = 0;
670 if (!mod_timer(&policy->timer, jiffies + HZ))
671 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800672 list_add(&policy->walk.all, &net->xfrm.policy_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 write_unlock_bh(&xfrm_policy_lock);
674
David S. Miller9b78a822005-12-22 07:39:48 -0800675 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800677 else if (xfrm_bydst_should_resize(net, dir, NULL))
678 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return 0;
681}
682EXPORT_SYMBOL(xfrm_policy_insert);
683
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000684struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
685 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800686 struct xfrm_sec_ctx *ctx, int delete,
687 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
David S. Miller2518c7c2006-08-24 04:45:07 -0700689 struct xfrm_policy *pol, *ret;
690 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Eric Parisef41aaa2007-03-07 15:37:58 -0800692 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800694 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700695 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800696 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700697 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000698 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700699 !selector_cmp(sel, &pol->selector) &&
700 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700702 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700703 *err = security_xfrm_policy_delete(
704 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800705 if (*err) {
706 write_unlock_bh(&xfrm_policy_lock);
707 return pol;
708 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800709 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700710 }
711 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 break;
713 }
714 }
715 write_unlock_bh(&xfrm_policy_lock);
716
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000717 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700718 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700719 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
Trent Jaegerdf718372005-12-13 23:12:27 -0800721EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000723struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
724 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
David S. Miller2518c7c2006-08-24 04:45:07 -0700726 struct xfrm_policy *pol, *ret;
727 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Herbert Xub5505c62007-05-14 02:15:47 -0700729 *err = -ENOENT;
730 if (xfrm_policy_id2dir(id) != dir)
731 return NULL;
732
Eric Parisef41aaa2007-03-07 15:37:58 -0800733 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800735 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700736 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800737 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000738 if (pol->type == type && pol->index == id &&
739 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700741 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700742 *err = security_xfrm_policy_delete(
743 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800744 if (*err) {
745 write_unlock_bh(&xfrm_policy_lock);
746 return pol;
747 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800748 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700749 }
750 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 break;
752 }
753 }
754 write_unlock_bh(&xfrm_policy_lock);
755
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000756 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700757 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700758 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760EXPORT_SYMBOL(xfrm_policy_byid);
761
Joy Latten4aa2e622007-06-04 19:05:57 -0400762#ifdef CONFIG_SECURITY_NETWORK_XFRM
763static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800764xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Joy Latten4aa2e622007-06-04 19:05:57 -0400766 int dir, err = 0;
767
768 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
769 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400770 int i;
771
Sasha Levinb67bfe02013-02-27 17:06:00 -0800772 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800773 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400774 if (pol->type != type)
775 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700776 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400777 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700778 xfrm_audit_policy_delete(pol, 0,
779 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400780 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700781 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400782 return err;
783 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900784 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800785 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800786 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800787 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400788 bydst) {
789 if (pol->type != type)
790 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700791 err = security_xfrm_policy_delete(
792 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400793 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700794 xfrm_audit_policy_delete(pol, 0,
795 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400796 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700797 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400798 return err;
799 }
800 }
801 }
802 }
803 return err;
804}
805#else
806static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800807xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400808{
809 return 0;
810}
811#endif
812
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800813int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400814{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000815 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 write_lock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400818
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800819 err = xfrm_policy_flush_secctx_check(net, type, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400820 if (err)
821 goto out;
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700824 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800825 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700826
827 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800828 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800829 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700830 if (pol->type != type)
831 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000832 __xfrm_policy_unlink(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000834 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Joy Lattenab5f5e82007-09-17 11:51:22 -0700836 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400837 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700838 audit_info->secid);
Joy Latten161a09e2006-11-27 13:11:54 -0600839
David S. Miller2518c7c2006-08-24 04:45:07 -0700840 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700843 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700845
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800846 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700847 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800848 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800849 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700850 bydst) {
851 if (pol->type != type)
852 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000853 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700854 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000855 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700856
Joy Lattenab5f5e82007-09-17 11:51:22 -0700857 xfrm_audit_policy_delete(pol, 1,
858 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400859 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700860 audit_info->secid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700861 xfrm_policy_kill(pol);
862
863 write_lock_bh(&xfrm_policy_lock);
864 goto again2;
865 }
866 }
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000869 if (!cnt)
870 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400871out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 write_unlock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400873 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875EXPORT_SYMBOL(xfrm_policy_flush);
876
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800877int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800878 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 void *data)
880{
Herbert Xu12a169e2008-10-01 07:03:24 -0700881 struct xfrm_policy *pol;
882 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -0800883 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Timo Teras4c563f72008-02-28 21:31:08 -0800885 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
886 walk->type != XFRM_POLICY_TYPE_ANY)
887 return -EINVAL;
888
Herbert Xu12a169e2008-10-01 07:03:24 -0700889 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -0800890 return 0;
891
Herbert Xu12a169e2008-10-01 07:03:24 -0700892 write_lock_bh(&xfrm_policy_lock);
893 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800894 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -0700895 else
896 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800897 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -0700898 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -0800899 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -0700900 pol = container_of(x, struct xfrm_policy, walk);
901 if (walk->type != XFRM_POLICY_TYPE_ANY &&
902 walk->type != pol->type)
903 continue;
904 error = func(pol, xfrm_policy_id2dir(pol->index),
905 walk->seq, data);
906 if (error) {
907 list_move_tail(&walk->walk.all, &x->all);
908 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -0800909 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700910 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700912 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800913 error = -ENOENT;
914 goto out;
915 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700916 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917out:
Herbert Xu12a169e2008-10-01 07:03:24 -0700918 write_unlock_bh(&xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return error;
920}
921EXPORT_SYMBOL(xfrm_policy_walk);
922
Herbert Xu12a169e2008-10-01 07:03:24 -0700923void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
924{
925 INIT_LIST_HEAD(&walk->walk.all);
926 walk->walk.dead = 1;
927 walk->type = type;
928 walk->seq = 0;
929}
930EXPORT_SYMBOL(xfrm_policy_walk_init);
931
932void xfrm_policy_walk_done(struct xfrm_policy_walk *walk)
933{
934 if (list_empty(&walk->walk.all))
935 return;
936
937 write_lock_bh(&xfrm_policy_lock);
938 list_del(&walk->walk.all);
939 write_unlock_bh(&xfrm_policy_lock);
940}
941EXPORT_SYMBOL(xfrm_policy_walk_done);
942
James Morris134b0fc2006-10-05 15:42:27 -0500943/*
944 * Find policy to apply to this flow.
945 *
946 * Returns 0 if policy found, else an -errno.
947 */
David S. Millerf299d552011-02-24 01:23:30 -0500948static int xfrm_policy_match(const struct xfrm_policy *pol,
949 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -0700950 u8 type, u16 family, int dir)
951{
David S. Millerf299d552011-02-24 01:23:30 -0500952 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -0400953 int ret = -ESRCH;
954 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -0700955
956 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -0500957 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -0700958 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500959 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700960
961 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500962 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -0500963 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -0700964 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700965
James Morris134b0fc2006-10-05 15:42:27 -0500966 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700967}
968
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800969static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -0800970 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700971 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
James Morris134b0fc2006-10-05 15:42:27 -0500973 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700974 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -0500975 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -0700976 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700977 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700978
979 daddr = xfrm_flowi_daddr(fl, family);
980 saddr = xfrm_flowi_saddr(fl, family);
981 if (unlikely(!daddr || !saddr))
982 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800985 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700986 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800987 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500988 err = xfrm_policy_match(pol, fl, type, family, dir);
989 if (err) {
990 if (err == -ESRCH)
991 continue;
992 else {
993 ret = ERR_PTR(err);
994 goto fail;
995 }
996 } else {
David S. Milleracba48e2006-08-25 15:46:46 -0700997 ret = pol;
998 priority = ret->priority;
999 break;
1000 }
1001 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001002 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001003 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001004 err = xfrm_policy_match(pol, fl, type, family, dir);
1005 if (err) {
1006 if (err == -ESRCH)
1007 continue;
1008 else {
1009 ret = ERR_PTR(err);
1010 goto fail;
1011 }
1012 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001013 ret = pol;
1014 break;
1015 }
1016 }
David S. Milleracba48e2006-08-25 15:46:46 -07001017 if (ret)
1018 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -05001019fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001021
David S. Miller2518c7c2006-08-24 04:45:07 -07001022 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001023}
1024
Timo Teräs80c802f2010-04-07 00:30:05 +00001025static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001026__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001027{
1028#ifdef CONFIG_XFRM_SUB_POLICY
1029 struct xfrm_policy *pol;
1030
1031 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1032 if (pol != NULL)
1033 return pol;
1034#endif
1035 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1036}
1037
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001038static int flow_to_policy_dir(int dir)
1039{
1040 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1041 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1042 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1043 return dir;
1044
1045 switch (dir) {
1046 default:
1047 case FLOW_DIR_IN:
1048 return XFRM_POLICY_IN;
1049 case FLOW_DIR_OUT:
1050 return XFRM_POLICY_OUT;
1051 case FLOW_DIR_FWD:
1052 return XFRM_POLICY_FWD;
1053 }
1054}
1055
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001056static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001057xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001058 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001059{
1060 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001061
1062 if (old_obj)
1063 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001064
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001065 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001066 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001067 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001068
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001069 /* Resolver returns two references:
1070 * one for cache and one for caller of flow_cache_lookup() */
1071 xfrm_pol_hold(pol);
1072
1073 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
Trent Jaegerdf718372005-12-13 23:12:27 -08001076static inline int policy_to_flow_dir(int dir)
1077{
1078 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001079 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1080 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1081 return dir;
1082 switch (dir) {
1083 default:
1084 case XFRM_POLICY_IN:
1085 return FLOW_DIR_IN;
1086 case XFRM_POLICY_OUT:
1087 return FLOW_DIR_OUT;
1088 case XFRM_POLICY_FWD:
1089 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001090 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001091}
1092
David S. Millerdee9f4b2011-02-22 18:44:31 -08001093static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir,
1094 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
1096 struct xfrm_policy *pol;
1097
1098 read_lock_bh(&xfrm_policy_lock);
1099 if ((pol = sk->sk_policy[dir]) != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001100 bool match = xfrm_selector_match(&pol->selector, fl,
1101 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001102 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001103
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001104 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001105 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1106 pol = NULL;
1107 goto out;
1108 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001109 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001110 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001111 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001112 if (!err)
1113 xfrm_pol_hold(pol);
1114 else if (err == -ESRCH)
1115 pol = NULL;
1116 else
1117 pol = ERR_PTR(err);
1118 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 pol = NULL;
1120 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001121out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 read_unlock_bh(&xfrm_policy_lock);
1123 return pol;
1124}
1125
1126static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1127{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001128 struct net *net = xp_net(pol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001129 struct hlist_head *chain = policy_hash_bysel(net, &pol->selector,
David S. Miller2518c7c2006-08-24 04:45:07 -07001130 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001131
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001132 list_add(&pol->walk.all, &net->xfrm.policy_all);
David S. Miller2518c7c2006-08-24 04:45:07 -07001133 hlist_add_head(&pol->bydst, chain);
Alexey Dobriyane92303f2008-11-25 17:32:41 -08001134 hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001135 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001137
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001138 if (xfrm_bydst_should_resize(net, dir, NULL))
1139 schedule_work(&net->xfrm.policy_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
1142static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1143 int dir)
1144{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001145 struct net *net = xp_net(pol);
1146
David S. Miller2518c7c2006-08-24 04:45:07 -07001147 if (hlist_unhashed(&pol->bydst))
1148 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
David S. Miller2518c7c2006-08-24 04:45:07 -07001150 hlist_del(&pol->bydst);
1151 hlist_del(&pol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -07001152 list_del(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001153 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001154
1155 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
Herbert Xu4666faa2005-06-18 22:43:22 -07001158int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 write_lock_bh(&xfrm_policy_lock);
1161 pol = __xfrm_policy_unlink(pol, dir);
1162 write_unlock_bh(&xfrm_policy_lock);
1163 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001167 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
David S. Millera70fcb02006-03-20 19:18:52 -08001169EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1172{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001173 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 struct xfrm_policy *old_pol;
1175
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001176#ifdef CONFIG_XFRM_SUB_POLICY
1177 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1178 return -EINVAL;
1179#endif
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 write_lock_bh(&xfrm_policy_lock);
1182 old_pol = sk->sk_policy[dir];
1183 sk->sk_policy[dir] = pol;
1184 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001185 pol->curlft.add_time = get_seconds();
Alexey Dobriyan11219942008-11-25 17:33:06 -08001186 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1188 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001189 if (old_pol) {
1190 if (pol)
1191 xfrm_policy_requeue(old_pol, pol);
1192
Timo Teräsea2dea92010-03-31 00:17:05 +00001193 /* Unlinking succeeds always. This is the only function
1194 * allowed to delete or replace socket policy.
1195 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 write_unlock_bh(&xfrm_policy_lock);
1199
1200 if (old_pol) {
1201 xfrm_policy_kill(old_pol);
1202 }
1203 return 0;
1204}
1205
David S. Millerd3e40a92011-02-24 01:25:41 -05001206static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001208 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 if (newp) {
1211 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001212 if (security_xfrm_policy_clone(old->security,
1213 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001214 kfree(newp);
1215 return NULL; /* ENOMEM */
1216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 newp->lft = old->lft;
1218 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001219 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 newp->action = old->action;
1221 newp->flags = old->flags;
1222 newp->xfrm_nr = old->xfrm_nr;
1223 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001224 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 memcpy(newp->xfrm_vec, old->xfrm_vec,
1226 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1227 write_lock_bh(&xfrm_policy_lock);
1228 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1229 write_unlock_bh(&xfrm_policy_lock);
1230 xfrm_pol_put(newp);
1231 }
1232 return newp;
1233}
1234
1235int __xfrm_sk_clone_policy(struct sock *sk)
1236{
1237 struct xfrm_policy *p0 = sk->sk_policy[0],
1238 *p1 = sk->sk_policy[1];
1239
1240 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1241 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1242 return -ENOMEM;
1243 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1244 return -ENOMEM;
1245 return 0;
1246}
1247
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001248static int
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001249xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001250 unsigned short family)
1251{
1252 int err;
1253 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1254
1255 if (unlikely(afinfo == NULL))
1256 return -EINVAL;
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001257 err = afinfo->get_saddr(net, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001258 xfrm_policy_put_afinfo(afinfo);
1259 return err;
1260}
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262/* Resolve list of templates for the flow, given policy. */
1263
1264static int
David S. Millera6c2e612011-02-22 18:35:39 -08001265xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1266 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001268 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 int nx;
1270 int i, error;
1271 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1272 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001273 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1276 struct xfrm_state *x;
1277 xfrm_address_t *remote = daddr;
1278 xfrm_address_t *local = saddr;
1279 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1280
Joakim Koskela48b8d782007-07-26 00:08:42 -07001281 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1282 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 remote = &tmpl->id.daddr;
1284 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001285 if (xfrm_addr_any(local, tmpl->encap_family)) {
1286 error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001287 if (error)
1288 goto fail;
1289 local = &tmp;
1290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
1292
1293 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1294
1295 if (x && x->km.state == XFRM_STATE_VALID) {
1296 xfrm[nx++] = x;
1297 daddr = remote;
1298 saddr = local;
1299 continue;
1300 }
1301 if (x) {
1302 error = (x->km.state == XFRM_STATE_ERROR ?
1303 -EINVAL : -EAGAIN);
1304 xfrm_state_put(x);
1305 }
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001306 else if (error == -ESRCH)
1307 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 if (!tmpl->optional)
1310 goto fail;
1311 }
1312 return nx;
1313
1314fail:
1315 for (nx--; nx>=0; nx--)
1316 xfrm_state_put(xfrm[nx]);
1317 return error;
1318}
1319
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001320static int
David S. Millera6c2e612011-02-22 18:35:39 -08001321xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1322 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001323{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001324 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1325 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001326 int cnx = 0;
1327 int error;
1328 int ret;
1329 int i;
1330
1331 for (i = 0; i < npols; i++) {
1332 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1333 error = -ENOBUFS;
1334 goto fail;
1335 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001336
1337 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001338 if (ret < 0) {
1339 error = ret;
1340 goto fail;
1341 } else
1342 cnx += ret;
1343 }
1344
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001345 /* found states are sorted for outbound processing */
1346 if (npols > 1)
1347 xfrm_state_sort(xfrm, tpp, cnx, family);
1348
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001349 return cnx;
1350
1351 fail:
1352 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001353 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001354 return error;
1355
1356}
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358/* Check that the bundle accepts the flow and its components are
1359 * still valid.
1360 */
1361
David S. Miller05d84022011-02-22 17:47:10 -08001362static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001363{
1364 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1365 int tos;
1366
1367 if (!afinfo)
1368 return -EINVAL;
1369
1370 tos = afinfo->get_tos(fl);
1371
1372 xfrm_policy_put_afinfo(afinfo);
1373
1374 return tos;
1375}
1376
Timo Teräs80c802f2010-04-07 00:30:05 +00001377static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1378{
1379 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1380 struct dst_entry *dst = &xdst->u.dst;
1381
1382 if (xdst->route == NULL) {
1383 /* Dummy bundle - if it has xfrms we were not
1384 * able to build bundle as template resolution failed.
1385 * It means we need to try again resolving. */
1386 if (xdst->num_xfrms > 0)
1387 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001388 } else if (dst->flags & DST_XFRM_QUEUE) {
1389 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001390 } else {
1391 /* Real bundle */
1392 if (stale_bundle(dst))
1393 return NULL;
1394 }
1395
1396 dst_hold(dst);
1397 return flo;
1398}
1399
1400static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1401{
1402 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1403 struct dst_entry *dst = &xdst->u.dst;
1404
1405 if (!xdst->route)
1406 return 0;
1407 if (stale_bundle(dst))
1408 return 0;
1409
1410 return 1;
1411}
1412
1413static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1414{
1415 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1416 struct dst_entry *dst = &xdst->u.dst;
1417
1418 dst_free(dst);
1419}
1420
1421static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1422 .get = xfrm_bundle_flo_get,
1423 .check = xfrm_bundle_flo_check,
1424 .delete = xfrm_bundle_flo_delete,
1425};
1426
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001427static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001428{
1429 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001430 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001431 struct xfrm_dst *xdst;
1432
1433 if (!afinfo)
1434 return ERR_PTR(-EINVAL);
1435
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001436 switch (family) {
1437 case AF_INET:
1438 dst_ops = &net->xfrm.xfrm4_dst_ops;
1439 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001440#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001441 case AF_INET6:
1442 dst_ops = &net->xfrm.xfrm6_dst_ops;
1443 break;
1444#endif
1445 default:
1446 BUG();
1447 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001448 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001449
Madalin Bucurd4cae562011-09-26 07:04:36 +00001450 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001451 struct dst_entry *dst = &xdst->u.dst;
1452
1453 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001454 xdst->flo.ops = &xfrm_bundle_fc_ops;
Patrick McHardy9d7b0fc2012-08-20 02:56:56 -07001455 if (afinfo->init_dst)
1456 afinfo->init_dst(net, xdst);
Madalin Bucurd4cae562011-09-26 07:04:36 +00001457 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001458 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001459
Madalin Bucurd4cae562011-09-26 07:04:36 +00001460 xfrm_policy_put_afinfo(afinfo);
1461
Herbert Xu25ee3282007-12-11 09:32:34 -08001462 return xdst;
1463}
1464
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001465static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1466 int nfheader_len)
1467{
1468 struct xfrm_policy_afinfo *afinfo =
1469 xfrm_policy_get_afinfo(dst->ops->family);
1470 int err;
1471
1472 if (!afinfo)
1473 return -EINVAL;
1474
1475 err = afinfo->init_path(path, dst, nfheader_len);
1476
1477 xfrm_policy_put_afinfo(afinfo);
1478
1479 return err;
1480}
1481
Herbert Xu87c1e122010-03-02 02:51:56 +00001482static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001483 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001484{
1485 struct xfrm_policy_afinfo *afinfo =
1486 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1487 int err;
1488
1489 if (!afinfo)
1490 return -EINVAL;
1491
Herbert Xu87c1e122010-03-02 02:51:56 +00001492 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001493
1494 xfrm_policy_put_afinfo(afinfo);
1495
1496 return err;
1497}
1498
Timo Teräs80c802f2010-04-07 00:30:05 +00001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1501 * all the metrics... Shortly, bundle a bundle.
1502 */
1503
Herbert Xu25ee3282007-12-11 09:32:34 -08001504static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1505 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001506 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001507 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001509 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001510 unsigned long now = jiffies;
1511 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001512 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001513 struct dst_entry *dst_prev = NULL;
1514 struct dst_entry *dst0 = NULL;
1515 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001517 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001518 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001519 int trailer_len = 0;
1520 int tos;
1521 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001522 xfrm_address_t saddr, daddr;
1523
1524 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001525
1526 tos = xfrm_get_tos(fl, family);
1527 err = tos;
1528 if (tos < 0)
1529 goto put_states;
1530
1531 dst_hold(dst);
1532
1533 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001534 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001535 struct dst_entry *dst1 = &xdst->u.dst;
1536
1537 err = PTR_ERR(xdst);
1538 if (IS_ERR(xdst)) {
1539 dst_release(dst);
1540 goto put_states;
1541 }
1542
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001543 if (xfrm[i]->sel.family == AF_UNSPEC) {
1544 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1545 xfrm_af2proto(family));
1546 if (!inner_mode) {
1547 err = -EAFNOSUPPORT;
1548 dst_release(dst);
1549 goto put_states;
1550 }
1551 } else
1552 inner_mode = xfrm[i]->inner_mode;
1553
Herbert Xu25ee3282007-12-11 09:32:34 -08001554 if (!dst_prev)
1555 dst0 = dst1;
1556 else {
1557 dst_prev->child = dst_clone(dst1);
1558 dst1->flags |= DST_NOHASH;
1559 }
1560
1561 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001562 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001563
1564 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1565 family = xfrm[i]->props.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001566 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1567 family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001568 err = PTR_ERR(dst);
1569 if (IS_ERR(dst))
1570 goto put_states;
1571 } else
1572 dst_hold(dst);
1573
1574 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001575 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001576
David S. Millerf5b0a872012-07-19 12:31:33 -07001577 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001578 dst1->flags |= DST_HOST;
1579 dst1->lastuse = now;
1580
1581 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001582 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001583
1584 dst1->next = dst_prev;
1585 dst_prev = dst1;
1586
1587 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001588 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1589 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001590 trailer_len += xfrm[i]->props.trailer_len;
1591 }
1592
1593 dst_prev->child = dst;
1594 dst0->path = dst;
1595
1596 err = -ENODEV;
1597 dev = dst->dev;
1598 if (!dev)
1599 goto free_dst;
1600
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001601 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001602 xfrm_init_pmtu(dst_prev);
1603
1604 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1605 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1606
Herbert Xu87c1e122010-03-02 02:51:56 +00001607 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001608 if (err)
1609 goto free_dst;
1610
1611 dst_prev->header_len = header_len;
1612 dst_prev->trailer_len = trailer_len;
1613 header_len -= xdst->u.dst.xfrm->props.header_len;
1614 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1615 }
1616
1617out:
1618 return dst0;
1619
1620put_states:
1621 for (; i < nx; i++)
1622 xfrm_state_put(xfrm[i]);
1623free_dst:
1624 if (dst0)
1625 dst_free(dst0);
1626 dst0 = ERR_PTR(err);
1627 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
1629
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001630static int inline
David S. Miller3f0e18f2011-02-22 18:38:14 -08001631xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001632{
1633 if (!*target) {
1634 *target = kmalloc(size, GFP_ATOMIC);
1635 if (!*target)
1636 return -ENOMEM;
1637 }
1638 memcpy(*target, src, size);
1639 return 0;
1640}
1641
1642static int inline
David S. Miller1786b382011-02-24 01:32:54 -05001643xfrm_dst_update_parent(struct dst_entry *dst, const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001644{
1645#ifdef CONFIG_XFRM_SUB_POLICY
1646 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1647 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1648 sel, sizeof(*sel));
1649#else
1650 return 0;
1651#endif
1652}
1653
1654static int inline
David S. Miller3f0e18f2011-02-22 18:38:14 -08001655xfrm_dst_update_origin(struct dst_entry *dst, const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001656{
1657#ifdef CONFIG_XFRM_SUB_POLICY
1658 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1659 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1660#else
1661 return 0;
1662#endif
1663}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
David S. Miller73ff93c2011-02-22 18:33:42 -08001665static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001666 struct xfrm_policy **pols,
1667 int *num_pols, int *num_xfrms)
1668{
1669 int i;
1670
1671 if (*num_pols == 0 || !pols[0]) {
1672 *num_pols = 0;
1673 *num_xfrms = 0;
1674 return 0;
1675 }
1676 if (IS_ERR(pols[0]))
1677 return PTR_ERR(pols[0]);
1678
1679 *num_xfrms = pols[0]->xfrm_nr;
1680
1681#ifdef CONFIG_XFRM_SUB_POLICY
1682 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1683 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1684 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1685 XFRM_POLICY_TYPE_MAIN,
1686 fl, family,
1687 XFRM_POLICY_OUT);
1688 if (pols[1]) {
1689 if (IS_ERR(pols[1])) {
1690 xfrm_pols_put(pols, *num_pols);
1691 return PTR_ERR(pols[1]);
1692 }
1693 (*num_pols) ++;
1694 (*num_xfrms) += pols[1]->xfrm_nr;
1695 }
1696 }
1697#endif
1698 for (i = 0; i < *num_pols; i++) {
1699 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1700 *num_xfrms = -1;
1701 break;
1702 }
1703 }
1704
1705 return 0;
1706
1707}
1708
1709static struct xfrm_dst *
1710xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001711 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001712 struct dst_entry *dst_orig)
1713{
1714 struct net *net = xp_net(pols[0]);
1715 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1716 struct dst_entry *dst;
1717 struct xfrm_dst *xdst;
1718 int err;
1719
1720 /* Try to instantiate a bundle */
1721 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001722 if (err <= 0) {
1723 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001724 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1725 return ERR_PTR(err);
1726 }
1727
1728 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1729 if (IS_ERR(dst)) {
1730 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1731 return ERR_CAST(dst);
1732 }
1733
1734 xdst = (struct xfrm_dst *)dst;
1735 xdst->num_xfrms = err;
1736 if (num_pols > 1)
1737 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1738 else
1739 err = xfrm_dst_update_origin(dst, fl);
1740 if (unlikely(err)) {
1741 dst_free(dst);
1742 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1743 return ERR_PTR(err);
1744 }
1745
1746 xdst->num_pols = num_pols;
1747 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
1748 xdst->policy_genid = atomic_read(&pols[0]->genid);
1749
1750 return xdst;
1751}
1752
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001753static void xfrm_policy_queue_process(unsigned long arg)
1754{
1755 int err = 0;
1756 struct sk_buff *skb;
1757 struct sock *sk;
1758 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001759 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1760 struct xfrm_policy_queue *pq = &pol->polq;
1761 struct flowi fl;
1762 struct sk_buff_head list;
1763
1764 spin_lock(&pq->hold_queue.lock);
1765 skb = skb_peek(&pq->hold_queue);
1766 dst = skb_dst(skb);
1767 sk = skb->sk;
1768 xfrm_decode_session(skb, &fl, dst->ops->family);
1769 spin_unlock(&pq->hold_queue.lock);
1770
1771 dst_hold(dst->path);
1772 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1773 sk, 0);
1774 if (IS_ERR(dst))
1775 goto purge_queue;
1776
1777 if (dst->flags & DST_XFRM_QUEUE) {
1778 dst_release(dst);
1779
1780 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1781 goto purge_queue;
1782
1783 pq->timeout = pq->timeout << 1;
1784 mod_timer(&pq->hold_timer, jiffies + pq->timeout);
1785 return;
1786 }
1787
1788 dst_release(dst);
1789
1790 __skb_queue_head_init(&list);
1791
1792 spin_lock(&pq->hold_queue.lock);
1793 pq->timeout = 0;
1794 skb_queue_splice_init(&pq->hold_queue, &list);
1795 spin_unlock(&pq->hold_queue.lock);
1796
1797 while (!skb_queue_empty(&list)) {
1798 skb = __skb_dequeue(&list);
1799
1800 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1801 dst_hold(skb_dst(skb)->path);
1802 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1803 &fl, skb->sk, 0);
1804 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001805 kfree_skb(skb);
1806 continue;
1807 }
1808
1809 nf_reset(skb);
1810 skb_dst_drop(skb);
1811 skb_dst_set(skb, dst);
1812
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001813 err = dst_output(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001814 }
1815
1816 return;
1817
1818purge_queue:
1819 pq->timeout = 0;
1820 xfrm_queue_purge(&pq->hold_queue);
1821}
1822
1823static int xdst_queue_output(struct sk_buff *skb)
1824{
1825 unsigned long sched_next;
1826 struct dst_entry *dst = skb_dst(skb);
1827 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
1828 struct xfrm_policy_queue *pq = &xdst->pols[0]->polq;
1829
1830 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1831 kfree_skb(skb);
1832 return -EAGAIN;
1833 }
1834
1835 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001836
1837 spin_lock_bh(&pq->hold_queue.lock);
1838
1839 if (!pq->timeout)
1840 pq->timeout = XFRM_QUEUE_TMO_MIN;
1841
1842 sched_next = jiffies + pq->timeout;
1843
1844 if (del_timer(&pq->hold_timer)) {
1845 if (time_before(pq->hold_timer.expires, sched_next))
1846 sched_next = pq->hold_timer.expires;
1847 }
1848
1849 __skb_queue_tail(&pq->hold_queue, skb);
1850 mod_timer(&pq->hold_timer, sched_next);
1851
1852 spin_unlock_bh(&pq->hold_queue.lock);
1853
1854 return 0;
1855}
1856
1857static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
1858 struct dst_entry *dst,
1859 const struct flowi *fl,
1860 int num_xfrms,
1861 u16 family)
1862{
1863 int err;
1864 struct net_device *dev;
1865 struct dst_entry *dst1;
1866 struct xfrm_dst *xdst;
1867
1868 xdst = xfrm_alloc_dst(net, family);
1869 if (IS_ERR(xdst))
1870 return xdst;
1871
1872 if (net->xfrm.sysctl_larval_drop || num_xfrms <= 0 ||
1873 (fl->flowi_flags & FLOWI_FLAG_CAN_SLEEP))
1874 return xdst;
1875
1876 dst1 = &xdst->u.dst;
1877 dst_hold(dst);
1878 xdst->route = dst;
1879
1880 dst_copy_metrics(dst1, dst);
1881
1882 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1883 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
1884 dst1->lastuse = jiffies;
1885
1886 dst1->input = dst_discard;
1887 dst1->output = xdst_queue_output;
1888
1889 dst_hold(dst);
1890 dst1->child = dst;
1891 dst1->path = dst;
1892
1893 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
1894
1895 err = -ENODEV;
1896 dev = dst->dev;
1897 if (!dev)
1898 goto free_dst;
1899
1900 err = xfrm_fill_dst(xdst, dev, fl);
1901 if (err)
1902 goto free_dst;
1903
1904out:
1905 return xdst;
1906
1907free_dst:
1908 dst_release(dst1);
1909 xdst = ERR_PTR(err);
1910 goto out;
1911}
1912
Timo Teräs80c802f2010-04-07 00:30:05 +00001913static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001914xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00001915 struct flow_cache_object *oldflo, void *ctx)
1916{
1917 struct dst_entry *dst_orig = (struct dst_entry *)ctx;
1918 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1919 struct xfrm_dst *xdst, *new_xdst;
1920 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
1921
1922 /* Check if the policies from old bundle are usable */
1923 xdst = NULL;
1924 if (oldflo) {
1925 xdst = container_of(oldflo, struct xfrm_dst, flo);
1926 num_pols = xdst->num_pols;
1927 num_xfrms = xdst->num_xfrms;
1928 pol_dead = 0;
1929 for (i = 0; i < num_pols; i++) {
1930 pols[i] = xdst->pols[i];
1931 pol_dead |= pols[i]->walk.dead;
1932 }
1933 if (pol_dead) {
1934 dst_free(&xdst->u.dst);
1935 xdst = NULL;
1936 num_pols = 0;
1937 num_xfrms = 0;
1938 oldflo = NULL;
1939 }
1940 }
1941
1942 /* Resolve policies to use if we couldn't get them from
1943 * previous cache entry */
1944 if (xdst == NULL) {
1945 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001946 pols[0] = __xfrm_policy_lookup(net, fl, family,
1947 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001948 err = xfrm_expand_policies(fl, family, pols,
1949 &num_pols, &num_xfrms);
1950 if (err < 0)
1951 goto inc_error;
1952 if (num_pols == 0)
1953 return NULL;
1954 if (num_xfrms <= 0)
1955 goto make_dummy_bundle;
1956 }
1957
1958 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family, dst_orig);
1959 if (IS_ERR(new_xdst)) {
1960 err = PTR_ERR(new_xdst);
1961 if (err != -EAGAIN)
1962 goto error;
1963 if (oldflo == NULL)
1964 goto make_dummy_bundle;
1965 dst_hold(&xdst->u.dst);
1966 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00001967 } else if (new_xdst == NULL) {
1968 num_xfrms = 0;
1969 if (oldflo == NULL)
1970 goto make_dummy_bundle;
1971 xdst->num_xfrms = 0;
1972 dst_hold(&xdst->u.dst);
1973 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00001974 }
1975
1976 /* Kill the previous bundle */
1977 if (xdst) {
1978 /* The policies were stolen for newly generated bundle */
1979 xdst->num_pols = 0;
1980 dst_free(&xdst->u.dst);
1981 }
1982
1983 /* Flow cache does not have reference, it dst_free()'s,
1984 * but we do need to return one reference for original caller */
1985 dst_hold(&new_xdst->u.dst);
1986 return &new_xdst->flo;
1987
1988make_dummy_bundle:
1989 /* We found policies, but there's no bundles to instantiate:
1990 * either because the policy blocks, has no transformations or
1991 * we could not build template (no xfrm_states).*/
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001992 xdst = xfrm_create_dummy_bundle(net, dst_orig, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00001993 if (IS_ERR(xdst)) {
1994 xfrm_pols_put(pols, num_pols);
1995 return ERR_CAST(xdst);
1996 }
1997 xdst->num_pols = num_pols;
1998 xdst->num_xfrms = num_xfrms;
1999 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
2000
2001 dst_hold(&xdst->u.dst);
2002 return &xdst->flo;
2003
2004inc_error:
2005 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2006error:
2007 if (xdst != NULL)
2008 dst_free(&xdst->u.dst);
2009 else
2010 xfrm_pols_put(pols, num_pols);
2011 return ERR_PTR(err);
2012}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
David S. Miller2774c132011-03-01 14:59:04 -08002014static struct dst_entry *make_blackhole(struct net *net, u16 family,
2015 struct dst_entry *dst_orig)
2016{
2017 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2018 struct dst_entry *ret;
2019
2020 if (!afinfo) {
2021 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002022 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002023 } else {
2024 ret = afinfo->blackhole_route(net, dst_orig);
2025 }
2026 xfrm_policy_put_afinfo(afinfo);
2027
2028 return ret;
2029}
2030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031/* Main function: finds/creates a bundle for given flow.
2032 *
2033 * At the moment we eat a raw IP route. Mostly to speed up lookups
2034 * on interfaces with disabled IPsec.
2035 */
David S. Miller452edd52011-03-02 13:27:41 -08002036struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2037 const struct flowi *fl,
2038 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002040 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002041 struct flow_cache_object *flo;
2042 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002043 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002044 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002045 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002046 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048restart:
Timo Teräs80c802f2010-04-07 00:30:05 +00002049 dst = NULL;
2050 xdst = NULL;
2051 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002052
Thomas Graff7944fb2007-08-25 13:46:55 -07002053 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002054 num_pols = 1;
2055 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2056 err = xfrm_expand_policies(fl, family, pols,
2057 &num_pols, &num_xfrms);
2058 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002059 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002060
2061 if (num_pols) {
2062 if (num_xfrms <= 0) {
2063 drop_pols = num_pols;
2064 goto no_transform;
2065 }
2066
2067 xdst = xfrm_resolve_and_create_bundle(
2068 pols, num_pols, fl,
2069 family, dst_orig);
2070 if (IS_ERR(xdst)) {
2071 xfrm_pols_put(pols, num_pols);
2072 err = PTR_ERR(xdst);
2073 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002074 } else if (xdst == NULL) {
2075 num_xfrms = 0;
2076 drop_pols = num_pols;
2077 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002078 }
2079
Steffen Klassertfbd50602011-03-15 21:12:49 +00002080 dst_hold(&xdst->u.dst);
2081
Timo Teräs80c802f2010-04-07 00:30:05 +00002082 spin_lock_bh(&xfrm_policy_sk_bundle_lock);
2083 xdst->u.dst.next = xfrm_policy_sk_bundles;
2084 xfrm_policy_sk_bundles = &xdst->u.dst;
2085 spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
2086
2087 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002088 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Timo Teräs80c802f2010-04-07 00:30:05 +00002091 if (xdst == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002093 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002094 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002095 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Timo Teräs80c802f2010-04-07 00:30:05 +00002097 flo = flow_cache_lookup(net, fl, family, dir,
2098 xfrm_bundle_lookup, dst_orig);
2099 if (flo == NULL)
2100 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002101 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002102 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002103 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002104 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002105 xdst = container_of(flo, struct xfrm_dst, flo);
2106
2107 num_pols = xdst->num_pols;
2108 num_xfrms = xdst->num_xfrms;
2109 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy*) * num_pols);
2110 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
2112
Timo Teräs80c802f2010-04-07 00:30:05 +00002113 dst = &xdst->u.dst;
2114 if (route == NULL && num_xfrms > 0) {
2115 /* The only case when xfrm_bundle_lookup() returns a
2116 * bundle with null route, is when the template could
2117 * not be resolved. It means policies are there, but
2118 * bundle could not be created, since we don't yet
2119 * have the xfrm_state's. We need to wait for KM to
2120 * negotiate new SA's or bail out with error.*/
2121 if (net->xfrm.sysctl_larval_drop) {
2122 /* EREMOTE tells the caller to generate
2123 * a one-shot blackhole route. */
2124 dst_release(dst);
Timo Terasa1aa3482010-05-15 23:49:26 -07002125 xfrm_pols_put(pols, drop_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002126 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
David S. Miller2774c132011-03-01 14:59:04 -08002127
David S. Miller452edd52011-03-02 13:27:41 -08002128 return make_blackhole(net, family, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002129 }
David S. Miller1d28f422011-03-12 00:29:39 -05002130 if (fl->flowi_flags & FLOWI_FLAG_CAN_SLEEP) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002131 DECLARE_WAITQUEUE(wait, current);
2132
2133 add_wait_queue(&net->xfrm.km_waitq, &wait);
2134 set_current_state(TASK_INTERRUPTIBLE);
2135 schedule();
2136 set_current_state(TASK_RUNNING);
2137 remove_wait_queue(&net->xfrm.km_waitq, &wait);
2138
2139 if (!signal_pending(current)) {
2140 dst_release(dst);
2141 goto restart;
2142 }
2143
2144 err = -ERESTART;
2145 } else
2146 err = -EAGAIN;
2147
2148 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2149 goto error;
2150 }
2151
2152no_transform:
2153 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002154 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Timo Teräs80c802f2010-04-07 00:30:05 +00002156 if ((flags & XFRM_LOOKUP_ICMP) &&
2157 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2158 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002159 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002160 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002161
Timo Teräs80c802f2010-04-07 00:30:05 +00002162 for (i = 0; i < num_pols; i++)
2163 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002164
Timo Teräs80c802f2010-04-07 00:30:05 +00002165 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002167 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002168 err = -EPERM;
2169 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002170 } else if (num_xfrms > 0) {
2171 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002172 dst_release(dst_orig);
2173 } else {
2174 /* Flow passes untransformed */
2175 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002176 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002178ok:
2179 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002180 if (dst && dst->xfrm &&
2181 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2182 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002183 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Timo Teräs80c802f2010-04-07 00:30:05 +00002185nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002186 if (!(flags & XFRM_LOOKUP_ICMP)) {
2187 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002188 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002189 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002190 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002192 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002193dropdst:
2194 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002195 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002196 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198EXPORT_SYMBOL(xfrm_lookup);
2199
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002200static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002201xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002202{
2203 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002204
2205 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2206 return 0;
2207 x = skb->sp->xvec[idx];
2208 if (!x->type->reject)
2209 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002210 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002211}
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213/* When skb is transformed back to its "native" form, we have to
2214 * check policy restrictions. At the moment we make this in maximally
2215 * stupid way. Shame on me. :-) Of course, connected sockets must
2216 * have policy cached at them.
2217 */
2218
2219static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002220xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 unsigned short family)
2222{
2223 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002224 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return x->id.proto == tmpl->id.proto &&
2226 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2227 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2228 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002229 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002230 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002231 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2232 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002235/*
2236 * 0 or more than 0 is returned when validation is succeeded (either bypass
2237 * because of optional transport mode, or next index of the mathced secpath
2238 * state with the template.
2239 * -1 is returned when no matching template is found.
2240 * Otherwise "-2 - errored_index" is returned.
2241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002243xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 unsigned short family)
2245{
2246 int idx = start;
2247
2248 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002249 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return start;
2251 } else
2252 start = -1;
2253 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002254 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002256 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2257 if (start == -1)
2258 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
2262 return start;
2263}
2264
Herbert Xud5422ef2007-12-12 10:44:16 -08002265int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2266 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
2268 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002269 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271 if (unlikely(afinfo == NULL))
2272 return -EAFNOSUPPORT;
2273
Herbert Xud5422ef2007-12-12 10:44:16 -08002274 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002275 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002277 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
Herbert Xud5422ef2007-12-12 10:44:16 -08002279EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
David S. Miller9a7386e2011-02-24 01:44:12 -05002281static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
2283 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002284 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002285 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 }
2289
2290 return 0;
2291}
2292
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002293int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 unsigned short family)
2295{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002296 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002298 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2299 int npols = 0;
2300 int xfrm_nr;
2301 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002302 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002304 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002305 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Herbert Xud5422ef2007-12-12 10:44:16 -08002307 reverse = dir & ~XFRM_POLICY_MASK;
2308 dir &= XFRM_POLICY_MASK;
2309 fl_dir = policy_to_flow_dir(dir);
2310
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002311 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002312 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002314 }
2315
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002316 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 /* First, check used SA against their selectors. */
2319 if (skb->sp) {
2320 int i;
2321
2322 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002323 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002324 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002325 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 }
2329 }
2330
2331 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002332 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002333 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002334 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002335 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002336 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002337 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002340 if (!pol) {
2341 struct flow_cache_object *flo;
2342
2343 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2344 xfrm_policy_lookup, NULL);
2345 if (IS_ERR_OR_NULL(flo))
2346 pol = ERR_CAST(flo);
2347 else
2348 pol = container_of(flo, struct xfrm_policy, flo);
2349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002351 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002352 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002353 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002354 }
James Morris134b0fc2006-10-05 15:42:27 -05002355
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002356 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002357 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002358 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002359 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002360 return 0;
2361 }
2362 return 1;
2363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
James Morris9d729f72007-03-04 16:12:44 -08002365 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002367 pols[0] = pol;
2368 npols ++;
2369#ifdef CONFIG_XFRM_SUB_POLICY
2370 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002371 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002372 &fl, family,
2373 XFRM_POLICY_IN);
2374 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002375 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002376 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002377 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002378 }
James Morris9d729f72007-03-04 16:12:44 -08002379 pols[1]->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002380 npols ++;
2381 }
2382 }
2383#endif
2384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (pol->action == XFRM_POLICY_ALLOW) {
2386 struct sec_path *sp;
2387 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002388 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002389 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002390 struct xfrm_tmpl **tpp = tp;
2391 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 int i, k;
2393
2394 if ((sp = skb->sp) == NULL)
2395 sp = &dummy;
2396
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002397 for (pi = 0; pi < npols; pi++) {
2398 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002399 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002400 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002401 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002402 }
2403 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002404 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002405 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002406 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002407 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2408 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2409 }
2410 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002411 if (npols > 1) {
2412 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
2413 tpp = stp;
2414 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 /* For each tunnel xfrm, find the first matching tmpl.
2417 * For each tmpl before that, find corresponding xfrm.
2418 * Order is _important_. Later we will implement
2419 * some barriers, but at the moment barriers
2420 * are implied between each two transformations.
2421 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002422 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2423 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002424 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002425 if (k < -1)
2426 /* "-2 - errored_index" returned */
2427 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002428 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 }
2432
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002433 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002434 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002438 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 return 1;
2440 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002441 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002444 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002445reject_error:
2446 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 return 0;
2448}
2449EXPORT_SYMBOL(__xfrm_policy_check);
2450
2451int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2452{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002453 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002455 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002456 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002458 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002459 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002463 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002464
David S. Miller452edd52011-03-02 13:27:41 -08002465 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, 0);
2466 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002467 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002468 dst = NULL;
2469 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002470 skb_dst_set(skb, dst);
2471 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472}
2473EXPORT_SYMBOL(__xfrm_route_forward);
2474
David S. Millerd49c73c2006-08-13 18:55:53 -07002475/* Optimize later using cookies and generation ids. */
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2478{
David S. Millerd49c73c2006-08-13 18:55:53 -07002479 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002480 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2481 * get validated by dst_ops->check on every use. We do this
2482 * because when a normal route referenced by an XFRM dst is
2483 * obsoleted we do not go looking around for all parent
2484 * referencing XFRM dsts so that we can invalidate them. It
2485 * is just too much work. Instead we make the checks here on
2486 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002487 *
2488 * XFRM dst A --> IPv4 dst X
2489 *
2490 * X is the "xdst->route" of A (X is also the "dst->path" of A
2491 * in this example). If X is marked obsolete, "A" will not
2492 * notice. That's what we are validating here via the
2493 * stale_bundle() check.
2494 *
2495 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002496 * dst which causes it's ->obsolete field to be set to
2497 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2498 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002499 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002500 if (dst->obsolete < 0 && !stale_bundle(dst))
2501 return dst;
2502
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 return NULL;
2504}
2505
2506static int stale_bundle(struct dst_entry *dst)
2507{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002508 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510
Herbert Xuaabc9762005-05-03 16:27:10 -07002511void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002514 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002515 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 dev_put(dev);
2517 }
2518}
Herbert Xuaabc9762005-05-03 16:27:10 -07002519EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
2521static void xfrm_link_failure(struct sk_buff *skb)
2522{
2523 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524}
2525
2526static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2527{
2528 if (dst) {
2529 if (dst->obsolete) {
2530 dst_release(dst);
2531 dst = NULL;
2532 }
2533 }
2534 return dst;
2535}
2536
Alexey Dobriyanddcfd792008-11-25 17:37:23 -08002537static void __xfrm_garbage_collect(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
Timo Teräs80c802f2010-04-07 00:30:05 +00002539 struct dst_entry *head, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Timo Teräs80c802f2010-04-07 00:30:05 +00002541 spin_lock_bh(&xfrm_policy_sk_bundle_lock);
2542 head = xfrm_policy_sk_bundles;
2543 xfrm_policy_sk_bundles = NULL;
2544 spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
2545
2546 while (head) {
2547 next = head->next;
2548 dst_free(head);
2549 head = next;
2550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551}
2552
Paul Mooree4c17212013-05-29 07:36:25 +00002553void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002554{
2555 flow_cache_flush();
2556 __xfrm_garbage_collect(net);
2557}
Paul Mooree4c17212013-05-29 07:36:25 +00002558EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002559
2560static void xfrm_garbage_collect_deferred(struct net *net)
2561{
2562 flow_cache_flush_deferred();
2563 __xfrm_garbage_collect(net);
2564}
2565
Herbert Xu25ee3282007-12-11 09:32:34 -08002566static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
2568 do {
2569 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2570 u32 pmtu, route_mtu_cached;
2571
2572 pmtu = dst_mtu(dst->child);
2573 xdst->child_mtu_cached = pmtu;
2574
2575 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2576
2577 route_mtu_cached = dst_mtu(xdst->route);
2578 xdst->route_mtu_cached = route_mtu_cached;
2579
2580 if (pmtu > route_mtu_cached)
2581 pmtu = route_mtu_cached;
2582
David S. Millerdefb3512010-12-08 21:16:57 -08002583 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 } while ((dst = dst->next));
2585}
2586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587/* Check that the bundle accepts the flow and its components are
2588 * still valid.
2589 */
2590
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002591static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
2593 struct dst_entry *dst = &first->u.dst;
2594 struct xfrm_dst *last;
2595 u32 mtu;
2596
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002597 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 (dst->dev && !netif_running(dst->dev)))
2599 return 0;
2600
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002601 if (dst->flags & DST_XFRM_QUEUE)
2602 return 1;
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 last = NULL;
2605
2606 do {
2607 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2610 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002611 if (xdst->xfrm_genid != dst->xfrm->genid)
2612 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002613 if (xdst->num_pols > 0 &&
2614 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
2617 mtu = dst_mtu(dst->child);
2618 if (xdst->child_mtu_cached != mtu) {
2619 last = xdst;
2620 xdst->child_mtu_cached = mtu;
2621 }
2622
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002623 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 return 0;
2625 mtu = dst_mtu(xdst->route);
2626 if (xdst->route_mtu_cached != mtu) {
2627 last = xdst;
2628 xdst->route_mtu_cached = mtu;
2629 }
2630
2631 dst = dst->child;
2632 } while (dst->xfrm);
2633
2634 if (likely(!last))
2635 return 1;
2636
2637 mtu = last->child_mtu_cached;
2638 for (;;) {
2639 dst = &last->u.dst;
2640
2641 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2642 if (mtu > last->route_mtu_cached)
2643 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002644 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
2646 if (last == first)
2647 break;
2648
Patrick McHardybd0bf072007-07-18 01:55:52 -07002649 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 last->child_mtu_cached = mtu;
2651 }
2652
2653 return 1;
2654}
2655
David S. Miller0dbaee32010-12-13 12:52:14 -08002656static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2657{
2658 return dst_metric_advmss(dst->path);
2659}
2660
Steffen Klassertebb762f2011-11-23 02:12:51 +00002661static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002662{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002663 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2664
2665 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002666}
2667
David S. Millerf894cbf2012-07-02 21:52:24 -07002668static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2669 struct sk_buff *skb,
2670 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002671{
David S. Millerf894cbf2012-07-02 21:52:24 -07002672 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002673}
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2676{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002677 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 int err = 0;
2679 if (unlikely(afinfo == NULL))
2680 return -EINVAL;
2681 if (unlikely(afinfo->family >= NPROTO))
2682 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002683 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2685 err = -ENOBUFS;
2686 else {
2687 struct dst_ops *dst_ops = afinfo->dst_ops;
2688 if (likely(dst_ops->kmem_cachep == NULL))
2689 dst_ops->kmem_cachep = xfrm_dst_cache;
2690 if (likely(dst_ops->check == NULL))
2691 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002692 if (likely(dst_ops->default_advmss == NULL))
2693 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002694 if (likely(dst_ops->mtu == NULL))
2695 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 if (likely(dst_ops->negative_advice == NULL))
2697 dst_ops->negative_advice = xfrm_negative_advice;
2698 if (likely(dst_ops->link_failure == NULL))
2699 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002700 if (likely(dst_ops->neigh_lookup == NULL))
2701 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002703 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002704 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002706 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002707
2708 rtnl_lock();
2709 for_each_net(net) {
2710 struct dst_ops *xfrm_dst_ops;
2711
2712 switch (afinfo->family) {
2713 case AF_INET:
2714 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2715 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002716#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002717 case AF_INET6:
2718 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2719 break;
2720#endif
2721 default:
2722 BUG();
2723 }
2724 *xfrm_dst_ops = *afinfo->dst_ops;
2725 }
2726 rtnl_unlock();
2727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 return err;
2729}
2730EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2731
2732int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2733{
2734 int err = 0;
2735 if (unlikely(afinfo == NULL))
2736 return -EINVAL;
2737 if (unlikely(afinfo->family >= NPROTO))
2738 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002739 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2741 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2742 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002743 else
2744 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2745 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002747 spin_unlock(&xfrm_policy_afinfo_lock);
2748 if (!err) {
2749 struct dst_ops *dst_ops = afinfo->dst_ops;
2750
2751 synchronize_rcu();
2752
2753 dst_ops->kmem_cachep = NULL;
2754 dst_ops->check = NULL;
2755 dst_ops->negative_advice = NULL;
2756 dst_ops->link_failure = NULL;
2757 afinfo->garbage_collect = NULL;
2758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 return err;
2760}
2761EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2762
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002763static void __net_init xfrm_dst_ops_init(struct net *net)
2764{
2765 struct xfrm_policy_afinfo *afinfo;
2766
Eric Dumazetef8531b2012-08-19 12:31:48 +02002767 rcu_read_lock();
2768 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002769 if (afinfo)
2770 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002771#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetef8531b2012-08-19 12:31:48 +02002772 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002773 if (afinfo)
2774 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2775#endif
Priyanka Jain418a99a2012-08-12 21:22:29 +00002776 rcu_read_unlock();
Herbert Xu546be242006-05-27 23:03:58 -07002777}
2778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2780{
Jiri Pirko351638e2013-05-28 01:30:21 +00002781 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 switch (event) {
2784 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002785 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 }
2787 return NOTIFY_DONE;
2788}
2789
2790static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002791 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792};
2793
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002794#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002795static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002796{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002797 int rv;
2798
Tejun Heo7d720c32010-02-16 15:20:26 +00002799 if (snmp_mib_init((void __percpu **)net->mib.xfrm_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00002800 sizeof(struct linux_xfrm_mib),
2801 __alignof__(struct linux_xfrm_mib)) < 0)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002802 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002803 rv = xfrm_proc_init(net);
2804 if (rv < 0)
Tejun Heo7d720c32010-02-16 15:20:26 +00002805 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002806 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002807}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002808
2809static void xfrm_statistics_fini(struct net *net)
2810{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002811 xfrm_proc_fini(net);
Tejun Heo7d720c32010-02-16 15:20:26 +00002812 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002813}
2814#else
2815static int __net_init xfrm_statistics_init(struct net *net)
2816{
2817 return 0;
2818}
2819
2820static void xfrm_statistics_fini(struct net *net)
2821{
2822}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002823#endif
2824
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002825static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
David S. Miller2518c7c2006-08-24 04:45:07 -07002827 unsigned int hmask, sz;
2828 int dir;
2829
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002830 if (net_eq(net, &init_net))
2831 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002833 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002834 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
David S. Miller2518c7c2006-08-24 04:45:07 -07002836 hmask = 8 - 1;
2837 sz = (hmask+1) * sizeof(struct hlist_head);
2838
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002839 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2840 if (!net->xfrm.policy_byidx)
2841 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002842 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002843
2844 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2845 struct xfrm_policy_hash *htab;
2846
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002847 net->xfrm.policy_count[dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002848 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002849
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002850 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002851 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002852 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002853 goto out_bydst;
2854 htab->hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002855 }
2856
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002857 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002858 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002859 if (net_eq(net, &init_net))
2860 register_netdevice_notifier(&xfrm_dev_notifier);
2861 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002862
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002863out_bydst:
2864 for (dir--; dir >= 0; dir--) {
2865 struct xfrm_policy_hash *htab;
2866
2867 htab = &net->xfrm.policy_bydst[dir];
2868 xfrm_hash_free(htab->table, sz);
2869 }
2870 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002871out_byidx:
2872 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873}
2874
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002875static void xfrm_policy_fini(struct net *net)
2876{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002877 struct xfrm_audit audit_info;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002878 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002879 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002880
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002881 flush_work(&net->xfrm.policy_hash_work);
2882#ifdef CONFIG_XFRM_SUB_POLICY
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002883 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002884 audit_info.sessionid = -1;
2885 audit_info.secid = 0;
2886 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, &audit_info);
2887#endif
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002888 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002889 audit_info.sessionid = -1;
2890 audit_info.secid = 0;
2891 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002892
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002893 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002894
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002895 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002896 struct xfrm_policy_hash *htab;
2897
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002898 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002899
2900 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01002901 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002902 WARN_ON(!hlist_empty(htab->table));
2903 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002904 }
2905
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002906 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002907 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2908 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002909}
2910
2911static int __net_init xfrm_net_init(struct net *net)
2912{
2913 int rv;
2914
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002915 rv = xfrm_statistics_init(net);
2916 if (rv < 0)
2917 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002918 rv = xfrm_state_init(net);
2919 if (rv < 0)
2920 goto out_state;
2921 rv = xfrm_policy_init(net);
2922 if (rv < 0)
2923 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002924 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002925 rv = xfrm_sysctl_init(net);
2926 if (rv < 0)
2927 goto out_sysctl;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002928 return 0;
2929
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002930out_sysctl:
2931 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002932out_policy:
2933 xfrm_state_fini(net);
2934out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002935 xfrm_statistics_fini(net);
2936out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002937 return rv;
2938}
2939
2940static void __net_exit xfrm_net_exit(struct net *net)
2941{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002942 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002943 xfrm_policy_fini(net);
2944 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002945 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002946}
2947
2948static struct pernet_operations __net_initdata xfrm_net_ops = {
2949 .init = xfrm_net_init,
2950 .exit = xfrm_net_exit,
2951};
2952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953void __init xfrm_init(void)
2954{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002955 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 xfrm_input_init();
2957}
2958
Joy Lattenab5f5e82007-09-17 11:51:22 -07002959#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08002960static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2961 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002962{
Paul Moore875179f2007-12-01 23:27:18 +11002963 struct xfrm_sec_ctx *ctx = xp->security;
2964 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002965
Paul Moore875179f2007-12-01 23:27:18 +11002966 if (ctx)
2967 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2968 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2969
2970 switch(sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002971 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002972 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002973 if (sel->prefixlen_s != 32)
2974 audit_log_format(audit_buf, " src_prefixlen=%d",
2975 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002976 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002977 if (sel->prefixlen_d != 32)
2978 audit_log_format(audit_buf, " dst_prefixlen=%d",
2979 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002980 break;
2981 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002982 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002983 if (sel->prefixlen_s != 128)
2984 audit_log_format(audit_buf, " src_prefixlen=%d",
2985 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002986 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002987 if (sel->prefixlen_d != 128)
2988 audit_log_format(audit_buf, " dst_prefixlen=%d",
2989 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002990 break;
2991 }
2992}
2993
Paul Moore68277ac2007-12-20 20:49:33 -08002994void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002995 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002996{
2997 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002998
Paul Mooreafeb14b2007-12-21 14:58:11 -08002999 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003000 if (audit_buf == NULL)
3001 return;
Eric Paris25323862008-04-18 10:09:25 -04003002 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003003 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003004 xfrm_audit_common_policyinfo(xp, audit_buf);
3005 audit_log_end(audit_buf);
3006}
3007EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3008
Paul Moore68277ac2007-12-20 20:49:33 -08003009void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07003010 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003011{
3012 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003013
Paul Mooreafeb14b2007-12-21 14:58:11 -08003014 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003015 if (audit_buf == NULL)
3016 return;
Eric Paris25323862008-04-18 10:09:25 -04003017 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003018 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003019 xfrm_audit_common_policyinfo(xp, audit_buf);
3020 audit_log_end(audit_buf);
3021}
3022EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3023#endif
3024
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003025#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003026static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3027 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003028{
3029 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3030 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003031 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3032 sel_cmp->family) &&
3033 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3034 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003035 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3036 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003037 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003038 }
3039 } else {
3040 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003041 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003042 }
3043 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003044 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003045}
3046
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003047static struct xfrm_policy * xfrm_migrate_policy_find(const struct xfrm_selector *sel,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003048 u8 dir, u8 type)
3049{
3050 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003051 struct hlist_head *chain;
3052 u32 priority = ~0U;
3053
3054 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -08003055 chain = policy_hash_direct(&init_net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003056 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003057 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3058 pol->type == type) {
3059 ret = pol;
3060 priority = ret->priority;
3061 break;
3062 }
3063 }
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003064 chain = &init_net.xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003065 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003066 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3067 pol->type == type &&
3068 pol->priority < priority) {
3069 ret = pol;
3070 break;
3071 }
3072 }
3073
3074 if (ret)
3075 xfrm_pol_hold(ret);
3076
3077 read_unlock_bh(&xfrm_policy_lock);
3078
3079 return ret;
3080}
3081
David S. Millerdd701752011-02-24 00:21:08 -05003082static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003083{
3084 int match = 0;
3085
3086 if (t->mode == m->mode && t->id.proto == m->proto &&
3087 (m->reqid == 0 || t->reqid == m->reqid)) {
3088 switch (t->mode) {
3089 case XFRM_MODE_TUNNEL:
3090 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003091 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3092 m->old_family) &&
3093 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3094 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003095 match = 1;
3096 }
3097 break;
3098 case XFRM_MODE_TRANSPORT:
3099 /* in case of transport mode, template does not store
3100 any IP addresses, hence we just compare mode and
3101 protocol */
3102 match = 1;
3103 break;
3104 default:
3105 break;
3106 }
3107 }
3108 return match;
3109}
3110
3111/* update endpoint address(es) of template(s) */
3112static int xfrm_policy_migrate(struct xfrm_policy *pol,
3113 struct xfrm_migrate *m, int num_migrate)
3114{
3115 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003116 int i, j, n = 0;
3117
3118 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003119 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003120 /* target policy has been deleted */
3121 write_unlock_bh(&pol->lock);
3122 return -ENOENT;
3123 }
3124
3125 for (i = 0; i < pol->xfrm_nr; i++) {
3126 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3127 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3128 continue;
3129 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003130 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3131 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003132 continue;
3133 /* update endpoints */
3134 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3135 sizeof(pol->xfrm_vec[i].id.daddr));
3136 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3137 sizeof(pol->xfrm_vec[i].saddr));
3138 pol->xfrm_vec[i].encap_family = mp->new_family;
3139 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003140 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003141 }
3142 }
3143
3144 write_unlock_bh(&pol->lock);
3145
3146 if (!n)
3147 return -ENODATA;
3148
3149 return 0;
3150}
3151
David S. Millerdd701752011-02-24 00:21:08 -05003152static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003153{
3154 int i, j;
3155
3156 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3157 return -EINVAL;
3158
3159 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003160 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3161 m[i].old_family) &&
3162 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3163 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003164 return -EINVAL;
3165 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3166 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3167 return -EINVAL;
3168
3169 /* check if there is any duplicated entry */
3170 for (j = i + 1; j < num_migrate; j++) {
3171 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3172 sizeof(m[i].old_daddr)) &&
3173 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3174 sizeof(m[i].old_saddr)) &&
3175 m[i].proto == m[j].proto &&
3176 m[i].mode == m[j].mode &&
3177 m[i].reqid == m[j].reqid &&
3178 m[i].old_family == m[j].old_family)
3179 return -EINVAL;
3180 }
3181 }
3182
3183 return 0;
3184}
3185
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003186int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003187 struct xfrm_migrate *m, int num_migrate,
3188 struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003189{
3190 int i, err, nx_cur = 0, nx_new = 0;
3191 struct xfrm_policy *pol = NULL;
3192 struct xfrm_state *x, *xc;
3193 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3194 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3195 struct xfrm_migrate *mp;
3196
3197 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3198 goto out;
3199
3200 /* Stage 1 - find policy */
3201 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
3202 err = -ENOENT;
3203 goto out;
3204 }
3205
3206 /* Stage 2 - find and update state(s) */
3207 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
3208 if ((x = xfrm_migrate_state_find(mp))) {
3209 x_cur[nx_cur] = x;
3210 nx_cur++;
3211 if ((xc = xfrm_state_migrate(x, mp))) {
3212 x_new[nx_new] = xc;
3213 nx_new++;
3214 } else {
3215 err = -ENODATA;
3216 goto restore_state;
3217 }
3218 }
3219 }
3220
3221 /* Stage 3 - update policy */
3222 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3223 goto restore_state;
3224
3225 /* Stage 4 - delete old state(s) */
3226 if (nx_cur) {
3227 xfrm_states_put(x_cur, nx_cur);
3228 xfrm_states_delete(x_cur, nx_cur);
3229 }
3230
3231 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003232 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003233
3234 xfrm_pol_put(pol);
3235
3236 return 0;
3237out:
3238 return err;
3239
3240restore_state:
3241 if (pol)
3242 xfrm_pol_put(pol);
3243 if (nx_cur)
3244 xfrm_states_put(x_cur, nx_cur);
3245 if (nx_new)
3246 xfrm_states_delete(x_new, nx_new);
3247
3248 return err;
3249}
David S. Millere610e672007-02-08 13:29:15 -08003250EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003251#endif