blob: e6ff7b4046ea6b3b443d0e2216e58cdae524c154 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569/* Generate new index... KAME seems to generate them ordered by cost
570 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf2013-11-07 17:47:48 +0800571static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 static u32 idx_generator;
574
575 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700576 struct hlist_head *list;
577 struct xfrm_policy *p;
578 u32 idx;
579 int found;
580
Fan Due682adf2013-11-07 17:47:48 +0800581 if (!index) {
582 idx = (idx_generator | dir);
583 idx_generator += 8;
584 } else {
585 idx = index;
586 index = 0;
587 }
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (idx == 0)
590 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800591 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700592 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800593 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700594 if (p->index == idx) {
595 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700599 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return idx;
601 }
602}
603
David S. Miller2518c7c2006-08-24 04:45:07 -0700604static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
605{
606 u32 *p1 = (u32 *) s1;
607 u32 *p2 = (u32 *) s2;
608 int len = sizeof(struct xfrm_selector) / sizeof(u32);
609 int i;
610
611 for (i = 0; i < len; i++) {
612 if (p1[i] != p2[i])
613 return 1;
614 }
615
616 return 0;
617}
618
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100619static void xfrm_policy_requeue(struct xfrm_policy *old,
620 struct xfrm_policy *new)
621{
622 struct xfrm_policy_queue *pq = &old->polq;
623 struct sk_buff_head list;
624
625 __skb_queue_head_init(&list);
626
627 spin_lock_bh(&pq->hold_queue.lock);
628 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200629 if (del_timer(&pq->hold_timer))
630 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100631 spin_unlock_bh(&pq->hold_queue.lock);
632
633 if (skb_queue_empty(&list))
634 return;
635
636 pq = &new->polq;
637
638 spin_lock_bh(&pq->hold_queue.lock);
639 skb_queue_splice(&list, &pq->hold_queue);
640 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200641 if (!mod_timer(&pq->hold_timer, jiffies))
642 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100643 spin_unlock_bh(&pq->hold_queue.lock);
644}
645
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100646static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
647 struct xfrm_policy *pol)
648{
649 u32 mark = policy->mark.v & policy->mark.m;
650
651 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
652 return true;
653
654 if ((mark & pol->mark.m) == pol->mark.v &&
655 policy->priority == pol->priority)
656 return true;
657
658 return false;
659}
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
662{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800663 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700664 struct xfrm_policy *pol;
665 struct xfrm_policy *delpol;
666 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800667 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Fan Du283bc9f2013-11-07 17:47:50 +0800669 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800670 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700671 delpol = NULL;
672 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800673 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800674 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700675 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100676 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800677 xfrm_sec_ctx_match(pol->security, policy->security) &&
678 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (excl) {
Fan Du283bc9f2013-11-07 17:47:50 +0800680 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return -EEXIST;
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 delpol = pol;
684 if (policy->priority > pol->priority)
685 continue;
686 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800687 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 continue;
689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (delpol)
691 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693 if (newpos)
Ken Helias1d023282014-08-06 16:09:16 -0700694 hlist_add_behind(&policy->bydst, newpos);
David S. Miller2518c7c2006-08-24 04:45:07 -0700695 else
696 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800698 net->xfrm.policy_count[dir]++;
Fan Duca925cf2014-01-18 09:55:27 +0800699 atomic_inc(&net->xfrm.flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800700
701 /* After previous checking, family can either be AF_INET or AF_INET6 */
702 if (policy->family == AF_INET)
703 rt_genid_bump_ipv4(net);
704 else
705 rt_genid_bump_ipv6(net);
706
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100707 if (delpol) {
708 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800709 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100710 }
Fan Due682adf2013-11-07 17:47:48 +0800711 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800712 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800713 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 policy->curlft.use_time = 0;
715 if (!mod_timer(&policy->timer, jiffies + HZ))
716 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800717 list_add(&policy->walk.all, &net->xfrm.policy_all);
Fan Du283bc9f2013-11-07 17:47:50 +0800718 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
David S. Miller9b78a822005-12-22 07:39:48 -0800720 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800722 else if (xfrm_bydst_should_resize(net, dir, NULL))
723 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 0;
726}
727EXPORT_SYMBOL(xfrm_policy_insert);
728
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000729struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
730 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800731 struct xfrm_sec_ctx *ctx, int delete,
732 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
David S. Miller2518c7c2006-08-24 04:45:07 -0700734 struct xfrm_policy *pol, *ret;
735 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Eric Parisef41aaa2007-03-07 15:37:58 -0800737 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800738 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800739 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700740 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800741 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700742 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000743 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700744 !selector_cmp(sel, &pol->selector) &&
745 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700747 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700748 *err = security_xfrm_policy_delete(
749 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800750 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800751 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800752 return pol;
753 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800754 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700755 }
756 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 break;
758 }
759 }
Fan Du283bc9f2013-11-07 17:47:50 +0800760 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000762 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700763 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700764 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
Trent Jaegerdf718372005-12-13 23:12:27 -0800766EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000768struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
769 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
David S. Miller2518c7c2006-08-24 04:45:07 -0700771 struct xfrm_policy *pol, *ret;
772 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Herbert Xub5505c62007-05-14 02:15:47 -0700774 *err = -ENOENT;
775 if (xfrm_policy_id2dir(id) != dir)
776 return NULL;
777
Eric Parisef41aaa2007-03-07 15:37:58 -0800778 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800779 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800780 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700781 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800782 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000783 if (pol->type == type && pol->index == id &&
784 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700786 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700787 *err = security_xfrm_policy_delete(
788 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800789 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800790 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800791 return pol;
792 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800793 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700794 }
795 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 break;
797 }
798 }
Fan Du283bc9f2013-11-07 17:47:50 +0800799 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000801 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700802 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700803 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805EXPORT_SYMBOL(xfrm_policy_byid);
806
Joy Latten4aa2e622007-06-04 19:05:57 -0400807#ifdef CONFIG_SECURITY_NETWORK_XFRM
808static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900809xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Joy Latten4aa2e622007-06-04 19:05:57 -0400811 int dir, err = 0;
812
813 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
814 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400815 int i;
816
Sasha Levinb67bfe02013-02-27 17:06:00 -0800817 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800818 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400819 if (pol->type != type)
820 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700821 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400822 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900823 xfrm_audit_policy_delete(pol, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400824 return err;
825 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900826 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800827 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800828 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800829 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400830 bydst) {
831 if (pol->type != type)
832 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700833 err = security_xfrm_policy_delete(
834 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400835 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700836 xfrm_audit_policy_delete(pol, 0,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900837 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400838 return err;
839 }
840 }
841 }
842 }
843 return err;
844}
845#else
846static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900847xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400848{
849 return 0;
850}
851#endif
852
Tetsuo Handa2e710292014-04-22 21:48:30 +0900853int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400854{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000855 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Fan Du283bc9f2013-11-07 17:47:50 +0800857 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400858
Tetsuo Handa2e710292014-04-22 21:48:30 +0900859 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400860 if (err)
861 goto out;
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700864 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800865 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700866
867 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800868 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800869 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700870 if (pol->type != type)
871 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000872 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800873 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000874 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Tetsuo Handa2e710292014-04-22 21:48:30 +0900876 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -0600877
David S. Miller2518c7c2006-08-24 04:45:07 -0700878 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Fan Du283bc9f2013-11-07 17:47:50 +0800880 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700881 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700883
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800884 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700885 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800886 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800887 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700888 bydst) {
889 if (pol->type != type)
890 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000891 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800892 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000893 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700894
Tetsuo Handa2e710292014-04-22 21:48:30 +0900895 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700896 xfrm_policy_kill(pol);
897
Fan Du283bc9f2013-11-07 17:47:50 +0800898 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700899 goto again2;
900 }
901 }
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000904 if (!cnt)
905 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400906out:
Fan Du283bc9f2013-11-07 17:47:50 +0800907 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400908 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910EXPORT_SYMBOL(xfrm_policy_flush);
911
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800912int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800913 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 void *data)
915{
Herbert Xu12a169e2008-10-01 07:03:24 -0700916 struct xfrm_policy *pol;
917 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -0800918 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Timo Teras4c563f72008-02-28 21:31:08 -0800920 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
921 walk->type != XFRM_POLICY_TYPE_ANY)
922 return -EINVAL;
923
Herbert Xu12a169e2008-10-01 07:03:24 -0700924 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -0800925 return 0;
926
Fan Du283bc9f2013-11-07 17:47:50 +0800927 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700928 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800929 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -0700930 else
931 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800932 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -0700933 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -0800934 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -0700935 pol = container_of(x, struct xfrm_policy, walk);
936 if (walk->type != XFRM_POLICY_TYPE_ANY &&
937 walk->type != pol->type)
938 continue;
939 error = func(pol, xfrm_policy_id2dir(pol->index),
940 walk->seq, data);
941 if (error) {
942 list_move_tail(&walk->walk.all, &x->all);
943 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -0800944 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700945 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700947 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800948 error = -ENOENT;
949 goto out;
950 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700951 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952out:
Fan Du283bc9f2013-11-07 17:47:50 +0800953 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return error;
955}
956EXPORT_SYMBOL(xfrm_policy_walk);
957
Herbert Xu12a169e2008-10-01 07:03:24 -0700958void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
959{
960 INIT_LIST_HEAD(&walk->walk.all);
961 walk->walk.dead = 1;
962 walk->type = type;
963 walk->seq = 0;
964}
965EXPORT_SYMBOL(xfrm_policy_walk_init);
966
Fan Du283bc9f2013-11-07 17:47:50 +0800967void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -0700968{
969 if (list_empty(&walk->walk.all))
970 return;
971
Fan Du283bc9f2013-11-07 17:47:50 +0800972 write_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -0700973 list_del(&walk->walk.all);
Fan Du283bc9f2013-11-07 17:47:50 +0800974 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700975}
976EXPORT_SYMBOL(xfrm_policy_walk_done);
977
James Morris134b0fc2006-10-05 15:42:27 -0500978/*
979 * Find policy to apply to this flow.
980 *
981 * Returns 0 if policy found, else an -errno.
982 */
David S. Millerf299d552011-02-24 01:23:30 -0500983static int xfrm_policy_match(const struct xfrm_policy *pol,
984 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -0700985 u8 type, u16 family, int dir)
986{
David S. Millerf299d552011-02-24 01:23:30 -0500987 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -0400988 int ret = -ESRCH;
989 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -0700990
991 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -0500992 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -0700993 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500994 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700995
996 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500997 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -0500998 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -0700999 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001000
James Morris134b0fc2006-10-05 15:42:27 -05001001 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001002}
1003
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001004static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001005 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001006 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
James Morris134b0fc2006-10-05 15:42:27 -05001008 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001009 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -05001010 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -07001011 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -07001012 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001013
1014 daddr = xfrm_flowi_daddr(fl, family);
1015 saddr = xfrm_flowi_saddr(fl, family);
1016 if (unlikely(!daddr || !saddr))
1017 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Fan Du283bc9f2013-11-07 17:47:50 +08001019 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001020 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001021 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001022 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001023 err = xfrm_policy_match(pol, fl, type, family, dir);
1024 if (err) {
1025 if (err == -ESRCH)
1026 continue;
1027 else {
1028 ret = ERR_PTR(err);
1029 goto fail;
1030 }
1031 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001032 ret = pol;
1033 priority = ret->priority;
1034 break;
1035 }
1036 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001037 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001038 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001039 err = xfrm_policy_match(pol, fl, type, family, dir);
1040 if (err) {
1041 if (err == -ESRCH)
1042 continue;
1043 else {
1044 ret = ERR_PTR(err);
1045 goto fail;
1046 }
1047 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001048 ret = pol;
1049 break;
1050 }
1051 }
David S. Milleracba48e2006-08-25 15:46:46 -07001052 if (ret)
1053 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -05001054fail:
Fan Du283bc9f2013-11-07 17:47:50 +08001055 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001056
David S. Miller2518c7c2006-08-24 04:45:07 -07001057 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001058}
1059
Timo Teräs80c802f2010-04-07 00:30:05 +00001060static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001061__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001062{
1063#ifdef CONFIG_XFRM_SUB_POLICY
1064 struct xfrm_policy *pol;
1065
1066 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1067 if (pol != NULL)
1068 return pol;
1069#endif
1070 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1071}
1072
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001073static int flow_to_policy_dir(int dir)
1074{
1075 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1076 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1077 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1078 return dir;
1079
1080 switch (dir) {
1081 default:
1082 case FLOW_DIR_IN:
1083 return XFRM_POLICY_IN;
1084 case FLOW_DIR_OUT:
1085 return XFRM_POLICY_OUT;
1086 case FLOW_DIR_FWD:
1087 return XFRM_POLICY_FWD;
1088 }
1089}
1090
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001091static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001092xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001093 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001094{
1095 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001096
1097 if (old_obj)
1098 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001099
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001100 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001101 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001102 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001103
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001104 /* Resolver returns two references:
1105 * one for cache and one for caller of flow_cache_lookup() */
1106 xfrm_pol_hold(pol);
1107
1108 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
Trent Jaegerdf718372005-12-13 23:12:27 -08001111static inline int policy_to_flow_dir(int dir)
1112{
1113 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001114 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1115 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1116 return dir;
1117 switch (dir) {
1118 default:
1119 case XFRM_POLICY_IN:
1120 return FLOW_DIR_IN;
1121 case XFRM_POLICY_OUT:
1122 return FLOW_DIR_OUT;
1123 case XFRM_POLICY_FWD:
1124 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001125 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001126}
1127
David S. Millerdee9f4b2011-02-22 18:44:31 -08001128static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir,
1129 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 struct xfrm_policy *pol;
Fan Du283bc9f2013-11-07 17:47:50 +08001132 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Fan Du283bc9f2013-11-07 17:47:50 +08001134 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if ((pol = sk->sk_policy[dir]) != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001136 bool match = xfrm_selector_match(&pol->selector, fl,
1137 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001138 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001139
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001140 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001141 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1142 pol = NULL;
1143 goto out;
1144 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001145 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001146 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001147 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001148 if (!err)
1149 xfrm_pol_hold(pol);
1150 else if (err == -ESRCH)
1151 pol = NULL;
1152 else
1153 pol = ERR_PTR(err);
1154 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 pol = NULL;
1156 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001157out:
Fan Du283bc9f2013-11-07 17:47:50 +08001158 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return pol;
1160}
1161
1162static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1163{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001164 struct net *net = xp_net(pol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001165 struct hlist_head *chain = policy_hash_bysel(net, &pol->selector,
David S. Miller2518c7c2006-08-24 04:45:07 -07001166 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001167
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001168 list_add(&pol->walk.all, &net->xfrm.policy_all);
David S. Miller2518c7c2006-08-24 04:45:07 -07001169 hlist_add_head(&pol->bydst, chain);
Alexey Dobriyane92303f2008-11-25 17:32:41 -08001170 hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001171 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001173
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001174 if (xfrm_bydst_should_resize(net, dir, NULL))
1175 schedule_work(&net->xfrm.policy_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
1178static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1179 int dir)
1180{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001181 struct net *net = xp_net(pol);
1182
David S. Miller2518c7c2006-08-24 04:45:07 -07001183 if (hlist_unhashed(&pol->bydst))
1184 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Steffen Klassert3a9016f2014-02-19 10:07:34 +01001186 hlist_del_init(&pol->bydst);
David S. Miller2518c7c2006-08-24 04:45:07 -07001187 hlist_del(&pol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -07001188 list_del(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001189 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001190
1191 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Herbert Xu4666faa2005-06-18 22:43:22 -07001194int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Fan Du283bc9f2013-11-07 17:47:50 +08001196 struct net *net = xp_net(pol);
1197
1198 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 pol = __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001200 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001205 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
David S. Millera70fcb02006-03-20 19:18:52 -08001207EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1210{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001211 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 struct xfrm_policy *old_pol;
1213
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001214#ifdef CONFIG_XFRM_SUB_POLICY
1215 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1216 return -EINVAL;
1217#endif
1218
Fan Du283bc9f2013-11-07 17:47:50 +08001219 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 old_pol = sk->sk_policy[dir];
1221 sk->sk_policy[dir] = pol;
1222 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001223 pol->curlft.add_time = get_seconds();
Fan Due682adf2013-11-07 17:47:48 +08001224 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1226 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001227 if (old_pol) {
1228 if (pol)
1229 xfrm_policy_requeue(old_pol, pol);
1230
Timo Teräsea2dea92010-03-31 00:17:05 +00001231 /* Unlinking succeeds always. This is the only function
1232 * allowed to delete or replace socket policy.
1233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001235 }
Fan Du283bc9f2013-11-07 17:47:50 +08001236 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 if (old_pol) {
1239 xfrm_policy_kill(old_pol);
1240 }
1241 return 0;
1242}
1243
David S. Millerd3e40a92011-02-24 01:25:41 -05001244static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001246 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001247 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 if (newp) {
1250 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001251 if (security_xfrm_policy_clone(old->security,
1252 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001253 kfree(newp);
1254 return NULL; /* ENOMEM */
1255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 newp->lft = old->lft;
1257 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001258 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 newp->action = old->action;
1260 newp->flags = old->flags;
1261 newp->xfrm_nr = old->xfrm_nr;
1262 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001263 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 memcpy(newp->xfrm_vec, old->xfrm_vec,
1265 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Fan Du283bc9f2013-11-07 17:47:50 +08001266 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001268 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 xfrm_pol_put(newp);
1270 }
1271 return newp;
1272}
1273
1274int __xfrm_sk_clone_policy(struct sock *sk)
1275{
1276 struct xfrm_policy *p0 = sk->sk_policy[0],
1277 *p1 = sk->sk_policy[1];
1278
1279 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1280 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1281 return -ENOMEM;
1282 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1283 return -ENOMEM;
1284 return 0;
1285}
1286
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001287static int
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001288xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001289 unsigned short family)
1290{
1291 int err;
1292 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1293
1294 if (unlikely(afinfo == NULL))
1295 return -EINVAL;
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001296 err = afinfo->get_saddr(net, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001297 xfrm_policy_put_afinfo(afinfo);
1298 return err;
1299}
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301/* Resolve list of templates for the flow, given policy. */
1302
1303static int
David S. Millera6c2e612011-02-22 18:35:39 -08001304xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1305 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001307 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 int nx;
1309 int i, error;
1310 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1311 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001312 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Weilong Chen9b7a7872013-12-24 09:43:46 +08001314 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 struct xfrm_state *x;
1316 xfrm_address_t *remote = daddr;
1317 xfrm_address_t *local = saddr;
1318 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1319
Joakim Koskela48b8d782007-07-26 00:08:42 -07001320 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1321 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 remote = &tmpl->id.daddr;
1323 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001324 if (xfrm_addr_any(local, tmpl->encap_family)) {
1325 error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001326 if (error)
1327 goto fail;
1328 local = &tmp;
1329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331
1332 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1333
1334 if (x && x->km.state == XFRM_STATE_VALID) {
1335 xfrm[nx++] = x;
1336 daddr = remote;
1337 saddr = local;
1338 continue;
1339 }
1340 if (x) {
1341 error = (x->km.state == XFRM_STATE_ERROR ?
1342 -EINVAL : -EAGAIN);
1343 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001344 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001345 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 if (!tmpl->optional)
1349 goto fail;
1350 }
1351 return nx;
1352
1353fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001354 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 xfrm_state_put(xfrm[nx]);
1356 return error;
1357}
1358
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001359static int
David S. Millera6c2e612011-02-22 18:35:39 -08001360xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1361 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001362{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001363 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1364 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001365 int cnx = 0;
1366 int error;
1367 int ret;
1368 int i;
1369
1370 for (i = 0; i < npols; i++) {
1371 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1372 error = -ENOBUFS;
1373 goto fail;
1374 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001375
1376 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001377 if (ret < 0) {
1378 error = ret;
1379 goto fail;
1380 } else
1381 cnx += ret;
1382 }
1383
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001384 /* found states are sorted for outbound processing */
1385 if (npols > 1)
1386 xfrm_state_sort(xfrm, tpp, cnx, family);
1387
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001388 return cnx;
1389
1390 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001391 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001392 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001393 return error;
1394
1395}
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397/* Check that the bundle accepts the flow and its components are
1398 * still valid.
1399 */
1400
David S. Miller05d84022011-02-22 17:47:10 -08001401static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001402{
1403 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1404 int tos;
1405
1406 if (!afinfo)
1407 return -EINVAL;
1408
1409 tos = afinfo->get_tos(fl);
1410
1411 xfrm_policy_put_afinfo(afinfo);
1412
1413 return tos;
1414}
1415
Timo Teräs80c802f2010-04-07 00:30:05 +00001416static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1417{
1418 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1419 struct dst_entry *dst = &xdst->u.dst;
1420
1421 if (xdst->route == NULL) {
1422 /* Dummy bundle - if it has xfrms we were not
1423 * able to build bundle as template resolution failed.
1424 * It means we need to try again resolving. */
1425 if (xdst->num_xfrms > 0)
1426 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001427 } else if (dst->flags & DST_XFRM_QUEUE) {
1428 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001429 } else {
1430 /* Real bundle */
1431 if (stale_bundle(dst))
1432 return NULL;
1433 }
1434
1435 dst_hold(dst);
1436 return flo;
1437}
1438
1439static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1440{
1441 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1442 struct dst_entry *dst = &xdst->u.dst;
1443
1444 if (!xdst->route)
1445 return 0;
1446 if (stale_bundle(dst))
1447 return 0;
1448
1449 return 1;
1450}
1451
1452static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1453{
1454 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1455 struct dst_entry *dst = &xdst->u.dst;
1456
1457 dst_free(dst);
1458}
1459
1460static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1461 .get = xfrm_bundle_flo_get,
1462 .check = xfrm_bundle_flo_check,
1463 .delete = xfrm_bundle_flo_delete,
1464};
1465
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001466static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001467{
1468 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001469 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001470 struct xfrm_dst *xdst;
1471
1472 if (!afinfo)
1473 return ERR_PTR(-EINVAL);
1474
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001475 switch (family) {
1476 case AF_INET:
1477 dst_ops = &net->xfrm.xfrm4_dst_ops;
1478 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001479#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001480 case AF_INET6:
1481 dst_ops = &net->xfrm.xfrm6_dst_ops;
1482 break;
1483#endif
1484 default:
1485 BUG();
1486 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001487 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001488
Madalin Bucurd4cae562011-09-26 07:04:36 +00001489 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001490 struct dst_entry *dst = &xdst->u.dst;
1491
1492 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001493 xdst->flo.ops = &xfrm_bundle_fc_ops;
Patrick McHardy9d7b0fc2012-08-20 02:56:56 -07001494 if (afinfo->init_dst)
1495 afinfo->init_dst(net, xdst);
Madalin Bucurd4cae562011-09-26 07:04:36 +00001496 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001497 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001498
Madalin Bucurd4cae562011-09-26 07:04:36 +00001499 xfrm_policy_put_afinfo(afinfo);
1500
Herbert Xu25ee3282007-12-11 09:32:34 -08001501 return xdst;
1502}
1503
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001504static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1505 int nfheader_len)
1506{
1507 struct xfrm_policy_afinfo *afinfo =
1508 xfrm_policy_get_afinfo(dst->ops->family);
1509 int err;
1510
1511 if (!afinfo)
1512 return -EINVAL;
1513
1514 err = afinfo->init_path(path, dst, nfheader_len);
1515
1516 xfrm_policy_put_afinfo(afinfo);
1517
1518 return err;
1519}
1520
Herbert Xu87c1e122010-03-02 02:51:56 +00001521static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001522 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001523{
1524 struct xfrm_policy_afinfo *afinfo =
1525 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1526 int err;
1527
1528 if (!afinfo)
1529 return -EINVAL;
1530
Herbert Xu87c1e122010-03-02 02:51:56 +00001531 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001532
1533 xfrm_policy_put_afinfo(afinfo);
1534
1535 return err;
1536}
1537
Timo Teräs80c802f2010-04-07 00:30:05 +00001538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1540 * all the metrics... Shortly, bundle a bundle.
1541 */
1542
Herbert Xu25ee3282007-12-11 09:32:34 -08001543static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1544 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001545 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001546 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001548 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001549 unsigned long now = jiffies;
1550 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001551 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001552 struct dst_entry *dst_prev = NULL;
1553 struct dst_entry *dst0 = NULL;
1554 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001556 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001557 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001558 int trailer_len = 0;
1559 int tos;
1560 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001561 xfrm_address_t saddr, daddr;
1562
1563 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001564
1565 tos = xfrm_get_tos(fl, family);
1566 err = tos;
1567 if (tos < 0)
1568 goto put_states;
1569
1570 dst_hold(dst);
1571
1572 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001573 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001574 struct dst_entry *dst1 = &xdst->u.dst;
1575
1576 err = PTR_ERR(xdst);
1577 if (IS_ERR(xdst)) {
1578 dst_release(dst);
1579 goto put_states;
1580 }
1581
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001582 if (xfrm[i]->sel.family == AF_UNSPEC) {
1583 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1584 xfrm_af2proto(family));
1585 if (!inner_mode) {
1586 err = -EAFNOSUPPORT;
1587 dst_release(dst);
1588 goto put_states;
1589 }
1590 } else
1591 inner_mode = xfrm[i]->inner_mode;
1592
Herbert Xu25ee3282007-12-11 09:32:34 -08001593 if (!dst_prev)
1594 dst0 = dst1;
1595 else {
1596 dst_prev->child = dst_clone(dst1);
1597 dst1->flags |= DST_NOHASH;
1598 }
1599
1600 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001601 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001602
1603 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1604 family = xfrm[i]->props.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001605 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1606 family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001607 err = PTR_ERR(dst);
1608 if (IS_ERR(dst))
1609 goto put_states;
1610 } else
1611 dst_hold(dst);
1612
1613 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001614 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001615
David S. Millerf5b0a872012-07-19 12:31:33 -07001616 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001617 dst1->flags |= DST_HOST;
1618 dst1->lastuse = now;
1619
1620 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001621 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001622
1623 dst1->next = dst_prev;
1624 dst_prev = dst1;
1625
1626 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001627 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1628 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001629 trailer_len += xfrm[i]->props.trailer_len;
1630 }
1631
1632 dst_prev->child = dst;
1633 dst0->path = dst;
1634
1635 err = -ENODEV;
1636 dev = dst->dev;
1637 if (!dev)
1638 goto free_dst;
1639
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001640 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001641 xfrm_init_pmtu(dst_prev);
1642
1643 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1644 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1645
Herbert Xu87c1e122010-03-02 02:51:56 +00001646 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001647 if (err)
1648 goto free_dst;
1649
1650 dst_prev->header_len = header_len;
1651 dst_prev->trailer_len = trailer_len;
1652 header_len -= xdst->u.dst.xfrm->props.header_len;
1653 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1654 }
1655
1656out:
1657 return dst0;
1658
1659put_states:
1660 for (; i < nx; i++)
1661 xfrm_state_put(xfrm[i]);
1662free_dst:
1663 if (dst0)
1664 dst_free(dst0);
1665 dst0 = ERR_PTR(err);
1666 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
1668
Ying Xueda7c2242014-01-08 10:26:39 +08001669#ifdef CONFIG_XFRM_SUB_POLICY
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001670static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001671{
1672 if (!*target) {
1673 *target = kmalloc(size, GFP_ATOMIC);
1674 if (!*target)
1675 return -ENOMEM;
1676 }
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001677
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001678 memcpy(*target, src, size);
1679 return 0;
1680}
Ying Xueda7c2242014-01-08 10:26:39 +08001681#endif
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001682
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001683static int xfrm_dst_update_parent(struct dst_entry *dst,
1684 const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001685{
1686#ifdef CONFIG_XFRM_SUB_POLICY
1687 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1688 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1689 sel, sizeof(*sel));
1690#else
1691 return 0;
1692#endif
1693}
1694
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001695static int xfrm_dst_update_origin(struct dst_entry *dst,
1696 const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001697{
1698#ifdef CONFIG_XFRM_SUB_POLICY
1699 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1700 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1701#else
1702 return 0;
1703#endif
1704}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
David S. Miller73ff93c2011-02-22 18:33:42 -08001706static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001707 struct xfrm_policy **pols,
1708 int *num_pols, int *num_xfrms)
1709{
1710 int i;
1711
1712 if (*num_pols == 0 || !pols[0]) {
1713 *num_pols = 0;
1714 *num_xfrms = 0;
1715 return 0;
1716 }
1717 if (IS_ERR(pols[0]))
1718 return PTR_ERR(pols[0]);
1719
1720 *num_xfrms = pols[0]->xfrm_nr;
1721
1722#ifdef CONFIG_XFRM_SUB_POLICY
1723 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1724 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1725 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1726 XFRM_POLICY_TYPE_MAIN,
1727 fl, family,
1728 XFRM_POLICY_OUT);
1729 if (pols[1]) {
1730 if (IS_ERR(pols[1])) {
1731 xfrm_pols_put(pols, *num_pols);
1732 return PTR_ERR(pols[1]);
1733 }
Weilong Chen02d08922013-12-24 09:43:48 +08001734 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001735 (*num_xfrms) += pols[1]->xfrm_nr;
1736 }
1737 }
1738#endif
1739 for (i = 0; i < *num_pols; i++) {
1740 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1741 *num_xfrms = -1;
1742 break;
1743 }
1744 }
1745
1746 return 0;
1747
1748}
1749
1750static struct xfrm_dst *
1751xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001752 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001753 struct dst_entry *dst_orig)
1754{
1755 struct net *net = xp_net(pols[0]);
1756 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1757 struct dst_entry *dst;
1758 struct xfrm_dst *xdst;
1759 int err;
1760
1761 /* Try to instantiate a bundle */
1762 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001763 if (err <= 0) {
1764 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001765 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1766 return ERR_PTR(err);
1767 }
1768
1769 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1770 if (IS_ERR(dst)) {
1771 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1772 return ERR_CAST(dst);
1773 }
1774
1775 xdst = (struct xfrm_dst *)dst;
1776 xdst->num_xfrms = err;
1777 if (num_pols > 1)
1778 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1779 else
1780 err = xfrm_dst_update_origin(dst, fl);
1781 if (unlikely(err)) {
1782 dst_free(dst);
1783 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1784 return ERR_PTR(err);
1785 }
1786
1787 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001788 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001789 xdst->policy_genid = atomic_read(&pols[0]->genid);
1790
1791 return xdst;
1792}
1793
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001794static void xfrm_policy_queue_process(unsigned long arg)
1795{
1796 int err = 0;
1797 struct sk_buff *skb;
1798 struct sock *sk;
1799 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001800 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1801 struct xfrm_policy_queue *pq = &pol->polq;
1802 struct flowi fl;
1803 struct sk_buff_head list;
1804
1805 spin_lock(&pq->hold_queue.lock);
1806 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001807 if (!skb) {
1808 spin_unlock(&pq->hold_queue.lock);
1809 goto out;
1810 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001811 dst = skb_dst(skb);
1812 sk = skb->sk;
1813 xfrm_decode_session(skb, &fl, dst->ops->family);
1814 spin_unlock(&pq->hold_queue.lock);
1815
1816 dst_hold(dst->path);
1817 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1818 sk, 0);
1819 if (IS_ERR(dst))
1820 goto purge_queue;
1821
1822 if (dst->flags & DST_XFRM_QUEUE) {
1823 dst_release(dst);
1824
1825 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1826 goto purge_queue;
1827
1828 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001829 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1830 xfrm_pol_hold(pol);
1831 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001832 }
1833
1834 dst_release(dst);
1835
1836 __skb_queue_head_init(&list);
1837
1838 spin_lock(&pq->hold_queue.lock);
1839 pq->timeout = 0;
1840 skb_queue_splice_init(&pq->hold_queue, &list);
1841 spin_unlock(&pq->hold_queue.lock);
1842
1843 while (!skb_queue_empty(&list)) {
1844 skb = __skb_dequeue(&list);
1845
1846 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1847 dst_hold(skb_dst(skb)->path);
1848 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1849 &fl, skb->sk, 0);
1850 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001851 kfree_skb(skb);
1852 continue;
1853 }
1854
1855 nf_reset(skb);
1856 skb_dst_drop(skb);
1857 skb_dst_set(skb, dst);
1858
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001859 err = dst_output(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001860 }
1861
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001862out:
1863 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001864 return;
1865
1866purge_queue:
1867 pq->timeout = 0;
1868 xfrm_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001869 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001870}
1871
Eric Dumazetaad88722014-04-15 13:47:15 -04001872static int xdst_queue_output(struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001873{
1874 unsigned long sched_next;
1875 struct dst_entry *dst = skb_dst(skb);
1876 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001877 struct xfrm_policy *pol = xdst->pols[0];
1878 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001879 const struct sk_buff *fclone = skb + 1;
1880
1881 if (unlikely(skb->fclone == SKB_FCLONE_ORIG &&
1882 fclone->fclone == SKB_FCLONE_CLONE)) {
1883 kfree_skb(skb);
1884 return 0;
1885 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001886
1887 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1888 kfree_skb(skb);
1889 return -EAGAIN;
1890 }
1891
1892 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001893
1894 spin_lock_bh(&pq->hold_queue.lock);
1895
1896 if (!pq->timeout)
1897 pq->timeout = XFRM_QUEUE_TMO_MIN;
1898
1899 sched_next = jiffies + pq->timeout;
1900
1901 if (del_timer(&pq->hold_timer)) {
1902 if (time_before(pq->hold_timer.expires, sched_next))
1903 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001904 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001905 }
1906
1907 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001908 if (!mod_timer(&pq->hold_timer, sched_next))
1909 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001910
1911 spin_unlock_bh(&pq->hold_queue.lock);
1912
1913 return 0;
1914}
1915
1916static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
1917 struct dst_entry *dst,
1918 const struct flowi *fl,
1919 int num_xfrms,
1920 u16 family)
1921{
1922 int err;
1923 struct net_device *dev;
1924 struct dst_entry *dst1;
1925 struct xfrm_dst *xdst;
1926
1927 xdst = xfrm_alloc_dst(net, family);
1928 if (IS_ERR(xdst))
1929 return xdst;
1930
Steffen Klassert5b8ef342013-08-27 13:43:30 +02001931 if (net->xfrm.sysctl_larval_drop || num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001932 return xdst;
1933
1934 dst1 = &xdst->u.dst;
1935 dst_hold(dst);
1936 xdst->route = dst;
1937
1938 dst_copy_metrics(dst1, dst);
1939
1940 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1941 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
1942 dst1->lastuse = jiffies;
1943
1944 dst1->input = dst_discard;
1945 dst1->output = xdst_queue_output;
1946
1947 dst_hold(dst);
1948 dst1->child = dst;
1949 dst1->path = dst;
1950
1951 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
1952
1953 err = -ENODEV;
1954 dev = dst->dev;
1955 if (!dev)
1956 goto free_dst;
1957
1958 err = xfrm_fill_dst(xdst, dev, fl);
1959 if (err)
1960 goto free_dst;
1961
1962out:
1963 return xdst;
1964
1965free_dst:
1966 dst_release(dst1);
1967 xdst = ERR_PTR(err);
1968 goto out;
1969}
1970
Timo Teräs80c802f2010-04-07 00:30:05 +00001971static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001972xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00001973 struct flow_cache_object *oldflo, void *ctx)
1974{
1975 struct dst_entry *dst_orig = (struct dst_entry *)ctx;
1976 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1977 struct xfrm_dst *xdst, *new_xdst;
1978 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
1979
1980 /* Check if the policies from old bundle are usable */
1981 xdst = NULL;
1982 if (oldflo) {
1983 xdst = container_of(oldflo, struct xfrm_dst, flo);
1984 num_pols = xdst->num_pols;
1985 num_xfrms = xdst->num_xfrms;
1986 pol_dead = 0;
1987 for (i = 0; i < num_pols; i++) {
1988 pols[i] = xdst->pols[i];
1989 pol_dead |= pols[i]->walk.dead;
1990 }
1991 if (pol_dead) {
1992 dst_free(&xdst->u.dst);
1993 xdst = NULL;
1994 num_pols = 0;
1995 num_xfrms = 0;
1996 oldflo = NULL;
1997 }
1998 }
1999
2000 /* Resolve policies to use if we couldn't get them from
2001 * previous cache entry */
2002 if (xdst == NULL) {
2003 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00002004 pols[0] = __xfrm_policy_lookup(net, fl, family,
2005 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00002006 err = xfrm_expand_policies(fl, family, pols,
2007 &num_pols, &num_xfrms);
2008 if (err < 0)
2009 goto inc_error;
2010 if (num_pols == 0)
2011 return NULL;
2012 if (num_xfrms <= 0)
2013 goto make_dummy_bundle;
2014 }
2015
2016 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family, dst_orig);
2017 if (IS_ERR(new_xdst)) {
2018 err = PTR_ERR(new_xdst);
2019 if (err != -EAGAIN)
2020 goto error;
2021 if (oldflo == NULL)
2022 goto make_dummy_bundle;
2023 dst_hold(&xdst->u.dst);
2024 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00002025 } else if (new_xdst == NULL) {
2026 num_xfrms = 0;
2027 if (oldflo == NULL)
2028 goto make_dummy_bundle;
2029 xdst->num_xfrms = 0;
2030 dst_hold(&xdst->u.dst);
2031 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002032 }
2033
2034 /* Kill the previous bundle */
2035 if (xdst) {
2036 /* The policies were stolen for newly generated bundle */
2037 xdst->num_pols = 0;
2038 dst_free(&xdst->u.dst);
2039 }
2040
2041 /* Flow cache does not have reference, it dst_free()'s,
2042 * but we do need to return one reference for original caller */
2043 dst_hold(&new_xdst->u.dst);
2044 return &new_xdst->flo;
2045
2046make_dummy_bundle:
2047 /* We found policies, but there's no bundles to instantiate:
2048 * either because the policy blocks, has no transformations or
2049 * we could not build template (no xfrm_states).*/
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002050 xdst = xfrm_create_dummy_bundle(net, dst_orig, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002051 if (IS_ERR(xdst)) {
2052 xfrm_pols_put(pols, num_pols);
2053 return ERR_CAST(xdst);
2054 }
2055 xdst->num_pols = num_pols;
2056 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002057 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002058
2059 dst_hold(&xdst->u.dst);
2060 return &xdst->flo;
2061
2062inc_error:
2063 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2064error:
2065 if (xdst != NULL)
2066 dst_free(&xdst->u.dst);
2067 else
2068 xfrm_pols_put(pols, num_pols);
2069 return ERR_PTR(err);
2070}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
David S. Miller2774c132011-03-01 14:59:04 -08002072static struct dst_entry *make_blackhole(struct net *net, u16 family,
2073 struct dst_entry *dst_orig)
2074{
2075 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2076 struct dst_entry *ret;
2077
2078 if (!afinfo) {
2079 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002080 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002081 } else {
2082 ret = afinfo->blackhole_route(net, dst_orig);
2083 }
2084 xfrm_policy_put_afinfo(afinfo);
2085
2086 return ret;
2087}
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089/* Main function: finds/creates a bundle for given flow.
2090 *
2091 * At the moment we eat a raw IP route. Mostly to speed up lookups
2092 * on interfaces with disabled IPsec.
2093 */
David S. Miller452edd52011-03-02 13:27:41 -08002094struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2095 const struct flowi *fl,
2096 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002098 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002099 struct flow_cache_object *flo;
2100 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002101 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002102 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002103 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002104 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002105
Timo Teräs80c802f2010-04-07 00:30:05 +00002106 dst = NULL;
2107 xdst = NULL;
2108 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002109
Thomas Graff7944fb2007-08-25 13:46:55 -07002110 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002111 num_pols = 1;
2112 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2113 err = xfrm_expand_policies(fl, family, pols,
2114 &num_pols, &num_xfrms);
2115 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002116 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002117
2118 if (num_pols) {
2119 if (num_xfrms <= 0) {
2120 drop_pols = num_pols;
2121 goto no_transform;
2122 }
2123
2124 xdst = xfrm_resolve_and_create_bundle(
2125 pols, num_pols, fl,
2126 family, dst_orig);
2127 if (IS_ERR(xdst)) {
2128 xfrm_pols_put(pols, num_pols);
2129 err = PTR_ERR(xdst);
2130 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002131 } else if (xdst == NULL) {
2132 num_xfrms = 0;
2133 drop_pols = num_pols;
2134 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002135 }
2136
Steffen Klassertb7eea452014-06-18 12:34:21 +02002137 dst_hold(&xdst->u.dst);
2138 xdst->u.dst.flags |= DST_NOCACHE;
Timo Teräs80c802f2010-04-07 00:30:05 +00002139 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002140 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Timo Teräs80c802f2010-04-07 00:30:05 +00002143 if (xdst == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002145 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002146 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002147 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Timo Teräs80c802f2010-04-07 00:30:05 +00002149 flo = flow_cache_lookup(net, fl, family, dir,
2150 xfrm_bundle_lookup, dst_orig);
2151 if (flo == NULL)
2152 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002153 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002154 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002155 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002156 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002157 xdst = container_of(flo, struct xfrm_dst, flo);
2158
2159 num_pols = xdst->num_pols;
2160 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002161 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002162 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
2164
Timo Teräs80c802f2010-04-07 00:30:05 +00002165 dst = &xdst->u.dst;
2166 if (route == NULL && num_xfrms > 0) {
2167 /* The only case when xfrm_bundle_lookup() returns a
2168 * bundle with null route, is when the template could
2169 * not be resolved. It means policies are there, but
2170 * bundle could not be created, since we don't yet
2171 * have the xfrm_state's. We need to wait for KM to
2172 * negotiate new SA's or bail out with error.*/
2173 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002174 dst_release(dst);
Timo Terasa1aa3482010-05-15 23:49:26 -07002175 xfrm_pols_put(pols, drop_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002176 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
David S. Miller2774c132011-03-01 14:59:04 -08002177
David S. Miller452edd52011-03-02 13:27:41 -08002178 return make_blackhole(net, family, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002179 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002180
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002181 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002182
2183 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2184 goto error;
2185 }
2186
2187no_transform:
2188 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002189 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Timo Teräs80c802f2010-04-07 00:30:05 +00002191 if ((flags & XFRM_LOOKUP_ICMP) &&
2192 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2193 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002194 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002195 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002196
Timo Teräs80c802f2010-04-07 00:30:05 +00002197 for (i = 0; i < num_pols; i++)
2198 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002199
Timo Teräs80c802f2010-04-07 00:30:05 +00002200 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002202 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002203 err = -EPERM;
2204 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002205 } else if (num_xfrms > 0) {
2206 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002207 dst_release(dst_orig);
2208 } else {
2209 /* Flow passes untransformed */
2210 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002211 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002213ok:
2214 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002215 if (dst && dst->xfrm &&
2216 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2217 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002218 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Timo Teräs80c802f2010-04-07 00:30:05 +00002220nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002221 if (!(flags & XFRM_LOOKUP_ICMP)) {
2222 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002223 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002224 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002225 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002227 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002228dropdst:
2229 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002230 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002231 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233EXPORT_SYMBOL(xfrm_lookup);
2234
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002235static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002236xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002237{
2238 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002239
2240 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2241 return 0;
2242 x = skb->sp->xvec[idx];
2243 if (!x->type->reject)
2244 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002245 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002246}
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248/* When skb is transformed back to its "native" form, we have to
2249 * check policy restrictions. At the moment we make this in maximally
2250 * stupid way. Shame on me. :-) Of course, connected sockets must
2251 * have policy cached at them.
2252 */
2253
2254static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002255xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 unsigned short family)
2257{
2258 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002259 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 return x->id.proto == tmpl->id.proto &&
2261 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2262 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2263 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002264 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002265 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002266 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2267 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268}
2269
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002270/*
2271 * 0 or more than 0 is returned when validation is succeeded (either bypass
2272 * because of optional transport mode, or next index of the mathced secpath
2273 * state with the template.
2274 * -1 is returned when no matching template is found.
2275 * Otherwise "-2 - errored_index" is returned.
2276 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002278xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 unsigned short family)
2280{
2281 int idx = start;
2282
2283 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002284 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return start;
2286 } else
2287 start = -1;
2288 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002289 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002291 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2292 if (start == -1)
2293 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 }
2297 return start;
2298}
2299
Herbert Xud5422ef2007-12-12 10:44:16 -08002300int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2301 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
2303 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002304 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
2306 if (unlikely(afinfo == NULL))
2307 return -EAFNOSUPPORT;
2308
Herbert Xud5422ef2007-12-12 10:44:16 -08002309 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002310 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002312 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
Herbert Xud5422ef2007-12-12 10:44:16 -08002314EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
David S. Miller9a7386e2011-02-24 01:44:12 -05002316static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
2318 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002319 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002320 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
2324
2325 return 0;
2326}
2327
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002328int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 unsigned short family)
2330{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002331 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002333 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2334 int npols = 0;
2335 int xfrm_nr;
2336 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002337 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002339 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002340 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Herbert Xud5422ef2007-12-12 10:44:16 -08002342 reverse = dir & ~XFRM_POLICY_MASK;
2343 dir &= XFRM_POLICY_MASK;
2344 fl_dir = policy_to_flow_dir(dir);
2345
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002346 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002347 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002349 }
2350
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002351 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 /* First, check used SA against their selectors. */
2354 if (skb->sp) {
2355 int i;
2356
Weilong Chen9b7a7872013-12-24 09:43:46 +08002357 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002358 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002359 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002360 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 }
2364 }
2365
2366 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002367 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002368 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002369 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002370 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002371 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002372 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002375 if (!pol) {
2376 struct flow_cache_object *flo;
2377
2378 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2379 xfrm_policy_lookup, NULL);
2380 if (IS_ERR_OR_NULL(flo))
2381 pol = ERR_CAST(flo);
2382 else
2383 pol = container_of(flo, struct xfrm_policy, flo);
2384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002386 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002387 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002388 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002389 }
James Morris134b0fc2006-10-05 15:42:27 -05002390
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002391 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002392 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002393 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002394 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002395 return 0;
2396 }
2397 return 1;
2398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
James Morris9d729f72007-03-04 16:12:44 -08002400 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002402 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002403 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002404#ifdef CONFIG_XFRM_SUB_POLICY
2405 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002406 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002407 &fl, family,
2408 XFRM_POLICY_IN);
2409 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002410 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002411 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002412 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002413 }
James Morris9d729f72007-03-04 16:12:44 -08002414 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002415 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002416 }
2417 }
2418#endif
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 if (pol->action == XFRM_POLICY_ALLOW) {
2421 struct sec_path *sp;
2422 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002423 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002424 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002425 struct xfrm_tmpl **tpp = tp;
2426 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 int i, k;
2428
2429 if ((sp = skb->sp) == NULL)
2430 sp = &dummy;
2431
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002432 for (pi = 0; pi < npols; pi++) {
2433 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002434 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002435 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002436 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002437 }
2438 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002439 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002440 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002441 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002442 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2443 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2444 }
2445 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002446 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002447 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002448 tpp = stp;
2449 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 /* For each tunnel xfrm, find the first matching tmpl.
2452 * For each tmpl before that, find corresponding xfrm.
2453 * Order is _important_. Later we will implement
2454 * some barriers, but at the moment barriers
2455 * are implied between each two transformations.
2456 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002457 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2458 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002459 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002460 if (k < -1)
2461 /* "-2 - errored_index" returned */
2462 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002463 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 }
2467
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002468 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002469 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002473 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 return 1;
2475 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002476 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002479 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002480reject_error:
2481 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 return 0;
2483}
2484EXPORT_SYMBOL(__xfrm_policy_check);
2485
2486int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2487{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002488 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002490 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002491 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002493 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002494 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002498 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002499
David S. Miller452edd52011-03-02 13:27:41 -08002500 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, 0);
2501 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002502 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002503 dst = NULL;
2504 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002505 skb_dst_set(skb, dst);
2506 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507}
2508EXPORT_SYMBOL(__xfrm_route_forward);
2509
David S. Millerd49c73c2006-08-13 18:55:53 -07002510/* Optimize later using cookies and generation ids. */
2511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2513{
David S. Millerd49c73c2006-08-13 18:55:53 -07002514 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002515 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2516 * get validated by dst_ops->check on every use. We do this
2517 * because when a normal route referenced by an XFRM dst is
2518 * obsoleted we do not go looking around for all parent
2519 * referencing XFRM dsts so that we can invalidate them. It
2520 * is just too much work. Instead we make the checks here on
2521 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002522 *
2523 * XFRM dst A --> IPv4 dst X
2524 *
2525 * X is the "xdst->route" of A (X is also the "dst->path" of A
2526 * in this example). If X is marked obsolete, "A" will not
2527 * notice. That's what we are validating here via the
2528 * stale_bundle() check.
2529 *
2530 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002531 * dst which causes it's ->obsolete field to be set to
2532 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2533 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002534 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002535 if (dst->obsolete < 0 && !stale_bundle(dst))
2536 return dst;
2537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return NULL;
2539}
2540
2541static int stale_bundle(struct dst_entry *dst)
2542{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002543 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544}
2545
Herbert Xuaabc9762005-05-03 16:27:10 -07002546void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002549 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002550 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 dev_put(dev);
2552 }
2553}
Herbert Xuaabc9762005-05-03 16:27:10 -07002554EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
2556static void xfrm_link_failure(struct sk_buff *skb)
2557{
2558 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559}
2560
2561static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2562{
2563 if (dst) {
2564 if (dst->obsolete) {
2565 dst_release(dst);
2566 dst = NULL;
2567 }
2568 }
2569 return dst;
2570}
2571
Paul Mooree4c17212013-05-29 07:36:25 +00002572void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002573{
Fan Duca925cf2014-01-18 09:55:27 +08002574 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002575}
Paul Mooree4c17212013-05-29 07:36:25 +00002576EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002577
2578static void xfrm_garbage_collect_deferred(struct net *net)
2579{
Fan Duca925cf2014-01-18 09:55:27 +08002580 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002581}
2582
Herbert Xu25ee3282007-12-11 09:32:34 -08002583static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584{
2585 do {
2586 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2587 u32 pmtu, route_mtu_cached;
2588
2589 pmtu = dst_mtu(dst->child);
2590 xdst->child_mtu_cached = pmtu;
2591
2592 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2593
2594 route_mtu_cached = dst_mtu(xdst->route);
2595 xdst->route_mtu_cached = route_mtu_cached;
2596
2597 if (pmtu > route_mtu_cached)
2598 pmtu = route_mtu_cached;
2599
David S. Millerdefb3512010-12-08 21:16:57 -08002600 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 } while ((dst = dst->next));
2602}
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604/* Check that the bundle accepts the flow and its components are
2605 * still valid.
2606 */
2607
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002608static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609{
2610 struct dst_entry *dst = &first->u.dst;
2611 struct xfrm_dst *last;
2612 u32 mtu;
2613
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002614 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 (dst->dev && !netif_running(dst->dev)))
2616 return 0;
2617
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002618 if (dst->flags & DST_XFRM_QUEUE)
2619 return 1;
2620
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 last = NULL;
2622
2623 do {
2624 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2625
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2627 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002628 if (xdst->xfrm_genid != dst->xfrm->genid)
2629 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002630 if (xdst->num_pols > 0 &&
2631 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002632 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634 mtu = dst_mtu(dst->child);
2635 if (xdst->child_mtu_cached != mtu) {
2636 last = xdst;
2637 xdst->child_mtu_cached = mtu;
2638 }
2639
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002640 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 return 0;
2642 mtu = dst_mtu(xdst->route);
2643 if (xdst->route_mtu_cached != mtu) {
2644 last = xdst;
2645 xdst->route_mtu_cached = mtu;
2646 }
2647
2648 dst = dst->child;
2649 } while (dst->xfrm);
2650
2651 if (likely(!last))
2652 return 1;
2653
2654 mtu = last->child_mtu_cached;
2655 for (;;) {
2656 dst = &last->u.dst;
2657
2658 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2659 if (mtu > last->route_mtu_cached)
2660 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002661 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
2663 if (last == first)
2664 break;
2665
Patrick McHardybd0bf072007-07-18 01:55:52 -07002666 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 last->child_mtu_cached = mtu;
2668 }
2669
2670 return 1;
2671}
2672
David S. Miller0dbaee32010-12-13 12:52:14 -08002673static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2674{
2675 return dst_metric_advmss(dst->path);
2676}
2677
Steffen Klassertebb762f2011-11-23 02:12:51 +00002678static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002679{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002680 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2681
2682 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002683}
2684
David S. Millerf894cbf2012-07-02 21:52:24 -07002685static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2686 struct sk_buff *skb,
2687 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002688{
David S. Millerf894cbf2012-07-02 21:52:24 -07002689 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002690}
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2693{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002694 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 int err = 0;
2696 if (unlikely(afinfo == NULL))
2697 return -EINVAL;
2698 if (unlikely(afinfo->family >= NPROTO))
2699 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002700 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2702 err = -ENOBUFS;
2703 else {
2704 struct dst_ops *dst_ops = afinfo->dst_ops;
2705 if (likely(dst_ops->kmem_cachep == NULL))
2706 dst_ops->kmem_cachep = xfrm_dst_cache;
2707 if (likely(dst_ops->check == NULL))
2708 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002709 if (likely(dst_ops->default_advmss == NULL))
2710 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002711 if (likely(dst_ops->mtu == NULL))
2712 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 if (likely(dst_ops->negative_advice == NULL))
2714 dst_ops->negative_advice = xfrm_negative_advice;
2715 if (likely(dst_ops->link_failure == NULL))
2716 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002717 if (likely(dst_ops->neigh_lookup == NULL))
2718 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002720 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002721 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002723 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002724
2725 rtnl_lock();
2726 for_each_net(net) {
2727 struct dst_ops *xfrm_dst_ops;
2728
2729 switch (afinfo->family) {
2730 case AF_INET:
2731 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2732 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002733#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002734 case AF_INET6:
2735 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2736 break;
2737#endif
2738 default:
2739 BUG();
2740 }
2741 *xfrm_dst_ops = *afinfo->dst_ops;
2742 }
2743 rtnl_unlock();
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 return err;
2746}
2747EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2748
2749int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2750{
2751 int err = 0;
2752 if (unlikely(afinfo == NULL))
2753 return -EINVAL;
2754 if (unlikely(afinfo->family >= NPROTO))
2755 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002756 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2758 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2759 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002760 else
2761 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2762 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002764 spin_unlock(&xfrm_policy_afinfo_lock);
2765 if (!err) {
2766 struct dst_ops *dst_ops = afinfo->dst_ops;
2767
2768 synchronize_rcu();
2769
2770 dst_ops->kmem_cachep = NULL;
2771 dst_ops->check = NULL;
2772 dst_ops->negative_advice = NULL;
2773 dst_ops->link_failure = NULL;
2774 afinfo->garbage_collect = NULL;
2775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 return err;
2777}
2778EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2779
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002780static void __net_init xfrm_dst_ops_init(struct net *net)
2781{
2782 struct xfrm_policy_afinfo *afinfo;
2783
Eric Dumazetef8531b2012-08-19 12:31:48 +02002784 rcu_read_lock();
2785 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002786 if (afinfo)
2787 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002788#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetef8531b2012-08-19 12:31:48 +02002789 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002790 if (afinfo)
2791 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2792#endif
Priyanka Jain418a99a2012-08-12 21:22:29 +00002793 rcu_read_unlock();
Herbert Xu546be242006-05-27 23:03:58 -07002794}
2795
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2797{
Jiri Pirko351638e2013-05-28 01:30:21 +00002798 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002799
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 switch (event) {
2801 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002802 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 }
2804 return NOTIFY_DONE;
2805}
2806
2807static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002808 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809};
2810
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002811#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002812static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002813{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002814 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002815 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2816 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002817 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002818 rv = xfrm_proc_init(net);
2819 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002820 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002821 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002822}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002823
2824static void xfrm_statistics_fini(struct net *net)
2825{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002826 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002827 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002828}
2829#else
2830static int __net_init xfrm_statistics_init(struct net *net)
2831{
2832 return 0;
2833}
2834
2835static void xfrm_statistics_fini(struct net *net)
2836{
2837}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002838#endif
2839
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002840static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
David S. Miller2518c7c2006-08-24 04:45:07 -07002842 unsigned int hmask, sz;
2843 int dir;
2844
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002845 if (net_eq(net, &init_net))
2846 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002848 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002849 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
David S. Miller2518c7c2006-08-24 04:45:07 -07002851 hmask = 8 - 1;
2852 sz = (hmask+1) * sizeof(struct hlist_head);
2853
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002854 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2855 if (!net->xfrm.policy_byidx)
2856 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002857 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002858
2859 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2860 struct xfrm_policy_hash *htab;
2861
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002862 net->xfrm.policy_count[dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002863 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002864
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002865 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002866 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002867 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002868 goto out_bydst;
2869 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02002870 htab->dbits4 = 32;
2871 htab->sbits4 = 32;
2872 htab->dbits6 = 128;
2873 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07002874 }
2875
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002876 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002877 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002878 if (net_eq(net, &init_net))
2879 register_netdevice_notifier(&xfrm_dev_notifier);
2880 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002881
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002882out_bydst:
2883 for (dir--; dir >= 0; dir--) {
2884 struct xfrm_policy_hash *htab;
2885
2886 htab = &net->xfrm.policy_bydst[dir];
2887 xfrm_hash_free(htab->table, sz);
2888 }
2889 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002890out_byidx:
2891 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892}
2893
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002894static void xfrm_policy_fini(struct net *net)
2895{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002896 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002897 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002898
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002899 flush_work(&net->xfrm.policy_hash_work);
2900#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09002901 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002902#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09002903 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002904
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002905 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002906
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002907 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002908 struct xfrm_policy_hash *htab;
2909
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002910 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002911
2912 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01002913 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002914 WARN_ON(!hlist_empty(htab->table));
2915 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002916 }
2917
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002918 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002919 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2920 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002921}
2922
2923static int __net_init xfrm_net_init(struct net *net)
2924{
2925 int rv;
2926
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002927 rv = xfrm_statistics_init(net);
2928 if (rv < 0)
2929 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002930 rv = xfrm_state_init(net);
2931 if (rv < 0)
2932 goto out_state;
2933 rv = xfrm_policy_init(net);
2934 if (rv < 0)
2935 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002936 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002937 rv = xfrm_sysctl_init(net);
2938 if (rv < 0)
2939 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01002940 rv = flow_cache_init(net);
2941 if (rv < 0)
2942 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08002943
2944 /* Initialize the per-net locks here */
2945 spin_lock_init(&net->xfrm.xfrm_state_lock);
2946 rwlock_init(&net->xfrm.xfrm_policy_lock);
Fan Du283bc9f2013-11-07 17:47:50 +08002947 mutex_init(&net->xfrm.xfrm_cfg_mutex);
2948
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002949 return 0;
2950
Steffen Klassert4a93f502014-03-12 09:43:17 +01002951out:
2952 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002953out_sysctl:
2954 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002955out_policy:
2956 xfrm_state_fini(net);
2957out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002958 xfrm_statistics_fini(net);
2959out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002960 return rv;
2961}
2962
2963static void __net_exit xfrm_net_exit(struct net *net)
2964{
Steffen Klassert4a93f502014-03-12 09:43:17 +01002965 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002966 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002967 xfrm_policy_fini(net);
2968 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002969 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002970}
2971
2972static struct pernet_operations __net_initdata xfrm_net_ops = {
2973 .init = xfrm_net_init,
2974 .exit = xfrm_net_exit,
2975};
2976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977void __init xfrm_init(void)
2978{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002979 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 xfrm_input_init();
2981}
2982
Joy Lattenab5f5e82007-09-17 11:51:22 -07002983#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08002984static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2985 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002986{
Paul Moore875179f2007-12-01 23:27:18 +11002987 struct xfrm_sec_ctx *ctx = xp->security;
2988 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002989
Paul Moore875179f2007-12-01 23:27:18 +11002990 if (ctx)
2991 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2992 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2993
Weilong Chen9b7a7872013-12-24 09:43:46 +08002994 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002995 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002996 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002997 if (sel->prefixlen_s != 32)
2998 audit_log_format(audit_buf, " src_prefixlen=%d",
2999 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003000 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003001 if (sel->prefixlen_d != 32)
3002 audit_log_format(audit_buf, " dst_prefixlen=%d",
3003 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003004 break;
3005 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003006 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003007 if (sel->prefixlen_s != 128)
3008 audit_log_format(audit_buf, " src_prefixlen=%d",
3009 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003010 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003011 if (sel->prefixlen_d != 128)
3012 audit_log_format(audit_buf, " dst_prefixlen=%d",
3013 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003014 break;
3015 }
3016}
3017
Tetsuo Handa2e710292014-04-22 21:48:30 +09003018void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003019{
3020 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003021
Paul Mooreafeb14b2007-12-21 14:58:11 -08003022 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003023 if (audit_buf == NULL)
3024 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003025 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003026 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003027 xfrm_audit_common_policyinfo(xp, audit_buf);
3028 audit_log_end(audit_buf);
3029}
3030EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3031
Paul Moore68277ac2007-12-20 20:49:33 -08003032void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003033 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003034{
3035 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003036
Paul Mooreafeb14b2007-12-21 14:58:11 -08003037 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003038 if (audit_buf == NULL)
3039 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003040 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003041 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003042 xfrm_audit_common_policyinfo(xp, audit_buf);
3043 audit_log_end(audit_buf);
3044}
3045EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3046#endif
3047
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003048#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003049static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3050 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003051{
3052 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3053 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003054 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3055 sel_cmp->family) &&
3056 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3057 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003058 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3059 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003060 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003061 }
3062 } else {
3063 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003064 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003065 }
3066 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003067 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003068}
3069
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003070static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3071 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003072{
3073 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003074 struct hlist_head *chain;
3075 u32 priority = ~0U;
3076
Fan Du283bc9f2013-11-07 17:47:50 +08003077 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
Fan Du8d549c42013-11-07 17:47:49 +08003078 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003079 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003080 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3081 pol->type == type) {
3082 ret = pol;
3083 priority = ret->priority;
3084 break;
3085 }
3086 }
Fan Du8d549c42013-11-07 17:47:49 +08003087 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003088 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003089 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3090 pol->type == type &&
3091 pol->priority < priority) {
3092 ret = pol;
3093 break;
3094 }
3095 }
3096
3097 if (ret)
3098 xfrm_pol_hold(ret);
3099
Fan Du283bc9f2013-11-07 17:47:50 +08003100 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003101
3102 return ret;
3103}
3104
David S. Millerdd701752011-02-24 00:21:08 -05003105static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003106{
3107 int match = 0;
3108
3109 if (t->mode == m->mode && t->id.proto == m->proto &&
3110 (m->reqid == 0 || t->reqid == m->reqid)) {
3111 switch (t->mode) {
3112 case XFRM_MODE_TUNNEL:
3113 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003114 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3115 m->old_family) &&
3116 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3117 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003118 match = 1;
3119 }
3120 break;
3121 case XFRM_MODE_TRANSPORT:
3122 /* in case of transport mode, template does not store
3123 any IP addresses, hence we just compare mode and
3124 protocol */
3125 match = 1;
3126 break;
3127 default:
3128 break;
3129 }
3130 }
3131 return match;
3132}
3133
3134/* update endpoint address(es) of template(s) */
3135static int xfrm_policy_migrate(struct xfrm_policy *pol,
3136 struct xfrm_migrate *m, int num_migrate)
3137{
3138 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003139 int i, j, n = 0;
3140
3141 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003142 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003143 /* target policy has been deleted */
3144 write_unlock_bh(&pol->lock);
3145 return -ENOENT;
3146 }
3147
3148 for (i = 0; i < pol->xfrm_nr; i++) {
3149 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3150 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3151 continue;
3152 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003153 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3154 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003155 continue;
3156 /* update endpoints */
3157 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3158 sizeof(pol->xfrm_vec[i].id.daddr));
3159 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3160 sizeof(pol->xfrm_vec[i].saddr));
3161 pol->xfrm_vec[i].encap_family = mp->new_family;
3162 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003163 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003164 }
3165 }
3166
3167 write_unlock_bh(&pol->lock);
3168
3169 if (!n)
3170 return -ENODATA;
3171
3172 return 0;
3173}
3174
David S. Millerdd701752011-02-24 00:21:08 -05003175static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003176{
3177 int i, j;
3178
3179 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3180 return -EINVAL;
3181
3182 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003183 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3184 m[i].old_family) &&
3185 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3186 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003187 return -EINVAL;
3188 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3189 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3190 return -EINVAL;
3191
3192 /* check if there is any duplicated entry */
3193 for (j = i + 1; j < num_migrate; j++) {
3194 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3195 sizeof(m[i].old_daddr)) &&
3196 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3197 sizeof(m[i].old_saddr)) &&
3198 m[i].proto == m[j].proto &&
3199 m[i].mode == m[j].mode &&
3200 m[i].reqid == m[j].reqid &&
3201 m[i].old_family == m[j].old_family)
3202 return -EINVAL;
3203 }
3204 }
3205
3206 return 0;
3207}
3208
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003209int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003210 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003211 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003212{
3213 int i, err, nx_cur = 0, nx_new = 0;
3214 struct xfrm_policy *pol = NULL;
3215 struct xfrm_state *x, *xc;
3216 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3217 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3218 struct xfrm_migrate *mp;
3219
3220 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3221 goto out;
3222
3223 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003224 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003225 err = -ENOENT;
3226 goto out;
3227 }
3228
3229 /* Stage 2 - find and update state(s) */
3230 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003231 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003232 x_cur[nx_cur] = x;
3233 nx_cur++;
3234 if ((xc = xfrm_state_migrate(x, mp))) {
3235 x_new[nx_new] = xc;
3236 nx_new++;
3237 } else {
3238 err = -ENODATA;
3239 goto restore_state;
3240 }
3241 }
3242 }
3243
3244 /* Stage 3 - update policy */
3245 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3246 goto restore_state;
3247
3248 /* Stage 4 - delete old state(s) */
3249 if (nx_cur) {
3250 xfrm_states_put(x_cur, nx_cur);
3251 xfrm_states_delete(x_cur, nx_cur);
3252 }
3253
3254 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003255 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003256
3257 xfrm_pol_put(pol);
3258
3259 return 0;
3260out:
3261 return err;
3262
3263restore_state:
3264 if (pol)
3265 xfrm_pol_put(pol);
3266 if (nx_cur)
3267 xfrm_states_put(x_cur, nx_cur);
3268 if (nx_new)
3269 xfrm_states_delete(x_new, nx_new);
3270
3271 return err;
3272}
David S. Millere610e672007-02-08 13:29:15 -08003273EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003274#endif