blob: fdde51f4271adf22b26e36c46f76c2b93c4ce56e [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
Steffen Klassertb8c203b2014-09-16 10:08:49 +020042struct xfrm_flo {
43 struct dst_entry *dst_orig;
44 u8 flags;
45};
46
Priyanka Jain418a99a2012-08-12 21:22:29 +000047static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
48static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO]
49 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Christoph Lametere18b8902006-12-06 20:33:20 -080051static struct kmem_cache *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Herbert Xu25ee3282007-12-11 09:32:34 -080053static void xfrm_init_pmtu(struct dst_entry *dst);
Timo Teräs80c802f2010-04-07 00:30:05 +000054static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +000055static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Steffen Klasserta0073fe2013-02-05 12:52:55 +010056static void xfrm_policy_queue_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Wei Yongjun29fa0b302008-12-03 00:33:09 -080058static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
59 int dir);
60
David S. Millerbc9b35a2012-05-15 15:04:57 -040061static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050062__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080063{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050064 const struct flowi4 *fl4 = &fl->u.ip4;
65
Alexey Dobriyan26bff942011-11-22 06:46:02 +000066 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
67 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -050068 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
69 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
70 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
71 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080072}
73
David S. Millerbc9b35a2012-05-15 15:04:57 -040074static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050075__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080076{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050077 const struct flowi6 *fl6 = &fl->u.ip6;
78
79 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
80 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
81 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
82 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
83 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
84 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080085}
86
David S. Millerbc9b35a2012-05-15 15:04:57 -040087bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
88 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -080089{
90 switch (family) {
91 case AF_INET:
92 return __xfrm4_selector_match(sel, fl);
93 case AF_INET6:
94 return __xfrm6_selector_match(sel, fl);
95 }
David S. Millerbc9b35a2012-05-15 15:04:57 -040096 return false;
Andrew Morton77681022006-11-08 22:46:26 -080097}
98
Eric Dumazetef8531b2012-08-19 12:31:48 +020099static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
100{
101 struct xfrm_policy_afinfo *afinfo;
102
103 if (unlikely(family >= NPROTO))
104 return NULL;
105 rcu_read_lock();
106 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
107 if (unlikely(!afinfo))
108 rcu_read_unlock();
109 return afinfo;
110}
111
112static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
113{
114 rcu_read_unlock();
115}
116
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800117static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos,
David S. Miller6418c4e2011-02-24 00:16:53 -0500118 const xfrm_address_t *saddr,
119 const xfrm_address_t *daddr,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900120 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800122 struct xfrm_policy_afinfo *afinfo;
123 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Herbert Xu25ee3282007-12-11 09:32:34 -0800125 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800127 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800129 dst = afinfo->dst_lookup(net, tos, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900132
133 return dst;
134}
135
136static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
137 xfrm_address_t *prev_saddr,
138 xfrm_address_t *prev_daddr,
139 int family)
140{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800141 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900142 xfrm_address_t *saddr = &x->props.saddr;
143 xfrm_address_t *daddr = &x->id.daddr;
144 struct dst_entry *dst;
145
146 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
147 saddr = x->coaddr;
148 daddr = prev_daddr;
149 }
150 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
151 saddr = prev_saddr;
152 daddr = x->coaddr;
153 }
154
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800155 dst = __xfrm_dst_lookup(net, tos, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900156
157 if (!IS_ERR(dst)) {
158 if (prev_saddr != saddr)
159 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
160 if (prev_daddr != daddr)
161 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
162 }
163
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800164 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static inline unsigned long make_jiffies(long secs)
168{
169 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
170 return MAX_SCHEDULE_TIMEOUT-1;
171 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900172 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175static void xfrm_policy_timer(unsigned long data)
176{
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800177 struct xfrm_policy *xp = (struct xfrm_policy *)data;
James Morris9d729f72007-03-04 16:12:44 -0800178 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 long next = LONG_MAX;
180 int warn = 0;
181 int dir;
182
183 read_lock(&xp->lock);
184
Timo Teräsea2dea92010-03-31 00:17:05 +0000185 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 goto out;
187
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700188 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 if (xp->lft.hard_add_expires_seconds) {
191 long tmo = xp->lft.hard_add_expires_seconds +
192 xp->curlft.add_time - now;
193 if (tmo <= 0)
194 goto expired;
195 if (tmo < next)
196 next = tmo;
197 }
198 if (xp->lft.hard_use_expires_seconds) {
199 long tmo = xp->lft.hard_use_expires_seconds +
200 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
201 if (tmo <= 0)
202 goto expired;
203 if (tmo < next)
204 next = tmo;
205 }
206 if (xp->lft.soft_add_expires_seconds) {
207 long tmo = xp->lft.soft_add_expires_seconds +
208 xp->curlft.add_time - now;
209 if (tmo <= 0) {
210 warn = 1;
211 tmo = XFRM_KM_TIMEOUT;
212 }
213 if (tmo < next)
214 next = tmo;
215 }
216 if (xp->lft.soft_use_expires_seconds) {
217 long tmo = xp->lft.soft_use_expires_seconds +
218 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
219 if (tmo <= 0) {
220 warn = 1;
221 tmo = XFRM_KM_TIMEOUT;
222 }
223 if (tmo < next)
224 next = tmo;
225 }
226
227 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800228 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (next != LONG_MAX &&
230 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
231 xfrm_pol_hold(xp);
232
233out:
234 read_unlock(&xp->lock);
235 xfrm_pol_put(xp);
236 return;
237
238expired:
239 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700240 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800241 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 xfrm_pol_put(xp);
243}
244
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000245static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
246{
247 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
248
249 if (unlikely(pol->walk.dead))
250 flo = NULL;
251 else
252 xfrm_pol_hold(pol);
253
254 return flo;
255}
256
257static int xfrm_policy_flo_check(struct flow_cache_object *flo)
258{
259 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
260
261 return !pol->walk.dead;
262}
263
264static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
265{
266 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
267}
268
269static const struct flow_cache_ops xfrm_policy_fc_ops = {
270 .get = xfrm_policy_flo_get,
271 .check = xfrm_policy_flo_check,
272 .delete = xfrm_policy_flo_delete,
273};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
276 * SPD calls.
277 */
278
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800279struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 struct xfrm_policy *policy;
282
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700283 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800286 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700287 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700288 INIT_HLIST_NODE(&policy->bydst);
289 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700291 atomic_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100292 skb_queue_head_init(&policy->polq.hold_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800293 setup_timer(&policy->timer, xfrm_policy_timer,
294 (unsigned long)policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100295 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
296 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000297 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299 return policy;
300}
301EXPORT_SYMBOL(xfrm_policy_alloc);
302
303/* Destroy xfrm_policy: descendant resources must be released to this moment. */
304
WANG Cong64c31b32008-01-07 22:34:29 -0800305void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Herbert Xu12a169e2008-10-01 07:03:24 -0700307 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Fan Du0659eea2013-08-01 18:08:36 +0800309 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 BUG();
311
Paul Moore03e1ad72008-04-12 19:07:52 -0700312 security_xfrm_policy_free(policy->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 kfree(policy);
314}
WANG Cong64c31b32008-01-07 22:34:29 -0800315EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100317static void xfrm_queue_purge(struct sk_buff_head *list)
318{
319 struct sk_buff *skb;
320
Steffen Klassert302a50b2013-08-28 08:47:14 +0200321 while ((skb = skb_dequeue(list)) != NULL)
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100322 kfree_skb(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/* Rule must be locked. Release descentant resources, announce
326 * entry dead. The rule must be unlinked from lists to the moment.
327 */
328
329static void xfrm_policy_kill(struct xfrm_policy *policy)
330{
Herbert Xu12a169e2008-10-01 07:03:24 -0700331 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Timo Teräs285ead12010-04-07 00:30:06 +0000333 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200335 if (del_timer(&policy->polq.hold_timer))
336 xfrm_pol_put(policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100337 xfrm_queue_purge(&policy->polq.hold_queue);
338
Timo Teräs285ead12010-04-07 00:30:06 +0000339 if (del_timer(&policy->timer))
340 xfrm_pol_put(policy);
341
342 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
David S. Miller2518c7c2006-08-24 04:45:07 -0700345static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
346
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800347static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700348{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800349 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700350}
351
David S. Miller5f803b52011-02-24 00:33:19 -0500352static struct hlist_head *policy_hash_bysel(struct net *net,
353 const struct xfrm_selector *sel,
354 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700355{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800356 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700357 unsigned int hash = __sel_hash(sel, family, hmask);
358
359 return (hash == hmask + 1 ?
Alexey Dobriyan11219942008-11-25 17:33:06 -0800360 &net->xfrm.policy_inexact[dir] :
361 net->xfrm.policy_bydst[dir].table + hash);
David S. Miller2518c7c2006-08-24 04:45:07 -0700362}
363
David S. Miller5f803b52011-02-24 00:33:19 -0500364static struct hlist_head *policy_hash_direct(struct net *net,
365 const xfrm_address_t *daddr,
366 const xfrm_address_t *saddr,
367 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700368{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800369 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700370 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
371
Alexey Dobriyan11219942008-11-25 17:33:06 -0800372 return net->xfrm.policy_bydst[dir].table + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700373}
374
David S. Miller2518c7c2006-08-24 04:45:07 -0700375static void xfrm_dst_hash_transfer(struct hlist_head *list,
376 struct hlist_head *ndsttable,
377 unsigned int nhashmask)
378{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800379 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700380 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800381 unsigned int h0 = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700382
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800383redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800384 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700385 unsigned int h;
386
387 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
388 pol->family, nhashmask);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800389 if (!entry0) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800390 hlist_del(&pol->bydst);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800391 hlist_add_head(&pol->bydst, ndsttable+h);
392 h0 = h;
393 } else {
394 if (h != h0)
395 continue;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800396 hlist_del(&pol->bydst);
Ken Helias1d023282014-08-06 16:09:16 -0700397 hlist_add_behind(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800398 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800399 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800400 }
401 if (!hlist_empty(list)) {
402 entry0 = NULL;
403 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700404 }
405}
406
407static void xfrm_idx_hash_transfer(struct hlist_head *list,
408 struct hlist_head *nidxtable,
409 unsigned int nhashmask)
410{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800411 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700412 struct xfrm_policy *pol;
413
Sasha Levinb67bfe02013-02-27 17:06:00 -0800414 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700415 unsigned int h;
416
417 h = __idx_hash(pol->index, nhashmask);
418 hlist_add_head(&pol->byidx, nidxtable+h);
419 }
420}
421
422static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
423{
424 return ((old_hmask + 1) << 1) - 1;
425}
426
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800427static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700428{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800429 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700430 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
431 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800432 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700433 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700434 int i;
435
436 if (!ndst)
437 return;
438
Fan Du283bc9f2013-11-07 17:47:50 +0800439 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700440
441 for (i = hmask; i >= 0; i--)
442 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
443
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800444 net->xfrm.policy_bydst[dir].table = ndst;
445 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700446
Fan Du283bc9f2013-11-07 17:47:50 +0800447 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700448
David S. Miller44e36b42006-08-24 04:50:50 -0700449 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700450}
451
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800452static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700453{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800454 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700455 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
456 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800457 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700458 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700459 int i;
460
461 if (!nidx)
462 return;
463
Fan Du283bc9f2013-11-07 17:47:50 +0800464 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700465
466 for (i = hmask; i >= 0; i--)
467 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
468
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800469 net->xfrm.policy_byidx = nidx;
470 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700471
Fan Du283bc9f2013-11-07 17:47:50 +0800472 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700473
David S. Miller44e36b42006-08-24 04:50:50 -0700474 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700475}
476
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800477static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700478{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800479 unsigned int cnt = net->xfrm.policy_count[dir];
480 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700481
482 if (total)
483 *total += cnt;
484
485 if ((hmask + 1) < xfrm_policy_hashmax &&
486 cnt > hmask)
487 return 1;
488
489 return 0;
490}
491
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800492static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700493{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800494 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700495
496 if ((hmask + 1) < xfrm_policy_hashmax &&
497 total > hmask)
498 return 1;
499
500 return 0;
501}
502
Alexey Dobriyane0710412010-01-23 13:37:10 +0000503void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700504{
Fan Du283bc9f2013-11-07 17:47:50 +0800505 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000506 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
507 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
508 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
509 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
510 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
511 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
512 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700513 si->spdhmcnt = xfrm_policy_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800514 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700515}
516EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700517
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700518static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800519static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700520{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800521 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700522 int dir, total;
523
524 mutex_lock(&hash_resize_mutex);
525
526 total = 0;
527 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800528 if (xfrm_bydst_should_resize(net, dir, &total))
529 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700530 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800531 if (xfrm_byidx_should_resize(net, total))
532 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700533
534 mutex_unlock(&hash_resize_mutex);
535}
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/* Generate new index... KAME seems to generate them ordered by cost
538 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf2013-11-07 17:47:48 +0800539static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 static u32 idx_generator;
542
543 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700544 struct hlist_head *list;
545 struct xfrm_policy *p;
546 u32 idx;
547 int found;
548
Fan Due682adf2013-11-07 17:47:48 +0800549 if (!index) {
550 idx = (idx_generator | dir);
551 idx_generator += 8;
552 } else {
553 idx = index;
554 index = 0;
555 }
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (idx == 0)
558 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800559 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700560 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800561 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700562 if (p->index == idx) {
563 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700567 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return idx;
569 }
570}
571
David S. Miller2518c7c2006-08-24 04:45:07 -0700572static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
573{
574 u32 *p1 = (u32 *) s1;
575 u32 *p2 = (u32 *) s2;
576 int len = sizeof(struct xfrm_selector) / sizeof(u32);
577 int i;
578
579 for (i = 0; i < len; i++) {
580 if (p1[i] != p2[i])
581 return 1;
582 }
583
584 return 0;
585}
586
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100587static void xfrm_policy_requeue(struct xfrm_policy *old,
588 struct xfrm_policy *new)
589{
590 struct xfrm_policy_queue *pq = &old->polq;
591 struct sk_buff_head list;
592
593 __skb_queue_head_init(&list);
594
595 spin_lock_bh(&pq->hold_queue.lock);
596 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200597 if (del_timer(&pq->hold_timer))
598 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100599 spin_unlock_bh(&pq->hold_queue.lock);
600
601 if (skb_queue_empty(&list))
602 return;
603
604 pq = &new->polq;
605
606 spin_lock_bh(&pq->hold_queue.lock);
607 skb_queue_splice(&list, &pq->hold_queue);
608 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200609 if (!mod_timer(&pq->hold_timer, jiffies))
610 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100611 spin_unlock_bh(&pq->hold_queue.lock);
612}
613
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100614static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
615 struct xfrm_policy *pol)
616{
617 u32 mark = policy->mark.v & policy->mark.m;
618
619 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
620 return true;
621
622 if ((mark & pol->mark.m) == pol->mark.v &&
623 policy->priority == pol->priority)
624 return true;
625
626 return false;
627}
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
630{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800631 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700632 struct xfrm_policy *pol;
633 struct xfrm_policy *delpol;
634 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800635 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Fan Du283bc9f2013-11-07 17:47:50 +0800637 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800638 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700639 delpol = NULL;
640 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800641 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800642 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700643 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100644 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800645 xfrm_sec_ctx_match(pol->security, policy->security) &&
646 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (excl) {
Fan Du283bc9f2013-11-07 17:47:50 +0800648 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return -EEXIST;
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 delpol = pol;
652 if (policy->priority > pol->priority)
653 continue;
654 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800655 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 continue;
657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (delpol)
659 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661 if (newpos)
Ken Helias1d023282014-08-06 16:09:16 -0700662 hlist_add_behind(&policy->bydst, newpos);
David S. Miller2518c7c2006-08-24 04:45:07 -0700663 else
664 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800666 net->xfrm.policy_count[dir]++;
Fan Duca925cf2014-01-18 09:55:27 +0800667 atomic_inc(&net->xfrm.flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800668
669 /* After previous checking, family can either be AF_INET or AF_INET6 */
670 if (policy->family == AF_INET)
671 rt_genid_bump_ipv4(net);
672 else
673 rt_genid_bump_ipv6(net);
674
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100675 if (delpol) {
676 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800677 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100678 }
Fan Due682adf2013-11-07 17:47:48 +0800679 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800680 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800681 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 policy->curlft.use_time = 0;
683 if (!mod_timer(&policy->timer, jiffies + HZ))
684 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800685 list_add(&policy->walk.all, &net->xfrm.policy_all);
Fan Du283bc9f2013-11-07 17:47:50 +0800686 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
David S. Miller9b78a822005-12-22 07:39:48 -0800688 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800690 else if (xfrm_bydst_should_resize(net, dir, NULL))
691 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return 0;
694}
695EXPORT_SYMBOL(xfrm_policy_insert);
696
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000697struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
698 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800699 struct xfrm_sec_ctx *ctx, int delete,
700 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
David S. Miller2518c7c2006-08-24 04:45:07 -0700702 struct xfrm_policy *pol, *ret;
703 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Eric Parisef41aaa2007-03-07 15:37:58 -0800705 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800706 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800707 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700708 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800709 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700710 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000711 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700712 !selector_cmp(sel, &pol->selector) &&
713 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700715 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700716 *err = security_xfrm_policy_delete(
717 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800718 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800719 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800720 return pol;
721 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800722 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700723 }
724 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 break;
726 }
727 }
Fan Du283bc9f2013-11-07 17:47:50 +0800728 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000730 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700731 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700732 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
Trent Jaegerdf718372005-12-13 23:12:27 -0800734EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000736struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
737 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
David S. Miller2518c7c2006-08-24 04:45:07 -0700739 struct xfrm_policy *pol, *ret;
740 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Herbert Xub5505c62007-05-14 02:15:47 -0700742 *err = -ENOENT;
743 if (xfrm_policy_id2dir(id) != dir)
744 return NULL;
745
Eric Parisef41aaa2007-03-07 15:37:58 -0800746 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800747 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800748 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700749 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800750 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000751 if (pol->type == type && pol->index == id &&
752 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700754 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700755 *err = security_xfrm_policy_delete(
756 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800757 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800758 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800759 return pol;
760 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800761 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700762 }
763 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 break;
765 }
766 }
Fan Du283bc9f2013-11-07 17:47:50 +0800767 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000769 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700770 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700771 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773EXPORT_SYMBOL(xfrm_policy_byid);
774
Joy Latten4aa2e622007-06-04 19:05:57 -0400775#ifdef CONFIG_SECURITY_NETWORK_XFRM
776static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900777xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Joy Latten4aa2e622007-06-04 19:05:57 -0400779 int dir, err = 0;
780
781 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
782 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400783 int i;
784
Sasha Levinb67bfe02013-02-27 17:06:00 -0800785 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800786 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400787 if (pol->type != type)
788 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700789 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400790 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900791 xfrm_audit_policy_delete(pol, 0, task_valid);
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,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900805 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400806 return err;
807 }
808 }
809 }
810 }
811 return err;
812}
813#else
814static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900815xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400816{
817 return 0;
818}
819#endif
820
Tetsuo Handa2e710292014-04-22 21:48:30 +0900821int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400822{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000823 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Fan Du283bc9f2013-11-07 17:47:50 +0800825 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400826
Tetsuo Handa2e710292014-04-22 21:48:30 +0900827 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400828 if (err)
829 goto out;
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700832 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800833 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700834
835 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800836 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800837 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700838 if (pol->type != type)
839 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000840 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800841 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000842 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Tetsuo Handa2e710292014-04-22 21:48:30 +0900844 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -0600845
David S. Miller2518c7c2006-08-24 04:45:07 -0700846 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Fan Du283bc9f2013-11-07 17:47:50 +0800848 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700849 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700851
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800852 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700853 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800854 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800855 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700856 bydst) {
857 if (pol->type != type)
858 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000859 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800860 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000861 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700862
Tetsuo Handa2e710292014-04-22 21:48:30 +0900863 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700864 xfrm_policy_kill(pol);
865
Fan Du283bc9f2013-11-07 17:47:50 +0800866 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700867 goto again2;
868 }
869 }
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000872 if (!cnt)
873 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400874out:
Fan Du283bc9f2013-11-07 17:47:50 +0800875 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400876 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878EXPORT_SYMBOL(xfrm_policy_flush);
879
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800880int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800881 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 void *data)
883{
Herbert Xu12a169e2008-10-01 07:03:24 -0700884 struct xfrm_policy *pol;
885 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -0800886 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Timo Teras4c563f72008-02-28 21:31:08 -0800888 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
889 walk->type != XFRM_POLICY_TYPE_ANY)
890 return -EINVAL;
891
Herbert Xu12a169e2008-10-01 07:03:24 -0700892 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -0800893 return 0;
894
Fan Du283bc9f2013-11-07 17:47:50 +0800895 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700896 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800897 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -0700898 else
899 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800900 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -0700901 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -0800902 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -0700903 pol = container_of(x, struct xfrm_policy, walk);
904 if (walk->type != XFRM_POLICY_TYPE_ANY &&
905 walk->type != pol->type)
906 continue;
907 error = func(pol, xfrm_policy_id2dir(pol->index),
908 walk->seq, data);
909 if (error) {
910 list_move_tail(&walk->walk.all, &x->all);
911 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -0800912 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700913 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700915 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800916 error = -ENOENT;
917 goto out;
918 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700919 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920out:
Fan Du283bc9f2013-11-07 17:47:50 +0800921 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return error;
923}
924EXPORT_SYMBOL(xfrm_policy_walk);
925
Herbert Xu12a169e2008-10-01 07:03:24 -0700926void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
927{
928 INIT_LIST_HEAD(&walk->walk.all);
929 walk->walk.dead = 1;
930 walk->type = type;
931 walk->seq = 0;
932}
933EXPORT_SYMBOL(xfrm_policy_walk_init);
934
Fan Du283bc9f2013-11-07 17:47:50 +0800935void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -0700936{
937 if (list_empty(&walk->walk.all))
938 return;
939
Fan Du283bc9f2013-11-07 17:47:50 +0800940 write_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -0700941 list_del(&walk->walk.all);
Fan Du283bc9f2013-11-07 17:47:50 +0800942 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700943}
944EXPORT_SYMBOL(xfrm_policy_walk_done);
945
James Morris134b0fc2006-10-05 15:42:27 -0500946/*
947 * Find policy to apply to this flow.
948 *
949 * Returns 0 if policy found, else an -errno.
950 */
David S. Millerf299d552011-02-24 01:23:30 -0500951static int xfrm_policy_match(const struct xfrm_policy *pol,
952 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -0700953 u8 type, u16 family, int dir)
954{
David S. Millerf299d552011-02-24 01:23:30 -0500955 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -0400956 int ret = -ESRCH;
957 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -0700958
959 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -0500960 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -0700961 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500962 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700963
964 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500965 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -0500966 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -0700967 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700968
James Morris134b0fc2006-10-05 15:42:27 -0500969 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700970}
971
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800972static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -0800973 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700974 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
James Morris134b0fc2006-10-05 15:42:27 -0500976 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700977 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -0500978 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -0700979 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700980 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700981
982 daddr = xfrm_flowi_daddr(fl, family);
983 saddr = xfrm_flowi_saddr(fl, family);
984 if (unlikely(!daddr || !saddr))
985 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Fan Du283bc9f2013-11-07 17:47:50 +0800987 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800988 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700989 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800990 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500991 err = xfrm_policy_match(pol, fl, type, family, dir);
992 if (err) {
993 if (err == -ESRCH)
994 continue;
995 else {
996 ret = ERR_PTR(err);
997 goto fail;
998 }
999 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001000 ret = pol;
1001 priority = ret->priority;
1002 break;
1003 }
1004 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001005 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001006 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001007 err = xfrm_policy_match(pol, fl, type, family, dir);
1008 if (err) {
1009 if (err == -ESRCH)
1010 continue;
1011 else {
1012 ret = ERR_PTR(err);
1013 goto fail;
1014 }
1015 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001016 ret = pol;
1017 break;
1018 }
1019 }
David S. Milleracba48e2006-08-25 15:46:46 -07001020 if (ret)
1021 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -05001022fail:
Fan Du283bc9f2013-11-07 17:47:50 +08001023 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001024
David S. Miller2518c7c2006-08-24 04:45:07 -07001025 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001026}
1027
Timo Teräs80c802f2010-04-07 00:30:05 +00001028static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001029__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001030{
1031#ifdef CONFIG_XFRM_SUB_POLICY
1032 struct xfrm_policy *pol;
1033
1034 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1035 if (pol != NULL)
1036 return pol;
1037#endif
1038 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1039}
1040
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001041static int flow_to_policy_dir(int dir)
1042{
1043 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1044 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1045 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1046 return dir;
1047
1048 switch (dir) {
1049 default:
1050 case FLOW_DIR_IN:
1051 return XFRM_POLICY_IN;
1052 case FLOW_DIR_OUT:
1053 return XFRM_POLICY_OUT;
1054 case FLOW_DIR_FWD:
1055 return XFRM_POLICY_FWD;
1056 }
1057}
1058
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001059static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001060xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001061 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001062{
1063 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001064
1065 if (old_obj)
1066 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001067
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001068 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001069 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001070 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001071
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001072 /* Resolver returns two references:
1073 * one for cache and one for caller of flow_cache_lookup() */
1074 xfrm_pol_hold(pol);
1075
1076 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
Trent Jaegerdf718372005-12-13 23:12:27 -08001079static inline int policy_to_flow_dir(int dir)
1080{
1081 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001082 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1083 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1084 return dir;
1085 switch (dir) {
1086 default:
1087 case XFRM_POLICY_IN:
1088 return FLOW_DIR_IN;
1089 case XFRM_POLICY_OUT:
1090 return FLOW_DIR_OUT;
1091 case XFRM_POLICY_FWD:
1092 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001093 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001094}
1095
David S. Millerdee9f4b2011-02-22 18:44:31 -08001096static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir,
1097 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
1099 struct xfrm_policy *pol;
Fan Du283bc9f2013-11-07 17:47:50 +08001100 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Fan Du283bc9f2013-11-07 17:47:50 +08001102 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if ((pol = sk->sk_policy[dir]) != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001104 bool match = xfrm_selector_match(&pol->selector, fl,
1105 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001106 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001107
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001108 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001109 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1110 pol = NULL;
1111 goto out;
1112 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001113 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001114 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001115 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001116 if (!err)
1117 xfrm_pol_hold(pol);
1118 else if (err == -ESRCH)
1119 pol = NULL;
1120 else
1121 pol = ERR_PTR(err);
1122 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 pol = NULL;
1124 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001125out:
Fan Du283bc9f2013-11-07 17:47:50 +08001126 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return pol;
1128}
1129
1130static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1131{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001132 struct net *net = xp_net(pol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001133 struct hlist_head *chain = policy_hash_bysel(net, &pol->selector,
David S. Miller2518c7c2006-08-24 04:45:07 -07001134 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001135
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001136 list_add(&pol->walk.all, &net->xfrm.policy_all);
David S. Miller2518c7c2006-08-24 04:45:07 -07001137 hlist_add_head(&pol->bydst, chain);
Alexey Dobriyane92303f2008-11-25 17:32:41 -08001138 hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001139 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001141
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001142 if (xfrm_bydst_should_resize(net, dir, NULL))
1143 schedule_work(&net->xfrm.policy_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1147 int dir)
1148{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001149 struct net *net = xp_net(pol);
1150
David S. Miller2518c7c2006-08-24 04:45:07 -07001151 if (hlist_unhashed(&pol->bydst))
1152 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Steffen Klassert3a9016f2014-02-19 10:07:34 +01001154 hlist_del_init(&pol->bydst);
David S. Miller2518c7c2006-08-24 04:45:07 -07001155 hlist_del(&pol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -07001156 list_del(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001157 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001158
1159 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
Herbert Xu4666faa2005-06-18 22:43:22 -07001162int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Fan Du283bc9f2013-11-07 17:47:50 +08001164 struct net *net = xp_net(pol);
1165
1166 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 pol = __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001168 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001173 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
David S. Millera70fcb02006-03-20 19:18:52 -08001175EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1178{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001179 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 struct xfrm_policy *old_pol;
1181
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001182#ifdef CONFIG_XFRM_SUB_POLICY
1183 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1184 return -EINVAL;
1185#endif
1186
Fan Du283bc9f2013-11-07 17:47:50 +08001187 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 old_pol = sk->sk_policy[dir];
1189 sk->sk_policy[dir] = pol;
1190 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001191 pol->curlft.add_time = get_seconds();
Fan Due682adf2013-11-07 17:47:48 +08001192 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1194 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001195 if (old_pol) {
1196 if (pol)
1197 xfrm_policy_requeue(old_pol, pol);
1198
Timo Teräsea2dea92010-03-31 00:17:05 +00001199 /* Unlinking succeeds always. This is the only function
1200 * allowed to delete or replace socket policy.
1201 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001203 }
Fan Du283bc9f2013-11-07 17:47:50 +08001204 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 if (old_pol) {
1207 xfrm_policy_kill(old_pol);
1208 }
1209 return 0;
1210}
1211
David S. Millerd3e40a92011-02-24 01:25:41 -05001212static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001214 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001215 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 if (newp) {
1218 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001219 if (security_xfrm_policy_clone(old->security,
1220 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001221 kfree(newp);
1222 return NULL; /* ENOMEM */
1223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 newp->lft = old->lft;
1225 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001226 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 newp->action = old->action;
1228 newp->flags = old->flags;
1229 newp->xfrm_nr = old->xfrm_nr;
1230 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001231 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 memcpy(newp->xfrm_vec, old->xfrm_vec,
1233 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Fan Du283bc9f2013-11-07 17:47:50 +08001234 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001236 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 xfrm_pol_put(newp);
1238 }
1239 return newp;
1240}
1241
1242int __xfrm_sk_clone_policy(struct sock *sk)
1243{
1244 struct xfrm_policy *p0 = sk->sk_policy[0],
1245 *p1 = sk->sk_policy[1];
1246
1247 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1248 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1249 return -ENOMEM;
1250 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1251 return -ENOMEM;
1252 return 0;
1253}
1254
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001255static int
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001256xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001257 unsigned short family)
1258{
1259 int err;
1260 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1261
1262 if (unlikely(afinfo == NULL))
1263 return -EINVAL;
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001264 err = afinfo->get_saddr(net, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001265 xfrm_policy_put_afinfo(afinfo);
1266 return err;
1267}
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269/* Resolve list of templates for the flow, given policy. */
1270
1271static int
David S. Millera6c2e612011-02-22 18:35:39 -08001272xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1273 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001275 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 int nx;
1277 int i, error;
1278 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1279 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001280 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Weilong Chen9b7a7872013-12-24 09:43:46 +08001282 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 struct xfrm_state *x;
1284 xfrm_address_t *remote = daddr;
1285 xfrm_address_t *local = saddr;
1286 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1287
Joakim Koskela48b8d782007-07-26 00:08:42 -07001288 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1289 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 remote = &tmpl->id.daddr;
1291 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001292 if (xfrm_addr_any(local, tmpl->encap_family)) {
1293 error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001294 if (error)
1295 goto fail;
1296 local = &tmp;
1297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299
1300 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1301
1302 if (x && x->km.state == XFRM_STATE_VALID) {
1303 xfrm[nx++] = x;
1304 daddr = remote;
1305 saddr = local;
1306 continue;
1307 }
1308 if (x) {
1309 error = (x->km.state == XFRM_STATE_ERROR ?
1310 -EINVAL : -EAGAIN);
1311 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001312 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001313 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 if (!tmpl->optional)
1317 goto fail;
1318 }
1319 return nx;
1320
1321fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001322 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 xfrm_state_put(xfrm[nx]);
1324 return error;
1325}
1326
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001327static int
David S. Millera6c2e612011-02-22 18:35:39 -08001328xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1329 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001330{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001331 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1332 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001333 int cnx = 0;
1334 int error;
1335 int ret;
1336 int i;
1337
1338 for (i = 0; i < npols; i++) {
1339 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1340 error = -ENOBUFS;
1341 goto fail;
1342 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001343
1344 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001345 if (ret < 0) {
1346 error = ret;
1347 goto fail;
1348 } else
1349 cnx += ret;
1350 }
1351
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001352 /* found states are sorted for outbound processing */
1353 if (npols > 1)
1354 xfrm_state_sort(xfrm, tpp, cnx, family);
1355
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001356 return cnx;
1357
1358 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001359 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001360 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001361 return error;
1362
1363}
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365/* Check that the bundle accepts the flow and its components are
1366 * still valid.
1367 */
1368
David S. Miller05d84022011-02-22 17:47:10 -08001369static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001370{
1371 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1372 int tos;
1373
1374 if (!afinfo)
1375 return -EINVAL;
1376
1377 tos = afinfo->get_tos(fl);
1378
1379 xfrm_policy_put_afinfo(afinfo);
1380
1381 return tos;
1382}
1383
Timo Teräs80c802f2010-04-07 00:30:05 +00001384static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1385{
1386 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1387 struct dst_entry *dst = &xdst->u.dst;
1388
1389 if (xdst->route == NULL) {
1390 /* Dummy bundle - if it has xfrms we were not
1391 * able to build bundle as template resolution failed.
1392 * It means we need to try again resolving. */
1393 if (xdst->num_xfrms > 0)
1394 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001395 } else if (dst->flags & DST_XFRM_QUEUE) {
1396 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001397 } else {
1398 /* Real bundle */
1399 if (stale_bundle(dst))
1400 return NULL;
1401 }
1402
1403 dst_hold(dst);
1404 return flo;
1405}
1406
1407static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1408{
1409 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1410 struct dst_entry *dst = &xdst->u.dst;
1411
1412 if (!xdst->route)
1413 return 0;
1414 if (stale_bundle(dst))
1415 return 0;
1416
1417 return 1;
1418}
1419
1420static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1421{
1422 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1423 struct dst_entry *dst = &xdst->u.dst;
1424
1425 dst_free(dst);
1426}
1427
1428static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1429 .get = xfrm_bundle_flo_get,
1430 .check = xfrm_bundle_flo_check,
1431 .delete = xfrm_bundle_flo_delete,
1432};
1433
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001434static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001435{
1436 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001437 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001438 struct xfrm_dst *xdst;
1439
1440 if (!afinfo)
1441 return ERR_PTR(-EINVAL);
1442
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001443 switch (family) {
1444 case AF_INET:
1445 dst_ops = &net->xfrm.xfrm4_dst_ops;
1446 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001447#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001448 case AF_INET6:
1449 dst_ops = &net->xfrm.xfrm6_dst_ops;
1450 break;
1451#endif
1452 default:
1453 BUG();
1454 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001455 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001456
Madalin Bucurd4cae562011-09-26 07:04:36 +00001457 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001458 struct dst_entry *dst = &xdst->u.dst;
1459
1460 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001461 xdst->flo.ops = &xfrm_bundle_fc_ops;
Patrick McHardy9d7b0fc2012-08-20 02:56:56 -07001462 if (afinfo->init_dst)
1463 afinfo->init_dst(net, xdst);
Madalin Bucurd4cae562011-09-26 07:04:36 +00001464 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001465 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001466
Madalin Bucurd4cae562011-09-26 07:04:36 +00001467 xfrm_policy_put_afinfo(afinfo);
1468
Herbert Xu25ee3282007-12-11 09:32:34 -08001469 return xdst;
1470}
1471
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001472static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1473 int nfheader_len)
1474{
1475 struct xfrm_policy_afinfo *afinfo =
1476 xfrm_policy_get_afinfo(dst->ops->family);
1477 int err;
1478
1479 if (!afinfo)
1480 return -EINVAL;
1481
1482 err = afinfo->init_path(path, dst, nfheader_len);
1483
1484 xfrm_policy_put_afinfo(afinfo);
1485
1486 return err;
1487}
1488
Herbert Xu87c1e122010-03-02 02:51:56 +00001489static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001490 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001491{
1492 struct xfrm_policy_afinfo *afinfo =
1493 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1494 int err;
1495
1496 if (!afinfo)
1497 return -EINVAL;
1498
Herbert Xu87c1e122010-03-02 02:51:56 +00001499 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001500
1501 xfrm_policy_put_afinfo(afinfo);
1502
1503 return err;
1504}
1505
Timo Teräs80c802f2010-04-07 00:30:05 +00001506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1508 * all the metrics... Shortly, bundle a bundle.
1509 */
1510
Herbert Xu25ee3282007-12-11 09:32:34 -08001511static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1512 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001513 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001514 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001516 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001517 unsigned long now = jiffies;
1518 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001519 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001520 struct dst_entry *dst_prev = NULL;
1521 struct dst_entry *dst0 = NULL;
1522 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001524 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001525 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001526 int trailer_len = 0;
1527 int tos;
1528 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001529 xfrm_address_t saddr, daddr;
1530
1531 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001532
1533 tos = xfrm_get_tos(fl, family);
1534 err = tos;
1535 if (tos < 0)
1536 goto put_states;
1537
1538 dst_hold(dst);
1539
1540 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001541 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001542 struct dst_entry *dst1 = &xdst->u.dst;
1543
1544 err = PTR_ERR(xdst);
1545 if (IS_ERR(xdst)) {
1546 dst_release(dst);
1547 goto put_states;
1548 }
1549
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001550 if (xfrm[i]->sel.family == AF_UNSPEC) {
1551 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1552 xfrm_af2proto(family));
1553 if (!inner_mode) {
1554 err = -EAFNOSUPPORT;
1555 dst_release(dst);
1556 goto put_states;
1557 }
1558 } else
1559 inner_mode = xfrm[i]->inner_mode;
1560
Herbert Xu25ee3282007-12-11 09:32:34 -08001561 if (!dst_prev)
1562 dst0 = dst1;
1563 else {
1564 dst_prev->child = dst_clone(dst1);
1565 dst1->flags |= DST_NOHASH;
1566 }
1567
1568 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001569 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001570
1571 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1572 family = xfrm[i]->props.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001573 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1574 family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001575 err = PTR_ERR(dst);
1576 if (IS_ERR(dst))
1577 goto put_states;
1578 } else
1579 dst_hold(dst);
1580
1581 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001582 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001583
David S. Millerf5b0a872012-07-19 12:31:33 -07001584 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001585 dst1->flags |= DST_HOST;
1586 dst1->lastuse = now;
1587
1588 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001589 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001590
1591 dst1->next = dst_prev;
1592 dst_prev = dst1;
1593
1594 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001595 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1596 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001597 trailer_len += xfrm[i]->props.trailer_len;
1598 }
1599
1600 dst_prev->child = dst;
1601 dst0->path = dst;
1602
1603 err = -ENODEV;
1604 dev = dst->dev;
1605 if (!dev)
1606 goto free_dst;
1607
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001608 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001609 xfrm_init_pmtu(dst_prev);
1610
1611 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1612 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1613
Herbert Xu87c1e122010-03-02 02:51:56 +00001614 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001615 if (err)
1616 goto free_dst;
1617
1618 dst_prev->header_len = header_len;
1619 dst_prev->trailer_len = trailer_len;
1620 header_len -= xdst->u.dst.xfrm->props.header_len;
1621 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1622 }
1623
1624out:
1625 return dst0;
1626
1627put_states:
1628 for (; i < nx; i++)
1629 xfrm_state_put(xfrm[i]);
1630free_dst:
1631 if (dst0)
1632 dst_free(dst0);
1633 dst0 = ERR_PTR(err);
1634 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
Ying Xueda7c2242014-01-08 10:26:39 +08001637#ifdef CONFIG_XFRM_SUB_POLICY
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001638static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001639{
1640 if (!*target) {
1641 *target = kmalloc(size, GFP_ATOMIC);
1642 if (!*target)
1643 return -ENOMEM;
1644 }
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001645
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001646 memcpy(*target, src, size);
1647 return 0;
1648}
Ying Xueda7c2242014-01-08 10:26:39 +08001649#endif
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001650
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001651static int xfrm_dst_update_parent(struct dst_entry *dst,
1652 const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001653{
1654#ifdef CONFIG_XFRM_SUB_POLICY
1655 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1656 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1657 sel, sizeof(*sel));
1658#else
1659 return 0;
1660#endif
1661}
1662
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001663static int xfrm_dst_update_origin(struct dst_entry *dst,
1664 const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001665{
1666#ifdef CONFIG_XFRM_SUB_POLICY
1667 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1668 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1669#else
1670 return 0;
1671#endif
1672}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
David S. Miller73ff93c2011-02-22 18:33:42 -08001674static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001675 struct xfrm_policy **pols,
1676 int *num_pols, int *num_xfrms)
1677{
1678 int i;
1679
1680 if (*num_pols == 0 || !pols[0]) {
1681 *num_pols = 0;
1682 *num_xfrms = 0;
1683 return 0;
1684 }
1685 if (IS_ERR(pols[0]))
1686 return PTR_ERR(pols[0]);
1687
1688 *num_xfrms = pols[0]->xfrm_nr;
1689
1690#ifdef CONFIG_XFRM_SUB_POLICY
1691 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1692 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1693 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1694 XFRM_POLICY_TYPE_MAIN,
1695 fl, family,
1696 XFRM_POLICY_OUT);
1697 if (pols[1]) {
1698 if (IS_ERR(pols[1])) {
1699 xfrm_pols_put(pols, *num_pols);
1700 return PTR_ERR(pols[1]);
1701 }
Weilong Chen02d08922013-12-24 09:43:48 +08001702 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001703 (*num_xfrms) += pols[1]->xfrm_nr;
1704 }
1705 }
1706#endif
1707 for (i = 0; i < *num_pols; i++) {
1708 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1709 *num_xfrms = -1;
1710 break;
1711 }
1712 }
1713
1714 return 0;
1715
1716}
1717
1718static struct xfrm_dst *
1719xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001720 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001721 struct dst_entry *dst_orig)
1722{
1723 struct net *net = xp_net(pols[0]);
1724 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1725 struct dst_entry *dst;
1726 struct xfrm_dst *xdst;
1727 int err;
1728
1729 /* Try to instantiate a bundle */
1730 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001731 if (err <= 0) {
1732 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001733 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1734 return ERR_PTR(err);
1735 }
1736
1737 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1738 if (IS_ERR(dst)) {
1739 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1740 return ERR_CAST(dst);
1741 }
1742
1743 xdst = (struct xfrm_dst *)dst;
1744 xdst->num_xfrms = err;
1745 if (num_pols > 1)
1746 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1747 else
1748 err = xfrm_dst_update_origin(dst, fl);
1749 if (unlikely(err)) {
1750 dst_free(dst);
1751 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1752 return ERR_PTR(err);
1753 }
1754
1755 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001756 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001757 xdst->policy_genid = atomic_read(&pols[0]->genid);
1758
1759 return xdst;
1760}
1761
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001762static void xfrm_policy_queue_process(unsigned long arg)
1763{
1764 int err = 0;
1765 struct sk_buff *skb;
1766 struct sock *sk;
1767 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001768 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1769 struct xfrm_policy_queue *pq = &pol->polq;
1770 struct flowi fl;
1771 struct sk_buff_head list;
1772
1773 spin_lock(&pq->hold_queue.lock);
1774 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001775 if (!skb) {
1776 spin_unlock(&pq->hold_queue.lock);
1777 goto out;
1778 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001779 dst = skb_dst(skb);
1780 sk = skb->sk;
1781 xfrm_decode_session(skb, &fl, dst->ops->family);
1782 spin_unlock(&pq->hold_queue.lock);
1783
1784 dst_hold(dst->path);
1785 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1786 sk, 0);
1787 if (IS_ERR(dst))
1788 goto purge_queue;
1789
1790 if (dst->flags & DST_XFRM_QUEUE) {
1791 dst_release(dst);
1792
1793 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1794 goto purge_queue;
1795
1796 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001797 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1798 xfrm_pol_hold(pol);
1799 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001800 }
1801
1802 dst_release(dst);
1803
1804 __skb_queue_head_init(&list);
1805
1806 spin_lock(&pq->hold_queue.lock);
1807 pq->timeout = 0;
1808 skb_queue_splice_init(&pq->hold_queue, &list);
1809 spin_unlock(&pq->hold_queue.lock);
1810
1811 while (!skb_queue_empty(&list)) {
1812 skb = __skb_dequeue(&list);
1813
1814 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1815 dst_hold(skb_dst(skb)->path);
1816 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1817 &fl, skb->sk, 0);
1818 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001819 kfree_skb(skb);
1820 continue;
1821 }
1822
1823 nf_reset(skb);
1824 skb_dst_drop(skb);
1825 skb_dst_set(skb, dst);
1826
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001827 err = dst_output(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001828 }
1829
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001830out:
1831 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001832 return;
1833
1834purge_queue:
1835 pq->timeout = 0;
1836 xfrm_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001837 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001838}
1839
Eric Dumazetaad88722014-04-15 13:47:15 -04001840static int xdst_queue_output(struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001841{
1842 unsigned long sched_next;
1843 struct dst_entry *dst = skb_dst(skb);
1844 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001845 struct xfrm_policy *pol = xdst->pols[0];
1846 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001847 const struct sk_buff *fclone = skb + 1;
1848
1849 if (unlikely(skb->fclone == SKB_FCLONE_ORIG &&
1850 fclone->fclone == SKB_FCLONE_CLONE)) {
1851 kfree_skb(skb);
1852 return 0;
1853 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001854
1855 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1856 kfree_skb(skb);
1857 return -EAGAIN;
1858 }
1859
1860 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001861
1862 spin_lock_bh(&pq->hold_queue.lock);
1863
1864 if (!pq->timeout)
1865 pq->timeout = XFRM_QUEUE_TMO_MIN;
1866
1867 sched_next = jiffies + pq->timeout;
1868
1869 if (del_timer(&pq->hold_timer)) {
1870 if (time_before(pq->hold_timer.expires, sched_next))
1871 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001872 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001873 }
1874
1875 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001876 if (!mod_timer(&pq->hold_timer, sched_next))
1877 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001878
1879 spin_unlock_bh(&pq->hold_queue.lock);
1880
1881 return 0;
1882}
1883
1884static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02001885 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001886 const struct flowi *fl,
1887 int num_xfrms,
1888 u16 family)
1889{
1890 int err;
1891 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02001892 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001893 struct dst_entry *dst1;
1894 struct xfrm_dst *xdst;
1895
1896 xdst = xfrm_alloc_dst(net, family);
1897 if (IS_ERR(xdst))
1898 return xdst;
1899
Steffen Klassertb8c203b2014-09-16 10:08:49 +02001900 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
1901 net->xfrm.sysctl_larval_drop ||
1902 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001903 return xdst;
1904
Steffen Klassertb8c203b2014-09-16 10:08:49 +02001905 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001906 dst1 = &xdst->u.dst;
1907 dst_hold(dst);
1908 xdst->route = dst;
1909
1910 dst_copy_metrics(dst1, dst);
1911
1912 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1913 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
1914 dst1->lastuse = jiffies;
1915
1916 dst1->input = dst_discard;
1917 dst1->output = xdst_queue_output;
1918
1919 dst_hold(dst);
1920 dst1->child = dst;
1921 dst1->path = dst;
1922
1923 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
1924
1925 err = -ENODEV;
1926 dev = dst->dev;
1927 if (!dev)
1928 goto free_dst;
1929
1930 err = xfrm_fill_dst(xdst, dev, fl);
1931 if (err)
1932 goto free_dst;
1933
1934out:
1935 return xdst;
1936
1937free_dst:
1938 dst_release(dst1);
1939 xdst = ERR_PTR(err);
1940 goto out;
1941}
1942
Timo Teräs80c802f2010-04-07 00:30:05 +00001943static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001944xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00001945 struct flow_cache_object *oldflo, void *ctx)
1946{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02001947 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
Timo Teräs80c802f2010-04-07 00:30:05 +00001948 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1949 struct xfrm_dst *xdst, *new_xdst;
1950 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
1951
1952 /* Check if the policies from old bundle are usable */
1953 xdst = NULL;
1954 if (oldflo) {
1955 xdst = container_of(oldflo, struct xfrm_dst, flo);
1956 num_pols = xdst->num_pols;
1957 num_xfrms = xdst->num_xfrms;
1958 pol_dead = 0;
1959 for (i = 0; i < num_pols; i++) {
1960 pols[i] = xdst->pols[i];
1961 pol_dead |= pols[i]->walk.dead;
1962 }
1963 if (pol_dead) {
1964 dst_free(&xdst->u.dst);
1965 xdst = NULL;
1966 num_pols = 0;
1967 num_xfrms = 0;
1968 oldflo = NULL;
1969 }
1970 }
1971
1972 /* Resolve policies to use if we couldn't get them from
1973 * previous cache entry */
1974 if (xdst == NULL) {
1975 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001976 pols[0] = __xfrm_policy_lookup(net, fl, family,
1977 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001978 err = xfrm_expand_policies(fl, family, pols,
1979 &num_pols, &num_xfrms);
1980 if (err < 0)
1981 goto inc_error;
1982 if (num_pols == 0)
1983 return NULL;
1984 if (num_xfrms <= 0)
1985 goto make_dummy_bundle;
1986 }
1987
Steffen Klassertb8c203b2014-09-16 10:08:49 +02001988 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
1989 xflo->dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00001990 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 Klassertb8c203b2014-09-16 10:08:49 +02002023 xdst = xfrm_create_dummy_bundle(net, xflo, 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;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002030 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002031
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 Klassertb7eea452014-06-18 12:34:21 +02002110 dst_hold(&xdst->u.dst);
2111 xdst->u.dst.flags |= DST_NOCACHE;
Timo Teräs80c802f2010-04-07 00:30:05 +00002112 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002113 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Timo Teräs80c802f2010-04-07 00:30:05 +00002116 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002117 struct xfrm_flo xflo;
2118
2119 xflo.dst_orig = dst_orig;
2120 xflo.flags = flags;
2121
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,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002128 xfrm_bundle_lookup, &xflo);
Timo Teräs80c802f2010-04-07 00:30:05 +00002129 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;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002139 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002140 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
Steffen Klassertf92ee612014-09-16 10:08:40 +02002156 return ERR_PTR(-EREMOTE);
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
Steffen Klassertf92ee612014-09-16 10:08:40 +02002213/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2214 * Otherwise we may send out blackholed packets.
2215 */
2216struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2217 const struct flowi *fl,
2218 struct sock *sk, int flags)
2219{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002220 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
2221 flags | XFRM_LOOKUP_QUEUE);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002222
2223 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2224 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2225
2226 return dst;
2227}
2228EXPORT_SYMBOL(xfrm_lookup_route);
2229
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002230static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002231xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002232{
2233 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002234
2235 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2236 return 0;
2237 x = skb->sp->xvec[idx];
2238 if (!x->type->reject)
2239 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002240 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002241}
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243/* When skb is transformed back to its "native" form, we have to
2244 * check policy restrictions. At the moment we make this in maximally
2245 * stupid way. Shame on me. :-) Of course, connected sockets must
2246 * have policy cached at them.
2247 */
2248
2249static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002250xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 unsigned short family)
2252{
2253 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002254 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 return x->id.proto == tmpl->id.proto &&
2256 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2257 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2258 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002259 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002260 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002261 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2262 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263}
2264
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002265/*
2266 * 0 or more than 0 is returned when validation is succeeded (either bypass
2267 * because of optional transport mode, or next index of the mathced secpath
2268 * state with the template.
2269 * -1 is returned when no matching template is found.
2270 * Otherwise "-2 - errored_index" is returned.
2271 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002273xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 unsigned short family)
2275{
2276 int idx = start;
2277
2278 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002279 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 return start;
2281 } else
2282 start = -1;
2283 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002284 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002286 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2287 if (start == -1)
2288 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 }
2292 return start;
2293}
2294
Herbert Xud5422ef2007-12-12 10:44:16 -08002295int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2296 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
2298 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002299 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
2301 if (unlikely(afinfo == NULL))
2302 return -EAFNOSUPPORT;
2303
Herbert Xud5422ef2007-12-12 10:44:16 -08002304 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002305 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002307 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
Herbert Xud5422ef2007-12-12 10:44:16 -08002309EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
David S. Miller9a7386e2011-02-24 01:44:12 -05002311static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
2313 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002314 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002315 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 }
2319
2320 return 0;
2321}
2322
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002323int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 unsigned short family)
2325{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002326 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002328 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2329 int npols = 0;
2330 int xfrm_nr;
2331 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002332 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002334 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002335 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Herbert Xud5422ef2007-12-12 10:44:16 -08002337 reverse = dir & ~XFRM_POLICY_MASK;
2338 dir &= XFRM_POLICY_MASK;
2339 fl_dir = policy_to_flow_dir(dir);
2340
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002341 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002342 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002344 }
2345
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002346 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
2348 /* First, check used SA against their selectors. */
2349 if (skb->sp) {
2350 int i;
2351
Weilong Chen9b7a7872013-12-24 09:43:46 +08002352 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002353 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002354 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002355 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 }
2359 }
2360
2361 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002362 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002363 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
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);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002366 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002367 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002370 if (!pol) {
2371 struct flow_cache_object *flo;
2372
2373 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2374 xfrm_policy_lookup, NULL);
2375 if (IS_ERR_OR_NULL(flo))
2376 pol = ERR_CAST(flo);
2377 else
2378 pol = container_of(flo, struct xfrm_policy, flo);
2379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002381 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002382 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002383 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002384 }
James Morris134b0fc2006-10-05 15:42:27 -05002385
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002386 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002387 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002388 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002389 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002390 return 0;
2391 }
2392 return 1;
2393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
James Morris9d729f72007-03-04 16:12:44 -08002395 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002397 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002398 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002399#ifdef CONFIG_XFRM_SUB_POLICY
2400 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002401 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002402 &fl, family,
2403 XFRM_POLICY_IN);
2404 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002405 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002406 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002407 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002408 }
James Morris9d729f72007-03-04 16:12:44 -08002409 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002410 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002411 }
2412 }
2413#endif
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if (pol->action == XFRM_POLICY_ALLOW) {
2416 struct sec_path *sp;
2417 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002418 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002419 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002420 struct xfrm_tmpl **tpp = tp;
2421 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 int i, k;
2423
2424 if ((sp = skb->sp) == NULL)
2425 sp = &dummy;
2426
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002427 for (pi = 0; pi < npols; pi++) {
2428 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002429 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002430 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002431 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002432 }
2433 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002434 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002435 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002436 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002437 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2438 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2439 }
2440 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002441 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002442 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002443 tpp = stp;
2444 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 /* For each tunnel xfrm, find the first matching tmpl.
2447 * For each tmpl before that, find corresponding xfrm.
2448 * Order is _important_. Later we will implement
2449 * some barriers, but at the moment barriers
2450 * are implied between each two transformations.
2451 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002452 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2453 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002454 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002455 if (k < -1)
2456 /* "-2 - errored_index" returned */
2457 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002458 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
2462
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002463 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002464 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002468 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 return 1;
2470 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002471 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002474 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002475reject_error:
2476 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return 0;
2478}
2479EXPORT_SYMBOL(__xfrm_policy_check);
2480
2481int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2482{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002483 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002485 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002486 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002488 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002489 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002493 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002494
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002495 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002496 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002497 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002498 dst = NULL;
2499 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002500 skb_dst_set(skb, dst);
2501 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503EXPORT_SYMBOL(__xfrm_route_forward);
2504
David S. Millerd49c73c2006-08-13 18:55:53 -07002505/* Optimize later using cookies and generation ids. */
2506
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2508{
David S. Millerd49c73c2006-08-13 18:55:53 -07002509 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002510 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2511 * get validated by dst_ops->check on every use. We do this
2512 * because when a normal route referenced by an XFRM dst is
2513 * obsoleted we do not go looking around for all parent
2514 * referencing XFRM dsts so that we can invalidate them. It
2515 * is just too much work. Instead we make the checks here on
2516 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002517 *
2518 * XFRM dst A --> IPv4 dst X
2519 *
2520 * X is the "xdst->route" of A (X is also the "dst->path" of A
2521 * in this example). If X is marked obsolete, "A" will not
2522 * notice. That's what we are validating here via the
2523 * stale_bundle() check.
2524 *
2525 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002526 * dst which causes it's ->obsolete field to be set to
2527 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2528 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002529 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002530 if (dst->obsolete < 0 && !stale_bundle(dst))
2531 return dst;
2532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 return NULL;
2534}
2535
2536static int stale_bundle(struct dst_entry *dst)
2537{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002538 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539}
2540
Herbert Xuaabc9762005-05-03 16:27:10 -07002541void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002544 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002545 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 dev_put(dev);
2547 }
2548}
Herbert Xuaabc9762005-05-03 16:27:10 -07002549EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551static void xfrm_link_failure(struct sk_buff *skb)
2552{
2553 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554}
2555
2556static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2557{
2558 if (dst) {
2559 if (dst->obsolete) {
2560 dst_release(dst);
2561 dst = NULL;
2562 }
2563 }
2564 return dst;
2565}
2566
Paul Mooree4c17212013-05-29 07:36:25 +00002567void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002568{
Fan Duca925cf2014-01-18 09:55:27 +08002569 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002570}
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{
Fan Duca925cf2014-01-18 09:55:27 +08002575 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002576}
2577
Herbert Xu25ee3282007-12-11 09:32:34 -08002578static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
2580 do {
2581 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2582 u32 pmtu, route_mtu_cached;
2583
2584 pmtu = dst_mtu(dst->child);
2585 xdst->child_mtu_cached = pmtu;
2586
2587 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2588
2589 route_mtu_cached = dst_mtu(xdst->route);
2590 xdst->route_mtu_cached = route_mtu_cached;
2591
2592 if (pmtu > route_mtu_cached)
2593 pmtu = route_mtu_cached;
2594
David S. Millerdefb3512010-12-08 21:16:57 -08002595 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 } while ((dst = dst->next));
2597}
2598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599/* Check that the bundle accepts the flow and its components are
2600 * still valid.
2601 */
2602
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002603static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
2605 struct dst_entry *dst = &first->u.dst;
2606 struct xfrm_dst *last;
2607 u32 mtu;
2608
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002609 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 (dst->dev && !netif_running(dst->dev)))
2611 return 0;
2612
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002613 if (dst->flags & DST_XFRM_QUEUE)
2614 return 1;
2615
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 last = NULL;
2617
2618 do {
2619 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2620
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2622 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002623 if (xdst->xfrm_genid != dst->xfrm->genid)
2624 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002625 if (xdst->num_pols > 0 &&
2626 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002627 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
2629 mtu = dst_mtu(dst->child);
2630 if (xdst->child_mtu_cached != mtu) {
2631 last = xdst;
2632 xdst->child_mtu_cached = mtu;
2633 }
2634
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002635 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 return 0;
2637 mtu = dst_mtu(xdst->route);
2638 if (xdst->route_mtu_cached != mtu) {
2639 last = xdst;
2640 xdst->route_mtu_cached = mtu;
2641 }
2642
2643 dst = dst->child;
2644 } while (dst->xfrm);
2645
2646 if (likely(!last))
2647 return 1;
2648
2649 mtu = last->child_mtu_cached;
2650 for (;;) {
2651 dst = &last->u.dst;
2652
2653 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2654 if (mtu > last->route_mtu_cached)
2655 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002656 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
2658 if (last == first)
2659 break;
2660
Patrick McHardybd0bf072007-07-18 01:55:52 -07002661 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 last->child_mtu_cached = mtu;
2663 }
2664
2665 return 1;
2666}
2667
David S. Miller0dbaee32010-12-13 12:52:14 -08002668static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2669{
2670 return dst_metric_advmss(dst->path);
2671}
2672
Steffen Klassertebb762f2011-11-23 02:12:51 +00002673static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002674{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002675 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2676
2677 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002678}
2679
David S. Millerf894cbf2012-07-02 21:52:24 -07002680static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2681 struct sk_buff *skb,
2682 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002683{
David S. Millerf894cbf2012-07-02 21:52:24 -07002684 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002685}
2686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2688{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002689 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 int err = 0;
2691 if (unlikely(afinfo == NULL))
2692 return -EINVAL;
2693 if (unlikely(afinfo->family >= NPROTO))
2694 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002695 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2697 err = -ENOBUFS;
2698 else {
2699 struct dst_ops *dst_ops = afinfo->dst_ops;
2700 if (likely(dst_ops->kmem_cachep == NULL))
2701 dst_ops->kmem_cachep = xfrm_dst_cache;
2702 if (likely(dst_ops->check == NULL))
2703 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002704 if (likely(dst_ops->default_advmss == NULL))
2705 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002706 if (likely(dst_ops->mtu == NULL))
2707 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 if (likely(dst_ops->negative_advice == NULL))
2709 dst_ops->negative_advice = xfrm_negative_advice;
2710 if (likely(dst_ops->link_failure == NULL))
2711 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002712 if (likely(dst_ops->neigh_lookup == NULL))
2713 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002715 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002716 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002718 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002719
2720 rtnl_lock();
2721 for_each_net(net) {
2722 struct dst_ops *xfrm_dst_ops;
2723
2724 switch (afinfo->family) {
2725 case AF_INET:
2726 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2727 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002728#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002729 case AF_INET6:
2730 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2731 break;
2732#endif
2733 default:
2734 BUG();
2735 }
2736 *xfrm_dst_ops = *afinfo->dst_ops;
2737 }
2738 rtnl_unlock();
2739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 return err;
2741}
2742EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2743
2744int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2745{
2746 int err = 0;
2747 if (unlikely(afinfo == NULL))
2748 return -EINVAL;
2749 if (unlikely(afinfo->family >= NPROTO))
2750 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002751 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2753 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2754 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002755 else
2756 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2757 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002759 spin_unlock(&xfrm_policy_afinfo_lock);
2760 if (!err) {
2761 struct dst_ops *dst_ops = afinfo->dst_ops;
2762
2763 synchronize_rcu();
2764
2765 dst_ops->kmem_cachep = NULL;
2766 dst_ops->check = NULL;
2767 dst_ops->negative_advice = NULL;
2768 dst_ops->link_failure = NULL;
2769 afinfo->garbage_collect = NULL;
2770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 return err;
2772}
2773EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2774
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002775static void __net_init xfrm_dst_ops_init(struct net *net)
2776{
2777 struct xfrm_policy_afinfo *afinfo;
2778
Eric Dumazetef8531b2012-08-19 12:31:48 +02002779 rcu_read_lock();
2780 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002781 if (afinfo)
2782 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002783#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetef8531b2012-08-19 12:31:48 +02002784 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002785 if (afinfo)
2786 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2787#endif
Priyanka Jain418a99a2012-08-12 21:22:29 +00002788 rcu_read_unlock();
Herbert Xu546be242006-05-27 23:03:58 -07002789}
2790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2792{
Jiri Pirko351638e2013-05-28 01:30:21 +00002793 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002794
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 switch (event) {
2796 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002797 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 }
2799 return NOTIFY_DONE;
2800}
2801
2802static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002803 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804};
2805
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002806#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002807static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002808{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002809 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002810 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2811 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002812 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002813 rv = xfrm_proc_init(net);
2814 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002815 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002816 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002817}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002818
2819static void xfrm_statistics_fini(struct net *net)
2820{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002821 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002822 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002823}
2824#else
2825static int __net_init xfrm_statistics_init(struct net *net)
2826{
2827 return 0;
2828}
2829
2830static void xfrm_statistics_fini(struct net *net)
2831{
2832}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002833#endif
2834
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002835static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
David S. Miller2518c7c2006-08-24 04:45:07 -07002837 unsigned int hmask, sz;
2838 int dir;
2839
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002840 if (net_eq(net, &init_net))
2841 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002843 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002844 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
David S. Miller2518c7c2006-08-24 04:45:07 -07002846 hmask = 8 - 1;
2847 sz = (hmask+1) * sizeof(struct hlist_head);
2848
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002849 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2850 if (!net->xfrm.policy_byidx)
2851 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002852 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002853
2854 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2855 struct xfrm_policy_hash *htab;
2856
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002857 net->xfrm.policy_count[dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002858 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002859
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002860 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002861 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002862 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002863 goto out_bydst;
2864 htab->hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002865 }
2866
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002867 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002868 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002869 if (net_eq(net, &init_net))
2870 register_netdevice_notifier(&xfrm_dev_notifier);
2871 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002872
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002873out_bydst:
2874 for (dir--; dir >= 0; dir--) {
2875 struct xfrm_policy_hash *htab;
2876
2877 htab = &net->xfrm.policy_bydst[dir];
2878 xfrm_hash_free(htab->table, sz);
2879 }
2880 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002881out_byidx:
2882 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883}
2884
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002885static void xfrm_policy_fini(struct net *net)
2886{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002887 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002888 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002889
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002890 flush_work(&net->xfrm.policy_hash_work);
2891#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09002892 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002893#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09002894 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002895
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002896 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002897
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002898 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002899 struct xfrm_policy_hash *htab;
2900
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002901 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002902
2903 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01002904 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002905 WARN_ON(!hlist_empty(htab->table));
2906 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002907 }
2908
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002909 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002910 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2911 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002912}
2913
2914static int __net_init xfrm_net_init(struct net *net)
2915{
2916 int rv;
2917
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002918 rv = xfrm_statistics_init(net);
2919 if (rv < 0)
2920 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002921 rv = xfrm_state_init(net);
2922 if (rv < 0)
2923 goto out_state;
2924 rv = xfrm_policy_init(net);
2925 if (rv < 0)
2926 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002927 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002928 rv = xfrm_sysctl_init(net);
2929 if (rv < 0)
2930 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01002931 rv = flow_cache_init(net);
2932 if (rv < 0)
2933 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08002934
2935 /* Initialize the per-net locks here */
2936 spin_lock_init(&net->xfrm.xfrm_state_lock);
2937 rwlock_init(&net->xfrm.xfrm_policy_lock);
Fan Du283bc9f2013-11-07 17:47:50 +08002938 mutex_init(&net->xfrm.xfrm_cfg_mutex);
2939
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002940 return 0;
2941
Steffen Klassert4a93f502014-03-12 09:43:17 +01002942out:
2943 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002944out_sysctl:
2945 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002946out_policy:
2947 xfrm_state_fini(net);
2948out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002949 xfrm_statistics_fini(net);
2950out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002951 return rv;
2952}
2953
2954static void __net_exit xfrm_net_exit(struct net *net)
2955{
Steffen Klassert4a93f502014-03-12 09:43:17 +01002956 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002957 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002958 xfrm_policy_fini(net);
2959 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002960 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002961}
2962
2963static struct pernet_operations __net_initdata xfrm_net_ops = {
2964 .init = xfrm_net_init,
2965 .exit = xfrm_net_exit,
2966};
2967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968void __init xfrm_init(void)
2969{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002970 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 xfrm_input_init();
2972}
2973
Joy Lattenab5f5e82007-09-17 11:51:22 -07002974#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08002975static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2976 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002977{
Paul Moore875179f2007-12-01 23:27:18 +11002978 struct xfrm_sec_ctx *ctx = xp->security;
2979 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002980
Paul Moore875179f2007-12-01 23:27:18 +11002981 if (ctx)
2982 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2983 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2984
Weilong Chen9b7a7872013-12-24 09:43:46 +08002985 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002986 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002987 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002988 if (sel->prefixlen_s != 32)
2989 audit_log_format(audit_buf, " src_prefixlen=%d",
2990 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002991 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002992 if (sel->prefixlen_d != 32)
2993 audit_log_format(audit_buf, " dst_prefixlen=%d",
2994 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002995 break;
2996 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002997 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002998 if (sel->prefixlen_s != 128)
2999 audit_log_format(audit_buf, " src_prefixlen=%d",
3000 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003001 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003002 if (sel->prefixlen_d != 128)
3003 audit_log_format(audit_buf, " dst_prefixlen=%d",
3004 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003005 break;
3006 }
3007}
3008
Tetsuo Handa2e710292014-04-22 21:48:30 +09003009void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003010{
3011 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003012
Paul Mooreafeb14b2007-12-21 14:58:11 -08003013 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003014 if (audit_buf == NULL)
3015 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003016 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003017 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003018 xfrm_audit_common_policyinfo(xp, audit_buf);
3019 audit_log_end(audit_buf);
3020}
3021EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3022
Paul Moore68277ac2007-12-20 20:49:33 -08003023void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003024 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003025{
3026 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003027
Paul Mooreafeb14b2007-12-21 14:58:11 -08003028 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003029 if (audit_buf == NULL)
3030 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003031 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003032 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003033 xfrm_audit_common_policyinfo(xp, audit_buf);
3034 audit_log_end(audit_buf);
3035}
3036EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3037#endif
3038
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003039#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003040static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3041 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003042{
3043 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3044 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003045 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3046 sel_cmp->family) &&
3047 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3048 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003049 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3050 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003051 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003052 }
3053 } else {
3054 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003055 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003056 }
3057 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003058 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003059}
3060
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003061static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3062 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003063{
3064 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003065 struct hlist_head *chain;
3066 u32 priority = ~0U;
3067
Fan Du283bc9f2013-11-07 17:47:50 +08003068 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
Fan Du8d549c42013-11-07 17:47:49 +08003069 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003070 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003071 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3072 pol->type == type) {
3073 ret = pol;
3074 priority = ret->priority;
3075 break;
3076 }
3077 }
Fan Du8d549c42013-11-07 17:47:49 +08003078 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003079 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003080 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3081 pol->type == type &&
3082 pol->priority < priority) {
3083 ret = pol;
3084 break;
3085 }
3086 }
3087
3088 if (ret)
3089 xfrm_pol_hold(ret);
3090
Fan Du283bc9f2013-11-07 17:47:50 +08003091 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003092
3093 return ret;
3094}
3095
David S. Millerdd701752011-02-24 00:21:08 -05003096static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003097{
3098 int match = 0;
3099
3100 if (t->mode == m->mode && t->id.proto == m->proto &&
3101 (m->reqid == 0 || t->reqid == m->reqid)) {
3102 switch (t->mode) {
3103 case XFRM_MODE_TUNNEL:
3104 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003105 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3106 m->old_family) &&
3107 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3108 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003109 match = 1;
3110 }
3111 break;
3112 case XFRM_MODE_TRANSPORT:
3113 /* in case of transport mode, template does not store
3114 any IP addresses, hence we just compare mode and
3115 protocol */
3116 match = 1;
3117 break;
3118 default:
3119 break;
3120 }
3121 }
3122 return match;
3123}
3124
3125/* update endpoint address(es) of template(s) */
3126static int xfrm_policy_migrate(struct xfrm_policy *pol,
3127 struct xfrm_migrate *m, int num_migrate)
3128{
3129 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003130 int i, j, n = 0;
3131
3132 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003133 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003134 /* target policy has been deleted */
3135 write_unlock_bh(&pol->lock);
3136 return -ENOENT;
3137 }
3138
3139 for (i = 0; i < pol->xfrm_nr; i++) {
3140 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3141 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3142 continue;
3143 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003144 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3145 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003146 continue;
3147 /* update endpoints */
3148 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3149 sizeof(pol->xfrm_vec[i].id.daddr));
3150 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3151 sizeof(pol->xfrm_vec[i].saddr));
3152 pol->xfrm_vec[i].encap_family = mp->new_family;
3153 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003154 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003155 }
3156 }
3157
3158 write_unlock_bh(&pol->lock);
3159
3160 if (!n)
3161 return -ENODATA;
3162
3163 return 0;
3164}
3165
David S. Millerdd701752011-02-24 00:21:08 -05003166static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003167{
3168 int i, j;
3169
3170 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3171 return -EINVAL;
3172
3173 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003174 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3175 m[i].old_family) &&
3176 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3177 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003178 return -EINVAL;
3179 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3180 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3181 return -EINVAL;
3182
3183 /* check if there is any duplicated entry */
3184 for (j = i + 1; j < num_migrate; j++) {
3185 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3186 sizeof(m[i].old_daddr)) &&
3187 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3188 sizeof(m[i].old_saddr)) &&
3189 m[i].proto == m[j].proto &&
3190 m[i].mode == m[j].mode &&
3191 m[i].reqid == m[j].reqid &&
3192 m[i].old_family == m[j].old_family)
3193 return -EINVAL;
3194 }
3195 }
3196
3197 return 0;
3198}
3199
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003200int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003201 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003202 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003203{
3204 int i, err, nx_cur = 0, nx_new = 0;
3205 struct xfrm_policy *pol = NULL;
3206 struct xfrm_state *x, *xc;
3207 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3208 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3209 struct xfrm_migrate *mp;
3210
3211 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3212 goto out;
3213
3214 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003215 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003216 err = -ENOENT;
3217 goto out;
3218 }
3219
3220 /* Stage 2 - find and update state(s) */
3221 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003222 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003223 x_cur[nx_cur] = x;
3224 nx_cur++;
3225 if ((xc = xfrm_state_migrate(x, mp))) {
3226 x_new[nx_new] = xc;
3227 nx_new++;
3228 } else {
3229 err = -ENODATA;
3230 goto restore_state;
3231 }
3232 }
3233 }
3234
3235 /* Stage 3 - update policy */
3236 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3237 goto restore_state;
3238
3239 /* Stage 4 - delete old state(s) */
3240 if (nx_cur) {
3241 xfrm_states_put(x_cur, nx_cur);
3242 xfrm_states_delete(x_cur, nx_cur);
3243 }
3244
3245 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003246 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003247
3248 xfrm_pol_put(pol);
3249
3250 return 0;
3251out:
3252 return err;
3253
3254restore_state:
3255 if (pol)
3256 xfrm_pol_put(pol);
3257 if (nx_cur)
3258 xfrm_states_put(x_cur, nx_cur);
3259 if (nx_new)
3260 xfrm_states_delete(x_new, nx_new);
3261
3262 return err;
3263}
David S. Millere610e672007-02-08 13:29:15 -08003264EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003265#endif