blob: 55bcb8604bc6de77033280082176ed68f9dbc666 [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
Priyanka Jain418a99a2012-08-12 21:22:29 +000042static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
43static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO]
44 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Christoph Lametere18b8902006-12-06 20:33:20 -080046static struct kmem_cache *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Herbert Xu25ee3282007-12-11 09:32:34 -080048static void xfrm_init_pmtu(struct dst_entry *dst);
Timo Teräs80c802f2010-04-07 00:30:05 +000049static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +000050static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Steffen Klasserta0073fe2013-02-05 12:52:55 +010051static void xfrm_policy_queue_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Wei Yongjun29fa0b302008-12-03 00:33:09 -080053static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
54 int dir);
55
David S. Millerbc9b35a2012-05-15 15:04:57 -040056static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050057__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080058{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050059 const struct flowi4 *fl4 = &fl->u.ip4;
60
Alexey Dobriyan26bff942011-11-22 06:46:02 +000061 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
62 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -050063 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
64 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
65 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
66 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080067}
68
David S. Millerbc9b35a2012-05-15 15:04:57 -040069static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050070__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080071{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050072 const struct flowi6 *fl6 = &fl->u.ip6;
73
74 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
75 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
76 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
77 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
78 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
79 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080080}
81
David S. Millerbc9b35a2012-05-15 15:04:57 -040082bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
83 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -080084{
85 switch (family) {
86 case AF_INET:
87 return __xfrm4_selector_match(sel, fl);
88 case AF_INET6:
89 return __xfrm6_selector_match(sel, fl);
90 }
David S. Millerbc9b35a2012-05-15 15:04:57 -040091 return false;
Andrew Morton77681022006-11-08 22:46:26 -080092}
93
Eric Dumazetef8531b2012-08-19 12:31:48 +020094static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
95{
96 struct xfrm_policy_afinfo *afinfo;
97
98 if (unlikely(family >= NPROTO))
99 return NULL;
100 rcu_read_lock();
101 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
102 if (unlikely(!afinfo))
103 rcu_read_unlock();
104 return afinfo;
105}
106
107static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
108{
109 rcu_read_unlock();
110}
111
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800112static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos,
David S. Miller6418c4e2011-02-24 00:16:53 -0500113 const xfrm_address_t *saddr,
114 const xfrm_address_t *daddr,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900115 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800117 struct xfrm_policy_afinfo *afinfo;
118 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Herbert Xu25ee3282007-12-11 09:32:34 -0800120 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800122 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800124 dst = afinfo->dst_lookup(net, tos, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900127
128 return dst;
129}
130
131static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
132 xfrm_address_t *prev_saddr,
133 xfrm_address_t *prev_daddr,
134 int family)
135{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800136 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900137 xfrm_address_t *saddr = &x->props.saddr;
138 xfrm_address_t *daddr = &x->id.daddr;
139 struct dst_entry *dst;
140
141 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
142 saddr = x->coaddr;
143 daddr = prev_daddr;
144 }
145 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
146 saddr = prev_saddr;
147 daddr = x->coaddr;
148 }
149
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800150 dst = __xfrm_dst_lookup(net, tos, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900151
152 if (!IS_ERR(dst)) {
153 if (prev_saddr != saddr)
154 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
155 if (prev_daddr != daddr)
156 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
157 }
158
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800159 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static inline unsigned long make_jiffies(long secs)
163{
164 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
165 return MAX_SCHEDULE_TIMEOUT-1;
166 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900167 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170static void xfrm_policy_timer(unsigned long data)
171{
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800172 struct xfrm_policy *xp = (struct xfrm_policy *)data;
James Morris9d729f72007-03-04 16:12:44 -0800173 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 long next = LONG_MAX;
175 int warn = 0;
176 int dir;
177
178 read_lock(&xp->lock);
179
Timo Teräsea2dea92010-03-31 00:17:05 +0000180 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 goto out;
182
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700183 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 if (xp->lft.hard_add_expires_seconds) {
186 long tmo = xp->lft.hard_add_expires_seconds +
187 xp->curlft.add_time - now;
188 if (tmo <= 0)
189 goto expired;
190 if (tmo < next)
191 next = tmo;
192 }
193 if (xp->lft.hard_use_expires_seconds) {
194 long tmo = xp->lft.hard_use_expires_seconds +
195 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
196 if (tmo <= 0)
197 goto expired;
198 if (tmo < next)
199 next = tmo;
200 }
201 if (xp->lft.soft_add_expires_seconds) {
202 long tmo = xp->lft.soft_add_expires_seconds +
203 xp->curlft.add_time - now;
204 if (tmo <= 0) {
205 warn = 1;
206 tmo = XFRM_KM_TIMEOUT;
207 }
208 if (tmo < next)
209 next = tmo;
210 }
211 if (xp->lft.soft_use_expires_seconds) {
212 long tmo = xp->lft.soft_use_expires_seconds +
213 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
214 if (tmo <= 0) {
215 warn = 1;
216 tmo = XFRM_KM_TIMEOUT;
217 }
218 if (tmo < next)
219 next = tmo;
220 }
221
222 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800223 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (next != LONG_MAX &&
225 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
226 xfrm_pol_hold(xp);
227
228out:
229 read_unlock(&xp->lock);
230 xfrm_pol_put(xp);
231 return;
232
233expired:
234 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700235 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800236 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 xfrm_pol_put(xp);
238}
239
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000240static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
241{
242 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
243
244 if (unlikely(pol->walk.dead))
245 flo = NULL;
246 else
247 xfrm_pol_hold(pol);
248
249 return flo;
250}
251
252static int xfrm_policy_flo_check(struct flow_cache_object *flo)
253{
254 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
255
256 return !pol->walk.dead;
257}
258
259static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
260{
261 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
262}
263
264static const struct flow_cache_ops xfrm_policy_fc_ops = {
265 .get = xfrm_policy_flo_get,
266 .check = xfrm_policy_flo_check,
267 .delete = xfrm_policy_flo_delete,
268};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
271 * SPD calls.
272 */
273
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800274struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct xfrm_policy *policy;
277
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700278 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800281 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700282 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700283 INIT_HLIST_NODE(&policy->bydst);
284 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700286 atomic_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100287 skb_queue_head_init(&policy->polq.hold_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800288 setup_timer(&policy->timer, xfrm_policy_timer,
289 (unsigned long)policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100290 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
291 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000292 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294 return policy;
295}
296EXPORT_SYMBOL(xfrm_policy_alloc);
297
298/* Destroy xfrm_policy: descendant resources must be released to this moment. */
299
WANG Cong64c31b32008-01-07 22:34:29 -0800300void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Herbert Xu12a169e2008-10-01 07:03:24 -0700302 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Fan Du0659eea2013-08-01 18:08:36 +0800304 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 BUG();
306
Paul Moore03e1ad72008-04-12 19:07:52 -0700307 security_xfrm_policy_free(policy->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 kfree(policy);
309}
WANG Cong64c31b32008-01-07 22:34:29 -0800310EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100312static void xfrm_queue_purge(struct sk_buff_head *list)
313{
314 struct sk_buff *skb;
315
Steffen Klassert302a50b2013-08-28 08:47:14 +0200316 while ((skb = skb_dequeue(list)) != NULL)
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100317 kfree_skb(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320/* Rule must be locked. Release descentant resources, announce
321 * entry dead. The rule must be unlinked from lists to the moment.
322 */
323
324static void xfrm_policy_kill(struct xfrm_policy *policy)
325{
Herbert Xu12a169e2008-10-01 07:03:24 -0700326 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Timo Teräs285ead12010-04-07 00:30:06 +0000328 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200330 if (del_timer(&policy->polq.hold_timer))
331 xfrm_pol_put(policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100332 xfrm_queue_purge(&policy->polq.hold_queue);
333
Timo Teräs285ead12010-04-07 00:30:06 +0000334 if (del_timer(&policy->timer))
335 xfrm_pol_put(policy);
336
337 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
David S. Miller2518c7c2006-08-24 04:45:07 -0700340static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
341
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800342static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700343{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800344 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700345}
346
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200347/* calculate policy hash thresholds */
348static void __get_hash_thresh(struct net *net,
349 unsigned short family, int dir,
350 u8 *dbits, u8 *sbits)
351{
352 switch (family) {
353 case AF_INET:
354 *dbits = net->xfrm.policy_bydst[dir].dbits4;
355 *sbits = net->xfrm.policy_bydst[dir].sbits4;
356 break;
357
358 case AF_INET6:
359 *dbits = net->xfrm.policy_bydst[dir].dbits6;
360 *sbits = net->xfrm.policy_bydst[dir].sbits6;
361 break;
362
363 default:
364 *dbits = 0;
365 *sbits = 0;
366 }
367}
368
David S. Miller5f803b52011-02-24 00:33:19 -0500369static struct hlist_head *policy_hash_bysel(struct net *net,
370 const struct xfrm_selector *sel,
371 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700372{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800373 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200374 unsigned int hash;
375 u8 dbits;
376 u8 sbits;
377
378 __get_hash_thresh(net, family, dir, &dbits, &sbits);
379 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700380
381 return (hash == hmask + 1 ?
Alexey Dobriyan11219942008-11-25 17:33:06 -0800382 &net->xfrm.policy_inexact[dir] :
383 net->xfrm.policy_bydst[dir].table + hash);
David S. Miller2518c7c2006-08-24 04:45:07 -0700384}
385
David S. Miller5f803b52011-02-24 00:33:19 -0500386static struct hlist_head *policy_hash_direct(struct net *net,
387 const xfrm_address_t *daddr,
388 const xfrm_address_t *saddr,
389 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700390{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800391 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200392 unsigned int hash;
393 u8 dbits;
394 u8 sbits;
395
396 __get_hash_thresh(net, family, dir, &dbits, &sbits);
397 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700398
Alexey Dobriyan11219942008-11-25 17:33:06 -0800399 return net->xfrm.policy_bydst[dir].table + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700400}
401
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200402static void xfrm_dst_hash_transfer(struct net *net,
403 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700404 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200405 unsigned int nhashmask,
406 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700407{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800408 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700409 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800410 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200411 u8 dbits;
412 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700413
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800414redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800415 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700416 unsigned int h;
417
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200418 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700419 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200420 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800421 if (!entry0) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800422 hlist_del(&pol->bydst);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800423 hlist_add_head(&pol->bydst, ndsttable+h);
424 h0 = h;
425 } else {
426 if (h != h0)
427 continue;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800428 hlist_del(&pol->bydst);
Ken Helias1d023282014-08-06 16:09:16 -0700429 hlist_add_behind(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800430 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800431 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800432 }
433 if (!hlist_empty(list)) {
434 entry0 = NULL;
435 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700436 }
437}
438
439static void xfrm_idx_hash_transfer(struct hlist_head *list,
440 struct hlist_head *nidxtable,
441 unsigned int nhashmask)
442{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800443 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700444 struct xfrm_policy *pol;
445
Sasha Levinb67bfe02013-02-27 17:06:00 -0800446 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700447 unsigned int h;
448
449 h = __idx_hash(pol->index, nhashmask);
450 hlist_add_head(&pol->byidx, nidxtable+h);
451 }
452}
453
454static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
455{
456 return ((old_hmask + 1) << 1) - 1;
457}
458
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800459static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700460{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800461 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700462 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
463 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800464 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700465 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700466 int i;
467
468 if (!ndst)
469 return;
470
Fan Du283bc9f2013-11-07 17:47:50 +0800471 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700472
473 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200474 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700475
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800476 net->xfrm.policy_bydst[dir].table = ndst;
477 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700478
Fan Du283bc9f2013-11-07 17:47:50 +0800479 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700480
David S. Miller44e36b42006-08-24 04:50:50 -0700481 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700482}
483
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800484static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700485{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800486 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700487 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
488 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800489 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700490 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700491 int i;
492
493 if (!nidx)
494 return;
495
Fan Du283bc9f2013-11-07 17:47:50 +0800496 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700497
498 for (i = hmask; i >= 0; i--)
499 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
500
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800501 net->xfrm.policy_byidx = nidx;
502 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700503
Fan Du283bc9f2013-11-07 17:47:50 +0800504 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700505
David S. Miller44e36b42006-08-24 04:50:50 -0700506 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700507}
508
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800509static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700510{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800511 unsigned int cnt = net->xfrm.policy_count[dir];
512 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700513
514 if (total)
515 *total += cnt;
516
517 if ((hmask + 1) < xfrm_policy_hashmax &&
518 cnt > hmask)
519 return 1;
520
521 return 0;
522}
523
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800524static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700525{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800526 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700527
528 if ((hmask + 1) < xfrm_policy_hashmax &&
529 total > hmask)
530 return 1;
531
532 return 0;
533}
534
Alexey Dobriyane0710412010-01-23 13:37:10 +0000535void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700536{
Fan Du283bc9f2013-11-07 17:47:50 +0800537 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000538 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
539 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
540 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
541 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
542 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
543 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
544 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700545 si->spdhmcnt = xfrm_policy_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800546 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700547}
548EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700549
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700550static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800551static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700552{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800553 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700554 int dir, total;
555
556 mutex_lock(&hash_resize_mutex);
557
558 total = 0;
559 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800560 if (xfrm_bydst_should_resize(net, dir, &total))
561 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700562 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800563 if (xfrm_byidx_should_resize(net, total))
564 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700565
566 mutex_unlock(&hash_resize_mutex);
567}
568
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200569static void xfrm_hash_rebuild(struct work_struct *work)
570{
571 struct net *net = container_of(work, struct net,
572 xfrm.policy_hthresh.work);
573 unsigned int hmask;
574 struct xfrm_policy *pol;
575 struct xfrm_policy *policy;
576 struct hlist_head *chain;
577 struct hlist_head *odst;
578 struct hlist_node *newpos;
579 int i;
580 int dir;
581 unsigned seq;
582 u8 lbits4, rbits4, lbits6, rbits6;
583
584 mutex_lock(&hash_resize_mutex);
585
586 /* read selector prefixlen thresholds */
587 do {
588 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
589
590 lbits4 = net->xfrm.policy_hthresh.lbits4;
591 rbits4 = net->xfrm.policy_hthresh.rbits4;
592 lbits6 = net->xfrm.policy_hthresh.lbits6;
593 rbits6 = net->xfrm.policy_hthresh.rbits6;
594 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
595
596 write_lock_bh(&net->xfrm.xfrm_policy_lock);
597
598 /* reset the bydst and inexact table in all directions */
599 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
600 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
601 hmask = net->xfrm.policy_bydst[dir].hmask;
602 odst = net->xfrm.policy_bydst[dir].table;
603 for (i = hmask; i >= 0; i--)
604 INIT_HLIST_HEAD(odst + i);
605 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
606 /* dir out => dst = remote, src = local */
607 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
608 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
609 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
610 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
611 } else {
612 /* dir in/fwd => dst = local, src = remote */
613 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
614 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
615 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
616 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
617 }
618 }
619
620 /* re-insert all policies by order of creation */
621 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
622 newpos = NULL;
623 chain = policy_hash_bysel(net, &policy->selector,
624 policy->family,
625 xfrm_policy_id2dir(policy->index));
626 hlist_for_each_entry(pol, chain, bydst) {
627 if (policy->priority >= pol->priority)
628 newpos = &pol->bydst;
629 else
630 break;
631 }
632 if (newpos)
633 hlist_add_behind(&policy->bydst, newpos);
634 else
635 hlist_add_head(&policy->bydst, chain);
636 }
637
638 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
639
640 mutex_unlock(&hash_resize_mutex);
641}
642
643void xfrm_policy_hash_rebuild(struct net *net)
644{
645 schedule_work(&net->xfrm.policy_hthresh.work);
646}
647EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649/* Generate new index... KAME seems to generate them ordered by cost
650 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf2013-11-07 17:47:48 +0800651static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 static u32 idx_generator;
654
655 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700656 struct hlist_head *list;
657 struct xfrm_policy *p;
658 u32 idx;
659 int found;
660
Fan Due682adf2013-11-07 17:47:48 +0800661 if (!index) {
662 idx = (idx_generator | dir);
663 idx_generator += 8;
664 } else {
665 idx = index;
666 index = 0;
667 }
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (idx == 0)
670 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800671 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700672 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800673 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700674 if (p->index == idx) {
675 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700679 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return idx;
681 }
682}
683
David S. Miller2518c7c2006-08-24 04:45:07 -0700684static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
685{
686 u32 *p1 = (u32 *) s1;
687 u32 *p2 = (u32 *) s2;
688 int len = sizeof(struct xfrm_selector) / sizeof(u32);
689 int i;
690
691 for (i = 0; i < len; i++) {
692 if (p1[i] != p2[i])
693 return 1;
694 }
695
696 return 0;
697}
698
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100699static void xfrm_policy_requeue(struct xfrm_policy *old,
700 struct xfrm_policy *new)
701{
702 struct xfrm_policy_queue *pq = &old->polq;
703 struct sk_buff_head list;
704
705 __skb_queue_head_init(&list);
706
707 spin_lock_bh(&pq->hold_queue.lock);
708 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200709 if (del_timer(&pq->hold_timer))
710 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100711 spin_unlock_bh(&pq->hold_queue.lock);
712
713 if (skb_queue_empty(&list))
714 return;
715
716 pq = &new->polq;
717
718 spin_lock_bh(&pq->hold_queue.lock);
719 skb_queue_splice(&list, &pq->hold_queue);
720 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200721 if (!mod_timer(&pq->hold_timer, jiffies))
722 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100723 spin_unlock_bh(&pq->hold_queue.lock);
724}
725
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100726static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
727 struct xfrm_policy *pol)
728{
729 u32 mark = policy->mark.v & policy->mark.m;
730
731 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
732 return true;
733
734 if ((mark & pol->mark.m) == pol->mark.v &&
735 policy->priority == pol->priority)
736 return true;
737
738 return false;
739}
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
742{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800743 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700744 struct xfrm_policy *pol;
745 struct xfrm_policy *delpol;
746 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800747 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Fan Du283bc9f2013-11-07 17:47:50 +0800749 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800750 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700751 delpol = NULL;
752 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800753 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800754 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700755 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100756 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800757 xfrm_sec_ctx_match(pol->security, policy->security) &&
758 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (excl) {
Fan Du283bc9f2013-11-07 17:47:50 +0800760 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return -EEXIST;
762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 delpol = pol;
764 if (policy->priority > pol->priority)
765 continue;
766 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800767 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 continue;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (delpol)
771 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773 if (newpos)
Ken Helias1d023282014-08-06 16:09:16 -0700774 hlist_add_behind(&policy->bydst, newpos);
David S. Miller2518c7c2006-08-24 04:45:07 -0700775 else
776 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800778 net->xfrm.policy_count[dir]++;
Fan Duca925cf2014-01-18 09:55:27 +0800779 atomic_inc(&net->xfrm.flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800780
781 /* After previous checking, family can either be AF_INET or AF_INET6 */
782 if (policy->family == AF_INET)
783 rt_genid_bump_ipv4(net);
784 else
785 rt_genid_bump_ipv6(net);
786
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100787 if (delpol) {
788 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800789 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100790 }
Fan Due682adf2013-11-07 17:47:48 +0800791 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800792 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800793 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 policy->curlft.use_time = 0;
795 if (!mod_timer(&policy->timer, jiffies + HZ))
796 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800797 list_add(&policy->walk.all, &net->xfrm.policy_all);
Fan Du283bc9f2013-11-07 17:47:50 +0800798 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
David S. Miller9b78a822005-12-22 07:39:48 -0800800 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800802 else if (xfrm_bydst_should_resize(net, dir, NULL))
803 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return 0;
806}
807EXPORT_SYMBOL(xfrm_policy_insert);
808
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000809struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
810 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800811 struct xfrm_sec_ctx *ctx, int delete,
812 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
David S. Miller2518c7c2006-08-24 04:45:07 -0700814 struct xfrm_policy *pol, *ret;
815 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Eric Parisef41aaa2007-03-07 15:37:58 -0800817 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800818 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800819 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700820 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800821 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700822 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000823 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700824 !selector_cmp(sel, &pol->selector) &&
825 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700827 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700828 *err = security_xfrm_policy_delete(
829 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800830 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800831 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800832 return pol;
833 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800834 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700835 }
836 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 break;
838 }
839 }
Fan Du283bc9f2013-11-07 17:47:50 +0800840 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000842 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700843 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700844 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
Trent Jaegerdf718372005-12-13 23:12:27 -0800846EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000848struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
849 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
David S. Miller2518c7c2006-08-24 04:45:07 -0700851 struct xfrm_policy *pol, *ret;
852 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Herbert Xub5505c62007-05-14 02:15:47 -0700854 *err = -ENOENT;
855 if (xfrm_policy_id2dir(id) != dir)
856 return NULL;
857
Eric Parisef41aaa2007-03-07 15:37:58 -0800858 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800859 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800860 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700861 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800862 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000863 if (pol->type == type && pol->index == id &&
864 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700866 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700867 *err = security_xfrm_policy_delete(
868 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800869 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800870 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800871 return pol;
872 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800873 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700874 }
875 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 break;
877 }
878 }
Fan Du283bc9f2013-11-07 17:47:50 +0800879 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000881 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700882 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700883 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885EXPORT_SYMBOL(xfrm_policy_byid);
886
Joy Latten4aa2e622007-06-04 19:05:57 -0400887#ifdef CONFIG_SECURITY_NETWORK_XFRM
888static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900889xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Joy Latten4aa2e622007-06-04 19:05:57 -0400891 int dir, err = 0;
892
893 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
894 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400895 int i;
896
Sasha Levinb67bfe02013-02-27 17:06:00 -0800897 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800898 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400899 if (pol->type != type)
900 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700901 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400902 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900903 xfrm_audit_policy_delete(pol, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400904 return err;
905 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900906 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800907 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800908 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800909 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400910 bydst) {
911 if (pol->type != type)
912 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700913 err = security_xfrm_policy_delete(
914 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400915 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700916 xfrm_audit_policy_delete(pol, 0,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900917 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400918 return err;
919 }
920 }
921 }
922 }
923 return err;
924}
925#else
926static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900927xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400928{
929 return 0;
930}
931#endif
932
Tetsuo Handa2e710292014-04-22 21:48:30 +0900933int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400934{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000935 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Fan Du283bc9f2013-11-07 17:47:50 +0800937 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400938
Tetsuo Handa2e710292014-04-22 21:48:30 +0900939 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400940 if (err)
941 goto out;
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700944 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800945 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700946
947 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800948 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800949 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700950 if (pol->type != type)
951 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000952 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800953 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000954 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Tetsuo Handa2e710292014-04-22 21:48:30 +0900956 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -0600957
David S. Miller2518c7c2006-08-24 04:45:07 -0700958 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Fan Du283bc9f2013-11-07 17:47:50 +0800960 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700961 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700963
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800964 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700965 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800966 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800967 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700968 bydst) {
969 if (pol->type != type)
970 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000971 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800972 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000973 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700974
Tetsuo Handa2e710292014-04-22 21:48:30 +0900975 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700976 xfrm_policy_kill(pol);
977
Fan Du283bc9f2013-11-07 17:47:50 +0800978 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700979 goto again2;
980 }
981 }
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000984 if (!cnt)
985 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400986out:
Fan Du283bc9f2013-11-07 17:47:50 +0800987 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400988 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990EXPORT_SYMBOL(xfrm_policy_flush);
991
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800992int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800993 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 void *data)
995{
Herbert Xu12a169e2008-10-01 07:03:24 -0700996 struct xfrm_policy *pol;
997 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -0800998 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Timo Teras4c563f72008-02-28 21:31:08 -08001000 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1001 walk->type != XFRM_POLICY_TYPE_ANY)
1002 return -EINVAL;
1003
Herbert Xu12a169e2008-10-01 07:03:24 -07001004 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001005 return 0;
1006
Fan Du283bc9f2013-11-07 17:47:50 +08001007 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001008 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001009 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001010 else
1011 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001012 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001013 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001014 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001015 pol = container_of(x, struct xfrm_policy, walk);
1016 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1017 walk->type != pol->type)
1018 continue;
1019 error = func(pol, xfrm_policy_id2dir(pol->index),
1020 walk->seq, data);
1021 if (error) {
1022 list_move_tail(&walk->walk.all, &x->all);
1023 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001024 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001025 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001027 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001028 error = -ENOENT;
1029 goto out;
1030 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001031 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032out:
Fan Du283bc9f2013-11-07 17:47:50 +08001033 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return error;
1035}
1036EXPORT_SYMBOL(xfrm_policy_walk);
1037
Herbert Xu12a169e2008-10-01 07:03:24 -07001038void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1039{
1040 INIT_LIST_HEAD(&walk->walk.all);
1041 walk->walk.dead = 1;
1042 walk->type = type;
1043 walk->seq = 0;
1044}
1045EXPORT_SYMBOL(xfrm_policy_walk_init);
1046
Fan Du283bc9f2013-11-07 17:47:50 +08001047void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001048{
1049 if (list_empty(&walk->walk.all))
1050 return;
1051
Fan Du283bc9f2013-11-07 17:47:50 +08001052 write_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001053 list_del(&walk->walk.all);
Fan Du283bc9f2013-11-07 17:47:50 +08001054 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001055}
1056EXPORT_SYMBOL(xfrm_policy_walk_done);
1057
James Morris134b0fc2006-10-05 15:42:27 -05001058/*
1059 * Find policy to apply to this flow.
1060 *
1061 * Returns 0 if policy found, else an -errno.
1062 */
David S. Millerf299d552011-02-24 01:23:30 -05001063static int xfrm_policy_match(const struct xfrm_policy *pol,
1064 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -07001065 u8 type, u16 family, int dir)
1066{
David S. Millerf299d552011-02-24 01:23:30 -05001067 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001068 int ret = -ESRCH;
1069 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001070
1071 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -05001072 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001073 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001074 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001075
1076 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001077 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001078 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001079 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001080
James Morris134b0fc2006-10-05 15:42:27 -05001081 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001082}
1083
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001084static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001085 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001086 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
James Morris134b0fc2006-10-05 15:42:27 -05001088 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001089 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -05001090 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -07001091 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -07001092 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001093
1094 daddr = xfrm_flowi_daddr(fl, family);
1095 saddr = xfrm_flowi_saddr(fl, family);
1096 if (unlikely(!daddr || !saddr))
1097 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Fan Du283bc9f2013-11-07 17:47:50 +08001099 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001100 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001101 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001102 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001103 err = xfrm_policy_match(pol, fl, type, family, dir);
1104 if (err) {
1105 if (err == -ESRCH)
1106 continue;
1107 else {
1108 ret = ERR_PTR(err);
1109 goto fail;
1110 }
1111 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001112 ret = pol;
1113 priority = ret->priority;
1114 break;
1115 }
1116 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001117 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001118 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001119 err = xfrm_policy_match(pol, fl, type, family, dir);
1120 if (err) {
1121 if (err == -ESRCH)
1122 continue;
1123 else {
1124 ret = ERR_PTR(err);
1125 goto fail;
1126 }
1127 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001128 ret = pol;
1129 break;
1130 }
1131 }
David S. Milleracba48e2006-08-25 15:46:46 -07001132 if (ret)
1133 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -05001134fail:
Fan Du283bc9f2013-11-07 17:47:50 +08001135 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001136
David S. Miller2518c7c2006-08-24 04:45:07 -07001137 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001138}
1139
Timo Teräs80c802f2010-04-07 00:30:05 +00001140static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001141__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001142{
1143#ifdef CONFIG_XFRM_SUB_POLICY
1144 struct xfrm_policy *pol;
1145
1146 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1147 if (pol != NULL)
1148 return pol;
1149#endif
1150 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1151}
1152
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001153static int flow_to_policy_dir(int dir)
1154{
1155 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1156 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1157 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1158 return dir;
1159
1160 switch (dir) {
1161 default:
1162 case FLOW_DIR_IN:
1163 return XFRM_POLICY_IN;
1164 case FLOW_DIR_OUT:
1165 return XFRM_POLICY_OUT;
1166 case FLOW_DIR_FWD:
1167 return XFRM_POLICY_FWD;
1168 }
1169}
1170
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001171static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001172xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001173 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001174{
1175 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001176
1177 if (old_obj)
1178 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001179
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001180 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001181 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001182 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001183
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001184 /* Resolver returns two references:
1185 * one for cache and one for caller of flow_cache_lookup() */
1186 xfrm_pol_hold(pol);
1187
1188 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
Trent Jaegerdf718372005-12-13 23:12:27 -08001191static inline int policy_to_flow_dir(int dir)
1192{
1193 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001194 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1195 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1196 return dir;
1197 switch (dir) {
1198 default:
1199 case XFRM_POLICY_IN:
1200 return FLOW_DIR_IN;
1201 case XFRM_POLICY_OUT:
1202 return FLOW_DIR_OUT;
1203 case XFRM_POLICY_FWD:
1204 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001205 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001206}
1207
David S. Millerdee9f4b2011-02-22 18:44:31 -08001208static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir,
1209 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
1211 struct xfrm_policy *pol;
Fan Du283bc9f2013-11-07 17:47:50 +08001212 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Fan Du283bc9f2013-11-07 17:47:50 +08001214 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if ((pol = sk->sk_policy[dir]) != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001216 bool match = xfrm_selector_match(&pol->selector, fl,
1217 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001218 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001219
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001220 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001221 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1222 pol = NULL;
1223 goto out;
1224 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001225 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001226 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001227 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001228 if (!err)
1229 xfrm_pol_hold(pol);
1230 else if (err == -ESRCH)
1231 pol = NULL;
1232 else
1233 pol = ERR_PTR(err);
1234 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 pol = NULL;
1236 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001237out:
Fan Du283bc9f2013-11-07 17:47:50 +08001238 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return pol;
1240}
1241
1242static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1243{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001244 struct net *net = xp_net(pol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001245 struct hlist_head *chain = policy_hash_bysel(net, &pol->selector,
David S. Miller2518c7c2006-08-24 04:45:07 -07001246 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001247
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001248 list_add(&pol->walk.all, &net->xfrm.policy_all);
David S. Miller2518c7c2006-08-24 04:45:07 -07001249 hlist_add_head(&pol->bydst, chain);
Alexey Dobriyane92303f2008-11-25 17:32:41 -08001250 hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001251 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001253
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001254 if (xfrm_bydst_should_resize(net, dir, NULL))
1255 schedule_work(&net->xfrm.policy_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
1258static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1259 int dir)
1260{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001261 struct net *net = xp_net(pol);
1262
David S. Miller2518c7c2006-08-24 04:45:07 -07001263 if (hlist_unhashed(&pol->bydst))
1264 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Steffen Klassert3a9016f2014-02-19 10:07:34 +01001266 hlist_del_init(&pol->bydst);
David S. Miller2518c7c2006-08-24 04:45:07 -07001267 hlist_del(&pol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -07001268 list_del(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001269 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001270
1271 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
Herbert Xu4666faa2005-06-18 22:43:22 -07001274int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Fan Du283bc9f2013-11-07 17:47:50 +08001276 struct net *net = xp_net(pol);
1277
1278 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 pol = __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001280 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001285 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
David S. Millera70fcb02006-03-20 19:18:52 -08001287EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1290{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001291 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 struct xfrm_policy *old_pol;
1293
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001294#ifdef CONFIG_XFRM_SUB_POLICY
1295 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1296 return -EINVAL;
1297#endif
1298
Fan Du283bc9f2013-11-07 17:47:50 +08001299 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 old_pol = sk->sk_policy[dir];
1301 sk->sk_policy[dir] = pol;
1302 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001303 pol->curlft.add_time = get_seconds();
Fan Due682adf2013-11-07 17:47:48 +08001304 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1306 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001307 if (old_pol) {
1308 if (pol)
1309 xfrm_policy_requeue(old_pol, pol);
1310
Timo Teräsea2dea92010-03-31 00:17:05 +00001311 /* Unlinking succeeds always. This is the only function
1312 * allowed to delete or replace socket policy.
1313 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001315 }
Fan Du283bc9f2013-11-07 17:47:50 +08001316 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 if (old_pol) {
1319 xfrm_policy_kill(old_pol);
1320 }
1321 return 0;
1322}
1323
David S. Millerd3e40a92011-02-24 01:25:41 -05001324static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001326 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001327 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 if (newp) {
1330 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001331 if (security_xfrm_policy_clone(old->security,
1332 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001333 kfree(newp);
1334 return NULL; /* ENOMEM */
1335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 newp->lft = old->lft;
1337 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001338 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 newp->action = old->action;
1340 newp->flags = old->flags;
1341 newp->xfrm_nr = old->xfrm_nr;
1342 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001343 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 memcpy(newp->xfrm_vec, old->xfrm_vec,
1345 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Fan Du283bc9f2013-11-07 17:47:50 +08001346 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001348 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 xfrm_pol_put(newp);
1350 }
1351 return newp;
1352}
1353
1354int __xfrm_sk_clone_policy(struct sock *sk)
1355{
1356 struct xfrm_policy *p0 = sk->sk_policy[0],
1357 *p1 = sk->sk_policy[1];
1358
1359 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1360 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1361 return -ENOMEM;
1362 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1363 return -ENOMEM;
1364 return 0;
1365}
1366
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001367static int
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001368xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001369 unsigned short family)
1370{
1371 int err;
1372 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1373
1374 if (unlikely(afinfo == NULL))
1375 return -EINVAL;
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001376 err = afinfo->get_saddr(net, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001377 xfrm_policy_put_afinfo(afinfo);
1378 return err;
1379}
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381/* Resolve list of templates for the flow, given policy. */
1382
1383static int
David S. Millera6c2e612011-02-22 18:35:39 -08001384xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1385 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001387 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 int nx;
1389 int i, error;
1390 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1391 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001392 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Weilong Chen9b7a7872013-12-24 09:43:46 +08001394 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 struct xfrm_state *x;
1396 xfrm_address_t *remote = daddr;
1397 xfrm_address_t *local = saddr;
1398 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1399
Joakim Koskela48b8d782007-07-26 00:08:42 -07001400 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1401 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 remote = &tmpl->id.daddr;
1403 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001404 if (xfrm_addr_any(local, tmpl->encap_family)) {
1405 error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001406 if (error)
1407 goto fail;
1408 local = &tmp;
1409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411
1412 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1413
1414 if (x && x->km.state == XFRM_STATE_VALID) {
1415 xfrm[nx++] = x;
1416 daddr = remote;
1417 saddr = local;
1418 continue;
1419 }
1420 if (x) {
1421 error = (x->km.state == XFRM_STATE_ERROR ?
1422 -EINVAL : -EAGAIN);
1423 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001424 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001425 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 if (!tmpl->optional)
1429 goto fail;
1430 }
1431 return nx;
1432
1433fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001434 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 xfrm_state_put(xfrm[nx]);
1436 return error;
1437}
1438
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001439static int
David S. Millera6c2e612011-02-22 18:35:39 -08001440xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1441 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001442{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001443 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1444 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001445 int cnx = 0;
1446 int error;
1447 int ret;
1448 int i;
1449
1450 for (i = 0; i < npols; i++) {
1451 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1452 error = -ENOBUFS;
1453 goto fail;
1454 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001455
1456 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001457 if (ret < 0) {
1458 error = ret;
1459 goto fail;
1460 } else
1461 cnx += ret;
1462 }
1463
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001464 /* found states are sorted for outbound processing */
1465 if (npols > 1)
1466 xfrm_state_sort(xfrm, tpp, cnx, family);
1467
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001468 return cnx;
1469
1470 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001471 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001472 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001473 return error;
1474
1475}
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477/* Check that the bundle accepts the flow and its components are
1478 * still valid.
1479 */
1480
David S. Miller05d84022011-02-22 17:47:10 -08001481static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001482{
1483 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1484 int tos;
1485
1486 if (!afinfo)
1487 return -EINVAL;
1488
1489 tos = afinfo->get_tos(fl);
1490
1491 xfrm_policy_put_afinfo(afinfo);
1492
1493 return tos;
1494}
1495
Timo Teräs80c802f2010-04-07 00:30:05 +00001496static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1497{
1498 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1499 struct dst_entry *dst = &xdst->u.dst;
1500
1501 if (xdst->route == NULL) {
1502 /* Dummy bundle - if it has xfrms we were not
1503 * able to build bundle as template resolution failed.
1504 * It means we need to try again resolving. */
1505 if (xdst->num_xfrms > 0)
1506 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001507 } else if (dst->flags & DST_XFRM_QUEUE) {
1508 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001509 } else {
1510 /* Real bundle */
1511 if (stale_bundle(dst))
1512 return NULL;
1513 }
1514
1515 dst_hold(dst);
1516 return flo;
1517}
1518
1519static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1520{
1521 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1522 struct dst_entry *dst = &xdst->u.dst;
1523
1524 if (!xdst->route)
1525 return 0;
1526 if (stale_bundle(dst))
1527 return 0;
1528
1529 return 1;
1530}
1531
1532static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1533{
1534 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1535 struct dst_entry *dst = &xdst->u.dst;
1536
1537 dst_free(dst);
1538}
1539
1540static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1541 .get = xfrm_bundle_flo_get,
1542 .check = xfrm_bundle_flo_check,
1543 .delete = xfrm_bundle_flo_delete,
1544};
1545
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001546static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001547{
1548 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001549 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001550 struct xfrm_dst *xdst;
1551
1552 if (!afinfo)
1553 return ERR_PTR(-EINVAL);
1554
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001555 switch (family) {
1556 case AF_INET:
1557 dst_ops = &net->xfrm.xfrm4_dst_ops;
1558 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001559#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001560 case AF_INET6:
1561 dst_ops = &net->xfrm.xfrm6_dst_ops;
1562 break;
1563#endif
1564 default:
1565 BUG();
1566 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001567 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001568
Madalin Bucurd4cae562011-09-26 07:04:36 +00001569 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001570 struct dst_entry *dst = &xdst->u.dst;
1571
1572 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001573 xdst->flo.ops = &xfrm_bundle_fc_ops;
Patrick McHardy9d7b0fc2012-08-20 02:56:56 -07001574 if (afinfo->init_dst)
1575 afinfo->init_dst(net, xdst);
Madalin Bucurd4cae562011-09-26 07:04:36 +00001576 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001577 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001578
Madalin Bucurd4cae562011-09-26 07:04:36 +00001579 xfrm_policy_put_afinfo(afinfo);
1580
Herbert Xu25ee3282007-12-11 09:32:34 -08001581 return xdst;
1582}
1583
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001584static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1585 int nfheader_len)
1586{
1587 struct xfrm_policy_afinfo *afinfo =
1588 xfrm_policy_get_afinfo(dst->ops->family);
1589 int err;
1590
1591 if (!afinfo)
1592 return -EINVAL;
1593
1594 err = afinfo->init_path(path, dst, nfheader_len);
1595
1596 xfrm_policy_put_afinfo(afinfo);
1597
1598 return err;
1599}
1600
Herbert Xu87c1e122010-03-02 02:51:56 +00001601static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001602 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001603{
1604 struct xfrm_policy_afinfo *afinfo =
1605 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1606 int err;
1607
1608 if (!afinfo)
1609 return -EINVAL;
1610
Herbert Xu87c1e122010-03-02 02:51:56 +00001611 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001612
1613 xfrm_policy_put_afinfo(afinfo);
1614
1615 return err;
1616}
1617
Timo Teräs80c802f2010-04-07 00:30:05 +00001618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1620 * all the metrics... Shortly, bundle a bundle.
1621 */
1622
Herbert Xu25ee3282007-12-11 09:32:34 -08001623static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1624 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001625 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001626 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001628 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001629 unsigned long now = jiffies;
1630 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001631 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001632 struct dst_entry *dst_prev = NULL;
1633 struct dst_entry *dst0 = NULL;
1634 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001636 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001637 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001638 int trailer_len = 0;
1639 int tos;
1640 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001641 xfrm_address_t saddr, daddr;
1642
1643 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001644
1645 tos = xfrm_get_tos(fl, family);
1646 err = tos;
1647 if (tos < 0)
1648 goto put_states;
1649
1650 dst_hold(dst);
1651
1652 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001653 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001654 struct dst_entry *dst1 = &xdst->u.dst;
1655
1656 err = PTR_ERR(xdst);
1657 if (IS_ERR(xdst)) {
1658 dst_release(dst);
1659 goto put_states;
1660 }
1661
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001662 if (xfrm[i]->sel.family == AF_UNSPEC) {
1663 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1664 xfrm_af2proto(family));
1665 if (!inner_mode) {
1666 err = -EAFNOSUPPORT;
1667 dst_release(dst);
1668 goto put_states;
1669 }
1670 } else
1671 inner_mode = xfrm[i]->inner_mode;
1672
Herbert Xu25ee3282007-12-11 09:32:34 -08001673 if (!dst_prev)
1674 dst0 = dst1;
1675 else {
1676 dst_prev->child = dst_clone(dst1);
1677 dst1->flags |= DST_NOHASH;
1678 }
1679
1680 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001681 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001682
1683 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1684 family = xfrm[i]->props.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001685 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1686 family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001687 err = PTR_ERR(dst);
1688 if (IS_ERR(dst))
1689 goto put_states;
1690 } else
1691 dst_hold(dst);
1692
1693 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001694 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001695
David S. Millerf5b0a872012-07-19 12:31:33 -07001696 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001697 dst1->flags |= DST_HOST;
1698 dst1->lastuse = now;
1699
1700 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001701 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001702
1703 dst1->next = dst_prev;
1704 dst_prev = dst1;
1705
1706 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001707 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1708 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001709 trailer_len += xfrm[i]->props.trailer_len;
1710 }
1711
1712 dst_prev->child = dst;
1713 dst0->path = dst;
1714
1715 err = -ENODEV;
1716 dev = dst->dev;
1717 if (!dev)
1718 goto free_dst;
1719
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001720 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001721 xfrm_init_pmtu(dst_prev);
1722
1723 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1724 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1725
Herbert Xu87c1e122010-03-02 02:51:56 +00001726 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001727 if (err)
1728 goto free_dst;
1729
1730 dst_prev->header_len = header_len;
1731 dst_prev->trailer_len = trailer_len;
1732 header_len -= xdst->u.dst.xfrm->props.header_len;
1733 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1734 }
1735
1736out:
1737 return dst0;
1738
1739put_states:
1740 for (; i < nx; i++)
1741 xfrm_state_put(xfrm[i]);
1742free_dst:
1743 if (dst0)
1744 dst_free(dst0);
1745 dst0 = ERR_PTR(err);
1746 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
Ying Xueda7c2242014-01-08 10:26:39 +08001749#ifdef CONFIG_XFRM_SUB_POLICY
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001750static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001751{
1752 if (!*target) {
1753 *target = kmalloc(size, GFP_ATOMIC);
1754 if (!*target)
1755 return -ENOMEM;
1756 }
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001757
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001758 memcpy(*target, src, size);
1759 return 0;
1760}
Ying Xueda7c2242014-01-08 10:26:39 +08001761#endif
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001762
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001763static int xfrm_dst_update_parent(struct dst_entry *dst,
1764 const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001765{
1766#ifdef CONFIG_XFRM_SUB_POLICY
1767 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1768 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1769 sel, sizeof(*sel));
1770#else
1771 return 0;
1772#endif
1773}
1774
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001775static int xfrm_dst_update_origin(struct dst_entry *dst,
1776 const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001777{
1778#ifdef CONFIG_XFRM_SUB_POLICY
1779 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1780 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1781#else
1782 return 0;
1783#endif
1784}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
David S. Miller73ff93c2011-02-22 18:33:42 -08001786static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001787 struct xfrm_policy **pols,
1788 int *num_pols, int *num_xfrms)
1789{
1790 int i;
1791
1792 if (*num_pols == 0 || !pols[0]) {
1793 *num_pols = 0;
1794 *num_xfrms = 0;
1795 return 0;
1796 }
1797 if (IS_ERR(pols[0]))
1798 return PTR_ERR(pols[0]);
1799
1800 *num_xfrms = pols[0]->xfrm_nr;
1801
1802#ifdef CONFIG_XFRM_SUB_POLICY
1803 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1804 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1805 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1806 XFRM_POLICY_TYPE_MAIN,
1807 fl, family,
1808 XFRM_POLICY_OUT);
1809 if (pols[1]) {
1810 if (IS_ERR(pols[1])) {
1811 xfrm_pols_put(pols, *num_pols);
1812 return PTR_ERR(pols[1]);
1813 }
Weilong Chen02d08922013-12-24 09:43:48 +08001814 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001815 (*num_xfrms) += pols[1]->xfrm_nr;
1816 }
1817 }
1818#endif
1819 for (i = 0; i < *num_pols; i++) {
1820 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1821 *num_xfrms = -1;
1822 break;
1823 }
1824 }
1825
1826 return 0;
1827
1828}
1829
1830static struct xfrm_dst *
1831xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001832 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001833 struct dst_entry *dst_orig)
1834{
1835 struct net *net = xp_net(pols[0]);
1836 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1837 struct dst_entry *dst;
1838 struct xfrm_dst *xdst;
1839 int err;
1840
1841 /* Try to instantiate a bundle */
1842 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001843 if (err <= 0) {
1844 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001845 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1846 return ERR_PTR(err);
1847 }
1848
1849 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1850 if (IS_ERR(dst)) {
1851 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1852 return ERR_CAST(dst);
1853 }
1854
1855 xdst = (struct xfrm_dst *)dst;
1856 xdst->num_xfrms = err;
1857 if (num_pols > 1)
1858 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1859 else
1860 err = xfrm_dst_update_origin(dst, fl);
1861 if (unlikely(err)) {
1862 dst_free(dst);
1863 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1864 return ERR_PTR(err);
1865 }
1866
1867 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001868 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001869 xdst->policy_genid = atomic_read(&pols[0]->genid);
1870
1871 return xdst;
1872}
1873
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001874static void xfrm_policy_queue_process(unsigned long arg)
1875{
1876 int err = 0;
1877 struct sk_buff *skb;
1878 struct sock *sk;
1879 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001880 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1881 struct xfrm_policy_queue *pq = &pol->polq;
1882 struct flowi fl;
1883 struct sk_buff_head list;
1884
1885 spin_lock(&pq->hold_queue.lock);
1886 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001887 if (!skb) {
1888 spin_unlock(&pq->hold_queue.lock);
1889 goto out;
1890 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001891 dst = skb_dst(skb);
1892 sk = skb->sk;
1893 xfrm_decode_session(skb, &fl, dst->ops->family);
1894 spin_unlock(&pq->hold_queue.lock);
1895
1896 dst_hold(dst->path);
1897 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1898 sk, 0);
1899 if (IS_ERR(dst))
1900 goto purge_queue;
1901
1902 if (dst->flags & DST_XFRM_QUEUE) {
1903 dst_release(dst);
1904
1905 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1906 goto purge_queue;
1907
1908 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001909 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1910 xfrm_pol_hold(pol);
1911 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001912 }
1913
1914 dst_release(dst);
1915
1916 __skb_queue_head_init(&list);
1917
1918 spin_lock(&pq->hold_queue.lock);
1919 pq->timeout = 0;
1920 skb_queue_splice_init(&pq->hold_queue, &list);
1921 spin_unlock(&pq->hold_queue.lock);
1922
1923 while (!skb_queue_empty(&list)) {
1924 skb = __skb_dequeue(&list);
1925
1926 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1927 dst_hold(skb_dst(skb)->path);
1928 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1929 &fl, skb->sk, 0);
1930 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001931 kfree_skb(skb);
1932 continue;
1933 }
1934
1935 nf_reset(skb);
1936 skb_dst_drop(skb);
1937 skb_dst_set(skb, dst);
1938
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001939 err = dst_output(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001940 }
1941
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001942out:
1943 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001944 return;
1945
1946purge_queue:
1947 pq->timeout = 0;
1948 xfrm_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001949 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001950}
1951
Eric Dumazetaad88722014-04-15 13:47:15 -04001952static int xdst_queue_output(struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001953{
1954 unsigned long sched_next;
1955 struct dst_entry *dst = skb_dst(skb);
1956 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001957 struct xfrm_policy *pol = xdst->pols[0];
1958 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001959 const struct sk_buff *fclone = skb + 1;
1960
1961 if (unlikely(skb->fclone == SKB_FCLONE_ORIG &&
1962 fclone->fclone == SKB_FCLONE_CLONE)) {
1963 kfree_skb(skb);
1964 return 0;
1965 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001966
1967 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1968 kfree_skb(skb);
1969 return -EAGAIN;
1970 }
1971
1972 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001973
1974 spin_lock_bh(&pq->hold_queue.lock);
1975
1976 if (!pq->timeout)
1977 pq->timeout = XFRM_QUEUE_TMO_MIN;
1978
1979 sched_next = jiffies + pq->timeout;
1980
1981 if (del_timer(&pq->hold_timer)) {
1982 if (time_before(pq->hold_timer.expires, sched_next))
1983 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001984 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001985 }
1986
1987 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001988 if (!mod_timer(&pq->hold_timer, sched_next))
1989 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001990
1991 spin_unlock_bh(&pq->hold_queue.lock);
1992
1993 return 0;
1994}
1995
1996static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
1997 struct dst_entry *dst,
1998 const struct flowi *fl,
1999 int num_xfrms,
2000 u16 family)
2001{
2002 int err;
2003 struct net_device *dev;
2004 struct dst_entry *dst1;
2005 struct xfrm_dst *xdst;
2006
2007 xdst = xfrm_alloc_dst(net, family);
2008 if (IS_ERR(xdst))
2009 return xdst;
2010
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002011 if (net->xfrm.sysctl_larval_drop || num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002012 return xdst;
2013
2014 dst1 = &xdst->u.dst;
2015 dst_hold(dst);
2016 xdst->route = dst;
2017
2018 dst_copy_metrics(dst1, dst);
2019
2020 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2021 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2022 dst1->lastuse = jiffies;
2023
2024 dst1->input = dst_discard;
2025 dst1->output = xdst_queue_output;
2026
2027 dst_hold(dst);
2028 dst1->child = dst;
2029 dst1->path = dst;
2030
2031 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2032
2033 err = -ENODEV;
2034 dev = dst->dev;
2035 if (!dev)
2036 goto free_dst;
2037
2038 err = xfrm_fill_dst(xdst, dev, fl);
2039 if (err)
2040 goto free_dst;
2041
2042out:
2043 return xdst;
2044
2045free_dst:
2046 dst_release(dst1);
2047 xdst = ERR_PTR(err);
2048 goto out;
2049}
2050
Timo Teräs80c802f2010-04-07 00:30:05 +00002051static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08002052xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00002053 struct flow_cache_object *oldflo, void *ctx)
2054{
2055 struct dst_entry *dst_orig = (struct dst_entry *)ctx;
2056 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2057 struct xfrm_dst *xdst, *new_xdst;
2058 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2059
2060 /* Check if the policies from old bundle are usable */
2061 xdst = NULL;
2062 if (oldflo) {
2063 xdst = container_of(oldflo, struct xfrm_dst, flo);
2064 num_pols = xdst->num_pols;
2065 num_xfrms = xdst->num_xfrms;
2066 pol_dead = 0;
2067 for (i = 0; i < num_pols; i++) {
2068 pols[i] = xdst->pols[i];
2069 pol_dead |= pols[i]->walk.dead;
2070 }
2071 if (pol_dead) {
2072 dst_free(&xdst->u.dst);
2073 xdst = NULL;
2074 num_pols = 0;
2075 num_xfrms = 0;
2076 oldflo = NULL;
2077 }
2078 }
2079
2080 /* Resolve policies to use if we couldn't get them from
2081 * previous cache entry */
2082 if (xdst == NULL) {
2083 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00002084 pols[0] = __xfrm_policy_lookup(net, fl, family,
2085 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00002086 err = xfrm_expand_policies(fl, family, pols,
2087 &num_pols, &num_xfrms);
2088 if (err < 0)
2089 goto inc_error;
2090 if (num_pols == 0)
2091 return NULL;
2092 if (num_xfrms <= 0)
2093 goto make_dummy_bundle;
2094 }
2095
2096 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family, dst_orig);
2097 if (IS_ERR(new_xdst)) {
2098 err = PTR_ERR(new_xdst);
2099 if (err != -EAGAIN)
2100 goto error;
2101 if (oldflo == NULL)
2102 goto make_dummy_bundle;
2103 dst_hold(&xdst->u.dst);
2104 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00002105 } else if (new_xdst == NULL) {
2106 num_xfrms = 0;
2107 if (oldflo == NULL)
2108 goto make_dummy_bundle;
2109 xdst->num_xfrms = 0;
2110 dst_hold(&xdst->u.dst);
2111 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002112 }
2113
2114 /* Kill the previous bundle */
2115 if (xdst) {
2116 /* The policies were stolen for newly generated bundle */
2117 xdst->num_pols = 0;
2118 dst_free(&xdst->u.dst);
2119 }
2120
2121 /* Flow cache does not have reference, it dst_free()'s,
2122 * but we do need to return one reference for original caller */
2123 dst_hold(&new_xdst->u.dst);
2124 return &new_xdst->flo;
2125
2126make_dummy_bundle:
2127 /* We found policies, but there's no bundles to instantiate:
2128 * either because the policy blocks, has no transformations or
2129 * we could not build template (no xfrm_states).*/
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002130 xdst = xfrm_create_dummy_bundle(net, dst_orig, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002131 if (IS_ERR(xdst)) {
2132 xfrm_pols_put(pols, num_pols);
2133 return ERR_CAST(xdst);
2134 }
2135 xdst->num_pols = num_pols;
2136 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002137 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002138
2139 dst_hold(&xdst->u.dst);
2140 return &xdst->flo;
2141
2142inc_error:
2143 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2144error:
2145 if (xdst != NULL)
2146 dst_free(&xdst->u.dst);
2147 else
2148 xfrm_pols_put(pols, num_pols);
2149 return ERR_PTR(err);
2150}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
David S. Miller2774c132011-03-01 14:59:04 -08002152static struct dst_entry *make_blackhole(struct net *net, u16 family,
2153 struct dst_entry *dst_orig)
2154{
2155 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2156 struct dst_entry *ret;
2157
2158 if (!afinfo) {
2159 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002160 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002161 } else {
2162 ret = afinfo->blackhole_route(net, dst_orig);
2163 }
2164 xfrm_policy_put_afinfo(afinfo);
2165
2166 return ret;
2167}
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169/* Main function: finds/creates a bundle for given flow.
2170 *
2171 * At the moment we eat a raw IP route. Mostly to speed up lookups
2172 * on interfaces with disabled IPsec.
2173 */
David S. Miller452edd52011-03-02 13:27:41 -08002174struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2175 const struct flowi *fl,
2176 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002178 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002179 struct flow_cache_object *flo;
2180 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002181 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002182 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002183 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002184 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002185
Timo Teräs80c802f2010-04-07 00:30:05 +00002186 dst = NULL;
2187 xdst = NULL;
2188 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002189
Thomas Graff7944fb2007-08-25 13:46:55 -07002190 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002191 num_pols = 1;
2192 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2193 err = xfrm_expand_policies(fl, family, pols,
2194 &num_pols, &num_xfrms);
2195 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002196 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002197
2198 if (num_pols) {
2199 if (num_xfrms <= 0) {
2200 drop_pols = num_pols;
2201 goto no_transform;
2202 }
2203
2204 xdst = xfrm_resolve_and_create_bundle(
2205 pols, num_pols, fl,
2206 family, dst_orig);
2207 if (IS_ERR(xdst)) {
2208 xfrm_pols_put(pols, num_pols);
2209 err = PTR_ERR(xdst);
2210 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002211 } else if (xdst == NULL) {
2212 num_xfrms = 0;
2213 drop_pols = num_pols;
2214 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002215 }
2216
Steffen Klassertb7eea452014-06-18 12:34:21 +02002217 dst_hold(&xdst->u.dst);
2218 xdst->u.dst.flags |= DST_NOCACHE;
Timo Teräs80c802f2010-04-07 00:30:05 +00002219 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002220 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Timo Teräs80c802f2010-04-07 00:30:05 +00002223 if (xdst == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002225 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002226 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002227 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Timo Teräs80c802f2010-04-07 00:30:05 +00002229 flo = flow_cache_lookup(net, fl, family, dir,
2230 xfrm_bundle_lookup, dst_orig);
2231 if (flo == NULL)
2232 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002233 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002234 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002235 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002236 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002237 xdst = container_of(flo, struct xfrm_dst, flo);
2238
2239 num_pols = xdst->num_pols;
2240 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002241 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002242 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 }
2244
Timo Teräs80c802f2010-04-07 00:30:05 +00002245 dst = &xdst->u.dst;
2246 if (route == NULL && num_xfrms > 0) {
2247 /* The only case when xfrm_bundle_lookup() returns a
2248 * bundle with null route, is when the template could
2249 * not be resolved. It means policies are there, but
2250 * bundle could not be created, since we don't yet
2251 * have the xfrm_state's. We need to wait for KM to
2252 * negotiate new SA's or bail out with error.*/
2253 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002254 dst_release(dst);
Timo Terasa1aa3482010-05-15 23:49:26 -07002255 xfrm_pols_put(pols, drop_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002256 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
David S. Miller2774c132011-03-01 14:59:04 -08002257
David S. Miller452edd52011-03-02 13:27:41 -08002258 return make_blackhole(net, family, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002259 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002260
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002261 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002262
2263 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2264 goto error;
2265 }
2266
2267no_transform:
2268 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002269 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Timo Teräs80c802f2010-04-07 00:30:05 +00002271 if ((flags & XFRM_LOOKUP_ICMP) &&
2272 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2273 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002274 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002275 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002276
Timo Teräs80c802f2010-04-07 00:30:05 +00002277 for (i = 0; i < num_pols; i++)
2278 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002279
Timo Teräs80c802f2010-04-07 00:30:05 +00002280 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002282 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002283 err = -EPERM;
2284 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002285 } else if (num_xfrms > 0) {
2286 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002287 dst_release(dst_orig);
2288 } else {
2289 /* Flow passes untransformed */
2290 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002291 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002293ok:
2294 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002295 if (dst && dst->xfrm &&
2296 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2297 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002298 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Timo Teräs80c802f2010-04-07 00:30:05 +00002300nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002301 if (!(flags & XFRM_LOOKUP_ICMP)) {
2302 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002303 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002304 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002305 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002307 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002308dropdst:
2309 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002310 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002311 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313EXPORT_SYMBOL(xfrm_lookup);
2314
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002315static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002316xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002317{
2318 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002319
2320 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2321 return 0;
2322 x = skb->sp->xvec[idx];
2323 if (!x->type->reject)
2324 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002325 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002326}
2327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328/* When skb is transformed back to its "native" form, we have to
2329 * check policy restrictions. At the moment we make this in maximally
2330 * stupid way. Shame on me. :-) Of course, connected sockets must
2331 * have policy cached at them.
2332 */
2333
2334static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002335xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 unsigned short family)
2337{
2338 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002339 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return x->id.proto == tmpl->id.proto &&
2341 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2342 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2343 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002344 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002345 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002346 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2347 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002350/*
2351 * 0 or more than 0 is returned when validation is succeeded (either bypass
2352 * because of optional transport mode, or next index of the mathced secpath
2353 * state with the template.
2354 * -1 is returned when no matching template is found.
2355 * Otherwise "-2 - errored_index" is returned.
2356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002358xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 unsigned short family)
2360{
2361 int idx = start;
2362
2363 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002364 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 return start;
2366 } else
2367 start = -1;
2368 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002369 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002371 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2372 if (start == -1)
2373 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
2377 return start;
2378}
2379
Herbert Xud5422ef2007-12-12 10:44:16 -08002380int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2381 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
2383 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002384 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
2386 if (unlikely(afinfo == NULL))
2387 return -EAFNOSUPPORT;
2388
Herbert Xud5422ef2007-12-12 10:44:16 -08002389 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002390 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002392 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393}
Herbert Xud5422ef2007-12-12 10:44:16 -08002394EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
David S. Miller9a7386e2011-02-24 01:44:12 -05002396static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397{
2398 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002399 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002400 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 }
2404
2405 return 0;
2406}
2407
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002408int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 unsigned short family)
2410{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002411 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002413 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2414 int npols = 0;
2415 int xfrm_nr;
2416 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002417 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002419 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002420 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Herbert Xud5422ef2007-12-12 10:44:16 -08002422 reverse = dir & ~XFRM_POLICY_MASK;
2423 dir &= XFRM_POLICY_MASK;
2424 fl_dir = policy_to_flow_dir(dir);
2425
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002426 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002427 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002429 }
2430
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002431 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433 /* First, check used SA against their selectors. */
2434 if (skb->sp) {
2435 int i;
2436
Weilong Chen9b7a7872013-12-24 09:43:46 +08002437 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002438 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002439 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002440 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
2444 }
2445
2446 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002447 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002448 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002449 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002450 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002451 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002452 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002455 if (!pol) {
2456 struct flow_cache_object *flo;
2457
2458 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2459 xfrm_policy_lookup, NULL);
2460 if (IS_ERR_OR_NULL(flo))
2461 pol = ERR_CAST(flo);
2462 else
2463 pol = container_of(flo, struct xfrm_policy, flo);
2464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002466 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002467 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002468 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002469 }
James Morris134b0fc2006-10-05 15:42:27 -05002470
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002471 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002472 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002473 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002474 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002475 return 0;
2476 }
2477 return 1;
2478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
James Morris9d729f72007-03-04 16:12:44 -08002480 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002482 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002483 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002484#ifdef CONFIG_XFRM_SUB_POLICY
2485 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002486 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002487 &fl, family,
2488 XFRM_POLICY_IN);
2489 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002490 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002491 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002492 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002493 }
James Morris9d729f72007-03-04 16:12:44 -08002494 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002495 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002496 }
2497 }
2498#endif
2499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 if (pol->action == XFRM_POLICY_ALLOW) {
2501 struct sec_path *sp;
2502 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002503 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002504 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002505 struct xfrm_tmpl **tpp = tp;
2506 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 int i, k;
2508
2509 if ((sp = skb->sp) == NULL)
2510 sp = &dummy;
2511
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002512 for (pi = 0; pi < npols; pi++) {
2513 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002514 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002515 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002516 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002517 }
2518 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002519 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002520 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002521 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002522 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2523 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2524 }
2525 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002526 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002527 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002528 tpp = stp;
2529 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 /* For each tunnel xfrm, find the first matching tmpl.
2532 * For each tmpl before that, find corresponding xfrm.
2533 * Order is _important_. Later we will implement
2534 * some barriers, but at the moment barriers
2535 * are implied between each two transformations.
2536 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002537 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2538 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002539 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002540 if (k < -1)
2541 /* "-2 - errored_index" returned */
2542 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002543 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 }
2547
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002548 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002549 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002553 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 return 1;
2555 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002556 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
2558reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002559 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002560reject_error:
2561 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 return 0;
2563}
2564EXPORT_SYMBOL(__xfrm_policy_check);
2565
2566int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2567{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002568 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002570 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002571 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002573 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002574 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002578 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002579
David S. Miller452edd52011-03-02 13:27:41 -08002580 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, 0);
2581 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002582 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002583 dst = NULL;
2584 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002585 skb_dst_set(skb, dst);
2586 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587}
2588EXPORT_SYMBOL(__xfrm_route_forward);
2589
David S. Millerd49c73c2006-08-13 18:55:53 -07002590/* Optimize later using cookies and generation ids. */
2591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2593{
David S. Millerd49c73c2006-08-13 18:55:53 -07002594 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002595 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2596 * get validated by dst_ops->check on every use. We do this
2597 * because when a normal route referenced by an XFRM dst is
2598 * obsoleted we do not go looking around for all parent
2599 * referencing XFRM dsts so that we can invalidate them. It
2600 * is just too much work. Instead we make the checks here on
2601 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002602 *
2603 * XFRM dst A --> IPv4 dst X
2604 *
2605 * X is the "xdst->route" of A (X is also the "dst->path" of A
2606 * in this example). If X is marked obsolete, "A" will not
2607 * notice. That's what we are validating here via the
2608 * stale_bundle() check.
2609 *
2610 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002611 * dst which causes it's ->obsolete field to be set to
2612 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2613 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002614 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002615 if (dst->obsolete < 0 && !stale_bundle(dst))
2616 return dst;
2617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 return NULL;
2619}
2620
2621static int stale_bundle(struct dst_entry *dst)
2622{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002623 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624}
2625
Herbert Xuaabc9762005-05-03 16:27:10 -07002626void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002629 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002630 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 dev_put(dev);
2632 }
2633}
Herbert Xuaabc9762005-05-03 16:27:10 -07002634EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636static void xfrm_link_failure(struct sk_buff *skb)
2637{
2638 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639}
2640
2641static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2642{
2643 if (dst) {
2644 if (dst->obsolete) {
2645 dst_release(dst);
2646 dst = NULL;
2647 }
2648 }
2649 return dst;
2650}
2651
Paul Mooree4c17212013-05-29 07:36:25 +00002652void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002653{
Fan Duca925cf2014-01-18 09:55:27 +08002654 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002655}
Paul Mooree4c17212013-05-29 07:36:25 +00002656EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002657
2658static void xfrm_garbage_collect_deferred(struct net *net)
2659{
Fan Duca925cf2014-01-18 09:55:27 +08002660 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002661}
2662
Herbert Xu25ee3282007-12-11 09:32:34 -08002663static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
2665 do {
2666 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2667 u32 pmtu, route_mtu_cached;
2668
2669 pmtu = dst_mtu(dst->child);
2670 xdst->child_mtu_cached = pmtu;
2671
2672 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2673
2674 route_mtu_cached = dst_mtu(xdst->route);
2675 xdst->route_mtu_cached = route_mtu_cached;
2676
2677 if (pmtu > route_mtu_cached)
2678 pmtu = route_mtu_cached;
2679
David S. Millerdefb3512010-12-08 21:16:57 -08002680 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 } while ((dst = dst->next));
2682}
2683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684/* Check that the bundle accepts the flow and its components are
2685 * still valid.
2686 */
2687
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002688static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689{
2690 struct dst_entry *dst = &first->u.dst;
2691 struct xfrm_dst *last;
2692 u32 mtu;
2693
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002694 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 (dst->dev && !netif_running(dst->dev)))
2696 return 0;
2697
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002698 if (dst->flags & DST_XFRM_QUEUE)
2699 return 1;
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 last = NULL;
2702
2703 do {
2704 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2707 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002708 if (xdst->xfrm_genid != dst->xfrm->genid)
2709 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002710 if (xdst->num_pols > 0 &&
2711 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002712 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
2714 mtu = dst_mtu(dst->child);
2715 if (xdst->child_mtu_cached != mtu) {
2716 last = xdst;
2717 xdst->child_mtu_cached = mtu;
2718 }
2719
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002720 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 return 0;
2722 mtu = dst_mtu(xdst->route);
2723 if (xdst->route_mtu_cached != mtu) {
2724 last = xdst;
2725 xdst->route_mtu_cached = mtu;
2726 }
2727
2728 dst = dst->child;
2729 } while (dst->xfrm);
2730
2731 if (likely(!last))
2732 return 1;
2733
2734 mtu = last->child_mtu_cached;
2735 for (;;) {
2736 dst = &last->u.dst;
2737
2738 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2739 if (mtu > last->route_mtu_cached)
2740 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002741 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
2743 if (last == first)
2744 break;
2745
Patrick McHardybd0bf072007-07-18 01:55:52 -07002746 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 last->child_mtu_cached = mtu;
2748 }
2749
2750 return 1;
2751}
2752
David S. Miller0dbaee32010-12-13 12:52:14 -08002753static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2754{
2755 return dst_metric_advmss(dst->path);
2756}
2757
Steffen Klassertebb762f2011-11-23 02:12:51 +00002758static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002759{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002760 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2761
2762 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002763}
2764
David S. Millerf894cbf2012-07-02 21:52:24 -07002765static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2766 struct sk_buff *skb,
2767 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002768{
David S. Millerf894cbf2012-07-02 21:52:24 -07002769 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002770}
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2773{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002774 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 int err = 0;
2776 if (unlikely(afinfo == NULL))
2777 return -EINVAL;
2778 if (unlikely(afinfo->family >= NPROTO))
2779 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002780 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2782 err = -ENOBUFS;
2783 else {
2784 struct dst_ops *dst_ops = afinfo->dst_ops;
2785 if (likely(dst_ops->kmem_cachep == NULL))
2786 dst_ops->kmem_cachep = xfrm_dst_cache;
2787 if (likely(dst_ops->check == NULL))
2788 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002789 if (likely(dst_ops->default_advmss == NULL))
2790 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002791 if (likely(dst_ops->mtu == NULL))
2792 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 if (likely(dst_ops->negative_advice == NULL))
2794 dst_ops->negative_advice = xfrm_negative_advice;
2795 if (likely(dst_ops->link_failure == NULL))
2796 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002797 if (likely(dst_ops->neigh_lookup == NULL))
2798 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002800 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002801 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002803 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002804
2805 rtnl_lock();
2806 for_each_net(net) {
2807 struct dst_ops *xfrm_dst_ops;
2808
2809 switch (afinfo->family) {
2810 case AF_INET:
2811 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2812 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002813#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002814 case AF_INET6:
2815 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2816 break;
2817#endif
2818 default:
2819 BUG();
2820 }
2821 *xfrm_dst_ops = *afinfo->dst_ops;
2822 }
2823 rtnl_unlock();
2824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return err;
2826}
2827EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2828
2829int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2830{
2831 int err = 0;
2832 if (unlikely(afinfo == NULL))
2833 return -EINVAL;
2834 if (unlikely(afinfo->family >= NPROTO))
2835 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002836 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2838 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2839 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002840 else
2841 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2842 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002844 spin_unlock(&xfrm_policy_afinfo_lock);
2845 if (!err) {
2846 struct dst_ops *dst_ops = afinfo->dst_ops;
2847
2848 synchronize_rcu();
2849
2850 dst_ops->kmem_cachep = NULL;
2851 dst_ops->check = NULL;
2852 dst_ops->negative_advice = NULL;
2853 dst_ops->link_failure = NULL;
2854 afinfo->garbage_collect = NULL;
2855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return err;
2857}
2858EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2859
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002860static void __net_init xfrm_dst_ops_init(struct net *net)
2861{
2862 struct xfrm_policy_afinfo *afinfo;
2863
Eric Dumazetef8531b2012-08-19 12:31:48 +02002864 rcu_read_lock();
2865 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002866 if (afinfo)
2867 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002868#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetef8531b2012-08-19 12:31:48 +02002869 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002870 if (afinfo)
2871 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2872#endif
Priyanka Jain418a99a2012-08-12 21:22:29 +00002873 rcu_read_unlock();
Herbert Xu546be242006-05-27 23:03:58 -07002874}
2875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2877{
Jiri Pirko351638e2013-05-28 01:30:21 +00002878 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 switch (event) {
2881 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002882 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 }
2884 return NOTIFY_DONE;
2885}
2886
2887static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002888 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889};
2890
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002891#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002892static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002893{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002894 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002895 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2896 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002897 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002898 rv = xfrm_proc_init(net);
2899 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002900 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002901 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002902}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002903
2904static void xfrm_statistics_fini(struct net *net)
2905{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002906 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002907 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002908}
2909#else
2910static int __net_init xfrm_statistics_init(struct net *net)
2911{
2912 return 0;
2913}
2914
2915static void xfrm_statistics_fini(struct net *net)
2916{
2917}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002918#endif
2919
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002920static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921{
David S. Miller2518c7c2006-08-24 04:45:07 -07002922 unsigned int hmask, sz;
2923 int dir;
2924
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002925 if (net_eq(net, &init_net))
2926 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002928 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002929 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
David S. Miller2518c7c2006-08-24 04:45:07 -07002931 hmask = 8 - 1;
2932 sz = (hmask+1) * sizeof(struct hlist_head);
2933
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002934 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2935 if (!net->xfrm.policy_byidx)
2936 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002937 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002938
2939 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2940 struct xfrm_policy_hash *htab;
2941
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002942 net->xfrm.policy_count[dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002943 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002944
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002945 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002946 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002947 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002948 goto out_bydst;
2949 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02002950 htab->dbits4 = 32;
2951 htab->sbits4 = 32;
2952 htab->dbits6 = 128;
2953 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07002954 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002955 net->xfrm.policy_hthresh.lbits4 = 32;
2956 net->xfrm.policy_hthresh.rbits4 = 32;
2957 net->xfrm.policy_hthresh.lbits6 = 128;
2958 net->xfrm.policy_hthresh.rbits6 = 128;
2959
2960 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07002961
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002962 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002963 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002964 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002965 if (net_eq(net, &init_net))
2966 register_netdevice_notifier(&xfrm_dev_notifier);
2967 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002968
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002969out_bydst:
2970 for (dir--; dir >= 0; dir--) {
2971 struct xfrm_policy_hash *htab;
2972
2973 htab = &net->xfrm.policy_bydst[dir];
2974 xfrm_hash_free(htab->table, sz);
2975 }
2976 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002977out_byidx:
2978 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979}
2980
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002981static void xfrm_policy_fini(struct net *net)
2982{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002983 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002984 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002985
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002986 flush_work(&net->xfrm.policy_hash_work);
2987#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09002988 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002989#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09002990 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002991
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002992 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002993
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002994 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002995 struct xfrm_policy_hash *htab;
2996
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002997 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002998
2999 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003000 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003001 WARN_ON(!hlist_empty(htab->table));
3002 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003003 }
3004
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003005 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003006 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3007 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003008}
3009
3010static int __net_init xfrm_net_init(struct net *net)
3011{
3012 int rv;
3013
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003014 rv = xfrm_statistics_init(net);
3015 if (rv < 0)
3016 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003017 rv = xfrm_state_init(net);
3018 if (rv < 0)
3019 goto out_state;
3020 rv = xfrm_policy_init(net);
3021 if (rv < 0)
3022 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08003023 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003024 rv = xfrm_sysctl_init(net);
3025 if (rv < 0)
3026 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01003027 rv = flow_cache_init(net);
3028 if (rv < 0)
3029 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08003030
3031 /* Initialize the per-net locks here */
3032 spin_lock_init(&net->xfrm.xfrm_state_lock);
3033 rwlock_init(&net->xfrm.xfrm_policy_lock);
Fan Du283bc9f2013-11-07 17:47:50 +08003034 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3035
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003036 return 0;
3037
Steffen Klassert4a93f502014-03-12 09:43:17 +01003038out:
3039 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003040out_sysctl:
3041 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003042out_policy:
3043 xfrm_state_fini(net);
3044out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003045 xfrm_statistics_fini(net);
3046out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003047 return rv;
3048}
3049
3050static void __net_exit xfrm_net_exit(struct net *net)
3051{
Steffen Klassert4a93f502014-03-12 09:43:17 +01003052 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003053 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003054 xfrm_policy_fini(net);
3055 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003056 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003057}
3058
3059static struct pernet_operations __net_initdata xfrm_net_ops = {
3060 .init = xfrm_net_init,
3061 .exit = xfrm_net_exit,
3062};
3063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064void __init xfrm_init(void)
3065{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003066 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 xfrm_input_init();
3068}
3069
Joy Lattenab5f5e82007-09-17 11:51:22 -07003070#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003071static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3072 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003073{
Paul Moore875179f2007-12-01 23:27:18 +11003074 struct xfrm_sec_ctx *ctx = xp->security;
3075 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003076
Paul Moore875179f2007-12-01 23:27:18 +11003077 if (ctx)
3078 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3079 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3080
Weilong Chen9b7a7872013-12-24 09:43:46 +08003081 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003082 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003083 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003084 if (sel->prefixlen_s != 32)
3085 audit_log_format(audit_buf, " src_prefixlen=%d",
3086 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003087 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003088 if (sel->prefixlen_d != 32)
3089 audit_log_format(audit_buf, " dst_prefixlen=%d",
3090 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003091 break;
3092 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003093 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003094 if (sel->prefixlen_s != 128)
3095 audit_log_format(audit_buf, " src_prefixlen=%d",
3096 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003097 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003098 if (sel->prefixlen_d != 128)
3099 audit_log_format(audit_buf, " dst_prefixlen=%d",
3100 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003101 break;
3102 }
3103}
3104
Tetsuo Handa2e710292014-04-22 21:48:30 +09003105void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003106{
3107 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003108
Paul Mooreafeb14b2007-12-21 14:58:11 -08003109 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003110 if (audit_buf == NULL)
3111 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003112 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003113 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003114 xfrm_audit_common_policyinfo(xp, audit_buf);
3115 audit_log_end(audit_buf);
3116}
3117EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3118
Paul Moore68277ac2007-12-20 20:49:33 -08003119void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003120 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003121{
3122 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003123
Paul Mooreafeb14b2007-12-21 14:58:11 -08003124 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003125 if (audit_buf == NULL)
3126 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003127 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003128 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003129 xfrm_audit_common_policyinfo(xp, audit_buf);
3130 audit_log_end(audit_buf);
3131}
3132EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3133#endif
3134
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003135#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003136static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3137 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003138{
3139 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3140 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003141 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3142 sel_cmp->family) &&
3143 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3144 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003145 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3146 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003147 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003148 }
3149 } else {
3150 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003151 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003152 }
3153 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003154 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003155}
3156
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003157static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3158 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003159{
3160 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003161 struct hlist_head *chain;
3162 u32 priority = ~0U;
3163
Fan Du283bc9f2013-11-07 17:47:50 +08003164 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
Fan Du8d549c42013-11-07 17:47:49 +08003165 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003166 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003167 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3168 pol->type == type) {
3169 ret = pol;
3170 priority = ret->priority;
3171 break;
3172 }
3173 }
Fan Du8d549c42013-11-07 17:47:49 +08003174 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003175 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003176 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3177 pol->type == type &&
3178 pol->priority < priority) {
3179 ret = pol;
3180 break;
3181 }
3182 }
3183
3184 if (ret)
3185 xfrm_pol_hold(ret);
3186
Fan Du283bc9f2013-11-07 17:47:50 +08003187 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003188
3189 return ret;
3190}
3191
David S. Millerdd701752011-02-24 00:21:08 -05003192static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003193{
3194 int match = 0;
3195
3196 if (t->mode == m->mode && t->id.proto == m->proto &&
3197 (m->reqid == 0 || t->reqid == m->reqid)) {
3198 switch (t->mode) {
3199 case XFRM_MODE_TUNNEL:
3200 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003201 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3202 m->old_family) &&
3203 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3204 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003205 match = 1;
3206 }
3207 break;
3208 case XFRM_MODE_TRANSPORT:
3209 /* in case of transport mode, template does not store
3210 any IP addresses, hence we just compare mode and
3211 protocol */
3212 match = 1;
3213 break;
3214 default:
3215 break;
3216 }
3217 }
3218 return match;
3219}
3220
3221/* update endpoint address(es) of template(s) */
3222static int xfrm_policy_migrate(struct xfrm_policy *pol,
3223 struct xfrm_migrate *m, int num_migrate)
3224{
3225 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003226 int i, j, n = 0;
3227
3228 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003229 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003230 /* target policy has been deleted */
3231 write_unlock_bh(&pol->lock);
3232 return -ENOENT;
3233 }
3234
3235 for (i = 0; i < pol->xfrm_nr; i++) {
3236 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3237 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3238 continue;
3239 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003240 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3241 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003242 continue;
3243 /* update endpoints */
3244 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3245 sizeof(pol->xfrm_vec[i].id.daddr));
3246 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3247 sizeof(pol->xfrm_vec[i].saddr));
3248 pol->xfrm_vec[i].encap_family = mp->new_family;
3249 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003250 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003251 }
3252 }
3253
3254 write_unlock_bh(&pol->lock);
3255
3256 if (!n)
3257 return -ENODATA;
3258
3259 return 0;
3260}
3261
David S. Millerdd701752011-02-24 00:21:08 -05003262static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003263{
3264 int i, j;
3265
3266 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3267 return -EINVAL;
3268
3269 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003270 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3271 m[i].old_family) &&
3272 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3273 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003274 return -EINVAL;
3275 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3276 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3277 return -EINVAL;
3278
3279 /* check if there is any duplicated entry */
3280 for (j = i + 1; j < num_migrate; j++) {
3281 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3282 sizeof(m[i].old_daddr)) &&
3283 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3284 sizeof(m[i].old_saddr)) &&
3285 m[i].proto == m[j].proto &&
3286 m[i].mode == m[j].mode &&
3287 m[i].reqid == m[j].reqid &&
3288 m[i].old_family == m[j].old_family)
3289 return -EINVAL;
3290 }
3291 }
3292
3293 return 0;
3294}
3295
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003296int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003297 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003298 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003299{
3300 int i, err, nx_cur = 0, nx_new = 0;
3301 struct xfrm_policy *pol = NULL;
3302 struct xfrm_state *x, *xc;
3303 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3304 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3305 struct xfrm_migrate *mp;
3306
3307 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3308 goto out;
3309
3310 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003311 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003312 err = -ENOENT;
3313 goto out;
3314 }
3315
3316 /* Stage 2 - find and update state(s) */
3317 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003318 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003319 x_cur[nx_cur] = x;
3320 nx_cur++;
3321 if ((xc = xfrm_state_migrate(x, mp))) {
3322 x_new[nx_new] = xc;
3323 nx_new++;
3324 } else {
3325 err = -ENODATA;
3326 goto restore_state;
3327 }
3328 }
3329 }
3330
3331 /* Stage 3 - update policy */
3332 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3333 goto restore_state;
3334
3335 /* Stage 4 - delete old state(s) */
3336 if (nx_cur) {
3337 xfrm_states_put(x_cur, nx_cur);
3338 xfrm_states_delete(x_cur, nx_cur);
3339 }
3340
3341 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003342 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003343
3344 xfrm_pol_put(pol);
3345
3346 return 0;
3347out:
3348 return err;
3349
3350restore_state:
3351 if (pol)
3352 xfrm_pol_put(pol);
3353 if (nx_cur)
3354 xfrm_states_put(x_cur, nx_cur);
3355 if (nx_new)
3356 xfrm_states_delete(x_new, nx_new);
3357
3358 return err;
3359}
David S. Millere610e672007-02-08 13:29:15 -08003360EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003361#endif