blob: a7487f34e813c5ad6a3d3e4550a487c2a340ee65 [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
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001644static int inline
David S. Miller3f0e18f2011-02-22 18:38:14 -08001645xfrm_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 }
1652 memcpy(*target, src, size);
1653 return 0;
1654}
1655
1656static int inline
David S. Miller1786b382011-02-24 01:32:54 -05001657xfrm_dst_update_parent(struct dst_entry *dst, const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001658{
1659#ifdef CONFIG_XFRM_SUB_POLICY
1660 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1661 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1662 sel, sizeof(*sel));
1663#else
1664 return 0;
1665#endif
1666}
1667
1668static int inline
David S. Miller3f0e18f2011-02-22 18:38:14 -08001669xfrm_dst_update_origin(struct dst_entry *dst, const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001670{
1671#ifdef CONFIG_XFRM_SUB_POLICY
1672 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1673 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1674#else
1675 return 0;
1676#endif
1677}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
David S. Miller73ff93c2011-02-22 18:33:42 -08001679static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001680 struct xfrm_policy **pols,
1681 int *num_pols, int *num_xfrms)
1682{
1683 int i;
1684
1685 if (*num_pols == 0 || !pols[0]) {
1686 *num_pols = 0;
1687 *num_xfrms = 0;
1688 return 0;
1689 }
1690 if (IS_ERR(pols[0]))
1691 return PTR_ERR(pols[0]);
1692
1693 *num_xfrms = pols[0]->xfrm_nr;
1694
1695#ifdef CONFIG_XFRM_SUB_POLICY
1696 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1697 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1698 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1699 XFRM_POLICY_TYPE_MAIN,
1700 fl, family,
1701 XFRM_POLICY_OUT);
1702 if (pols[1]) {
1703 if (IS_ERR(pols[1])) {
1704 xfrm_pols_put(pols, *num_pols);
1705 return PTR_ERR(pols[1]);
1706 }
1707 (*num_pols) ++;
1708 (*num_xfrms) += pols[1]->xfrm_nr;
1709 }
1710 }
1711#endif
1712 for (i = 0; i < *num_pols; i++) {
1713 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1714 *num_xfrms = -1;
1715 break;
1716 }
1717 }
1718
1719 return 0;
1720
1721}
1722
1723static struct xfrm_dst *
1724xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001725 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001726 struct dst_entry *dst_orig)
1727{
1728 struct net *net = xp_net(pols[0]);
1729 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1730 struct dst_entry *dst;
1731 struct xfrm_dst *xdst;
1732 int err;
1733
1734 /* Try to instantiate a bundle */
1735 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001736 if (err <= 0) {
1737 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001738 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1739 return ERR_PTR(err);
1740 }
1741
1742 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1743 if (IS_ERR(dst)) {
1744 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1745 return ERR_CAST(dst);
1746 }
1747
1748 xdst = (struct xfrm_dst *)dst;
1749 xdst->num_xfrms = err;
1750 if (num_pols > 1)
1751 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1752 else
1753 err = xfrm_dst_update_origin(dst, fl);
1754 if (unlikely(err)) {
1755 dst_free(dst);
1756 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1757 return ERR_PTR(err);
1758 }
1759
1760 xdst->num_pols = num_pols;
1761 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
1762 xdst->policy_genid = atomic_read(&pols[0]->genid);
1763
1764 return xdst;
1765}
1766
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001767static void xfrm_policy_queue_process(unsigned long arg)
1768{
1769 int err = 0;
1770 struct sk_buff *skb;
1771 struct sock *sk;
1772 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001773 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1774 struct xfrm_policy_queue *pq = &pol->polq;
1775 struct flowi fl;
1776 struct sk_buff_head list;
1777
1778 spin_lock(&pq->hold_queue.lock);
1779 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001780 if (!skb) {
1781 spin_unlock(&pq->hold_queue.lock);
1782 goto out;
1783 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001784 dst = skb_dst(skb);
1785 sk = skb->sk;
1786 xfrm_decode_session(skb, &fl, dst->ops->family);
1787 spin_unlock(&pq->hold_queue.lock);
1788
1789 dst_hold(dst->path);
1790 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1791 sk, 0);
1792 if (IS_ERR(dst))
1793 goto purge_queue;
1794
1795 if (dst->flags & DST_XFRM_QUEUE) {
1796 dst_release(dst);
1797
1798 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1799 goto purge_queue;
1800
1801 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001802 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1803 xfrm_pol_hold(pol);
1804 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001805 }
1806
1807 dst_release(dst);
1808
1809 __skb_queue_head_init(&list);
1810
1811 spin_lock(&pq->hold_queue.lock);
1812 pq->timeout = 0;
1813 skb_queue_splice_init(&pq->hold_queue, &list);
1814 spin_unlock(&pq->hold_queue.lock);
1815
1816 while (!skb_queue_empty(&list)) {
1817 skb = __skb_dequeue(&list);
1818
1819 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1820 dst_hold(skb_dst(skb)->path);
1821 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1822 &fl, skb->sk, 0);
1823 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001824 kfree_skb(skb);
1825 continue;
1826 }
1827
1828 nf_reset(skb);
1829 skb_dst_drop(skb);
1830 skb_dst_set(skb, dst);
1831
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001832 err = dst_output(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001833 }
1834
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001835out:
1836 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001837 return;
1838
1839purge_queue:
1840 pq->timeout = 0;
1841 xfrm_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001842 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001843}
1844
1845static int xdst_queue_output(struct sk_buff *skb)
1846{
1847 unsigned long sched_next;
1848 struct dst_entry *dst = skb_dst(skb);
1849 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001850 struct xfrm_policy *pol = xdst->pols[0];
1851 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001852 const struct sk_buff *fclone = skb + 1;
1853
1854 if (unlikely(skb->fclone == SKB_FCLONE_ORIG &&
1855 fclone->fclone == SKB_FCLONE_CLONE)) {
1856 kfree_skb(skb);
1857 return 0;
1858 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001859
1860 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1861 kfree_skb(skb);
1862 return -EAGAIN;
1863 }
1864
1865 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001866
1867 spin_lock_bh(&pq->hold_queue.lock);
1868
1869 if (!pq->timeout)
1870 pq->timeout = XFRM_QUEUE_TMO_MIN;
1871
1872 sched_next = jiffies + pq->timeout;
1873
1874 if (del_timer(&pq->hold_timer)) {
1875 if (time_before(pq->hold_timer.expires, sched_next))
1876 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001877 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001878 }
1879
1880 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001881 if (!mod_timer(&pq->hold_timer, sched_next))
1882 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001883
1884 spin_unlock_bh(&pq->hold_queue.lock);
1885
1886 return 0;
1887}
1888
1889static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
1890 struct dst_entry *dst,
1891 const struct flowi *fl,
1892 int num_xfrms,
1893 u16 family)
1894{
1895 int err;
1896 struct net_device *dev;
1897 struct dst_entry *dst1;
1898 struct xfrm_dst *xdst;
1899
1900 xdst = xfrm_alloc_dst(net, family);
1901 if (IS_ERR(xdst))
1902 return xdst;
1903
Steffen Klassert5b8ef342013-08-27 13:43:30 +02001904 if (net->xfrm.sysctl_larval_drop || num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001905 return xdst;
1906
1907 dst1 = &xdst->u.dst;
1908 dst_hold(dst);
1909 xdst->route = dst;
1910
1911 dst_copy_metrics(dst1, dst);
1912
1913 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1914 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
1915 dst1->lastuse = jiffies;
1916
1917 dst1->input = dst_discard;
1918 dst1->output = xdst_queue_output;
1919
1920 dst_hold(dst);
1921 dst1->child = dst;
1922 dst1->path = dst;
1923
1924 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
1925
1926 err = -ENODEV;
1927 dev = dst->dev;
1928 if (!dev)
1929 goto free_dst;
1930
1931 err = xfrm_fill_dst(xdst, dev, fl);
1932 if (err)
1933 goto free_dst;
1934
1935out:
1936 return xdst;
1937
1938free_dst:
1939 dst_release(dst1);
1940 xdst = ERR_PTR(err);
1941 goto out;
1942}
1943
Timo Teräs80c802f2010-04-07 00:30:05 +00001944static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001945xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00001946 struct flow_cache_object *oldflo, void *ctx)
1947{
1948 struct dst_entry *dst_orig = (struct dst_entry *)ctx;
1949 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1950 struct xfrm_dst *xdst, *new_xdst;
1951 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
1952
1953 /* Check if the policies from old bundle are usable */
1954 xdst = NULL;
1955 if (oldflo) {
1956 xdst = container_of(oldflo, struct xfrm_dst, flo);
1957 num_pols = xdst->num_pols;
1958 num_xfrms = xdst->num_xfrms;
1959 pol_dead = 0;
1960 for (i = 0; i < num_pols; i++) {
1961 pols[i] = xdst->pols[i];
1962 pol_dead |= pols[i]->walk.dead;
1963 }
1964 if (pol_dead) {
1965 dst_free(&xdst->u.dst);
1966 xdst = NULL;
1967 num_pols = 0;
1968 num_xfrms = 0;
1969 oldflo = NULL;
1970 }
1971 }
1972
1973 /* Resolve policies to use if we couldn't get them from
1974 * previous cache entry */
1975 if (xdst == NULL) {
1976 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001977 pols[0] = __xfrm_policy_lookup(net, fl, family,
1978 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001979 err = xfrm_expand_policies(fl, family, pols,
1980 &num_pols, &num_xfrms);
1981 if (err < 0)
1982 goto inc_error;
1983 if (num_pols == 0)
1984 return NULL;
1985 if (num_xfrms <= 0)
1986 goto make_dummy_bundle;
1987 }
1988
1989 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family, dst_orig);
1990 if (IS_ERR(new_xdst)) {
1991 err = PTR_ERR(new_xdst);
1992 if (err != -EAGAIN)
1993 goto error;
1994 if (oldflo == NULL)
1995 goto make_dummy_bundle;
1996 dst_hold(&xdst->u.dst);
1997 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00001998 } else if (new_xdst == NULL) {
1999 num_xfrms = 0;
2000 if (oldflo == NULL)
2001 goto make_dummy_bundle;
2002 xdst->num_xfrms = 0;
2003 dst_hold(&xdst->u.dst);
2004 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002005 }
2006
2007 /* Kill the previous bundle */
2008 if (xdst) {
2009 /* The policies were stolen for newly generated bundle */
2010 xdst->num_pols = 0;
2011 dst_free(&xdst->u.dst);
2012 }
2013
2014 /* Flow cache does not have reference, it dst_free()'s,
2015 * but we do need to return one reference for original caller */
2016 dst_hold(&new_xdst->u.dst);
2017 return &new_xdst->flo;
2018
2019make_dummy_bundle:
2020 /* We found policies, but there's no bundles to instantiate:
2021 * either because the policy blocks, has no transformations or
2022 * we could not build template (no xfrm_states).*/
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002023 xdst = xfrm_create_dummy_bundle(net, dst_orig, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002024 if (IS_ERR(xdst)) {
2025 xfrm_pols_put(pols, num_pols);
2026 return ERR_CAST(xdst);
2027 }
2028 xdst->num_pols = num_pols;
2029 xdst->num_xfrms = num_xfrms;
2030 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
2031
2032 dst_hold(&xdst->u.dst);
2033 return &xdst->flo;
2034
2035inc_error:
2036 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2037error:
2038 if (xdst != NULL)
2039 dst_free(&xdst->u.dst);
2040 else
2041 xfrm_pols_put(pols, num_pols);
2042 return ERR_PTR(err);
2043}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
David S. Miller2774c132011-03-01 14:59:04 -08002045static struct dst_entry *make_blackhole(struct net *net, u16 family,
2046 struct dst_entry *dst_orig)
2047{
2048 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2049 struct dst_entry *ret;
2050
2051 if (!afinfo) {
2052 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002053 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002054 } else {
2055 ret = afinfo->blackhole_route(net, dst_orig);
2056 }
2057 xfrm_policy_put_afinfo(afinfo);
2058
2059 return ret;
2060}
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062/* Main function: finds/creates a bundle for given flow.
2063 *
2064 * At the moment we eat a raw IP route. Mostly to speed up lookups
2065 * on interfaces with disabled IPsec.
2066 */
David S. Miller452edd52011-03-02 13:27:41 -08002067struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2068 const struct flowi *fl,
2069 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002071 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002072 struct flow_cache_object *flo;
2073 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002074 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002075 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002076 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002077 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002078
Timo Teräs80c802f2010-04-07 00:30:05 +00002079 dst = NULL;
2080 xdst = NULL;
2081 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002082
Thomas Graff7944fb2007-08-25 13:46:55 -07002083 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002084 num_pols = 1;
2085 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2086 err = xfrm_expand_policies(fl, family, pols,
2087 &num_pols, &num_xfrms);
2088 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002089 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002090
2091 if (num_pols) {
2092 if (num_xfrms <= 0) {
2093 drop_pols = num_pols;
2094 goto no_transform;
2095 }
2096
2097 xdst = xfrm_resolve_and_create_bundle(
2098 pols, num_pols, fl,
2099 family, dst_orig);
2100 if (IS_ERR(xdst)) {
2101 xfrm_pols_put(pols, num_pols);
2102 err = PTR_ERR(xdst);
2103 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002104 } else if (xdst == NULL) {
2105 num_xfrms = 0;
2106 drop_pols = num_pols;
2107 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002108 }
2109
Steffen Klassertfbd50602011-03-15 21:12:49 +00002110 dst_hold(&xdst->u.dst);
2111
Fan Du283bc9f2013-11-07 17:47:50 +08002112 spin_lock_bh(&net->xfrm.xfrm_policy_sk_bundle_lock);
Timo Teräs80c802f2010-04-07 00:30:05 +00002113 xdst->u.dst.next = xfrm_policy_sk_bundles;
2114 xfrm_policy_sk_bundles = &xdst->u.dst;
Fan Du283bc9f2013-11-07 17:47:50 +08002115 spin_unlock_bh(&net->xfrm.xfrm_policy_sk_bundle_lock);
Timo Teräs80c802f2010-04-07 00:30:05 +00002116
2117 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002118 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Timo Teräs80c802f2010-04-07 00:30:05 +00002121 if (xdst == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002123 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002124 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002125 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Timo Teräs80c802f2010-04-07 00:30:05 +00002127 flo = flow_cache_lookup(net, fl, family, dir,
2128 xfrm_bundle_lookup, dst_orig);
2129 if (flo == NULL)
2130 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002131 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002132 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002133 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002134 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002135 xdst = container_of(flo, struct xfrm_dst, flo);
2136
2137 num_pols = xdst->num_pols;
2138 num_xfrms = xdst->num_xfrms;
2139 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy*) * num_pols);
2140 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
2142
Timo Teräs80c802f2010-04-07 00:30:05 +00002143 dst = &xdst->u.dst;
2144 if (route == NULL && num_xfrms > 0) {
2145 /* The only case when xfrm_bundle_lookup() returns a
2146 * bundle with null route, is when the template could
2147 * not be resolved. It means policies are there, but
2148 * bundle could not be created, since we don't yet
2149 * have the xfrm_state's. We need to wait for KM to
2150 * negotiate new SA's or bail out with error.*/
2151 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002152 dst_release(dst);
Timo Terasa1aa3482010-05-15 23:49:26 -07002153 xfrm_pols_put(pols, drop_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002154 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
David S. Miller2774c132011-03-01 14:59:04 -08002155
David S. Miller452edd52011-03-02 13:27:41 -08002156 return make_blackhole(net, family, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002157 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002158
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002159 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002160
2161 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2162 goto error;
2163 }
2164
2165no_transform:
2166 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002167 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Timo Teräs80c802f2010-04-07 00:30:05 +00002169 if ((flags & XFRM_LOOKUP_ICMP) &&
2170 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2171 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002172 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002173 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002174
Timo Teräs80c802f2010-04-07 00:30:05 +00002175 for (i = 0; i < num_pols; i++)
2176 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002177
Timo Teräs80c802f2010-04-07 00:30:05 +00002178 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002180 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002181 err = -EPERM;
2182 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002183 } else if (num_xfrms > 0) {
2184 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002185 dst_release(dst_orig);
2186 } else {
2187 /* Flow passes untransformed */
2188 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002189 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002191ok:
2192 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002193 if (dst && dst->xfrm &&
2194 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2195 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002196 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Timo Teräs80c802f2010-04-07 00:30:05 +00002198nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002199 if (!(flags & XFRM_LOOKUP_ICMP)) {
2200 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002201 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002202 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002203 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002205 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002206dropdst:
2207 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002208 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002209 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211EXPORT_SYMBOL(xfrm_lookup);
2212
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002213static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002214xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002215{
2216 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002217
2218 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2219 return 0;
2220 x = skb->sp->xvec[idx];
2221 if (!x->type->reject)
2222 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002223 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002224}
2225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226/* When skb is transformed back to its "native" form, we have to
2227 * check policy restrictions. At the moment we make this in maximally
2228 * stupid way. Shame on me. :-) Of course, connected sockets must
2229 * have policy cached at them.
2230 */
2231
2232static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002233xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 unsigned short family)
2235{
2236 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002237 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return x->id.proto == tmpl->id.proto &&
2239 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2240 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2241 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002242 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002243 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002244 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2245 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002248/*
2249 * 0 or more than 0 is returned when validation is succeeded (either bypass
2250 * because of optional transport mode, or next index of the mathced secpath
2251 * state with the template.
2252 * -1 is returned when no matching template is found.
2253 * Otherwise "-2 - errored_index" is returned.
2254 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002256xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 unsigned short family)
2258{
2259 int idx = start;
2260
2261 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002262 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 return start;
2264 } else
2265 start = -1;
2266 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002267 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002269 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2270 if (start == -1)
2271 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
2275 return start;
2276}
2277
Herbert Xud5422ef2007-12-12 10:44:16 -08002278int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2279 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
2281 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002282 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284 if (unlikely(afinfo == NULL))
2285 return -EAFNOSUPPORT;
2286
Herbert Xud5422ef2007-12-12 10:44:16 -08002287 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002288 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002290 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
Herbert Xud5422ef2007-12-12 10:44:16 -08002292EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
David S. Miller9a7386e2011-02-24 01:44:12 -05002294static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
2296 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002297 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002298 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
2302
2303 return 0;
2304}
2305
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002306int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 unsigned short family)
2308{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002309 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002311 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2312 int npols = 0;
2313 int xfrm_nr;
2314 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002315 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002317 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002318 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Herbert Xud5422ef2007-12-12 10:44:16 -08002320 reverse = dir & ~XFRM_POLICY_MASK;
2321 dir &= XFRM_POLICY_MASK;
2322 fl_dir = policy_to_flow_dir(dir);
2323
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002324 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002325 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002327 }
2328
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002329 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
2331 /* First, check used SA against their selectors. */
2332 if (skb->sp) {
2333 int i;
2334
2335 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002336 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002337 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002338 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 }
2342 }
2343
2344 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002345 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002346 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002347 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002348 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002349 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002350 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002353 if (!pol) {
2354 struct flow_cache_object *flo;
2355
2356 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2357 xfrm_policy_lookup, NULL);
2358 if (IS_ERR_OR_NULL(flo))
2359 pol = ERR_CAST(flo);
2360 else
2361 pol = container_of(flo, struct xfrm_policy, flo);
2362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002364 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002365 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002366 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002367 }
James Morris134b0fc2006-10-05 15:42:27 -05002368
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002369 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002370 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002371 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002372 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002373 return 0;
2374 }
2375 return 1;
2376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
James Morris9d729f72007-03-04 16:12:44 -08002378 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002380 pols[0] = pol;
2381 npols ++;
2382#ifdef CONFIG_XFRM_SUB_POLICY
2383 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002384 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002385 &fl, family,
2386 XFRM_POLICY_IN);
2387 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002388 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002389 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002390 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002391 }
James Morris9d729f72007-03-04 16:12:44 -08002392 pols[1]->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002393 npols ++;
2394 }
2395 }
2396#endif
2397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (pol->action == XFRM_POLICY_ALLOW) {
2399 struct sec_path *sp;
2400 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002401 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002402 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002403 struct xfrm_tmpl **tpp = tp;
2404 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 int i, k;
2406
2407 if ((sp = skb->sp) == NULL)
2408 sp = &dummy;
2409
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002410 for (pi = 0; pi < npols; pi++) {
2411 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002412 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002413 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002414 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002415 }
2416 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002417 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002418 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002419 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002420 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2421 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2422 }
2423 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002424 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002425 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002426 tpp = stp;
2427 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 /* For each tunnel xfrm, find the first matching tmpl.
2430 * For each tmpl before that, find corresponding xfrm.
2431 * Order is _important_. Later we will implement
2432 * some barriers, but at the moment barriers
2433 * are implied between each two transformations.
2434 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002435 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2436 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002437 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002438 if (k < -1)
2439 /* "-2 - errored_index" returned */
2440 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002441 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
2445
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002446 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002447 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002451 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 return 1;
2453 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002454 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002457 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002458reject_error:
2459 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 return 0;
2461}
2462EXPORT_SYMBOL(__xfrm_policy_check);
2463
2464int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2465{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002466 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002468 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002469 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002471 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002472 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002476 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002477
David S. Miller452edd52011-03-02 13:27:41 -08002478 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, 0);
2479 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002480 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002481 dst = NULL;
2482 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002483 skb_dst_set(skb, dst);
2484 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
2486EXPORT_SYMBOL(__xfrm_route_forward);
2487
David S. Millerd49c73c2006-08-13 18:55:53 -07002488/* Optimize later using cookies and generation ids. */
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2491{
David S. Millerd49c73c2006-08-13 18:55:53 -07002492 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002493 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2494 * get validated by dst_ops->check on every use. We do this
2495 * because when a normal route referenced by an XFRM dst is
2496 * obsoleted we do not go looking around for all parent
2497 * referencing XFRM dsts so that we can invalidate them. It
2498 * is just too much work. Instead we make the checks here on
2499 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002500 *
2501 * XFRM dst A --> IPv4 dst X
2502 *
2503 * X is the "xdst->route" of A (X is also the "dst->path" of A
2504 * in this example). If X is marked obsolete, "A" will not
2505 * notice. That's what we are validating here via the
2506 * stale_bundle() check.
2507 *
2508 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002509 * dst which causes it's ->obsolete field to be set to
2510 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2511 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002512 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002513 if (dst->obsolete < 0 && !stale_bundle(dst))
2514 return dst;
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return NULL;
2517}
2518
2519static int stale_bundle(struct dst_entry *dst)
2520{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002521 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522}
2523
Herbert Xuaabc9762005-05-03 16:27:10 -07002524void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002527 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002528 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 dev_put(dev);
2530 }
2531}
Herbert Xuaabc9762005-05-03 16:27:10 -07002532EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534static void xfrm_link_failure(struct sk_buff *skb)
2535{
2536 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537}
2538
2539static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2540{
2541 if (dst) {
2542 if (dst->obsolete) {
2543 dst_release(dst);
2544 dst = NULL;
2545 }
2546 }
2547 return dst;
2548}
2549
Alexey Dobriyanddcfd792008-11-25 17:37:23 -08002550static void __xfrm_garbage_collect(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
Timo Teräs80c802f2010-04-07 00:30:05 +00002552 struct dst_entry *head, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Fan Du283bc9f2013-11-07 17:47:50 +08002554 spin_lock_bh(&net->xfrm.xfrm_policy_sk_bundle_lock);
Timo Teräs80c802f2010-04-07 00:30:05 +00002555 head = xfrm_policy_sk_bundles;
2556 xfrm_policy_sk_bundles = NULL;
Fan Du283bc9f2013-11-07 17:47:50 +08002557 spin_unlock_bh(&net->xfrm.xfrm_policy_sk_bundle_lock);
Timo Teräs80c802f2010-04-07 00:30:05 +00002558
2559 while (head) {
2560 next = head->next;
2561 dst_free(head);
2562 head = next;
2563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564}
2565
Paul Mooree4c17212013-05-29 07:36:25 +00002566void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002567{
2568 flow_cache_flush();
2569 __xfrm_garbage_collect(net);
2570}
Paul Mooree4c17212013-05-29 07:36:25 +00002571EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002572
2573static void xfrm_garbage_collect_deferred(struct net *net)
2574{
2575 flow_cache_flush_deferred();
2576 __xfrm_garbage_collect(net);
2577}
2578
Herbert Xu25ee3282007-12-11 09:32:34 -08002579static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
2581 do {
2582 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2583 u32 pmtu, route_mtu_cached;
2584
2585 pmtu = dst_mtu(dst->child);
2586 xdst->child_mtu_cached = pmtu;
2587
2588 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2589
2590 route_mtu_cached = dst_mtu(xdst->route);
2591 xdst->route_mtu_cached = route_mtu_cached;
2592
2593 if (pmtu > route_mtu_cached)
2594 pmtu = route_mtu_cached;
2595
David S. Millerdefb3512010-12-08 21:16:57 -08002596 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 } while ((dst = dst->next));
2598}
2599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600/* Check that the bundle accepts the flow and its components are
2601 * still valid.
2602 */
2603
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002604static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605{
2606 struct dst_entry *dst = &first->u.dst;
2607 struct xfrm_dst *last;
2608 u32 mtu;
2609
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002610 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 (dst->dev && !netif_running(dst->dev)))
2612 return 0;
2613
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002614 if (dst->flags & DST_XFRM_QUEUE)
2615 return 1;
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 last = NULL;
2618
2619 do {
2620 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2623 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002624 if (xdst->xfrm_genid != dst->xfrm->genid)
2625 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002626 if (xdst->num_pols > 0 &&
2627 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 mtu = dst_mtu(dst->child);
2631 if (xdst->child_mtu_cached != mtu) {
2632 last = xdst;
2633 xdst->child_mtu_cached = mtu;
2634 }
2635
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002636 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 return 0;
2638 mtu = dst_mtu(xdst->route);
2639 if (xdst->route_mtu_cached != mtu) {
2640 last = xdst;
2641 xdst->route_mtu_cached = mtu;
2642 }
2643
2644 dst = dst->child;
2645 } while (dst->xfrm);
2646
2647 if (likely(!last))
2648 return 1;
2649
2650 mtu = last->child_mtu_cached;
2651 for (;;) {
2652 dst = &last->u.dst;
2653
2654 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2655 if (mtu > last->route_mtu_cached)
2656 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002657 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
2659 if (last == first)
2660 break;
2661
Patrick McHardybd0bf072007-07-18 01:55:52 -07002662 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 last->child_mtu_cached = mtu;
2664 }
2665
2666 return 1;
2667}
2668
David S. Miller0dbaee32010-12-13 12:52:14 -08002669static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2670{
2671 return dst_metric_advmss(dst->path);
2672}
2673
Steffen Klassertebb762f2011-11-23 02:12:51 +00002674static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002675{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002676 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2677
2678 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002679}
2680
David S. Millerf894cbf2012-07-02 21:52:24 -07002681static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2682 struct sk_buff *skb,
2683 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002684{
David S. Millerf894cbf2012-07-02 21:52:24 -07002685 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002686}
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2689{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002690 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 int err = 0;
2692 if (unlikely(afinfo == NULL))
2693 return -EINVAL;
2694 if (unlikely(afinfo->family >= NPROTO))
2695 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002696 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2698 err = -ENOBUFS;
2699 else {
2700 struct dst_ops *dst_ops = afinfo->dst_ops;
2701 if (likely(dst_ops->kmem_cachep == NULL))
2702 dst_ops->kmem_cachep = xfrm_dst_cache;
2703 if (likely(dst_ops->check == NULL))
2704 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002705 if (likely(dst_ops->default_advmss == NULL))
2706 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002707 if (likely(dst_ops->mtu == NULL))
2708 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 if (likely(dst_ops->negative_advice == NULL))
2710 dst_ops->negative_advice = xfrm_negative_advice;
2711 if (likely(dst_ops->link_failure == NULL))
2712 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002713 if (likely(dst_ops->neigh_lookup == NULL))
2714 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002716 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002717 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002719 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002720
2721 rtnl_lock();
2722 for_each_net(net) {
2723 struct dst_ops *xfrm_dst_ops;
2724
2725 switch (afinfo->family) {
2726 case AF_INET:
2727 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2728 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002729#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002730 case AF_INET6:
2731 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2732 break;
2733#endif
2734 default:
2735 BUG();
2736 }
2737 *xfrm_dst_ops = *afinfo->dst_ops;
2738 }
2739 rtnl_unlock();
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return err;
2742}
2743EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2744
2745int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2746{
2747 int err = 0;
2748 if (unlikely(afinfo == NULL))
2749 return -EINVAL;
2750 if (unlikely(afinfo->family >= NPROTO))
2751 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002752 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2754 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2755 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002756 else
2757 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2758 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002760 spin_unlock(&xfrm_policy_afinfo_lock);
2761 if (!err) {
2762 struct dst_ops *dst_ops = afinfo->dst_ops;
2763
2764 synchronize_rcu();
2765
2766 dst_ops->kmem_cachep = NULL;
2767 dst_ops->check = NULL;
2768 dst_ops->negative_advice = NULL;
2769 dst_ops->link_failure = NULL;
2770 afinfo->garbage_collect = NULL;
2771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return err;
2773}
2774EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2775
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002776static void __net_init xfrm_dst_ops_init(struct net *net)
2777{
2778 struct xfrm_policy_afinfo *afinfo;
2779
Eric Dumazetef8531b2012-08-19 12:31:48 +02002780 rcu_read_lock();
2781 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002782 if (afinfo)
2783 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002784#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetef8531b2012-08-19 12:31:48 +02002785 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002786 if (afinfo)
2787 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2788#endif
Priyanka Jain418a99a2012-08-12 21:22:29 +00002789 rcu_read_unlock();
Herbert Xu546be242006-05-27 23:03:58 -07002790}
2791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2793{
Jiri Pirko351638e2013-05-28 01:30:21 +00002794 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002795
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 switch (event) {
2797 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002798 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 }
2800 return NOTIFY_DONE;
2801}
2802
2803static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002804 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805};
2806
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002807#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002808static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002809{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002810 int rv;
2811
Tejun Heo7d720c32010-02-16 15:20:26 +00002812 if (snmp_mib_init((void __percpu **)net->mib.xfrm_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00002813 sizeof(struct linux_xfrm_mib),
2814 __alignof__(struct linux_xfrm_mib)) < 0)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002815 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002816 rv = xfrm_proc_init(net);
2817 if (rv < 0)
Tejun Heo7d720c32010-02-16 15:20:26 +00002818 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002819 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002820}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002821
2822static void xfrm_statistics_fini(struct net *net)
2823{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002824 xfrm_proc_fini(net);
Tejun Heo7d720c32010-02-16 15:20:26 +00002825 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002826}
2827#else
2828static int __net_init xfrm_statistics_init(struct net *net)
2829{
2830 return 0;
2831}
2832
2833static void xfrm_statistics_fini(struct net *net)
2834{
2835}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002836#endif
2837
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002838static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
David S. Miller2518c7c2006-08-24 04:45:07 -07002840 unsigned int hmask, sz;
2841 int dir;
2842
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002843 if (net_eq(net, &init_net))
2844 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002846 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002847 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
David S. Miller2518c7c2006-08-24 04:45:07 -07002849 hmask = 8 - 1;
2850 sz = (hmask+1) * sizeof(struct hlist_head);
2851
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002852 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2853 if (!net->xfrm.policy_byidx)
2854 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002855 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002856
2857 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2858 struct xfrm_policy_hash *htab;
2859
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002860 net->xfrm.policy_count[dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002861 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002862
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002863 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002864 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002865 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002866 goto out_bydst;
2867 htab->hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002868 }
2869
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002870 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002871 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002872 if (net_eq(net, &init_net))
2873 register_netdevice_notifier(&xfrm_dev_notifier);
2874 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002875
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002876out_bydst:
2877 for (dir--; dir >= 0; dir--) {
2878 struct xfrm_policy_hash *htab;
2879
2880 htab = &net->xfrm.policy_bydst[dir];
2881 xfrm_hash_free(htab->table, sz);
2882 }
2883 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002884out_byidx:
2885 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886}
2887
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002888static void xfrm_policy_fini(struct net *net)
2889{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002890 struct xfrm_audit audit_info;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002891 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002892 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002893
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002894 flush_work(&net->xfrm.policy_hash_work);
2895#ifdef CONFIG_XFRM_SUB_POLICY
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002896 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002897 audit_info.sessionid = -1;
2898 audit_info.secid = 0;
2899 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, &audit_info);
2900#endif
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002901 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002902 audit_info.sessionid = -1;
2903 audit_info.secid = 0;
2904 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002905
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002906 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002907
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002908 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002909 struct xfrm_policy_hash *htab;
2910
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002911 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002912
2913 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01002914 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002915 WARN_ON(!hlist_empty(htab->table));
2916 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002917 }
2918
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002919 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002920 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2921 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002922}
2923
2924static int __net_init xfrm_net_init(struct net *net)
2925{
2926 int rv;
2927
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002928 rv = xfrm_statistics_init(net);
2929 if (rv < 0)
2930 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002931 rv = xfrm_state_init(net);
2932 if (rv < 0)
2933 goto out_state;
2934 rv = xfrm_policy_init(net);
2935 if (rv < 0)
2936 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002937 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002938 rv = xfrm_sysctl_init(net);
2939 if (rv < 0)
2940 goto out_sysctl;
Fan Du283bc9f2013-11-07 17:47:50 +08002941
2942 /* Initialize the per-net locks here */
2943 spin_lock_init(&net->xfrm.xfrm_state_lock);
2944 rwlock_init(&net->xfrm.xfrm_policy_lock);
2945 spin_lock_init(&net->xfrm.xfrm_policy_sk_bundle_lock);
2946 mutex_init(&net->xfrm.xfrm_cfg_mutex);
2947
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002948 return 0;
2949
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002950out_sysctl:
2951 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002952out_policy:
2953 xfrm_state_fini(net);
2954out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002955 xfrm_statistics_fini(net);
2956out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002957 return rv;
2958}
2959
2960static void __net_exit xfrm_net_exit(struct net *net)
2961{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002962 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002963 xfrm_policy_fini(net);
2964 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002965 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002966}
2967
2968static struct pernet_operations __net_initdata xfrm_net_ops = {
2969 .init = xfrm_net_init,
2970 .exit = xfrm_net_exit,
2971};
2972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973void __init xfrm_init(void)
2974{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002975 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 xfrm_input_init();
2977}
2978
Joy Lattenab5f5e82007-09-17 11:51:22 -07002979#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08002980static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2981 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002982{
Paul Moore875179f2007-12-01 23:27:18 +11002983 struct xfrm_sec_ctx *ctx = xp->security;
2984 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002985
Paul Moore875179f2007-12-01 23:27:18 +11002986 if (ctx)
2987 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2988 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2989
2990 switch(sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002991 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002992 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002993 if (sel->prefixlen_s != 32)
2994 audit_log_format(audit_buf, " src_prefixlen=%d",
2995 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002996 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002997 if (sel->prefixlen_d != 32)
2998 audit_log_format(audit_buf, " dst_prefixlen=%d",
2999 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003000 break;
3001 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003002 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003003 if (sel->prefixlen_s != 128)
3004 audit_log_format(audit_buf, " src_prefixlen=%d",
3005 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003006 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003007 if (sel->prefixlen_d != 128)
3008 audit_log_format(audit_buf, " dst_prefixlen=%d",
3009 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003010 break;
3011 }
3012}
3013
Paul Moore68277ac2007-12-20 20:49:33 -08003014void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07003015 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003016{
3017 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003018
Paul Mooreafeb14b2007-12-21 14:58:11 -08003019 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003020 if (audit_buf == NULL)
3021 return;
Eric Paris25323862008-04-18 10:09:25 -04003022 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003023 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003024 xfrm_audit_common_policyinfo(xp, audit_buf);
3025 audit_log_end(audit_buf);
3026}
3027EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3028
Paul Moore68277ac2007-12-20 20:49:33 -08003029void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07003030 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003031{
3032 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003033
Paul Mooreafeb14b2007-12-21 14:58:11 -08003034 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003035 if (audit_buf == NULL)
3036 return;
Eric Paris25323862008-04-18 10:09:25 -04003037 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003038 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003039 xfrm_audit_common_policyinfo(xp, audit_buf);
3040 audit_log_end(audit_buf);
3041}
3042EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3043#endif
3044
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003045#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003046static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3047 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003048{
3049 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3050 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003051 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3052 sel_cmp->family) &&
3053 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3054 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003055 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3056 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003057 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003058 }
3059 } else {
3060 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003061 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003062 }
3063 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003064 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003065}
3066
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003067static struct xfrm_policy * xfrm_migrate_policy_find(const struct xfrm_selector *sel,
Fan Du8d549c42013-11-07 17:47:49 +08003068 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003069{
3070 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003071 struct hlist_head *chain;
3072 u32 priority = ~0U;
3073
Fan Du283bc9f2013-11-07 17:47:50 +08003074 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
Fan Du8d549c42013-11-07 17:47:49 +08003075 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003076 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003077 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3078 pol->type == type) {
3079 ret = pol;
3080 priority = ret->priority;
3081 break;
3082 }
3083 }
Fan Du8d549c42013-11-07 17:47:49 +08003084 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003085 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003086 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3087 pol->type == type &&
3088 pol->priority < priority) {
3089 ret = pol;
3090 break;
3091 }
3092 }
3093
3094 if (ret)
3095 xfrm_pol_hold(ret);
3096
Fan Du283bc9f2013-11-07 17:47:50 +08003097 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003098
3099 return ret;
3100}
3101
David S. Millerdd701752011-02-24 00:21:08 -05003102static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003103{
3104 int match = 0;
3105
3106 if (t->mode == m->mode && t->id.proto == m->proto &&
3107 (m->reqid == 0 || t->reqid == m->reqid)) {
3108 switch (t->mode) {
3109 case XFRM_MODE_TUNNEL:
3110 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003111 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3112 m->old_family) &&
3113 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3114 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003115 match = 1;
3116 }
3117 break;
3118 case XFRM_MODE_TRANSPORT:
3119 /* in case of transport mode, template does not store
3120 any IP addresses, hence we just compare mode and
3121 protocol */
3122 match = 1;
3123 break;
3124 default:
3125 break;
3126 }
3127 }
3128 return match;
3129}
3130
3131/* update endpoint address(es) of template(s) */
3132static int xfrm_policy_migrate(struct xfrm_policy *pol,
3133 struct xfrm_migrate *m, int num_migrate)
3134{
3135 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003136 int i, j, n = 0;
3137
3138 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003139 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003140 /* target policy has been deleted */
3141 write_unlock_bh(&pol->lock);
3142 return -ENOENT;
3143 }
3144
3145 for (i = 0; i < pol->xfrm_nr; i++) {
3146 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3147 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3148 continue;
3149 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003150 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3151 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003152 continue;
3153 /* update endpoints */
3154 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3155 sizeof(pol->xfrm_vec[i].id.daddr));
3156 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3157 sizeof(pol->xfrm_vec[i].saddr));
3158 pol->xfrm_vec[i].encap_family = mp->new_family;
3159 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003160 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003161 }
3162 }
3163
3164 write_unlock_bh(&pol->lock);
3165
3166 if (!n)
3167 return -ENODATA;
3168
3169 return 0;
3170}
3171
David S. Millerdd701752011-02-24 00:21:08 -05003172static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003173{
3174 int i, j;
3175
3176 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3177 return -EINVAL;
3178
3179 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003180 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3181 m[i].old_family) &&
3182 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3183 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003184 return -EINVAL;
3185 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3186 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3187 return -EINVAL;
3188
3189 /* check if there is any duplicated entry */
3190 for (j = i + 1; j < num_migrate; j++) {
3191 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3192 sizeof(m[i].old_daddr)) &&
3193 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3194 sizeof(m[i].old_saddr)) &&
3195 m[i].proto == m[j].proto &&
3196 m[i].mode == m[j].mode &&
3197 m[i].reqid == m[j].reqid &&
3198 m[i].old_family == m[j].old_family)
3199 return -EINVAL;
3200 }
3201 }
3202
3203 return 0;
3204}
3205
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003206int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003207 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003208 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003209{
3210 int i, err, nx_cur = 0, nx_new = 0;
3211 struct xfrm_policy *pol = NULL;
3212 struct xfrm_state *x, *xc;
3213 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3214 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3215 struct xfrm_migrate *mp;
3216
3217 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3218 goto out;
3219
3220 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003221 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003222 err = -ENOENT;
3223 goto out;
3224 }
3225
3226 /* Stage 2 - find and update state(s) */
3227 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003228 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003229 x_cur[nx_cur] = x;
3230 nx_cur++;
3231 if ((xc = xfrm_state_migrate(x, mp))) {
3232 x_new[nx_new] = xc;
3233 nx_new++;
3234 } else {
3235 err = -ENODATA;
3236 goto restore_state;
3237 }
3238 }
3239 }
3240
3241 /* Stage 3 - update policy */
3242 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3243 goto restore_state;
3244
3245 /* Stage 4 - delete old state(s) */
3246 if (nx_cur) {
3247 xfrm_states_put(x_cur, nx_cur);
3248 xfrm_states_delete(x_cur, nx_cur);
3249 }
3250
3251 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003252 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003253
3254 xfrm_pol_put(pol);
3255
3256 return 0;
3257out:
3258 return err;
3259
3260restore_state:
3261 if (pol)
3262 xfrm_pol_put(pol);
3263 if (nx_cur)
3264 xfrm_states_put(x_cur, nx_cur);
3265 if (nx_new)
3266 xfrm_states_delete(x_new, nx_new);
3267
3268 return err;
3269}
David S. Millere610e672007-02-08 13:29:15 -08003270EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003271#endif