blob: f7078eb5b64e2bba4e98c5d3150d2b803816f870 [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
323 while ((skb = skb_dequeue(list)) != NULL) {
324 dev_put(skb->dev);
325 kfree_skb(skb);
326 }
327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329/* Rule must be locked. Release descentant resources, announce
330 * entry dead. The rule must be unlinked from lists to the moment.
331 */
332
333static void xfrm_policy_kill(struct xfrm_policy *policy)
334{
Herbert Xu12a169e2008-10-01 07:03:24 -0700335 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Timo Teräs285ead12010-04-07 00:30:06 +0000337 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100339 del_timer(&policy->polq.hold_timer);
340 xfrm_queue_purge(&policy->polq.hold_queue);
341
Timo Teräs285ead12010-04-07 00:30:06 +0000342 if (del_timer(&policy->timer))
343 xfrm_pol_put(policy);
344
345 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
David S. Miller2518c7c2006-08-24 04:45:07 -0700348static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
349
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800350static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700351{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800352 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700353}
354
David S. Miller5f803b52011-02-24 00:33:19 -0500355static struct hlist_head *policy_hash_bysel(struct net *net,
356 const struct xfrm_selector *sel,
357 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700358{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800359 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700360 unsigned int hash = __sel_hash(sel, family, hmask);
361
362 return (hash == hmask + 1 ?
Alexey Dobriyan11219942008-11-25 17:33:06 -0800363 &net->xfrm.policy_inexact[dir] :
364 net->xfrm.policy_bydst[dir].table + hash);
David S. Miller2518c7c2006-08-24 04:45:07 -0700365}
366
David S. Miller5f803b52011-02-24 00:33:19 -0500367static struct hlist_head *policy_hash_direct(struct net *net,
368 const xfrm_address_t *daddr,
369 const xfrm_address_t *saddr,
370 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700371{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800372 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700373 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
374
Alexey Dobriyan11219942008-11-25 17:33:06 -0800375 return net->xfrm.policy_bydst[dir].table + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700376}
377
David S. Miller2518c7c2006-08-24 04:45:07 -0700378static void xfrm_dst_hash_transfer(struct hlist_head *list,
379 struct hlist_head *ndsttable,
380 unsigned int nhashmask)
381{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800382 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700383 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800384 unsigned int h0 = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700385
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800386redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800387 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700388 unsigned int h;
389
390 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
391 pol->family, nhashmask);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800392 if (!entry0) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800393 hlist_del(&pol->bydst);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800394 hlist_add_head(&pol->bydst, ndsttable+h);
395 h0 = h;
396 } else {
397 if (h != h0)
398 continue;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800399 hlist_del(&pol->bydst);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800400 hlist_add_after(entry0, &pol->bydst);
401 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800402 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800403 }
404 if (!hlist_empty(list)) {
405 entry0 = NULL;
406 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700407 }
408}
409
410static void xfrm_idx_hash_transfer(struct hlist_head *list,
411 struct hlist_head *nidxtable,
412 unsigned int nhashmask)
413{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800414 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700415 struct xfrm_policy *pol;
416
Sasha Levinb67bfe02013-02-27 17:06:00 -0800417 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700418 unsigned int h;
419
420 h = __idx_hash(pol->index, nhashmask);
421 hlist_add_head(&pol->byidx, nidxtable+h);
422 }
423}
424
425static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
426{
427 return ((old_hmask + 1) << 1) - 1;
428}
429
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800430static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700431{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800432 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700433 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
434 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800435 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700436 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700437 int i;
438
439 if (!ndst)
440 return;
441
442 write_lock_bh(&xfrm_policy_lock);
443
444 for (i = hmask; i >= 0; i--)
445 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
446
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800447 net->xfrm.policy_bydst[dir].table = ndst;
448 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700449
450 write_unlock_bh(&xfrm_policy_lock);
451
David S. Miller44e36b42006-08-24 04:50:50 -0700452 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700453}
454
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800455static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700456{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800457 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700458 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
459 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800460 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700461 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700462 int i;
463
464 if (!nidx)
465 return;
466
467 write_lock_bh(&xfrm_policy_lock);
468
469 for (i = hmask; i >= 0; i--)
470 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
471
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800472 net->xfrm.policy_byidx = nidx;
473 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700474
475 write_unlock_bh(&xfrm_policy_lock);
476
David S. Miller44e36b42006-08-24 04:50:50 -0700477 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700478}
479
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800480static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700481{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800482 unsigned int cnt = net->xfrm.policy_count[dir];
483 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700484
485 if (total)
486 *total += cnt;
487
488 if ((hmask + 1) < xfrm_policy_hashmax &&
489 cnt > hmask)
490 return 1;
491
492 return 0;
493}
494
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800495static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700496{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800497 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700498
499 if ((hmask + 1) < xfrm_policy_hashmax &&
500 total > hmask)
501 return 1;
502
503 return 0;
504}
505
Alexey Dobriyane0710412010-01-23 13:37:10 +0000506void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700507{
508 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000509 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
510 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
511 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
512 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
513 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
514 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
515 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700516 si->spdhmcnt = xfrm_policy_hashmax;
517 read_unlock_bh(&xfrm_policy_lock);
518}
519EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700520
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700521static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800522static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700523{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800524 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700525 int dir, total;
526
527 mutex_lock(&hash_resize_mutex);
528
529 total = 0;
530 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800531 if (xfrm_bydst_should_resize(net, dir, &total))
532 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700533 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800534 if (xfrm_byidx_should_resize(net, total))
535 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700536
537 mutex_unlock(&hash_resize_mutex);
538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540/* Generate new index... KAME seems to generate them ordered by cost
541 * of an absolute inpredictability of ordering of rules. This will not pass. */
Alexey Dobriyan11219942008-11-25 17:33:06 -0800542static u32 xfrm_gen_index(struct net *net, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 static u32 idx_generator;
545
546 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700547 struct hlist_head *list;
548 struct xfrm_policy *p;
549 u32 idx;
550 int found;
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 idx = (idx_generator | dir);
553 idx_generator += 8;
554 if (idx == 0)
555 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800556 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700557 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800558 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700559 if (p->index == idx) {
560 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700564 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return idx;
566 }
567}
568
David S. Miller2518c7c2006-08-24 04:45:07 -0700569static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
570{
571 u32 *p1 = (u32 *) s1;
572 u32 *p2 = (u32 *) s2;
573 int len = sizeof(struct xfrm_selector) / sizeof(u32);
574 int i;
575
576 for (i = 0; i < len; i++) {
577 if (p1[i] != p2[i])
578 return 1;
579 }
580
581 return 0;
582}
583
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100584static void xfrm_policy_requeue(struct xfrm_policy *old,
585 struct xfrm_policy *new)
586{
587 struct xfrm_policy_queue *pq = &old->polq;
588 struct sk_buff_head list;
589
590 __skb_queue_head_init(&list);
591
592 spin_lock_bh(&pq->hold_queue.lock);
593 skb_queue_splice_init(&pq->hold_queue, &list);
594 del_timer(&pq->hold_timer);
595 spin_unlock_bh(&pq->hold_queue.lock);
596
597 if (skb_queue_empty(&list))
598 return;
599
600 pq = &new->polq;
601
602 spin_lock_bh(&pq->hold_queue.lock);
603 skb_queue_splice(&list, &pq->hold_queue);
604 pq->timeout = XFRM_QUEUE_TMO_MIN;
605 mod_timer(&pq->hold_timer, jiffies);
606 spin_unlock_bh(&pq->hold_queue.lock);
607}
608
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100609static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
610 struct xfrm_policy *pol)
611{
612 u32 mark = policy->mark.v & policy->mark.m;
613
614 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
615 return true;
616
617 if ((mark & pol->mark.m) == pol->mark.v &&
618 policy->priority == pol->priority)
619 return true;
620
621 return false;
622}
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
625{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800626 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700627 struct xfrm_policy *pol;
628 struct xfrm_policy *delpol;
629 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800630 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800633 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700634 delpol = NULL;
635 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800636 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800637 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700638 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100639 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800640 xfrm_sec_ctx_match(pol->security, policy->security) &&
641 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (excl) {
643 write_unlock_bh(&xfrm_policy_lock);
644 return -EEXIST;
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 delpol = pol;
647 if (policy->priority > pol->priority)
648 continue;
649 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800650 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 continue;
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (delpol)
654 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700657 hlist_add_after(newpos, &policy->bydst);
658 else
659 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800661 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 atomic_inc(&flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800663
664 /* After previous checking, family can either be AF_INET or AF_INET6 */
665 if (policy->family == AF_INET)
666 rt_genid_bump_ipv4(net);
667 else
668 rt_genid_bump_ipv6(net);
669
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100670 if (delpol) {
671 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800672 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100673 }
Alexey Dobriyan11219942008-11-25 17:33:06 -0800674 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir);
675 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800676 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 policy->curlft.use_time = 0;
678 if (!mod_timer(&policy->timer, jiffies + HZ))
679 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800680 list_add(&policy->walk.all, &net->xfrm.policy_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 write_unlock_bh(&xfrm_policy_lock);
682
David S. Miller9b78a822005-12-22 07:39:48 -0800683 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800685 else if (xfrm_bydst_should_resize(net, dir, NULL))
686 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return 0;
689}
690EXPORT_SYMBOL(xfrm_policy_insert);
691
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000692struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
693 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800694 struct xfrm_sec_ctx *ctx, int delete,
695 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
David S. Miller2518c7c2006-08-24 04:45:07 -0700697 struct xfrm_policy *pol, *ret;
698 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Eric Parisef41aaa2007-03-07 15:37:58 -0800700 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800702 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700703 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800704 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700705 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000706 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700707 !selector_cmp(sel, &pol->selector) &&
708 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700710 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700711 *err = security_xfrm_policy_delete(
712 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800713 if (*err) {
714 write_unlock_bh(&xfrm_policy_lock);
715 return pol;
716 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800717 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700718 }
719 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 break;
721 }
722 }
723 write_unlock_bh(&xfrm_policy_lock);
724
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000725 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700726 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700727 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
Trent Jaegerdf718372005-12-13 23:12:27 -0800729EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000731struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
732 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
David S. Miller2518c7c2006-08-24 04:45:07 -0700734 struct xfrm_policy *pol, *ret;
735 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Herbert Xub5505c62007-05-14 02:15:47 -0700737 *err = -ENOENT;
738 if (xfrm_policy_id2dir(id) != dir)
739 return NULL;
740
Eric Parisef41aaa2007-03-07 15:37:58 -0800741 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800743 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700744 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800745 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000746 if (pol->type == type && pol->index == id &&
747 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700749 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700750 *err = security_xfrm_policy_delete(
751 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800752 if (*err) {
753 write_unlock_bh(&xfrm_policy_lock);
754 return pol;
755 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800756 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700757 }
758 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 break;
760 }
761 }
762 write_unlock_bh(&xfrm_policy_lock);
763
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000764 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700765 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700766 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768EXPORT_SYMBOL(xfrm_policy_byid);
769
Joy Latten4aa2e622007-06-04 19:05:57 -0400770#ifdef CONFIG_SECURITY_NETWORK_XFRM
771static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800772xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Joy Latten4aa2e622007-06-04 19:05:57 -0400774 int dir, err = 0;
775
776 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
777 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400778 int i;
779
Sasha Levinb67bfe02013-02-27 17:06:00 -0800780 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800781 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400782 if (pol->type != type)
783 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700784 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400785 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700786 xfrm_audit_policy_delete(pol, 0,
787 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400788 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700789 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400790 return err;
791 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900792 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800793 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800794 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800795 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400796 bydst) {
797 if (pol->type != type)
798 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700799 err = security_xfrm_policy_delete(
800 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400801 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700802 xfrm_audit_policy_delete(pol, 0,
803 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400804 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700805 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400806 return err;
807 }
808 }
809 }
810 }
811 return err;
812}
813#else
814static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800815xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400816{
817 return 0;
818}
819#endif
820
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800821int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400822{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000823 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 write_lock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400826
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800827 err = xfrm_policy_flush_secctx_check(net, type, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400828 if (err)
829 goto out;
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700832 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800833 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700834
835 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800836 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800837 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700838 if (pol->type != type)
839 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000840 __xfrm_policy_unlink(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000842 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Joy Lattenab5f5e82007-09-17 11:51:22 -0700844 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400845 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700846 audit_info->secid);
Joy Latten161a09e2006-11-27 13:11:54 -0600847
David S. Miller2518c7c2006-08-24 04:45:07 -0700848 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700851 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700853
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800854 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700855 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800856 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800857 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700858 bydst) {
859 if (pol->type != type)
860 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000861 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700862 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000863 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700864
Joy Lattenab5f5e82007-09-17 11:51:22 -0700865 xfrm_audit_policy_delete(pol, 1,
866 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400867 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700868 audit_info->secid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700869 xfrm_policy_kill(pol);
870
871 write_lock_bh(&xfrm_policy_lock);
872 goto again2;
873 }
874 }
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000877 if (!cnt)
878 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400879out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 write_unlock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400881 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883EXPORT_SYMBOL(xfrm_policy_flush);
884
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800885int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800886 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 void *data)
888{
Herbert Xu12a169e2008-10-01 07:03:24 -0700889 struct xfrm_policy *pol;
890 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -0800891 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Timo Teras4c563f72008-02-28 21:31:08 -0800893 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
894 walk->type != XFRM_POLICY_TYPE_ANY)
895 return -EINVAL;
896
Herbert Xu12a169e2008-10-01 07:03:24 -0700897 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -0800898 return 0;
899
Herbert Xu12a169e2008-10-01 07:03:24 -0700900 write_lock_bh(&xfrm_policy_lock);
901 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800902 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -0700903 else
904 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800905 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -0700906 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -0800907 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -0700908 pol = container_of(x, struct xfrm_policy, walk);
909 if (walk->type != XFRM_POLICY_TYPE_ANY &&
910 walk->type != pol->type)
911 continue;
912 error = func(pol, xfrm_policy_id2dir(pol->index),
913 walk->seq, data);
914 if (error) {
915 list_move_tail(&walk->walk.all, &x->all);
916 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -0800917 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700918 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700920 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800921 error = -ENOENT;
922 goto out;
923 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700924 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925out:
Herbert Xu12a169e2008-10-01 07:03:24 -0700926 write_unlock_bh(&xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return error;
928}
929EXPORT_SYMBOL(xfrm_policy_walk);
930
Herbert Xu12a169e2008-10-01 07:03:24 -0700931void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
932{
933 INIT_LIST_HEAD(&walk->walk.all);
934 walk->walk.dead = 1;
935 walk->type = type;
936 walk->seq = 0;
937}
938EXPORT_SYMBOL(xfrm_policy_walk_init);
939
940void xfrm_policy_walk_done(struct xfrm_policy_walk *walk)
941{
942 if (list_empty(&walk->walk.all))
943 return;
944
945 write_lock_bh(&xfrm_policy_lock);
946 list_del(&walk->walk.all);
947 write_unlock_bh(&xfrm_policy_lock);
948}
949EXPORT_SYMBOL(xfrm_policy_walk_done);
950
James Morris134b0fc2006-10-05 15:42:27 -0500951/*
952 * Find policy to apply to this flow.
953 *
954 * Returns 0 if policy found, else an -errno.
955 */
David S. Millerf299d552011-02-24 01:23:30 -0500956static int xfrm_policy_match(const struct xfrm_policy *pol,
957 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -0700958 u8 type, u16 family, int dir)
959{
David S. Millerf299d552011-02-24 01:23:30 -0500960 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -0400961 int ret = -ESRCH;
962 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -0700963
964 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -0500965 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -0700966 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500967 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700968
969 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500970 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -0500971 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -0700972 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700973
James Morris134b0fc2006-10-05 15:42:27 -0500974 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700975}
976
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800977static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -0800978 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700979 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
James Morris134b0fc2006-10-05 15:42:27 -0500981 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700982 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -0500983 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -0700984 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700985 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700986
987 daddr = xfrm_flowi_daddr(fl, family);
988 saddr = xfrm_flowi_saddr(fl, family);
989 if (unlikely(!daddr || !saddr))
990 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800993 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700994 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800995 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500996 err = xfrm_policy_match(pol, fl, type, family, dir);
997 if (err) {
998 if (err == -ESRCH)
999 continue;
1000 else {
1001 ret = ERR_PTR(err);
1002 goto fail;
1003 }
1004 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001005 ret = pol;
1006 priority = ret->priority;
1007 break;
1008 }
1009 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001010 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001011 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001012 err = xfrm_policy_match(pol, fl, type, family, dir);
1013 if (err) {
1014 if (err == -ESRCH)
1015 continue;
1016 else {
1017 ret = ERR_PTR(err);
1018 goto fail;
1019 }
1020 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001021 ret = pol;
1022 break;
1023 }
1024 }
David S. Milleracba48e2006-08-25 15:46:46 -07001025 if (ret)
1026 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -05001027fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001029
David S. Miller2518c7c2006-08-24 04:45:07 -07001030 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001031}
1032
Timo Teräs80c802f2010-04-07 00:30:05 +00001033static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001034__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001035{
1036#ifdef CONFIG_XFRM_SUB_POLICY
1037 struct xfrm_policy *pol;
1038
1039 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1040 if (pol != NULL)
1041 return pol;
1042#endif
1043 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1044}
1045
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001046static int flow_to_policy_dir(int dir)
1047{
1048 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1049 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1050 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1051 return dir;
1052
1053 switch (dir) {
1054 default:
1055 case FLOW_DIR_IN:
1056 return XFRM_POLICY_IN;
1057 case FLOW_DIR_OUT:
1058 return XFRM_POLICY_OUT;
1059 case FLOW_DIR_FWD:
1060 return XFRM_POLICY_FWD;
1061 }
1062}
1063
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001064static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001065xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001066 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001067{
1068 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001069
1070 if (old_obj)
1071 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001072
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001073 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001074 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001075 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001076
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001077 /* Resolver returns two references:
1078 * one for cache and one for caller of flow_cache_lookup() */
1079 xfrm_pol_hold(pol);
1080
1081 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
Trent Jaegerdf718372005-12-13 23:12:27 -08001084static inline int policy_to_flow_dir(int dir)
1085{
1086 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001087 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1088 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1089 return dir;
1090 switch (dir) {
1091 default:
1092 case XFRM_POLICY_IN:
1093 return FLOW_DIR_IN;
1094 case XFRM_POLICY_OUT:
1095 return FLOW_DIR_OUT;
1096 case XFRM_POLICY_FWD:
1097 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001098 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001099}
1100
David S. Millerdee9f4b2011-02-22 18:44:31 -08001101static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir,
1102 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 struct xfrm_policy *pol;
1105
1106 read_lock_bh(&xfrm_policy_lock);
1107 if ((pol = sk->sk_policy[dir]) != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001108 bool match = xfrm_selector_match(&pol->selector, fl,
1109 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001110 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001111
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001112 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001113 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1114 pol = NULL;
1115 goto out;
1116 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001117 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001118 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001119 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001120 if (!err)
1121 xfrm_pol_hold(pol);
1122 else if (err == -ESRCH)
1123 pol = NULL;
1124 else
1125 pol = ERR_PTR(err);
1126 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 pol = NULL;
1128 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001129out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 read_unlock_bh(&xfrm_policy_lock);
1131 return pol;
1132}
1133
1134static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1135{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001136 struct net *net = xp_net(pol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001137 struct hlist_head *chain = policy_hash_bysel(net, &pol->selector,
David S. Miller2518c7c2006-08-24 04:45:07 -07001138 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001139
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001140 list_add(&pol->walk.all, &net->xfrm.policy_all);
David S. Miller2518c7c2006-08-24 04:45:07 -07001141 hlist_add_head(&pol->bydst, chain);
Alexey Dobriyane92303f2008-11-25 17:32:41 -08001142 hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001143 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001145
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001146 if (xfrm_bydst_should_resize(net, dir, NULL))
1147 schedule_work(&net->xfrm.policy_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
1150static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1151 int dir)
1152{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001153 struct net *net = xp_net(pol);
1154
David S. Miller2518c7c2006-08-24 04:45:07 -07001155 if (hlist_unhashed(&pol->bydst))
1156 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
David S. Miller2518c7c2006-08-24 04:45:07 -07001158 hlist_del(&pol->bydst);
1159 hlist_del(&pol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -07001160 list_del(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001161 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001162
1163 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
Herbert Xu4666faa2005-06-18 22:43:22 -07001166int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 write_lock_bh(&xfrm_policy_lock);
1169 pol = __xfrm_policy_unlink(pol, dir);
1170 write_unlock_bh(&xfrm_policy_lock);
1171 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001173 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001175 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
David S. Millera70fcb02006-03-20 19:18:52 -08001177EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1180{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001181 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 struct xfrm_policy *old_pol;
1183
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001184#ifdef CONFIG_XFRM_SUB_POLICY
1185 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1186 return -EINVAL;
1187#endif
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 write_lock_bh(&xfrm_policy_lock);
1190 old_pol = sk->sk_policy[dir];
1191 sk->sk_policy[dir] = pol;
1192 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001193 pol->curlft.add_time = get_seconds();
Alexey Dobriyan11219942008-11-25 17:33:06 -08001194 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1196 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001197 if (old_pol) {
1198 if (pol)
1199 xfrm_policy_requeue(old_pol, pol);
1200
Timo Teräsea2dea92010-03-31 00:17:05 +00001201 /* Unlinking succeeds always. This is the only function
1202 * allowed to delete or replace socket policy.
1203 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 write_unlock_bh(&xfrm_policy_lock);
1207
1208 if (old_pol) {
1209 xfrm_policy_kill(old_pol);
1210 }
1211 return 0;
1212}
1213
David S. Millerd3e40a92011-02-24 01:25:41 -05001214static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001216 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 if (newp) {
1219 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001220 if (security_xfrm_policy_clone(old->security,
1221 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001222 kfree(newp);
1223 return NULL; /* ENOMEM */
1224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 newp->lft = old->lft;
1226 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001227 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 newp->action = old->action;
1229 newp->flags = old->flags;
1230 newp->xfrm_nr = old->xfrm_nr;
1231 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001232 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 memcpy(newp->xfrm_vec, old->xfrm_vec,
1234 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1235 write_lock_bh(&xfrm_policy_lock);
1236 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1237 write_unlock_bh(&xfrm_policy_lock);
1238 xfrm_pol_put(newp);
1239 }
1240 return newp;
1241}
1242
1243int __xfrm_sk_clone_policy(struct sock *sk)
1244{
1245 struct xfrm_policy *p0 = sk->sk_policy[0],
1246 *p1 = sk->sk_policy[1];
1247
1248 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1249 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1250 return -ENOMEM;
1251 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1252 return -ENOMEM;
1253 return 0;
1254}
1255
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001256static int
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001257xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001258 unsigned short family)
1259{
1260 int err;
1261 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1262
1263 if (unlikely(afinfo == NULL))
1264 return -EINVAL;
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001265 err = afinfo->get_saddr(net, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001266 xfrm_policy_put_afinfo(afinfo);
1267 return err;
1268}
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270/* Resolve list of templates for the flow, given policy. */
1271
1272static int
David S. Millera6c2e612011-02-22 18:35:39 -08001273xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1274 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001276 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 int nx;
1278 int i, error;
1279 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1280 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001281 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1284 struct xfrm_state *x;
1285 xfrm_address_t *remote = daddr;
1286 xfrm_address_t *local = saddr;
1287 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1288
Joakim Koskela48b8d782007-07-26 00:08:42 -07001289 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1290 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 remote = &tmpl->id.daddr;
1292 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001293 if (xfrm_addr_any(local, tmpl->encap_family)) {
1294 error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001295 if (error)
1296 goto fail;
1297 local = &tmp;
1298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300
1301 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1302
1303 if (x && x->km.state == XFRM_STATE_VALID) {
1304 xfrm[nx++] = x;
1305 daddr = remote;
1306 saddr = local;
1307 continue;
1308 }
1309 if (x) {
1310 error = (x->km.state == XFRM_STATE_ERROR ?
1311 -EINVAL : -EAGAIN);
1312 xfrm_state_put(x);
1313 }
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001314 else if (error == -ESRCH)
1315 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 if (!tmpl->optional)
1318 goto fail;
1319 }
1320 return nx;
1321
1322fail:
1323 for (nx--; nx>=0; nx--)
1324 xfrm_state_put(xfrm[nx]);
1325 return error;
1326}
1327
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001328static int
David S. Millera6c2e612011-02-22 18:35:39 -08001329xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1330 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001331{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001332 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1333 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001334 int cnx = 0;
1335 int error;
1336 int ret;
1337 int i;
1338
1339 for (i = 0; i < npols; i++) {
1340 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1341 error = -ENOBUFS;
1342 goto fail;
1343 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001344
1345 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001346 if (ret < 0) {
1347 error = ret;
1348 goto fail;
1349 } else
1350 cnx += ret;
1351 }
1352
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001353 /* found states are sorted for outbound processing */
1354 if (npols > 1)
1355 xfrm_state_sort(xfrm, tpp, cnx, family);
1356
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001357 return cnx;
1358
1359 fail:
1360 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001361 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001362 return error;
1363
1364}
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366/* Check that the bundle accepts the flow and its components are
1367 * still valid.
1368 */
1369
David S. Miller05d84022011-02-22 17:47:10 -08001370static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001371{
1372 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1373 int tos;
1374
1375 if (!afinfo)
1376 return -EINVAL;
1377
1378 tos = afinfo->get_tos(fl);
1379
1380 xfrm_policy_put_afinfo(afinfo);
1381
1382 return tos;
1383}
1384
Timo Teräs80c802f2010-04-07 00:30:05 +00001385static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1386{
1387 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1388 struct dst_entry *dst = &xdst->u.dst;
1389
1390 if (xdst->route == NULL) {
1391 /* Dummy bundle - if it has xfrms we were not
1392 * able to build bundle as template resolution failed.
1393 * It means we need to try again resolving. */
1394 if (xdst->num_xfrms > 0)
1395 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001396 } else if (dst->flags & DST_XFRM_QUEUE) {
1397 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001398 } else {
1399 /* Real bundle */
1400 if (stale_bundle(dst))
1401 return NULL;
1402 }
1403
1404 dst_hold(dst);
1405 return flo;
1406}
1407
1408static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1409{
1410 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1411 struct dst_entry *dst = &xdst->u.dst;
1412
1413 if (!xdst->route)
1414 return 0;
1415 if (stale_bundle(dst))
1416 return 0;
1417
1418 return 1;
1419}
1420
1421static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1422{
1423 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1424 struct dst_entry *dst = &xdst->u.dst;
1425
1426 dst_free(dst);
1427}
1428
1429static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1430 .get = xfrm_bundle_flo_get,
1431 .check = xfrm_bundle_flo_check,
1432 .delete = xfrm_bundle_flo_delete,
1433};
1434
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001435static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001436{
1437 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001438 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001439 struct xfrm_dst *xdst;
1440
1441 if (!afinfo)
1442 return ERR_PTR(-EINVAL);
1443
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001444 switch (family) {
1445 case AF_INET:
1446 dst_ops = &net->xfrm.xfrm4_dst_ops;
1447 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001448#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001449 case AF_INET6:
1450 dst_ops = &net->xfrm.xfrm6_dst_ops;
1451 break;
1452#endif
1453 default:
1454 BUG();
1455 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001456 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001457
Madalin Bucurd4cae562011-09-26 07:04:36 +00001458 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001459 struct dst_entry *dst = &xdst->u.dst;
1460
1461 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001462 xdst->flo.ops = &xfrm_bundle_fc_ops;
Patrick McHardy9d7b0fc2012-08-20 02:56:56 -07001463 if (afinfo->init_dst)
1464 afinfo->init_dst(net, xdst);
Madalin Bucurd4cae562011-09-26 07:04:36 +00001465 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001466 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001467
Madalin Bucurd4cae562011-09-26 07:04:36 +00001468 xfrm_policy_put_afinfo(afinfo);
1469
Herbert Xu25ee3282007-12-11 09:32:34 -08001470 return xdst;
1471}
1472
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001473static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1474 int nfheader_len)
1475{
1476 struct xfrm_policy_afinfo *afinfo =
1477 xfrm_policy_get_afinfo(dst->ops->family);
1478 int err;
1479
1480 if (!afinfo)
1481 return -EINVAL;
1482
1483 err = afinfo->init_path(path, dst, nfheader_len);
1484
1485 xfrm_policy_put_afinfo(afinfo);
1486
1487 return err;
1488}
1489
Herbert Xu87c1e122010-03-02 02:51:56 +00001490static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001491 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001492{
1493 struct xfrm_policy_afinfo *afinfo =
1494 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1495 int err;
1496
1497 if (!afinfo)
1498 return -EINVAL;
1499
Herbert Xu87c1e122010-03-02 02:51:56 +00001500 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001501
1502 xfrm_policy_put_afinfo(afinfo);
1503
1504 return err;
1505}
1506
Timo Teräs80c802f2010-04-07 00:30:05 +00001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1509 * all the metrics... Shortly, bundle a bundle.
1510 */
1511
Herbert Xu25ee3282007-12-11 09:32:34 -08001512static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1513 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001514 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001515 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001517 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001518 unsigned long now = jiffies;
1519 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001520 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001521 struct dst_entry *dst_prev = NULL;
1522 struct dst_entry *dst0 = NULL;
1523 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001525 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001526 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001527 int trailer_len = 0;
1528 int tos;
1529 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001530 xfrm_address_t saddr, daddr;
1531
1532 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001533
1534 tos = xfrm_get_tos(fl, family);
1535 err = tos;
1536 if (tos < 0)
1537 goto put_states;
1538
1539 dst_hold(dst);
1540
1541 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001542 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001543 struct dst_entry *dst1 = &xdst->u.dst;
1544
1545 err = PTR_ERR(xdst);
1546 if (IS_ERR(xdst)) {
1547 dst_release(dst);
1548 goto put_states;
1549 }
1550
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001551 if (xfrm[i]->sel.family == AF_UNSPEC) {
1552 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1553 xfrm_af2proto(family));
1554 if (!inner_mode) {
1555 err = -EAFNOSUPPORT;
1556 dst_release(dst);
1557 goto put_states;
1558 }
1559 } else
1560 inner_mode = xfrm[i]->inner_mode;
1561
Herbert Xu25ee3282007-12-11 09:32:34 -08001562 if (!dst_prev)
1563 dst0 = dst1;
1564 else {
1565 dst_prev->child = dst_clone(dst1);
1566 dst1->flags |= DST_NOHASH;
1567 }
1568
1569 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001570 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001571
1572 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1573 family = xfrm[i]->props.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001574 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1575 family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001576 err = PTR_ERR(dst);
1577 if (IS_ERR(dst))
1578 goto put_states;
1579 } else
1580 dst_hold(dst);
1581
1582 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001583 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001584
David S. Millerf5b0a872012-07-19 12:31:33 -07001585 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001586 dst1->flags |= DST_HOST;
1587 dst1->lastuse = now;
1588
1589 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001590 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001591
1592 dst1->next = dst_prev;
1593 dst_prev = dst1;
1594
1595 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001596 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1597 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001598 trailer_len += xfrm[i]->props.trailer_len;
1599 }
1600
1601 dst_prev->child = dst;
1602 dst0->path = dst;
1603
1604 err = -ENODEV;
1605 dev = dst->dev;
1606 if (!dev)
1607 goto free_dst;
1608
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001609 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001610 xfrm_init_pmtu(dst_prev);
1611
1612 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1613 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1614
Herbert Xu87c1e122010-03-02 02:51:56 +00001615 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001616 if (err)
1617 goto free_dst;
1618
1619 dst_prev->header_len = header_len;
1620 dst_prev->trailer_len = trailer_len;
1621 header_len -= xdst->u.dst.xfrm->props.header_len;
1622 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1623 }
1624
1625out:
1626 return dst0;
1627
1628put_states:
1629 for (; i < nx; i++)
1630 xfrm_state_put(xfrm[i]);
1631free_dst:
1632 if (dst0)
1633 dst_free(dst0);
1634 dst0 = ERR_PTR(err);
1635 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001638static int inline
David S. Miller3f0e18f2011-02-22 18:38:14 -08001639xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001640{
1641 if (!*target) {
1642 *target = kmalloc(size, GFP_ATOMIC);
1643 if (!*target)
1644 return -ENOMEM;
1645 }
1646 memcpy(*target, src, size);
1647 return 0;
1648}
1649
1650static int inline
David S. Miller1786b382011-02-24 01:32:54 -05001651xfrm_dst_update_parent(struct dst_entry *dst, const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001652{
1653#ifdef CONFIG_XFRM_SUB_POLICY
1654 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1655 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1656 sel, sizeof(*sel));
1657#else
1658 return 0;
1659#endif
1660}
1661
1662static int inline
David S. Miller3f0e18f2011-02-22 18:38:14 -08001663xfrm_dst_update_origin(struct dst_entry *dst, const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001664{
1665#ifdef CONFIG_XFRM_SUB_POLICY
1666 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1667 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1668#else
1669 return 0;
1670#endif
1671}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
David S. Miller73ff93c2011-02-22 18:33:42 -08001673static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001674 struct xfrm_policy **pols,
1675 int *num_pols, int *num_xfrms)
1676{
1677 int i;
1678
1679 if (*num_pols == 0 || !pols[0]) {
1680 *num_pols = 0;
1681 *num_xfrms = 0;
1682 return 0;
1683 }
1684 if (IS_ERR(pols[0]))
1685 return PTR_ERR(pols[0]);
1686
1687 *num_xfrms = pols[0]->xfrm_nr;
1688
1689#ifdef CONFIG_XFRM_SUB_POLICY
1690 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1691 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1692 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1693 XFRM_POLICY_TYPE_MAIN,
1694 fl, family,
1695 XFRM_POLICY_OUT);
1696 if (pols[1]) {
1697 if (IS_ERR(pols[1])) {
1698 xfrm_pols_put(pols, *num_pols);
1699 return PTR_ERR(pols[1]);
1700 }
1701 (*num_pols) ++;
1702 (*num_xfrms) += pols[1]->xfrm_nr;
1703 }
1704 }
1705#endif
1706 for (i = 0; i < *num_pols; i++) {
1707 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1708 *num_xfrms = -1;
1709 break;
1710 }
1711 }
1712
1713 return 0;
1714
1715}
1716
1717static struct xfrm_dst *
1718xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001719 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001720 struct dst_entry *dst_orig)
1721{
1722 struct net *net = xp_net(pols[0]);
1723 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1724 struct dst_entry *dst;
1725 struct xfrm_dst *xdst;
1726 int err;
1727
1728 /* Try to instantiate a bundle */
1729 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001730 if (err <= 0) {
1731 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001732 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1733 return ERR_PTR(err);
1734 }
1735
1736 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1737 if (IS_ERR(dst)) {
1738 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1739 return ERR_CAST(dst);
1740 }
1741
1742 xdst = (struct xfrm_dst *)dst;
1743 xdst->num_xfrms = err;
1744 if (num_pols > 1)
1745 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1746 else
1747 err = xfrm_dst_update_origin(dst, fl);
1748 if (unlikely(err)) {
1749 dst_free(dst);
1750 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1751 return ERR_PTR(err);
1752 }
1753
1754 xdst->num_pols = num_pols;
1755 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
1756 xdst->policy_genid = atomic_read(&pols[0]->genid);
1757
1758 return xdst;
1759}
1760
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001761static void xfrm_policy_queue_process(unsigned long arg)
1762{
1763 int err = 0;
1764 struct sk_buff *skb;
1765 struct sock *sk;
1766 struct dst_entry *dst;
1767 struct net_device *dev;
1768 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1769 struct xfrm_policy_queue *pq = &pol->polq;
1770 struct flowi fl;
1771 struct sk_buff_head list;
1772
1773 spin_lock(&pq->hold_queue.lock);
1774 skb = skb_peek(&pq->hold_queue);
1775 dst = skb_dst(skb);
1776 sk = skb->sk;
1777 xfrm_decode_session(skb, &fl, dst->ops->family);
1778 spin_unlock(&pq->hold_queue.lock);
1779
1780 dst_hold(dst->path);
1781 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1782 sk, 0);
1783 if (IS_ERR(dst))
1784 goto purge_queue;
1785
1786 if (dst->flags & DST_XFRM_QUEUE) {
1787 dst_release(dst);
1788
1789 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1790 goto purge_queue;
1791
1792 pq->timeout = pq->timeout << 1;
1793 mod_timer(&pq->hold_timer, jiffies + pq->timeout);
1794 return;
1795 }
1796
1797 dst_release(dst);
1798
1799 __skb_queue_head_init(&list);
1800
1801 spin_lock(&pq->hold_queue.lock);
1802 pq->timeout = 0;
1803 skb_queue_splice_init(&pq->hold_queue, &list);
1804 spin_unlock(&pq->hold_queue.lock);
1805
1806 while (!skb_queue_empty(&list)) {
1807 skb = __skb_dequeue(&list);
1808
1809 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1810 dst_hold(skb_dst(skb)->path);
1811 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1812 &fl, skb->sk, 0);
1813 if (IS_ERR(dst)) {
1814 dev_put(skb->dev);
1815 kfree_skb(skb);
1816 continue;
1817 }
1818
1819 nf_reset(skb);
1820 skb_dst_drop(skb);
1821 skb_dst_set(skb, dst);
1822
1823 dev = skb->dev;
1824 err = dst_output(skb);
1825 dev_put(dev);
1826 }
1827
1828 return;
1829
1830purge_queue:
1831 pq->timeout = 0;
1832 xfrm_queue_purge(&pq->hold_queue);
1833}
1834
1835static int xdst_queue_output(struct sk_buff *skb)
1836{
1837 unsigned long sched_next;
1838 struct dst_entry *dst = skb_dst(skb);
1839 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
1840 struct xfrm_policy_queue *pq = &xdst->pols[0]->polq;
1841
1842 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1843 kfree_skb(skb);
1844 return -EAGAIN;
1845 }
1846
1847 skb_dst_force(skb);
1848 dev_hold(skb->dev);
1849
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) {
2135 /* EREMOTE tells the caller to generate
2136 * a one-shot blackhole route. */
2137 dst_release(dst);
Timo Terasa1aa3482010-05-15 23:49:26 -07002138 xfrm_pols_put(pols, drop_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002139 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
David S. Miller2774c132011-03-01 14:59:04 -08002140
David S. Miller452edd52011-03-02 13:27:41 -08002141 return make_blackhole(net, family, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002142 }
David S. Miller1d28f422011-03-12 00:29:39 -05002143 if (fl->flowi_flags & FLOWI_FLAG_CAN_SLEEP) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002144 DECLARE_WAITQUEUE(wait, current);
2145
2146 add_wait_queue(&net->xfrm.km_waitq, &wait);
2147 set_current_state(TASK_INTERRUPTIBLE);
2148 schedule();
2149 set_current_state(TASK_RUNNING);
2150 remove_wait_queue(&net->xfrm.km_waitq, &wait);
2151
2152 if (!signal_pending(current)) {
2153 dst_release(dst);
2154 goto restart;
2155 }
2156
2157 err = -ERESTART;
2158 } else
2159 err = -EAGAIN;
2160
2161 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2162 goto error;
2163 }
2164
2165no_transform:
2166 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002167 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Timo Teräs80c802f2010-04-07 00:30:05 +00002169 if ((flags & XFRM_LOOKUP_ICMP) &&
2170 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2171 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002172 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002173 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002174
Timo Teräs80c802f2010-04-07 00:30:05 +00002175 for (i = 0; i < num_pols; i++)
2176 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002177
Timo Teräs80c802f2010-04-07 00:30:05 +00002178 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002180 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002181 err = -EPERM;
2182 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002183 } else if (num_xfrms > 0) {
2184 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002185 dst_release(dst_orig);
2186 } else {
2187 /* Flow passes untransformed */
2188 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002189 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002191ok:
2192 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002193 if (dst && dst->xfrm &&
2194 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2195 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002196 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Timo Teräs80c802f2010-04-07 00:30:05 +00002198nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002199 if (!(flags & XFRM_LOOKUP_ICMP)) {
2200 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002201 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002202 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002203 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002205 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002206dropdst:
2207 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002208 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002209 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211EXPORT_SYMBOL(xfrm_lookup);
2212
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002213static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002214xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002215{
2216 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002217
2218 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2219 return 0;
2220 x = skb->sp->xvec[idx];
2221 if (!x->type->reject)
2222 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002223 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002224}
2225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226/* When skb is transformed back to its "native" form, we have to
2227 * check policy restrictions. At the moment we make this in maximally
2228 * stupid way. Shame on me. :-) Of course, connected sockets must
2229 * have policy cached at them.
2230 */
2231
2232static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002233xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 unsigned short family)
2235{
2236 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002237 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return x->id.proto == tmpl->id.proto &&
2239 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2240 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2241 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002242 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002243 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002244 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2245 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002248/*
2249 * 0 or more than 0 is returned when validation is succeeded (either bypass
2250 * because of optional transport mode, or next index of the mathced secpath
2251 * state with the template.
2252 * -1 is returned when no matching template is found.
2253 * Otherwise "-2 - errored_index" is returned.
2254 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002256xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 unsigned short family)
2258{
2259 int idx = start;
2260
2261 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002262 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 return start;
2264 } else
2265 start = -1;
2266 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002267 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002269 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2270 if (start == -1)
2271 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
2275 return start;
2276}
2277
Herbert Xud5422ef2007-12-12 10:44:16 -08002278int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2279 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
2281 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002282 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284 if (unlikely(afinfo == NULL))
2285 return -EAFNOSUPPORT;
2286
Herbert Xud5422ef2007-12-12 10:44:16 -08002287 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002288 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002290 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
Herbert Xud5422ef2007-12-12 10:44:16 -08002292EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
David S. Miller9a7386e2011-02-24 01:44:12 -05002294static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
2296 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002297 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002298 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
2302
2303 return 0;
2304}
2305
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002306int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 unsigned short family)
2308{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002309 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002311 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2312 int npols = 0;
2313 int xfrm_nr;
2314 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002315 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002317 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002318 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Herbert Xud5422ef2007-12-12 10:44:16 -08002320 reverse = dir & ~XFRM_POLICY_MASK;
2321 dir &= XFRM_POLICY_MASK;
2322 fl_dir = policy_to_flow_dir(dir);
2323
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002324 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002325 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002327 }
2328
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002329 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
2331 /* First, check used SA against their selectors. */
2332 if (skb->sp) {
2333 int i;
2334
2335 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002336 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002337 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002338 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 }
2342 }
2343
2344 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002345 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002346 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002347 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002348 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002349 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002350 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002353 if (!pol) {
2354 struct flow_cache_object *flo;
2355
2356 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2357 xfrm_policy_lookup, NULL);
2358 if (IS_ERR_OR_NULL(flo))
2359 pol = ERR_CAST(flo);
2360 else
2361 pol = container_of(flo, struct xfrm_policy, flo);
2362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002364 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002365 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002366 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002367 }
James Morris134b0fc2006-10-05 15:42:27 -05002368
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002369 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002370 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002371 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002372 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002373 return 0;
2374 }
2375 return 1;
2376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
James Morris9d729f72007-03-04 16:12:44 -08002378 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002380 pols[0] = pol;
2381 npols ++;
2382#ifdef CONFIG_XFRM_SUB_POLICY
2383 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002384 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002385 &fl, family,
2386 XFRM_POLICY_IN);
2387 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002388 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002389 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002390 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002391 }
James Morris9d729f72007-03-04 16:12:44 -08002392 pols[1]->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002393 npols ++;
2394 }
2395 }
2396#endif
2397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (pol->action == XFRM_POLICY_ALLOW) {
2399 struct sec_path *sp;
2400 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002401 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002402 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002403 struct xfrm_tmpl **tpp = tp;
2404 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 int i, k;
2406
2407 if ((sp = skb->sp) == NULL)
2408 sp = &dummy;
2409
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002410 for (pi = 0; pi < npols; pi++) {
2411 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002412 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002413 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002414 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002415 }
2416 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002417 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002418 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002419 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002420 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2421 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2422 }
2423 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002424 if (npols > 1) {
2425 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
2426 tpp = stp;
2427 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 /* For each tunnel xfrm, find the first matching tmpl.
2430 * For each tmpl before that, find corresponding xfrm.
2431 * Order is _important_. Later we will implement
2432 * some barriers, but at the moment barriers
2433 * are implied between each two transformations.
2434 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002435 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2436 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002437 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002438 if (k < -1)
2439 /* "-2 - errored_index" returned */
2440 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002441 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
2445
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002446 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002447 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002451 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 return 1;
2453 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002454 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002457 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002458reject_error:
2459 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 return 0;
2461}
2462EXPORT_SYMBOL(__xfrm_policy_check);
2463
2464int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2465{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002466 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002468 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002469 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002471 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002472 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002476 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002477
David S. Miller452edd52011-03-02 13:27:41 -08002478 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, 0);
2479 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002480 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002481 dst = NULL;
2482 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002483 skb_dst_set(skb, dst);
2484 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
2486EXPORT_SYMBOL(__xfrm_route_forward);
2487
David S. Millerd49c73c2006-08-13 18:55:53 -07002488/* Optimize later using cookies and generation ids. */
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2491{
David S. Millerd49c73c2006-08-13 18:55:53 -07002492 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002493 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2494 * get validated by dst_ops->check on every use. We do this
2495 * because when a normal route referenced by an XFRM dst is
2496 * obsoleted we do not go looking around for all parent
2497 * referencing XFRM dsts so that we can invalidate them. It
2498 * is just too much work. Instead we make the checks here on
2499 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002500 *
2501 * XFRM dst A --> IPv4 dst X
2502 *
2503 * X is the "xdst->route" of A (X is also the "dst->path" of A
2504 * in this example). If X is marked obsolete, "A" will not
2505 * notice. That's what we are validating here via the
2506 * stale_bundle() check.
2507 *
2508 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002509 * dst which causes it's ->obsolete field to be set to
2510 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2511 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002512 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002513 if (dst->obsolete < 0 && !stale_bundle(dst))
2514 return dst;
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return NULL;
2517}
2518
2519static int stale_bundle(struct dst_entry *dst)
2520{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002521 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522}
2523
Herbert Xuaabc9762005-05-03 16:27:10 -07002524void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002527 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002528 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 dev_put(dev);
2530 }
2531}
Herbert Xuaabc9762005-05-03 16:27:10 -07002532EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534static void xfrm_link_failure(struct sk_buff *skb)
2535{
2536 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537}
2538
2539static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2540{
2541 if (dst) {
2542 if (dst->obsolete) {
2543 dst_release(dst);
2544 dst = NULL;
2545 }
2546 }
2547 return dst;
2548}
2549
Alexey Dobriyanddcfd792008-11-25 17:37:23 -08002550static void __xfrm_garbage_collect(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
Timo Teräs80c802f2010-04-07 00:30:05 +00002552 struct dst_entry *head, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Timo Teräs80c802f2010-04-07 00:30:05 +00002554 spin_lock_bh(&xfrm_policy_sk_bundle_lock);
2555 head = xfrm_policy_sk_bundles;
2556 xfrm_policy_sk_bundles = NULL;
2557 spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
2558
2559 while (head) {
2560 next = head->next;
2561 dst_free(head);
2562 head = next;
2563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564}
2565
Paul Mooree4c17212013-05-29 07:36:25 +00002566void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002567{
2568 flow_cache_flush();
2569 __xfrm_garbage_collect(net);
2570}
Paul Mooree4c17212013-05-29 07:36:25 +00002571EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002572
2573static void xfrm_garbage_collect_deferred(struct net *net)
2574{
2575 flow_cache_flush_deferred();
2576 __xfrm_garbage_collect(net);
2577}
2578
Herbert Xu25ee3282007-12-11 09:32:34 -08002579static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
2581 do {
2582 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2583 u32 pmtu, route_mtu_cached;
2584
2585 pmtu = dst_mtu(dst->child);
2586 xdst->child_mtu_cached = pmtu;
2587
2588 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2589
2590 route_mtu_cached = dst_mtu(xdst->route);
2591 xdst->route_mtu_cached = route_mtu_cached;
2592
2593 if (pmtu > route_mtu_cached)
2594 pmtu = route_mtu_cached;
2595
David S. Millerdefb3512010-12-08 21:16:57 -08002596 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 } while ((dst = dst->next));
2598}
2599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600/* Check that the bundle accepts the flow and its components are
2601 * still valid.
2602 */
2603
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002604static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605{
2606 struct dst_entry *dst = &first->u.dst;
2607 struct xfrm_dst *last;
2608 u32 mtu;
2609
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002610 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 (dst->dev && !netif_running(dst->dev)))
2612 return 0;
2613
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002614 if (dst->flags & DST_XFRM_QUEUE)
2615 return 1;
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 last = NULL;
2618
2619 do {
2620 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2623 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002624 if (xdst->xfrm_genid != dst->xfrm->genid)
2625 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002626 if (xdst->num_pols > 0 &&
2627 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 mtu = dst_mtu(dst->child);
2631 if (xdst->child_mtu_cached != mtu) {
2632 last = xdst;
2633 xdst->child_mtu_cached = mtu;
2634 }
2635
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002636 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 return 0;
2638 mtu = dst_mtu(xdst->route);
2639 if (xdst->route_mtu_cached != mtu) {
2640 last = xdst;
2641 xdst->route_mtu_cached = mtu;
2642 }
2643
2644 dst = dst->child;
2645 } while (dst->xfrm);
2646
2647 if (likely(!last))
2648 return 1;
2649
2650 mtu = last->child_mtu_cached;
2651 for (;;) {
2652 dst = &last->u.dst;
2653
2654 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2655 if (mtu > last->route_mtu_cached)
2656 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002657 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
2659 if (last == first)
2660 break;
2661
Patrick McHardybd0bf072007-07-18 01:55:52 -07002662 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 last->child_mtu_cached = mtu;
2664 }
2665
2666 return 1;
2667}
2668
David S. Miller0dbaee32010-12-13 12:52:14 -08002669static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2670{
2671 return dst_metric_advmss(dst->path);
2672}
2673
Steffen Klassertebb762f2011-11-23 02:12:51 +00002674static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002675{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002676 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2677
2678 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002679}
2680
David S. Millerf894cbf2012-07-02 21:52:24 -07002681static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2682 struct sk_buff *skb,
2683 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002684{
David S. Millerf894cbf2012-07-02 21:52:24 -07002685 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002686}
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2689{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002690 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 int err = 0;
2692 if (unlikely(afinfo == NULL))
2693 return -EINVAL;
2694 if (unlikely(afinfo->family >= NPROTO))
2695 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002696 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2698 err = -ENOBUFS;
2699 else {
2700 struct dst_ops *dst_ops = afinfo->dst_ops;
2701 if (likely(dst_ops->kmem_cachep == NULL))
2702 dst_ops->kmem_cachep = xfrm_dst_cache;
2703 if (likely(dst_ops->check == NULL))
2704 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002705 if (likely(dst_ops->default_advmss == NULL))
2706 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002707 if (likely(dst_ops->mtu == NULL))
2708 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 if (likely(dst_ops->negative_advice == NULL))
2710 dst_ops->negative_advice = xfrm_negative_advice;
2711 if (likely(dst_ops->link_failure == NULL))
2712 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002713 if (likely(dst_ops->neigh_lookup == NULL))
2714 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002716 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002717 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002719 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002720
2721 rtnl_lock();
2722 for_each_net(net) {
2723 struct dst_ops *xfrm_dst_ops;
2724
2725 switch (afinfo->family) {
2726 case AF_INET:
2727 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2728 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002729#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002730 case AF_INET6:
2731 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2732 break;
2733#endif
2734 default:
2735 BUG();
2736 }
2737 *xfrm_dst_ops = *afinfo->dst_ops;
2738 }
2739 rtnl_unlock();
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return err;
2742}
2743EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2744
2745int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2746{
2747 int err = 0;
2748 if (unlikely(afinfo == NULL))
2749 return -EINVAL;
2750 if (unlikely(afinfo->family >= NPROTO))
2751 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002752 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2754 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2755 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002756 else
2757 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2758 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002760 spin_unlock(&xfrm_policy_afinfo_lock);
2761 if (!err) {
2762 struct dst_ops *dst_ops = afinfo->dst_ops;
2763
2764 synchronize_rcu();
2765
2766 dst_ops->kmem_cachep = NULL;
2767 dst_ops->check = NULL;
2768 dst_ops->negative_advice = NULL;
2769 dst_ops->link_failure = NULL;
2770 afinfo->garbage_collect = NULL;
2771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return err;
2773}
2774EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2775
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002776static void __net_init xfrm_dst_ops_init(struct net *net)
2777{
2778 struct xfrm_policy_afinfo *afinfo;
2779
Eric Dumazetef8531b2012-08-19 12:31:48 +02002780 rcu_read_lock();
2781 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002782 if (afinfo)
2783 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002784#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetef8531b2012-08-19 12:31:48 +02002785 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002786 if (afinfo)
2787 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2788#endif
Priyanka Jain418a99a2012-08-12 21:22:29 +00002789 rcu_read_unlock();
Herbert Xu546be242006-05-27 23:03:58 -07002790}
2791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2793{
Jiri Pirko351638e2013-05-28 01:30:21 +00002794 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002795
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 switch (event) {
2797 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002798 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 }
2800 return NOTIFY_DONE;
2801}
2802
2803static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002804 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805};
2806
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002807#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002808static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002809{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002810 int rv;
2811
Tejun Heo7d720c32010-02-16 15:20:26 +00002812 if (snmp_mib_init((void __percpu **)net->mib.xfrm_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00002813 sizeof(struct linux_xfrm_mib),
2814 __alignof__(struct linux_xfrm_mib)) < 0)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002815 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002816 rv = xfrm_proc_init(net);
2817 if (rv < 0)
Tejun Heo7d720c32010-02-16 15:20:26 +00002818 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002819 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002820}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002821
2822static void xfrm_statistics_fini(struct net *net)
2823{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002824 xfrm_proc_fini(net);
Tejun Heo7d720c32010-02-16 15:20:26 +00002825 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002826}
2827#else
2828static int __net_init xfrm_statistics_init(struct net *net)
2829{
2830 return 0;
2831}
2832
2833static void xfrm_statistics_fini(struct net *net)
2834{
2835}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002836#endif
2837
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002838static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
David S. Miller2518c7c2006-08-24 04:45:07 -07002840 unsigned int hmask, sz;
2841 int dir;
2842
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002843 if (net_eq(net, &init_net))
2844 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002846 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002847 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
David S. Miller2518c7c2006-08-24 04:45:07 -07002849 hmask = 8 - 1;
2850 sz = (hmask+1) * sizeof(struct hlist_head);
2851
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002852 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2853 if (!net->xfrm.policy_byidx)
2854 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002855 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002856
2857 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2858 struct xfrm_policy_hash *htab;
2859
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002860 net->xfrm.policy_count[dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002861 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002862
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002863 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002864 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002865 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002866 goto out_bydst;
2867 htab->hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002868 }
2869
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002870 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002871 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002872 if (net_eq(net, &init_net))
2873 register_netdevice_notifier(&xfrm_dev_notifier);
2874 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002875
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002876out_bydst:
2877 for (dir--; dir >= 0; dir--) {
2878 struct xfrm_policy_hash *htab;
2879
2880 htab = &net->xfrm.policy_bydst[dir];
2881 xfrm_hash_free(htab->table, sz);
2882 }
2883 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002884out_byidx:
2885 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886}
2887
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002888static void xfrm_policy_fini(struct net *net)
2889{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002890 struct xfrm_audit audit_info;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002891 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002892 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002893
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002894 flush_work(&net->xfrm.policy_hash_work);
2895#ifdef CONFIG_XFRM_SUB_POLICY
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002896 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002897 audit_info.sessionid = -1;
2898 audit_info.secid = 0;
2899 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, &audit_info);
2900#endif
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002901 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002902 audit_info.sessionid = -1;
2903 audit_info.secid = 0;
2904 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002905
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002906 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002907
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002908 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002909 struct xfrm_policy_hash *htab;
2910
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002911 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002912
2913 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01002914 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002915 WARN_ON(!hlist_empty(htab->table));
2916 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002917 }
2918
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002919 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002920 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2921 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002922}
2923
2924static int __net_init xfrm_net_init(struct net *net)
2925{
2926 int rv;
2927
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002928 rv = xfrm_statistics_init(net);
2929 if (rv < 0)
2930 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002931 rv = xfrm_state_init(net);
2932 if (rv < 0)
2933 goto out_state;
2934 rv = xfrm_policy_init(net);
2935 if (rv < 0)
2936 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002937 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002938 rv = xfrm_sysctl_init(net);
2939 if (rv < 0)
2940 goto out_sysctl;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002941 return 0;
2942
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002943out_sysctl:
2944 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002945out_policy:
2946 xfrm_state_fini(net);
2947out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002948 xfrm_statistics_fini(net);
2949out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002950 return rv;
2951}
2952
2953static void __net_exit xfrm_net_exit(struct net *net)
2954{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002955 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002956 xfrm_policy_fini(net);
2957 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002958 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002959}
2960
2961static struct pernet_operations __net_initdata xfrm_net_ops = {
2962 .init = xfrm_net_init,
2963 .exit = xfrm_net_exit,
2964};
2965
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966void __init xfrm_init(void)
2967{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002968 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 xfrm_input_init();
2970}
2971
Joy Lattenab5f5e82007-09-17 11:51:22 -07002972#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08002973static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2974 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002975{
Paul Moore875179f2007-12-01 23:27:18 +11002976 struct xfrm_sec_ctx *ctx = xp->security;
2977 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002978
Paul Moore875179f2007-12-01 23:27:18 +11002979 if (ctx)
2980 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2981 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2982
2983 switch(sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002984 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002985 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002986 if (sel->prefixlen_s != 32)
2987 audit_log_format(audit_buf, " src_prefixlen=%d",
2988 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002989 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002990 if (sel->prefixlen_d != 32)
2991 audit_log_format(audit_buf, " dst_prefixlen=%d",
2992 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002993 break;
2994 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002995 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002996 if (sel->prefixlen_s != 128)
2997 audit_log_format(audit_buf, " src_prefixlen=%d",
2998 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002999 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003000 if (sel->prefixlen_d != 128)
3001 audit_log_format(audit_buf, " dst_prefixlen=%d",
3002 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003003 break;
3004 }
3005}
3006
Paul Moore68277ac2007-12-20 20:49:33 -08003007void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07003008 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003009{
3010 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003011
Paul Mooreafeb14b2007-12-21 14:58:11 -08003012 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003013 if (audit_buf == NULL)
3014 return;
Eric Paris25323862008-04-18 10:09:25 -04003015 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003016 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003017 xfrm_audit_common_policyinfo(xp, audit_buf);
3018 audit_log_end(audit_buf);
3019}
3020EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3021
Paul Moore68277ac2007-12-20 20:49:33 -08003022void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07003023 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003024{
3025 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003026
Paul Mooreafeb14b2007-12-21 14:58:11 -08003027 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003028 if (audit_buf == NULL)
3029 return;
Eric Paris25323862008-04-18 10:09:25 -04003030 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003031 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003032 xfrm_audit_common_policyinfo(xp, audit_buf);
3033 audit_log_end(audit_buf);
3034}
3035EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3036#endif
3037
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003038#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003039static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3040 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003041{
3042 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3043 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003044 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3045 sel_cmp->family) &&
3046 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3047 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003048 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3049 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003050 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003051 }
3052 } else {
3053 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003054 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003055 }
3056 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003057 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003058}
3059
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003060static struct xfrm_policy * xfrm_migrate_policy_find(const struct xfrm_selector *sel,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003061 u8 dir, u8 type)
3062{
3063 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003064 struct hlist_head *chain;
3065 u32 priority = ~0U;
3066
3067 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -08003068 chain = policy_hash_direct(&init_net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003069 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003070 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3071 pol->type == type) {
3072 ret = pol;
3073 priority = ret->priority;
3074 break;
3075 }
3076 }
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003077 chain = &init_net.xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003078 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003079 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3080 pol->type == type &&
3081 pol->priority < priority) {
3082 ret = pol;
3083 break;
3084 }
3085 }
3086
3087 if (ret)
3088 xfrm_pol_hold(ret);
3089
3090 read_unlock_bh(&xfrm_policy_lock);
3091
3092 return ret;
3093}
3094
David S. Millerdd701752011-02-24 00:21:08 -05003095static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003096{
3097 int match = 0;
3098
3099 if (t->mode == m->mode && t->id.proto == m->proto &&
3100 (m->reqid == 0 || t->reqid == m->reqid)) {
3101 switch (t->mode) {
3102 case XFRM_MODE_TUNNEL:
3103 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003104 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3105 m->old_family) &&
3106 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3107 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003108 match = 1;
3109 }
3110 break;
3111 case XFRM_MODE_TRANSPORT:
3112 /* in case of transport mode, template does not store
3113 any IP addresses, hence we just compare mode and
3114 protocol */
3115 match = 1;
3116 break;
3117 default:
3118 break;
3119 }
3120 }
3121 return match;
3122}
3123
3124/* update endpoint address(es) of template(s) */
3125static int xfrm_policy_migrate(struct xfrm_policy *pol,
3126 struct xfrm_migrate *m, int num_migrate)
3127{
3128 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003129 int i, j, n = 0;
3130
3131 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003132 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003133 /* target policy has been deleted */
3134 write_unlock_bh(&pol->lock);
3135 return -ENOENT;
3136 }
3137
3138 for (i = 0; i < pol->xfrm_nr; i++) {
3139 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3140 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3141 continue;
3142 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003143 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3144 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003145 continue;
3146 /* update endpoints */
3147 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3148 sizeof(pol->xfrm_vec[i].id.daddr));
3149 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3150 sizeof(pol->xfrm_vec[i].saddr));
3151 pol->xfrm_vec[i].encap_family = mp->new_family;
3152 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003153 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003154 }
3155 }
3156
3157 write_unlock_bh(&pol->lock);
3158
3159 if (!n)
3160 return -ENODATA;
3161
3162 return 0;
3163}
3164
David S. Millerdd701752011-02-24 00:21:08 -05003165static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003166{
3167 int i, j;
3168
3169 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3170 return -EINVAL;
3171
3172 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003173 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3174 m[i].old_family) &&
3175 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3176 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003177 return -EINVAL;
3178 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3179 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3180 return -EINVAL;
3181
3182 /* check if there is any duplicated entry */
3183 for (j = i + 1; j < num_migrate; j++) {
3184 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3185 sizeof(m[i].old_daddr)) &&
3186 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3187 sizeof(m[i].old_saddr)) &&
3188 m[i].proto == m[j].proto &&
3189 m[i].mode == m[j].mode &&
3190 m[i].reqid == m[j].reqid &&
3191 m[i].old_family == m[j].old_family)
3192 return -EINVAL;
3193 }
3194 }
3195
3196 return 0;
3197}
3198
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003199int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003200 struct xfrm_migrate *m, int num_migrate,
3201 struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003202{
3203 int i, err, nx_cur = 0, nx_new = 0;
3204 struct xfrm_policy *pol = NULL;
3205 struct xfrm_state *x, *xc;
3206 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3207 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3208 struct xfrm_migrate *mp;
3209
3210 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3211 goto out;
3212
3213 /* Stage 1 - find policy */
3214 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
3215 err = -ENOENT;
3216 goto out;
3217 }
3218
3219 /* Stage 2 - find and update state(s) */
3220 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
3221 if ((x = xfrm_migrate_state_find(mp))) {
3222 x_cur[nx_cur] = x;
3223 nx_cur++;
3224 if ((xc = xfrm_state_migrate(x, mp))) {
3225 x_new[nx_new] = xc;
3226 nx_new++;
3227 } else {
3228 err = -ENODATA;
3229 goto restore_state;
3230 }
3231 }
3232 }
3233
3234 /* Stage 3 - update policy */
3235 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3236 goto restore_state;
3237
3238 /* Stage 4 - delete old state(s) */
3239 if (nx_cur) {
3240 xfrm_states_put(x_cur, nx_cur);
3241 xfrm_states_delete(x_cur, nx_cur);
3242 }
3243
3244 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003245 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003246
3247 xfrm_pol_put(pol);
3248
3249 return 0;
3250out:
3251 return err;
3252
3253restore_state:
3254 if (pol)
3255 xfrm_pol_put(pol);
3256 if (nx_cur)
3257 xfrm_states_put(x_cur, nx_cur);
3258 if (nx_new)
3259 xfrm_states_delete(x_new, nx_new);
3260
3261 return err;
3262}
David S. Millere610e672007-02-08 13:29:15 -08003263EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003264#endif