blob: e09edfcf1b796e982e06f0e96e080f7bc0893ec9 [file] [log] [blame]
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * xfrm_policy.c
3 *
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * Kazunori MIYAZAWA @USAGI
10 * YOSHIFUJI Hideaki
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
Trent Jaegerdf718372005-12-13 23:12:27 -080013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Herbert Xu66cdb3c2007-11-13 21:37:28 -080016#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/slab.h>
18#include <linux/kmod.h>
19#include <linux/list.h>
20#include <linux/spinlock.h>
21#include <linux/workqueue.h>
22#include <linux/notifier.h>
23#include <linux/netdevice.h>
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -080024#include <linux/netfilter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
David S. Miller2518c7c2006-08-24 04:45:07 -070026#include <linux/cache.h>
Paul Moore68277ac2007-12-20 20:49:33 -080027#include <linux/audit.h>
Herbert Xu25ee3282007-12-11 09:32:34 -080028#include <net/dst.h>
Eric Paris6ce74ec2012-02-16 15:08:39 -050029#include <net/flow.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <net/xfrm.h>
31#include <net/ip.h>
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -080032#ifdef CONFIG_XFRM_STATISTICS
33#include <net/snmp.h>
34#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
David S. Miller44e36b42006-08-24 04:50:50 -070036#include "xfrm_hash.h"
37
Steffen Klasserta0073fe2013-02-05 12:52:55 +010038#define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
39#define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
40#define XFRM_MAX_QUEUE_LEN 100
41
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080042DEFINE_MUTEX(xfrm_cfg_mutex);
43EXPORT_SYMBOL(xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Timo Teräs80c802f2010-04-07 00:30:05 +000045static DEFINE_SPINLOCK(xfrm_policy_sk_bundle_lock);
46static struct dst_entry *xfrm_policy_sk_bundles;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static DEFINE_RWLOCK(xfrm_policy_lock);
48
Priyanka Jain418a99a2012-08-12 21:22:29 +000049static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
50static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO]
51 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Christoph Lametere18b8902006-12-06 20:33:20 -080053static struct kmem_cache *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Herbert Xu25ee3282007-12-11 09:32:34 -080055static void xfrm_init_pmtu(struct dst_entry *dst);
Timo Teräs80c802f2010-04-07 00:30:05 +000056static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +000057static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Steffen Klasserta0073fe2013-02-05 12:52:55 +010058static void xfrm_policy_queue_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Wei Yongjun29fa0b302008-12-03 00:33:09 -080060static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
61 int dir);
62
David S. Millerbc9b35a2012-05-15 15:04:57 -040063static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050064__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080065{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050066 const struct flowi4 *fl4 = &fl->u.ip4;
67
Alexey Dobriyan26bff942011-11-22 06:46:02 +000068 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
69 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -050070 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
71 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
72 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
73 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080074}
75
David S. Millerbc9b35a2012-05-15 15:04:57 -040076static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050077__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080078{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050079 const struct flowi6 *fl6 = &fl->u.ip6;
80
81 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
82 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
83 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
84 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
85 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
86 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080087}
88
David S. Millerbc9b35a2012-05-15 15:04:57 -040089bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
90 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -080091{
92 switch (family) {
93 case AF_INET:
94 return __xfrm4_selector_match(sel, fl);
95 case AF_INET6:
96 return __xfrm6_selector_match(sel, fl);
97 }
David S. Millerbc9b35a2012-05-15 15:04:57 -040098 return false;
Andrew Morton77681022006-11-08 22:46:26 -080099}
100
Eric Dumazetef8531b2012-08-19 12:31:48 +0200101static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
102{
103 struct xfrm_policy_afinfo *afinfo;
104
105 if (unlikely(family >= NPROTO))
106 return NULL;
107 rcu_read_lock();
108 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
109 if (unlikely(!afinfo))
110 rcu_read_unlock();
111 return afinfo;
112}
113
114static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
115{
116 rcu_read_unlock();
117}
118
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800119static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos,
David S. Miller6418c4e2011-02-24 00:16:53 -0500120 const xfrm_address_t *saddr,
121 const xfrm_address_t *daddr,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900122 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800124 struct xfrm_policy_afinfo *afinfo;
125 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Herbert Xu25ee3282007-12-11 09:32:34 -0800127 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800129 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800131 dst = afinfo->dst_lookup(net, tos, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900134
135 return dst;
136}
137
138static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
139 xfrm_address_t *prev_saddr,
140 xfrm_address_t *prev_daddr,
141 int family)
142{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800143 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900144 xfrm_address_t *saddr = &x->props.saddr;
145 xfrm_address_t *daddr = &x->id.daddr;
146 struct dst_entry *dst;
147
148 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
149 saddr = x->coaddr;
150 daddr = prev_daddr;
151 }
152 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
153 saddr = prev_saddr;
154 daddr = x->coaddr;
155 }
156
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800157 dst = __xfrm_dst_lookup(net, tos, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900158
159 if (!IS_ERR(dst)) {
160 if (prev_saddr != saddr)
161 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
162 if (prev_daddr != daddr)
163 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
164 }
165
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800166 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static inline unsigned long make_jiffies(long secs)
170{
171 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
172 return MAX_SCHEDULE_TIMEOUT-1;
173 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900174 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177static void xfrm_policy_timer(unsigned long data)
178{
179 struct xfrm_policy *xp = (struct xfrm_policy*)data;
James Morris9d729f72007-03-04 16:12:44 -0800180 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 long next = LONG_MAX;
182 int warn = 0;
183 int dir;
184
185 read_lock(&xp->lock);
186
Timo Teräsea2dea92010-03-31 00:17:05 +0000187 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 goto out;
189
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700190 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 if (xp->lft.hard_add_expires_seconds) {
193 long tmo = xp->lft.hard_add_expires_seconds +
194 xp->curlft.add_time - now;
195 if (tmo <= 0)
196 goto expired;
197 if (tmo < next)
198 next = tmo;
199 }
200 if (xp->lft.hard_use_expires_seconds) {
201 long tmo = xp->lft.hard_use_expires_seconds +
202 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
203 if (tmo <= 0)
204 goto expired;
205 if (tmo < next)
206 next = tmo;
207 }
208 if (xp->lft.soft_add_expires_seconds) {
209 long tmo = xp->lft.soft_add_expires_seconds +
210 xp->curlft.add_time - now;
211 if (tmo <= 0) {
212 warn = 1;
213 tmo = XFRM_KM_TIMEOUT;
214 }
215 if (tmo < next)
216 next = tmo;
217 }
218 if (xp->lft.soft_use_expires_seconds) {
219 long tmo = xp->lft.soft_use_expires_seconds +
220 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
221 if (tmo <= 0) {
222 warn = 1;
223 tmo = XFRM_KM_TIMEOUT;
224 }
225 if (tmo < next)
226 next = tmo;
227 }
228
229 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800230 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (next != LONG_MAX &&
232 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
233 xfrm_pol_hold(xp);
234
235out:
236 read_unlock(&xp->lock);
237 xfrm_pol_put(xp);
238 return;
239
240expired:
241 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700242 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800243 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 xfrm_pol_put(xp);
245}
246
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000247static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
248{
249 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
250
251 if (unlikely(pol->walk.dead))
252 flo = NULL;
253 else
254 xfrm_pol_hold(pol);
255
256 return flo;
257}
258
259static int xfrm_policy_flo_check(struct flow_cache_object *flo)
260{
261 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
262
263 return !pol->walk.dead;
264}
265
266static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
267{
268 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
269}
270
271static const struct flow_cache_ops xfrm_policy_fc_ops = {
272 .get = xfrm_policy_flo_get,
273 .check = xfrm_policy_flo_check,
274 .delete = xfrm_policy_flo_delete,
275};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
278 * SPD calls.
279 */
280
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800281struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct xfrm_policy *policy;
284
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700285 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800288 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700289 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700290 INIT_HLIST_NODE(&policy->bydst);
291 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700293 atomic_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100294 skb_queue_head_init(&policy->polq.hold_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800295 setup_timer(&policy->timer, xfrm_policy_timer,
296 (unsigned long)policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100297 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
298 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000299 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301 return policy;
302}
303EXPORT_SYMBOL(xfrm_policy_alloc);
304
305/* Destroy xfrm_policy: descendant resources must be released to this moment. */
306
WANG Cong64c31b32008-01-07 22:34:29 -0800307void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Herbert Xu12a169e2008-10-01 07:03:24 -0700309 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Fan Du0659eea2013-08-01 18:08:36 +0800311 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 BUG();
313
Paul Moore03e1ad72008-04-12 19:07:52 -0700314 security_xfrm_policy_free(policy->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 kfree(policy);
316}
WANG Cong64c31b32008-01-07 22:34:29 -0800317EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100319static void xfrm_queue_purge(struct sk_buff_head *list)
320{
321 struct sk_buff *skb;
322
Steffen Klassert302a50b2013-08-28 08:47:14 +0200323 while ((skb = skb_dequeue(list)) != NULL)
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100324 kfree_skb(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/* Rule must be locked. Release descentant resources, announce
328 * entry dead. The rule must be unlinked from lists to the moment.
329 */
330
331static void xfrm_policy_kill(struct xfrm_policy *policy)
332{
Herbert Xu12a169e2008-10-01 07:03:24 -0700333 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Timo Teräs285ead12010-04-07 00:30:06 +0000335 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Steffen 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);
fan.duca4c3fc2013-07-30 08:33:53 +0800661
662 /* After previous checking, family can either be AF_INET or AF_INET6 */
663 if (policy->family == AF_INET)
664 rt_genid_bump_ipv4(net);
665 else
666 rt_genid_bump_ipv6(net);
667
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100668 if (delpol) {
669 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800670 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100671 }
Alexey Dobriyan11219942008-11-25 17:33:06 -0800672 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir);
673 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800674 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 policy->curlft.use_time = 0;
676 if (!mod_timer(&policy->timer, jiffies + HZ))
677 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800678 list_add(&policy->walk.all, &net->xfrm.policy_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 write_unlock_bh(&xfrm_policy_lock);
680
David S. Miller9b78a822005-12-22 07:39:48 -0800681 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800683 else if (xfrm_bydst_should_resize(net, dir, NULL))
684 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return 0;
687}
688EXPORT_SYMBOL(xfrm_policy_insert);
689
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000690struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
691 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800692 struct xfrm_sec_ctx *ctx, int delete,
693 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
David S. Miller2518c7c2006-08-24 04:45:07 -0700695 struct xfrm_policy *pol, *ret;
696 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Eric Parisef41aaa2007-03-07 15:37:58 -0800698 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800700 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700701 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800702 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700703 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000704 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700705 !selector_cmp(sel, &pol->selector) &&
706 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700708 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700709 *err = security_xfrm_policy_delete(
710 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800711 if (*err) {
712 write_unlock_bh(&xfrm_policy_lock);
713 return pol;
714 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800715 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700716 }
717 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 break;
719 }
720 }
721 write_unlock_bh(&xfrm_policy_lock);
722
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000723 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700724 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700725 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
Trent Jaegerdf718372005-12-13 23:12:27 -0800727EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000729struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
730 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
David S. Miller2518c7c2006-08-24 04:45:07 -0700732 struct xfrm_policy *pol, *ret;
733 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Herbert Xub5505c62007-05-14 02:15:47 -0700735 *err = -ENOENT;
736 if (xfrm_policy_id2dir(id) != dir)
737 return NULL;
738
Eric Parisef41aaa2007-03-07 15:37:58 -0800739 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800741 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700742 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800743 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000744 if (pol->type == type && pol->index == id &&
745 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700747 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700748 *err = security_xfrm_policy_delete(
749 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800750 if (*err) {
751 write_unlock_bh(&xfrm_policy_lock);
752 return pol;
753 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800754 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700755 }
756 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 break;
758 }
759 }
760 write_unlock_bh(&xfrm_policy_lock);
761
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000762 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700763 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700764 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766EXPORT_SYMBOL(xfrm_policy_byid);
767
Joy Latten4aa2e622007-06-04 19:05:57 -0400768#ifdef CONFIG_SECURITY_NETWORK_XFRM
769static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800770xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Joy Latten4aa2e622007-06-04 19:05:57 -0400772 int dir, err = 0;
773
774 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
775 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400776 int i;
777
Sasha Levinb67bfe02013-02-27 17:06:00 -0800778 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800779 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400780 if (pol->type != type)
781 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700782 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400783 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700784 xfrm_audit_policy_delete(pol, 0,
785 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400786 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700787 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400788 return err;
789 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900790 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800791 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800792 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800793 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400794 bydst) {
795 if (pol->type != type)
796 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700797 err = security_xfrm_policy_delete(
798 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400799 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700800 xfrm_audit_policy_delete(pol, 0,
801 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400802 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700803 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400804 return err;
805 }
806 }
807 }
808 }
809 return err;
810}
811#else
812static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800813xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400814{
815 return 0;
816}
817#endif
818
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800819int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400820{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000821 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 write_lock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400824
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800825 err = xfrm_policy_flush_secctx_check(net, type, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400826 if (err)
827 goto out;
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700830 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800831 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700832
833 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800834 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800835 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700836 if (pol->type != type)
837 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000838 __xfrm_policy_unlink(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000840 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Joy Lattenab5f5e82007-09-17 11:51:22 -0700842 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400843 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700844 audit_info->secid);
Joy Latten161a09e2006-11-27 13:11:54 -0600845
David S. Miller2518c7c2006-08-24 04:45:07 -0700846 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700849 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700851
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800852 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700853 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800854 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800855 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700856 bydst) {
857 if (pol->type != type)
858 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000859 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700860 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000861 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700862
Joy Lattenab5f5e82007-09-17 11:51:22 -0700863 xfrm_audit_policy_delete(pol, 1,
864 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400865 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700866 audit_info->secid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700867 xfrm_policy_kill(pol);
868
869 write_lock_bh(&xfrm_policy_lock);
870 goto again2;
871 }
872 }
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000875 if (!cnt)
876 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400877out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 write_unlock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400879 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881EXPORT_SYMBOL(xfrm_policy_flush);
882
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800883int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800884 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 void *data)
886{
Herbert Xu12a169e2008-10-01 07:03:24 -0700887 struct xfrm_policy *pol;
888 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -0800889 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Timo Teras4c563f72008-02-28 21:31:08 -0800891 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
892 walk->type != XFRM_POLICY_TYPE_ANY)
893 return -EINVAL;
894
Herbert Xu12a169e2008-10-01 07:03:24 -0700895 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -0800896 return 0;
897
Herbert Xu12a169e2008-10-01 07:03:24 -0700898 write_lock_bh(&xfrm_policy_lock);
899 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800900 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -0700901 else
902 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800903 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -0700904 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -0800905 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -0700906 pol = container_of(x, struct xfrm_policy, walk);
907 if (walk->type != XFRM_POLICY_TYPE_ANY &&
908 walk->type != pol->type)
909 continue;
910 error = func(pol, xfrm_policy_id2dir(pol->index),
911 walk->seq, data);
912 if (error) {
913 list_move_tail(&walk->walk.all, &x->all);
914 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -0800915 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700916 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700918 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800919 error = -ENOENT;
920 goto out;
921 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700922 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923out:
Herbert Xu12a169e2008-10-01 07:03:24 -0700924 write_unlock_bh(&xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return error;
926}
927EXPORT_SYMBOL(xfrm_policy_walk);
928
Herbert Xu12a169e2008-10-01 07:03:24 -0700929void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
930{
931 INIT_LIST_HEAD(&walk->walk.all);
932 walk->walk.dead = 1;
933 walk->type = type;
934 walk->seq = 0;
935}
936EXPORT_SYMBOL(xfrm_policy_walk_init);
937
938void xfrm_policy_walk_done(struct xfrm_policy_walk *walk)
939{
940 if (list_empty(&walk->walk.all))
941 return;
942
943 write_lock_bh(&xfrm_policy_lock);
944 list_del(&walk->walk.all);
945 write_unlock_bh(&xfrm_policy_lock);
946}
947EXPORT_SYMBOL(xfrm_policy_walk_done);
948
James Morris134b0fc2006-10-05 15:42:27 -0500949/*
950 * Find policy to apply to this flow.
951 *
952 * Returns 0 if policy found, else an -errno.
953 */
David S. Millerf299d552011-02-24 01:23:30 -0500954static int xfrm_policy_match(const struct xfrm_policy *pol,
955 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -0700956 u8 type, u16 family, int dir)
957{
David S. Millerf299d552011-02-24 01:23:30 -0500958 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -0400959 int ret = -ESRCH;
960 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -0700961
962 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -0500963 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -0700964 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500965 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700966
967 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500968 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -0500969 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -0700970 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700971
James Morris134b0fc2006-10-05 15:42:27 -0500972 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700973}
974
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800975static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -0800976 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700977 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
James Morris134b0fc2006-10-05 15:42:27 -0500979 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700980 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -0500981 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -0700982 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700983 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700984
985 daddr = xfrm_flowi_daddr(fl, family);
986 saddr = xfrm_flowi_saddr(fl, family);
987 if (unlikely(!daddr || !saddr))
988 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800991 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700992 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800993 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500994 err = xfrm_policy_match(pol, fl, type, family, dir);
995 if (err) {
996 if (err == -ESRCH)
997 continue;
998 else {
999 ret = ERR_PTR(err);
1000 goto fail;
1001 }
1002 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001003 ret = pol;
1004 priority = ret->priority;
1005 break;
1006 }
1007 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001008 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001009 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001010 err = xfrm_policy_match(pol, fl, type, family, dir);
1011 if (err) {
1012 if (err == -ESRCH)
1013 continue;
1014 else {
1015 ret = ERR_PTR(err);
1016 goto fail;
1017 }
1018 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001019 ret = pol;
1020 break;
1021 }
1022 }
David S. Milleracba48e2006-08-25 15:46:46 -07001023 if (ret)
1024 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -05001025fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001027
David S. Miller2518c7c2006-08-24 04:45:07 -07001028 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001029}
1030
Timo Teräs80c802f2010-04-07 00:30:05 +00001031static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001032__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001033{
1034#ifdef CONFIG_XFRM_SUB_POLICY
1035 struct xfrm_policy *pol;
1036
1037 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1038 if (pol != NULL)
1039 return pol;
1040#endif
1041 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1042}
1043
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001044static int flow_to_policy_dir(int dir)
1045{
1046 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1047 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1048 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1049 return dir;
1050
1051 switch (dir) {
1052 default:
1053 case FLOW_DIR_IN:
1054 return XFRM_POLICY_IN;
1055 case FLOW_DIR_OUT:
1056 return XFRM_POLICY_OUT;
1057 case FLOW_DIR_FWD:
1058 return XFRM_POLICY_FWD;
1059 }
1060}
1061
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001062static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001063xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001064 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001065{
1066 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001067
1068 if (old_obj)
1069 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001070
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001071 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001072 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001073 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001074
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001075 /* Resolver returns two references:
1076 * one for cache and one for caller of flow_cache_lookup() */
1077 xfrm_pol_hold(pol);
1078
1079 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Trent Jaegerdf718372005-12-13 23:12:27 -08001082static inline int policy_to_flow_dir(int dir)
1083{
1084 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001085 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1086 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1087 return dir;
1088 switch (dir) {
1089 default:
1090 case XFRM_POLICY_IN:
1091 return FLOW_DIR_IN;
1092 case XFRM_POLICY_OUT:
1093 return FLOW_DIR_OUT;
1094 case XFRM_POLICY_FWD:
1095 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001096 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001097}
1098
David S. Millerdee9f4b2011-02-22 18:44:31 -08001099static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir,
1100 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 struct xfrm_policy *pol;
1103
1104 read_lock_bh(&xfrm_policy_lock);
1105 if ((pol = sk->sk_policy[dir]) != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001106 bool match = xfrm_selector_match(&pol->selector, fl,
1107 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001108 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001109
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001110 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001111 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1112 pol = NULL;
1113 goto out;
1114 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001115 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001116 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001117 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001118 if (!err)
1119 xfrm_pol_hold(pol);
1120 else if (err == -ESRCH)
1121 pol = NULL;
1122 else
1123 pol = ERR_PTR(err);
1124 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 pol = NULL;
1126 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001127out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 read_unlock_bh(&xfrm_policy_lock);
1129 return pol;
1130}
1131
1132static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1133{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001134 struct net *net = xp_net(pol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001135 struct hlist_head *chain = policy_hash_bysel(net, &pol->selector,
David S. Miller2518c7c2006-08-24 04:45:07 -07001136 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001137
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001138 list_add(&pol->walk.all, &net->xfrm.policy_all);
David S. Miller2518c7c2006-08-24 04:45:07 -07001139 hlist_add_head(&pol->bydst, chain);
Alexey Dobriyane92303f2008-11-25 17:32:41 -08001140 hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001141 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001143
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001144 if (xfrm_bydst_should_resize(net, dir, NULL))
1145 schedule_work(&net->xfrm.policy_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
1148static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1149 int dir)
1150{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001151 struct net *net = xp_net(pol);
1152
David S. Miller2518c7c2006-08-24 04:45:07 -07001153 if (hlist_unhashed(&pol->bydst))
1154 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
David S. Miller2518c7c2006-08-24 04:45:07 -07001156 hlist_del(&pol->bydst);
1157 hlist_del(&pol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -07001158 list_del(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001159 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001160
1161 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
Herbert Xu4666faa2005-06-18 22:43:22 -07001164int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 write_lock_bh(&xfrm_policy_lock);
1167 pol = __xfrm_policy_unlink(pol, dir);
1168 write_unlock_bh(&xfrm_policy_lock);
1169 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001173 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
David S. Millera70fcb02006-03-20 19:18:52 -08001175EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1178{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001179 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 struct xfrm_policy *old_pol;
1181
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001182#ifdef CONFIG_XFRM_SUB_POLICY
1183 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1184 return -EINVAL;
1185#endif
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 write_lock_bh(&xfrm_policy_lock);
1188 old_pol = sk->sk_policy[dir];
1189 sk->sk_policy[dir] = pol;
1190 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001191 pol->curlft.add_time = get_seconds();
Alexey Dobriyan11219942008-11-25 17:33:06 -08001192 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1194 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001195 if (old_pol) {
1196 if (pol)
1197 xfrm_policy_requeue(old_pol, pol);
1198
Timo Teräsea2dea92010-03-31 00:17:05 +00001199 /* Unlinking succeeds always. This is the only function
1200 * allowed to delete or replace socket policy.
1201 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 write_unlock_bh(&xfrm_policy_lock);
1205
1206 if (old_pol) {
1207 xfrm_policy_kill(old_pol);
1208 }
1209 return 0;
1210}
1211
David S. Millerd3e40a92011-02-24 01:25:41 -05001212static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001214 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 if (newp) {
1217 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001218 if (security_xfrm_policy_clone(old->security,
1219 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001220 kfree(newp);
1221 return NULL; /* ENOMEM */
1222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 newp->lft = old->lft;
1224 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001225 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 newp->action = old->action;
1227 newp->flags = old->flags;
1228 newp->xfrm_nr = old->xfrm_nr;
1229 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001230 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 memcpy(newp->xfrm_vec, old->xfrm_vec,
1232 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1233 write_lock_bh(&xfrm_policy_lock);
1234 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1235 write_unlock_bh(&xfrm_policy_lock);
1236 xfrm_pol_put(newp);
1237 }
1238 return newp;
1239}
1240
1241int __xfrm_sk_clone_policy(struct sock *sk)
1242{
1243 struct xfrm_policy *p0 = sk->sk_policy[0],
1244 *p1 = sk->sk_policy[1];
1245
1246 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1247 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1248 return -ENOMEM;
1249 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1250 return -ENOMEM;
1251 return 0;
1252}
1253
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001254static int
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001255xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001256 unsigned short family)
1257{
1258 int err;
1259 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1260
1261 if (unlikely(afinfo == NULL))
1262 return -EINVAL;
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001263 err = afinfo->get_saddr(net, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001264 xfrm_policy_put_afinfo(afinfo);
1265 return err;
1266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268/* Resolve list of templates for the flow, given policy. */
1269
1270static int
David S. Millera6c2e612011-02-22 18:35:39 -08001271xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1272 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001274 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int nx;
1276 int i, error;
1277 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1278 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001279 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1282 struct xfrm_state *x;
1283 xfrm_address_t *remote = daddr;
1284 xfrm_address_t *local = saddr;
1285 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1286
Joakim Koskela48b8d782007-07-26 00:08:42 -07001287 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1288 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 remote = &tmpl->id.daddr;
1290 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001291 if (xfrm_addr_any(local, tmpl->encap_family)) {
1292 error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001293 if (error)
1294 goto fail;
1295 local = &tmp;
1296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298
1299 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1300
1301 if (x && x->km.state == XFRM_STATE_VALID) {
1302 xfrm[nx++] = x;
1303 daddr = remote;
1304 saddr = local;
1305 continue;
1306 }
1307 if (x) {
1308 error = (x->km.state == XFRM_STATE_ERROR ?
1309 -EINVAL : -EAGAIN);
1310 xfrm_state_put(x);
1311 }
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001312 else if (error == -ESRCH)
1313 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 if (!tmpl->optional)
1316 goto fail;
1317 }
1318 return nx;
1319
1320fail:
1321 for (nx--; nx>=0; nx--)
1322 xfrm_state_put(xfrm[nx]);
1323 return error;
1324}
1325
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001326static int
David S. Millera6c2e612011-02-22 18:35:39 -08001327xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1328 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001329{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001330 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1331 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001332 int cnx = 0;
1333 int error;
1334 int ret;
1335 int i;
1336
1337 for (i = 0; i < npols; i++) {
1338 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1339 error = -ENOBUFS;
1340 goto fail;
1341 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001342
1343 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001344 if (ret < 0) {
1345 error = ret;
1346 goto fail;
1347 } else
1348 cnx += ret;
1349 }
1350
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001351 /* found states are sorted for outbound processing */
1352 if (npols > 1)
1353 xfrm_state_sort(xfrm, tpp, cnx, family);
1354
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001355 return cnx;
1356
1357 fail:
1358 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001359 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001360 return error;
1361
1362}
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364/* Check that the bundle accepts the flow and its components are
1365 * still valid.
1366 */
1367
David S. Miller05d84022011-02-22 17:47:10 -08001368static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001369{
1370 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1371 int tos;
1372
1373 if (!afinfo)
1374 return -EINVAL;
1375
1376 tos = afinfo->get_tos(fl);
1377
1378 xfrm_policy_put_afinfo(afinfo);
1379
1380 return tos;
1381}
1382
Timo Teräs80c802f2010-04-07 00:30:05 +00001383static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1384{
1385 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1386 struct dst_entry *dst = &xdst->u.dst;
1387
1388 if (xdst->route == NULL) {
1389 /* Dummy bundle - if it has xfrms we were not
1390 * able to build bundle as template resolution failed.
1391 * It means we need to try again resolving. */
1392 if (xdst->num_xfrms > 0)
1393 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001394 } else if (dst->flags & DST_XFRM_QUEUE) {
1395 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001396 } else {
1397 /* Real bundle */
1398 if (stale_bundle(dst))
1399 return NULL;
1400 }
1401
1402 dst_hold(dst);
1403 return flo;
1404}
1405
1406static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1407{
1408 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1409 struct dst_entry *dst = &xdst->u.dst;
1410
1411 if (!xdst->route)
1412 return 0;
1413 if (stale_bundle(dst))
1414 return 0;
1415
1416 return 1;
1417}
1418
1419static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1420{
1421 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1422 struct dst_entry *dst = &xdst->u.dst;
1423
1424 dst_free(dst);
1425}
1426
1427static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1428 .get = xfrm_bundle_flo_get,
1429 .check = xfrm_bundle_flo_check,
1430 .delete = xfrm_bundle_flo_delete,
1431};
1432
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001433static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001434{
1435 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001436 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001437 struct xfrm_dst *xdst;
1438
1439 if (!afinfo)
1440 return ERR_PTR(-EINVAL);
1441
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001442 switch (family) {
1443 case AF_INET:
1444 dst_ops = &net->xfrm.xfrm4_dst_ops;
1445 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001446#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001447 case AF_INET6:
1448 dst_ops = &net->xfrm.xfrm6_dst_ops;
1449 break;
1450#endif
1451 default:
1452 BUG();
1453 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001454 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001455
Madalin Bucurd4cae562011-09-26 07:04:36 +00001456 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001457 struct dst_entry *dst = &xdst->u.dst;
1458
1459 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001460 xdst->flo.ops = &xfrm_bundle_fc_ops;
Patrick McHardy9d7b0fc2012-08-20 02:56:56 -07001461 if (afinfo->init_dst)
1462 afinfo->init_dst(net, xdst);
Madalin Bucurd4cae562011-09-26 07:04:36 +00001463 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001464 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001465
Madalin Bucurd4cae562011-09-26 07:04:36 +00001466 xfrm_policy_put_afinfo(afinfo);
1467
Herbert Xu25ee3282007-12-11 09:32:34 -08001468 return xdst;
1469}
1470
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001471static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1472 int nfheader_len)
1473{
1474 struct xfrm_policy_afinfo *afinfo =
1475 xfrm_policy_get_afinfo(dst->ops->family);
1476 int err;
1477
1478 if (!afinfo)
1479 return -EINVAL;
1480
1481 err = afinfo->init_path(path, dst, nfheader_len);
1482
1483 xfrm_policy_put_afinfo(afinfo);
1484
1485 return err;
1486}
1487
Herbert Xu87c1e122010-03-02 02:51:56 +00001488static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001489 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001490{
1491 struct xfrm_policy_afinfo *afinfo =
1492 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1493 int err;
1494
1495 if (!afinfo)
1496 return -EINVAL;
1497
Herbert Xu87c1e122010-03-02 02:51:56 +00001498 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001499
1500 xfrm_policy_put_afinfo(afinfo);
1501
1502 return err;
1503}
1504
Timo Teräs80c802f2010-04-07 00:30:05 +00001505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1507 * all the metrics... Shortly, bundle a bundle.
1508 */
1509
Herbert Xu25ee3282007-12-11 09:32:34 -08001510static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1511 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001512 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001513 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001515 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001516 unsigned long now = jiffies;
1517 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001518 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001519 struct dst_entry *dst_prev = NULL;
1520 struct dst_entry *dst0 = NULL;
1521 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001523 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001524 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001525 int trailer_len = 0;
1526 int tos;
1527 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001528 xfrm_address_t saddr, daddr;
1529
1530 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001531
1532 tos = xfrm_get_tos(fl, family);
1533 err = tos;
1534 if (tos < 0)
1535 goto put_states;
1536
1537 dst_hold(dst);
1538
1539 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001540 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001541 struct dst_entry *dst1 = &xdst->u.dst;
1542
1543 err = PTR_ERR(xdst);
1544 if (IS_ERR(xdst)) {
1545 dst_release(dst);
1546 goto put_states;
1547 }
1548
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001549 if (xfrm[i]->sel.family == AF_UNSPEC) {
1550 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1551 xfrm_af2proto(family));
1552 if (!inner_mode) {
1553 err = -EAFNOSUPPORT;
1554 dst_release(dst);
1555 goto put_states;
1556 }
1557 } else
1558 inner_mode = xfrm[i]->inner_mode;
1559
Herbert Xu25ee3282007-12-11 09:32:34 -08001560 if (!dst_prev)
1561 dst0 = dst1;
1562 else {
1563 dst_prev->child = dst_clone(dst1);
1564 dst1->flags |= DST_NOHASH;
1565 }
1566
1567 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001568 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001569
1570 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1571 family = xfrm[i]->props.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001572 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1573 family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001574 err = PTR_ERR(dst);
1575 if (IS_ERR(dst))
1576 goto put_states;
1577 } else
1578 dst_hold(dst);
1579
1580 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001581 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001582
David S. Millerf5b0a872012-07-19 12:31:33 -07001583 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001584 dst1->flags |= DST_HOST;
1585 dst1->lastuse = now;
1586
1587 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001588 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001589
1590 dst1->next = dst_prev;
1591 dst_prev = dst1;
1592
1593 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001594 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1595 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001596 trailer_len += xfrm[i]->props.trailer_len;
1597 }
1598
1599 dst_prev->child = dst;
1600 dst0->path = dst;
1601
1602 err = -ENODEV;
1603 dev = dst->dev;
1604 if (!dev)
1605 goto free_dst;
1606
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001607 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001608 xfrm_init_pmtu(dst_prev);
1609
1610 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1611 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1612
Herbert Xu87c1e122010-03-02 02:51:56 +00001613 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001614 if (err)
1615 goto free_dst;
1616
1617 dst_prev->header_len = header_len;
1618 dst_prev->trailer_len = trailer_len;
1619 header_len -= xdst->u.dst.xfrm->props.header_len;
1620 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1621 }
1622
1623out:
1624 return dst0;
1625
1626put_states:
1627 for (; i < nx; i++)
1628 xfrm_state_put(xfrm[i]);
1629free_dst:
1630 if (dst0)
1631 dst_free(dst0);
1632 dst0 = ERR_PTR(err);
1633 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001636static int inline
David S. Miller3f0e18f2011-02-22 18:38:14 -08001637xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001638{
1639 if (!*target) {
1640 *target = kmalloc(size, GFP_ATOMIC);
1641 if (!*target)
1642 return -ENOMEM;
1643 }
1644 memcpy(*target, src, size);
1645 return 0;
1646}
1647
1648static int inline
David S. Miller1786b382011-02-24 01:32:54 -05001649xfrm_dst_update_parent(struct dst_entry *dst, const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001650{
1651#ifdef CONFIG_XFRM_SUB_POLICY
1652 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1653 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1654 sel, sizeof(*sel));
1655#else
1656 return 0;
1657#endif
1658}
1659
1660static int inline
David S. Miller3f0e18f2011-02-22 18:38:14 -08001661xfrm_dst_update_origin(struct dst_entry *dst, const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001662{
1663#ifdef CONFIG_XFRM_SUB_POLICY
1664 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1665 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1666#else
1667 return 0;
1668#endif
1669}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
David S. Miller73ff93c2011-02-22 18:33:42 -08001671static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001672 struct xfrm_policy **pols,
1673 int *num_pols, int *num_xfrms)
1674{
1675 int i;
1676
1677 if (*num_pols == 0 || !pols[0]) {
1678 *num_pols = 0;
1679 *num_xfrms = 0;
1680 return 0;
1681 }
1682 if (IS_ERR(pols[0]))
1683 return PTR_ERR(pols[0]);
1684
1685 *num_xfrms = pols[0]->xfrm_nr;
1686
1687#ifdef CONFIG_XFRM_SUB_POLICY
1688 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1689 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1690 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1691 XFRM_POLICY_TYPE_MAIN,
1692 fl, family,
1693 XFRM_POLICY_OUT);
1694 if (pols[1]) {
1695 if (IS_ERR(pols[1])) {
1696 xfrm_pols_put(pols, *num_pols);
1697 return PTR_ERR(pols[1]);
1698 }
1699 (*num_pols) ++;
1700 (*num_xfrms) += pols[1]->xfrm_nr;
1701 }
1702 }
1703#endif
1704 for (i = 0; i < *num_pols; i++) {
1705 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1706 *num_xfrms = -1;
1707 break;
1708 }
1709 }
1710
1711 return 0;
1712
1713}
1714
1715static struct xfrm_dst *
1716xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001717 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001718 struct dst_entry *dst_orig)
1719{
1720 struct net *net = xp_net(pols[0]);
1721 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1722 struct dst_entry *dst;
1723 struct xfrm_dst *xdst;
1724 int err;
1725
1726 /* Try to instantiate a bundle */
1727 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001728 if (err <= 0) {
1729 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001730 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1731 return ERR_PTR(err);
1732 }
1733
1734 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1735 if (IS_ERR(dst)) {
1736 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1737 return ERR_CAST(dst);
1738 }
1739
1740 xdst = (struct xfrm_dst *)dst;
1741 xdst->num_xfrms = err;
1742 if (num_pols > 1)
1743 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1744 else
1745 err = xfrm_dst_update_origin(dst, fl);
1746 if (unlikely(err)) {
1747 dst_free(dst);
1748 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1749 return ERR_PTR(err);
1750 }
1751
1752 xdst->num_pols = num_pols;
1753 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
1754 xdst->policy_genid = atomic_read(&pols[0]->genid);
1755
1756 return xdst;
1757}
1758
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001759static void xfrm_policy_queue_process(unsigned long arg)
1760{
1761 int err = 0;
1762 struct sk_buff *skb;
1763 struct sock *sk;
1764 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001765 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1766 struct xfrm_policy_queue *pq = &pol->polq;
1767 struct flowi fl;
1768 struct sk_buff_head list;
1769
1770 spin_lock(&pq->hold_queue.lock);
1771 skb = skb_peek(&pq->hold_queue);
1772 dst = skb_dst(skb);
1773 sk = skb->sk;
1774 xfrm_decode_session(skb, &fl, dst->ops->family);
1775 spin_unlock(&pq->hold_queue.lock);
1776
1777 dst_hold(dst->path);
1778 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1779 sk, 0);
1780 if (IS_ERR(dst))
1781 goto purge_queue;
1782
1783 if (dst->flags & DST_XFRM_QUEUE) {
1784 dst_release(dst);
1785
1786 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1787 goto purge_queue;
1788
1789 pq->timeout = pq->timeout << 1;
1790 mod_timer(&pq->hold_timer, jiffies + pq->timeout);
1791 return;
1792 }
1793
1794 dst_release(dst);
1795
1796 __skb_queue_head_init(&list);
1797
1798 spin_lock(&pq->hold_queue.lock);
1799 pq->timeout = 0;
1800 skb_queue_splice_init(&pq->hold_queue, &list);
1801 spin_unlock(&pq->hold_queue.lock);
1802
1803 while (!skb_queue_empty(&list)) {
1804 skb = __skb_dequeue(&list);
1805
1806 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1807 dst_hold(skb_dst(skb)->path);
1808 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1809 &fl, skb->sk, 0);
1810 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001811 kfree_skb(skb);
1812 continue;
1813 }
1814
1815 nf_reset(skb);
1816 skb_dst_drop(skb);
1817 skb_dst_set(skb, dst);
1818
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001819 err = dst_output(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001820 }
1821
1822 return;
1823
1824purge_queue:
1825 pq->timeout = 0;
1826 xfrm_queue_purge(&pq->hold_queue);
1827}
1828
1829static int xdst_queue_output(struct sk_buff *skb)
1830{
1831 unsigned long sched_next;
1832 struct dst_entry *dst = skb_dst(skb);
1833 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
1834 struct xfrm_policy_queue *pq = &xdst->pols[0]->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001835 const struct sk_buff *fclone = skb + 1;
1836
1837 if (unlikely(skb->fclone == SKB_FCLONE_ORIG &&
1838 fclone->fclone == SKB_FCLONE_CLONE)) {
1839 kfree_skb(skb);
1840 return 0;
1841 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001842
1843 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1844 kfree_skb(skb);
1845 return -EAGAIN;
1846 }
1847
1848 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001849
1850 spin_lock_bh(&pq->hold_queue.lock);
1851
1852 if (!pq->timeout)
1853 pq->timeout = XFRM_QUEUE_TMO_MIN;
1854
1855 sched_next = jiffies + pq->timeout;
1856
1857 if (del_timer(&pq->hold_timer)) {
1858 if (time_before(pq->hold_timer.expires, sched_next))
1859 sched_next = pq->hold_timer.expires;
1860 }
1861
1862 __skb_queue_tail(&pq->hold_queue, skb);
1863 mod_timer(&pq->hold_timer, sched_next);
1864
1865 spin_unlock_bh(&pq->hold_queue.lock);
1866
1867 return 0;
1868}
1869
1870static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
1871 struct dst_entry *dst,
1872 const struct flowi *fl,
1873 int num_xfrms,
1874 u16 family)
1875{
1876 int err;
1877 struct net_device *dev;
1878 struct dst_entry *dst1;
1879 struct xfrm_dst *xdst;
1880
1881 xdst = xfrm_alloc_dst(net, family);
1882 if (IS_ERR(xdst))
1883 return xdst;
1884
1885 if (net->xfrm.sysctl_larval_drop || num_xfrms <= 0 ||
1886 (fl->flowi_flags & FLOWI_FLAG_CAN_SLEEP))
1887 return xdst;
1888
1889 dst1 = &xdst->u.dst;
1890 dst_hold(dst);
1891 xdst->route = dst;
1892
1893 dst_copy_metrics(dst1, dst);
1894
1895 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1896 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
1897 dst1->lastuse = jiffies;
1898
1899 dst1->input = dst_discard;
1900 dst1->output = xdst_queue_output;
1901
1902 dst_hold(dst);
1903 dst1->child = dst;
1904 dst1->path = dst;
1905
1906 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
1907
1908 err = -ENODEV;
1909 dev = dst->dev;
1910 if (!dev)
1911 goto free_dst;
1912
1913 err = xfrm_fill_dst(xdst, dev, fl);
1914 if (err)
1915 goto free_dst;
1916
1917out:
1918 return xdst;
1919
1920free_dst:
1921 dst_release(dst1);
1922 xdst = ERR_PTR(err);
1923 goto out;
1924}
1925
Timo Teräs80c802f2010-04-07 00:30:05 +00001926static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001927xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00001928 struct flow_cache_object *oldflo, void *ctx)
1929{
1930 struct dst_entry *dst_orig = (struct dst_entry *)ctx;
1931 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1932 struct xfrm_dst *xdst, *new_xdst;
1933 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
1934
1935 /* Check if the policies from old bundle are usable */
1936 xdst = NULL;
1937 if (oldflo) {
1938 xdst = container_of(oldflo, struct xfrm_dst, flo);
1939 num_pols = xdst->num_pols;
1940 num_xfrms = xdst->num_xfrms;
1941 pol_dead = 0;
1942 for (i = 0; i < num_pols; i++) {
1943 pols[i] = xdst->pols[i];
1944 pol_dead |= pols[i]->walk.dead;
1945 }
1946 if (pol_dead) {
1947 dst_free(&xdst->u.dst);
1948 xdst = NULL;
1949 num_pols = 0;
1950 num_xfrms = 0;
1951 oldflo = NULL;
1952 }
1953 }
1954
1955 /* Resolve policies to use if we couldn't get them from
1956 * previous cache entry */
1957 if (xdst == NULL) {
1958 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001959 pols[0] = __xfrm_policy_lookup(net, fl, family,
1960 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001961 err = xfrm_expand_policies(fl, family, pols,
1962 &num_pols, &num_xfrms);
1963 if (err < 0)
1964 goto inc_error;
1965 if (num_pols == 0)
1966 return NULL;
1967 if (num_xfrms <= 0)
1968 goto make_dummy_bundle;
1969 }
1970
1971 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family, dst_orig);
1972 if (IS_ERR(new_xdst)) {
1973 err = PTR_ERR(new_xdst);
1974 if (err != -EAGAIN)
1975 goto error;
1976 if (oldflo == NULL)
1977 goto make_dummy_bundle;
1978 dst_hold(&xdst->u.dst);
1979 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00001980 } else if (new_xdst == NULL) {
1981 num_xfrms = 0;
1982 if (oldflo == NULL)
1983 goto make_dummy_bundle;
1984 xdst->num_xfrms = 0;
1985 dst_hold(&xdst->u.dst);
1986 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00001987 }
1988
1989 /* Kill the previous bundle */
1990 if (xdst) {
1991 /* The policies were stolen for newly generated bundle */
1992 xdst->num_pols = 0;
1993 dst_free(&xdst->u.dst);
1994 }
1995
1996 /* Flow cache does not have reference, it dst_free()'s,
1997 * but we do need to return one reference for original caller */
1998 dst_hold(&new_xdst->u.dst);
1999 return &new_xdst->flo;
2000
2001make_dummy_bundle:
2002 /* We found policies, but there's no bundles to instantiate:
2003 * either because the policy blocks, has no transformations or
2004 * we could not build template (no xfrm_states).*/
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002005 xdst = xfrm_create_dummy_bundle(net, dst_orig, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002006 if (IS_ERR(xdst)) {
2007 xfrm_pols_put(pols, num_pols);
2008 return ERR_CAST(xdst);
2009 }
2010 xdst->num_pols = num_pols;
2011 xdst->num_xfrms = num_xfrms;
2012 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
2013
2014 dst_hold(&xdst->u.dst);
2015 return &xdst->flo;
2016
2017inc_error:
2018 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2019error:
2020 if (xdst != NULL)
2021 dst_free(&xdst->u.dst);
2022 else
2023 xfrm_pols_put(pols, num_pols);
2024 return ERR_PTR(err);
2025}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
David S. Miller2774c132011-03-01 14:59:04 -08002027static struct dst_entry *make_blackhole(struct net *net, u16 family,
2028 struct dst_entry *dst_orig)
2029{
2030 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2031 struct dst_entry *ret;
2032
2033 if (!afinfo) {
2034 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002035 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002036 } else {
2037 ret = afinfo->blackhole_route(net, dst_orig);
2038 }
2039 xfrm_policy_put_afinfo(afinfo);
2040
2041 return ret;
2042}
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044/* Main function: finds/creates a bundle for given flow.
2045 *
2046 * At the moment we eat a raw IP route. Mostly to speed up lookups
2047 * on interfaces with disabled IPsec.
2048 */
David S. Miller452edd52011-03-02 13:27:41 -08002049struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2050 const struct flowi *fl,
2051 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002053 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002054 struct flow_cache_object *flo;
2055 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002056 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002057 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002058 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002059 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061restart:
Timo Teräs80c802f2010-04-07 00:30:05 +00002062 dst = NULL;
2063 xdst = NULL;
2064 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002065
Thomas Graff7944fb2007-08-25 13:46:55 -07002066 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002067 num_pols = 1;
2068 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2069 err = xfrm_expand_policies(fl, family, pols,
2070 &num_pols, &num_xfrms);
2071 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002072 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002073
2074 if (num_pols) {
2075 if (num_xfrms <= 0) {
2076 drop_pols = num_pols;
2077 goto no_transform;
2078 }
2079
2080 xdst = xfrm_resolve_and_create_bundle(
2081 pols, num_pols, fl,
2082 family, dst_orig);
2083 if (IS_ERR(xdst)) {
2084 xfrm_pols_put(pols, num_pols);
2085 err = PTR_ERR(xdst);
2086 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002087 } else if (xdst == NULL) {
2088 num_xfrms = 0;
2089 drop_pols = num_pols;
2090 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002091 }
2092
Steffen Klassertfbd50602011-03-15 21:12:49 +00002093 dst_hold(&xdst->u.dst);
2094
Timo Teräs80c802f2010-04-07 00:30:05 +00002095 spin_lock_bh(&xfrm_policy_sk_bundle_lock);
2096 xdst->u.dst.next = xfrm_policy_sk_bundles;
2097 xfrm_policy_sk_bundles = &xdst->u.dst;
2098 spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
2099
2100 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002101 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Timo Teräs80c802f2010-04-07 00:30:05 +00002104 if (xdst == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002106 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002107 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002108 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Timo Teräs80c802f2010-04-07 00:30:05 +00002110 flo = flow_cache_lookup(net, fl, family, dir,
2111 xfrm_bundle_lookup, dst_orig);
2112 if (flo == NULL)
2113 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002114 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002115 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002116 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002117 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002118 xdst = container_of(flo, struct xfrm_dst, flo);
2119
2120 num_pols = xdst->num_pols;
2121 num_xfrms = xdst->num_xfrms;
2122 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy*) * num_pols);
2123 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
2125
Timo Teräs80c802f2010-04-07 00:30:05 +00002126 dst = &xdst->u.dst;
2127 if (route == NULL && num_xfrms > 0) {
2128 /* The only case when xfrm_bundle_lookup() returns a
2129 * bundle with null route, is when the template could
2130 * not be resolved. It means policies are there, but
2131 * bundle could not be created, since we don't yet
2132 * have the xfrm_state's. We need to wait for KM to
2133 * negotiate new SA's or bail out with error.*/
2134 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002135 dst_release(dst);
Timo Terasa1aa3482010-05-15 23:49:26 -07002136 xfrm_pols_put(pols, drop_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002137 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
David S. Miller2774c132011-03-01 14:59:04 -08002138
David S. Miller452edd52011-03-02 13:27:41 -08002139 return make_blackhole(net, family, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002140 }
David S. Miller1d28f422011-03-12 00:29:39 -05002141 if (fl->flowi_flags & FLOWI_FLAG_CAN_SLEEP) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002142 DECLARE_WAITQUEUE(wait, current);
2143
2144 add_wait_queue(&net->xfrm.km_waitq, &wait);
2145 set_current_state(TASK_INTERRUPTIBLE);
2146 schedule();
2147 set_current_state(TASK_RUNNING);
2148 remove_wait_queue(&net->xfrm.km_waitq, &wait);
2149
2150 if (!signal_pending(current)) {
2151 dst_release(dst);
2152 goto restart;
2153 }
2154
2155 err = -ERESTART;
2156 } else
2157 err = -EAGAIN;
2158
2159 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2160 goto error;
2161 }
2162
2163no_transform:
2164 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002165 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Timo Teräs80c802f2010-04-07 00:30:05 +00002167 if ((flags & XFRM_LOOKUP_ICMP) &&
2168 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2169 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002170 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002171 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002172
Timo Teräs80c802f2010-04-07 00:30:05 +00002173 for (i = 0; i < num_pols; i++)
2174 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002175
Timo Teräs80c802f2010-04-07 00:30:05 +00002176 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002178 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002179 err = -EPERM;
2180 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002181 } else if (num_xfrms > 0) {
2182 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002183 dst_release(dst_orig);
2184 } else {
2185 /* Flow passes untransformed */
2186 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002187 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002189ok:
2190 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002191 if (dst && dst->xfrm &&
2192 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2193 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002194 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Timo Teräs80c802f2010-04-07 00:30:05 +00002196nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002197 if (!(flags & XFRM_LOOKUP_ICMP)) {
2198 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002199 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002200 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002201 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002203 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002204dropdst:
2205 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002206 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002207 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208}
2209EXPORT_SYMBOL(xfrm_lookup);
2210
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002211static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002212xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002213{
2214 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002215
2216 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2217 return 0;
2218 x = skb->sp->xvec[idx];
2219 if (!x->type->reject)
2220 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002221 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002222}
2223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224/* When skb is transformed back to its "native" form, we have to
2225 * check policy restrictions. At the moment we make this in maximally
2226 * stupid way. Shame on me. :-) Of course, connected sockets must
2227 * have policy cached at them.
2228 */
2229
2230static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002231xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 unsigned short family)
2233{
2234 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002235 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 return x->id.proto == tmpl->id.proto &&
2237 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2238 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2239 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002240 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002241 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002242 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2243 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
2245
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002246/*
2247 * 0 or more than 0 is returned when validation is succeeded (either bypass
2248 * because of optional transport mode, or next index of the mathced secpath
2249 * state with the template.
2250 * -1 is returned when no matching template is found.
2251 * Otherwise "-2 - errored_index" is returned.
2252 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002254xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 unsigned short family)
2256{
2257 int idx = start;
2258
2259 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002260 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return start;
2262 } else
2263 start = -1;
2264 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002265 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002267 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2268 if (start == -1)
2269 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 }
2273 return start;
2274}
2275
Herbert Xud5422ef2007-12-12 10:44:16 -08002276int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2277 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
2279 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002280 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
2282 if (unlikely(afinfo == NULL))
2283 return -EAFNOSUPPORT;
2284
Herbert Xud5422ef2007-12-12 10:44:16 -08002285 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002286 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002288 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
Herbert Xud5422ef2007-12-12 10:44:16 -08002290EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
David S. Miller9a7386e2011-02-24 01:44:12 -05002292static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
2294 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002295 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002296 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
2300
2301 return 0;
2302}
2303
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002304int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 unsigned short family)
2306{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002307 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002309 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2310 int npols = 0;
2311 int xfrm_nr;
2312 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002313 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002315 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002316 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Herbert Xud5422ef2007-12-12 10:44:16 -08002318 reverse = dir & ~XFRM_POLICY_MASK;
2319 dir &= XFRM_POLICY_MASK;
2320 fl_dir = policy_to_flow_dir(dir);
2321
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002322 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002323 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002325 }
2326
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002327 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 /* First, check used SA against their selectors. */
2330 if (skb->sp) {
2331 int i;
2332
2333 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002334 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002335 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002336 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 }
2340 }
2341
2342 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002343 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002344 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002345 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002346 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002347 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002348 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002351 if (!pol) {
2352 struct flow_cache_object *flo;
2353
2354 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2355 xfrm_policy_lookup, NULL);
2356 if (IS_ERR_OR_NULL(flo))
2357 pol = ERR_CAST(flo);
2358 else
2359 pol = container_of(flo, struct xfrm_policy, flo);
2360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002362 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002363 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002364 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002365 }
James Morris134b0fc2006-10-05 15:42:27 -05002366
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002367 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002368 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002369 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002370 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002371 return 0;
2372 }
2373 return 1;
2374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
James Morris9d729f72007-03-04 16:12:44 -08002376 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002378 pols[0] = pol;
2379 npols ++;
2380#ifdef CONFIG_XFRM_SUB_POLICY
2381 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002382 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002383 &fl, family,
2384 XFRM_POLICY_IN);
2385 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002386 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002387 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002388 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002389 }
James Morris9d729f72007-03-04 16:12:44 -08002390 pols[1]->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002391 npols ++;
2392 }
2393 }
2394#endif
2395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 if (pol->action == XFRM_POLICY_ALLOW) {
2397 struct sec_path *sp;
2398 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002399 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002400 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002401 struct xfrm_tmpl **tpp = tp;
2402 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 int i, k;
2404
2405 if ((sp = skb->sp) == NULL)
2406 sp = &dummy;
2407
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002408 for (pi = 0; pi < npols; pi++) {
2409 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002410 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002411 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002412 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002413 }
2414 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002415 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002416 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002417 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002418 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2419 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2420 }
2421 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002422 if (npols > 1) {
2423 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
2424 tpp = stp;
2425 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 /* For each tunnel xfrm, find the first matching tmpl.
2428 * For each tmpl before that, find corresponding xfrm.
2429 * Order is _important_. Later we will implement
2430 * some barriers, but at the moment barriers
2431 * are implied between each two transformations.
2432 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002433 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2434 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002435 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002436 if (k < -1)
2437 /* "-2 - errored_index" returned */
2438 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002439 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 }
2443
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002444 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002445 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002449 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 return 1;
2451 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002452 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
2454reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002455 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002456reject_error:
2457 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 return 0;
2459}
2460EXPORT_SYMBOL(__xfrm_policy_check);
2461
2462int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2463{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002464 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002466 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002467 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002469 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002470 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002474 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002475
David S. Miller452edd52011-03-02 13:27:41 -08002476 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, 0);
2477 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002478 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002479 dst = NULL;
2480 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002481 skb_dst_set(skb, dst);
2482 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
2484EXPORT_SYMBOL(__xfrm_route_forward);
2485
David S. Millerd49c73c2006-08-13 18:55:53 -07002486/* Optimize later using cookies and generation ids. */
2487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2489{
David S. Millerd49c73c2006-08-13 18:55:53 -07002490 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002491 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2492 * get validated by dst_ops->check on every use. We do this
2493 * because when a normal route referenced by an XFRM dst is
2494 * obsoleted we do not go looking around for all parent
2495 * referencing XFRM dsts so that we can invalidate them. It
2496 * is just too much work. Instead we make the checks here on
2497 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002498 *
2499 * XFRM dst A --> IPv4 dst X
2500 *
2501 * X is the "xdst->route" of A (X is also the "dst->path" of A
2502 * in this example). If X is marked obsolete, "A" will not
2503 * notice. That's what we are validating here via the
2504 * stale_bundle() check.
2505 *
2506 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002507 * dst which causes it's ->obsolete field to be set to
2508 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2509 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002510 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002511 if (dst->obsolete < 0 && !stale_bundle(dst))
2512 return dst;
2513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 return NULL;
2515}
2516
2517static int stale_bundle(struct dst_entry *dst)
2518{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002519 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
Herbert Xuaabc9762005-05-03 16:27:10 -07002522void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002525 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002526 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 dev_put(dev);
2528 }
2529}
Herbert Xuaabc9762005-05-03 16:27:10 -07002530EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
2532static void xfrm_link_failure(struct sk_buff *skb)
2533{
2534 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535}
2536
2537static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2538{
2539 if (dst) {
2540 if (dst->obsolete) {
2541 dst_release(dst);
2542 dst = NULL;
2543 }
2544 }
2545 return dst;
2546}
2547
Alexey Dobriyanddcfd792008-11-25 17:37:23 -08002548static void __xfrm_garbage_collect(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
Timo Teräs80c802f2010-04-07 00:30:05 +00002550 struct dst_entry *head, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Timo Teräs80c802f2010-04-07 00:30:05 +00002552 spin_lock_bh(&xfrm_policy_sk_bundle_lock);
2553 head = xfrm_policy_sk_bundles;
2554 xfrm_policy_sk_bundles = NULL;
2555 spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
2556
2557 while (head) {
2558 next = head->next;
2559 dst_free(head);
2560 head = next;
2561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562}
2563
Paul Mooree4c17212013-05-29 07:36:25 +00002564void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002565{
2566 flow_cache_flush();
2567 __xfrm_garbage_collect(net);
2568}
Paul Mooree4c17212013-05-29 07:36:25 +00002569EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002570
2571static void xfrm_garbage_collect_deferred(struct net *net)
2572{
2573 flow_cache_flush_deferred();
2574 __xfrm_garbage_collect(net);
2575}
2576
Herbert Xu25ee3282007-12-11 09:32:34 -08002577static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
2579 do {
2580 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2581 u32 pmtu, route_mtu_cached;
2582
2583 pmtu = dst_mtu(dst->child);
2584 xdst->child_mtu_cached = pmtu;
2585
2586 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2587
2588 route_mtu_cached = dst_mtu(xdst->route);
2589 xdst->route_mtu_cached = route_mtu_cached;
2590
2591 if (pmtu > route_mtu_cached)
2592 pmtu = route_mtu_cached;
2593
David S. Millerdefb3512010-12-08 21:16:57 -08002594 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 } while ((dst = dst->next));
2596}
2597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598/* Check that the bundle accepts the flow and its components are
2599 * still valid.
2600 */
2601
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002602static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
2604 struct dst_entry *dst = &first->u.dst;
2605 struct xfrm_dst *last;
2606 u32 mtu;
2607
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002608 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 (dst->dev && !netif_running(dst->dev)))
2610 return 0;
2611
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002612 if (dst->flags & DST_XFRM_QUEUE)
2613 return 1;
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 last = NULL;
2616
2617 do {
2618 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2619
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2621 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002622 if (xdst->xfrm_genid != dst->xfrm->genid)
2623 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002624 if (xdst->num_pols > 0 &&
2625 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002626 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
2628 mtu = dst_mtu(dst->child);
2629 if (xdst->child_mtu_cached != mtu) {
2630 last = xdst;
2631 xdst->child_mtu_cached = mtu;
2632 }
2633
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002634 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 return 0;
2636 mtu = dst_mtu(xdst->route);
2637 if (xdst->route_mtu_cached != mtu) {
2638 last = xdst;
2639 xdst->route_mtu_cached = mtu;
2640 }
2641
2642 dst = dst->child;
2643 } while (dst->xfrm);
2644
2645 if (likely(!last))
2646 return 1;
2647
2648 mtu = last->child_mtu_cached;
2649 for (;;) {
2650 dst = &last->u.dst;
2651
2652 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2653 if (mtu > last->route_mtu_cached)
2654 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002655 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
2657 if (last == first)
2658 break;
2659
Patrick McHardybd0bf072007-07-18 01:55:52 -07002660 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 last->child_mtu_cached = mtu;
2662 }
2663
2664 return 1;
2665}
2666
David S. Miller0dbaee32010-12-13 12:52:14 -08002667static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2668{
2669 return dst_metric_advmss(dst->path);
2670}
2671
Steffen Klassertebb762f2011-11-23 02:12:51 +00002672static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002673{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002674 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2675
2676 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002677}
2678
David S. Millerf894cbf2012-07-02 21:52:24 -07002679static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2680 struct sk_buff *skb,
2681 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002682{
David S. Millerf894cbf2012-07-02 21:52:24 -07002683 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002684}
2685
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2687{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002688 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 int err = 0;
2690 if (unlikely(afinfo == NULL))
2691 return -EINVAL;
2692 if (unlikely(afinfo->family >= NPROTO))
2693 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002694 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2696 err = -ENOBUFS;
2697 else {
2698 struct dst_ops *dst_ops = afinfo->dst_ops;
2699 if (likely(dst_ops->kmem_cachep == NULL))
2700 dst_ops->kmem_cachep = xfrm_dst_cache;
2701 if (likely(dst_ops->check == NULL))
2702 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002703 if (likely(dst_ops->default_advmss == NULL))
2704 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002705 if (likely(dst_ops->mtu == NULL))
2706 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 if (likely(dst_ops->negative_advice == NULL))
2708 dst_ops->negative_advice = xfrm_negative_advice;
2709 if (likely(dst_ops->link_failure == NULL))
2710 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002711 if (likely(dst_ops->neigh_lookup == NULL))
2712 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002714 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002715 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002717 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002718
2719 rtnl_lock();
2720 for_each_net(net) {
2721 struct dst_ops *xfrm_dst_ops;
2722
2723 switch (afinfo->family) {
2724 case AF_INET:
2725 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2726 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002727#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002728 case AF_INET6:
2729 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2730 break;
2731#endif
2732 default:
2733 BUG();
2734 }
2735 *xfrm_dst_ops = *afinfo->dst_ops;
2736 }
2737 rtnl_unlock();
2738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 return err;
2740}
2741EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2742
2743int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2744{
2745 int err = 0;
2746 if (unlikely(afinfo == NULL))
2747 return -EINVAL;
2748 if (unlikely(afinfo->family >= NPROTO))
2749 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002750 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2752 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2753 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002754 else
2755 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2756 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002758 spin_unlock(&xfrm_policy_afinfo_lock);
2759 if (!err) {
2760 struct dst_ops *dst_ops = afinfo->dst_ops;
2761
2762 synchronize_rcu();
2763
2764 dst_ops->kmem_cachep = NULL;
2765 dst_ops->check = NULL;
2766 dst_ops->negative_advice = NULL;
2767 dst_ops->link_failure = NULL;
2768 afinfo->garbage_collect = NULL;
2769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return err;
2771}
2772EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2773
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002774static void __net_init xfrm_dst_ops_init(struct net *net)
2775{
2776 struct xfrm_policy_afinfo *afinfo;
2777
Eric Dumazetef8531b2012-08-19 12:31:48 +02002778 rcu_read_lock();
2779 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002780 if (afinfo)
2781 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002782#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetef8531b2012-08-19 12:31:48 +02002783 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002784 if (afinfo)
2785 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2786#endif
Priyanka Jain418a99a2012-08-12 21:22:29 +00002787 rcu_read_unlock();
Herbert Xu546be242006-05-27 23:03:58 -07002788}
2789
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2791{
Jiri Pirko351638e2013-05-28 01:30:21 +00002792 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 switch (event) {
2795 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002796 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 }
2798 return NOTIFY_DONE;
2799}
2800
2801static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002802 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803};
2804
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002805#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002806static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002807{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002808 int rv;
2809
Tejun Heo7d720c32010-02-16 15:20:26 +00002810 if (snmp_mib_init((void __percpu **)net->mib.xfrm_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00002811 sizeof(struct linux_xfrm_mib),
2812 __alignof__(struct linux_xfrm_mib)) < 0)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002813 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002814 rv = xfrm_proc_init(net);
2815 if (rv < 0)
Tejun Heo7d720c32010-02-16 15:20:26 +00002816 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002817 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002818}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002819
2820static void xfrm_statistics_fini(struct net *net)
2821{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002822 xfrm_proc_fini(net);
Tejun Heo7d720c32010-02-16 15:20:26 +00002823 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002824}
2825#else
2826static int __net_init xfrm_statistics_init(struct net *net)
2827{
2828 return 0;
2829}
2830
2831static void xfrm_statistics_fini(struct net *net)
2832{
2833}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002834#endif
2835
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002836static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
David S. Miller2518c7c2006-08-24 04:45:07 -07002838 unsigned int hmask, sz;
2839 int dir;
2840
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002841 if (net_eq(net, &init_net))
2842 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002844 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002845 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
David S. Miller2518c7c2006-08-24 04:45:07 -07002847 hmask = 8 - 1;
2848 sz = (hmask+1) * sizeof(struct hlist_head);
2849
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002850 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2851 if (!net->xfrm.policy_byidx)
2852 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002853 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002854
2855 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2856 struct xfrm_policy_hash *htab;
2857
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002858 net->xfrm.policy_count[dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002859 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002860
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002861 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002862 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002863 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002864 goto out_bydst;
2865 htab->hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002866 }
2867
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002868 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002869 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002870 if (net_eq(net, &init_net))
2871 register_netdevice_notifier(&xfrm_dev_notifier);
2872 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002873
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002874out_bydst:
2875 for (dir--; dir >= 0; dir--) {
2876 struct xfrm_policy_hash *htab;
2877
2878 htab = &net->xfrm.policy_bydst[dir];
2879 xfrm_hash_free(htab->table, sz);
2880 }
2881 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002882out_byidx:
2883 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884}
2885
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002886static void xfrm_policy_fini(struct net *net)
2887{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002888 struct xfrm_audit audit_info;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002889 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002890 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002891
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002892 flush_work(&net->xfrm.policy_hash_work);
2893#ifdef CONFIG_XFRM_SUB_POLICY
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002894 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002895 audit_info.sessionid = -1;
2896 audit_info.secid = 0;
2897 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, &audit_info);
2898#endif
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002899 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002900 audit_info.sessionid = -1;
2901 audit_info.secid = 0;
2902 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002903
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002904 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002905
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002906 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002907 struct xfrm_policy_hash *htab;
2908
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002909 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002910
2911 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01002912 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002913 WARN_ON(!hlist_empty(htab->table));
2914 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002915 }
2916
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002917 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002918 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2919 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002920}
2921
2922static int __net_init xfrm_net_init(struct net *net)
2923{
2924 int rv;
2925
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002926 rv = xfrm_statistics_init(net);
2927 if (rv < 0)
2928 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002929 rv = xfrm_state_init(net);
2930 if (rv < 0)
2931 goto out_state;
2932 rv = xfrm_policy_init(net);
2933 if (rv < 0)
2934 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002935 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002936 rv = xfrm_sysctl_init(net);
2937 if (rv < 0)
2938 goto out_sysctl;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002939 return 0;
2940
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002941out_sysctl:
2942 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002943out_policy:
2944 xfrm_state_fini(net);
2945out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002946 xfrm_statistics_fini(net);
2947out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002948 return rv;
2949}
2950
2951static void __net_exit xfrm_net_exit(struct net *net)
2952{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002953 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002954 xfrm_policy_fini(net);
2955 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002956 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002957}
2958
2959static struct pernet_operations __net_initdata xfrm_net_ops = {
2960 .init = xfrm_net_init,
2961 .exit = xfrm_net_exit,
2962};
2963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964void __init xfrm_init(void)
2965{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002966 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 xfrm_input_init();
2968}
2969
Joy Lattenab5f5e82007-09-17 11:51:22 -07002970#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08002971static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2972 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002973{
Paul Moore875179f2007-12-01 23:27:18 +11002974 struct xfrm_sec_ctx *ctx = xp->security;
2975 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002976
Paul Moore875179f2007-12-01 23:27:18 +11002977 if (ctx)
2978 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2979 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2980
2981 switch(sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002982 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002983 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002984 if (sel->prefixlen_s != 32)
2985 audit_log_format(audit_buf, " src_prefixlen=%d",
2986 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002987 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002988 if (sel->prefixlen_d != 32)
2989 audit_log_format(audit_buf, " dst_prefixlen=%d",
2990 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002991 break;
2992 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002993 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002994 if (sel->prefixlen_s != 128)
2995 audit_log_format(audit_buf, " src_prefixlen=%d",
2996 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002997 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002998 if (sel->prefixlen_d != 128)
2999 audit_log_format(audit_buf, " dst_prefixlen=%d",
3000 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003001 break;
3002 }
3003}
3004
Paul Moore68277ac2007-12-20 20:49:33 -08003005void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07003006 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003007{
3008 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003009
Paul Mooreafeb14b2007-12-21 14:58:11 -08003010 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003011 if (audit_buf == NULL)
3012 return;
Eric Paris25323862008-04-18 10:09:25 -04003013 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003014 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003015 xfrm_audit_common_policyinfo(xp, audit_buf);
3016 audit_log_end(audit_buf);
3017}
3018EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3019
Paul Moore68277ac2007-12-20 20:49:33 -08003020void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07003021 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003022{
3023 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003024
Paul Mooreafeb14b2007-12-21 14:58:11 -08003025 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003026 if (audit_buf == NULL)
3027 return;
Eric Paris25323862008-04-18 10:09:25 -04003028 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003029 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003030 xfrm_audit_common_policyinfo(xp, audit_buf);
3031 audit_log_end(audit_buf);
3032}
3033EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3034#endif
3035
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003036#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003037static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3038 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003039{
3040 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3041 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003042 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3043 sel_cmp->family) &&
3044 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3045 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003046 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3047 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003048 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003049 }
3050 } else {
3051 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003052 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003053 }
3054 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003055 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003056}
3057
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003058static struct xfrm_policy * xfrm_migrate_policy_find(const struct xfrm_selector *sel,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003059 u8 dir, u8 type)
3060{
3061 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003062 struct hlist_head *chain;
3063 u32 priority = ~0U;
3064
3065 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -08003066 chain = policy_hash_direct(&init_net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003067 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003068 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3069 pol->type == type) {
3070 ret = pol;
3071 priority = ret->priority;
3072 break;
3073 }
3074 }
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003075 chain = &init_net.xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003076 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003077 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3078 pol->type == type &&
3079 pol->priority < priority) {
3080 ret = pol;
3081 break;
3082 }
3083 }
3084
3085 if (ret)
3086 xfrm_pol_hold(ret);
3087
3088 read_unlock_bh(&xfrm_policy_lock);
3089
3090 return ret;
3091}
3092
David S. Millerdd701752011-02-24 00:21:08 -05003093static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003094{
3095 int match = 0;
3096
3097 if (t->mode == m->mode && t->id.proto == m->proto &&
3098 (m->reqid == 0 || t->reqid == m->reqid)) {
3099 switch (t->mode) {
3100 case XFRM_MODE_TUNNEL:
3101 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003102 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3103 m->old_family) &&
3104 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3105 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003106 match = 1;
3107 }
3108 break;
3109 case XFRM_MODE_TRANSPORT:
3110 /* in case of transport mode, template does not store
3111 any IP addresses, hence we just compare mode and
3112 protocol */
3113 match = 1;
3114 break;
3115 default:
3116 break;
3117 }
3118 }
3119 return match;
3120}
3121
3122/* update endpoint address(es) of template(s) */
3123static int xfrm_policy_migrate(struct xfrm_policy *pol,
3124 struct xfrm_migrate *m, int num_migrate)
3125{
3126 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003127 int i, j, n = 0;
3128
3129 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003130 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003131 /* target policy has been deleted */
3132 write_unlock_bh(&pol->lock);
3133 return -ENOENT;
3134 }
3135
3136 for (i = 0; i < pol->xfrm_nr; i++) {
3137 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3138 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3139 continue;
3140 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003141 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3142 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003143 continue;
3144 /* update endpoints */
3145 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3146 sizeof(pol->xfrm_vec[i].id.daddr));
3147 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3148 sizeof(pol->xfrm_vec[i].saddr));
3149 pol->xfrm_vec[i].encap_family = mp->new_family;
3150 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003151 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003152 }
3153 }
3154
3155 write_unlock_bh(&pol->lock);
3156
3157 if (!n)
3158 return -ENODATA;
3159
3160 return 0;
3161}
3162
David S. Millerdd701752011-02-24 00:21:08 -05003163static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003164{
3165 int i, j;
3166
3167 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3168 return -EINVAL;
3169
3170 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003171 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3172 m[i].old_family) &&
3173 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3174 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003175 return -EINVAL;
3176 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3177 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3178 return -EINVAL;
3179
3180 /* check if there is any duplicated entry */
3181 for (j = i + 1; j < num_migrate; j++) {
3182 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3183 sizeof(m[i].old_daddr)) &&
3184 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3185 sizeof(m[i].old_saddr)) &&
3186 m[i].proto == m[j].proto &&
3187 m[i].mode == m[j].mode &&
3188 m[i].reqid == m[j].reqid &&
3189 m[i].old_family == m[j].old_family)
3190 return -EINVAL;
3191 }
3192 }
3193
3194 return 0;
3195}
3196
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003197int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003198 struct xfrm_migrate *m, int num_migrate,
3199 struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003200{
3201 int i, err, nx_cur = 0, nx_new = 0;
3202 struct xfrm_policy *pol = NULL;
3203 struct xfrm_state *x, *xc;
3204 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3205 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3206 struct xfrm_migrate *mp;
3207
3208 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3209 goto out;
3210
3211 /* Stage 1 - find policy */
3212 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
3213 err = -ENOENT;
3214 goto out;
3215 }
3216
3217 /* Stage 2 - find and update state(s) */
3218 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
3219 if ((x = xfrm_migrate_state_find(mp))) {
3220 x_cur[nx_cur] = x;
3221 nx_cur++;
3222 if ((xc = xfrm_state_migrate(x, mp))) {
3223 x_new[nx_new] = xc;
3224 nx_new++;
3225 } else {
3226 err = -ENODATA;
3227 goto restore_state;
3228 }
3229 }
3230 }
3231
3232 /* Stage 3 - update policy */
3233 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3234 goto restore_state;
3235
3236 /* Stage 4 - delete old state(s) */
3237 if (nx_cur) {
3238 xfrm_states_put(x_cur, nx_cur);
3239 xfrm_states_delete(x_cur, nx_cur);
3240 }
3241
3242 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003243 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003244
3245 xfrm_pol_put(pol);
3246
3247 return 0;
3248out:
3249 return err;
3250
3251restore_state:
3252 if (pol)
3253 xfrm_pol_put(pol);
3254 if (nx_cur)
3255 xfrm_states_put(x_cur, nx_cur);
3256 if (nx_new)
3257 xfrm_states_delete(x_new, nx_new);
3258
3259 return err;
3260}
David S. Millere610e672007-02-08 13:29:15 -08003261EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003262#endif