blob: e205c4b56afb4d5ff8ed720a0e9759b2a47491c5 [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
Timo Teräs80c802f2010-04-07 00:30:05 +000042static struct dst_entry *xfrm_policy_sk_bundles;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Priyanka Jain418a99a2012-08-12 21:22:29 +000044static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
45static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO]
46 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Christoph Lametere18b8902006-12-06 20:33:20 -080048static struct kmem_cache *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Herbert Xu25ee3282007-12-11 09:32:34 -080050static void xfrm_init_pmtu(struct dst_entry *dst);
Timo Teräs80c802f2010-04-07 00:30:05 +000051static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +000052static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Steffen Klasserta0073fe2013-02-05 12:52:55 +010053static void xfrm_policy_queue_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Wei Yongjun29fa0b302008-12-03 00:33:09 -080055static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
56 int dir);
57
David S. Millerbc9b35a2012-05-15 15:04:57 -040058static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050059__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080060{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050061 const struct flowi4 *fl4 = &fl->u.ip4;
62
Alexey Dobriyan26bff942011-11-22 06:46:02 +000063 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
64 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -050065 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
66 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
67 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
68 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080069}
70
David S. Millerbc9b35a2012-05-15 15:04:57 -040071static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050072__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080073{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050074 const struct flowi6 *fl6 = &fl->u.ip6;
75
76 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
77 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
78 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
79 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
80 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
81 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080082}
83
David S. Millerbc9b35a2012-05-15 15:04:57 -040084bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
85 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -080086{
87 switch (family) {
88 case AF_INET:
89 return __xfrm4_selector_match(sel, fl);
90 case AF_INET6:
91 return __xfrm6_selector_match(sel, fl);
92 }
David S. Millerbc9b35a2012-05-15 15:04:57 -040093 return false;
Andrew Morton77681022006-11-08 22:46:26 -080094}
95
Eric Dumazetef8531b2012-08-19 12:31:48 +020096static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
97{
98 struct xfrm_policy_afinfo *afinfo;
99
100 if (unlikely(family >= NPROTO))
101 return NULL;
102 rcu_read_lock();
103 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
104 if (unlikely(!afinfo))
105 rcu_read_unlock();
106 return afinfo;
107}
108
109static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
110{
111 rcu_read_unlock();
112}
113
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800114static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos,
David S. Miller6418c4e2011-02-24 00:16:53 -0500115 const xfrm_address_t *saddr,
116 const xfrm_address_t *daddr,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900117 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800119 struct xfrm_policy_afinfo *afinfo;
120 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Herbert Xu25ee3282007-12-11 09:32:34 -0800122 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800124 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800126 dst = afinfo->dst_lookup(net, tos, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900129
130 return dst;
131}
132
133static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
134 xfrm_address_t *prev_saddr,
135 xfrm_address_t *prev_daddr,
136 int family)
137{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800138 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900139 xfrm_address_t *saddr = &x->props.saddr;
140 xfrm_address_t *daddr = &x->id.daddr;
141 struct dst_entry *dst;
142
143 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
144 saddr = x->coaddr;
145 daddr = prev_daddr;
146 }
147 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
148 saddr = prev_saddr;
149 daddr = x->coaddr;
150 }
151
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800152 dst = __xfrm_dst_lookup(net, tos, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900153
154 if (!IS_ERR(dst)) {
155 if (prev_saddr != saddr)
156 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
157 if (prev_daddr != daddr)
158 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
159 }
160
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800161 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static inline unsigned long make_jiffies(long secs)
165{
166 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
167 return MAX_SCHEDULE_TIMEOUT-1;
168 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900169 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172static void xfrm_policy_timer(unsigned long data)
173{
174 struct xfrm_policy *xp = (struct xfrm_policy*)data;
James Morris9d729f72007-03-04 16:12:44 -0800175 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 long next = LONG_MAX;
177 int warn = 0;
178 int dir;
179
180 read_lock(&xp->lock);
181
Timo Teräsea2dea92010-03-31 00:17:05 +0000182 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 goto out;
184
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700185 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 if (xp->lft.hard_add_expires_seconds) {
188 long tmo = xp->lft.hard_add_expires_seconds +
189 xp->curlft.add_time - now;
190 if (tmo <= 0)
191 goto expired;
192 if (tmo < next)
193 next = tmo;
194 }
195 if (xp->lft.hard_use_expires_seconds) {
196 long tmo = xp->lft.hard_use_expires_seconds +
197 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
198 if (tmo <= 0)
199 goto expired;
200 if (tmo < next)
201 next = tmo;
202 }
203 if (xp->lft.soft_add_expires_seconds) {
204 long tmo = xp->lft.soft_add_expires_seconds +
205 xp->curlft.add_time - now;
206 if (tmo <= 0) {
207 warn = 1;
208 tmo = XFRM_KM_TIMEOUT;
209 }
210 if (tmo < next)
211 next = tmo;
212 }
213 if (xp->lft.soft_use_expires_seconds) {
214 long tmo = xp->lft.soft_use_expires_seconds +
215 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
216 if (tmo <= 0) {
217 warn = 1;
218 tmo = XFRM_KM_TIMEOUT;
219 }
220 if (tmo < next)
221 next = tmo;
222 }
223
224 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800225 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (next != LONG_MAX &&
227 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
228 xfrm_pol_hold(xp);
229
230out:
231 read_unlock(&xp->lock);
232 xfrm_pol_put(xp);
233 return;
234
235expired:
236 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700237 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800238 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 xfrm_pol_put(xp);
240}
241
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000242static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
243{
244 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
245
246 if (unlikely(pol->walk.dead))
247 flo = NULL;
248 else
249 xfrm_pol_hold(pol);
250
251 return flo;
252}
253
254static int xfrm_policy_flo_check(struct flow_cache_object *flo)
255{
256 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
257
258 return !pol->walk.dead;
259}
260
261static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
262{
263 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
264}
265
266static const struct flow_cache_ops xfrm_policy_fc_ops = {
267 .get = xfrm_policy_flo_get,
268 .check = xfrm_policy_flo_check,
269 .delete = xfrm_policy_flo_delete,
270};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
273 * SPD calls.
274 */
275
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800276struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 struct xfrm_policy *policy;
279
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700280 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800283 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700284 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700285 INIT_HLIST_NODE(&policy->bydst);
286 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700288 atomic_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100289 skb_queue_head_init(&policy->polq.hold_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800290 setup_timer(&policy->timer, xfrm_policy_timer,
291 (unsigned long)policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100292 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
293 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000294 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296 return policy;
297}
298EXPORT_SYMBOL(xfrm_policy_alloc);
299
300/* Destroy xfrm_policy: descendant resources must be released to this moment. */
301
WANG Cong64c31b32008-01-07 22:34:29 -0800302void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Herbert Xu12a169e2008-10-01 07:03:24 -0700304 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Fan Du0659eea2013-08-01 18:08:36 +0800306 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 BUG();
308
Paul Moore03e1ad72008-04-12 19:07:52 -0700309 security_xfrm_policy_free(policy->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 kfree(policy);
311}
WANG Cong64c31b32008-01-07 22:34:29 -0800312EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100314static void xfrm_queue_purge(struct sk_buff_head *list)
315{
316 struct sk_buff *skb;
317
Steffen Klassert302a50b2013-08-28 08:47:14 +0200318 while ((skb = skb_dequeue(list)) != NULL)
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100319 kfree_skb(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322/* Rule must be locked. Release descentant resources, announce
323 * entry dead. The rule must be unlinked from lists to the moment.
324 */
325
326static void xfrm_policy_kill(struct xfrm_policy *policy)
327{
Herbert Xu12a169e2008-10-01 07:03:24 -0700328 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Timo Teräs285ead12010-04-07 00:30:06 +0000330 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200332 if (del_timer(&policy->polq.hold_timer))
333 xfrm_pol_put(policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100334 xfrm_queue_purge(&policy->polq.hold_queue);
335
Timo Teräs285ead12010-04-07 00:30:06 +0000336 if (del_timer(&policy->timer))
337 xfrm_pol_put(policy);
338
339 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
David S. Miller2518c7c2006-08-24 04:45:07 -0700342static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
343
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800344static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700345{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800346 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700347}
348
David S. Miller5f803b52011-02-24 00:33:19 -0500349static struct hlist_head *policy_hash_bysel(struct net *net,
350 const struct xfrm_selector *sel,
351 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700352{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800353 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700354 unsigned int hash = __sel_hash(sel, family, hmask);
355
356 return (hash == hmask + 1 ?
Alexey Dobriyan11219942008-11-25 17:33:06 -0800357 &net->xfrm.policy_inexact[dir] :
358 net->xfrm.policy_bydst[dir].table + hash);
David S. Miller2518c7c2006-08-24 04:45:07 -0700359}
360
David S. Miller5f803b52011-02-24 00:33:19 -0500361static struct hlist_head *policy_hash_direct(struct net *net,
362 const xfrm_address_t *daddr,
363 const xfrm_address_t *saddr,
364 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700365{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800366 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700367 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
368
Alexey Dobriyan11219942008-11-25 17:33:06 -0800369 return net->xfrm.policy_bydst[dir].table + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700370}
371
David S. Miller2518c7c2006-08-24 04:45:07 -0700372static void xfrm_dst_hash_transfer(struct hlist_head *list,
373 struct hlist_head *ndsttable,
374 unsigned int nhashmask)
375{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800376 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700377 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800378 unsigned int h0 = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700379
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800380redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800381 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700382 unsigned int h;
383
384 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
385 pol->family, nhashmask);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800386 if (!entry0) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800387 hlist_del(&pol->bydst);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800388 hlist_add_head(&pol->bydst, ndsttable+h);
389 h0 = h;
390 } else {
391 if (h != h0)
392 continue;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800393 hlist_del(&pol->bydst);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800394 hlist_add_after(entry0, &pol->bydst);
395 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800396 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800397 }
398 if (!hlist_empty(list)) {
399 entry0 = NULL;
400 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700401 }
402}
403
404static void xfrm_idx_hash_transfer(struct hlist_head *list,
405 struct hlist_head *nidxtable,
406 unsigned int nhashmask)
407{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800408 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700409 struct xfrm_policy *pol;
410
Sasha Levinb67bfe02013-02-27 17:06:00 -0800411 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700412 unsigned int h;
413
414 h = __idx_hash(pol->index, nhashmask);
415 hlist_add_head(&pol->byidx, nidxtable+h);
416 }
417}
418
419static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
420{
421 return ((old_hmask + 1) << 1) - 1;
422}
423
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800424static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700425{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800426 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700427 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
428 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800429 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700430 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700431 int i;
432
433 if (!ndst)
434 return;
435
Fan Du283bc9f2013-11-07 17:47:50 +0800436 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700437
438 for (i = hmask; i >= 0; i--)
439 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
440
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800441 net->xfrm.policy_bydst[dir].table = ndst;
442 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700443
Fan Du283bc9f2013-11-07 17:47:50 +0800444 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700445
David S. Miller44e36b42006-08-24 04:50:50 -0700446 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700447}
448
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800449static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700450{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800451 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700452 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
453 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800454 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700455 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700456 int i;
457
458 if (!nidx)
459 return;
460
Fan Du283bc9f2013-11-07 17:47:50 +0800461 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700462
463 for (i = hmask; i >= 0; i--)
464 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
465
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800466 net->xfrm.policy_byidx = nidx;
467 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700468
Fan Du283bc9f2013-11-07 17:47:50 +0800469 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700470
David S. Miller44e36b42006-08-24 04:50:50 -0700471 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700472}
473
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800474static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700475{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800476 unsigned int cnt = net->xfrm.policy_count[dir];
477 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700478
479 if (total)
480 *total += cnt;
481
482 if ((hmask + 1) < xfrm_policy_hashmax &&
483 cnt > hmask)
484 return 1;
485
486 return 0;
487}
488
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800489static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700490{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800491 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700492
493 if ((hmask + 1) < xfrm_policy_hashmax &&
494 total > hmask)
495 return 1;
496
497 return 0;
498}
499
Alexey Dobriyane0710412010-01-23 13:37:10 +0000500void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700501{
Fan Du283bc9f2013-11-07 17:47:50 +0800502 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000503 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
504 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
505 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
506 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
507 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
508 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
509 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700510 si->spdhmcnt = xfrm_policy_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800511 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700512}
513EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700514
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700515static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800516static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700517{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800518 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700519 int dir, total;
520
521 mutex_lock(&hash_resize_mutex);
522
523 total = 0;
524 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800525 if (xfrm_bydst_should_resize(net, dir, &total))
526 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700527 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800528 if (xfrm_byidx_should_resize(net, total))
529 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700530
531 mutex_unlock(&hash_resize_mutex);
532}
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534/* Generate new index... KAME seems to generate them ordered by cost
535 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf2013-11-07 17:47:48 +0800536static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 static u32 idx_generator;
539
540 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700541 struct hlist_head *list;
542 struct xfrm_policy *p;
543 u32 idx;
544 int found;
545
Fan Due682adf2013-11-07 17:47:48 +0800546 if (!index) {
547 idx = (idx_generator | dir);
548 idx_generator += 8;
549 } else {
550 idx = index;
551 index = 0;
552 }
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 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);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200594 if (del_timer(&pq->hold_timer))
595 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100596 spin_unlock_bh(&pq->hold_queue.lock);
597
598 if (skb_queue_empty(&list))
599 return;
600
601 pq = &new->polq;
602
603 spin_lock_bh(&pq->hold_queue.lock);
604 skb_queue_splice(&list, &pq->hold_queue);
605 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200606 if (!mod_timer(&pq->hold_timer, jiffies))
607 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100608 spin_unlock_bh(&pq->hold_queue.lock);
609}
610
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100611static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
612 struct xfrm_policy *pol)
613{
614 u32 mark = policy->mark.v & policy->mark.m;
615
616 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
617 return true;
618
619 if ((mark & pol->mark.m) == pol->mark.v &&
620 policy->priority == pol->priority)
621 return true;
622
623 return false;
624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
627{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800628 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700629 struct xfrm_policy *pol;
630 struct xfrm_policy *delpol;
631 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800632 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Fan Du283bc9f2013-11-07 17:47:50 +0800634 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800635 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700636 delpol = NULL;
637 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800638 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800639 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700640 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100641 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800642 xfrm_sec_ctx_match(pol->security, policy->security) &&
643 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (excl) {
Fan Du283bc9f2013-11-07 17:47:50 +0800645 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return -EEXIST;
647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 delpol = pol;
649 if (policy->priority > pol->priority)
650 continue;
651 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800652 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 continue;
654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (delpol)
656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700659 hlist_add_after(newpos, &policy->bydst);
660 else
661 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800663 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 atomic_inc(&flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800665
666 /* After previous checking, family can either be AF_INET or AF_INET6 */
667 if (policy->family == AF_INET)
668 rt_genid_bump_ipv4(net);
669 else
670 rt_genid_bump_ipv6(net);
671
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100672 if (delpol) {
673 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800674 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100675 }
Fan Due682adf2013-11-07 17:47:48 +0800676 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800677 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800678 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 policy->curlft.use_time = 0;
680 if (!mod_timer(&policy->timer, jiffies + HZ))
681 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800682 list_add(&policy->walk.all, &net->xfrm.policy_all);
Fan Du283bc9f2013-11-07 17:47:50 +0800683 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
David S. Miller9b78a822005-12-22 07:39:48 -0800685 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800687 else if (xfrm_bydst_should_resize(net, dir, NULL))
688 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return 0;
691}
692EXPORT_SYMBOL(xfrm_policy_insert);
693
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000694struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
695 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800696 struct xfrm_sec_ctx *ctx, int delete,
697 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
David S. Miller2518c7c2006-08-24 04:45:07 -0700699 struct xfrm_policy *pol, *ret;
700 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Eric Parisef41aaa2007-03-07 15:37:58 -0800702 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800703 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800704 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700705 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800706 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700707 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000708 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700709 !selector_cmp(sel, &pol->selector) &&
710 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700712 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700713 *err = security_xfrm_policy_delete(
714 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800715 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800716 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800717 return pol;
718 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800719 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700720 }
721 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 break;
723 }
724 }
Fan Du283bc9f2013-11-07 17:47:50 +0800725 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000727 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700728 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700729 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
Trent Jaegerdf718372005-12-13 23:12:27 -0800731EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000733struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
734 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
David S. Miller2518c7c2006-08-24 04:45:07 -0700736 struct xfrm_policy *pol, *ret;
737 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Herbert Xub5505c62007-05-14 02:15:47 -0700739 *err = -ENOENT;
740 if (xfrm_policy_id2dir(id) != dir)
741 return NULL;
742
Eric Parisef41aaa2007-03-07 15:37:58 -0800743 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800744 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800745 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700746 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800747 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000748 if (pol->type == type && pol->index == id &&
749 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700751 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700752 *err = security_xfrm_policy_delete(
753 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800754 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800755 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800756 return pol;
757 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800758 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700759 }
760 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 break;
762 }
763 }
Fan Du283bc9f2013-11-07 17:47:50 +0800764 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000766 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700767 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700768 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770EXPORT_SYMBOL(xfrm_policy_byid);
771
Joy Latten4aa2e622007-06-04 19:05:57 -0400772#ifdef CONFIG_SECURITY_NETWORK_XFRM
773static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800774xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Joy Latten4aa2e622007-06-04 19:05:57 -0400776 int dir, err = 0;
777
778 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
779 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400780 int i;
781
Sasha Levinb67bfe02013-02-27 17:06:00 -0800782 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800783 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400784 if (pol->type != type)
785 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700786 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400787 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700788 xfrm_audit_policy_delete(pol, 0,
789 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400790 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700791 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400792 return err;
793 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900794 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800795 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800796 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800797 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400798 bydst) {
799 if (pol->type != type)
800 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700801 err = security_xfrm_policy_delete(
802 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400803 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700804 xfrm_audit_policy_delete(pol, 0,
805 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400806 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700807 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400808 return err;
809 }
810 }
811 }
812 }
813 return err;
814}
815#else
816static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800817xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400818{
819 return 0;
820}
821#endif
822
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800823int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400824{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000825 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Fan Du283bc9f2013-11-07 17:47:50 +0800827 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400828
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800829 err = xfrm_policy_flush_secctx_check(net, type, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400830 if (err)
831 goto out;
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700834 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800835 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700836
837 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800838 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800839 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700840 if (pol->type != type)
841 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000842 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800843 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000844 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Joy Lattenab5f5e82007-09-17 11:51:22 -0700846 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400847 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700848 audit_info->secid);
Joy Latten161a09e2006-11-27 13:11:54 -0600849
David S. Miller2518c7c2006-08-24 04:45:07 -0700850 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Fan Du283bc9f2013-11-07 17:47:50 +0800852 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700853 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700855
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800856 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700857 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800858 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800859 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700860 bydst) {
861 if (pol->type != type)
862 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000863 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800864 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000865 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700866
Joy Lattenab5f5e82007-09-17 11:51:22 -0700867 xfrm_audit_policy_delete(pol, 1,
868 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400869 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700870 audit_info->secid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700871 xfrm_policy_kill(pol);
872
Fan Du283bc9f2013-11-07 17:47:50 +0800873 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700874 goto again2;
875 }
876 }
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000879 if (!cnt)
880 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400881out:
Fan Du283bc9f2013-11-07 17:47:50 +0800882 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400883 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885EXPORT_SYMBOL(xfrm_policy_flush);
886
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800887int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800888 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 void *data)
890{
Herbert Xu12a169e2008-10-01 07:03:24 -0700891 struct xfrm_policy *pol;
892 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -0800893 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Timo Teras4c563f72008-02-28 21:31:08 -0800895 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
896 walk->type != XFRM_POLICY_TYPE_ANY)
897 return -EINVAL;
898
Herbert Xu12a169e2008-10-01 07:03:24 -0700899 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -0800900 return 0;
901
Fan Du283bc9f2013-11-07 17:47:50 +0800902 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700903 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800904 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -0700905 else
906 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800907 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -0700908 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -0800909 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -0700910 pol = container_of(x, struct xfrm_policy, walk);
911 if (walk->type != XFRM_POLICY_TYPE_ANY &&
912 walk->type != pol->type)
913 continue;
914 error = func(pol, xfrm_policy_id2dir(pol->index),
915 walk->seq, data);
916 if (error) {
917 list_move_tail(&walk->walk.all, &x->all);
918 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -0800919 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700920 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700922 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800923 error = -ENOENT;
924 goto out;
925 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700926 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927out:
Fan Du283bc9f2013-11-07 17:47:50 +0800928 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return error;
930}
931EXPORT_SYMBOL(xfrm_policy_walk);
932
Herbert Xu12a169e2008-10-01 07:03:24 -0700933void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
934{
935 INIT_LIST_HEAD(&walk->walk.all);
936 walk->walk.dead = 1;
937 walk->type = type;
938 walk->seq = 0;
939}
940EXPORT_SYMBOL(xfrm_policy_walk_init);
941
Fan Du283bc9f2013-11-07 17:47:50 +0800942void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -0700943{
944 if (list_empty(&walk->walk.all))
945 return;
946
Fan Du283bc9f2013-11-07 17:47:50 +0800947 write_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -0700948 list_del(&walk->walk.all);
Fan Du283bc9f2013-11-07 17:47:50 +0800949 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700950}
951EXPORT_SYMBOL(xfrm_policy_walk_done);
952
James Morris134b0fc2006-10-05 15:42:27 -0500953/*
954 * Find policy to apply to this flow.
955 *
956 * Returns 0 if policy found, else an -errno.
957 */
David S. Millerf299d552011-02-24 01:23:30 -0500958static int xfrm_policy_match(const struct xfrm_policy *pol,
959 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -0700960 u8 type, u16 family, int dir)
961{
David S. Millerf299d552011-02-24 01:23:30 -0500962 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -0400963 int ret = -ESRCH;
964 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -0700965
966 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -0500967 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -0700968 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500969 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700970
971 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500972 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -0500973 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -0700974 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700975
James Morris134b0fc2006-10-05 15:42:27 -0500976 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700977}
978
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800979static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -0800980 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700981 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
James Morris134b0fc2006-10-05 15:42:27 -0500983 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700984 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -0500985 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -0700986 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700987 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700988
989 daddr = xfrm_flowi_daddr(fl, family);
990 saddr = xfrm_flowi_saddr(fl, family);
991 if (unlikely(!daddr || !saddr))
992 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Fan Du283bc9f2013-11-07 17:47:50 +0800994 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800995 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700996 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800997 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500998 err = xfrm_policy_match(pol, fl, type, family, dir);
999 if (err) {
1000 if (err == -ESRCH)
1001 continue;
1002 else {
1003 ret = ERR_PTR(err);
1004 goto fail;
1005 }
1006 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001007 ret = pol;
1008 priority = ret->priority;
1009 break;
1010 }
1011 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001012 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001013 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001014 err = xfrm_policy_match(pol, fl, type, family, dir);
1015 if (err) {
1016 if (err == -ESRCH)
1017 continue;
1018 else {
1019 ret = ERR_PTR(err);
1020 goto fail;
1021 }
1022 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001023 ret = pol;
1024 break;
1025 }
1026 }
David S. Milleracba48e2006-08-25 15:46:46 -07001027 if (ret)
1028 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -05001029fail:
Fan Du283bc9f2013-11-07 17:47:50 +08001030 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001031
David S. Miller2518c7c2006-08-24 04:45:07 -07001032 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001033}
1034
Timo Teräs80c802f2010-04-07 00:30:05 +00001035static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001036__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001037{
1038#ifdef CONFIG_XFRM_SUB_POLICY
1039 struct xfrm_policy *pol;
1040
1041 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1042 if (pol != NULL)
1043 return pol;
1044#endif
1045 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1046}
1047
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001048static int flow_to_policy_dir(int dir)
1049{
1050 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1051 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1052 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1053 return dir;
1054
1055 switch (dir) {
1056 default:
1057 case FLOW_DIR_IN:
1058 return XFRM_POLICY_IN;
1059 case FLOW_DIR_OUT:
1060 return XFRM_POLICY_OUT;
1061 case FLOW_DIR_FWD:
1062 return XFRM_POLICY_FWD;
1063 }
1064}
1065
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001066static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001067xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001068 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001069{
1070 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001071
1072 if (old_obj)
1073 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001074
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001075 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001076 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001077 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001078
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001079 /* Resolver returns two references:
1080 * one for cache and one for caller of flow_cache_lookup() */
1081 xfrm_pol_hold(pol);
1082
1083 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
Trent Jaegerdf718372005-12-13 23:12:27 -08001086static inline int policy_to_flow_dir(int dir)
1087{
1088 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001089 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1090 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1091 return dir;
1092 switch (dir) {
1093 default:
1094 case XFRM_POLICY_IN:
1095 return FLOW_DIR_IN;
1096 case XFRM_POLICY_OUT:
1097 return FLOW_DIR_OUT;
1098 case XFRM_POLICY_FWD:
1099 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001100 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001101}
1102
David S. Millerdee9f4b2011-02-22 18:44:31 -08001103static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir,
1104 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 struct xfrm_policy *pol;
Fan Du283bc9f2013-11-07 17:47:50 +08001107 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Fan Du283bc9f2013-11-07 17:47:50 +08001109 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if ((pol = sk->sk_policy[dir]) != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001111 bool match = xfrm_selector_match(&pol->selector, fl,
1112 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001113 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001114
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001115 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001116 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1117 pol = NULL;
1118 goto out;
1119 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001120 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001121 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001122 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001123 if (!err)
1124 xfrm_pol_hold(pol);
1125 else if (err == -ESRCH)
1126 pol = NULL;
1127 else
1128 pol = ERR_PTR(err);
1129 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 pol = NULL;
1131 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001132out:
Fan Du283bc9f2013-11-07 17:47:50 +08001133 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return pol;
1135}
1136
1137static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1138{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001139 struct net *net = xp_net(pol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001140 struct hlist_head *chain = policy_hash_bysel(net, &pol->selector,
David S. Miller2518c7c2006-08-24 04:45:07 -07001141 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001142
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001143 list_add(&pol->walk.all, &net->xfrm.policy_all);
David S. Miller2518c7c2006-08-24 04:45:07 -07001144 hlist_add_head(&pol->bydst, chain);
Alexey Dobriyane92303f2008-11-25 17:32:41 -08001145 hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001146 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001148
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001149 if (xfrm_bydst_should_resize(net, dir, NULL))
1150 schedule_work(&net->xfrm.policy_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
1152
1153static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1154 int dir)
1155{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001156 struct net *net = xp_net(pol);
1157
David S. Miller2518c7c2006-08-24 04:45:07 -07001158 if (hlist_unhashed(&pol->bydst))
1159 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
David S. Miller2518c7c2006-08-24 04:45:07 -07001161 hlist_del(&pol->bydst);
1162 hlist_del(&pol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -07001163 list_del(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001164 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001165
1166 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168
Herbert Xu4666faa2005-06-18 22:43:22 -07001169int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Fan Du283bc9f2013-11-07 17:47:50 +08001171 struct net *net = xp_net(pol);
1172
1173 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 pol = __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001175 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001180 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
David S. Millera70fcb02006-03-20 19:18:52 -08001182EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1185{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001186 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 struct xfrm_policy *old_pol;
1188
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001189#ifdef CONFIG_XFRM_SUB_POLICY
1190 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1191 return -EINVAL;
1192#endif
1193
Fan Du283bc9f2013-11-07 17:47:50 +08001194 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 old_pol = sk->sk_policy[dir];
1196 sk->sk_policy[dir] = pol;
1197 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001198 pol->curlft.add_time = get_seconds();
Fan Due682adf2013-11-07 17:47:48 +08001199 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1201 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001202 if (old_pol) {
1203 if (pol)
1204 xfrm_policy_requeue(old_pol, pol);
1205
Timo Teräsea2dea92010-03-31 00:17:05 +00001206 /* Unlinking succeeds always. This is the only function
1207 * allowed to delete or replace socket policy.
1208 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001210 }
Fan Du283bc9f2013-11-07 17:47:50 +08001211 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 if (old_pol) {
1214 xfrm_policy_kill(old_pol);
1215 }
1216 return 0;
1217}
1218
David S. Millerd3e40a92011-02-24 01:25:41 -05001219static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001221 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001222 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 if (newp) {
1225 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001226 if (security_xfrm_policy_clone(old->security,
1227 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001228 kfree(newp);
1229 return NULL; /* ENOMEM */
1230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 newp->lft = old->lft;
1232 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001233 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 newp->action = old->action;
1235 newp->flags = old->flags;
1236 newp->xfrm_nr = old->xfrm_nr;
1237 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001238 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 memcpy(newp->xfrm_vec, old->xfrm_vec,
1240 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Fan Du283bc9f2013-11-07 17:47:50 +08001241 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001243 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 xfrm_pol_put(newp);
1245 }
1246 return newp;
1247}
1248
1249int __xfrm_sk_clone_policy(struct sock *sk)
1250{
1251 struct xfrm_policy *p0 = sk->sk_policy[0],
1252 *p1 = sk->sk_policy[1];
1253
1254 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1255 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1256 return -ENOMEM;
1257 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1258 return -ENOMEM;
1259 return 0;
1260}
1261
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001262static int
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001263xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001264 unsigned short family)
1265{
1266 int err;
1267 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1268
1269 if (unlikely(afinfo == NULL))
1270 return -EINVAL;
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001271 err = afinfo->get_saddr(net, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001272 xfrm_policy_put_afinfo(afinfo);
1273 return err;
1274}
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276/* Resolve list of templates for the flow, given policy. */
1277
1278static int
David S. Millera6c2e612011-02-22 18:35:39 -08001279xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1280 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001282 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 int nx;
1284 int i, error;
1285 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1286 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001287 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1290 struct xfrm_state *x;
1291 xfrm_address_t *remote = daddr;
1292 xfrm_address_t *local = saddr;
1293 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1294
Joakim Koskela48b8d782007-07-26 00:08:42 -07001295 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1296 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 remote = &tmpl->id.daddr;
1298 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001299 if (xfrm_addr_any(local, tmpl->encap_family)) {
1300 error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001301 if (error)
1302 goto fail;
1303 local = &tmp;
1304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
1306
1307 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1308
1309 if (x && x->km.state == XFRM_STATE_VALID) {
1310 xfrm[nx++] = x;
1311 daddr = remote;
1312 saddr = local;
1313 continue;
1314 }
1315 if (x) {
1316 error = (x->km.state == XFRM_STATE_ERROR ?
1317 -EINVAL : -EAGAIN);
1318 xfrm_state_put(x);
1319 }
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001320 else if (error == -ESRCH)
1321 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 if (!tmpl->optional)
1324 goto fail;
1325 }
1326 return nx;
1327
1328fail:
1329 for (nx--; nx>=0; nx--)
1330 xfrm_state_put(xfrm[nx]);
1331 return error;
1332}
1333
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001334static int
David S. Millera6c2e612011-02-22 18:35:39 -08001335xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1336 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001337{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001338 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1339 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001340 int cnx = 0;
1341 int error;
1342 int ret;
1343 int i;
1344
1345 for (i = 0; i < npols; i++) {
1346 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1347 error = -ENOBUFS;
1348 goto fail;
1349 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001350
1351 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001352 if (ret < 0) {
1353 error = ret;
1354 goto fail;
1355 } else
1356 cnx += ret;
1357 }
1358
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001359 /* found states are sorted for outbound processing */
1360 if (npols > 1)
1361 xfrm_state_sort(xfrm, tpp, cnx, family);
1362
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001363 return cnx;
1364
1365 fail:
1366 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001367 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001368 return error;
1369
1370}
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372/* Check that the bundle accepts the flow and its components are
1373 * still valid.
1374 */
1375
David S. Miller05d84022011-02-22 17:47:10 -08001376static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001377{
1378 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1379 int tos;
1380
1381 if (!afinfo)
1382 return -EINVAL;
1383
1384 tos = afinfo->get_tos(fl);
1385
1386 xfrm_policy_put_afinfo(afinfo);
1387
1388 return tos;
1389}
1390
Timo Teräs80c802f2010-04-07 00:30:05 +00001391static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1392{
1393 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1394 struct dst_entry *dst = &xdst->u.dst;
1395
1396 if (xdst->route == NULL) {
1397 /* Dummy bundle - if it has xfrms we were not
1398 * able to build bundle as template resolution failed.
1399 * It means we need to try again resolving. */
1400 if (xdst->num_xfrms > 0)
1401 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001402 } else if (dst->flags & DST_XFRM_QUEUE) {
1403 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001404 } else {
1405 /* Real bundle */
1406 if (stale_bundle(dst))
1407 return NULL;
1408 }
1409
1410 dst_hold(dst);
1411 return flo;
1412}
1413
1414static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1415{
1416 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1417 struct dst_entry *dst = &xdst->u.dst;
1418
1419 if (!xdst->route)
1420 return 0;
1421 if (stale_bundle(dst))
1422 return 0;
1423
1424 return 1;
1425}
1426
1427static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1428{
1429 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1430 struct dst_entry *dst = &xdst->u.dst;
1431
1432 dst_free(dst);
1433}
1434
1435static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1436 .get = xfrm_bundle_flo_get,
1437 .check = xfrm_bundle_flo_check,
1438 .delete = xfrm_bundle_flo_delete,
1439};
1440
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001441static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001442{
1443 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001444 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001445 struct xfrm_dst *xdst;
1446
1447 if (!afinfo)
1448 return ERR_PTR(-EINVAL);
1449
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001450 switch (family) {
1451 case AF_INET:
1452 dst_ops = &net->xfrm.xfrm4_dst_ops;
1453 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001454#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001455 case AF_INET6:
1456 dst_ops = &net->xfrm.xfrm6_dst_ops;
1457 break;
1458#endif
1459 default:
1460 BUG();
1461 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001462 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001463
Madalin Bucurd4cae562011-09-26 07:04:36 +00001464 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001465 struct dst_entry *dst = &xdst->u.dst;
1466
1467 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001468 xdst->flo.ops = &xfrm_bundle_fc_ops;
Patrick McHardy9d7b0fc2012-08-20 02:56:56 -07001469 if (afinfo->init_dst)
1470 afinfo->init_dst(net, xdst);
Madalin Bucurd4cae562011-09-26 07:04:36 +00001471 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001472 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001473
Madalin Bucurd4cae562011-09-26 07:04:36 +00001474 xfrm_policy_put_afinfo(afinfo);
1475
Herbert Xu25ee3282007-12-11 09:32:34 -08001476 return xdst;
1477}
1478
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001479static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1480 int nfheader_len)
1481{
1482 struct xfrm_policy_afinfo *afinfo =
1483 xfrm_policy_get_afinfo(dst->ops->family);
1484 int err;
1485
1486 if (!afinfo)
1487 return -EINVAL;
1488
1489 err = afinfo->init_path(path, dst, nfheader_len);
1490
1491 xfrm_policy_put_afinfo(afinfo);
1492
1493 return err;
1494}
1495
Herbert Xu87c1e122010-03-02 02:51:56 +00001496static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001497 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001498{
1499 struct xfrm_policy_afinfo *afinfo =
1500 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1501 int err;
1502
1503 if (!afinfo)
1504 return -EINVAL;
1505
Herbert Xu87c1e122010-03-02 02:51:56 +00001506 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001507
1508 xfrm_policy_put_afinfo(afinfo);
1509
1510 return err;
1511}
1512
Timo Teräs80c802f2010-04-07 00:30:05 +00001513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1515 * all the metrics... Shortly, bundle a bundle.
1516 */
1517
Herbert Xu25ee3282007-12-11 09:32:34 -08001518static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1519 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001520 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001521 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001523 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001524 unsigned long now = jiffies;
1525 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001526 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001527 struct dst_entry *dst_prev = NULL;
1528 struct dst_entry *dst0 = NULL;
1529 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001531 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001532 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001533 int trailer_len = 0;
1534 int tos;
1535 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001536 xfrm_address_t saddr, daddr;
1537
1538 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001539
1540 tos = xfrm_get_tos(fl, family);
1541 err = tos;
1542 if (tos < 0)
1543 goto put_states;
1544
1545 dst_hold(dst);
1546
1547 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001548 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001549 struct dst_entry *dst1 = &xdst->u.dst;
1550
1551 err = PTR_ERR(xdst);
1552 if (IS_ERR(xdst)) {
1553 dst_release(dst);
1554 goto put_states;
1555 }
1556
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001557 if (xfrm[i]->sel.family == AF_UNSPEC) {
1558 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1559 xfrm_af2proto(family));
1560 if (!inner_mode) {
1561 err = -EAFNOSUPPORT;
1562 dst_release(dst);
1563 goto put_states;
1564 }
1565 } else
1566 inner_mode = xfrm[i]->inner_mode;
1567
Herbert Xu25ee3282007-12-11 09:32:34 -08001568 if (!dst_prev)
1569 dst0 = dst1;
1570 else {
1571 dst_prev->child = dst_clone(dst1);
1572 dst1->flags |= DST_NOHASH;
1573 }
1574
1575 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001576 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001577
1578 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1579 family = xfrm[i]->props.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001580 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1581 family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001582 err = PTR_ERR(dst);
1583 if (IS_ERR(dst))
1584 goto put_states;
1585 } else
1586 dst_hold(dst);
1587
1588 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001589 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001590
David S. Millerf5b0a872012-07-19 12:31:33 -07001591 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001592 dst1->flags |= DST_HOST;
1593 dst1->lastuse = now;
1594
1595 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001596 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001597
1598 dst1->next = dst_prev;
1599 dst_prev = dst1;
1600
1601 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001602 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1603 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001604 trailer_len += xfrm[i]->props.trailer_len;
1605 }
1606
1607 dst_prev->child = dst;
1608 dst0->path = dst;
1609
1610 err = -ENODEV;
1611 dev = dst->dev;
1612 if (!dev)
1613 goto free_dst;
1614
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001615 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001616 xfrm_init_pmtu(dst_prev);
1617
1618 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1619 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1620
Herbert Xu87c1e122010-03-02 02:51:56 +00001621 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001622 if (err)
1623 goto free_dst;
1624
1625 dst_prev->header_len = header_len;
1626 dst_prev->trailer_len = trailer_len;
1627 header_len -= xdst->u.dst.xfrm->props.header_len;
1628 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1629 }
1630
1631out:
1632 return dst0;
1633
1634put_states:
1635 for (; i < nx; i++)
1636 xfrm_state_put(xfrm[i]);
1637free_dst:
1638 if (dst0)
1639 dst_free(dst0);
1640 dst0 = ERR_PTR(err);
1641 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
Ying Xueda7c2242014-01-08 10:26:39 +08001644#ifdef CONFIG_XFRM_SUB_POLICY
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001645static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001646{
1647 if (!*target) {
1648 *target = kmalloc(size, GFP_ATOMIC);
1649 if (!*target)
1650 return -ENOMEM;
1651 }
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001652
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001653 memcpy(*target, src, size);
1654 return 0;
1655}
Ying Xueda7c2242014-01-08 10:26:39 +08001656#endif
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001657
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001658static int xfrm_dst_update_parent(struct dst_entry *dst,
1659 const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001660{
1661#ifdef CONFIG_XFRM_SUB_POLICY
1662 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1663 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1664 sel, sizeof(*sel));
1665#else
1666 return 0;
1667#endif
1668}
1669
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001670static int xfrm_dst_update_origin(struct dst_entry *dst,
1671 const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001672{
1673#ifdef CONFIG_XFRM_SUB_POLICY
1674 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1675 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1676#else
1677 return 0;
1678#endif
1679}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
David S. Miller73ff93c2011-02-22 18:33:42 -08001681static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001682 struct xfrm_policy **pols,
1683 int *num_pols, int *num_xfrms)
1684{
1685 int i;
1686
1687 if (*num_pols == 0 || !pols[0]) {
1688 *num_pols = 0;
1689 *num_xfrms = 0;
1690 return 0;
1691 }
1692 if (IS_ERR(pols[0]))
1693 return PTR_ERR(pols[0]);
1694
1695 *num_xfrms = pols[0]->xfrm_nr;
1696
1697#ifdef CONFIG_XFRM_SUB_POLICY
1698 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1699 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1700 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1701 XFRM_POLICY_TYPE_MAIN,
1702 fl, family,
1703 XFRM_POLICY_OUT);
1704 if (pols[1]) {
1705 if (IS_ERR(pols[1])) {
1706 xfrm_pols_put(pols, *num_pols);
1707 return PTR_ERR(pols[1]);
1708 }
1709 (*num_pols) ++;
1710 (*num_xfrms) += pols[1]->xfrm_nr;
1711 }
1712 }
1713#endif
1714 for (i = 0; i < *num_pols; i++) {
1715 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1716 *num_xfrms = -1;
1717 break;
1718 }
1719 }
1720
1721 return 0;
1722
1723}
1724
1725static struct xfrm_dst *
1726xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001727 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001728 struct dst_entry *dst_orig)
1729{
1730 struct net *net = xp_net(pols[0]);
1731 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1732 struct dst_entry *dst;
1733 struct xfrm_dst *xdst;
1734 int err;
1735
1736 /* Try to instantiate a bundle */
1737 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001738 if (err <= 0) {
1739 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001740 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1741 return ERR_PTR(err);
1742 }
1743
1744 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1745 if (IS_ERR(dst)) {
1746 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1747 return ERR_CAST(dst);
1748 }
1749
1750 xdst = (struct xfrm_dst *)dst;
1751 xdst->num_xfrms = err;
1752 if (num_pols > 1)
1753 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1754 else
1755 err = xfrm_dst_update_origin(dst, fl);
1756 if (unlikely(err)) {
1757 dst_free(dst);
1758 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1759 return ERR_PTR(err);
1760 }
1761
1762 xdst->num_pols = num_pols;
1763 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
1764 xdst->policy_genid = atomic_read(&pols[0]->genid);
1765
1766 return xdst;
1767}
1768
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001769static void xfrm_policy_queue_process(unsigned long arg)
1770{
1771 int err = 0;
1772 struct sk_buff *skb;
1773 struct sock *sk;
1774 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001775 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1776 struct xfrm_policy_queue *pq = &pol->polq;
1777 struct flowi fl;
1778 struct sk_buff_head list;
1779
1780 spin_lock(&pq->hold_queue.lock);
1781 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001782 if (!skb) {
1783 spin_unlock(&pq->hold_queue.lock);
1784 goto out;
1785 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001786 dst = skb_dst(skb);
1787 sk = skb->sk;
1788 xfrm_decode_session(skb, &fl, dst->ops->family);
1789 spin_unlock(&pq->hold_queue.lock);
1790
1791 dst_hold(dst->path);
1792 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1793 sk, 0);
1794 if (IS_ERR(dst))
1795 goto purge_queue;
1796
1797 if (dst->flags & DST_XFRM_QUEUE) {
1798 dst_release(dst);
1799
1800 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1801 goto purge_queue;
1802
1803 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001804 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1805 xfrm_pol_hold(pol);
1806 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001807 }
1808
1809 dst_release(dst);
1810
1811 __skb_queue_head_init(&list);
1812
1813 spin_lock(&pq->hold_queue.lock);
1814 pq->timeout = 0;
1815 skb_queue_splice_init(&pq->hold_queue, &list);
1816 spin_unlock(&pq->hold_queue.lock);
1817
1818 while (!skb_queue_empty(&list)) {
1819 skb = __skb_dequeue(&list);
1820
1821 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1822 dst_hold(skb_dst(skb)->path);
1823 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1824 &fl, skb->sk, 0);
1825 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001826 kfree_skb(skb);
1827 continue;
1828 }
1829
1830 nf_reset(skb);
1831 skb_dst_drop(skb);
1832 skb_dst_set(skb, dst);
1833
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001834 err = dst_output(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001835 }
1836
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001837out:
1838 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001839 return;
1840
1841purge_queue:
1842 pq->timeout = 0;
1843 xfrm_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001844 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001845}
1846
1847static int xdst_queue_output(struct sk_buff *skb)
1848{
1849 unsigned long sched_next;
1850 struct dst_entry *dst = skb_dst(skb);
1851 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001852 struct xfrm_policy *pol = xdst->pols[0];
1853 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001854 const struct sk_buff *fclone = skb + 1;
1855
1856 if (unlikely(skb->fclone == SKB_FCLONE_ORIG &&
1857 fclone->fclone == SKB_FCLONE_CLONE)) {
1858 kfree_skb(skb);
1859 return 0;
1860 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001861
1862 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1863 kfree_skb(skb);
1864 return -EAGAIN;
1865 }
1866
1867 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001868
1869 spin_lock_bh(&pq->hold_queue.lock);
1870
1871 if (!pq->timeout)
1872 pq->timeout = XFRM_QUEUE_TMO_MIN;
1873
1874 sched_next = jiffies + pq->timeout;
1875
1876 if (del_timer(&pq->hold_timer)) {
1877 if (time_before(pq->hold_timer.expires, sched_next))
1878 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001879 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001880 }
1881
1882 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001883 if (!mod_timer(&pq->hold_timer, sched_next))
1884 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001885
1886 spin_unlock_bh(&pq->hold_queue.lock);
1887
1888 return 0;
1889}
1890
1891static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
1892 struct dst_entry *dst,
1893 const struct flowi *fl,
1894 int num_xfrms,
1895 u16 family)
1896{
1897 int err;
1898 struct net_device *dev;
1899 struct dst_entry *dst1;
1900 struct xfrm_dst *xdst;
1901
1902 xdst = xfrm_alloc_dst(net, family);
1903 if (IS_ERR(xdst))
1904 return xdst;
1905
Steffen Klassert5b8ef342013-08-27 13:43:30 +02001906 if (net->xfrm.sysctl_larval_drop || num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001907 return xdst;
1908
1909 dst1 = &xdst->u.dst;
1910 dst_hold(dst);
1911 xdst->route = dst;
1912
1913 dst_copy_metrics(dst1, dst);
1914
1915 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1916 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
1917 dst1->lastuse = jiffies;
1918
1919 dst1->input = dst_discard;
1920 dst1->output = xdst_queue_output;
1921
1922 dst_hold(dst);
1923 dst1->child = dst;
1924 dst1->path = dst;
1925
1926 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
1927
1928 err = -ENODEV;
1929 dev = dst->dev;
1930 if (!dev)
1931 goto free_dst;
1932
1933 err = xfrm_fill_dst(xdst, dev, fl);
1934 if (err)
1935 goto free_dst;
1936
1937out:
1938 return xdst;
1939
1940free_dst:
1941 dst_release(dst1);
1942 xdst = ERR_PTR(err);
1943 goto out;
1944}
1945
Timo Teräs80c802f2010-04-07 00:30:05 +00001946static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001947xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00001948 struct flow_cache_object *oldflo, void *ctx)
1949{
1950 struct dst_entry *dst_orig = (struct dst_entry *)ctx;
1951 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1952 struct xfrm_dst *xdst, *new_xdst;
1953 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
1954
1955 /* Check if the policies from old bundle are usable */
1956 xdst = NULL;
1957 if (oldflo) {
1958 xdst = container_of(oldflo, struct xfrm_dst, flo);
1959 num_pols = xdst->num_pols;
1960 num_xfrms = xdst->num_xfrms;
1961 pol_dead = 0;
1962 for (i = 0; i < num_pols; i++) {
1963 pols[i] = xdst->pols[i];
1964 pol_dead |= pols[i]->walk.dead;
1965 }
1966 if (pol_dead) {
1967 dst_free(&xdst->u.dst);
1968 xdst = NULL;
1969 num_pols = 0;
1970 num_xfrms = 0;
1971 oldflo = NULL;
1972 }
1973 }
1974
1975 /* Resolve policies to use if we couldn't get them from
1976 * previous cache entry */
1977 if (xdst == NULL) {
1978 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001979 pols[0] = __xfrm_policy_lookup(net, fl, family,
1980 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001981 err = xfrm_expand_policies(fl, family, pols,
1982 &num_pols, &num_xfrms);
1983 if (err < 0)
1984 goto inc_error;
1985 if (num_pols == 0)
1986 return NULL;
1987 if (num_xfrms <= 0)
1988 goto make_dummy_bundle;
1989 }
1990
1991 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family, dst_orig);
1992 if (IS_ERR(new_xdst)) {
1993 err = PTR_ERR(new_xdst);
1994 if (err != -EAGAIN)
1995 goto error;
1996 if (oldflo == NULL)
1997 goto make_dummy_bundle;
1998 dst_hold(&xdst->u.dst);
1999 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00002000 } else if (new_xdst == NULL) {
2001 num_xfrms = 0;
2002 if (oldflo == NULL)
2003 goto make_dummy_bundle;
2004 xdst->num_xfrms = 0;
2005 dst_hold(&xdst->u.dst);
2006 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002007 }
2008
2009 /* Kill the previous bundle */
2010 if (xdst) {
2011 /* The policies were stolen for newly generated bundle */
2012 xdst->num_pols = 0;
2013 dst_free(&xdst->u.dst);
2014 }
2015
2016 /* Flow cache does not have reference, it dst_free()'s,
2017 * but we do need to return one reference for original caller */
2018 dst_hold(&new_xdst->u.dst);
2019 return &new_xdst->flo;
2020
2021make_dummy_bundle:
2022 /* We found policies, but there's no bundles to instantiate:
2023 * either because the policy blocks, has no transformations or
2024 * we could not build template (no xfrm_states).*/
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002025 xdst = xfrm_create_dummy_bundle(net, dst_orig, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002026 if (IS_ERR(xdst)) {
2027 xfrm_pols_put(pols, num_pols);
2028 return ERR_CAST(xdst);
2029 }
2030 xdst->num_pols = num_pols;
2031 xdst->num_xfrms = num_xfrms;
2032 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
2033
2034 dst_hold(&xdst->u.dst);
2035 return &xdst->flo;
2036
2037inc_error:
2038 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2039error:
2040 if (xdst != NULL)
2041 dst_free(&xdst->u.dst);
2042 else
2043 xfrm_pols_put(pols, num_pols);
2044 return ERR_PTR(err);
2045}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
David S. Miller2774c132011-03-01 14:59:04 -08002047static struct dst_entry *make_blackhole(struct net *net, u16 family,
2048 struct dst_entry *dst_orig)
2049{
2050 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2051 struct dst_entry *ret;
2052
2053 if (!afinfo) {
2054 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002055 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002056 } else {
2057 ret = afinfo->blackhole_route(net, dst_orig);
2058 }
2059 xfrm_policy_put_afinfo(afinfo);
2060
2061 return ret;
2062}
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064/* Main function: finds/creates a bundle for given flow.
2065 *
2066 * At the moment we eat a raw IP route. Mostly to speed up lookups
2067 * on interfaces with disabled IPsec.
2068 */
David S. Miller452edd52011-03-02 13:27:41 -08002069struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2070 const struct flowi *fl,
2071 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002073 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002074 struct flow_cache_object *flo;
2075 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002076 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002077 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002078 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002079 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002080
Timo Teräs80c802f2010-04-07 00:30:05 +00002081 dst = NULL;
2082 xdst = NULL;
2083 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002084
Thomas Graff7944fb2007-08-25 13:46:55 -07002085 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002086 num_pols = 1;
2087 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2088 err = xfrm_expand_policies(fl, family, pols,
2089 &num_pols, &num_xfrms);
2090 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002091 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002092
2093 if (num_pols) {
2094 if (num_xfrms <= 0) {
2095 drop_pols = num_pols;
2096 goto no_transform;
2097 }
2098
2099 xdst = xfrm_resolve_and_create_bundle(
2100 pols, num_pols, fl,
2101 family, dst_orig);
2102 if (IS_ERR(xdst)) {
2103 xfrm_pols_put(pols, num_pols);
2104 err = PTR_ERR(xdst);
2105 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002106 } else if (xdst == NULL) {
2107 num_xfrms = 0;
2108 drop_pols = num_pols;
2109 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002110 }
2111
Steffen Klassertfbd50602011-03-15 21:12:49 +00002112 dst_hold(&xdst->u.dst);
2113
Fan Du283bc9f2013-11-07 17:47:50 +08002114 spin_lock_bh(&net->xfrm.xfrm_policy_sk_bundle_lock);
Timo Teräs80c802f2010-04-07 00:30:05 +00002115 xdst->u.dst.next = xfrm_policy_sk_bundles;
2116 xfrm_policy_sk_bundles = &xdst->u.dst;
Fan Du283bc9f2013-11-07 17:47:50 +08002117 spin_unlock_bh(&net->xfrm.xfrm_policy_sk_bundle_lock);
Timo Teräs80c802f2010-04-07 00:30:05 +00002118
2119 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002120 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Timo Teräs80c802f2010-04-07 00:30:05 +00002123 if (xdst == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002125 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002126 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002127 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Timo Teräs80c802f2010-04-07 00:30:05 +00002129 flo = flow_cache_lookup(net, fl, family, dir,
2130 xfrm_bundle_lookup, dst_orig);
2131 if (flo == NULL)
2132 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002133 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002134 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002135 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002136 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002137 xdst = container_of(flo, struct xfrm_dst, flo);
2138
2139 num_pols = xdst->num_pols;
2140 num_xfrms = xdst->num_xfrms;
2141 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy*) * num_pols);
2142 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 }
2144
Timo Teräs80c802f2010-04-07 00:30:05 +00002145 dst = &xdst->u.dst;
2146 if (route == NULL && num_xfrms > 0) {
2147 /* The only case when xfrm_bundle_lookup() returns a
2148 * bundle with null route, is when the template could
2149 * not be resolved. It means policies are there, but
2150 * bundle could not be created, since we don't yet
2151 * have the xfrm_state's. We need to wait for KM to
2152 * negotiate new SA's or bail out with error.*/
2153 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002154 dst_release(dst);
Timo Terasa1aa3482010-05-15 23:49:26 -07002155 xfrm_pols_put(pols, drop_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002156 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
David S. Miller2774c132011-03-01 14:59:04 -08002157
David S. Miller452edd52011-03-02 13:27:41 -08002158 return make_blackhole(net, family, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002159 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002160
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002161 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002162
2163 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2164 goto error;
2165 }
2166
2167no_transform:
2168 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002169 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Timo Teräs80c802f2010-04-07 00:30:05 +00002171 if ((flags & XFRM_LOOKUP_ICMP) &&
2172 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2173 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002174 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002175 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002176
Timo Teräs80c802f2010-04-07 00:30:05 +00002177 for (i = 0; i < num_pols; i++)
2178 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002179
Timo Teräs80c802f2010-04-07 00:30:05 +00002180 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002182 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002183 err = -EPERM;
2184 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002185 } else if (num_xfrms > 0) {
2186 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002187 dst_release(dst_orig);
2188 } else {
2189 /* Flow passes untransformed */
2190 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002191 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002193ok:
2194 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002195 if (dst && dst->xfrm &&
2196 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2197 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002198 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Timo Teräs80c802f2010-04-07 00:30:05 +00002200nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002201 if (!(flags & XFRM_LOOKUP_ICMP)) {
2202 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002203 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002204 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002205 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002207 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002208dropdst:
2209 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002210 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002211 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213EXPORT_SYMBOL(xfrm_lookup);
2214
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002215static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002216xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002217{
2218 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002219
2220 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2221 return 0;
2222 x = skb->sp->xvec[idx];
2223 if (!x->type->reject)
2224 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002225 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002226}
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228/* When skb is transformed back to its "native" form, we have to
2229 * check policy restrictions. At the moment we make this in maximally
2230 * stupid way. Shame on me. :-) Of course, connected sockets must
2231 * have policy cached at them.
2232 */
2233
2234static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002235xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 unsigned short family)
2237{
2238 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002239 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 return x->id.proto == tmpl->id.proto &&
2241 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2242 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2243 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002244 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002245 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002246 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2247 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248}
2249
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002250/*
2251 * 0 or more than 0 is returned when validation is succeeded (either bypass
2252 * because of optional transport mode, or next index of the mathced secpath
2253 * state with the template.
2254 * -1 is returned when no matching template is found.
2255 * Otherwise "-2 - errored_index" is returned.
2256 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002258xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 unsigned short family)
2260{
2261 int idx = start;
2262
2263 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002264 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return start;
2266 } else
2267 start = -1;
2268 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002269 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002271 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2272 if (start == -1)
2273 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 }
2277 return start;
2278}
2279
Herbert Xud5422ef2007-12-12 10:44:16 -08002280int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2281 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
2283 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002284 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 if (unlikely(afinfo == NULL))
2287 return -EAFNOSUPPORT;
2288
Herbert Xud5422ef2007-12-12 10:44:16 -08002289 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002290 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002292 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293}
Herbert Xud5422ef2007-12-12 10:44:16 -08002294EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
David S. Miller9a7386e2011-02-24 01:44:12 -05002296static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
2298 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002299 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002300 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 }
2304
2305 return 0;
2306}
2307
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002308int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 unsigned short family)
2310{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002311 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002313 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2314 int npols = 0;
2315 int xfrm_nr;
2316 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002317 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002319 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002320 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Herbert Xud5422ef2007-12-12 10:44:16 -08002322 reverse = dir & ~XFRM_POLICY_MASK;
2323 dir &= XFRM_POLICY_MASK;
2324 fl_dir = policy_to_flow_dir(dir);
2325
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002326 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002327 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002329 }
2330
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002331 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
2333 /* First, check used SA against their selectors. */
2334 if (skb->sp) {
2335 int i;
2336
2337 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002338 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002339 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002340 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 }
2344 }
2345
2346 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002347 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002348 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002349 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002350 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002351 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002352 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002355 if (!pol) {
2356 struct flow_cache_object *flo;
2357
2358 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2359 xfrm_policy_lookup, NULL);
2360 if (IS_ERR_OR_NULL(flo))
2361 pol = ERR_CAST(flo);
2362 else
2363 pol = container_of(flo, struct xfrm_policy, flo);
2364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002366 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002367 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002368 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002369 }
James Morris134b0fc2006-10-05 15:42:27 -05002370
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002371 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002372 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002373 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002374 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002375 return 0;
2376 }
2377 return 1;
2378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
James Morris9d729f72007-03-04 16:12:44 -08002380 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002382 pols[0] = pol;
2383 npols ++;
2384#ifdef CONFIG_XFRM_SUB_POLICY
2385 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002386 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002387 &fl, family,
2388 XFRM_POLICY_IN);
2389 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002390 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002391 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002392 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002393 }
James Morris9d729f72007-03-04 16:12:44 -08002394 pols[1]->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002395 npols ++;
2396 }
2397 }
2398#endif
2399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (pol->action == XFRM_POLICY_ALLOW) {
2401 struct sec_path *sp;
2402 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002403 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002404 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002405 struct xfrm_tmpl **tpp = tp;
2406 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 int i, k;
2408
2409 if ((sp = skb->sp) == NULL)
2410 sp = &dummy;
2411
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002412 for (pi = 0; pi < npols; pi++) {
2413 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002414 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002415 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002416 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002417 }
2418 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002419 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002420 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002421 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002422 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2423 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2424 }
2425 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002426 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002427 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002428 tpp = stp;
2429 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 /* For each tunnel xfrm, find the first matching tmpl.
2432 * For each tmpl before that, find corresponding xfrm.
2433 * Order is _important_. Later we will implement
2434 * some barriers, but at the moment barriers
2435 * are implied between each two transformations.
2436 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002437 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2438 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002439 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002440 if (k < -1)
2441 /* "-2 - errored_index" returned */
2442 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002443 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
2447
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002448 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002449 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002453 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 return 1;
2455 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002456 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
2458reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002459 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002460reject_error:
2461 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 return 0;
2463}
2464EXPORT_SYMBOL(__xfrm_policy_check);
2465
2466int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2467{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002468 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002470 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002471 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002473 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002474 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002478 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002479
David S. Miller452edd52011-03-02 13:27:41 -08002480 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, 0);
2481 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002482 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002483 dst = NULL;
2484 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002485 skb_dst_set(skb, dst);
2486 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488EXPORT_SYMBOL(__xfrm_route_forward);
2489
David S. Millerd49c73c2006-08-13 18:55:53 -07002490/* Optimize later using cookies and generation ids. */
2491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2493{
David S. Millerd49c73c2006-08-13 18:55:53 -07002494 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002495 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2496 * get validated by dst_ops->check on every use. We do this
2497 * because when a normal route referenced by an XFRM dst is
2498 * obsoleted we do not go looking around for all parent
2499 * referencing XFRM dsts so that we can invalidate them. It
2500 * is just too much work. Instead we make the checks here on
2501 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002502 *
2503 * XFRM dst A --> IPv4 dst X
2504 *
2505 * X is the "xdst->route" of A (X is also the "dst->path" of A
2506 * in this example). If X is marked obsolete, "A" will not
2507 * notice. That's what we are validating here via the
2508 * stale_bundle() check.
2509 *
2510 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002511 * dst which causes it's ->obsolete field to be set to
2512 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2513 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002514 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002515 if (dst->obsolete < 0 && !stale_bundle(dst))
2516 return dst;
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 return NULL;
2519}
2520
2521static int stale_bundle(struct dst_entry *dst)
2522{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002523 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524}
2525
Herbert Xuaabc9762005-05-03 16:27:10 -07002526void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002529 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002530 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 dev_put(dev);
2532 }
2533}
Herbert Xuaabc9762005-05-03 16:27:10 -07002534EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536static void xfrm_link_failure(struct sk_buff *skb)
2537{
2538 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539}
2540
2541static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2542{
2543 if (dst) {
2544 if (dst->obsolete) {
2545 dst_release(dst);
2546 dst = NULL;
2547 }
2548 }
2549 return dst;
2550}
2551
Alexey Dobriyanddcfd792008-11-25 17:37:23 -08002552static void __xfrm_garbage_collect(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
Timo Teräs80c802f2010-04-07 00:30:05 +00002554 struct dst_entry *head, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Fan Du283bc9f2013-11-07 17:47:50 +08002556 spin_lock_bh(&net->xfrm.xfrm_policy_sk_bundle_lock);
Timo Teräs80c802f2010-04-07 00:30:05 +00002557 head = xfrm_policy_sk_bundles;
2558 xfrm_policy_sk_bundles = NULL;
Fan Du283bc9f2013-11-07 17:47:50 +08002559 spin_unlock_bh(&net->xfrm.xfrm_policy_sk_bundle_lock);
Timo Teräs80c802f2010-04-07 00:30:05 +00002560
2561 while (head) {
2562 next = head->next;
2563 dst_free(head);
2564 head = next;
2565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566}
2567
Paul Mooree4c17212013-05-29 07:36:25 +00002568void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002569{
2570 flow_cache_flush();
2571 __xfrm_garbage_collect(net);
2572}
Paul Mooree4c17212013-05-29 07:36:25 +00002573EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002574
2575static void xfrm_garbage_collect_deferred(struct net *net)
2576{
2577 flow_cache_flush_deferred();
2578 __xfrm_garbage_collect(net);
2579}
2580
Herbert Xu25ee3282007-12-11 09:32:34 -08002581static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582{
2583 do {
2584 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2585 u32 pmtu, route_mtu_cached;
2586
2587 pmtu = dst_mtu(dst->child);
2588 xdst->child_mtu_cached = pmtu;
2589
2590 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2591
2592 route_mtu_cached = dst_mtu(xdst->route);
2593 xdst->route_mtu_cached = route_mtu_cached;
2594
2595 if (pmtu > route_mtu_cached)
2596 pmtu = route_mtu_cached;
2597
David S. Millerdefb3512010-12-08 21:16:57 -08002598 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 } while ((dst = dst->next));
2600}
2601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602/* Check that the bundle accepts the flow and its components are
2603 * still valid.
2604 */
2605
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002606static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607{
2608 struct dst_entry *dst = &first->u.dst;
2609 struct xfrm_dst *last;
2610 u32 mtu;
2611
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002612 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 (dst->dev && !netif_running(dst->dev)))
2614 return 0;
2615
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002616 if (dst->flags & DST_XFRM_QUEUE)
2617 return 1;
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 last = NULL;
2620
2621 do {
2622 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2625 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002626 if (xdst->xfrm_genid != dst->xfrm->genid)
2627 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002628 if (xdst->num_pols > 0 &&
2629 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002630 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632 mtu = dst_mtu(dst->child);
2633 if (xdst->child_mtu_cached != mtu) {
2634 last = xdst;
2635 xdst->child_mtu_cached = mtu;
2636 }
2637
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002638 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 return 0;
2640 mtu = dst_mtu(xdst->route);
2641 if (xdst->route_mtu_cached != mtu) {
2642 last = xdst;
2643 xdst->route_mtu_cached = mtu;
2644 }
2645
2646 dst = dst->child;
2647 } while (dst->xfrm);
2648
2649 if (likely(!last))
2650 return 1;
2651
2652 mtu = last->child_mtu_cached;
2653 for (;;) {
2654 dst = &last->u.dst;
2655
2656 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2657 if (mtu > last->route_mtu_cached)
2658 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002659 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
2661 if (last == first)
2662 break;
2663
Patrick McHardybd0bf072007-07-18 01:55:52 -07002664 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 last->child_mtu_cached = mtu;
2666 }
2667
2668 return 1;
2669}
2670
David S. Miller0dbaee32010-12-13 12:52:14 -08002671static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2672{
2673 return dst_metric_advmss(dst->path);
2674}
2675
Steffen Klassertebb762f2011-11-23 02:12:51 +00002676static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002677{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002678 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2679
2680 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002681}
2682
David S. Millerf894cbf2012-07-02 21:52:24 -07002683static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2684 struct sk_buff *skb,
2685 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002686{
David S. Millerf894cbf2012-07-02 21:52:24 -07002687 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002688}
2689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2691{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002692 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 int err = 0;
2694 if (unlikely(afinfo == NULL))
2695 return -EINVAL;
2696 if (unlikely(afinfo->family >= NPROTO))
2697 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002698 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2700 err = -ENOBUFS;
2701 else {
2702 struct dst_ops *dst_ops = afinfo->dst_ops;
2703 if (likely(dst_ops->kmem_cachep == NULL))
2704 dst_ops->kmem_cachep = xfrm_dst_cache;
2705 if (likely(dst_ops->check == NULL))
2706 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002707 if (likely(dst_ops->default_advmss == NULL))
2708 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002709 if (likely(dst_ops->mtu == NULL))
2710 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if (likely(dst_ops->negative_advice == NULL))
2712 dst_ops->negative_advice = xfrm_negative_advice;
2713 if (likely(dst_ops->link_failure == NULL))
2714 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002715 if (likely(dst_ops->neigh_lookup == NULL))
2716 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002718 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002719 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002721 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002722
2723 rtnl_lock();
2724 for_each_net(net) {
2725 struct dst_ops *xfrm_dst_ops;
2726
2727 switch (afinfo->family) {
2728 case AF_INET:
2729 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2730 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002731#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002732 case AF_INET6:
2733 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2734 break;
2735#endif
2736 default:
2737 BUG();
2738 }
2739 *xfrm_dst_ops = *afinfo->dst_ops;
2740 }
2741 rtnl_unlock();
2742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 return err;
2744}
2745EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2746
2747int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2748{
2749 int err = 0;
2750 if (unlikely(afinfo == NULL))
2751 return -EINVAL;
2752 if (unlikely(afinfo->family >= NPROTO))
2753 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002754 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2756 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2757 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002758 else
2759 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2760 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002762 spin_unlock(&xfrm_policy_afinfo_lock);
2763 if (!err) {
2764 struct dst_ops *dst_ops = afinfo->dst_ops;
2765
2766 synchronize_rcu();
2767
2768 dst_ops->kmem_cachep = NULL;
2769 dst_ops->check = NULL;
2770 dst_ops->negative_advice = NULL;
2771 dst_ops->link_failure = NULL;
2772 afinfo->garbage_collect = NULL;
2773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 return err;
2775}
2776EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2777
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002778static void __net_init xfrm_dst_ops_init(struct net *net)
2779{
2780 struct xfrm_policy_afinfo *afinfo;
2781
Eric Dumazetef8531b2012-08-19 12:31:48 +02002782 rcu_read_lock();
2783 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002784 if (afinfo)
2785 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002786#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetef8531b2012-08-19 12:31:48 +02002787 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002788 if (afinfo)
2789 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2790#endif
Priyanka Jain418a99a2012-08-12 21:22:29 +00002791 rcu_read_unlock();
Herbert Xu546be242006-05-27 23:03:58 -07002792}
2793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2795{
Jiri Pirko351638e2013-05-28 01:30:21 +00002796 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 switch (event) {
2799 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002800 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 }
2802 return NOTIFY_DONE;
2803}
2804
2805static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002806 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807};
2808
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002809#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002810static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002811{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002812 int rv;
2813
Tejun Heo7d720c32010-02-16 15:20:26 +00002814 if (snmp_mib_init((void __percpu **)net->mib.xfrm_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00002815 sizeof(struct linux_xfrm_mib),
2816 __alignof__(struct linux_xfrm_mib)) < 0)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002817 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002818 rv = xfrm_proc_init(net);
2819 if (rv < 0)
Tejun Heo7d720c32010-02-16 15:20:26 +00002820 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002821 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002822}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002823
2824static void xfrm_statistics_fini(struct net *net)
2825{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002826 xfrm_proc_fini(net);
Tejun Heo7d720c32010-02-16 15:20:26 +00002827 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002828}
2829#else
2830static int __net_init xfrm_statistics_init(struct net *net)
2831{
2832 return 0;
2833}
2834
2835static void xfrm_statistics_fini(struct net *net)
2836{
2837}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002838#endif
2839
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002840static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
David S. Miller2518c7c2006-08-24 04:45:07 -07002842 unsigned int hmask, sz;
2843 int dir;
2844
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002845 if (net_eq(net, &init_net))
2846 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002848 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002849 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
David S. Miller2518c7c2006-08-24 04:45:07 -07002851 hmask = 8 - 1;
2852 sz = (hmask+1) * sizeof(struct hlist_head);
2853
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002854 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2855 if (!net->xfrm.policy_byidx)
2856 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002857 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002858
2859 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2860 struct xfrm_policy_hash *htab;
2861
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002862 net->xfrm.policy_count[dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002863 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002864
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002865 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002866 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002867 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002868 goto out_bydst;
2869 htab->hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002870 }
2871
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002872 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002873 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002874 if (net_eq(net, &init_net))
2875 register_netdevice_notifier(&xfrm_dev_notifier);
2876 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002877
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002878out_bydst:
2879 for (dir--; dir >= 0; dir--) {
2880 struct xfrm_policy_hash *htab;
2881
2882 htab = &net->xfrm.policy_bydst[dir];
2883 xfrm_hash_free(htab->table, sz);
2884 }
2885 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002886out_byidx:
2887 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888}
2889
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002890static void xfrm_policy_fini(struct net *net)
2891{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002892 struct xfrm_audit audit_info;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002893 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002894 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002895
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002896 flush_work(&net->xfrm.policy_hash_work);
2897#ifdef CONFIG_XFRM_SUB_POLICY
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002898 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002899 audit_info.sessionid = -1;
2900 audit_info.secid = 0;
2901 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, &audit_info);
2902#endif
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002903 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002904 audit_info.sessionid = -1;
2905 audit_info.secid = 0;
2906 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002907
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002908 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002909
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002910 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002911 struct xfrm_policy_hash *htab;
2912
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002913 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002914
2915 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01002916 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002917 WARN_ON(!hlist_empty(htab->table));
2918 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002919 }
2920
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002921 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002922 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2923 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002924}
2925
2926static int __net_init xfrm_net_init(struct net *net)
2927{
2928 int rv;
2929
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002930 rv = xfrm_statistics_init(net);
2931 if (rv < 0)
2932 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002933 rv = xfrm_state_init(net);
2934 if (rv < 0)
2935 goto out_state;
2936 rv = xfrm_policy_init(net);
2937 if (rv < 0)
2938 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002939 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002940 rv = xfrm_sysctl_init(net);
2941 if (rv < 0)
2942 goto out_sysctl;
Fan Du283bc9f2013-11-07 17:47:50 +08002943
2944 /* Initialize the per-net locks here */
2945 spin_lock_init(&net->xfrm.xfrm_state_lock);
2946 rwlock_init(&net->xfrm.xfrm_policy_lock);
2947 spin_lock_init(&net->xfrm.xfrm_policy_sk_bundle_lock);
2948 mutex_init(&net->xfrm.xfrm_cfg_mutex);
2949
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002950 return 0;
2951
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002952out_sysctl:
2953 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002954out_policy:
2955 xfrm_state_fini(net);
2956out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002957 xfrm_statistics_fini(net);
2958out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002959 return rv;
2960}
2961
2962static void __net_exit xfrm_net_exit(struct net *net)
2963{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002964 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002965 xfrm_policy_fini(net);
2966 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002967 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002968}
2969
2970static struct pernet_operations __net_initdata xfrm_net_ops = {
2971 .init = xfrm_net_init,
2972 .exit = xfrm_net_exit,
2973};
2974
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975void __init xfrm_init(void)
2976{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002977 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 xfrm_input_init();
2979}
2980
Joy Lattenab5f5e82007-09-17 11:51:22 -07002981#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08002982static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2983 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002984{
Paul Moore875179f2007-12-01 23:27:18 +11002985 struct xfrm_sec_ctx *ctx = xp->security;
2986 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002987
Paul Moore875179f2007-12-01 23:27:18 +11002988 if (ctx)
2989 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2990 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2991
2992 switch(sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002993 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002994 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002995 if (sel->prefixlen_s != 32)
2996 audit_log_format(audit_buf, " src_prefixlen=%d",
2997 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002998 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002999 if (sel->prefixlen_d != 32)
3000 audit_log_format(audit_buf, " dst_prefixlen=%d",
3001 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003002 break;
3003 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003004 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003005 if (sel->prefixlen_s != 128)
3006 audit_log_format(audit_buf, " src_prefixlen=%d",
3007 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003008 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003009 if (sel->prefixlen_d != 128)
3010 audit_log_format(audit_buf, " dst_prefixlen=%d",
3011 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003012 break;
3013 }
3014}
3015
Paul Moore68277ac2007-12-20 20:49:33 -08003016void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07003017 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003018{
3019 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003020
Paul Mooreafeb14b2007-12-21 14:58:11 -08003021 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003022 if (audit_buf == NULL)
3023 return;
Eric Paris25323862008-04-18 10:09:25 -04003024 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003025 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003026 xfrm_audit_common_policyinfo(xp, audit_buf);
3027 audit_log_end(audit_buf);
3028}
3029EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3030
Paul Moore68277ac2007-12-20 20:49:33 -08003031void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07003032 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003033{
3034 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003035
Paul Mooreafeb14b2007-12-21 14:58:11 -08003036 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003037 if (audit_buf == NULL)
3038 return;
Eric Paris25323862008-04-18 10:09:25 -04003039 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003040 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003041 xfrm_audit_common_policyinfo(xp, audit_buf);
3042 audit_log_end(audit_buf);
3043}
3044EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3045#endif
3046
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003047#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003048static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3049 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003050{
3051 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3052 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003053 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3054 sel_cmp->family) &&
3055 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3056 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003057 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3058 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003059 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003060 }
3061 } else {
3062 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003063 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003064 }
3065 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003066 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003067}
3068
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003069static struct xfrm_policy * xfrm_migrate_policy_find(const struct xfrm_selector *sel,
Fan Du8d549c42013-11-07 17:47:49 +08003070 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003071{
3072 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003073 struct hlist_head *chain;
3074 u32 priority = ~0U;
3075
Fan Du283bc9f2013-11-07 17:47:50 +08003076 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
Fan Du8d549c42013-11-07 17:47:49 +08003077 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, 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 ret = pol;
3082 priority = ret->priority;
3083 break;
3084 }
3085 }
Fan Du8d549c42013-11-07 17:47:49 +08003086 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003087 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003088 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3089 pol->type == type &&
3090 pol->priority < priority) {
3091 ret = pol;
3092 break;
3093 }
3094 }
3095
3096 if (ret)
3097 xfrm_pol_hold(ret);
3098
Fan Du283bc9f2013-11-07 17:47:50 +08003099 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003100
3101 return ret;
3102}
3103
David S. Millerdd701752011-02-24 00:21:08 -05003104static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003105{
3106 int match = 0;
3107
3108 if (t->mode == m->mode && t->id.proto == m->proto &&
3109 (m->reqid == 0 || t->reqid == m->reqid)) {
3110 switch (t->mode) {
3111 case XFRM_MODE_TUNNEL:
3112 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003113 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3114 m->old_family) &&
3115 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3116 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003117 match = 1;
3118 }
3119 break;
3120 case XFRM_MODE_TRANSPORT:
3121 /* in case of transport mode, template does not store
3122 any IP addresses, hence we just compare mode and
3123 protocol */
3124 match = 1;
3125 break;
3126 default:
3127 break;
3128 }
3129 }
3130 return match;
3131}
3132
3133/* update endpoint address(es) of template(s) */
3134static int xfrm_policy_migrate(struct xfrm_policy *pol,
3135 struct xfrm_migrate *m, int num_migrate)
3136{
3137 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003138 int i, j, n = 0;
3139
3140 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003141 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003142 /* target policy has been deleted */
3143 write_unlock_bh(&pol->lock);
3144 return -ENOENT;
3145 }
3146
3147 for (i = 0; i < pol->xfrm_nr; i++) {
3148 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3149 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3150 continue;
3151 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003152 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3153 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003154 continue;
3155 /* update endpoints */
3156 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3157 sizeof(pol->xfrm_vec[i].id.daddr));
3158 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3159 sizeof(pol->xfrm_vec[i].saddr));
3160 pol->xfrm_vec[i].encap_family = mp->new_family;
3161 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003162 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003163 }
3164 }
3165
3166 write_unlock_bh(&pol->lock);
3167
3168 if (!n)
3169 return -ENODATA;
3170
3171 return 0;
3172}
3173
David S. Millerdd701752011-02-24 00:21:08 -05003174static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003175{
3176 int i, j;
3177
3178 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3179 return -EINVAL;
3180
3181 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003182 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3183 m[i].old_family) &&
3184 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3185 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003186 return -EINVAL;
3187 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3188 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3189 return -EINVAL;
3190
3191 /* check if there is any duplicated entry */
3192 for (j = i + 1; j < num_migrate; j++) {
3193 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3194 sizeof(m[i].old_daddr)) &&
3195 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3196 sizeof(m[i].old_saddr)) &&
3197 m[i].proto == m[j].proto &&
3198 m[i].mode == m[j].mode &&
3199 m[i].reqid == m[j].reqid &&
3200 m[i].old_family == m[j].old_family)
3201 return -EINVAL;
3202 }
3203 }
3204
3205 return 0;
3206}
3207
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003208int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003209 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003210 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003211{
3212 int i, err, nx_cur = 0, nx_new = 0;
3213 struct xfrm_policy *pol = NULL;
3214 struct xfrm_state *x, *xc;
3215 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3216 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3217 struct xfrm_migrate *mp;
3218
3219 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3220 goto out;
3221
3222 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003223 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003224 err = -ENOENT;
3225 goto out;
3226 }
3227
3228 /* Stage 2 - find and update state(s) */
3229 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003230 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003231 x_cur[nx_cur] = x;
3232 nx_cur++;
3233 if ((xc = xfrm_state_migrate(x, mp))) {
3234 x_new[nx_new] = xc;
3235 nx_new++;
3236 } else {
3237 err = -ENODATA;
3238 goto restore_state;
3239 }
3240 }
3241 }
3242
3243 /* Stage 3 - update policy */
3244 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3245 goto restore_state;
3246
3247 /* Stage 4 - delete old state(s) */
3248 if (nx_cur) {
3249 xfrm_states_put(x_cur, nx_cur);
3250 xfrm_states_delete(x_cur, nx_cur);
3251 }
3252
3253 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003254 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003255
3256 xfrm_pol_put(pol);
3257
3258 return 0;
3259out:
3260 return err;
3261
3262restore_state:
3263 if (pol)
3264 xfrm_pol_put(pol);
3265 if (nx_cur)
3266 xfrm_states_put(x_cur, nx_cur);
3267 if (nx_new)
3268 xfrm_states_delete(x_new, nx_new);
3269
3270 return err;
3271}
David S. Millere610e672007-02-08 13:29:15 -08003272EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003273#endif